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-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
19 #define HTTP_WANT_CURL
20 #define HTTP_WANT_ZLIB
25 #include "php_http_api.h"
26 #include "php_http_encoding_api.h"
27 #include "php_http_headers_api.h"
28 #include "php_http_message_api.h"
29 #include "php_http_request_api.h"
30 #include "php_http_send_api.h"
31 #include "php_http_url_api.h"
33 ZEND_EXTERN_MODULE_GLOBALS(http
);
35 #define http_message_info_callback _http_message_info_callback
36 static void _http_message_info_callback(http_message
**message
, HashTable
**headers
, http_info
*info TSRMLS_DC
)
38 http_message
*old
= *message
;
41 if (old
->type
|| zend_hash_num_elements(&old
->hdrs
) || PHPSTR_LEN(old
)) {
42 (*message
) = http_message_new();
43 (*message
)->parent
= old
;
44 (*headers
) = &((*message
)->hdrs
);
47 (*message
)->http
.version
= info
->http
.version
;
52 (*message
)->type
= HTTP_MSG_REQUEST
;
53 HTTP_INFO(*message
).request
.URI
= estrdup(HTTP_INFO(info
).request
.URI
);
54 HTTP_INFO(*message
).request
.method
= estrdup(HTTP_INFO(info
).request
.method
);
57 case IS_HTTP_RESPONSE
:
58 (*message
)->type
= HTTP_MSG_RESPONSE
;
59 HTTP_INFO(*message
).response
.code
= HTTP_INFO(info
).response
.code
;
60 HTTP_INFO(*message
).response
.status
= estrdup(HTTP_INFO(info
).response
.status
);
65 #define http_message_init_type _http_message_init_type
66 static inline void _http_message_init_type(http_message
*message
, http_message_type type
)
68 message
->http
.version
= .0;
70 switch (message
->type
= type
)
72 case HTTP_MSG_RESPONSE
:
73 message
->http
.info
.response
.code
= 0;
74 message
->http
.info
.response
.status
= NULL
;
77 case HTTP_MSG_REQUEST
:
78 message
->http
.info
.request
.method
= NULL
;
79 message
->http
.info
.request
.URI
= NULL
;
88 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
)
91 message
= ecalloc_rel(1, sizeof(http_message
));
94 http_message_init_type(message
, type
);
95 message
->parent
= NULL
;
96 phpstr_init(&message
->body
);
97 zend_hash_init(&message
->hdrs
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
103 PHP_HTTP_API
void _http_message_set_type(http_message
*message
, http_message_type type
)
105 /* just act if different */
106 if (type
!= message
->type
) {
108 /* free request info */
109 switch (message
->type
)
111 case HTTP_MSG_REQUEST
:
112 STR_FREE(message
->http
.info
.request
.method
);
113 STR_FREE(message
->http
.info
.request
.URI
);
116 case HTTP_MSG_RESPONSE
:
117 STR_FREE(message
->http
.info
.response
.status
);
125 http_message_init_type(message
, type
);
129 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
)
131 const char *body
= NULL
;
132 zend_bool free_msg
= msg
? 0 : 1;
134 if ((!message
) || (message_length
< HTTP_MSG_MIN_SIZE
)) {
135 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Empty or too short HTTP message: '%s'", message
);
139 msg
= http_message_init_rel(msg
, 0);
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
);
145 http_error(HE_WARNING
, HTTP_E_MALFORMED_HEADERS
, "Failed to parse message headers");
149 /* header parsing stops at (CR)LF (CR)LF */
150 if ((body
= http_locate_body(message
))) {
152 const char *continue_at
= NULL
;
153 size_t remaining
= message
+ message_length
- body
;
155 /* message has chunked transfer encoding */
156 if ((c
= http_message_header(msg
, "Transfer-Encoding")) && (!strcasecmp("chunked", Z_STRVAL_P(c
)))) {
160 /* decode and replace Transfer-Encoding with Content-Length header */
161 if ((continue_at
= http_encoding_dechunk(body
, message
+ message_length
- body
, &decoded
, &decoded_len
))) {
166 tmp_len
= (int) spprintf(&tmp
, 0, "%zu", decoded_len
);
168 ZVAL_STRINGL(len
, tmp
, tmp_len
, 0);
171 zend_hash_add(&msg
->hdrs
, "X-Original-Transfer-Encoding", sizeof("X-Original-Transfer-Encoding"), (void *) &c
, sizeof(zval
*), NULL
);
172 zend_hash_del(&msg
->hdrs
, "Transfer-Encoding", sizeof("Transfer-Encoding"));
173 zend_hash_del(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"));
174 zend_hash_add(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"), (void *) &len
, sizeof(zval
*), NULL
);
176 phpstr_from_string_ex(PHPSTR(msg
), decoded
, decoded_len
);
181 /* message has content-length header */
182 if ((c
= http_message_header(msg
, "Content-Length"))) {
183 ulong len
= strtoul(Z_STRVAL_P(c
), NULL
, 10);
184 if (len
> remaining
) {
185 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
);
188 phpstr_from_string_ex(PHPSTR(msg
), body
, len
);
189 continue_at
= body
+ len
;
192 /* message has content-range header */
193 if ((c
= http_message_header(msg
, "Content-Range"))) {
194 ulong total
= 0, start
= 0, end
= 0, len
= 0;
196 if (!strncasecmp(Z_STRVAL_P(c
), "bytes", lenof("bytes")) &&
197 (Z_STRVAL_P(c
)[lenof("bytes")] == ':' || Z_STRVAL_P(c
)[lenof("bytes")] == ' ')) {
198 char *total_at
= NULL
, *end_at
= NULL
;
199 char *start_at
= Z_STRVAL_P(c
) + sizeof("bytes");
201 start
= strtoul(start_at
, &end_at
, 10);
203 end
= strtoul(end_at
+ 1, &total_at
, 10);
204 if (total_at
&& strncmp(total_at
+ 1, "*", 1)) {
205 total
= strtoul(total_at
+ 1, NULL
, 10);
207 if ((len
= (end
+ 1 - start
)) > remaining
) {
208 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
);
211 if (end
>= start
&& (!total
|| end
< total
)) {
212 phpstr_from_string_ex(PHPSTR(msg
), body
, len
);
213 continue_at
= body
+ len
;
219 http_error_ex(HE_WARNING
, HTTP_E_MALFORMED_HEADERS
, "Invalid Content-Range header: %s", Z_STRVAL_P(c
));
223 /* no headers that indicate content length */
224 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
225 phpstr_from_string_ex(PHPSTR(msg
), body
, remaining
);
230 #if defined(HTTP_HAVE_ZLIB) || defined(HAVE_ZLIB)
231 /* check for compressed data */
232 if (http_message_header(msg
, "Vary") && (c
= http_message_header(msg
, "Content-Encoding"))) {
233 char *decoded
= NULL
;
234 size_t decoded_len
= 0;
235 # if !defined(HTTP_HAVE_ZLIB)
236 zval func
, retval
, arg
, *args
[1];
241 # endif /* !HTTP_HAVE_ZLIB */
243 # define DECODE_WITH_EXT_ZLIB(function, S, L) \
244 ZVAL_STRINGL(&func, function, lenof(function), 0); \
245 ZVAL_STRINGL(&arg, (S), (L), 0); \
246 if (SUCCESS == call_user_function(EG(function_table), NULL, &func, &retval, 1, args TSRMLS_CC)) { \
247 if (Z_TYPE(retval) == IS_STRING) { \
248 decoded = Z_STRVAL(retval); \
249 decoded_len = Z_STRLEN(retval); \
253 if (!strcasecmp(Z_STRVAL_P(c
), "gzip") || !strcasecmp(Z_STRVAL_P(c
), "x-gzip")) {
254 # ifndef HTTP_HAVE_ZLIB
255 DECODE_WITH_EXT_ZLIB("gzinflate", PHPSTR_VAL(msg
) + 10, PHPSTR_LEN(msg
) - 18);
257 http_encoding_inflate(PHPSTR_VAL(msg
), PHPSTR_LEN(msg
), &decoded
, &decoded_len
);
258 } else if (!strcasecmp(Z_STRVAL_P(c
), "deflate") || !strcasecmp(Z_STRVAL_P(c
), "compress") || !strcasecmp(Z_STRVAL_P(c
), "x-compress")) {
259 http_encoding_inflate(PHPSTR_VAL(msg
), PHPSTR_LEN(msg
), &decoded
, &decoded_len
);
260 # endif /* HTTP_HAVE_ZLIB */
264 zval
*len
, **original_len
;
268 tmp_len
= (int) spprintf(&tmp
, 0, "%zu", decoded_len
);
270 ZVAL_STRINGL(len
, tmp
, tmp_len
, 0);
273 zend_hash_add(&msg
->hdrs
, "X-Original-Content-Encoding", sizeof("X-Original-Content-Encoding"), (void *) &c
, sizeof(zval
*), NULL
);
274 zend_hash_del(&msg
->hdrs
, "Content-Encoding", sizeof("Content-Encoding"));
275 if (SUCCESS
== zend_hash_find(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"), (void **) &original_len
)) {
276 ZVAL_ADDREF(*original_len
);
277 zend_hash_add(&msg
->hdrs
, "X-Original-Content-Length", sizeof("X-Original-Content-Length"), (void *) original_len
, sizeof(zval
*), NULL
);
278 zend_hash_update(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"), (void *) &len
, sizeof(zval
*), NULL
);
280 zend_hash_add(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"), (void *) &len
, sizeof(zval
*), NULL
);
283 phpstr_dtor(PHPSTR(msg
));
284 PHPSTR(msg
)->data
= decoded
;
285 PHPSTR(msg
)->used
= decoded_len
;
286 PHPSTR(msg
)->free
= 1;
289 #endif /* HTTP_HAVE_ZLIB || HAVE_ZLIB */
291 /* check for following messages */
292 if (continue_at
&& (continue_at
< (message
+ message_length
))) {
293 while (isspace(*continue_at
)) ++continue_at
;
294 if (continue_at
< (message
+ message_length
)) {
295 http_message
*next
= NULL
, *most
= NULL
;
297 /* set current message to parent of most parent following messages and return deepest */
298 if ((most
= next
= http_message_parse_rel(NULL
, continue_at
, message
+ message_length
- continue_at
))) {
299 while (most
->parent
) most
= most
->parent
;
310 PHP_HTTP_API
void _http_message_tostring(http_message
*msg
, char **string
, size_t *length
)
318 phpstr_init_ex(&str
, 4096, 0);
322 case HTTP_MSG_REQUEST
:
323 phpstr_appendf(&str
, "%s %s HTTP/%1.1f" HTTP_CRLF
,
324 msg
->http
.info
.request
.method
,
325 msg
->http
.info
.request
.URI
,
329 case HTTP_MSG_RESPONSE
:
330 phpstr_appendf(&str
, "HTTP/%1.1f %d%s%s" HTTP_CRLF
,
332 msg
->http
.info
.response
.code
,
333 *msg
->http
.info
.response
.status
? " ":"",
334 msg
->http
.info
.response
.status
);
342 FOREACH_HASH_KEYVAL(pos1
, &msg
->hdrs
, key
, idx
, header
) {
344 zval
**single_header
;
346 switch (Z_TYPE_PP(header
))
349 phpstr_appendf(&str
, "%s: %s" HTTP_CRLF
, key
, Z_STRVAL_PP(header
));
355 FOREACH_VAL(pos2
, *header
, single_header
) {
356 phpstr_appendf(&str
, "%s: %s" HTTP_CRLF
, key
, Z_STRVAL_PP(single_header
));
366 if (PHPSTR_LEN(msg
)) {
367 phpstr_appends(&str
, HTTP_CRLF
);
368 phpstr_append(&str
, PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
369 phpstr_appends(&str
, HTTP_CRLF
);
372 data
= phpstr_data(&str
, string
, length
);
380 PHP_HTTP_API
void _http_message_serialize(http_message
*message
, char **string
, size_t *length
)
389 http_message_tostring(message
, &buf
, &len
);
390 phpstr_prepend(&str
, buf
, len
);
392 } while ((message
= message
->parent
));
394 buf
= phpstr_data(&str
, string
, length
);
402 PHP_HTTP_API
void _http_message_tostruct_recursive(http_message
*msg
, zval
*obj TSRMLS_DC
)
407 INIT_ZARR(strct
, HASH_OF(obj
));
409 add_assoc_long(&strct
, "type", msg
->type
);
410 add_assoc_double(&strct
, "httpVersion", msg
->http
.version
);
413 case HTTP_MSG_RESPONSE
:
414 add_assoc_long(&strct
, "responseCode", msg
->http
.info
.response
.code
);
415 add_assoc_string(&strct
, "responseStatus", msg
->http
.info
.response
.status
, 1);
418 case HTTP_MSG_REQUEST
:
419 add_assoc_string(&strct
, "requestMethod", msg
->http
.info
.request
.method
, 1);
420 add_assoc_string(&strct
, "requestUri", msg
->http
.info
.request
.URI
, 1);
424 /* avoid compiler warning */
428 MAKE_STD_ZVAL(headers
);
430 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
431 add_assoc_zval(&strct
, "headers", headers
);
433 add_assoc_stringl(&strct
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
), 1);
438 MAKE_STD_ZVAL(parent
);
439 if (Z_TYPE_P(obj
) == IS_ARRAY
) {
444 add_assoc_zval(&strct
, "parentMessage", parent
);
445 http_message_tostruct_recursive(msg
->parent
, parent
);
447 add_assoc_null(&strct
, "parentMessage");
451 PHP_HTTP_API STATUS
_http_message_send(http_message
*message TSRMLS_DC
)
455 switch (message
->type
)
457 case HTTP_MSG_RESPONSE
:
464 FOREACH_HASH_KEYVAL(pos1
, &message
->hdrs
, key
, idx
, val
) {
466 if (Z_TYPE_PP(val
) == IS_ARRAY
) {
471 FOREACH_VAL(pos2
, *val
, data
) {
472 http_send_header_ex(key
, strlen(key
), Z_STRVAL_PP(data
), Z_STRLEN_PP(data
), first
, NULL
);
476 http_send_header_ex(key
, strlen(key
), Z_STRVAL_PP(val
), Z_STRLEN_PP(val
), 1, NULL
);
481 rs
= SUCCESS
== http_send_status(message
->http
.info
.response
.code
) &&
482 SUCCESS
== http_send_data(PHPSTR_VAL(message
), PHPSTR_LEN(message
)) ?
487 case HTTP_MSG_REQUEST
:
489 #ifdef HTTP_HAVE_CURL
491 http_request request
;
492 zval
**zhost
, options
, headers
;
494 INIT_PZVAL(&options
);
495 INIT_PZVAL(&headers
);
496 array_init(&options
);
497 array_init(&headers
);
498 zend_hash_copy(Z_ARRVAL(headers
), &message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
499 add_assoc_zval(&options
, "headers", &headers
);
501 /* check host header */
502 if (SUCCESS
== zend_hash_find(&message
->hdrs
, "Host", sizeof("Host"), (void **) &zhost
)) {
504 php_url parts
, *url
= php_url_parse(message
->http
.info
.request
.URI
);
506 memset(&parts
, 0, sizeof(php_url
));
509 if ((colon
= strchr(Z_STRVAL_PP(zhost
), ':'))) {
510 parts
.port
= atoi(colon
+ 1);
511 parts
.host
= estrndup(Z_STRVAL_PP(zhost
), (Z_STRVAL_PP(zhost
) - colon
- 1));
513 parts
.host
= estrndup(Z_STRVAL_PP(zhost
), Z_STRLEN_PP(zhost
));
516 http_build_url(url
, &parts
, NULL
, &uri
, NULL
);
520 uri
= http_absolute_url(message
->http
.info
.request
.URI
);
523 if ((request
.meth
= http_request_method_exists(1, 0, message
->http
.info
.request
.method
))) {
524 http_request_body body
;
526 http_request_init_ex(&request
, NULL
, request
.meth
, uri
);
527 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_CSTRING
, PHPSTR_VAL(message
), PHPSTR_LEN(message
), 0);
528 if (SUCCESS
== (rs
= http_request_prepare(&request
, NULL
))) {
529 http_request_exec(&request
);
531 http_request_dtor(&request
);
533 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
,
534 "Cannot send HttpMessage. Request method %s not supported",
535 message
->http
.info
.request
.method
);
539 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "HTTP requests not supported - ext/http was not linked against libcurl.");
546 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is neither of type HTTP_MSG_REQUEST nor HTTP_MSG_RESPONSE");
553 PHP_HTTP_API http_message
*_http_message_dup(http_message
*msg TSRMLS_DC
)
559 char *serialized_data
;
560 size_t serialized_length
;
562 http_message_serialize(msg
, &serialized_data
, &serialized_length
);
563 new = http_message_parse(serialized_data
, serialized_length
);
564 efree(serialized_data
);
568 PHP_HTTP_API
void _http_message_dtor(http_message
*message
)
571 zend_hash_destroy(&message
->hdrs
);
572 phpstr_dtor(PHPSTR(message
));
574 switch (message
->type
)
576 case HTTP_MSG_REQUEST
:
577 STR_SET(message
->http
.info
.request
.method
, NULL
);
578 STR_SET(message
->http
.info
.request
.URI
, NULL
);
581 case HTTP_MSG_RESPONSE
:
582 STR_SET(message
->http
.info
.response
.status
, NULL
);
591 PHP_HTTP_API
void _http_message_free(http_message
**message
)
594 if ((*message
)->parent
) {
595 http_message_free(&(*message
)->parent
);
597 http_message_dtor(*message
);
608 * vim600: noet sw=4 ts=4 fdm=marker
609 * vim<600: noet sw=4 ts=4