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
), ~0);
73 php_http_env_get_request_headers(&message
->hdrs
);
76 case PHP_HTTP_RESPONSE
:
77 message
= php_http_message_init(NULL
, type
, NULL
);
78 if (!SG(sapi_headers
).http_status_line
|| !php_http_info_parse((php_http_info_t
*) &message
->http
, SG(sapi_headers
).http_status_line
)) {
79 if (!(message
->http
.info
.response
.code
= SG(sapi_headers
).http_response_code
)) {
80 message
->http
.info
.response
.code
= 200;
82 message
->http
.info
.response
.status
= estrdup(php_http_env_get_response_status_for_code(message
->http
.info
.response
.code
));
85 php_http_env_get_response_headers(&message
->hdrs
);
86 if (php_output_get_level()) {
87 if (php_output_get_status() & PHP_OUTPUT_SENT
) {
88 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());
90 } else if (SUCCESS
!= php_output_get_contents(&tval
)) {
91 php_error_docref(NULL
, E_WARNING
, "Could not fetch response body");
94 php_http_message_body_append(message
->body
, Z_STRVAL(tval
), Z_STRLEN(tval
));
104 php_http_message_free(&message
);
115 php_http_message_t
*php_http_message_parse(php_http_message_t
*msg
, const char *str
, size_t len
, zend_bool greedy
)
117 php_http_message_parser_t p
;
118 php_http_buffer_t buf
;
119 unsigned flags
= PHP_HTTP_MESSAGE_PARSER_CLEANUP
;
122 php_http_buffer_from_string_ex(&buf
, str
, len
);
123 php_http_message_parser_init(&p
);
125 if ((free_msg
= !msg
)) {
126 msg
= php_http_message_init(NULL
, 0, NULL
);
130 flags
|= PHP_HTTP_MESSAGE_PARSER_GREEDY
;
132 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE
== php_http_message_parser_parse(&p
, &buf
, flags
, &msg
)) {
134 php_http_message_free(&msg
);
139 php_http_message_parser_dtor(&p
);
140 php_http_buffer_dtor(&buf
);
145 zval
*php_http_message_header(php_http_message_t
*msg
, const char *key_str
, size_t key_len
)
149 ALLOCA_FLAG(free_key
);
151 key
= do_alloca(key_len
+ 1, free_key
);
153 memcpy(key
, key_str
, key_len
);
155 php_http_pretty_key(key
, key_len
, 1, 1);
157 ret
= zend_symtable_str_find(&msg
->hdrs
, key
, key_len
);
159 free_alloca(key
, free_key
);
164 zend_bool
php_http_message_is_multipart(php_http_message_t
*msg
, char **boundary
)
166 zend_string
*ct
= php_http_message_header_string(msg
, ZEND_STRL("Content-Type"));
167 zend_bool is_multipart
= 0;
170 php_http_params_opts_t popts
;
173 ZEND_INIT_SYMTABLE(¶ms
);
174 php_http_params_opts_default_get(&popts
);
175 popts
.input
.str
= ct
->val
;
176 popts
.input
.len
= ct
->len
;
178 if (php_http_params_parse(¶ms
, &popts
)) {
183 zend_hash_internal_pointer_reset(¶ms
);
185 if ((cur
= zend_hash_get_current_data(¶ms
))
186 && (Z_TYPE_P(cur
) == IS_ARRAY
)
187 && (HASH_KEY_IS_STRING
== zend_hash_get_current_key(¶ms
, &ct_str
, &index
))
189 if (php_http_match(ct_str
->val
, "multipart", PHP_HTTP_MATCH_WORD
)) {
194 && (arg
= zend_hash_str_find(Z_ARRVAL_P(cur
), ZEND_STRL("arguments")))
195 && Z_TYPE_P(arg
) == IS_ARRAY
198 php_http_arrkey_t key
;
200 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arg
), key
.h
, key
.key
, val
)
202 if (key
.key
&& key
.key
->len
== lenof("boundary") && !strcasecmp(key
.key
->val
, "boundary")) {
203 zend_string
*bnd
= zval_get_string(val
);
206 *boundary
= estrndup(bnd
->val
, bnd
->len
);
208 zend_string_release(bnd
);
211 ZEND_HASH_FOREACH_END();
216 zend_hash_destroy(¶ms
);
217 zend_string_release(ct
);
224 void php_http_message_set_type(php_http_message_t
*message
, php_http_message_type_t type
)
226 /* just act if different */
227 if (type
!= message
->type
) {
229 /* free request info */
230 switch (message
->type
) {
231 case PHP_HTTP_REQUEST
:
232 PTR_FREE(message
->http
.info
.request
.method
);
233 PTR_FREE(message
->http
.info
.request
.url
);
236 case PHP_HTTP_RESPONSE
:
237 PTR_FREE(message
->http
.info
.response
.status
);
244 message
->type
= type
;
245 memset(&message
->http
, 0, sizeof(message
->http
));
249 void php_http_message_set_info(php_http_message_t
*message
, php_http_info_t
*info
)
251 php_http_message_set_type(message
, info
->type
);
252 message
->http
.version
= info
->http
.version
;
253 switch (message
->type
) {
254 case PHP_HTTP_REQUEST
:
255 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
);
256 PTR_SET(PHP_HTTP_INFO(message
).request
.method
, PHP_HTTP_INFO(info
).request
.method
? estrdup(PHP_HTTP_INFO(info
).request
.method
) : NULL
);
259 case PHP_HTTP_RESPONSE
:
260 PHP_HTTP_INFO(message
).response
.code
= PHP_HTTP_INFO(info
).response
.code
;
261 PTR_SET(PHP_HTTP_INFO(message
).response
.status
, PHP_HTTP_INFO(info
).response
.status
? estrdup(PHP_HTTP_INFO(info
).response
.status
) : NULL
);
269 void php_http_message_update_headers(php_http_message_t
*msg
)
275 if (php_http_message_body_stream(msg
->body
)->readfilters
.head
) {
276 /* if a read stream filter is attached to the body the caller must also care for the headers */
277 } else if (php_http_message_header(msg
, ZEND_STRL("Content-Range"))) {
278 /* don't mess around with a Content-Range message */
279 } else if ((size
= php_http_message_body_size(msg
->body
))) {
281 zend_hash_str_update(&msg
->hdrs
, "Content-Length", lenof("Content-Length"), &h
);
283 if (msg
->body
->boundary
) {
288 if (!(ct
= php_http_message_header_string(msg
, ZEND_STRL("Content-Type")))) {
289 len
= spprintf(&str
, 0, "multipart/form-data; boundary=\"%s\"", msg
->body
->boundary
);
290 ZVAL_STR(&h
, php_http_cs2zs(str
, len
));
291 zend_hash_str_update(&msg
->hdrs
, "Content-Type", lenof("Content-Type"), &h
);
292 } else if (!php_http_match(ct
->val
, "boundary=", PHP_HTTP_MATCH_WORD
)) {
293 len
= spprintf(&str
, 0, "%s; boundary=\"%s\"", ct
->val
, msg
->body
->boundary
);
294 ZVAL_STR(&h
, php_http_cs2zs(str
, len
));
295 zend_hash_str_update(&msg
->hdrs
, "Content-Type", lenof("Content-Type"), &h
);
296 zend_string_release(ct
);
298 zend_string_release(ct
);
301 } else if ((cl
= php_http_message_header_string(msg
, ZEND_STRL("Content-Length")))) {
302 if (!zend_string_equals_literal(cl
, "0")) {
303 /* body->size == 0, so get rid of old Content-Length */
304 zend_hash_str_del(&msg
->hdrs
, ZEND_STRL("Content-Length"));
306 zend_string_release(cl
);
307 } else if (msg
->type
== PHP_HTTP_REQUEST
) {
308 if (!php_http_message_header(msg
, ZEND_STRL("Transfer-Encoding"))) {
309 /* no filter, no CR, no size, no TE, no CL */
310 if (0 <= php_http_select_str(msg
->http
.info
.request
.method
, 3, "POST", "PUT", "PATCH")) {
311 /* quoting RFC7230#section-3.3.2
312 A user agent SHOULD send a Content-Length in a request message when
313 no Transfer-Encoding is sent and the request method defines a meaning
314 for an enclosed payload body. For example, a Content-Length header
315 field is normally sent in a POST request even when the value is 0
316 (indicating an empty payload body). A user agent SHOULD NOT send a
317 Content-Length header field when the request message does not contain
318 a payload body and the method semantics do not anticipate such a
322 zend_hash_str_update(&msg
->hdrs
, "Content-Length", lenof("Content-Length"), &h
);
328 static void message_headers(php_http_message_t
*msg
, php_http_buffer_t
*str
)
333 case PHP_HTTP_REQUEST
:
334 php_http_buffer_appendf(str
, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&msg
->http
, tmp
, PHP_HTTP_CRLF
));
338 case PHP_HTTP_RESPONSE
:
339 php_http_buffer_appendf(str
, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&msg
->http
, tmp
, PHP_HTTP_CRLF
));
347 php_http_message_update_headers(msg
);
348 php_http_header_to_string(str
, &msg
->hdrs
);
351 void php_http_message_to_callback(php_http_message_t
*msg
, php_http_pass_callback_t cb
, void *cb_arg
)
353 php_http_buffer_t str
;
355 php_http_buffer_init_ex(&str
, 0x1000, 0);
356 message_headers(msg
, &str
);
357 cb(cb_arg
, str
.data
, str
.used
);
358 php_http_buffer_dtor(&str
);
360 if (php_http_message_body_size(msg
->body
)) {
361 cb(cb_arg
, ZEND_STRL(PHP_HTTP_CRLF
));
362 php_http_message_body_to_callback(msg
->body
, cb
, cb_arg
, 0, 0);
366 void php_http_message_to_string(php_http_message_t
*msg
, char **string
, size_t *length
)
368 php_http_buffer_t str
;
371 php_http_buffer_init_ex(&str
, 0x1000, 0);
372 message_headers(msg
, &str
);
373 if (php_http_message_body_size(msg
->body
)) {
374 php_http_buffer_appends(&str
, PHP_HTTP_CRLF
);
375 php_http_message_body_to_callback(msg
->body
, (php_http_pass_callback_t
) php_http_buffer_append
, &str
, 0, 0);
378 data
= php_http_buffer_data(&str
, string
, length
);
383 php_http_buffer_dtor(&str
);
386 void php_http_message_serialize(php_http_message_t
*message
, char **string
, size_t *length
)
389 php_http_buffer_t str
;
390 php_http_message_t
*msg
;
392 php_http_buffer_init(&str
);
394 msg
= message
= php_http_message_reverse(message
);
396 php_http_message_to_callback(message
, (php_http_pass_callback_t
) php_http_buffer_append
, &str
);
397 php_http_buffer_appends(&str
, PHP_HTTP_CRLF
);
398 } while ((message
= message
->parent
));
399 php_http_message_reverse(msg
);
401 buf
= php_http_buffer_data(&str
, string
, length
);
406 php_http_buffer_dtor(&str
);
409 php_http_message_t
*php_http_message_reverse(php_http_message_t
*msg
)
411 size_t i
, c
= php_http_message_count(msg
);
414 php_http_message_t
*tmp
= msg
, **arr
;
416 arr
= ecalloc(c
, sizeof(**arr
));
417 for (i
= 0; i
< c
; ++i
) {
421 arr
[0]->parent
= NULL
;
422 for (i
= 0; i
< c
-1; ++i
) {
423 arr
[i
+1]->parent
= arr
[i
];
433 php_http_message_t
*php_http_message_zip(php_http_message_t
*dst
, php_http_message_t
*src
)
435 php_http_message_t
*tmp_dst
, *tmp_src
, *ret
= dst
;
438 tmp_dst
= dst
->parent
;
439 tmp_src
= src
->parent
;
442 src
->parent
= tmp_dst
;
451 php_http_message_t
*php_http_message_copy_ex(php_http_message_t
*from
, php_http_message_t
*to
, zend_bool parents
)
453 php_http_message_t
*temp
, *copy
= NULL
;
454 php_http_info_t info
;
457 info
.type
= from
->type
;
458 info
.http
= from
->http
;
460 copy
= temp
= php_http_message_init(to
, 0, php_http_message_body_copy(from
->body
, NULL
));
461 php_http_message_set_info(temp
, &info
);
462 zend_hash_copy(&temp
->hdrs
, &from
->hdrs
, (copy_ctor_func_t
) zval_add_ref
);
464 if (parents
) while (from
->parent
) {
465 info
.type
= from
->parent
->type
;
466 info
.http
= from
->parent
->http
;
468 temp
->parent
= php_http_message_init(NULL
, 0, php_http_message_body_copy(from
->parent
->body
, NULL
));
469 php_http_message_set_info(temp
->parent
, &info
);
470 array_copy(&from
->parent
->hdrs
, &temp
->parent
->hdrs
);
481 void php_http_message_dtor(php_http_message_t
*message
)
484 zend_hash_destroy(&message
->hdrs
);
485 php_http_message_body_free(&message
->body
);
487 switch (message
->type
) {
488 case PHP_HTTP_REQUEST
:
489 PTR_SET(message
->http
.info
.request
.method
, NULL
);
490 PTR_SET(message
->http
.info
.request
.url
, NULL
);
493 case PHP_HTTP_RESPONSE
:
494 PTR_SET(message
->http
.info
.response
.status
, NULL
);
503 void php_http_message_free(php_http_message_t
**message
)
506 if ((*message
)->parent
) {
507 php_http_message_free(&(*message
)->parent
);
509 php_http_message_dtor(*message
);
515 static zval
*php_http_message_object_read_prop(zval
*object
, zval
*member
, int type
, void **cache_slot
, zval
*rv
);
516 static void php_http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value
, void **cache_slot
);
518 static zend_object_handlers php_http_message_object_handlers
;
519 static HashTable php_http_message_object_prophandlers
;
521 static void php_http_message_object_prophandler_hash_dtor(zval
*pData
)
523 pefree(Z_PTR_P(pData
), 1);
526 typedef void (*php_http_message_object_prophandler_func_t
)(php_http_message_object_t
*o
, zval
*v
);
528 typedef struct php_http_message_object_prophandler
{
529 php_http_message_object_prophandler_func_t read
;
530 php_http_message_object_prophandler_func_t write
;
531 } php_http_message_object_prophandler_t
;
533 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
) {
534 php_http_message_object_prophandler_t h
= { read
, write
};
535 if (!zend_hash_str_add_mem(&php_http_message_object_prophandlers
, prop_str
, prop_len
, (void *) &h
, sizeof(h
))) {
540 static php_http_message_object_prophandler_t
*php_http_message_object_get_prophandler(zend_string
*name_str
) {
541 return zend_hash_str_find_ptr(&php_http_message_object_prophandlers
, name_str
->val
, name_str
->len
);
543 static void php_http_message_object_prophandler_get_type(php_http_message_object_t
*obj
, zval
*return_value
) {
544 RETVAL_LONG(obj
->message
->type
);
546 static void php_http_message_object_prophandler_set_type(php_http_message_object_t
*obj
, zval
*value
) {
547 php_http_message_set_type(obj
->message
, zval_get_long(value
));
549 static void php_http_message_object_prophandler_get_request_method(php_http_message_object_t
*obj
, zval
*return_value
) {
550 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
) && obj
->message
->http
.info
.request
.method
) {
551 RETVAL_STRING(obj
->message
->http
.info
.request
.method
);
556 static void php_http_message_object_prophandler_set_request_method(php_http_message_object_t
*obj
, zval
*value
) {
557 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
558 zend_string
*zs
= zval_get_string(value
);
559 PTR_SET(obj
->message
->http
.info
.request
.method
, estrndup(zs
->val
, zs
->len
));
560 zend_string_release(zs
);
563 static void php_http_message_object_prophandler_get_request_url(php_http_message_object_t
*obj
, zval
*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
, ~0));
578 static void php_http_message_object_prophandler_get_response_status(php_http_message_object_t
*obj
, zval
*return_value
) {
579 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
) && obj
->message
->http
.info
.response
.status
) {
580 RETVAL_STRING(obj
->message
->http
.info
.response
.status
);
585 static void php_http_message_object_prophandler_set_response_status(php_http_message_object_t
*obj
, zval
*value
) {
586 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
587 zend_string
*zs
= zval_get_string(value
);
588 PTR_SET(obj
->message
->http
.info
.response
.status
, estrndup(zs
->val
, zs
->len
));
589 zend_string_release(zs
);
592 static void php_http_message_object_prophandler_get_response_code(php_http_message_object_t
*obj
, zval
*return_value
) {
593 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
594 RETVAL_LONG(obj
->message
->http
.info
.response
.code
);
599 static void php_http_message_object_prophandler_set_response_code(php_http_message_object_t
*obj
, zval
*value
) {
600 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
601 obj
->message
->http
.info
.response
.code
= zval_get_long(value
);
602 PTR_SET(obj
->message
->http
.info
.response
.status
, estrdup(php_http_env_get_response_status_for_code(obj
->message
->http
.info
.response
.code
)));
605 static void php_http_message_object_prophandler_get_http_version(php_http_message_object_t
*obj
, zval
*return_value
) {
609 php_http_version_to_string(&obj
->message
->http
.version
, &version_str
, &version_len
, NULL
, NULL
);
610 RETVAL_STR(php_http_cs2zs(version_str
, version_len
));
612 static void php_http_message_object_prophandler_set_http_version(php_http_message_object_t
*obj
, zval
*value
) {
613 zend_string
*zs
= zval_get_string(value
);
614 php_http_version_parse(&obj
->message
->http
.version
, zs
->val
);
615 zend_string_release(zs
);
617 static void php_http_message_object_prophandler_get_headers(php_http_message_object_t
*obj
, zval
*return_value
) {
618 array_init(return_value
);
619 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(return_value
));
621 static void php_http_message_object_prophandler_set_headers(php_http_message_object_t
*obj
, zval
*value
) {
623 zval
*orig_value
= value
;
625 if (Z_TYPE_P(value
) != IS_ARRAY
&& Z_TYPE_P(value
) != IS_OBJECT
) {
626 convert_to_array_ex(value
);
628 headers
= HASH_OF(value
);
630 zend_hash_clean(&obj
->message
->hdrs
);
631 array_copy(headers
, &obj
->message
->hdrs
);
633 if (orig_value
!= value
) {
634 zval_ptr_dtor(value
);
637 static void php_http_message_object_prophandler_get_body(php_http_message_object_t
*obj
, zval
*return_value
) {
639 RETVAL_OBJECT(&obj
->body
->zo
, 1);
644 static void php_http_message_object_prophandler_set_body(php_http_message_object_t
*obj
, zval
*value
) {
645 php_http_message_object_set_body(obj
, value
);
647 static void php_http_message_object_prophandler_get_parent_message(php_http_message_object_t
*obj
, zval
*return_value
) {
648 if (obj
->message
->parent
) {
649 RETVAL_OBJECT(&obj
->parent
->zo
, 1);
654 static void php_http_message_object_prophandler_set_parent_message(php_http_message_object_t
*obj
, zval
*value
) {
655 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), php_http_message_class_entry
)) {
656 php_http_message_object_t
*parent_obj
= PHP_HTTP_OBJ(NULL
, value
);
658 if (obj
->message
->parent
) {
659 zend_objects_store_del(&obj
->parent
->zo
);
662 obj
->parent
= parent_obj
;
663 obj
->message
->parent
= parent_obj
->message
;
667 #define PHP_HTTP_MESSAGE_OBJECT_INIT(obj) \
669 if (!obj->message) { \
670 obj->message = php_http_message_init(NULL, 0, NULL); \
675 void php_http_message_object_reverse(zval
*zmsg
, zval
*return_value
)
678 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, zmsg
);
680 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
683 i
= php_http_message_count(obj
->message
);
686 php_http_message_object_t
**objects
;
689 objects
= ecalloc(i
, sizeof(**objects
));
691 /* we are the first message */
695 for (i
= 1; obj
->parent
; ++i
) {
696 objects
[i
] = obj
= obj
->parent
;
699 /* reorder parents */
700 for (last
= --i
; i
; --i
) {
701 objects
[i
]->message
->parent
= objects
[i
-1]->message
;
702 objects
[i
]->parent
= objects
[i
-1];
705 objects
[0]->message
->parent
= NULL
;
706 objects
[0]->parent
= NULL
;
708 /* add ref, because we previously have not been a parent message */
710 /* no addref, because we've been a parent message previously */
711 RETVAL_OBJECT(&objects
[last
]->zo
, 0);
715 RETURN_ZVAL(zmsg
, 1, 0);
719 void php_http_message_object_prepend(zval
*this_ptr
, zval
*prepend
, zend_bool top
)
721 php_http_message_t
*save_parent_msg
= NULL
;
722 php_http_message_object_t
*save_parent_obj
= NULL
, *obj
= PHP_HTTP_OBJ(NULL
, this_ptr
);
723 php_http_message_object_t
*prepend_obj
= PHP_HTTP_OBJ(NULL
, prepend
);
726 save_parent_obj
= obj
->parent
;
727 save_parent_msg
= obj
->message
->parent
;
729 /* iterate to the most parent object */
730 while (obj
->parent
) {
736 obj
->parent
= prepend_obj
;
737 obj
->message
->parent
= prepend_obj
->message
;
743 prepend_obj
->parent
= save_parent_obj
;
744 prepend_obj
->message
->parent
= save_parent_msg
;
748 ZEND_RESULT_CODE
php_http_message_object_set_body(php_http_message_object_t
*msg_obj
, zval
*zbody
)
751 zend_string
*body_str
;
752 php_http_message_body_t
*body
;
753 php_http_message_body_object_t
*body_obj
;
755 switch (Z_TYPE_P(zbody
)) {
757 php_stream_from_zval_no_verify(s
, zbody
);
759 php_http_throw(unexpected_val
, "The stream is not a valid resource", NULL
);
765 body
= php_http_message_body_init(NULL
, s
);
766 if (!(body_obj
= php_http_message_body_object_new_ex(php_http_message_body_class_entry
, body
))) {
767 php_http_message_body_free(&body
);
773 if (instanceof_function(Z_OBJCE_P(zbody
), php_http_message_body_class_entry
)) {
775 body_obj
= PHP_HTTP_OBJ(NULL
, zbody
);
781 body_str
= zval_get_string(zbody
);
782 s
= php_stream_temp_new();
783 php_stream_write(s
, body_str
->val
, body_str
->len
);
784 zend_string_release(body_str
);
789 if (!body_obj
->body
) {
790 body_obj
->body
= php_http_message_body_init(NULL
, NULL
);
793 zend_objects_store_del(&msg_obj
->body
->zo
);
795 if (msg_obj
->message
) {
796 php_http_message_body_free(&msg_obj
->message
->body
);
797 msg_obj
->message
->body
= body_obj
->body
;
799 msg_obj
->message
= php_http_message_init(NULL
, 0, body_obj
->body
);
801 php_http_message_body_addref(body_obj
->body
);
802 msg_obj
->body
= body_obj
;
807 ZEND_RESULT_CODE
php_http_message_object_init_body_object(php_http_message_object_t
*obj
)
809 php_http_message_body_addref(obj
->message
->body
);
810 return php_http_new((void *) &obj
->body
, php_http_message_body_class_entry
, (php_http_new_t
) php_http_message_body_object_new_ex
, NULL
, obj
->message
->body
);
813 zend_object
*php_http_message_object_new(zend_class_entry
*ce
)
815 return &php_http_message_object_new_ex(ce
, NULL
)->zo
;
818 php_http_message_object_t
*php_http_message_object_new_ex(zend_class_entry
*ce
, php_http_message_t
*msg
)
820 php_http_message_object_t
*o
;
822 o
= ecalloc(1, sizeof(*o
) + zend_object_properties_size(ce
));
823 zend_object_std_init(&o
->zo
, ce
);
824 object_properties_init(&o
->zo
, ce
);
829 o
->parent
= php_http_message_object_new_ex(ce
, msg
->parent
);
831 o
->body
= php_http_message_body_object_new_ex(php_http_message_body_class_entry
, php_http_message_body_init(&msg
->body
, NULL
));
834 o
->zo
.handlers
= &php_http_message_object_handlers
;
839 zend_object
*php_http_message_object_clone(zval
*this_ptr
)
841 php_http_message_object_t
*new_obj
= NULL
;
842 php_http_message_object_t
*old_obj
= PHP_HTTP_OBJ(NULL
, this_ptr
);
844 new_obj
= php_http_message_object_new_ex(old_obj
->zo
.ce
, php_http_message_copy(old_obj
->message
, NULL
));
845 zend_objects_clone_members(&new_obj
->zo
, &old_obj
->zo
);
850 void php_http_message_object_free(zend_object
*object
)
852 php_http_message_object_t
*o
= PHP_HTTP_OBJ(object
, NULL
);
854 if (!Z_ISUNDEF(o
->iterator
)) {
855 zval_ptr_dtor(&o
->iterator
);
856 ZVAL_UNDEF(&o
->iterator
);
859 /* do NOT free recursivly */
860 php_http_message_dtor(o
->message
);
865 if (GC_REFCOUNT(&o
->parent
->zo
) == 1) {
866 zend_objects_store_del(&o
->parent
->zo
);
868 zend_objects_store_del(&o
->parent
->zo
);
872 if (GC_REFCOUNT(&o
->body
->zo
) == 1) {
873 zend_objects_store_del(&o
->body
->zo
);
875 zend_objects_store_del(&o
->body
->zo
);
878 zend_object_std_dtor(object
);
881 static zval
*php_http_message_object_read_prop(zval
*object
, zval
*member
, int type
, void **cache_slot
, zval
*tmp
)
884 zend_string
*member_name
= zval_get_string(member
);
885 php_http_message_object_prophandler_t
*handler
= php_http_message_object_get_prophandler(member_name
);
887 if (!handler
|| type
== BP_VAR_R
|| type
== BP_VAR_IS
) {
888 return_value
= zend_get_std_object_handlers()->read_property(object
, member
, type
, cache_slot
, tmp
);
891 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, object
);
893 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
894 handler
->read(obj
, tmp
);
896 zval_ptr_dtor(return_value
);
897 ZVAL_COPY_VALUE(return_value
, tmp
);
899 zend_string_release(member_name
);
902 php_property_proxy_t
*proxy
;
903 php_property_proxy_object_t
*proxy_obj
;
905 proxy
= php_property_proxy_init(object
, member_name
);
906 proxy_obj
= php_property_proxy_object_new_ex(NULL
, proxy
);
908 ZVAL_OBJ(tmp
, &proxy_obj
->zo
);
909 zend_string_release(member_name
);
914 static void php_http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value
, void **cache_slot
)
916 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, object
);
917 php_http_message_object_prophandler_t
*handler
;
918 zend_string
*member_name
= zval_get_string(member
);
920 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
922 if ((handler
= php_http_message_object_get_prophandler(member_name
))) {
923 handler
->write(obj
, value
);
925 zend_get_std_object_handlers()->write_property(object
, member
, value
, cache_slot
);
928 zend_string_release(member_name
);
931 static HashTable
*php_http_message_object_get_debug_info(zval
*object
, int *is_temp
)
934 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, object
);
935 HashTable
*props
= zend_get_std_object_handlers()->get_properties(object
);
936 char *ver_str
, *url_str
= NULL
;
937 size_t ver_len
, url_len
= 0;
939 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
942 #define UPDATE_PROP(name_str, action_with_tmp) \
944 zend_property_info *pi; \
945 if ((pi = zend_hash_str_find_ptr(&obj->zo.ce->properties_info, name_str, lenof(name_str)))) { \
947 zend_hash_update_ind(props, pi->name, &tmp); \
951 UPDATE_PROP("type", ZVAL_LONG(&tmp
, obj
->message
->type
));
953 ver_len
= spprintf(&ver_str
, 0, "%u.%u", obj
->message
->http
.version
.major
, obj
->message
->http
.version
.minor
);
954 UPDATE_PROP("httpVersion", ZVAL_STR(&tmp
, php_http_cs2zs(ver_str
, ver_len
)));
956 switch (obj
->message
->type
) {
957 case PHP_HTTP_REQUEST
:
958 UPDATE_PROP("responseCode", ZVAL_LONG(&tmp
, 0));
959 UPDATE_PROP("responseStatus", ZVAL_EMPTY_STRING(&tmp
));
960 UPDATE_PROP("requestMethod", ZVAL_STRING(&tmp
, STR_PTR(obj
->message
->http
.info
.request
.method
)));
961 if (obj
->message
->http
.info
.request
.url
) {
962 php_http_url_to_string(obj
->message
->http
.info
.request
.url
, &url_str
, &url_len
, 0);
963 UPDATE_PROP("requestUrl", ZVAL_STR(&tmp
, php_http_cs2zs(url_str
, url_len
)));
965 UPDATE_PROP("requestUrl", ZVAL_EMPTY_STRING(&tmp
));
970 case PHP_HTTP_RESPONSE
:
971 UPDATE_PROP("responseCode", ZVAL_LONG(&tmp
, obj
->message
->http
.info
.response
.code
));
972 UPDATE_PROP("responseStatus", ZVAL_STRING(&tmp
, STR_PTR(obj
->message
->http
.info
.response
.status
)));
973 UPDATE_PROP("requestMethod", ZVAL_EMPTY_STRING(&tmp
));
974 UPDATE_PROP("requestUrl", ZVAL_EMPTY_STRING(&tmp
));
979 UPDATE_PROP("responseCode", ZVAL_LONG(&tmp
, 0));
980 UPDATE_PROP("responseStatus", ZVAL_EMPTY_STRING(&tmp
));
981 UPDATE_PROP("requestMethod", ZVAL_EMPTY_STRING(&tmp
));
982 UPDATE_PROP("requestUrl", ZVAL_EMPTY_STRING(&tmp
));
986 UPDATE_PROP("headers",
988 array_copy(&obj
->message
->hdrs
, Z_ARRVAL(tmp
));
993 ZVAL_OBJECT(&tmp
, &obj
->body
->zo
, 1);
999 UPDATE_PROP("parentMessage",
1000 if (obj
->message
->parent
) {
1001 ZVAL_OBJECT(&tmp
, &obj
->parent
->zo
, 1);
1010 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage___construct
, 0, 0, 0)
1011 ZEND_ARG_INFO(0, message
)
1012 ZEND_ARG_INFO(0, greedy
)
1013 ZEND_END_ARG_INFO();
1014 static PHP_METHOD(HttpMessage
, __construct
)
1016 zend_bool greedy
= 1;
1017 zval
*zmessage
= NULL
;
1018 php_http_message_t
*msg
= NULL
;
1019 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1020 zend_error_handling zeh
;
1022 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|z!b", &zmessage
, &greedy
), invalid_arg
, return);
1024 zend_replace_error_handling(EH_THROW
, php_http_exception_bad_message_class_entry
, &zeh
);
1025 if (zmessage
&& Z_TYPE_P(zmessage
) == IS_RESOURCE
) {
1027 php_http_message_parser_t p
;
1028 zend_error_handling zeh
;
1030 zend_replace_error_handling(EH_THROW
, php_http_exception_unexpected_val_class_entry
, &zeh
);
1031 php_stream_from_zval(s
, zmessage
);
1032 zend_restore_error_handling(&zeh
);
1034 if (s
&& php_http_message_parser_init(&p
)) {
1035 unsigned flags
= (greedy
? PHP_HTTP_MESSAGE_PARSER_GREEDY
: 0);
1036 php_http_buffer_t buf
;
1038 php_http_buffer_init_ex(&buf
, 0x1000, PHP_HTTP_BUFFER_INIT_PREALLOC
);
1039 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE
== php_http_message_parser_parse_stream(&p
, &buf
, s
, flags
, &msg
)) {
1040 if (!EG(exception
)) {
1041 php_http_throw(bad_message
, "Could not parse message from stream", NULL
);
1044 php_http_buffer_dtor(&buf
);
1045 php_http_message_parser_dtor(&p
);
1048 if (!msg
&& !EG(exception
)) {
1049 php_http_throw(bad_message
, "Empty message received from stream", NULL
);
1051 } else if (zmessage
) {
1052 zend_string
*zs_msg
= zval_get_string(zmessage
);
1054 msg
= php_http_message_parse(NULL
, zs_msg
->val
, zs_msg
->len
, greedy
);
1056 if (!msg
&& !EG(exception
)) {
1057 php_http_throw(bad_message
, "Could not parse message: %.*s", MIN(25, zs_msg
->len
), zs_msg
->val
);
1059 zend_string_release(zs_msg
);
1063 php_http_message_dtor(obj
->message
);
1065 if (obj
->message
->parent
) {
1066 obj
->parent
= php_http_message_object_new_ex(obj
->zo
.ce
, obj
->message
->parent
);
1069 zend_restore_error_handling(&zeh
);
1070 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1073 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getBody
, 0, 0, 0)
1074 ZEND_END_ARG_INFO();
1075 static PHP_METHOD(HttpMessage
, getBody
)
1077 php_http_message_object_t
*obj
;
1079 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
1081 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1082 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1085 php_http_message_object_init_body_object(obj
);
1089 RETVAL_OBJECT(&obj
->body
->zo
, 1);
1093 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setBody
, 0, 0, 1)
1094 ZEND_ARG_OBJ_INFO(0, body
, http
\\Message
\\Body
, 0)
1095 ZEND_END_ARG_INFO();
1096 static PHP_METHOD(HttpMessage
, setBody
)
1100 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zbody
, php_http_message_body_class_entry
)) {
1101 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1103 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1104 php_http_message_object_prophandler_set_body(obj
, zbody
);
1106 RETVAL_ZVAL(getThis(), 1, 0);
1109 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addBody
, 0, 0, 1)
1110 ZEND_ARG_OBJ_INFO(0, body
, http
\\Message
\\Body
, 0)
1111 ZEND_END_ARG_INFO();
1112 static PHP_METHOD(HttpMessage
, addBody
)
1116 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O", &new_body
, php_http_message_body_class_entry
)) {
1117 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1118 php_http_message_body_object_t
*new_obj
= PHP_HTTP_OBJ(NULL
, new_body
);
1120 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1121 php_http_message_body_to_callback(new_obj
->body
, (php_http_pass_callback_t
) php_http_message_body_append
, obj
->message
->body
, 0, 0);
1123 RETVAL_ZVAL(getThis(), 1, 0);
1126 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHeader
, 0, 0, 1)
1127 ZEND_ARG_INFO(0, header
)
1128 ZEND_ARG_INFO(0, into_class
)
1129 ZEND_END_ARG_INFO();
1130 static PHP_METHOD(HttpMessage
, getHeader
)
1134 zend_class_entry
*header_ce
= NULL
;
1136 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s|C!", &header_str
, &header_len
, &header_ce
)) {
1137 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1140 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1142 if ((header
= php_http_message_header(obj
->message
, header_str
, header_len
))) {
1144 RETURN_ZVAL(header
, 1, 0);
1145 } else if (instanceof_function(header_ce
, php_http_header_class_entry
)) {
1146 php_http_object_method_t cb
;
1149 ZVAL_STRINGL(&argv
[0], header_str
, header_len
);
1150 ZVAL_COPY(&argv
[1], header
);
1152 object_init_ex(return_value
, header_ce
);
1153 php_http_object_method_init(&cb
, return_value
, ZEND_STRL("__construct"));
1154 php_http_object_method_call(&cb
, return_value
, NULL
, 2, argv
);
1155 php_http_object_method_dtor(&cb
);
1157 zval_ptr_dtor(&argv
[0]);
1158 zval_ptr_dtor(&argv
[1]);
1162 php_error_docref(NULL
, E_WARNING
, "Class '%s' is not as descendant of http\\Header", header_ce
->name
->val
);
1169 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHeaders
, 0, 0, 0)
1170 ZEND_END_ARG_INFO();
1171 static PHP_METHOD(HttpMessage
, getHeaders
)
1173 if (SUCCESS
== zend_parse_parameters_none()) {
1174 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1176 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1178 array_init(return_value
);
1179 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(return_value
));
1183 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHeader
, 0, 0, 1)
1184 ZEND_ARG_INFO(0, header
)
1185 ZEND_ARG_INFO(0, value
)
1186 ZEND_END_ARG_INFO();
1187 static PHP_METHOD(HttpMessage
, setHeader
)
1189 zval
*zvalue
= NULL
;
1193 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s|z!", &name_str
, &name_len
, &zvalue
)) {
1194 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1195 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1197 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1200 zend_symtable_str_del(&obj
->message
->hdrs
, name
, name_len
);
1202 Z_TRY_ADDREF_P(zvalue
);
1203 zend_symtable_str_update(&obj
->message
->hdrs
, name
, name_len
, zvalue
);
1207 RETVAL_ZVAL(getThis(), 1, 0);
1210 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHeaders
, 0, 0, 1)
1211 ZEND_ARG_ARRAY_INFO(0, headers
, 1)
1212 ZEND_END_ARG_INFO();
1213 static PHP_METHOD(HttpMessage
, setHeaders
)
1215 zval
*new_headers
= NULL
;
1217 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "a/!", &new_headers
)) {
1218 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1220 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1222 zend_hash_clean(&obj
->message
->hdrs
);
1224 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, 0, ARRAY_JOIN_PRETTIFY
|ARRAY_JOIN_STRONLY
);
1227 RETVAL_ZVAL(getThis(), 1, 0);
1230 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeader
, 0, 0, 2)
1231 ZEND_ARG_INFO(0, header
)
1232 ZEND_ARG_INFO(0, value
)
1233 ZEND_END_ARG_INFO();
1234 static PHP_METHOD(HttpMessage
, addHeader
)
1240 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "sz", &name_str
, &name_len
, &zvalue
)) {
1241 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1242 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1245 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1247 Z_TRY_ADDREF_P(zvalue
);
1248 if ((header
= php_http_message_header(obj
->message
, name
, name_len
))) {
1249 convert_to_array(header
);
1250 zend_hash_next_index_insert(Z_ARRVAL_P(header
), zvalue
);
1252 zend_symtable_str_update(&obj
->message
->hdrs
, name
, name_len
, zvalue
);
1256 RETVAL_ZVAL(getThis(), 1, 0);
1259 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeaders
, 0, 0, 1)
1260 ZEND_ARG_ARRAY_INFO(0, headers
, 0)
1261 ZEND_ARG_INFO(0, append
)
1262 ZEND_END_ARG_INFO();
1263 static PHP_METHOD(HttpMessage
, addHeaders
)
1266 zend_bool append
= 0;
1268 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "a|b", &new_headers
, &append
)) {
1269 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1271 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1273 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, append
, ARRAY_JOIN_STRONLY
|ARRAY_JOIN_PRETTIFY
);
1275 RETVAL_ZVAL(getThis(), 1, 0);
1278 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getType
, 0, 0, 0)
1279 ZEND_END_ARG_INFO();
1280 static PHP_METHOD(HttpMessage
, getType
)
1282 if (SUCCESS
== zend_parse_parameters_none()) {
1283 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1285 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1287 RETURN_LONG(obj
->message
->type
);
1291 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setType
, 0, 0, 1)
1292 ZEND_ARG_INFO(0, type
)
1293 ZEND_END_ARG_INFO();
1294 static PHP_METHOD(HttpMessage
, setType
)
1298 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "l", &type
)) {
1299 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1301 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1303 php_http_message_set_type(obj
->message
, type
);
1305 RETVAL_ZVAL(getThis(), 1, 0);
1308 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getInfo
, 0, 0, 0)
1309 ZEND_END_ARG_INFO();
1310 static PHP_METHOD(HttpMessage
, getInfo
)
1312 if (SUCCESS
== zend_parse_parameters_none()) {
1313 char *str
, *tmp
= NULL
;
1315 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1317 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1319 switch (obj
->message
->type
) {
1320 case PHP_HTTP_REQUEST
:
1321 len
= spprintf(&str
, 0, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&obj
->message
->http
, tmp
, ""));
1324 case PHP_HTTP_RESPONSE
:
1325 len
= spprintf(&str
, 0, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&obj
->message
->http
, tmp
, ""));
1333 RETVAL_STR(php_http_cs2zs(str
, len
));
1337 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setInfo
, 0, 0, 1)
1338 ZEND_ARG_INFO(0, http_info
)
1339 ZEND_END_ARG_INFO();
1340 static PHP_METHOD(HttpMessage
, setInfo
)
1344 php_http_message_object_t
*obj
;
1345 php_http_info_t inf
;
1347 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
), invalid_arg
, return);
1349 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1350 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1352 if (!php_http_info_parse(&inf
, str
)) {
1353 php_http_throw(bad_header
, "Could not parse message info '%s'", str
);
1357 php_http_message_set_info(obj
->message
, &inf
);
1358 php_http_info_dtor(&inf
);
1360 RETVAL_ZVAL(getThis(), 1, 0);
1363 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHttpVersion
, 0, 0, 0)
1364 ZEND_END_ARG_INFO();
1365 static PHP_METHOD(HttpMessage
, getHttpVersion
)
1367 if (SUCCESS
== zend_parse_parameters_none()) {
1370 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1372 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1374 php_http_version_to_string(&obj
->message
->http
.version
, &str
, &len
, NULL
, NULL
);
1375 RETURN_STR(php_http_cs2zs(str
, len
));
1379 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHttpVersion
, 0, 0, 1)
1380 ZEND_ARG_INFO(0, http_version
)
1381 ZEND_END_ARG_INFO();
1382 static PHP_METHOD(HttpMessage
, setHttpVersion
)
1386 php_http_version_t version
;
1387 php_http_message_object_t
*obj
;
1389 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &v_str
, &v_len
), invalid_arg
, return);
1391 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1392 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1394 php_http_expect(php_http_version_parse(&version
, v_str
), unexpected_val
, return);
1396 obj
->message
->http
.version
= version
;
1398 RETVAL_ZVAL(getThis(), 1, 0);
1401 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getResponseCode
, 0, 0, 0)
1402 ZEND_END_ARG_INFO();
1403 static PHP_METHOD(HttpMessage
, getResponseCode
)
1405 if (SUCCESS
== zend_parse_parameters_none()) {
1406 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1408 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1410 if (obj
->message
->type
!= PHP_HTTP_RESPONSE
) {
1411 php_error_docref(NULL
, E_WARNING
, "http\\Message is not if type response");
1415 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
1419 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setResponseCode
, 0, 0, 1)
1420 ZEND_ARG_INFO(0, response_code
)
1421 ZEND_ARG_INFO(0, strict
)
1422 ZEND_END_ARG_INFO();
1423 static PHP_METHOD(HttpMessage
, setResponseCode
)
1426 zend_bool strict
= 1;
1427 php_http_message_object_t
*obj
;
1429 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &code
, &strict
), invalid_arg
, return);
1431 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1432 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1434 if (obj
->message
->type
!= PHP_HTTP_RESPONSE
) {
1435 php_http_throw(bad_method_call
, "http\\Message is not of type response", NULL
);
1439 if (strict
&& (code
< 100 || code
> 599)) {
1440 php_http_throw(invalid_arg
, "Invalid response code (100-599): %ld", code
);
1444 obj
->message
->http
.info
.response
.code
= code
;
1445 PTR_SET(obj
->message
->http
.info
.response
.status
, estrdup(php_http_env_get_response_status_for_code(code
)));
1447 RETVAL_ZVAL(getThis(), 1, 0);
1450 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getResponseStatus
, 0, 0, 0)
1451 ZEND_END_ARG_INFO();
1452 static PHP_METHOD(HttpMessage
, getResponseStatus
)
1454 if (SUCCESS
== zend_parse_parameters_none()) {
1455 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1457 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1459 if (obj
->message
->type
!= PHP_HTTP_RESPONSE
) {
1460 php_error_docref(NULL
, E_WARNING
, "http\\Message is not of type response");
1463 if (obj
->message
->http
.info
.response
.status
) {
1464 RETURN_STRING(obj
->message
->http
.info
.response
.status
);
1466 RETURN_EMPTY_STRING();
1471 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setResponseStatus
, 0, 0, 1)
1472 ZEND_ARG_INFO(0, response_status
)
1473 ZEND_END_ARG_INFO();
1474 static PHP_METHOD(HttpMessage
, setResponseStatus
)
1478 php_http_message_object_t
*obj
;
1480 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &status
, &status_len
), invalid_arg
, return);
1482 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1484 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1486 if (obj
->message
->type
!= PHP_HTTP_RESPONSE
) {
1487 php_http_throw(bad_method_call
, "http\\Message is not of type response", NULL
);
1490 PTR_SET(obj
->message
->http
.info
.response
.status
, estrndup(status
, status_len
));
1491 RETVAL_ZVAL(getThis(), 1, 0);
1494 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getRequestMethod
, 0, 0, 0)
1495 ZEND_END_ARG_INFO();
1496 static PHP_METHOD(HttpMessage
, getRequestMethod
)
1498 if (SUCCESS
== zend_parse_parameters_none()) {
1499 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1501 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1503 if (obj
->message
->type
!= PHP_HTTP_REQUEST
) {
1504 php_error_docref(NULL
, E_WARNING
, "http\\Message is not of type request");
1508 if (obj
->message
->http
.info
.request
.method
) {
1509 RETURN_STRING(obj
->message
->http
.info
.request
.method
);
1511 RETURN_EMPTY_STRING();
1516 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setRequestMethod
, 0, 0, 1)
1517 ZEND_ARG_INFO(0, request_method
)
1518 ZEND_END_ARG_INFO();
1519 static PHP_METHOD(HttpMessage
, setRequestMethod
)
1523 php_http_message_object_t
*obj
;
1525 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &method
, &method_len
), invalid_arg
, return);
1527 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1529 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1531 if (obj
->message
->type
!= PHP_HTTP_REQUEST
) {
1532 php_http_throw(bad_method_call
, "http\\Message is not of type request", NULL
);
1536 if (method_len
< 1) {
1537 php_http_throw(invalid_arg
, "Cannot set http\\Message's request method to an empty string", NULL
);
1541 PTR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
1542 RETVAL_ZVAL(getThis(), 1, 0);
1545 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getRequestUrl
, 0, 0, 0)
1546 ZEND_END_ARG_INFO();
1547 static PHP_METHOD(HttpMessage
, getRequestUrl
)
1549 if (SUCCESS
== zend_parse_parameters_none()) {
1550 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1552 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1554 if (obj
->message
->type
!= PHP_HTTP_REQUEST
) {
1555 php_error_docref(NULL
, E_WARNING
, "http\\Message is not of type request");
1559 if (obj
->message
->http
.info
.request
.url
) {
1563 php_http_url_to_string(obj
->message
->http
.info
.request
.url
, &url_str
, &url_len
, 0);
1564 RETURN_STR(php_http_cs2zs(url_str
, url_len
));
1566 RETURN_EMPTY_STRING();
1571 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setRequestUrl
, 0, 0, 1)
1572 ZEND_ARG_INFO(0, url
)
1573 ZEND_END_ARG_INFO();
1574 static PHP_METHOD(HttpMessage
, setRequestUrl
)
1577 php_http_url_t
*url
;
1578 php_http_message_object_t
*obj
;
1579 zend_error_handling zeh
;
1581 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zurl
), invalid_arg
, return);
1583 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1585 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1587 if (obj
->message
->type
!= PHP_HTTP_REQUEST
) {
1588 php_http_throw(bad_method_call
, "http\\Message is not of type request", NULL
);
1592 zend_replace_error_handling(EH_THROW
, php_http_exception_bad_url_class_entry
, &zeh
);
1593 url
= php_http_url_from_zval(zurl
, ~0);
1594 zend_restore_error_handling(&zeh
);
1596 if (url
&& php_http_url_is_empty(url
)) {
1597 php_http_url_free(&url
);
1598 php_http_throw(invalid_arg
, "Cannot set http\\Message's request url to an empty string", NULL
);
1600 PTR_SET(obj
->message
->http
.info
.request
.url
, url
);
1603 RETVAL_ZVAL(getThis(), 1, 0);
1606 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getParentMessage
, 0, 0, 0)
1607 ZEND_END_ARG_INFO();
1608 static PHP_METHOD(HttpMessage
, getParentMessage
)
1610 php_http_message_object_t
*obj
;
1612 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
1614 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1615 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1617 if (!obj
->message
->parent
) {
1618 php_http_throw(unexpected_val
, "http\\Message has not parent message", NULL
);
1622 RETVAL_OBJECT(&obj
->parent
->zo
, 1);
1625 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage___toString
, 0, 0, 0)
1626 ZEND_END_ARG_INFO();
1627 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toString
, 0, 0, 0)
1628 ZEND_ARG_INFO(0, include_parent
)
1629 ZEND_END_ARG_INFO();
1630 static PHP_METHOD(HttpMessage
, toString
)
1632 zend_bool include_parent
= 0;
1634 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &include_parent
)) {
1635 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1639 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1641 if (include_parent
) {
1642 php_http_message_serialize(obj
->message
, &string
, &length
);
1644 php_http_message_to_string(obj
->message
, &string
, &length
);
1647 RETURN_STR(php_http_cs2zs(string
, length
));
1650 RETURN_EMPTY_STRING();
1653 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toStream
, 0, 0, 1)
1654 ZEND_ARG_INFO(0, stream
)
1655 ZEND_END_ARG_INFO();
1656 static PHP_METHOD(HttpMessage
, toStream
)
1660 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zstream
)) {
1661 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1664 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1666 php_stream_from_zval(s
, zstream
);
1667 php_http_message_to_callback(obj
->message
, (php_http_pass_callback_t
) _php_stream_write
, s
);
1671 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toCallback
, 0, 0, 1)
1672 ZEND_ARG_INFO(0, callback
)
1673 ZEND_END_ARG_INFO();
1674 static PHP_METHOD(HttpMessage
, toCallback
)
1676 php_http_pass_fcall_arg_t fcd
;
1678 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "f", &fcd
.fci
, &fcd
.fcc
)) {
1679 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1681 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1683 ZVAL_COPY(&fcd
.fcz
, getThis());
1684 php_http_message_to_callback(obj
->message
, php_http_pass_fcall_callback
, &fcd
);
1685 zend_fcall_info_args_clear(&fcd
.fci
, 1);
1686 zval_ptr_dtor(&fcd
.fcz
);
1688 RETURN_ZVAL(getThis(), 1, 0);
1692 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_serialize
, 0, 0, 0)
1693 ZEND_END_ARG_INFO();
1694 static PHP_METHOD(HttpMessage
, serialize
)
1696 if (SUCCESS
== zend_parse_parameters_none()) {
1697 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1701 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1703 php_http_message_serialize(obj
->message
, &string
, &length
);
1704 RETURN_STR(php_http_cs2zs(string
, length
));
1706 RETURN_EMPTY_STRING();
1709 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_unserialize
, 0, 0, 1)
1710 ZEND_ARG_INFO(0, serialized
)
1711 ZEND_END_ARG_INFO();
1712 static PHP_METHOD(HttpMessage
, unserialize
)
1717 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &serialized
, &length
)) {
1718 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1719 php_http_message_t
*msg
;
1722 /* do not free recursively */
1723 php_http_message_dtor(obj
->message
);
1724 efree(obj
->message
);
1726 if ((msg
= php_http_message_parse(NULL
, serialized
, length
, 1))) {
1729 obj
->message
= php_http_message_init(NULL
, 0, NULL
);
1730 php_error_docref(NULL
, E_ERROR
, "Could not unserialize http\\Message");
1735 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_detach
, 0, 0, 0)
1736 ZEND_END_ARG_INFO();
1737 static PHP_METHOD(HttpMessage
, detach
)
1739 php_http_message_object_t
*obj
, *new_obj
;
1740 php_http_message_t
*msg_cpy
;
1742 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
1744 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1745 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1747 msg_cpy
= php_http_message_copy_ex(obj
->message
, NULL
, 0);
1748 new_obj
= php_http_message_object_new_ex(obj
->zo
.ce
, msg_cpy
);
1750 RETVAL_OBJ(&new_obj
->zo
);
1753 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_prepend
, 0, 0, 1)
1754 ZEND_ARG_OBJ_INFO(0, message
, http
\\Message
, 0)
1755 ZEND_ARG_INFO(0, top
)
1756 ZEND_END_ARG_INFO();
1757 static PHP_METHOD(HttpMessage
, prepend
)
1761 php_http_message_t
*msg
[2];
1762 php_http_message_object_t
*obj
, *prepend_obj
;
1764 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &prepend
, php_http_message_class_entry
, &top
), invalid_arg
, return);
1766 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1767 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1768 prepend_obj
= PHP_HTTP_OBJ(NULL
, prepend
);
1769 PHP_HTTP_MESSAGE_OBJECT_INIT(prepend_obj
);
1772 for (msg
[0] = obj
->message
; msg
[0]; msg
[0] = msg
[0]->parent
) {
1773 for (msg
[1] = prepend_obj
->message
; msg
[1]; msg
[1] = msg
[1]->parent
) {
1774 if (msg
[0] == msg
[1]) {
1775 php_http_throw(unexpected_val
, "Cannot prepend a message located within the same message chain", NULL
);
1781 php_http_message_object_prepend(getThis(), prepend
, top
);
1782 RETURN_ZVAL(getThis(), 1, 0);
1785 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_reverse
, 0, 0, 0)
1786 ZEND_END_ARG_INFO();
1787 static PHP_METHOD(HttpMessage
, reverse
)
1789 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
1791 php_http_message_object_reverse(getThis(), return_value
);
1794 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_isMultipart
, 0, 0, 0)
1795 ZEND_ARG_INFO(1, boundary
)
1796 ZEND_END_ARG_INFO();
1797 static PHP_METHOD(HttpMessage
, isMultipart
)
1799 zval
*zboundary
= NULL
;
1801 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|z!", &zboundary
)) {
1802 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1803 char *boundary
= NULL
;
1805 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1807 if (php_http_message_is_multipart(obj
->message
, zboundary
? &boundary
: NULL
)) {
1813 if (zboundary
&& boundary
) {
1814 ZVAL_DEREF(zboundary
);
1815 zval_dtor(zboundary
);
1816 ZVAL_STR(zboundary
, php_http_cs2zs(boundary
, strlen(boundary
)));
1821 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_splitMultipartBody
, 0, 0, 0)
1822 ZEND_END_ARG_INFO();
1823 static PHP_METHOD(HttpMessage
, splitMultipartBody
)
1825 php_http_message_object_t
*obj
;
1826 php_http_message_t
*msg
;
1827 char *boundary
= NULL
;
1829 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
1831 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1832 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1834 if (!php_http_message_is_multipart(obj
->message
, &boundary
)) {
1835 php_http_throw(bad_method_call
, "http\\Message is not a multipart message", NULL
);
1839 php_http_expect(msg
= php_http_message_body_split(obj
->message
->body
, boundary
), bad_message
, return);
1843 RETURN_OBJ(&php_http_message_object_new_ex(obj
->zo
.ce
, msg
)->zo
);
1846 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_count
, 0, 0, 0)
1847 ZEND_END_ARG_INFO();
1848 static PHP_METHOD(HttpMessage
, count
)
1850 zend_long count_mode
= -1;
1852 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &count_mode
)) {
1853 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1855 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1857 RETURN_LONG(php_http_message_count(obj
->message
));
1861 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_rewind
, 0, 0, 0)
1862 ZEND_END_ARG_INFO();
1863 static PHP_METHOD(HttpMessage
, rewind
)
1865 if (SUCCESS
== zend_parse_parameters_none()) {
1866 zval
*zobj
= getThis();
1867 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1869 if (!Z_ISUNDEF(obj
->iterator
)) {
1870 zval_ptr_dtor(&obj
->iterator
);
1872 ZVAL_COPY(&obj
->iterator
, zobj
);
1876 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_valid
, 0, 0, 0)
1877 ZEND_END_ARG_INFO();
1878 static PHP_METHOD(HttpMessage
, valid
)
1880 if (SUCCESS
== zend_parse_parameters_none()) {
1881 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1883 RETURN_BOOL(!Z_ISUNDEF(obj
->iterator
));
1887 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_next
, 0, 0, 0)
1888 ZEND_END_ARG_INFO();
1889 static PHP_METHOD(HttpMessage
, next
)
1891 if (SUCCESS
== zend_parse_parameters_none()) {
1892 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1894 if (!Z_ISUNDEF(obj
->iterator
)) {
1895 php_http_message_object_t
*itr
= PHP_HTTP_OBJ(NULL
, &obj
->iterator
);
1900 ZVAL_OBJECT(&tmp
, &itr
->parent
->zo
, 1);
1901 zval_ptr_dtor(&obj
->iterator
);
1902 obj
->iterator
= tmp
;
1904 zval_ptr_dtor(&obj
->iterator
);
1905 ZVAL_UNDEF(&obj
->iterator
);
1911 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_key
, 0, 0, 0)
1912 ZEND_END_ARG_INFO();
1913 static PHP_METHOD(HttpMessage
, key
)
1915 if (SUCCESS
== zend_parse_parameters_none()) {
1916 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1918 RETURN_LONG(Z_ISUNDEF(obj
->iterator
) ? 0 : Z_OBJ_HANDLE(obj
->iterator
));
1922 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_current
, 0, 0, 0)
1923 ZEND_END_ARG_INFO();
1924 static PHP_METHOD(HttpMessage
, current
)
1926 if (SUCCESS
== zend_parse_parameters_none()) {
1927 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1929 if (!Z_ISUNDEF(obj
->iterator
)) {
1930 RETURN_ZVAL(&obj
->iterator
, 1, 0);
1935 static zend_function_entry php_http_message_methods
[] = {
1936 PHP_ME(HttpMessage
, __construct
, ai_HttpMessage___construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
1937 PHP_ME(HttpMessage
, getBody
, ai_HttpMessage_getBody
, ZEND_ACC_PUBLIC
)
1938 PHP_ME(HttpMessage
, setBody
, ai_HttpMessage_setBody
, ZEND_ACC_PUBLIC
)
1939 PHP_ME(HttpMessage
, addBody
, ai_HttpMessage_addBody
, ZEND_ACC_PUBLIC
)
1940 PHP_ME(HttpMessage
, getHeader
, ai_HttpMessage_getHeader
, ZEND_ACC_PUBLIC
)
1941 PHP_ME(HttpMessage
, setHeader
, ai_HttpMessage_setHeader
, ZEND_ACC_PUBLIC
)
1942 PHP_ME(HttpMessage
, addHeader
, ai_HttpMessage_addHeader
, ZEND_ACC_PUBLIC
)
1943 PHP_ME(HttpMessage
, getHeaders
, ai_HttpMessage_getHeaders
, ZEND_ACC_PUBLIC
)
1944 PHP_ME(HttpMessage
, setHeaders
, ai_HttpMessage_setHeaders
, ZEND_ACC_PUBLIC
)
1945 PHP_ME(HttpMessage
, addHeaders
, ai_HttpMessage_addHeaders
, ZEND_ACC_PUBLIC
)
1946 PHP_ME(HttpMessage
, getType
, ai_HttpMessage_getType
, ZEND_ACC_PUBLIC
)
1947 PHP_ME(HttpMessage
, setType
, ai_HttpMessage_setType
, ZEND_ACC_PUBLIC
)
1948 PHP_ME(HttpMessage
, getInfo
, ai_HttpMessage_getInfo
, ZEND_ACC_PUBLIC
)
1949 PHP_ME(HttpMessage
, setInfo
, ai_HttpMessage_setInfo
, ZEND_ACC_PUBLIC
)
1950 PHP_ME(HttpMessage
, getResponseCode
, ai_HttpMessage_getResponseCode
, ZEND_ACC_PUBLIC
)
1951 PHP_ME(HttpMessage
, setResponseCode
, ai_HttpMessage_setResponseCode
, ZEND_ACC_PUBLIC
)
1952 PHP_ME(HttpMessage
, getResponseStatus
, ai_HttpMessage_getResponseStatus
, ZEND_ACC_PUBLIC
)
1953 PHP_ME(HttpMessage
, setResponseStatus
, ai_HttpMessage_setResponseStatus
, ZEND_ACC_PUBLIC
)
1954 PHP_ME(HttpMessage
, getRequestMethod
, ai_HttpMessage_getRequestMethod
, ZEND_ACC_PUBLIC
)
1955 PHP_ME(HttpMessage
, setRequestMethod
, ai_HttpMessage_setRequestMethod
, ZEND_ACC_PUBLIC
)
1956 PHP_ME(HttpMessage
, getRequestUrl
, ai_HttpMessage_getRequestUrl
, ZEND_ACC_PUBLIC
)
1957 PHP_ME(HttpMessage
, setRequestUrl
, ai_HttpMessage_setRequestUrl
, ZEND_ACC_PUBLIC
)
1958 PHP_ME(HttpMessage
, getHttpVersion
, ai_HttpMessage_getHttpVersion
, ZEND_ACC_PUBLIC
)
1959 PHP_ME(HttpMessage
, setHttpVersion
, ai_HttpMessage_setHttpVersion
, ZEND_ACC_PUBLIC
)
1960 PHP_ME(HttpMessage
, getParentMessage
, ai_HttpMessage_getParentMessage
, ZEND_ACC_PUBLIC
)
1961 PHP_ME(HttpMessage
, toString
, ai_HttpMessage_toString
, ZEND_ACC_PUBLIC
)
1962 PHP_ME(HttpMessage
, toCallback
, ai_HttpMessage_toCallback
, ZEND_ACC_PUBLIC
)
1963 PHP_ME(HttpMessage
, toStream
, ai_HttpMessage_toStream
, ZEND_ACC_PUBLIC
)
1965 /* implements Countable */
1966 PHP_ME(HttpMessage
, count
, ai_HttpMessage_count
, ZEND_ACC_PUBLIC
)
1968 /* implements Serializable */
1969 PHP_ME(HttpMessage
, serialize
, ai_HttpMessage_serialize
, ZEND_ACC_PUBLIC
)
1970 PHP_ME(HttpMessage
, unserialize
, ai_HttpMessage_unserialize
, ZEND_ACC_PUBLIC
)
1972 /* implements Iterator */
1973 PHP_ME(HttpMessage
, rewind
, ai_HttpMessage_rewind
, ZEND_ACC_PUBLIC
)
1974 PHP_ME(HttpMessage
, valid
, ai_HttpMessage_valid
, ZEND_ACC_PUBLIC
)
1975 PHP_ME(HttpMessage
, current
, ai_HttpMessage_current
, ZEND_ACC_PUBLIC
)
1976 PHP_ME(HttpMessage
, key
, ai_HttpMessage_key
, ZEND_ACC_PUBLIC
)
1977 PHP_ME(HttpMessage
, next
, ai_HttpMessage_next
, ZEND_ACC_PUBLIC
)
1979 ZEND_MALIAS(HttpMessage
, __toString
, toString
, ai_HttpMessage___toString
, ZEND_ACC_PUBLIC
)
1981 PHP_ME(HttpMessage
, detach
, ai_HttpMessage_detach
, ZEND_ACC_PUBLIC
)
1982 PHP_ME(HttpMessage
, prepend
, ai_HttpMessage_prepend
, ZEND_ACC_PUBLIC
)
1983 PHP_ME(HttpMessage
, reverse
, ai_HttpMessage_reverse
, ZEND_ACC_PUBLIC
)
1985 PHP_ME(HttpMessage
, isMultipart
, ai_HttpMessage_isMultipart
, ZEND_ACC_PUBLIC
)
1986 PHP_ME(HttpMessage
, splitMultipartBody
, ai_HttpMessage_splitMultipartBody
, ZEND_ACC_PUBLIC
)
1988 EMPTY_FUNCTION_ENTRY
1991 zend_class_entry
*php_http_message_class_entry
;
1993 PHP_MINIT_FUNCTION(http_message
)
1995 zend_class_entry ce
= {0};
1997 INIT_NS_CLASS_ENTRY(ce
, "http", "Message", php_http_message_methods
);
1998 php_http_message_class_entry
= zend_register_internal_class(&ce
);
1999 php_http_message_class_entry
->create_object
= php_http_message_object_new
;
2000 memcpy(&php_http_message_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
2001 php_http_message_object_handlers
.offset
= XtOffsetOf(php_http_message_object_t
, zo
);
2002 php_http_message_object_handlers
.clone_obj
= php_http_message_object_clone
;
2003 php_http_message_object_handlers
.free_obj
= php_http_message_object_free
;
2004 php_http_message_object_handlers
.read_property
= php_http_message_object_read_prop
;
2005 php_http_message_object_handlers
.write_property
= php_http_message_object_write_prop
;
2006 php_http_message_object_handlers
.get_debug_info
= php_http_message_object_get_debug_info
;
2007 php_http_message_object_handlers
.get_property_ptr_ptr
= NULL
;
2009 zend_class_implements(php_http_message_class_entry
, 3, spl_ce_Countable
, zend_ce_serializable
, zend_ce_iterator
);
2011 zend_hash_init(&php_http_message_object_prophandlers
, 9, NULL
, php_http_message_object_prophandler_hash_dtor
, 1);
2012 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("type"), PHP_HTTP_NONE
, ZEND_ACC_PROTECTED
);
2013 php_http_message_object_add_prophandler(ZEND_STRL("type"), php_http_message_object_prophandler_get_type
, php_http_message_object_prophandler_set_type
);
2014 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("body"), ZEND_ACC_PROTECTED
);
2015 php_http_message_object_add_prophandler(ZEND_STRL("body"), php_http_message_object_prophandler_get_body
, php_http_message_object_prophandler_set_body
);
2016 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestMethod"), "", ZEND_ACC_PROTECTED
);
2017 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
);
2018 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestUrl"), "", ZEND_ACC_PROTECTED
);
2019 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
);
2020 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("responseStatus"), "", ZEND_ACC_PROTECTED
);
2021 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
);
2022 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("responseCode"), 0, ZEND_ACC_PROTECTED
);
2023 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
);
2024 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("httpVersion"), ZEND_ACC_PROTECTED
);
2025 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
);
2026 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("headers"), ZEND_ACC_PROTECTED
);
2027 php_http_message_object_add_prophandler(ZEND_STRL("headers"), php_http_message_object_prophandler_get_headers
, php_http_message_object_prophandler_set_headers
);
2028 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("parentMessage"), ZEND_ACC_PROTECTED
);
2029 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
);
2031 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_NONE"), PHP_HTTP_NONE
);
2032 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_REQUEST"), PHP_HTTP_REQUEST
);
2033 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_RESPONSE"), PHP_HTTP_RESPONSE
);
2038 PHP_MSHUTDOWN_FUNCTION(http_message
)
2040 zend_hash_destroy(&php_http_message_object_prophandlers
);
2050 * vim600: noet sw=4 ts=4 fdm=marker
2051 * vim<600: noet sw=4 ts=4