2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #define HTTP_WANT_SAPI
16 #define HTTP_WANT_CURL
17 #define HTTP_WANT_ZLIB
20 #include "php_http_api.h"
21 #include "php_http_encoding_api.h"
22 #include "php_http_headers_api.h"
23 #include "php_http_message_api.h"
24 #include "php_http_request_api.h"
25 #include "php_http_send_api.h"
26 #include "php_http_url_api.h"
28 #define http_message_info_callback _http_message_info_callback
29 static void _http_message_info_callback(http_message
**message
, HashTable
**headers
, http_info
*info TSRMLS_DC
)
31 http_message
*old
= *message
;
34 if (old
->type
|| zend_hash_num_elements(&old
->hdrs
) || PHPSTR_LEN(old
)) {
35 (*message
) = http_message_new();
36 (*message
)->parent
= old
;
37 (*headers
) = &((*message
)->hdrs
);
40 http_message_set_info(*message
, info
);
43 #define http_message_init_type _http_message_init_type
44 static inline void _http_message_init_type(http_message
*message
, http_message_type type
)
46 message
->http
.version
= .0;
48 switch (message
->type
= type
) {
49 case HTTP_MSG_RESPONSE
:
50 message
->http
.info
.response
.code
= 0;
51 message
->http
.info
.response
.status
= NULL
;
54 case HTTP_MSG_REQUEST
:
55 message
->http
.info
.request
.method
= NULL
;
56 message
->http
.info
.request
.url
= NULL
;
65 PHP_HTTP_API http_message
*_http_message_init_ex(http_message
*message
, http_message_type type ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC
)
68 message
= ecalloc_rel(1, sizeof(http_message
));
71 http_message_init_type(message
, type
);
72 message
->parent
= NULL
;
73 phpstr_init(&message
->body
);
74 zend_hash_init(&message
->hdrs
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
80 PHP_HTTP_API
void _http_message_set_type(http_message
*message
, http_message_type type
)
82 /* just act if different */
83 if (type
!= message
->type
) {
85 /* free request info */
86 switch (message
->type
) {
87 case HTTP_MSG_REQUEST
:
88 STR_FREE(message
->http
.info
.request
.method
);
89 STR_FREE(message
->http
.info
.request
.url
);
92 case HTTP_MSG_RESPONSE
:
93 STR_FREE(message
->http
.info
.response
.status
);
101 http_message_init_type(message
, type
);
105 PHP_HTTP_API
void _http_message_set_info(http_message
*message
, http_info
*info
)
107 message
->http
.version
= info
->http
.version
;
109 switch (message
->type
= info
->type
) {
110 case IS_HTTP_REQUEST
:
111 HTTP_INFO(message
).request
.url
= estrdup(HTTP_INFO(info
).request
.url
);
112 STR_SET(HTTP_INFO(message
).request
.method
, estrdup(HTTP_INFO(info
).request
.method
));
115 case IS_HTTP_RESPONSE
:
116 HTTP_INFO(message
).response
.code
= HTTP_INFO(info
).response
.code
;
117 STR_SET(HTTP_INFO(message
).response
.status
, estrdup(HTTP_INFO(info
).response
.status
));
125 #define http_message_body_parse(m, ms, ml, c) _http_message_body_parse((m), (ms), (ml), (c) TSRMLS_CC)
126 static inline void _http_message_body_parse(http_message
*msg
, const char *message
, size_t message_length
, const char **continue_at TSRMLS_DC
)
133 if ((body
= http_locate_body(message
))) {
134 remaining
= message
+ message_length
- body
;
136 if ((c
= http_message_header(msg
, "Transfer-Encoding"))) {
137 if (strstr(Z_STRVAL_P(c
), "chunked")) {
138 /* message has chunked transfer encoding */
142 /* decode and replace Transfer-Encoding with Content-Length header */
143 if ((*continue_at
= http_encoding_dechunk(body
, message
+ message_length
- body
, &decoded
, &decoded_len
))) {
148 tmp_len
= (int) spprintf(&tmp
, 0, "%zu", decoded_len
);
150 ZVAL_STRINGL(len
, tmp
, tmp_len
, 0);
153 zend_hash_update(&msg
->hdrs
, "X-Original-Transfer-Encoding", sizeof("X-Original-Transfer-Encoding"), (void *) &c
, sizeof(zval
*), NULL
);
154 zend_hash_del(&msg
->hdrs
, "Transfer-Encoding", sizeof("Transfer-Encoding"));
155 zend_hash_del(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"));
156 zend_hash_update(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"), (void *) &len
, sizeof(zval
*), NULL
);
158 phpstr_from_string_ex(PHPSTR(msg
), decoded
, decoded_len
);
165 if (!*continue_at
&& (c
= http_message_header(msg
, "Content-Length"))) {
166 /* message has content-length header */
167 ulong len
= strtoul(Z_STRVAL_P(c
), NULL
, 10);
168 if (len
> remaining
) {
169 http_error_ex(HE_NOTICE
, HTTP_E_MALFORMED_HEADERS
, "The Content-Length header pretends a larger body than actually received (expected %lu bytes; got %lu bytes)", len
, remaining
);
172 phpstr_from_string_ex(PHPSTR(msg
), body
, len
);
173 *continue_at
= body
+ len
;
177 if (!*continue_at
&& (c
= http_message_header(msg
, "Content-Range"))) {
178 /* message has content-range header */
179 ulong total
= 0, start
= 0, end
= 0, len
= 0;
181 if (!strncasecmp(Z_STRVAL_P(c
), "bytes", lenof("bytes")) &&
182 ( Z_STRVAL_P(c
)[lenof("bytes")] == ':' ||
183 Z_STRVAL_P(c
)[lenof("bytes")] == ' ' ||
184 Z_STRVAL_P(c
)[lenof("bytes")] == '=')) {
185 char *total_at
= NULL
, *end_at
= NULL
;
186 char *start_at
= Z_STRVAL_P(c
) + sizeof("bytes");
188 start
= strtoul(start_at
, &end_at
, 10);
190 end
= strtoul(end_at
+ 1, &total_at
, 10);
191 if (total_at
&& strncmp(total_at
+ 1, "*", 1)) {
192 total
= strtoul(total_at
+ 1, NULL
, 10);
194 if ((len
= (end
+ 1 - start
)) > remaining
) {
195 http_error_ex(HE_NOTICE
, HTTP_E_MALFORMED_HEADERS
, "The Content-Range header pretends a larger body than actually received (expected %lu bytes; got %lu bytes)", len
, remaining
);
198 if (end
>= start
&& (!total
|| end
< total
)) {
199 phpstr_from_string_ex(PHPSTR(msg
), body
, len
);
200 *continue_at
= body
+ len
;
206 http_error_ex(HE_WARNING
, HTTP_E_MALFORMED_HEADERS
, "Invalid Content-Range header: %s", Z_STRVAL_P(c
));
212 /* no headers that indicate content length */
213 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
214 phpstr_from_string_ex(PHPSTR(msg
), body
, remaining
);
220 #ifdef HTTP_HAVE_ZLIB
221 /* check for compressed data */
222 if ((c
= http_message_header(msg
, "Vary"))) {
225 if ((c
= http_message_header(msg
, "Content-Encoding"))) {
226 char *decoded
= NULL
;
227 size_t decoded_len
= 0;
229 if ( !strcasecmp(Z_STRVAL_P(c
), "gzip") ||
230 !strcasecmp(Z_STRVAL_P(c
), "x-gzip") ||
231 !strcasecmp(Z_STRVAL_P(c
), "deflate")) {
232 http_encoding_inflate(PHPSTR_VAL(msg
), PHPSTR_LEN(msg
), &decoded
, &decoded_len
);
236 zval
*len
, **original_len
;
240 tmp_len
= (int) spprintf(&tmp
, 0, "%zu", decoded_len
);
242 ZVAL_STRINGL(len
, tmp
, tmp_len
, 0);
245 zend_hash_update(&msg
->hdrs
, "X-Original-Content-Encoding", sizeof("X-Original-Content-Encoding"), (void *) &c
, sizeof(zval
*), NULL
);
246 zend_hash_del(&msg
->hdrs
, "Content-Encoding", sizeof("Content-Encoding"));
247 if (SUCCESS
== zend_hash_find(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"), (void *) &original_len
)) {
248 ZVAL_ADDREF(*original_len
);
249 zend_hash_update(&msg
->hdrs
, "X-Original-Content-Length", sizeof("X-Original-Content-Length"), (void *) original_len
, sizeof(zval
*), NULL
);
250 zend_hash_update(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"), (void *) &len
, sizeof(zval
*), NULL
);
252 zend_hash_update(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"), (void *) &len
, sizeof(zval
*), NULL
);
255 phpstr_dtor(PHPSTR(msg
));
256 PHPSTR(msg
)->data
= decoded
;
257 PHPSTR(msg
)->used
= decoded_len
;
258 PHPSTR(msg
)->free
= 1;
264 #endif /* HTTP_HAVE_ZLIB */
268 PHP_HTTP_API http_message
*_http_message_parse_ex(http_message
*msg
, const char *message
, size_t message_length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC
)
270 const char *continue_at
;
271 zend_bool free_msg
= msg
? 0 : 1;
273 if ((!message
) || (message_length
< HTTP_MSG_MIN_SIZE
)) {
274 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Empty or too short HTTP message: '%s'", message
);
278 msg
= http_message_init_rel(msg
, 0);
280 if (SUCCESS
!= http_parse_headers_cb(message
, &msg
->hdrs
, 1, (http_info_callback
) http_message_info_callback
, (void *) &msg
)) {
282 http_message_free(&msg
);
284 http_error(HE_WARNING
, HTTP_E_MALFORMED_HEADERS
, "Failed to parse message headers");
288 http_message_body_parse(msg
, message
, message_length
, &continue_at
);
290 /* check for following messages */
291 if (continue_at
&& (continue_at
< (message
+ message_length
))) {
292 while (HTTP_IS_CTYPE(space
, *continue_at
)) ++continue_at
;
293 if (continue_at
< (message
+ message_length
)) {
294 http_message
*next
= NULL
, *most
= NULL
;
296 /* set current message to parent of most parent following messages and return deepest */
297 if ((most
= next
= http_message_parse_rel(NULL
, continue_at
, message
+ message_length
- continue_at
))) {
298 while (most
->parent
) most
= most
->parent
;
308 PHP_HTTP_API
void _http_message_tostring(http_message
*msg
, char **string
, size_t *length
)
311 HashKey key
= initHashKey(0);
316 phpstr_init_ex(&str
, 4096, 0);
319 case HTTP_MSG_REQUEST
:
320 phpstr_appendf(&str
, "%s %s HTTP/%1.1f" HTTP_CRLF
,
321 msg
->http
.info
.request
.method
?msg
->http
.info
.request
.method
:"UNKNOWN",
322 msg
->http
.info
.request
.url
?msg
->http
.info
.request
.url
:"/",
326 case HTTP_MSG_RESPONSE
:
327 phpstr_appendf(&str
, "HTTP/%1.1f %d%s%s" HTTP_CRLF
,
329 msg
->http
.info
.response
.code
,
330 msg
->http
.info
.response
.status
&&*msg
->http
.info
.response
.status
? " ":"",
331 STR_PTR(msg
->http
.info
.response
.status
));
339 FOREACH_HASH_KEYVAL(pos1
, &msg
->hdrs
, key
, header
) {
340 if (key
.type
== HASH_KEY_IS_STRING
) {
342 zval
**single_header
;
344 switch (Z_TYPE_PP(header
)) {
346 phpstr_appendf(&str
, "%s: %s" HTTP_CRLF
, key
.str
, Z_STRVAL_PP(header
));
350 FOREACH_VAL(pos2
, *header
, single_header
) {
351 phpstr_appendf(&str
, "%s: %s" HTTP_CRLF
, key
.str
, Z_STRVAL_PP(single_header
));
358 if (PHPSTR_LEN(msg
)) {
359 phpstr_appends(&str
, HTTP_CRLF
);
360 phpstr_append(&str
, PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
361 phpstr_appends(&str
, HTTP_CRLF
);
364 data
= phpstr_data(&str
, string
, length
);
372 PHP_HTTP_API
void _http_message_serialize(http_message
*message
, char **string
, size_t *length
)
381 http_message_tostring(message
, &buf
, &len
);
382 phpstr_prepend(&str
, buf
, len
);
384 } while ((message
= message
->parent
));
386 buf
= phpstr_data(&str
, string
, length
);
394 PHP_HTTP_API http_message
*_http_message_reverse(http_message
*msg
)
398 http_message_count(c
, msg
);
401 http_message
*tmp
= msg
, **arr
= ecalloc(c
, sizeof(http_message
*));
403 for (i
= 0; i
< c
; ++i
) {
407 arr
[0]->parent
= NULL
;
408 for (i
= 0; i
< c
-1; ++i
) {
409 arr
[i
+1]->parent
= arr
[i
];
419 PHP_HTTP_API http_message
*_http_message_interconnect(http_message
*m1
, http_message
*m2
)
423 http_message
*t1
= m1
, *t2
= m2
, *p1
, *p2
;
425 http_message_count(c1
, m1
);
426 http_message_count(c2
, m2
);
428 while (i
++ < (c1
- c2
)) {
439 } else if (!m1
&& m2
) {
445 PHP_HTTP_API
void _http_message_tostruct_recursive(http_message
*msg
, zval
*obj TSRMLS_DC
)
450 INIT_ZARR(strct
, HASH_OF(obj
));
452 add_assoc_long(&strct
, "type", msg
->type
);
453 add_assoc_double(&strct
, "httpVersion", msg
->http
.version
);
456 case HTTP_MSG_RESPONSE
:
457 add_assoc_long(&strct
, "responseCode", msg
->http
.info
.response
.code
);
458 add_assoc_string(&strct
, "responseStatus", msg
->http
.info
.response
.status
, 1);
461 case HTTP_MSG_REQUEST
:
462 add_assoc_string(&strct
, "requestMethod", msg
->http
.info
.request
.method
, 1);
463 add_assoc_string(&strct
, "requestUrl", msg
->http
.info
.request
.url
, 1);
467 /* avoid compiler warning */
471 MAKE_STD_ZVAL(headers
);
473 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
474 add_assoc_zval(&strct
, "headers", headers
);
476 add_assoc_stringl(&strct
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
), 1);
481 MAKE_STD_ZVAL(parent
);
482 if (Z_TYPE_P(obj
) == IS_ARRAY
) {
487 add_assoc_zval(&strct
, "parentMessage", parent
);
488 http_message_tostruct_recursive(msg
->parent
, parent
);
490 add_assoc_null(&strct
, "parentMessage");
494 PHP_HTTP_API STATUS
_http_message_send(http_message
*message TSRMLS_DC
)
498 switch (message
->type
) {
499 case HTTP_MSG_RESPONSE
:
501 HashKey key
= initHashKey(0);
505 FOREACH_HASH_KEYVAL(pos
, &message
->hdrs
, key
, val
) {
506 if (key
.type
== HASH_KEY_IS_STRING
) {
507 http_send_header_zval_ex(key
.str
, key
.len
-1, val
, 1);
510 rs
= SUCCESS
== http_send_status(message
->http
.info
.response
.code
) &&
511 SUCCESS
== http_send_data(PHPSTR_VAL(message
), PHPSTR_LEN(message
)) ?
516 case HTTP_MSG_REQUEST
:
518 #ifdef HTTP_HAVE_CURL
520 http_request request
;
521 zval
**zhost
, options
, headers
;
523 INIT_PZVAL(&options
);
524 INIT_PZVAL(&headers
);
525 array_init(&options
);
526 array_init(&headers
);
527 zend_hash_copy(Z_ARRVAL(headers
), &message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
528 add_assoc_zval(&options
, "headers", &headers
);
530 /* check host header */
531 if (SUCCESS
== zend_hash_find(&message
->hdrs
, "Host", sizeof("Host"), (void *) &zhost
)) {
533 php_url parts
, *url
= php_url_parse(message
->http
.info
.request
.url
);
535 memset(&parts
, 0, sizeof(php_url
));
538 if ((colon
= strchr(Z_STRVAL_PP(zhost
), ':'))) {
539 parts
.port
= atoi(colon
+ 1);
540 parts
.host
= estrndup(Z_STRVAL_PP(zhost
), (Z_STRVAL_PP(zhost
) - colon
- 1));
542 parts
.host
= estrndup(Z_STRVAL_PP(zhost
), Z_STRLEN_PP(zhost
));
545 http_build_url(HTTP_URL_REPLACE
, url
, &parts
, NULL
, &uri
, NULL
);
549 uri
= http_absolute_url(message
->http
.info
.request
.url
);
552 if ((request
.meth
= http_request_method_exists(1, 0, message
->http
.info
.request
.method
))) {
553 http_request_body body
;
555 http_request_init_ex(&request
, NULL
, request
.meth
, uri
);
556 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_CSTRING
, PHPSTR_VAL(message
), PHPSTR_LEN(message
), 0);
557 if (SUCCESS
== (rs
= http_request_prepare(&request
, NULL
))) {
558 http_request_exec(&request
);
560 http_request_dtor(&request
);
562 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
,
563 "Cannot send HttpMessage. Request method %s not supported",
564 message
->http
.info
.request
.method
);
568 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "HTTP requests not supported - ext/http was not linked against libcurl.");
575 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is neither of type HTTP_MSG_REQUEST nor HTTP_MSG_RESPONSE");
582 PHP_HTTP_API http_message
*_http_message_dup(http_message
*orig TSRMLS_DC
)
584 http_message
*temp
, *copy
= NULL
;
588 info
.type
= orig
->type
;
589 info
.http
= orig
->http
;
591 copy
= temp
= http_message_new();
592 http_message_set_info(temp
, &info
);
593 zend_hash_copy(&temp
->hdrs
, &orig
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
594 phpstr_append(&temp
->body
, orig
->body
.data
, orig
->body
.used
);
596 while (orig
->parent
) {
597 info
.type
= orig
->parent
->type
;
598 info
.http
= orig
->parent
->http
;
600 temp
->parent
= http_message_new();
601 http_message_set_info(temp
->parent
, &info
);
602 zend_hash_copy(&temp
->parent
->hdrs
, &orig
->parent
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
603 phpstr_append(&temp
->parent
->body
, orig
->parent
->body
.data
, orig
->parent
->body
.used
);
613 PHP_HTTP_API
void _http_message_dtor(http_message
*message
)
616 zend_hash_destroy(&message
->hdrs
);
617 phpstr_dtor(PHPSTR(message
));
619 switch (message
->type
) {
620 case HTTP_MSG_REQUEST
:
621 STR_SET(message
->http
.info
.request
.method
, NULL
);
622 STR_SET(message
->http
.info
.request
.url
, NULL
);
625 case HTTP_MSG_RESPONSE
:
626 STR_SET(message
->http
.info
.response
.status
, NULL
);
635 PHP_HTTP_API
void _http_message_free(http_message
**message
)
638 if ((*message
)->parent
) {
639 http_message_free(&(*message
)->parent
);
641 http_message_dtor(*message
);
652 * vim600: noet sw=4 ts=4 fdm=marker
653 * vim<600: noet sw=4 ts=4