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-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 static void message_headers(php_http_message_t
*msg
, php_http_buffer_t
*str
);
17 zend_bool
php_http_message_info_callback(php_http_message_t
**message
, HashTable
**headers
, php_http_info_t
*info
)
19 php_http_message_t
*old
= *message
;
22 if (!old
|| old
->type
|| zend_hash_num_elements(&old
->hdrs
)) {
23 (*message
) = php_http_message_init(NULL
, 0, NULL
);
24 (*message
)->parent
= old
;
26 (*headers
) = &((*message
)->hdrs
);
31 php_http_message_set_info(*message
, info
);
34 return old
!= *message
;
37 php_http_message_t
*php_http_message_init(php_http_message_t
*message
, php_http_message_type_t type
, php_http_message_body_t
*body
)
40 message
= emalloc(sizeof(*message
));
42 memset(message
, 0, sizeof(*message
));
44 php_http_message_set_type(message
, type
);
45 message
->http
.version
.major
= 1;
46 message
->http
.version
.minor
= 1;
47 zend_hash_init(&message
->hdrs
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
48 message
->body
= body
? body
: php_http_message_body_init(NULL
, NULL
);
53 php_http_message_t
*php_http_message_init_env(php_http_message_t
*message
, php_http_message_type_t type
)
55 int free_msg
= !message
;
57 php_http_message_body_t
*mbody
;
60 case PHP_HTTP_REQUEST
:
61 mbody
= php_http_env_get_request_body();
62 php_http_message_body_addref(mbody
);
63 message
= php_http_message_init(message
, type
, mbody
);
64 if ((sval
= php_http_env_get_server_var(ZEND_STRL("SERVER_PROTOCOL"), 1)) && !strncmp(Z_STRVAL_P(sval
), "HTTP/", lenof("HTTP/"))) {
65 php_http_version_parse(&message
->http
.version
, Z_STRVAL_P(sval
));
67 if ((sval
= php_http_env_get_server_var(ZEND_STRL("REQUEST_METHOD"), 1))) {
68 message
->http
.info
.request
.method
= estrdup(Z_STRVAL_P(sval
));
70 if ((sval
= php_http_env_get_server_var(ZEND_STRL("REQUEST_URI"), 1))) {
71 message
->http
.info
.request
.url
= php_http_url_parse(Z_STRVAL_P(sval
), Z_STRLEN_P(sval
), PHP_HTTP_URL_STDFLAGS
);
74 php_http_env_get_request_headers(&message
->hdrs
);
77 case PHP_HTTP_RESPONSE
:
78 message
= php_http_message_init(message
, type
, NULL
);
79 if (!SG(sapi_headers
).http_status_line
|| !php_http_info_parse((php_http_info_t
*) &message
->http
, SG(sapi_headers
).http_status_line
)) {
80 if (!(message
->http
.info
.response
.code
= SG(sapi_headers
).http_response_code
)) {
81 message
->http
.info
.response
.code
= 200;
83 message
->http
.info
.response
.status
= estrdup(php_http_env_get_response_status_for_code(message
->http
.info
.response
.code
));
86 php_http_env_get_response_headers(&message
->hdrs
);
87 if (php_output_get_level()) {
88 if (php_output_get_status() & PHP_OUTPUT_SENT
) {
89 php_error_docref(NULL
, E_WARNING
, "Could not fetch response body, output has already been sent at %s:%d", php_output_get_start_filename(), php_output_get_start_lineno());
92 } else if (SUCCESS
!= php_output_get_contents(&tval
)) {
93 php_error_docref(NULL
, E_WARNING
, "Could not fetch response body");
96 php_http_message_body_append(message
->body
, Z_STRVAL(tval
), Z_STRLEN(tval
));
106 php_http_message_free(&message
);
117 php_http_message_t
*php_http_message_parse(php_http_message_t
*msg
, const char *str
, size_t len
, zend_bool greedy
)
119 php_http_message_parser_t p
;
120 php_http_buffer_t buf
;
121 unsigned flags
= PHP_HTTP_MESSAGE_PARSER_CLEANUP
;
124 php_http_buffer_from_string_ex(&buf
, str
, len
);
125 php_http_message_parser_init(&p
);
127 if ((free_msg
= !msg
)) {
128 msg
= php_http_message_init(NULL
, 0, NULL
);
132 flags
|= PHP_HTTP_MESSAGE_PARSER_GREEDY
;
134 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE
== php_http_message_parser_parse(&p
, &buf
, flags
, &msg
)) {
136 php_http_message_free(&msg
);
141 php_http_message_parser_dtor(&p
);
142 php_http_buffer_dtor(&buf
);
147 zval
*php_http_message_header(php_http_message_t
*msg
, const char *key_str
, size_t key_len
)
151 ALLOCA_FLAG(free_key
);
153 key
= do_alloca(key_len
+ 1, free_key
);
155 memcpy(key
, key_str
, key_len
);
157 php_http_pretty_key(key
, key_len
, 1, 1);
159 ret
= zend_symtable_str_find(&msg
->hdrs
, key
, key_len
);
161 free_alloca(key
, free_key
);
166 zend_bool
php_http_message_is_multipart(php_http_message_t
*msg
, char **boundary
)
168 zend_string
*ct
= php_http_message_header_string(msg
, ZEND_STRL("Content-Type"));
169 zend_bool is_multipart
= 0;
172 php_http_params_opts_t popts
;
175 ZEND_INIT_SYMTABLE(¶ms
);
176 php_http_params_opts_default_get(&popts
);
177 popts
.input
.str
= ct
->val
;
178 popts
.input
.len
= ct
->len
;
180 if (EXPECTED(php_http_params_parse(¶ms
, &popts
))) {
185 zend_hash_internal_pointer_reset(¶ms
);
187 if (EXPECTED((cur
= zend_hash_get_current_data(¶ms
))
188 && (Z_TYPE_P(cur
) == IS_ARRAY
)
189 && (HASH_KEY_IS_STRING
== zend_hash_get_current_key(¶ms
, &ct_str
, &index
)))
191 if (php_http_match(ct_str
->val
, "multipart", PHP_HTTP_MATCH_WORD
)) {
195 if (EXPECTED(boundary
196 && (arg
= zend_hash_str_find(Z_ARRVAL_P(cur
), ZEND_STRL("arguments")))
197 && Z_TYPE_P(arg
) == IS_ARRAY
)
200 php_http_arrkey_t key
;
202 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arg
), key
.h
, key
.key
, val
)
204 if (key
.key
&& key
.key
->len
== lenof("boundary") && !strcasecmp(key
.key
->val
, "boundary")) {
205 zend_string
*bnd
= zval_get_string(val
);
207 if (EXPECTED(bnd
->len
)) {
208 *boundary
= estrndup(bnd
->val
, bnd
->len
);
210 zend_string_release(bnd
);
213 ZEND_HASH_FOREACH_END();
218 zend_hash_destroy(¶ms
);
219 zend_string_release(ct
);
226 void php_http_message_set_type(php_http_message_t
*message
, php_http_message_type_t type
)
228 /* just act if different */
229 if (type
!= message
->type
) {
231 /* free request info */
232 switch (message
->type
) {
233 case PHP_HTTP_REQUEST
:
234 PTR_FREE(message
->http
.info
.request
.method
);
235 PTR_FREE(message
->http
.info
.request
.url
);
238 case PHP_HTTP_RESPONSE
:
239 PTR_FREE(message
->http
.info
.response
.status
);
246 message
->type
= type
;
247 memset(&message
->http
, 0, sizeof(message
->http
));
251 void php_http_message_set_info(php_http_message_t
*message
, php_http_info_t
*info
)
253 php_http_message_set_type(message
, info
->type
);
254 message
->http
.version
= info
->http
.version
;
255 switch (message
->type
) {
256 case PHP_HTTP_REQUEST
:
257 PTR_SET(PHP_HTTP_INFO(message
).request
.url
, PHP_HTTP_INFO(info
).request
.url
? php_http_url_copy(PHP_HTTP_INFO(info
).request
.url
, 0) : NULL
);
258 PTR_SET(PHP_HTTP_INFO(message
).request
.method
, PHP_HTTP_INFO(info
).request
.method
? estrdup(PHP_HTTP_INFO(info
).request
.method
) : NULL
);
261 case PHP_HTTP_RESPONSE
:
262 PHP_HTTP_INFO(message
).response
.code
= PHP_HTTP_INFO(info
).response
.code
;
263 PTR_SET(PHP_HTTP_INFO(message
).response
.status
, PHP_HTTP_INFO(info
).response
.status
? estrdup(PHP_HTTP_INFO(info
).response
.status
) : NULL
);
271 void php_http_message_update_headers(php_http_message_t
*msg
)
277 if (php_http_message_body_stream(msg
->body
)->readfilters
.head
) {
278 /* if a read stream filter is attached to the body the caller must also care for the headers */
279 } else if (php_http_message_header(msg
, ZEND_STRL("Content-Range"))) {
280 /* don't mess around with a Content-Range message */
281 } else if ((size
= php_http_message_body_size(msg
->body
))) {
283 zend_hash_str_update(&msg
->hdrs
, "Content-Length", lenof("Content-Length"), &h
);
285 if (msg
->body
->boundary
) {
290 if (!(ct
= php_http_message_header_string(msg
, ZEND_STRL("Content-Type")))) {
291 len
= spprintf(&str
, 0, "multipart/form-data; boundary=\"%s\"", msg
->body
->boundary
);
292 ZVAL_STR(&h
, php_http_cs2zs(str
, len
));
293 zend_hash_str_update(&msg
->hdrs
, "Content-Type", lenof("Content-Type"), &h
);
294 } else if (!php_http_match(ct
->val
, "boundary=", PHP_HTTP_MATCH_WORD
)) {
295 len
= spprintf(&str
, 0, "%s; boundary=\"%s\"", ct
->val
, msg
->body
->boundary
);
296 ZVAL_STR(&h
, php_http_cs2zs(str
, len
));
297 zend_hash_str_update(&msg
->hdrs
, "Content-Type", lenof("Content-Type"), &h
);
298 zend_string_release(ct
);
300 zend_string_release(ct
);
303 } else if ((cl
= php_http_message_header_string(msg
, ZEND_STRL("Content-Length")))) {
304 if (!zend_string_equals_literal(cl
, "0")) {
305 /* body->size == 0, so get rid of old Content-Length */
306 zend_hash_str_del(&msg
->hdrs
, ZEND_STRL("Content-Length"));
308 zend_string_release(cl
);
309 } else if (msg
->type
== PHP_HTTP_REQUEST
) {
310 if (!php_http_message_header(msg
, ZEND_STRL("Transfer-Encoding"))) {
311 /* no filter, no CR, no size, no TE, no CL */
312 if (0 <= php_http_select_str(msg
->http
.info
.request
.method
, 3, "POST", "PUT", "PATCH")) {
313 /* quoting RFC7230#section-3.3.2
314 A user agent SHOULD send a Content-Length in a request message when
315 no Transfer-Encoding is sent and the request method defines a meaning
316 for an enclosed payload body. For example, a Content-Length header
317 field is normally sent in a POST request even when the value is 0
318 (indicating an empty payload body). A user agent SHOULD NOT send a
319 Content-Length header field when the request message does not contain
320 a payload body and the method semantics do not anticipate such a
324 zend_hash_str_update(&msg
->hdrs
, "Content-Length", lenof("Content-Length"), &h
);
330 static void message_headers(php_http_message_t
*msg
, php_http_buffer_t
*str
)
335 php_http_info_to_string((php_http_info_t
*) msg
, &tmp
, &len
, PHP_HTTP_CRLF
);
336 php_http_message_update_headers(msg
);
338 php_http_buffer_append(str
, tmp
, len
);
339 php_http_header_to_string(str
, &msg
->hdrs
);
343 void php_http_message_to_callback(php_http_message_t
*msg
, php_http_pass_callback_t cb
, void *cb_arg
)
345 php_http_buffer_t str
;
347 php_http_buffer_init_ex(&str
, 0x1000, 0);
348 message_headers(msg
, &str
);
349 cb(cb_arg
, str
.data
, str
.used
);
350 php_http_buffer_dtor(&str
);
352 if (php_http_message_body_size(msg
->body
)) {
353 cb(cb_arg
, ZEND_STRL(PHP_HTTP_CRLF
));
354 php_http_message_body_to_callback(msg
->body
, cb
, cb_arg
, 0, 0);
358 void php_http_message_to_string(php_http_message_t
*msg
, char **string
, size_t *length
)
360 php_http_buffer_t str
;
363 php_http_buffer_init_ex(&str
, 0x1000, 0);
364 message_headers(msg
, &str
);
365 if (php_http_message_body_size(msg
->body
)) {
366 php_http_buffer_appends(&str
, PHP_HTTP_CRLF
);
367 php_http_message_body_to_callback(msg
->body
, (php_http_pass_callback_t
) php_http_buffer_append
, &str
, 0, 0);
370 data
= php_http_buffer_data(&str
, string
, length
);
375 php_http_buffer_dtor(&str
);
378 void php_http_message_serialize(php_http_message_t
*message
, char **string
, size_t *length
)
381 php_http_buffer_t str
;
382 php_http_message_t
*msg
;
384 php_http_buffer_init(&str
);
386 msg
= message
= php_http_message_reverse(message
);
388 php_http_message_to_callback(message
, (php_http_pass_callback_t
) php_http_buffer_append
, &str
);
389 php_http_buffer_appends(&str
, PHP_HTTP_CRLF
);
390 } while ((message
= message
->parent
));
391 php_http_message_reverse(msg
);
393 buf
= php_http_buffer_data(&str
, string
, length
);
398 php_http_buffer_dtor(&str
);
401 php_http_message_t
*php_http_message_reverse(php_http_message_t
*msg
)
403 size_t i
, c
= php_http_message_count(msg
);
406 php_http_message_t
*tmp
= msg
, **arr
;
408 arr
= ecalloc(c
, sizeof(*arr
));
409 for (i
= 0; i
< c
; ++i
) {
413 arr
[0]->parent
= NULL
;
414 for (i
= 0; i
< c
-1; ++i
) {
415 arr
[i
+1]->parent
= arr
[i
];
425 php_http_message_t
*php_http_message_zip(php_http_message_t
*dst
, php_http_message_t
*src
)
427 php_http_message_t
*tmp_dst
, *tmp_src
, *ret
= dst
;
430 tmp_dst
= dst
->parent
;
431 tmp_src
= src
->parent
;
434 src
->parent
= tmp_dst
;
443 php_http_message_t
*php_http_message_copy_ex(php_http_message_t
*from
, php_http_message_t
*to
, zend_bool parents
)
445 php_http_message_t
*temp
, *copy
= NULL
;
446 php_http_info_t info
;
449 info
.type
= from
->type
;
450 info
.http
= from
->http
;
452 copy
= temp
= php_http_message_init(to
, 0, php_http_message_body_copy(from
->body
, NULL
));
453 php_http_message_set_info(temp
, &info
);
454 array_copy(&from
->hdrs
, &temp
->hdrs
);
456 if (parents
) while (from
->parent
) {
457 info
.type
= from
->parent
->type
;
458 info
.http
= from
->parent
->http
;
460 temp
->parent
= php_http_message_init(NULL
, 0, php_http_message_body_copy(from
->parent
->body
, NULL
));
461 php_http_message_set_info(temp
->parent
, &info
);
462 array_copy(&from
->parent
->hdrs
, &temp
->parent
->hdrs
);
472 void php_http_message_dtor(php_http_message_t
*message
)
474 if (EXPECTED(message
)) {
475 zend_hash_destroy(&message
->hdrs
);
476 php_http_message_body_free(&message
->body
);
478 switch (message
->type
) {
479 case PHP_HTTP_REQUEST
:
480 PTR_SET(message
->http
.info
.request
.method
, NULL
);
481 PTR_SET(message
->http
.info
.request
.url
, NULL
);
484 case PHP_HTTP_RESPONSE
:
485 PTR_SET(message
->http
.info
.response
.status
, NULL
);
494 void php_http_message_free(php_http_message_t
**message
)
496 if (EXPECTED(*message
)) {
497 if ((*message
)->parent
) {
498 php_http_message_free(&(*message
)->parent
);
500 php_http_message_dtor(*message
);
506 static zend_class_entry
*php_http_message_class_entry
;
507 zend_class_entry
*php_http_message_get_class_entry(void)
509 return php_http_message_class_entry
;
512 static zval
*php_http_message_object_read_prop(zend_object
*object
, zend_string
*member
, int type
, void **cache_slot
, zval
*rv
);
513 static zval
*php_http_message_object_write_prop(zend_object
*object
, zend_string
*member
, zval
*value
, void **cache_slot
);
515 static zend_object_handlers php_http_message_object_handlers
;
516 static HashTable php_http_message_object_prophandlers
;
518 static void php_http_message_object_prophandler_hash_dtor(zval
*pData
)
520 pefree(Z_PTR_P(pData
), 1);
523 typedef void (*php_http_message_object_prophandler_func_t
)(php_http_message_object_t
*o
, zval
*v
);
525 typedef struct php_http_message_object_prophandler
{
526 php_http_message_object_prophandler_func_t read
;
527 php_http_message_object_prophandler_func_t write
;
528 } php_http_message_object_prophandler_t
;
530 static ZEND_RESULT_CODE
php_http_message_object_add_prophandler(const char *prop_str
, size_t prop_len
, php_http_message_object_prophandler_func_t read
, php_http_message_object_prophandler_func_t write
) {
531 php_http_message_object_prophandler_t h
= { read
, write
};
532 if (!zend_hash_str_add_mem(&php_http_message_object_prophandlers
, prop_str
, prop_len
, (void *) &h
, sizeof(h
))) {
537 static php_http_message_object_prophandler_t
*php_http_message_object_get_prophandler(zend_string
*name_str
) {
538 return zend_hash_str_find_ptr(&php_http_message_object_prophandlers
, name_str
->val
, name_str
->len
);
540 static void php_http_message_object_prophandler_get_type(php_http_message_object_t
*obj
, zval
*return_value
) {
541 zval_ptr_dtor(return_value
);
542 RETVAL_LONG(obj
->message
->type
);
544 static void php_http_message_object_prophandler_set_type(php_http_message_object_t
*obj
, zval
*value
) {
545 php_http_message_set_type(obj
->message
, zval_get_long(value
));
547 static void php_http_message_object_prophandler_get_request_method(php_http_message_object_t
*obj
, zval
*return_value
) {
548 zval_ptr_dtor(return_value
);
549 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
) && obj
->message
->http
.info
.request
.method
) {
550 RETVAL_STRING(obj
->message
->http
.info
.request
.method
);
555 static void php_http_message_object_prophandler_set_request_method(php_http_message_object_t
*obj
, zval
*value
) {
556 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
557 zend_string
*zs
= zval_get_string(value
);
558 PTR_SET(obj
->message
->http
.info
.request
.method
, estrndup(zs
->val
, zs
->len
));
559 zend_string_release(zs
);
562 static void php_http_message_object_prophandler_get_request_url(php_http_message_object_t
*obj
, zval
*return_value
) {
566 zval_ptr_dtor(return_value
);
567 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
) && obj
->message
->http
.info
.request
.url
&& php_http_url_to_string(obj
->message
->http
.info
.request
.url
, &url_str
, &url_len
, 0)) {
568 RETVAL_STR(php_http_cs2zs(url_str
, url_len
));
573 static void php_http_message_object_prophandler_set_request_url(php_http_message_object_t
*obj
, zval
*value
) {
574 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
575 PTR_SET(obj
->message
->http
.info
.request
.url
, php_http_url_from_zval(value
, PHP_HTTP_URL_STDFLAGS
));
578 static void php_http_message_object_prophandler_get_response_status(php_http_message_object_t
*obj
, zval
*return_value
) {
579 zval_ptr_dtor(return_value
);
580 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
) && obj
->message
->http
.info
.response
.status
) {
581 RETVAL_STRING(obj
->message
->http
.info
.response
.status
);
586 static void php_http_message_object_prophandler_set_response_status(php_http_message_object_t
*obj
, zval
*value
) {
587 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
588 zend_string
*zs
= zval_get_string(value
);
589 PTR_SET(obj
->message
->http
.info
.response
.status
, estrndup(zs
->val
, zs
->len
));
590 zend_string_release(zs
);
593 static void php_http_message_object_prophandler_get_response_code(php_http_message_object_t
*obj
, zval
*return_value
) {
594 zval_ptr_dtor(return_value
);
595 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
596 RETVAL_LONG(obj
->message
->http
.info
.response
.code
);
601 static void php_http_message_object_prophandler_set_response_code(php_http_message_object_t
*obj
, zval
*value
) {
602 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
603 obj
->message
->http
.info
.response
.code
= zval_get_long(value
);
604 PTR_SET(obj
->message
->http
.info
.response
.status
, estrdup(php_http_env_get_response_status_for_code(obj
->message
->http
.info
.response
.code
)));
607 static void php_http_message_object_prophandler_get_http_version(php_http_message_object_t
*obj
, zval
*return_value
) {
611 zval_ptr_dtor(return_value
);
612 php_http_version_to_string(&obj
->message
->http
.version
, &version_str
, &version_len
, NULL
, NULL
);
613 RETVAL_STR(php_http_cs2zs(version_str
, version_len
));
615 static void php_http_message_object_prophandler_set_http_version(php_http_message_object_t
*obj
, zval
*value
) {
616 zend_string
*zs
= zval_get_string(value
);
617 php_http_version_parse(&obj
->message
->http
.version
, zs
->val
);
618 zend_string_release(zs
);
620 static void php_http_message_object_prophandler_get_headers(php_http_message_object_t
*obj
, zval
*return_value
) {
623 ZVAL_COPY_VALUE(&tmp
, return_value
);
624 array_init(return_value
);
625 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(return_value
));
628 static void php_http_message_object_prophandler_set_headers(php_http_message_object_t
*obj
, zval
*value
) {
630 HashTable garbage
, *src
;
632 if (Z_TYPE_P(value
) != IS_ARRAY
&& Z_TYPE_P(value
) != IS_OBJECT
) {
634 SEPARATE_ZVAL(value
);
635 convert_to_array(value
);
637 src
= HASH_OF(value
);
639 garbage
= obj
->message
->hdrs
;
640 zend_hash_init(&obj
->message
->hdrs
, zend_hash_num_elements(src
), NULL
, ZVAL_PTR_DTOR
, 0);
641 array_copy(HASH_OF(value
), &obj
->message
->hdrs
);
643 zend_hash_destroy(&garbage
);
646 zval_ptr_dtor(value
);
649 static void php_http_message_object_prophandler_get_body(php_http_message_object_t
*obj
, zval
*return_value
) {
653 ZVAL_COPY_VALUE(&tmp
, return_value
);
654 RETVAL_OBJECT(&obj
->body
->zo
, 1);
660 static void php_http_message_object_prophandler_set_body(php_http_message_object_t
*obj
, zval
*value
) {
661 php_http_message_object_set_body(obj
, value
);
663 static void php_http_message_object_prophandler_get_parent_message(php_http_message_object_t
*obj
, zval
*return_value
) {
664 if (obj
->message
->parent
) {
667 ZVAL_COPY_VALUE(&tmp
, return_value
);
668 RETVAL_OBJECT(&obj
->parent
->zo
, 1);
674 static void php_http_message_object_prophandler_set_parent_message(php_http_message_object_t
*obj
, zval
*value
) {
675 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), php_http_message_class_entry
)) {
676 php_http_message_object_t
*parent_obj
= PHP_HTTP_OBJ(NULL
, value
);
679 if (obj
->message
->parent
) {
680 zend_object_release(&obj
->parent
->zo
);
682 obj
->parent
= parent_obj
;
683 obj
->message
->parent
= parent_obj
->message
;
687 #define PHP_HTTP_MESSAGE_OBJECT_INIT(obj) \
689 if (!obj->message) { \
690 obj->message = php_http_message_init(NULL, 0, NULL); \
695 void php_http_message_object_reverse(zval
*zmsg
, zval
*return_value
)
698 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, zmsg
);
700 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
703 i
= php_http_message_count(obj
->message
);
706 php_http_message_object_t
**objects
;
709 objects
= ecalloc(i
, sizeof(*objects
));
711 /* we are the first message */
715 for (i
= 1; obj
->parent
; ++i
) {
716 objects
[i
] = obj
= obj
->parent
;
719 /* reorder parents */
720 for (last
= --i
; i
; --i
) {
721 objects
[i
]->message
->parent
= objects
[i
-1]->message
;
722 objects
[i
]->parent
= objects
[i
-1];
725 objects
[0]->message
->parent
= NULL
;
726 objects
[0]->parent
= NULL
;
728 /* add ref, because we previously have not been a parent message */
730 /* no addref, because we've been a parent message previously */
731 RETVAL_OBJECT(&objects
[last
]->zo
, 0);
735 RETURN_ZVAL(zmsg
, 1, 0);
739 void php_http_message_object_prepend(zval
*this_ptr
, zval
*prepend
, zend_bool top
)
741 php_http_message_t
*save_parent_msg
= NULL
;
742 php_http_message_object_t
*save_parent_obj
= NULL
, *obj
= PHP_HTTP_OBJ(NULL
, this_ptr
);
743 php_http_message_object_t
*prepend_obj
= PHP_HTTP_OBJ(NULL
, prepend
);
746 save_parent_obj
= obj
->parent
;
747 save_parent_msg
= obj
->message
->parent
;
749 /* iterate to the most parent object */
750 while (obj
->parent
) {
756 obj
->parent
= prepend_obj
;
757 obj
->message
->parent
= prepend_obj
->message
;
763 prepend_obj
->parent
= save_parent_obj
;
764 prepend_obj
->message
->parent
= save_parent_msg
;
768 ZEND_RESULT_CODE
php_http_message_object_set_body(php_http_message_object_t
*msg_obj
, zval
*zbody
)
771 zend_string
*body_str
;
772 php_http_message_body_t
*body
;
773 php_http_message_body_object_t
*body_obj
;
775 switch (Z_TYPE_P(zbody
)) {
777 php_stream_from_zval_no_verify(s
, zbody
);
779 php_http_throw(unexpected_val
, "The stream is not a valid resource");
785 body
= php_http_message_body_init(NULL
, s
);
786 if (!(body_obj
= php_http_message_body_object_new_ex(php_http_get_message_body_class_entry(), body
))) {
787 php_http_message_body_free(&body
);
793 if (instanceof_function(Z_OBJCE_P(zbody
), php_http_get_message_body_class_entry())) {
795 body_obj
= PHP_HTTP_OBJ(NULL
, zbody
);
801 body_str
= zval_get_string(zbody
);
802 s
= php_stream_temp_new();
803 php_stream_write(s
, body_str
->val
, body_str
->len
);
804 zend_string_release(body_str
);
809 if (!body_obj
->body
) {
810 body_obj
->body
= php_http_message_body_init(NULL
, NULL
);
813 zend_object_release(&msg_obj
->body
->zo
);
815 if (msg_obj
->message
) {
816 php_http_message_body_free(&msg_obj
->message
->body
);
817 msg_obj
->message
->body
= body_obj
->body
;
819 msg_obj
->message
= php_http_message_init(NULL
, 0, body_obj
->body
);
821 php_http_message_body_addref(body_obj
->body
);
822 msg_obj
->body
= body_obj
;
827 ZEND_RESULT_CODE
php_http_message_object_init_body_object(php_http_message_object_t
*obj
)
829 php_http_message_body_addref(obj
->message
->body
);
830 return php_http_new((void *) &obj
->body
, php_http_get_message_body_class_entry(), (php_http_new_t
) php_http_message_body_object_new_ex
, NULL
, obj
->message
->body
);
833 zend_object
*php_http_message_object_new(zend_class_entry
*ce
)
835 return &php_http_message_object_new_ex(ce
, NULL
)->zo
;
838 php_http_message_object_t
*php_http_message_object_new_ex(zend_class_entry
*ce
, php_http_message_t
*msg
)
840 php_http_message_object_t
*o
;
842 o
= ecalloc(1, sizeof(*o
) + zend_object_properties_size(ce
));
843 zend_object_std_init(&o
->zo
, ce
);
844 object_properties_init(&o
->zo
, ce
);
849 o
->parent
= php_http_message_object_new_ex(ce
, msg
->parent
);
851 o
->body
= php_http_message_body_object_new_ex(php_http_get_message_body_class_entry(), php_http_message_body_init(&msg
->body
, NULL
));
854 o
->zo
.handlers
= &php_http_message_object_handlers
;
859 zend_object
*php_http_message_object_clone(zend_object
*this_ptr
)
861 php_http_message_object_t
*new_obj
;
862 php_http_message_object_t
*old_obj
= PHP_HTTP_OBJ(this_ptr
, NULL
);
864 new_obj
= php_http_message_object_new_ex(old_obj
->zo
.ce
, php_http_message_copy(old_obj
->message
, NULL
));
865 zend_objects_clone_members(&new_obj
->zo
, &old_obj
->zo
);
870 void php_http_message_object_free(zend_object
*object
)
872 php_http_message_object_t
*o
= PHP_HTTP_OBJ(object
, NULL
);
876 if (!Z_ISUNDEF(o
->iterator
)) {
877 zval_ptr_dtor(&o
->iterator
);
878 ZVAL_UNDEF(&o
->iterator
);
881 /* do NOT free recursivly */
882 php_http_message_dtor(o
->message
);
887 zend_object_release(&o
->parent
->zo
);
891 zend_object_release(&o
->body
->zo
);
894 zend_object_std_dtor(object
);
897 static zval
*php_http_message_object_get_prop_ptr(zend_object
*object
, zend_string
*member
, int type
, void **cache_slot
)
902 static zval
*php_http_message_object_read_prop(zend_object
*object
, zend_string
*member
, int type
, void **cache_slot
, zval
*tmp
)
905 php_http_message_object_prophandler_t
*handler
= php_http_message_object_get_prophandler(member
);
907 return_value
= zend_get_std_object_handlers()->read_property(object
, member
, type
, cache_slot
, tmp
);
909 if (handler
&& handler
->read
) {
910 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(object
, NULL
);
912 handler
->read(obj
, return_value
);
917 static zval
*php_http_message_object_write_prop(zend_object
*object
, zend_string
*member
, zval
*value
, void **cache_slot
)
919 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(object
, NULL
);
920 php_http_message_object_prophandler_t
*handler
;
922 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
924 if ((handler
= php_http_message_object_get_prophandler(member
))) {
925 handler
->write(obj
, value
);
927 zend_get_std_object_handlers()->write_property(object
, member
, value
, cache_slot
);
932 static HashTable
*php_http_message_object_get_debug_info(zend_object
*object
, int *is_temp
)
935 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(object
, NULL
);
936 HashTable
*props
= zend_get_std_object_handlers()->get_properties(object
);
937 char *ver_str
, *url_str
= NULL
;
938 size_t ver_len
, url_len
= 0;
940 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
943 #define UPDATE_PROP(name_str, action_with_tmp) \
945 zend_property_info *pi; \
946 if ((pi = zend_hash_str_find_ptr(&obj->zo.ce->properties_info, name_str, lenof(name_str)))) { \
948 zend_hash_update_ind(props, pi->name, &tmp); \
952 UPDATE_PROP("type", ZVAL_LONG(&tmp
, obj
->message
->type
));
954 ver_len
= spprintf(&ver_str
, 0, "%u.%u", obj
->message
->http
.version
.major
, obj
->message
->http
.version
.minor
);
955 UPDATE_PROP("httpVersion", ZVAL_STR(&tmp
, php_http_cs2zs(ver_str
, ver_len
)));
957 switch (obj
->message
->type
) {
958 case PHP_HTTP_REQUEST
:
959 UPDATE_PROP("responseCode", ZVAL_LONG(&tmp
, 0));
960 UPDATE_PROP("responseStatus", ZVAL_EMPTY_STRING(&tmp
));
961 UPDATE_PROP("requestMethod", ZVAL_STRING(&tmp
, STR_PTR(obj
->message
->http
.info
.request
.method
)));
962 if (obj
->message
->http
.info
.request
.url
) {
963 php_http_url_to_string(obj
->message
->http
.info
.request
.url
, &url_str
, &url_len
, 0);
964 UPDATE_PROP("requestUrl", ZVAL_STR(&tmp
, php_http_cs2zs(url_str
, url_len
)));
966 UPDATE_PROP("requestUrl", ZVAL_EMPTY_STRING(&tmp
));
971 case PHP_HTTP_RESPONSE
:
972 UPDATE_PROP("responseCode", ZVAL_LONG(&tmp
, obj
->message
->http
.info
.response
.code
));
973 UPDATE_PROP("responseStatus", ZVAL_STRING(&tmp
, STR_PTR(obj
->message
->http
.info
.response
.status
)));
974 UPDATE_PROP("requestMethod", ZVAL_EMPTY_STRING(&tmp
));
975 UPDATE_PROP("requestUrl", ZVAL_EMPTY_STRING(&tmp
));
980 UPDATE_PROP("responseCode", ZVAL_LONG(&tmp
, 0));
981 UPDATE_PROP("responseStatus", ZVAL_EMPTY_STRING(&tmp
));
982 UPDATE_PROP("requestMethod", ZVAL_EMPTY_STRING(&tmp
));
983 UPDATE_PROP("requestUrl", ZVAL_EMPTY_STRING(&tmp
));
987 UPDATE_PROP("headers",
989 array_copy(&obj
->message
->hdrs
, Z_ARRVAL(tmp
));
994 ZVAL_OBJECT(&tmp
, &obj
->body
->zo
, 1);
1000 UPDATE_PROP("parentMessage",
1001 if (obj
->message
->parent
) {
1002 ZVAL_OBJECT(&tmp
, &obj
->parent
->zo
, 1);
1011 static HashTable
*php_http_message_object_get_gc(zend_object
*object
, zval
**table
, int *n
)
1013 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(object
, NULL
);
1014 HashTable
*props
= object
->handlers
->get_properties(object
);
1015 uint32_t count
= 2 + zend_hash_num_elements(props
);
1019 *table
= obj
->gc
= erealloc(obj
->gc
, count
* sizeof(zval
));
1022 ZVAL_OBJ(&obj
->gc
[(*n
)++], &obj
->body
->zo
);
1025 ZVAL_OBJ(&obj
->gc
[(*n
)++], &obj
->parent
->zo
);
1028 ZEND_HASH_FOREACH_VAL(props
, val
)
1030 ZVAL_COPY_VALUE(&obj
->gc
[(*n
)++], val
);
1032 ZEND_HASH_FOREACH_END();
1037 static int php_http_message_object_cast(zend_object
*object
, zval
*return_value
, int type
)
1039 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(object
, NULL
);
1045 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1046 php_http_message_to_string(obj
->message
, &string
, &length
);
1048 RETVAL_STR(php_http_cs2zs(string
, length
));
1050 RETVAL_EMPTY_STRING();
1061 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage___construct
, 0, 0, 0)
1062 ZEND_ARG_INFO(0, message
)
1063 ZEND_ARG_INFO(0, greedy
)
1064 ZEND_END_ARG_INFO();
1065 static PHP_METHOD(HttpMessage
, __construct
)
1067 zend_bool greedy
= 1;
1068 zval
*zmessage
= NULL
;
1069 php_http_message_t
*msg
= NULL
;
1070 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1071 zend_error_handling zeh
;
1073 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|z!b", &zmessage
, &greedy
), invalid_arg
, return);
1075 zend_replace_error_handling(EH_THROW
, php_http_get_exception_bad_message_class_entry(), &zeh
);
1076 if (zmessage
&& Z_TYPE_P(zmessage
) == IS_RESOURCE
) {
1078 php_http_message_parser_t p
;
1079 zend_error_handling zeh
;
1081 zend_replace_error_handling(EH_THROW
, php_http_get_exception_unexpected_val_class_entry(), &zeh
);
1082 php_stream_from_zval(s
, zmessage
);
1083 zend_restore_error_handling(&zeh
);
1085 if (s
&& php_http_message_parser_init(&p
)) {
1086 unsigned flags
= (greedy
? PHP_HTTP_MESSAGE_PARSER_GREEDY
: 0);
1087 php_http_buffer_t buf
;
1089 php_http_buffer_init_ex(&buf
, 0x1000, PHP_HTTP_BUFFER_INIT_PREALLOC
);
1090 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE
== php_http_message_parser_parse_stream(&p
, &buf
, s
, flags
, &msg
)) {
1091 if (!EG(exception
)) {
1092 php_http_throw(bad_message
, "Could not parse message from stream");
1095 php_http_buffer_dtor(&buf
);
1096 php_http_message_parser_dtor(&p
);
1099 if (!msg
&& !EG(exception
)) {
1100 php_http_throw(bad_message
, "Empty message received from stream");
1102 } else if (zmessage
) {
1103 zend_string
*zs_msg
= zval_get_string(zmessage
);
1105 msg
= php_http_message_parse(NULL
, zs_msg
->val
, zs_msg
->len
, greedy
);
1107 if (!msg
&& !EG(exception
)) {
1108 php_http_throw(bad_message
, "Could not parse message: %.*s", (int) MIN(25, zs_msg
->len
), zs_msg
->val
);
1110 zend_string_release(zs_msg
);
1114 php_http_message_dtor(obj
->message
);
1116 if (obj
->message
->parent
) {
1117 obj
->parent
= php_http_message_object_new_ex(obj
->zo
.ce
, obj
->message
->parent
);
1120 zend_restore_error_handling(&zeh
);
1121 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1124 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getBody
, 0, 0, 0)
1125 ZEND_END_ARG_INFO();
1126 static PHP_METHOD(HttpMessage
, getBody
)
1128 php_http_message_object_t
*obj
;
1130 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
1132 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1133 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1136 php_http_message_object_init_body_object(obj
);
1139 RETVAL_OBJECT(&obj
->body
->zo
, 1);
1143 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setBody
, 0, 0, 1)
1144 ZEND_ARG_OBJ_INFO(0, body
, http
\\Message
\\Body
, 0)
1145 ZEND_END_ARG_INFO();
1146 static PHP_METHOD(HttpMessage
, setBody
)
1150 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zbody
, php_http_get_message_body_class_entry())) {
1151 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1153 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1154 php_http_message_object_prophandler_set_body(obj
, zbody
);
1156 RETVAL_ZVAL(getThis(), 1, 0);
1159 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addBody
, 0, 0, 1)
1160 ZEND_ARG_OBJ_INFO(0, body
, http
\\Message
\\Body
, 0)
1161 ZEND_END_ARG_INFO();
1162 static PHP_METHOD(HttpMessage
, addBody
)
1166 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O", &new_body
, php_http_get_message_body_class_entry())) {
1167 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1168 php_http_message_body_object_t
*new_obj
= PHP_HTTP_OBJ(NULL
, new_body
);
1170 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1171 php_http_message_body_to_callback(new_obj
->body
, (php_http_pass_callback_t
) php_http_message_body_append
, obj
->message
->body
, 0, 0);
1173 RETVAL_ZVAL(getThis(), 1, 0);
1176 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHeader
, 0, 0, 1)
1177 ZEND_ARG_INFO(0, header
)
1178 ZEND_ARG_INFO(0, into_class
)
1179 ZEND_END_ARG_INFO();
1180 static PHP_METHOD(HttpMessage
, getHeader
)
1184 zend_class_entry
*header_ce
= NULL
;
1186 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s|C!", &header_str
, &header_len
, &header_ce
)) {
1187 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1190 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1192 if ((header
= php_http_message_header(obj
->message
, header_str
, header_len
))) {
1194 RETURN_ZVAL(header
, 1, 0);
1195 } else if (instanceof_function(header_ce
, php_http_header_get_class_entry())) {
1196 php_http_object_method_t cb
;
1199 ZVAL_STRINGL(&argv
[0], header_str
, header_len
);
1200 ZVAL_COPY(&argv
[1], header
);
1202 object_init_ex(return_value
, header_ce
);
1203 php_http_object_method_init(&cb
, return_value
, ZEND_STRL("__construct"));
1204 php_http_object_method_call(&cb
, return_value
, NULL
, 2, argv
);
1205 php_http_object_method_dtor(&cb
);
1207 zval_ptr_dtor(&argv
[0]);
1208 zval_ptr_dtor(&argv
[1]);
1212 php_error_docref(NULL
, E_WARNING
, "Class '%s' is not as descendant of http\\Header", header_ce
->name
->val
);
1219 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHeaders
, 0, 0, 0)
1220 ZEND_END_ARG_INFO();
1221 static PHP_METHOD(HttpMessage
, getHeaders
)
1223 if (SUCCESS
== zend_parse_parameters_none()) {
1224 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1226 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1228 array_init(return_value
);
1229 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(return_value
));
1233 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHeader
, 0, 0, 1)
1234 ZEND_ARG_INFO(0, header
)
1235 ZEND_ARG_INFO(0, value
)
1236 ZEND_END_ARG_INFO();
1237 static PHP_METHOD(HttpMessage
, setHeader
)
1239 zval
*zvalue
= NULL
;
1243 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s|z!", &name_str
, &name_len
, &zvalue
)) {
1244 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1245 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1247 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1250 zend_symtable_str_del(&obj
->message
->hdrs
, name
, name_len
);
1252 Z_TRY_ADDREF_P(zvalue
);
1253 zend_symtable_str_update(&obj
->message
->hdrs
, name
, name_len
, zvalue
);
1257 RETVAL_ZVAL(getThis(), 1, 0);
1260 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHeaders
, 0, 0, 1)
1261 ZEND_ARG_ARRAY_INFO(0, headers
, 1)
1262 ZEND_END_ARG_INFO();
1263 static PHP_METHOD(HttpMessage
, setHeaders
)
1265 zval
*new_headers
= NULL
;
1267 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "a/!", &new_headers
)) {
1268 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1270 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1272 zend_hash_clean(&obj
->message
->hdrs
);
1274 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, 0, ARRAY_JOIN_PRETTIFY
|ARRAY_JOIN_STRONLY
);
1277 RETVAL_ZVAL(getThis(), 1, 0);
1280 static inline void php_http_message_object_add_header(php_http_message_object_t
*obj
, const char *name_str
, size_t name_len
, zval
*zvalue
)
1282 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1283 zend_string
*hstr
, *vstr
;
1286 if (Z_TYPE_P(zvalue
) == IS_NULL
) {
1290 vstr
= php_http_header_value_to_string(zvalue
);
1292 if ((name_len
!= lenof("Set-Cookie") && strcmp(name
, "Set-Cookie"))
1293 && (hstr
= php_http_message_header_string(obj
->message
, name
, name_len
))) {
1295 size_t hdr_len
= spprintf(&hdr_str
, 0, "%s, %s", hstr
->val
, vstr
->val
);
1297 ZVAL_STR(&tmp
, php_http_cs2zs(hdr_str
, hdr_len
));
1298 zend_symtable_str_update(&obj
->message
->hdrs
, name
, name_len
, &tmp
);
1299 zend_string_release(hstr
);
1300 zend_string_release(vstr
);
1301 } else if ((header
= php_http_message_header(obj
->message
, name
, name_len
))) {
1302 convert_to_array(header
);
1303 ZVAL_STR(&tmp
, vstr
);
1304 zend_hash_next_index_insert(Z_ARRVAL_P(header
), &tmp
);
1306 ZVAL_STR(&tmp
, vstr
);
1307 zend_symtable_str_update(&obj
->message
->hdrs
, name
, name_len
, &tmp
);
1312 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeader
, 0, 0, 2)
1313 ZEND_ARG_INFO(0, header
)
1314 ZEND_ARG_INFO(0, value
)
1315 ZEND_END_ARG_INFO();
1316 static PHP_METHOD(HttpMessage
, addHeader
)
1322 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "sz", &name_str
, &name_len
, &zvalue
)) {
1323 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1325 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1327 php_http_message_object_add_header(obj
, name_str
, name_len
, zvalue
);
1329 RETVAL_ZVAL(getThis(), 1, 0);
1332 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeaders
, 0, 0, 1)
1333 ZEND_ARG_ARRAY_INFO(0, headers
, 0)
1334 ZEND_ARG_INFO(0, append
)
1335 ZEND_END_ARG_INFO();
1336 static PHP_METHOD(HttpMessage
, addHeaders
)
1339 zend_bool append
= 0;
1341 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "a|b", &new_headers
, &append
)) {
1342 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1344 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1347 php_http_arrkey_t key
= {0};
1350 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(new_headers
), key
.h
, key
.key
, val
)
1352 php_http_arrkey_stringify(&key
, NULL
);
1353 php_http_message_object_add_header(obj
, key
.key
->val
, key
.key
->len
, val
);
1354 php_http_arrkey_dtor(&key
);
1356 ZEND_HASH_FOREACH_END();
1358 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, 0, ARRAY_JOIN_PRETTIFY
|ARRAY_JOIN_STRONLY
);
1361 RETVAL_ZVAL(getThis(), 1, 0);
1364 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getType
, 0, 0, 0)
1365 ZEND_END_ARG_INFO();
1366 static PHP_METHOD(HttpMessage
, getType
)
1368 if (SUCCESS
== zend_parse_parameters_none()) {
1369 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1371 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1373 RETURN_LONG(obj
->message
->type
);
1377 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setType
, 0, 0, 1)
1378 ZEND_ARG_INFO(0, type
)
1379 ZEND_END_ARG_INFO();
1380 static PHP_METHOD(HttpMessage
, setType
)
1384 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "l", &type
)) {
1385 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1387 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1389 php_http_message_set_type(obj
->message
, type
);
1391 RETVAL_ZVAL(getThis(), 1, 0);
1394 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getInfo
, 0, 0, 0)
1395 ZEND_END_ARG_INFO();
1396 static PHP_METHOD(HttpMessage
, getInfo
)
1398 if (SUCCESS
== zend_parse_parameters_none()) {
1401 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1403 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1404 php_http_info_to_string((php_http_info_t
*) obj
->message
, &str
, &len
, "");
1406 RETVAL_STR(php_http_cs2zs(str
, len
));
1410 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setInfo
, 0, 0, 1)
1411 ZEND_ARG_INFO(0, http_info
)
1412 ZEND_END_ARG_INFO();
1413 static PHP_METHOD(HttpMessage
, setInfo
)
1417 php_http_message_object_t
*obj
;
1418 php_http_info_t inf
;
1420 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
), invalid_arg
, return);
1422 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1423 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1425 if (!php_http_info_parse(&inf
, str
)) {
1426 php_http_throw(bad_header
, "Could not parse message info '%s'", str
);
1430 php_http_message_set_info(obj
->message
, &inf
);
1431 php_http_info_dtor(&inf
);
1433 RETVAL_ZVAL(getThis(), 1, 0);
1436 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHttpVersion
, 0, 0, 0)
1437 ZEND_END_ARG_INFO();
1438 static PHP_METHOD(HttpMessage
, getHttpVersion
)
1440 if (SUCCESS
== zend_parse_parameters_none()) {
1443 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1445 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1447 php_http_version_to_string(&obj
->message
->http
.version
, &str
, &len
, NULL
, NULL
);
1448 RETURN_STR(php_http_cs2zs(str
, len
));
1452 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHttpVersion
, 0, 0, 1)
1453 ZEND_ARG_INFO(0, http_version
)
1454 ZEND_END_ARG_INFO();
1455 static PHP_METHOD(HttpMessage
, setHttpVersion
)
1459 php_http_version_t version
;
1460 php_http_message_object_t
*obj
;
1462 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &v_str
, &v_len
), invalid_arg
, return);
1464 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1465 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1467 php_http_expect(php_http_version_parse(&version
, v_str
), unexpected_val
, return);
1469 obj
->message
->http
.version
= version
;
1471 RETVAL_ZVAL(getThis(), 1, 0);
1474 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getResponseCode
, 0, 0, 0)
1475 ZEND_END_ARG_INFO();
1476 static PHP_METHOD(HttpMessage
, getResponseCode
)
1478 if (SUCCESS
== zend_parse_parameters_none()) {
1479 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1481 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1483 if (obj
->message
->type
!= PHP_HTTP_RESPONSE
) {
1484 php_error_docref(NULL
, E_WARNING
, "http\\Message is not of type response");
1488 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
1492 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setResponseCode
, 0, 0, 1)
1493 ZEND_ARG_INFO(0, response_code
)
1494 ZEND_ARG_INFO(0, strict
)
1495 ZEND_END_ARG_INFO();
1496 static PHP_METHOD(HttpMessage
, setResponseCode
)
1499 zend_bool strict
= 1;
1500 php_http_message_object_t
*obj
;
1502 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &code
, &strict
), invalid_arg
, return);
1504 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1505 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1507 if (obj
->message
->type
!= PHP_HTTP_RESPONSE
) {
1508 php_http_throw(bad_method_call
, "http\\Message is not of type response");
1512 if (strict
&& (code
< 100 || code
> 599)) {
1513 php_http_throw(invalid_arg
, "Invalid response code (100-599): %ld", code
);
1517 obj
->message
->http
.info
.response
.code
= code
;
1518 PTR_SET(obj
->message
->http
.info
.response
.status
, estrdup(php_http_env_get_response_status_for_code(code
)));
1520 RETVAL_ZVAL(getThis(), 1, 0);
1523 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getResponseStatus
, 0, 0, 0)
1524 ZEND_END_ARG_INFO();
1525 static PHP_METHOD(HttpMessage
, getResponseStatus
)
1527 if (SUCCESS
== zend_parse_parameters_none()) {
1528 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1530 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1532 if (obj
->message
->type
!= PHP_HTTP_RESPONSE
) {
1533 php_error_docref(NULL
, E_WARNING
, "http\\Message is not of type response");
1536 if (obj
->message
->http
.info
.response
.status
) {
1537 RETURN_STRING(obj
->message
->http
.info
.response
.status
);
1539 RETURN_EMPTY_STRING();
1544 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setResponseStatus
, 0, 0, 1)
1545 ZEND_ARG_INFO(0, response_status
)
1546 ZEND_END_ARG_INFO();
1547 static PHP_METHOD(HttpMessage
, setResponseStatus
)
1551 php_http_message_object_t
*obj
;
1553 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &status
, &status_len
), invalid_arg
, return);
1555 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1557 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1559 if (obj
->message
->type
!= PHP_HTTP_RESPONSE
) {
1560 php_http_throw(bad_method_call
, "http\\Message is not of type response");
1563 PTR_SET(obj
->message
->http
.info
.response
.status
, estrndup(status
, status_len
));
1564 RETVAL_ZVAL(getThis(), 1, 0);
1567 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getRequestMethod
, 0, 0, 0)
1568 ZEND_END_ARG_INFO();
1569 static PHP_METHOD(HttpMessage
, getRequestMethod
)
1571 if (SUCCESS
== zend_parse_parameters_none()) {
1572 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1574 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1576 if (obj
->message
->type
!= PHP_HTTP_REQUEST
) {
1577 php_error_docref(NULL
, E_WARNING
, "http\\Message is not of type request");
1581 if (obj
->message
->http
.info
.request
.method
) {
1582 RETURN_STRING(obj
->message
->http
.info
.request
.method
);
1584 RETURN_EMPTY_STRING();
1589 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setRequestMethod
, 0, 0, 1)
1590 ZEND_ARG_INFO(0, request_method
)
1591 ZEND_END_ARG_INFO();
1592 static PHP_METHOD(HttpMessage
, setRequestMethod
)
1596 php_http_message_object_t
*obj
;
1598 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &method
, &method_len
), invalid_arg
, return);
1600 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1602 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1604 if (obj
->message
->type
!= PHP_HTTP_REQUEST
) {
1605 php_http_throw(bad_method_call
, "http\\Message is not of type request");
1609 if (method_len
< 1) {
1610 php_http_throw(invalid_arg
, "Cannot set http\\Message's request method to an empty string");
1614 PTR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
1615 RETVAL_ZVAL(getThis(), 1, 0);
1618 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getRequestUrl
, 0, 0, 0)
1619 ZEND_END_ARG_INFO();
1620 static PHP_METHOD(HttpMessage
, getRequestUrl
)
1622 if (SUCCESS
== zend_parse_parameters_none()) {
1623 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1625 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1627 if (obj
->message
->type
!= PHP_HTTP_REQUEST
) {
1628 php_error_docref(NULL
, E_WARNING
, "http\\Message is not of type request");
1632 if (obj
->message
->http
.info
.request
.url
) {
1636 php_http_url_to_string(obj
->message
->http
.info
.request
.url
, &url_str
, &url_len
, 0);
1637 RETURN_STR(php_http_cs2zs(url_str
, url_len
));
1639 RETURN_EMPTY_STRING();
1644 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setRequestUrl
, 0, 0, 1)
1645 ZEND_ARG_INFO(0, url
)
1646 ZEND_END_ARG_INFO();
1647 static PHP_METHOD(HttpMessage
, setRequestUrl
)
1650 php_http_url_t
*url
;
1651 php_http_message_object_t
*obj
;
1652 zend_error_handling zeh
;
1654 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zurl
), invalid_arg
, return);
1656 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1658 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1660 if (obj
->message
->type
!= PHP_HTTP_REQUEST
) {
1661 php_http_throw(bad_method_call
, "http\\Message is not of type request");
1665 zend_replace_error_handling(EH_THROW
, php_http_get_exception_bad_url_class_entry(), &zeh
);
1666 url
= php_http_url_from_zval(zurl
, PHP_HTTP_URL_STDFLAGS
);
1667 zend_restore_error_handling(&zeh
);
1669 if (url
&& php_http_url_is_empty(url
)) {
1670 php_http_url_free(&url
);
1671 php_http_throw(invalid_arg
, "Cannot set http\\Message's request url to an empty string");
1673 PTR_SET(obj
->message
->http
.info
.request
.url
, url
);
1676 RETVAL_ZVAL(getThis(), 1, 0);
1679 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getParentMessage
, 0, 0, 0)
1680 ZEND_END_ARG_INFO();
1681 static PHP_METHOD(HttpMessage
, getParentMessage
)
1683 php_http_message_object_t
*obj
;
1685 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
1687 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1688 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1690 if (!obj
->message
->parent
) {
1691 php_http_throw(unexpected_val
, "http\\Message has no parent message");
1695 RETVAL_OBJECT(&obj
->parent
->zo
, 1);
1698 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage___toString
, 0, 0, 0)
1699 ZEND_END_ARG_INFO();
1700 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toString
, 0, 0, 0)
1701 ZEND_ARG_INFO(0, include_parent
)
1702 ZEND_END_ARG_INFO();
1703 static PHP_METHOD(HttpMessage
, toString
)
1705 zend_bool include_parent
= 0;
1707 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &include_parent
)) {
1708 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1712 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1714 if (include_parent
) {
1715 php_http_message_serialize(obj
->message
, &string
, &length
);
1717 php_http_message_to_string(obj
->message
, &string
, &length
);
1720 RETURN_STR(php_http_cs2zs(string
, length
));
1723 RETURN_EMPTY_STRING();
1726 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toStream
, 0, 0, 1)
1727 ZEND_ARG_INFO(0, stream
)
1728 ZEND_END_ARG_INFO();
1729 static PHP_METHOD(HttpMessage
, toStream
)
1733 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zstream
)) {
1734 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1737 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1739 php_stream_from_zval(s
, zstream
);
1740 php_http_message_to_callback(obj
->message
, (php_http_pass_callback_t
) _php_stream_write
, s
);
1744 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toCallback
, 0, 0, 1)
1745 ZEND_ARG_INFO(0, callback
)
1746 ZEND_END_ARG_INFO();
1747 static PHP_METHOD(HttpMessage
, toCallback
)
1749 php_http_pass_fcall_arg_t fcd
;
1751 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "f", &fcd
.fci
, &fcd
.fcc
)) {
1752 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1754 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1756 ZVAL_COPY(&fcd
.fcz
, getThis());
1757 php_http_message_to_callback(obj
->message
, php_http_pass_fcall_callback
, &fcd
);
1758 zend_fcall_info_args_clear(&fcd
.fci
, 1);
1759 zval_ptr_dtor(&fcd
.fcz
);
1761 RETURN_ZVAL(&fcd
.fcz
, 1, 0);
1765 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_serialize
, 0, 0, 0)
1766 ZEND_END_ARG_INFO();
1767 static PHP_METHOD(HttpMessage
, serialize
)
1769 if (SUCCESS
== zend_parse_parameters_none()) {
1770 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1774 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1776 php_http_message_serialize(obj
->message
, &string
, &length
);
1777 RETURN_STR(php_http_cs2zs(string
, length
));
1779 RETURN_EMPTY_STRING();
1782 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_unserialize
, 0, 0, 1)
1783 ZEND_ARG_INFO(0, serialized
)
1784 ZEND_END_ARG_INFO();
1785 static PHP_METHOD(HttpMessage
, unserialize
)
1790 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &serialized
, &length
)) {
1791 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1792 php_http_message_t
*msg
;
1795 /* do not free recursively */
1796 php_http_message_dtor(obj
->message
);
1797 efree(obj
->message
);
1799 if ((msg
= php_http_message_parse(NULL
, serialized
, length
, 1))) {
1802 obj
->message
= php_http_message_init(NULL
, 0, NULL
);
1803 php_error_docref(NULL
, E_ERROR
, "Could not unserialize http\\Message");
1808 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_detach
, 0, 0, 0)
1809 ZEND_END_ARG_INFO();
1810 static PHP_METHOD(HttpMessage
, detach
)
1812 php_http_message_object_t
*obj
, *new_obj
;
1813 php_http_message_t
*msg_cpy
;
1815 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
1817 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1818 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1820 msg_cpy
= php_http_message_copy_ex(obj
->message
, NULL
, 0);
1821 new_obj
= php_http_message_object_new_ex(obj
->zo
.ce
, msg_cpy
);
1823 RETVAL_OBJ(&new_obj
->zo
);
1826 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_prepend
, 0, 0, 1)
1827 ZEND_ARG_OBJ_INFO(0, message
, http
\\Message
, 0)
1828 ZEND_ARG_INFO(0, top
)
1829 ZEND_END_ARG_INFO();
1830 static PHP_METHOD(HttpMessage
, prepend
)
1834 php_http_message_t
*msg
[2];
1835 php_http_message_object_t
*obj
, *prepend_obj
;
1837 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &prepend
, php_http_message_class_entry
, &top
), invalid_arg
, return);
1839 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1840 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1841 prepend_obj
= PHP_HTTP_OBJ(NULL
, prepend
);
1842 PHP_HTTP_MESSAGE_OBJECT_INIT(prepend_obj
);
1845 for (msg
[0] = obj
->message
; msg
[0]; msg
[0] = msg
[0]->parent
) {
1846 for (msg
[1] = prepend_obj
->message
; msg
[1]; msg
[1] = msg
[1]->parent
) {
1847 if (msg
[0] == msg
[1]) {
1848 php_http_throw(unexpected_val
, "Cannot prepend a message located within the same message chain");
1854 php_http_message_object_prepend(getThis(), prepend
, top
);
1855 RETURN_ZVAL(getThis(), 1, 0);
1858 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_reverse
, 0, 0, 0)
1859 ZEND_END_ARG_INFO();
1860 static PHP_METHOD(HttpMessage
, reverse
)
1862 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
1864 php_http_message_object_reverse(getThis(), return_value
);
1867 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_isMultipart
, 0, 0, 0)
1868 ZEND_ARG_INFO(1, boundary
)
1869 ZEND_END_ARG_INFO();
1870 static PHP_METHOD(HttpMessage
, isMultipart
)
1872 zval
*zboundary
= NULL
;
1874 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|z!", &zboundary
)) {
1875 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1876 char *boundary
= NULL
;
1878 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1880 if (php_http_message_is_multipart(obj
->message
, zboundary
? &boundary
: NULL
)) {
1886 if (zboundary
&& boundary
) {
1887 ZVAL_DEREF(zboundary
);
1888 zval_dtor(zboundary
);
1889 ZVAL_STR(zboundary
, php_http_cs2zs(boundary
, strlen(boundary
)));
1894 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_splitMultipartBody
, 0, 0, 0)
1895 ZEND_END_ARG_INFO();
1896 static PHP_METHOD(HttpMessage
, splitMultipartBody
)
1898 php_http_message_object_t
*obj
;
1899 php_http_message_t
*msg
;
1900 char *boundary
= NULL
;
1902 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
1904 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1905 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1907 if (!php_http_message_is_multipart(obj
->message
, &boundary
)) {
1908 php_http_throw(bad_method_call
, "http\\Message is not a multipart message");
1912 php_http_expect(msg
= php_http_message_body_split(obj
->message
->body
, boundary
), bad_message
, return);
1916 RETURN_OBJ(&php_http_message_object_new_ex(obj
->zo
.ce
, msg
)->zo
);
1919 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_count
, 0, 0, 0)
1920 ZEND_END_ARG_INFO();
1921 static PHP_METHOD(HttpMessage
, count
)
1923 zend_long count_mode
= -1;
1925 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &count_mode
)) {
1926 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1928 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1930 RETURN_LONG(php_http_message_count(obj
->message
));
1934 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_rewind
, 0, 0, 0)
1935 ZEND_END_ARG_INFO();
1936 static PHP_METHOD(HttpMessage
, rewind
)
1938 if (SUCCESS
== zend_parse_parameters_none()) {
1939 zval
*zobj
= getThis();
1940 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1942 if (!Z_ISUNDEF(obj
->iterator
)) {
1943 zval_ptr_dtor(&obj
->iterator
);
1945 ZVAL_COPY(&obj
->iterator
, zobj
);
1949 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_valid
, 0, 0, 0)
1950 ZEND_END_ARG_INFO();
1951 static PHP_METHOD(HttpMessage
, valid
)
1953 if (SUCCESS
== zend_parse_parameters_none()) {
1954 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1956 RETURN_BOOL(!Z_ISUNDEF(obj
->iterator
));
1960 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_next
, 0, 0, 0)
1961 ZEND_END_ARG_INFO();
1962 static PHP_METHOD(HttpMessage
, next
)
1964 if (SUCCESS
== zend_parse_parameters_none()) {
1965 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1967 if (!Z_ISUNDEF(obj
->iterator
)) {
1968 php_http_message_object_t
*itr
= PHP_HTTP_OBJ(NULL
, &obj
->iterator
);
1973 ZVAL_OBJECT(&tmp
, &itr
->parent
->zo
, 1);
1974 zval_ptr_dtor(&obj
->iterator
);
1975 obj
->iterator
= tmp
;
1977 zval_ptr_dtor(&obj
->iterator
);
1978 ZVAL_UNDEF(&obj
->iterator
);
1984 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_key
, 0, 0, 0)
1985 ZEND_END_ARG_INFO();
1986 static PHP_METHOD(HttpMessage
, key
)
1988 if (SUCCESS
== zend_parse_parameters_none()) {
1989 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1991 RETURN_LONG(Z_ISUNDEF(obj
->iterator
) ? 0 : Z_OBJ_HANDLE(obj
->iterator
));
1995 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_current
, 0, 0, 0)
1996 ZEND_END_ARG_INFO();
1997 static PHP_METHOD(HttpMessage
, current
)
1999 if (SUCCESS
== zend_parse_parameters_none()) {
2000 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
2002 if (!Z_ISUNDEF(obj
->iterator
)) {
2003 RETURN_ZVAL(&obj
->iterator
, 1, 0);
2008 static zend_function_entry php_http_message_methods
[] = {
2009 PHP_ME(HttpMessage
, __construct
, ai_HttpMessage___construct
, ZEND_ACC_PUBLIC
)
2010 PHP_ME(HttpMessage
, getBody
, ai_HttpMessage_getBody
, ZEND_ACC_PUBLIC
)
2011 PHP_ME(HttpMessage
, setBody
, ai_HttpMessage_setBody
, ZEND_ACC_PUBLIC
)
2012 PHP_ME(HttpMessage
, addBody
, ai_HttpMessage_addBody
, ZEND_ACC_PUBLIC
)
2013 PHP_ME(HttpMessage
, getHeader
, ai_HttpMessage_getHeader
, ZEND_ACC_PUBLIC
)
2014 PHP_ME(HttpMessage
, setHeader
, ai_HttpMessage_setHeader
, ZEND_ACC_PUBLIC
)
2015 PHP_ME(HttpMessage
, addHeader
, ai_HttpMessage_addHeader
, ZEND_ACC_PUBLIC
)
2016 PHP_ME(HttpMessage
, getHeaders
, ai_HttpMessage_getHeaders
, ZEND_ACC_PUBLIC
)
2017 PHP_ME(HttpMessage
, setHeaders
, ai_HttpMessage_setHeaders
, ZEND_ACC_PUBLIC
)
2018 PHP_ME(HttpMessage
, addHeaders
, ai_HttpMessage_addHeaders
, ZEND_ACC_PUBLIC
)
2019 PHP_ME(HttpMessage
, getType
, ai_HttpMessage_getType
, ZEND_ACC_PUBLIC
)
2020 PHP_ME(HttpMessage
, setType
, ai_HttpMessage_setType
, ZEND_ACC_PUBLIC
)
2021 PHP_ME(HttpMessage
, getInfo
, ai_HttpMessage_getInfo
, ZEND_ACC_PUBLIC
)
2022 PHP_ME(HttpMessage
, setInfo
, ai_HttpMessage_setInfo
, ZEND_ACC_PUBLIC
)
2023 PHP_ME(HttpMessage
, getResponseCode
, ai_HttpMessage_getResponseCode
, ZEND_ACC_PUBLIC
)
2024 PHP_ME(HttpMessage
, setResponseCode
, ai_HttpMessage_setResponseCode
, ZEND_ACC_PUBLIC
)
2025 PHP_ME(HttpMessage
, getResponseStatus
, ai_HttpMessage_getResponseStatus
, ZEND_ACC_PUBLIC
)
2026 PHP_ME(HttpMessage
, setResponseStatus
, ai_HttpMessage_setResponseStatus
, ZEND_ACC_PUBLIC
)
2027 PHP_ME(HttpMessage
, getRequestMethod
, ai_HttpMessage_getRequestMethod
, ZEND_ACC_PUBLIC
)
2028 PHP_ME(HttpMessage
, setRequestMethod
, ai_HttpMessage_setRequestMethod
, ZEND_ACC_PUBLIC
)
2029 PHP_ME(HttpMessage
, getRequestUrl
, ai_HttpMessage_getRequestUrl
, ZEND_ACC_PUBLIC
)
2030 PHP_ME(HttpMessage
, setRequestUrl
, ai_HttpMessage_setRequestUrl
, ZEND_ACC_PUBLIC
)
2031 PHP_ME(HttpMessage
, getHttpVersion
, ai_HttpMessage_getHttpVersion
, ZEND_ACC_PUBLIC
)
2032 PHP_ME(HttpMessage
, setHttpVersion
, ai_HttpMessage_setHttpVersion
, ZEND_ACC_PUBLIC
)
2033 PHP_ME(HttpMessage
, getParentMessage
, ai_HttpMessage_getParentMessage
, ZEND_ACC_PUBLIC
)
2034 PHP_ME(HttpMessage
, toString
, ai_HttpMessage_toString
, ZEND_ACC_PUBLIC
)
2035 PHP_ME(HttpMessage
, toCallback
, ai_HttpMessage_toCallback
, ZEND_ACC_PUBLIC
)
2036 PHP_ME(HttpMessage
, toStream
, ai_HttpMessage_toStream
, ZEND_ACC_PUBLIC
)
2038 /* implements Countable */
2039 PHP_ME(HttpMessage
, count
, ai_HttpMessage_count
, ZEND_ACC_PUBLIC
)
2041 /* implements Serializable */
2042 PHP_ME(HttpMessage
, serialize
, ai_HttpMessage_serialize
, ZEND_ACC_PUBLIC
)
2043 PHP_ME(HttpMessage
, unserialize
, ai_HttpMessage_unserialize
, ZEND_ACC_PUBLIC
)
2045 /* implements Iterator */
2046 PHP_ME(HttpMessage
, rewind
, ai_HttpMessage_rewind
, ZEND_ACC_PUBLIC
)
2047 PHP_ME(HttpMessage
, valid
, ai_HttpMessage_valid
, ZEND_ACC_PUBLIC
)
2048 PHP_ME(HttpMessage
, current
, ai_HttpMessage_current
, ZEND_ACC_PUBLIC
)
2049 PHP_ME(HttpMessage
, key
, ai_HttpMessage_key
, ZEND_ACC_PUBLIC
)
2050 PHP_ME(HttpMessage
, next
, ai_HttpMessage_next
, ZEND_ACC_PUBLIC
)
2052 ZEND_MALIAS(HttpMessage
, __toString
, toString
, ai_HttpMessage___toString
, ZEND_ACC_PUBLIC
)
2054 PHP_ME(HttpMessage
, detach
, ai_HttpMessage_detach
, ZEND_ACC_PUBLIC
)
2055 PHP_ME(HttpMessage
, prepend
, ai_HttpMessage_prepend
, ZEND_ACC_PUBLIC
)
2056 PHP_ME(HttpMessage
, reverse
, ai_HttpMessage_reverse
, ZEND_ACC_PUBLIC
)
2058 PHP_ME(HttpMessage
, isMultipart
, ai_HttpMessage_isMultipart
, ZEND_ACC_PUBLIC
)
2059 PHP_ME(HttpMessage
, splitMultipartBody
, ai_HttpMessage_splitMultipartBody
, ZEND_ACC_PUBLIC
)
2061 EMPTY_FUNCTION_ENTRY
2064 PHP_MINIT_FUNCTION(http_message
)
2066 zend_class_entry ce
= {0};
2068 INIT_NS_CLASS_ENTRY(ce
, "http", "Message", php_http_message_methods
);
2069 php_http_message_class_entry
= zend_register_internal_class(&ce
);
2070 php_http_message_class_entry
->create_object
= php_http_message_object_new
;
2071 memcpy(&php_http_message_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
2072 php_http_message_object_handlers
.offset
= XtOffsetOf(php_http_message_object_t
, zo
);
2073 php_http_message_object_handlers
.clone_obj
= php_http_message_object_clone
;
2074 php_http_message_object_handlers
.free_obj
= php_http_message_object_free
;
2075 php_http_message_object_handlers
.read_property
= php_http_message_object_read_prop
;
2076 php_http_message_object_handlers
.write_property
= php_http_message_object_write_prop
;
2077 php_http_message_object_handlers
.get_debug_info
= php_http_message_object_get_debug_info
;
2078 #if PHP_VERSION_ID >= 70400
2079 php_http_message_object_handlers
.get_property_ptr_ptr
= php_http_message_object_get_prop_ptr
;
2081 php_http_message_object_handlers
.get_property_ptr_ptr
= NULL
;
2083 php_http_message_object_handlers
.get_gc
= php_http_message_object_get_gc
;
2084 php_http_message_object_handlers
.cast_object
= php_http_message_object_cast
;
2086 zend_class_implements(php_http_message_class_entry
, 3, spl_ce_Countable
, zend_ce_serializable
, zend_ce_iterator
);
2088 zend_hash_init(&php_http_message_object_prophandlers
, 9, NULL
, php_http_message_object_prophandler_hash_dtor
, 1);
2089 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("type"), PHP_HTTP_NONE
, ZEND_ACC_PROTECTED
);
2090 php_http_message_object_add_prophandler(ZEND_STRL("type"), php_http_message_object_prophandler_get_type
, php_http_message_object_prophandler_set_type
);
2091 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("body"), ZEND_ACC_PROTECTED
);
2092 php_http_message_object_add_prophandler(ZEND_STRL("body"), php_http_message_object_prophandler_get_body
, php_http_message_object_prophandler_set_body
);
2093 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestMethod"), "", ZEND_ACC_PROTECTED
);
2094 php_http_message_object_add_prophandler(ZEND_STRL("requestMethod"), php_http_message_object_prophandler_get_request_method
, php_http_message_object_prophandler_set_request_method
);
2095 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestUrl"), "", ZEND_ACC_PROTECTED
);
2096 php_http_message_object_add_prophandler(ZEND_STRL("requestUrl"), php_http_message_object_prophandler_get_request_url
, php_http_message_object_prophandler_set_request_url
);
2097 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("responseStatus"), "", ZEND_ACC_PROTECTED
);
2098 php_http_message_object_add_prophandler(ZEND_STRL("responseStatus"), php_http_message_object_prophandler_get_response_status
, php_http_message_object_prophandler_set_response_status
);
2099 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("responseCode"), 0, ZEND_ACC_PROTECTED
);
2100 php_http_message_object_add_prophandler(ZEND_STRL("responseCode"), php_http_message_object_prophandler_get_response_code
, php_http_message_object_prophandler_set_response_code
);
2101 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("httpVersion"), ZEND_ACC_PROTECTED
);
2102 php_http_message_object_add_prophandler(ZEND_STRL("httpVersion"), php_http_message_object_prophandler_get_http_version
, php_http_message_object_prophandler_set_http_version
);
2103 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("headers"), ZEND_ACC_PROTECTED
);
2104 php_http_message_object_add_prophandler(ZEND_STRL("headers"), php_http_message_object_prophandler_get_headers
, php_http_message_object_prophandler_set_headers
);
2105 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("parentMessage"), ZEND_ACC_PROTECTED
);
2106 php_http_message_object_add_prophandler(ZEND_STRL("parentMessage"), php_http_message_object_prophandler_get_parent_message
, php_http_message_object_prophandler_set_parent_message
);
2108 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_NONE"), PHP_HTTP_NONE
);
2109 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_REQUEST"), PHP_HTTP_REQUEST
);
2110 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_RESPONSE"), PHP_HTTP_RESPONSE
);
2115 PHP_MSHUTDOWN_FUNCTION(http_message
)
2117 zend_hash_destroy(&php_http_message_object_prophandlers
);
2127 * vim600: noet sw=4 ts=4 fdm=marker
2128 * vim<600: noet sw=4 ts=4