2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
24 #include "php_http_std_defs.h"
25 #include "php_http_api.h"
26 #include "php_http_message_api.h"
27 #include "php_http_headers_api.h"
28 #include "php_http_send_api.h"
29 #include "php_http_request_api.h"
30 #include "php_http_url_api.h"
32 #include "phpstr/phpstr.h"
34 ZEND_EXTERN_MODULE_GLOBALS(http
);
36 #define http_message_headers_cb _http_message_headers_cb
37 static void _http_message_headers_cb(const char *http_line
, HashTable
**headers
, void **message TSRMLS_DC
)
41 http_message
*new, *old
= (http_message
*) *message
;
43 if (crlf
= strstr(http_line
, HTTP_CRLF
)) {
44 line_length
= crlf
- http_line
;
46 line_length
= strlen(http_line
);
49 if (old
->type
|| zend_hash_num_elements(&old
->hdrs
) || PHPSTR_LEN(old
)) {
50 new = http_message_new();
54 *headers
= &new->hdrs
;
59 while (isspace(http_line
[line_length
-1])) --line_length
;
62 if (!strncmp(http_line
, "HTTP/1.", lenof("HTTP/1."))) {
63 new->type
= HTTP_MSG_RESPONSE
;
64 new->info
.response
.http_version
= atof(http_line
+ lenof("HTTP/"));
65 new->info
.response
.code
= atoi(http_line
+ lenof("HTTP/1.1 "));
68 if (!strncmp(http_line
+ line_length
- lenof("HTTP/1.1"), "HTTP/1.", lenof("HTTP/1."))) {
69 const char *method_sep_uri
= strchr(http_line
, ' ');
70 new->type
= HTTP_MSG_REQUEST
;
71 new->info
.request
.http_version
= atof(http_line
+ line_length
- lenof("1.1"));
72 new->info
.request
.method
= estrndup(http_line
, method_sep_uri
- http_line
);
73 new->info
.request
.URI
= estrndup(method_sep_uri
+ 1, http_line
+ line_length
- method_sep_uri
- 1 - lenof(" HTTP/1.1"));
77 #define http_message_init_type _http_message_init_type
78 static inline void _http_message_init_type(http_message
*message
, http_message_type type
)
80 switch (message
->type
= type
)
82 case HTTP_MSG_RESPONSE
:
83 message
->info
.response
.http_version
= .0;
84 message
->info
.response
.code
= 0;
87 case HTTP_MSG_REQUEST
:
88 message
->info
.request
.http_version
= .0;
89 message
->info
.request
.method
= NULL
;
90 message
->info
.request
.URI
= NULL
;
99 PHP_HTTP_API http_message
*_http_message_init_ex(http_message
*message
, http_message_type type
)
102 message
= ecalloc(1, sizeof(http_message
));
105 http_message_init_type(message
, type
);
106 message
->parent
= NULL
;
107 phpstr_init(&message
->body
);
108 zend_hash_init(&message
->hdrs
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
114 PHP_HTTP_API
void _http_message_set_type(http_message
*message
, http_message_type type
)
116 /* just act if different */
117 if (type
!= message
->type
) {
119 /* free request info */
120 if (message
->type
== HTTP_MSG_REQUEST
) {
121 if (message
->info
.request
.method
) {
122 efree(message
->info
.request
.method
);
124 if (message
->info
.request
.URI
) {
125 efree(message
->info
.request
.URI
);
130 http_message_init_type(message
, type
);
134 PHP_HTTP_API http_message
*_http_message_parse_ex(http_message
*msg
, const char *message
, size_t message_length TSRMLS_DC
)
137 zend_bool free_msg
= msg
? 0 : 1;
139 if (message_length
< HTTP_MSG_MIN_SIZE
) {
147 msg
= http_message_init(msg
);
149 if (SUCCESS
!= http_parse_headers_cb(message
, &msg
->hdrs
, 1, http_message_headers_cb
, (void **) &msg
)) {
151 http_message_free(msg
);
156 /* header parsing stops at CRLF CRLF */
157 if (body
= strstr(message
, HTTP_CRLF HTTP_CRLF
)) {
159 const char *continue_at
= NULL
;
161 body
+= lenof(HTTP_CRLF HTTP_CRLF
);
163 /* message has content-length header */
164 if (c
= http_message_header(msg
, "Content-Length")) {
165 long len
= atol(Z_STRVAL_P(c
));
166 phpstr_from_string_ex(PHPSTR(msg
), body
, len
);
167 continue_at
= body
+ len
;
170 /* message has chunked transfer encoding */
171 if (c
= http_message_header(msg
, "Transfer-Encoding")) {
172 if (!strcasecmp("chunked", Z_STRVAL_P(c
))) {
176 /* decode and replace Transfer-Encoding with Content-Length header */
177 if (continue_at
= http_chunked_decode(body
, message
+ message_length
- body
, &decoded
, &decoded_len
)) {
178 phpstr_from_string_ex(PHPSTR(msg
), decoded
, decoded_len
);
184 spprintf(&tmp
, 0, "%lu", (ulong
) decoded_len
);
186 ZVAL_STRING(len
, tmp
, 0);
188 zend_hash_del(&msg
->hdrs
, "Transfer-Encoding", sizeof("Transfer-Encoding"));
189 zend_hash_add(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"), (void *) &len
, sizeof(zval
*), NULL
);
195 /* message has content-range header */
196 if (c
= http_message_header(msg
, "Content-Range")) {
197 ulong start
= 0, end
= 0;
199 sscanf(Z_STRVAL_P(c
), "bytes=%lu-%lu", &start
, &end
);
201 phpstr_from_string_ex(PHPSTR(msg
), body
, (size_t) (end
- start
));
202 continue_at
= body
+ (end
- start
);
206 /* no headers that indicate content length */
207 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
208 phpstr_from_string_ex(PHPSTR(msg
), body
, message
+ message_length
- body
);
213 /* check for following messages */
215 while (isspace(*continue_at
)) ++continue_at
;
216 if (continue_at
< (message
+ message_length
)) {
217 http_message
*next
= NULL
, *most
= NULL
;
219 /* set current message to parent of most parent following messages and return deepest */
220 if (most
= next
= http_message_parse(continue_at
, message
+ message_length
- continue_at
)) {
221 while (most
->parent
) most
= most
->parent
;
232 PHP_HTTP_API
void _http_message_tostring(http_message
*msg
, char **string
, size_t *length
)
239 phpstr_init_ex(&str
, 4096, 0);
243 case HTTP_MSG_REQUEST
:
244 phpstr_appendf(&str
, "%s %s HTTP/%1.1f" HTTP_CRLF
,
245 msg
->info
.request
.method
,
246 msg
->info
.request
.URI
,
247 msg
->info
.request
.http_version
);
250 case HTTP_MSG_RESPONSE
:
251 phpstr_appendf(&str
, "HTTP/%1.1f %d" HTTP_CRLF
,
252 msg
->info
.response
.http_version
,
253 msg
->info
.response
.code
);
261 FOREACH_HASH_KEYVAL(&msg
->hdrs
, key
, idx
, header
) {
263 zval
**single_header
;
265 switch (Z_TYPE_PP(header
))
268 phpstr_appendf(&str
, "%s: %s" HTTP_CRLF
, key
, Z_STRVAL_PP(header
));
272 FOREACH_VAL(*header
, single_header
) {
273 phpstr_appendf(&str
, "%s: %s" HTTP_CRLF
, key
, Z_STRVAL_PP(single_header
));
282 if (PHPSTR_LEN(msg
)) {
283 phpstr_appends(&str
, HTTP_CRLF
);
284 phpstr_append(&str
, PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
285 phpstr_appends(&str
, HTTP_CRLF
);
288 data
= phpstr_data(&str
, string
, length
);
296 PHP_HTTP_API
void _http_message_serialize(http_message
*message
, char **string
, size_t *length
)
305 http_message_tostring(message
, &buf
, &len
);
306 phpstr_prepend(&str
, buf
, len
);
308 } while (message
= message
->parent
);
310 buf
= phpstr_data(&str
, string
, length
);
318 PHP_HTTP_API STATUS
_http_message_send(http_message
*message TSRMLS_DC
)
322 switch (message
->type
)
324 case HTTP_MSG_RESPONSE
:
330 FOREACH_HASH_KEYVAL(&message
->hdrs
, key
, idx
, val
) {
333 spprintf(&header
, 0, "%s: %s", key
, Z_STRVAL_PP(val
));
334 http_send_header(header
);
339 rs
= SUCCESS
== http_send_status(message
->info
.response
.code
) &&
340 SUCCESS
== http_send_data(PHPSTR_VAL(message
), PHPSTR_LEN(message
)) ?
345 case HTTP_MSG_REQUEST
:
347 #ifdef HTTP_HAVE_CURL
349 zval
**zhost
, options
, headers
;
351 array_init(&options
);
352 array_init(&headers
);
353 zend_hash_copy(Z_ARRVAL(headers
), &message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
354 add_assoc_zval(&options
, "headers", &headers
);
356 /* check host header */
357 if (SUCCESS
== zend_hash_find(&message
->hdrs
, "Host", sizeof("Host"), (void **) &zhost
)) {
358 char *colon
= NULL
, *host
= NULL
;
363 if (colon
= strchr(Z_STRVAL_PP(zhost
), ':')) {
364 port
= atoi(colon
+ 1);
365 host
= estrndup(Z_STRVAL_PP(zhost
), host_len
= (Z_STRVAL_PP(zhost
) - colon
- 1));
367 host
= estrndup(Z_STRVAL_PP(zhost
), host_len
= Z_STRLEN_PP(zhost
));
369 uri
= http_absolute_uri_ex(
370 message
->info
.request
.URI
, strlen(message
->info
.request
.URI
),
371 NULL
, 0, host
, host_len
, port
);
374 uri
= http_absolute_uri(message
->info
.request
.URI
);
377 if (!strcasecmp("POST", message
->info
.request
.method
)) {
378 http_request_body body
= {HTTP_REQUEST_BODY_CSTRING
, PHPSTR_VAL(message
), PHPSTR_LEN(message
)};
379 rs
= http_post(uri
, &body
, Z_ARRVAL(options
), NULL
, NULL
);
381 if (!strcasecmp("GET", message
->info
.request
.method
)) {
382 rs
= http_get(uri
, Z_ARRVAL(options
), NULL
, NULL
);
384 if (!strcasecmp("HEAD", message
->info
.request
.method
)) {
385 rs
= http_head(uri
, Z_ARRVAL(options
), NULL
, NULL
);
387 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
,
388 "Cannot send HttpMessage. Request method %s not supported",
389 message
->info
.request
.method
);
394 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "HTTP requests not supported - ext/http was not linked against libcurl.");
401 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is neither of type HTTP_MSG_REQUEST nor HTTP_MSG_RESPONSE");
408 PHP_HTTP_API http_message
*_http_message_dup(http_message
*msg TSRMLS_DC
)
414 char *serialized_data
;
415 size_t serialized_length
;
417 http_message_serialize(msg
, &serialized_data
, &serialized_length
);
418 new = http_message_parse(serialized_data
, serialized_length
);
419 efree(serialized_data
);
423 PHP_HTTP_API
void _http_message_dtor(http_message
*message
)
426 zend_hash_destroy(&message
->hdrs
);
427 phpstr_dtor(PHPSTR(message
));
428 if (HTTP_MSG_TYPE(REQUEST
, message
)) {
429 if (message
->info
.request
.method
) {
430 efree(message
->info
.request
.method
);
431 message
->info
.request
.method
= NULL
;
433 if (message
->info
.request
.URI
) {
434 efree(message
->info
.request
.URI
);
435 message
->info
.request
.URI
= NULL
;
441 PHP_HTTP_API
void _http_message_free(http_message
*message
)
444 if (message
->parent
) {
445 http_message_free(message
->parent
);
446 message
->parent
= NULL
;
448 http_message_dtor(message
);
458 * vim600: noet sw=4 ts=4 fdm=marker
459 * vim<600: noet sw=4 ts=4