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"
31 #include "php_http_encoding_api.h"
33 #include "phpstr/phpstr.h"
35 ZEND_EXTERN_MODULE_GLOBALS(http
);
37 #define http_message_info_callback _http_message_info_callback
38 static void _http_message_info_callback(http_message
**message
, HashTable
**headers
, http_info
*info TSRMLS_DC
)
40 http_message
*old
= *message
;
43 if (old
->type
|| zend_hash_num_elements(&old
->hdrs
) || PHPSTR_LEN(old
)) {
44 (*message
) = http_message_new();
45 (*message
)->parent
= old
;
46 (*headers
) = &((*message
)->hdrs
);
49 (*message
)->http
.version
= info
->http
.version
;
54 (*message
)->type
= HTTP_MSG_REQUEST
;
55 HTTP_INFO(*message
).request
.URI
= estrdup(HTTP_INFO(info
).request
.URI
);
56 HTTP_INFO(*message
).request
.method
= estrdup(HTTP_INFO(info
).request
.method
);
59 case IS_HTTP_RESPONSE
:
60 (*message
)->type
= HTTP_MSG_RESPONSE
;
61 HTTP_INFO(*message
).response
.code
= HTTP_INFO(info
).response
.code
;
62 HTTP_INFO(*message
).response
.status
= estrdup(HTTP_INFO(info
).response
.status
);
67 #define http_message_init_type _http_message_init_type
68 static inline void _http_message_init_type(http_message
*message
, http_message_type type
)
70 message
->http
.version
= .0;
72 switch (message
->type
= type
)
74 case HTTP_MSG_RESPONSE
:
75 message
->http
.info
.response
.code
= 0;
76 message
->http
.info
.response
.status
= NULL
;
79 case HTTP_MSG_REQUEST
:
80 message
->http
.info
.request
.method
= NULL
;
81 message
->http
.info
.request
.URI
= NULL
;
90 PHP_HTTP_API http_message
*_http_message_init_ex(http_message
*message
, http_message_type type
)
93 message
= ecalloc(1, sizeof(http_message
));
96 http_message_init_type(message
, type
);
97 message
->parent
= NULL
;
98 phpstr_init(&message
->body
);
99 zend_hash_init(&message
->hdrs
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
105 PHP_HTTP_API
void _http_message_set_type(http_message
*message
, http_message_type type
)
107 /* just act if different */
108 if (type
!= message
->type
) {
110 /* free request info */
111 switch (message
->type
)
113 case HTTP_MSG_REQUEST
:
114 STR_FREE(message
->http
.info
.request
.method
);
115 STR_FREE(message
->http
.info
.request
.URI
);
118 case HTTP_MSG_RESPONSE
:
119 STR_FREE(message
->http
.info
.response
.status
);
127 http_message_init_type(message
, type
);
131 PHP_HTTP_API http_message
*_http_message_parse_ex(http_message
*msg
, const char *message
, size_t message_length TSRMLS_DC
)
133 const char *body
= NULL
;
134 zend_bool free_msg
= msg
? 0 : 1;
136 if ((!message
) || (message_length
< HTTP_MSG_MIN_SIZE
)) {
140 msg
= http_message_init(msg
);
142 if (SUCCESS
!= http_parse_headers_cb(message
, &msg
->hdrs
, 1, (http_info_callback
) http_message_info_callback
, (void **) &msg
)) {
144 http_message_free(&msg
);
149 /* header parsing stops at (CR)LF (CR)LF */
150 if (body
= http_locate_body(message
)) {
152 const char *continue_at
= NULL
;
154 /* message has chunked transfer encoding */
155 if ((c
= http_message_header(msg
, "Transfer-Encoding")) && (!strcasecmp("chunked", Z_STRVAL_P(c
)))) {
159 /* decode and replace Transfer-Encoding with Content-Length header */
160 if (continue_at
= http_encoding_dechunk(body
, message
+ message_length
- body
, &decoded
, &decoded_len
)) {
165 tmp_len
= (int) spprintf(&tmp
, 0, "%lu", (ulong
) decoded_len
);
167 ZVAL_STRINGL(len
, tmp
, tmp_len
, 0);
169 zend_hash_del(&msg
->hdrs
, "Transfer-Encoding", sizeof("Transfer-Encoding"));
170 zend_hash_del(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"));
171 zend_hash_add(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"), (void *) &len
, sizeof(zval
*), NULL
);
173 phpstr_from_string_ex(PHPSTR(msg
), decoded
, decoded_len
);
178 /* message has content-length header */
179 if (c
= http_message_header(msg
, "Content-Length")) {
180 long len
= atol(Z_STRVAL_P(c
));
181 phpstr_from_string_ex(PHPSTR(msg
), body
, len
);
182 continue_at
= body
+ len
;
185 /* message has content-range header */
186 if (c
= http_message_header(msg
, "Content-Range")) {
187 ulong total
= 0, start
= 0, end
= 0;
189 if (!strncasecmp(Z_STRVAL_P(c
), "bytes=", lenof("bytes="))) {
190 char *total_at
= NULL
, *end_at
= NULL
;
191 char *start_at
= Z_STRVAL_P(c
) + lenof("bytes=");
193 start
= strtoul(start_at
, &end_at
, 10);
195 end
= strtoul(end_at
+ 1, &total_at
, 10);
196 if (total_at
&& strncmp(total_at
+ 1, "*", 1)) {
197 total
= strtoul(total_at
+ 1, NULL
, 10);
200 if (end
>= start
&& (!total
|| end
< total
)) {
201 phpstr_from_string_ex(PHPSTR(msg
), body
, (size_t) (end
+ 1 - start
));
202 continue_at
= body
+ (end
+ 1 - start
);
208 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
, message
+ message_length
- body
);
219 #ifdef HTTP_HAVE_ZLIB
220 /* check for compressed data */
221 if (c
= http_message_header(msg
, "Content-Encoding")) {
222 char *decoded
= NULL
;
223 size_t decoded_len
= 0;
225 if (!strcasecmp(Z_STRVAL_P(c
), "gzip")) {
226 http_encoding_gzdecode(PHPSTR_VAL(msg
), PHPSTR_LEN(msg
), &decoded
, &decoded_len
);
228 if (!strcasecmp(Z_STRVAL_P(c
), "deflate")) {
229 http_encoding_inflate(PHPSTR_VAL(msg
), PHPSTR_LEN(msg
), &decoded
, &decoded_len
);
231 if (!strcasecmp(Z_STRVAL_P(c
), "compress")) {
232 http_encoding_uncompress(PHPSTR_VAL(msg
), PHPSTR_LEN(msg
), &decoded
, &decoded_len
);
235 if (decoded
&& decoded_len
) {
240 tmp_len
= (int) spprintf(&tmp
, 0, "%lu", (ulong
) decoded_len
);
242 ZVAL_STRINGL(len
, tmp
, tmp_len
, 0);
244 zend_hash_del(&msg
->hdrs
, "Content-Encoding", sizeof("Content-Encoding"));
245 zend_hash_del(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"));
246 zend_hash_add(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"), (void *) &len
, sizeof(zval
*), NULL
);
248 phpstr_dtor(PHPSTR(msg
));
249 PHPSTR(msg
)->data
= decoded
;
250 PHPSTR(msg
)->used
= decoded_len
;
251 PHPSTR(msg
)->free
= 1;
256 /* check for following messages */
258 while (isspace(*continue_at
)) ++continue_at
;
259 if (continue_at
< (message
+ message_length
)) {
260 http_message
*next
= NULL
, *most
= NULL
;
262 /* set current message to parent of most parent following messages and return deepest */
263 if (most
= next
= http_message_parse(continue_at
, message
+ message_length
- continue_at
)) {
264 while (most
->parent
) most
= most
->parent
;
275 PHP_HTTP_API
void _http_message_tostring(http_message
*msg
, char **string
, size_t *length
)
282 phpstr_init_ex(&str
, 4096, 0);
286 case HTTP_MSG_REQUEST
:
287 phpstr_appendf(&str
, "%s %s HTTP/%1.1f" HTTP_CRLF
,
288 msg
->http
.info
.request
.method
,
289 msg
->http
.info
.request
.URI
,
293 case HTTP_MSG_RESPONSE
:
294 phpstr_appendf(&str
, "HTTP/%1.1f %d%s%s" HTTP_CRLF
,
296 msg
->http
.info
.response
.code
,
297 *msg
->http
.info
.response
.status
? " ":"",
298 msg
->http
.info
.response
.status
);
306 FOREACH_HASH_KEYVAL(&msg
->hdrs
, key
, idx
, header
) {
308 zval
**single_header
;
310 switch (Z_TYPE_PP(header
))
313 phpstr_appendf(&str
, "%s: %s" HTTP_CRLF
, key
, Z_STRVAL_PP(header
));
317 FOREACH_VAL(*header
, single_header
) {
318 phpstr_appendf(&str
, "%s: %s" HTTP_CRLF
, key
, Z_STRVAL_PP(single_header
));
327 if (PHPSTR_LEN(msg
)) {
328 phpstr_appends(&str
, HTTP_CRLF
);
329 phpstr_append(&str
, PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
330 phpstr_appends(&str
, HTTP_CRLF
);
333 data
= phpstr_data(&str
, string
, length
);
341 PHP_HTTP_API
void _http_message_serialize(http_message
*message
, char **string
, size_t *length
)
350 http_message_tostring(message
, &buf
, &len
);
351 phpstr_prepend(&str
, buf
, len
);
353 } while (message
= message
->parent
);
355 buf
= phpstr_data(&str
, string
, length
);
363 PHP_HTTP_API
void _http_message_tostruct_recursive(http_message
*msg
, zval
*obj TSRMLS_DC
)
368 INIT_ZARR(strct
, HASH_OF(obj
));
370 add_assoc_long(&strct
, "type", msg
->type
);
371 add_assoc_double(&strct
, "httpVersion", msg
->http
.version
);
374 case HTTP_MSG_RESPONSE
:
375 add_assoc_long(&strct
, "responseCode", msg
->http
.info
.response
.code
);
376 add_assoc_string(&strct
, "responseStatus", msg
->http
.info
.response
.status
, 1);
379 case HTTP_MSG_REQUEST
:
380 add_assoc_string(&strct
, "requestMethod", msg
->http
.info
.request
.method
, 1);
381 add_assoc_string(&strct
, "requestUri", msg
->http
.info
.request
.URI
, 1);
385 MAKE_STD_ZVAL(headers
);
387 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
388 add_assoc_zval(&strct
, "headers", headers
);
390 add_assoc_stringl(&strct
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
), 1);
395 MAKE_STD_ZVAL(parent
);
396 if (Z_TYPE_P(obj
) == IS_ARRAY
) {
401 add_assoc_zval(&strct
, "parentMessage", parent
);
402 http_message_tostruct_recursive(msg
->parent
, parent
);
403 zval_ptr_dtor(&parent
);
405 add_assoc_null(&strct
, "parentMessage");
409 PHP_HTTP_API STATUS
_http_message_send(http_message
*message TSRMLS_DC
)
413 switch (message
->type
)
415 case HTTP_MSG_RESPONSE
:
421 FOREACH_HASH_KEYVAL(&message
->hdrs
, key
, idx
, val
) {
423 if (Z_TYPE_PP(val
) == IS_ARRAY
) {
427 FOREACH_VAL(*val
, data
) {
428 http_send_header_ex(key
, strlen(key
), Z_STRVAL_PP(data
), Z_STRLEN_PP(data
), first
, NULL
);
432 http_send_header_ex(key
, strlen(key
), Z_STRVAL_PP(val
), Z_STRLEN_PP(val
), 1, NULL
);
437 rs
= SUCCESS
== http_send_status(message
->http
.info
.response
.code
) &&
438 SUCCESS
== http_send_data(PHPSTR_VAL(message
), PHPSTR_LEN(message
)) ?
443 case HTTP_MSG_REQUEST
:
445 #ifdef HTTP_HAVE_CURL
447 zval
**zhost
, options
, headers
;
449 INIT_PZVAL(&options
);
450 INIT_PZVAL(&headers
);
451 array_init(&options
);
452 array_init(&headers
);
453 zend_hash_copy(Z_ARRVAL(headers
), &message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
454 add_assoc_zval(&options
, "headers", &headers
);
456 /* check host header */
457 if (SUCCESS
== zend_hash_find(&message
->hdrs
, "Host", sizeof("Host"), (void **) &zhost
)) {
458 char *colon
= NULL
, *host
= NULL
;
463 if (colon
= strchr(Z_STRVAL_PP(zhost
), ':')) {
464 port
= atoi(colon
+ 1);
465 host
= estrndup(Z_STRVAL_PP(zhost
), host_len
= (Z_STRVAL_PP(zhost
) - colon
- 1));
467 host
= estrndup(Z_STRVAL_PP(zhost
), host_len
= Z_STRLEN_PP(zhost
));
469 uri
= http_absolute_uri_ex(
470 message
->http
.info
.request
.URI
, strlen(message
->http
.info
.request
.URI
),
471 NULL
, 0, host
, host_len
, port
);
474 uri
= http_absolute_uri(message
->http
.info
.request
.URI
);
477 if (!strcasecmp("POST", message
->http
.info
.request
.method
)) {
478 http_request_body body
= {HTTP_REQUEST_BODY_CSTRING
, PHPSTR_VAL(message
), PHPSTR_LEN(message
)};
479 rs
= http_post(uri
, &body
, Z_ARRVAL(options
), NULL
, NULL
);
481 if (!strcasecmp("GET", message
->http
.info
.request
.method
)) {
482 rs
= http_get(uri
, Z_ARRVAL(options
), NULL
, NULL
);
484 if (!strcasecmp("HEAD", message
->http
.info
.request
.method
)) {
485 rs
= http_head(uri
, Z_ARRVAL(options
), NULL
, NULL
);
487 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
,
488 "Cannot send HttpMessage. Request method %s not supported",
489 message
->http
.info
.request
.method
);
494 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "HTTP requests not supported - ext/http was not linked against libcurl.");
501 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is neither of type HTTP_MSG_REQUEST nor HTTP_MSG_RESPONSE");
508 PHP_HTTP_API http_message
*_http_message_dup(http_message
*msg TSRMLS_DC
)
514 char *serialized_data
;
515 size_t serialized_length
;
517 http_message_serialize(msg
, &serialized_data
, &serialized_length
);
518 new = http_message_parse(serialized_data
, serialized_length
);
519 efree(serialized_data
);
523 PHP_HTTP_API
void _http_message_dtor(http_message
*message
)
526 zend_hash_destroy(&message
->hdrs
);
527 phpstr_dtor(PHPSTR(message
));
529 switch (message
->type
)
531 case HTTP_MSG_REQUEST
:
532 STR_SET(message
->http
.info
.request
.method
, NULL
);
533 STR_SET(message
->http
.info
.request
.URI
, NULL
);
536 case HTTP_MSG_RESPONSE
:
537 STR_SET(message
->http
.info
.response
.status
, NULL
);
546 PHP_HTTP_API
void _http_message_free(http_message
**message
)
549 if ((*message
)->parent
) {
550 http_message_free(&(*message
)->parent
);
552 http_message_dtor(*message
);
563 * vim600: noet sw=4 ts=4 fdm=marker
564 * vim<600: noet sw=4 ts=4