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_info_callback _http_message_info_callback
37 static void _http_message_info_callback(http_message
**message
, HashTable
**headers
, http_info
*info TSRMLS_DC
)
39 http_message
*old
= *message
;
42 if (old
->type
|| zend_hash_num_elements(&old
->hdrs
) || PHPSTR_LEN(old
)) {
43 (*message
) = http_message_new();
44 (*message
)->parent
= old
;
45 (*headers
) = &((*message
)->hdrs
);
48 (*message
)->http
.version
= info
->http
.version
;
53 (*message
)->type
= HTTP_MSG_REQUEST
;
54 HTTP_INFO(*message
).request
.URI
= estrdup(HTTP_INFO(info
).request
.URI
);
55 HTTP_INFO(*message
).request
.method
= estrdup(HTTP_INFO(info
).request
.method
);
58 case IS_HTTP_RESPONSE
:
59 (*message
)->type
= HTTP_MSG_RESPONSE
;
60 HTTP_INFO(*message
).response
.code
= HTTP_INFO(info
).response
.code
;
61 HTTP_INFO(*message
).response
.status
= estrdup(HTTP_INFO(info
).response
.status
);
66 #define http_message_init_type _http_message_init_type
67 static inline void _http_message_init_type(http_message
*message
, http_message_type type
)
69 message
->http
.version
= .0;
71 switch (message
->type
= type
)
73 case HTTP_MSG_RESPONSE
:
74 message
->http
.info
.response
.code
= 0;
75 message
->http
.info
.response
.status
= NULL
;
78 case HTTP_MSG_REQUEST
:
79 message
->http
.info
.request
.method
= NULL
;
80 message
->http
.info
.request
.URI
= NULL
;
89 PHP_HTTP_API http_message
*_http_message_init_ex(http_message
*message
, http_message_type type
)
92 message
= ecalloc(1, sizeof(http_message
));
95 http_message_init_type(message
, type
);
96 message
->parent
= NULL
;
97 phpstr_init(&message
->body
);
98 zend_hash_init(&message
->hdrs
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
104 PHP_HTTP_API
void _http_message_set_type(http_message
*message
, http_message_type type
)
106 /* just act if different */
107 if (type
!= message
->type
) {
109 /* free request info */
110 switch (message
->type
)
112 case HTTP_MSG_REQUEST
:
113 STR_FREE(message
->http
.info
.request
.method
);
114 STR_FREE(message
->http
.info
.request
.URI
);
117 case HTTP_MSG_RESPONSE
:
118 STR_FREE(message
->http
.info
.response
.status
);
126 http_message_init_type(message
, type
);
130 PHP_HTTP_API http_message
*_http_message_parse_ex(http_message
*msg
, const char *message
, size_t message_length TSRMLS_DC
)
132 const char *body
= NULL
;
133 zend_bool free_msg
= msg
? 0 : 1;
135 if ((!message
) || (message_length
< HTTP_MSG_MIN_SIZE
)) {
139 msg
= http_message_init(msg
);
141 if (SUCCESS
!= http_parse_headers_cb(message
, &msg
->hdrs
, 1, (http_info_callback
) http_message_info_callback
, (void **) &msg
)) {
143 http_message_free(&msg
);
148 /* header parsing stops at (CR)LF (CR)LF */
149 if (body
= http_locate_body(message
)) {
151 const char *continue_at
= NULL
;
153 /* message has chunked transfer encoding */
154 if ((c
= http_message_header(msg
, "Transfer-Encoding")) && (!strcasecmp("chunked", Z_STRVAL_P(c
)))) {
158 /* decode and replace Transfer-Encoding with Content-Length header */
159 if (continue_at
= http_chunked_decode(body
, message
+ message_length
- body
, &decoded
, &decoded_len
)) {
164 tmp_len
= (int) spprintf(&tmp
, 0, "%lu", (ulong
) decoded_len
);
166 ZVAL_STRINGL(len
, tmp
, tmp_len
, 0);
168 zend_hash_del(&msg
->hdrs
, "Transfer-Encoding", sizeof("Transfer-Encoding"));
169 zend_hash_add(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"), (void *) &len
, sizeof(zval
*), NULL
);
171 phpstr_from_string_ex(PHPSTR(msg
), decoded
, decoded_len
);
176 /* message has content-length header */
177 if (c
= http_message_header(msg
, "Content-Length")) {
178 long len
= atol(Z_STRVAL_P(c
));
179 phpstr_from_string_ex(PHPSTR(msg
), body
, len
);
180 continue_at
= body
+ len
;
183 /* message has content-range header */
184 if (c
= http_message_header(msg
, "Content-Range")) {
185 ulong total
= 0, start
= 0, end
= 0;
187 if (!strncasecmp(Z_STRVAL_P(c
), "bytes=", lenof("bytes="))) {
188 char *total_at
= NULL
, *end_at
= NULL
;
189 char *start_at
= Z_STRVAL_P(c
) + lenof("bytes=");
191 start
= strtoul(start_at
, &end_at
, 10);
193 end
= strtoul(end_at
+ 1, &total_at
, 10);
194 if (total_at
&& strncmp(total_at
+ 1, "*", 1)) {
195 total
= strtoul(total_at
+ 1, NULL
, 10);
198 if (end
>= start
&& (!total
|| end
< total
)) {
199 phpstr_from_string_ex(PHPSTR(msg
), body
, (size_t) (end
+ 1 - start
));
200 continue_at
= body
+ (end
+ 1 - start
);
206 http_error_ex(HE_WARNING
, HTTP_E_MALFORMED_HEADERS
, "Invalid Content-Range header: %s", Z_STRVAL_P(c
));
210 /* no headers that indicate content length */
211 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
212 phpstr_from_string_ex(PHPSTR(msg
), body
, message
+ message_length
- body
);
217 /* check for following messages */
219 while (isspace(*continue_at
)) ++continue_at
;
220 if (continue_at
< (message
+ message_length
)) {
221 http_message
*next
= NULL
, *most
= NULL
;
223 /* set current message to parent of most parent following messages and return deepest */
224 if (most
= next
= http_message_parse(continue_at
, message
+ message_length
- continue_at
)) {
225 while (most
->parent
) most
= most
->parent
;
236 PHP_HTTP_API
void _http_message_tostring(http_message
*msg
, char **string
, size_t *length
)
243 phpstr_init_ex(&str
, 4096, 0);
247 case HTTP_MSG_REQUEST
:
248 phpstr_appendf(&str
, "%s %s HTTP/%1.1f" HTTP_CRLF
,
249 msg
->http
.info
.request
.method
,
250 msg
->http
.info
.request
.URI
,
254 case HTTP_MSG_RESPONSE
:
255 phpstr_appendf(&str
, "HTTP/%1.1f %d%s%s" HTTP_CRLF
,
257 msg
->http
.info
.response
.code
,
258 *msg
->http
.info
.response
.status
? " ":"",
259 msg
->http
.info
.response
.status
);
267 FOREACH_HASH_KEYVAL(&msg
->hdrs
, key
, idx
, header
) {
269 zval
**single_header
;
271 switch (Z_TYPE_PP(header
))
274 phpstr_appendf(&str
, "%s: %s" HTTP_CRLF
, key
, Z_STRVAL_PP(header
));
278 FOREACH_VAL(*header
, single_header
) {
279 phpstr_appendf(&str
, "%s: %s" HTTP_CRLF
, key
, Z_STRVAL_PP(single_header
));
288 if (PHPSTR_LEN(msg
)) {
289 phpstr_appends(&str
, HTTP_CRLF
);
290 phpstr_append(&str
, PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
291 phpstr_appends(&str
, HTTP_CRLF
);
294 data
= phpstr_data(&str
, string
, length
);
302 PHP_HTTP_API
void _http_message_serialize(http_message
*message
, char **string
, size_t *length
)
311 http_message_tostring(message
, &buf
, &len
);
312 phpstr_prepend(&str
, buf
, len
);
314 } while (message
= message
->parent
);
316 buf
= phpstr_data(&str
, string
, length
);
324 PHP_HTTP_API
void _http_message_tostruct_recursive(http_message
*msg
, zval
*obj TSRMLS_DC
)
329 INIT_ZARR(strct
, HASH_OF(obj
));
331 add_assoc_long(&strct
, "type", msg
->type
);
332 add_assoc_double(&strct
, "httpVersion", msg
->http
.version
);
335 case HTTP_MSG_RESPONSE
:
336 add_assoc_long(&strct
, "responseCode", msg
->http
.info
.response
.code
);
337 add_assoc_string(&strct
, "responseStatus", msg
->http
.info
.response
.status
, 1);
340 case HTTP_MSG_REQUEST
:
341 add_assoc_string(&strct
, "requestMethod", msg
->http
.info
.request
.method
, 1);
342 add_assoc_string(&strct
, "requestUri", msg
->http
.info
.request
.URI
, 1);
346 MAKE_STD_ZVAL(headers
);
348 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
349 add_assoc_zval(&strct
, "headers", headers
);
351 add_assoc_stringl(&strct
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
), 1);
356 MAKE_STD_ZVAL(parent
);
357 if (Z_TYPE_P(obj
) == IS_ARRAY
) {
362 add_assoc_zval(&strct
, "parentMessage", parent
);
363 http_message_tostruct_recursive(msg
->parent
, parent
);
364 zval_ptr_dtor(&parent
);
366 add_assoc_null(&strct
, "parentMessage");
370 PHP_HTTP_API STATUS
_http_message_send(http_message
*message TSRMLS_DC
)
374 switch (message
->type
)
376 case HTTP_MSG_RESPONSE
:
382 FOREACH_HASH_KEYVAL(&message
->hdrs
, key
, idx
, val
) {
384 if (Z_TYPE_PP(val
) == IS_ARRAY
) {
388 FOREACH_VAL(*val
, data
) {
389 http_send_header_ex(key
, strlen(key
), Z_STRVAL_PP(data
), Z_STRLEN_PP(data
), first
, NULL
);
393 http_send_header_ex(key
, strlen(key
), Z_STRVAL_PP(val
), Z_STRLEN_PP(val
), 1, NULL
);
398 rs
= SUCCESS
== http_send_status(message
->http
.info
.response
.code
) &&
399 SUCCESS
== http_send_data(PHPSTR_VAL(message
), PHPSTR_LEN(message
)) ?
404 case HTTP_MSG_REQUEST
:
406 #ifdef HTTP_HAVE_CURL
408 zval
**zhost
, options
, headers
;
410 INIT_PZVAL(&options
);
411 INIT_PZVAL(&headers
);
412 array_init(&options
);
413 array_init(&headers
);
414 zend_hash_copy(Z_ARRVAL(headers
), &message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
415 add_assoc_zval(&options
, "headers", &headers
);
417 /* check host header */
418 if (SUCCESS
== zend_hash_find(&message
->hdrs
, "Host", sizeof("Host"), (void **) &zhost
)) {
419 char *colon
= NULL
, *host
= NULL
;
424 if (colon
= strchr(Z_STRVAL_PP(zhost
), ':')) {
425 port
= atoi(colon
+ 1);
426 host
= estrndup(Z_STRVAL_PP(zhost
), host_len
= (Z_STRVAL_PP(zhost
) - colon
- 1));
428 host
= estrndup(Z_STRVAL_PP(zhost
), host_len
= Z_STRLEN_PP(zhost
));
430 uri
= http_absolute_uri_ex(
431 message
->http
.info
.request
.URI
, strlen(message
->http
.info
.request
.URI
),
432 NULL
, 0, host
, host_len
, port
);
435 uri
= http_absolute_uri(message
->http
.info
.request
.URI
);
438 if (!strcasecmp("POST", message
->http
.info
.request
.method
)) {
439 http_request_body body
= {HTTP_REQUEST_BODY_CSTRING
, PHPSTR_VAL(message
), PHPSTR_LEN(message
)};
440 rs
= http_post(uri
, &body
, Z_ARRVAL(options
), NULL
, NULL
);
442 if (!strcasecmp("GET", message
->http
.info
.request
.method
)) {
443 rs
= http_get(uri
, Z_ARRVAL(options
), NULL
, NULL
);
445 if (!strcasecmp("HEAD", message
->http
.info
.request
.method
)) {
446 rs
= http_head(uri
, Z_ARRVAL(options
), NULL
, NULL
);
448 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
,
449 "Cannot send HttpMessage. Request method %s not supported",
450 message
->http
.info
.request
.method
);
455 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "HTTP requests not supported - ext/http was not linked against libcurl.");
462 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is neither of type HTTP_MSG_REQUEST nor HTTP_MSG_RESPONSE");
469 PHP_HTTP_API http_message
*_http_message_dup(http_message
*msg TSRMLS_DC
)
475 char *serialized_data
;
476 size_t serialized_length
;
478 http_message_serialize(msg
, &serialized_data
, &serialized_length
);
479 new = http_message_parse(serialized_data
, serialized_length
);
480 efree(serialized_data
);
484 PHP_HTTP_API
void _http_message_dtor(http_message
*message
)
487 zend_hash_destroy(&message
->hdrs
);
488 phpstr_dtor(PHPSTR(message
));
490 switch (message
->type
)
492 case HTTP_MSG_REQUEST
:
493 STR_SET(message
->http
.info
.request
.method
, NULL
);
494 STR_SET(message
->http
.info
.request
.URI
, NULL
);
497 case HTTP_MSG_RESPONSE
:
498 STR_SET(message
->http
.info
.response
.status
, NULL
);
507 PHP_HTTP_API
void _http_message_free(http_message
**message
)
510 if ((*message
)->parent
) {
511 http_message_free(&(*message
)->parent
);
513 http_message_dtor(*message
);
524 * vim600: noet sw=4 ts=4 fdm=marker
525 * vim<600: noet sw=4 ts=4