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);
74 php_http_env_get_request_headers(&message
->hdrs
);
77 case PHP_HTTP_RESPONSE
:
78 message
= php_http_message_init(NULL
, 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 (php_http_params_parse(¶ms
, &popts
)) {
185 zend_hash_internal_pointer_reset(¶ms
);
187 if ((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
)) {
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
);
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 case PHP_HTTP_REQUEST
:
336 php_http_buffer_appendf(str
, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&msg
->http
, tmp
, PHP_HTTP_CRLF
));
340 case PHP_HTTP_RESPONSE
:
341 php_http_buffer_appendf(str
, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&msg
->http
, tmp
, PHP_HTTP_CRLF
));
349 php_http_message_update_headers(msg
);
350 php_http_header_to_string(str
, &msg
->hdrs
);
353 void php_http_message_to_callback(php_http_message_t
*msg
, php_http_pass_callback_t cb
, void *cb_arg
)
355 php_http_buffer_t str
;
357 php_http_buffer_init_ex(&str
, 0x1000, 0);
358 message_headers(msg
, &str
);
359 cb(cb_arg
, str
.data
, str
.used
);
360 php_http_buffer_dtor(&str
);
362 if (php_http_message_body_size(msg
->body
)) {
363 cb(cb_arg
, ZEND_STRL(PHP_HTTP_CRLF
));
364 php_http_message_body_to_callback(msg
->body
, cb
, cb_arg
, 0, 0);
368 void php_http_message_to_string(php_http_message_t
*msg
, char **string
, size_t *length
)
370 php_http_buffer_t str
;
373 php_http_buffer_init_ex(&str
, 0x1000, 0);
374 message_headers(msg
, &str
);
375 if (php_http_message_body_size(msg
->body
)) {
376 php_http_buffer_appends(&str
, PHP_HTTP_CRLF
);
377 php_http_message_body_to_callback(msg
->body
, (php_http_pass_callback_t
) php_http_buffer_append
, &str
, 0, 0);
380 data
= php_http_buffer_data(&str
, string
, length
);
385 php_http_buffer_dtor(&str
);
388 void php_http_message_serialize(php_http_message_t
*message
, char **string
, size_t *length
)
391 php_http_buffer_t str
;
392 php_http_message_t
*msg
;
394 php_http_buffer_init(&str
);
396 msg
= message
= php_http_message_reverse(message
);
398 php_http_message_to_callback(message
, (php_http_pass_callback_t
) php_http_buffer_append
, &str
);
399 php_http_buffer_appends(&str
, PHP_HTTP_CRLF
);
400 } while ((message
= message
->parent
));
401 php_http_message_reverse(msg
);
403 buf
= php_http_buffer_data(&str
, string
, length
);
408 php_http_buffer_dtor(&str
);
411 php_http_message_t
*php_http_message_reverse(php_http_message_t
*msg
)
413 size_t i
, c
= php_http_message_count(msg
);
416 php_http_message_t
*tmp
= msg
, **arr
;
418 arr
= ecalloc(c
, sizeof(**arr
));
419 for (i
= 0; i
< c
; ++i
) {
423 arr
[0]->parent
= NULL
;
424 for (i
= 0; i
< c
-1; ++i
) {
425 arr
[i
+1]->parent
= arr
[i
];
435 php_http_message_t
*php_http_message_zip(php_http_message_t
*dst
, php_http_message_t
*src
)
437 php_http_message_t
*tmp_dst
, *tmp_src
, *ret
= dst
;
440 tmp_dst
= dst
->parent
;
441 tmp_src
= src
->parent
;
444 src
->parent
= tmp_dst
;
453 php_http_message_t
*php_http_message_copy_ex(php_http_message_t
*from
, php_http_message_t
*to
, zend_bool parents
)
455 php_http_message_t
*temp
, *copy
= NULL
;
456 php_http_info_t info
;
459 info
.type
= from
->type
;
460 info
.http
= from
->http
;
462 copy
= temp
= php_http_message_init(to
, 0, php_http_message_body_copy(from
->body
, NULL
));
463 php_http_message_set_info(temp
, &info
);
464 array_copy(&from
->hdrs
, &temp
->hdrs
);
466 if (parents
) while (from
->parent
) {
467 info
.type
= from
->parent
->type
;
468 info
.http
= from
->parent
->http
;
470 temp
->parent
= php_http_message_init(NULL
, 0, php_http_message_body_copy(from
->parent
->body
, NULL
));
471 php_http_message_set_info(temp
->parent
, &info
);
472 array_copy(&from
->parent
->hdrs
, &temp
->parent
->hdrs
);
482 void php_http_message_dtor(php_http_message_t
*message
)
485 zend_hash_destroy(&message
->hdrs
);
486 php_http_message_body_free(&message
->body
);
488 switch (message
->type
) {
489 case PHP_HTTP_REQUEST
:
490 PTR_SET(message
->http
.info
.request
.method
, NULL
);
491 PTR_SET(message
->http
.info
.request
.url
, NULL
);
494 case PHP_HTTP_RESPONSE
:
495 PTR_SET(message
->http
.info
.response
.status
, NULL
);
504 void php_http_message_free(php_http_message_t
**message
)
507 if ((*message
)->parent
) {
508 php_http_message_free(&(*message
)->parent
);
510 php_http_message_dtor(*message
);
516 static zend_class_entry
*php_http_message_class_entry
;
517 zend_class_entry
*php_http_message_get_class_entry(void)
519 return php_http_message_class_entry
;
522 static zval
*php_http_message_object_read_prop(zval
*object
, zval
*member
, int type
, void **cache_slot
, zval
*rv
);
523 static void php_http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value
, void **cache_slot
);
525 static zend_object_handlers php_http_message_object_handlers
;
526 static HashTable php_http_message_object_prophandlers
;
528 static void php_http_message_object_prophandler_hash_dtor(zval
*pData
)
530 pefree(Z_PTR_P(pData
), 1);
533 typedef void (*php_http_message_object_prophandler_func_t
)(php_http_message_object_t
*o
, zval
*v
);
535 typedef struct php_http_message_object_prophandler
{
536 php_http_message_object_prophandler_func_t read
;
537 php_http_message_object_prophandler_func_t write
;
538 } php_http_message_object_prophandler_t
;
540 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
) {
541 php_http_message_object_prophandler_t h
= { read
, write
};
542 if (!zend_hash_str_add_mem(&php_http_message_object_prophandlers
, prop_str
, prop_len
, (void *) &h
, sizeof(h
))) {
547 static php_http_message_object_prophandler_t
*php_http_message_object_get_prophandler(zend_string
*name_str
) {
548 return zend_hash_str_find_ptr(&php_http_message_object_prophandlers
, name_str
->val
, name_str
->len
);
550 static void php_http_message_object_prophandler_get_type(php_http_message_object_t
*obj
, zval
*return_value
) {
551 RETVAL_LONG(obj
->message
->type
);
553 static void php_http_message_object_prophandler_set_type(php_http_message_object_t
*obj
, zval
*value
) {
554 php_http_message_set_type(obj
->message
, zval_get_long(value
));
556 static void php_http_message_object_prophandler_get_request_method(php_http_message_object_t
*obj
, zval
*return_value
) {
557 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
) && obj
->message
->http
.info
.request
.method
) {
558 RETVAL_STRING(obj
->message
->http
.info
.request
.method
);
563 static void php_http_message_object_prophandler_set_request_method(php_http_message_object_t
*obj
, zval
*value
) {
564 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
565 zend_string
*zs
= zval_get_string(value
);
566 PTR_SET(obj
->message
->http
.info
.request
.method
, estrndup(zs
->val
, zs
->len
));
567 zend_string_release(zs
);
570 static void php_http_message_object_prophandler_get_request_url(php_http_message_object_t
*obj
, zval
*return_value
) {
574 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)) {
575 RETVAL_STR(php_http_cs2zs(url_str
, url_len
));
580 static void php_http_message_object_prophandler_set_request_url(php_http_message_object_t
*obj
, zval
*value
) {
581 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
582 PTR_SET(obj
->message
->http
.info
.request
.url
, php_http_url_from_zval(value
, ~0));
585 static void php_http_message_object_prophandler_get_response_status(php_http_message_object_t
*obj
, zval
*return_value
) {
586 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
) && obj
->message
->http
.info
.response
.status
) {
587 RETVAL_STRING(obj
->message
->http
.info
.response
.status
);
592 static void php_http_message_object_prophandler_set_response_status(php_http_message_object_t
*obj
, zval
*value
) {
593 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
594 zend_string
*zs
= zval_get_string(value
);
595 PTR_SET(obj
->message
->http
.info
.response
.status
, estrndup(zs
->val
, zs
->len
));
596 zend_string_release(zs
);
599 static void php_http_message_object_prophandler_get_response_code(php_http_message_object_t
*obj
, zval
*return_value
) {
600 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
601 RETVAL_LONG(obj
->message
->http
.info
.response
.code
);
606 static void php_http_message_object_prophandler_set_response_code(php_http_message_object_t
*obj
, zval
*value
) {
607 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
608 obj
->message
->http
.info
.response
.code
= zval_get_long(value
);
609 PTR_SET(obj
->message
->http
.info
.response
.status
, estrdup(php_http_env_get_response_status_for_code(obj
->message
->http
.info
.response
.code
)));
612 static void php_http_message_object_prophandler_get_http_version(php_http_message_object_t
*obj
, zval
*return_value
) {
616 php_http_version_to_string(&obj
->message
->http
.version
, &version_str
, &version_len
, NULL
, NULL
);
617 RETVAL_STR(php_http_cs2zs(version_str
, version_len
));
619 static void php_http_message_object_prophandler_set_http_version(php_http_message_object_t
*obj
, zval
*value
) {
620 zend_string
*zs
= zval_get_string(value
);
621 php_http_version_parse(&obj
->message
->http
.version
, zs
->val
);
622 zend_string_release(zs
);
624 static void php_http_message_object_prophandler_get_headers(php_http_message_object_t
*obj
, zval
*return_value
) {
625 array_init(return_value
);
626 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 zval
*orig_value
= value
;
632 if (Z_TYPE_P(value
) != IS_ARRAY
&& Z_TYPE_P(value
) != IS_OBJECT
) {
633 convert_to_array_ex(value
);
635 headers
= HASH_OF(value
);
637 zend_hash_clean(&obj
->message
->hdrs
);
638 array_copy(headers
, &obj
->message
->hdrs
);
640 if (orig_value
!= value
) {
641 zval_ptr_dtor(value
);
644 static void php_http_message_object_prophandler_get_body(php_http_message_object_t
*obj
, zval
*return_value
) {
646 RETVAL_OBJECT(&obj
->body
->zo
, 1);
651 static void php_http_message_object_prophandler_set_body(php_http_message_object_t
*obj
, zval
*value
) {
652 php_http_message_object_set_body(obj
, value
);
654 static void php_http_message_object_prophandler_get_parent_message(php_http_message_object_t
*obj
, zval
*return_value
) {
655 if (obj
->message
->parent
) {
656 RETVAL_OBJECT(&obj
->parent
->zo
, 1);
661 static void php_http_message_object_prophandler_set_parent_message(php_http_message_object_t
*obj
, zval
*value
) {
662 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), php_http_message_class_entry
)) {
663 php_http_message_object_t
*parent_obj
= PHP_HTTP_OBJ(NULL
, value
);
665 if (obj
->message
->parent
) {
666 zend_object_release(&obj
->parent
->zo
);
669 obj
->parent
= parent_obj
;
670 obj
->message
->parent
= parent_obj
->message
;
674 #define PHP_HTTP_MESSAGE_OBJECT_INIT(obj) \
676 if (!obj->message) { \
677 obj->message = php_http_message_init(NULL, 0, NULL); \
682 void php_http_message_object_reverse(zval
*zmsg
, zval
*return_value
)
685 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, zmsg
);
687 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
690 i
= php_http_message_count(obj
->message
);
693 php_http_message_object_t
**objects
;
696 objects
= ecalloc(i
, sizeof(**objects
));
698 /* we are the first message */
702 for (i
= 1; obj
->parent
; ++i
) {
703 objects
[i
] = obj
= obj
->parent
;
706 /* reorder parents */
707 for (last
= --i
; i
; --i
) {
708 objects
[i
]->message
->parent
= objects
[i
-1]->message
;
709 objects
[i
]->parent
= objects
[i
-1];
712 objects
[0]->message
->parent
= NULL
;
713 objects
[0]->parent
= NULL
;
715 /* add ref, because we previously have not been a parent message */
717 /* no addref, because we've been a parent message previously */
718 RETVAL_OBJECT(&objects
[last
]->zo
, 0);
722 RETURN_ZVAL(zmsg
, 1, 0);
726 void php_http_message_object_prepend(zval
*this_ptr
, zval
*prepend
, zend_bool top
)
728 php_http_message_t
*save_parent_msg
= NULL
;
729 php_http_message_object_t
*save_parent_obj
= NULL
, *obj
= PHP_HTTP_OBJ(NULL
, this_ptr
);
730 php_http_message_object_t
*prepend_obj
= PHP_HTTP_OBJ(NULL
, prepend
);
733 save_parent_obj
= obj
->parent
;
734 save_parent_msg
= obj
->message
->parent
;
736 /* iterate to the most parent object */
737 while (obj
->parent
) {
743 obj
->parent
= prepend_obj
;
744 obj
->message
->parent
= prepend_obj
->message
;
750 prepend_obj
->parent
= save_parent_obj
;
751 prepend_obj
->message
->parent
= save_parent_msg
;
755 ZEND_RESULT_CODE
php_http_message_object_set_body(php_http_message_object_t
*msg_obj
, zval
*zbody
)
758 zend_string
*body_str
;
759 php_http_message_body_t
*body
;
760 php_http_message_body_object_t
*body_obj
;
762 switch (Z_TYPE_P(zbody
)) {
764 php_stream_from_zval_no_verify(s
, zbody
);
766 php_http_throw(unexpected_val
, "The stream is not a valid resource", NULL
);
772 body
= php_http_message_body_init(NULL
, s
);
773 if (!(body_obj
= php_http_message_body_object_new_ex(php_http_get_message_body_class_entry(), body
))) {
774 php_http_message_body_free(&body
);
780 if (instanceof_function(Z_OBJCE_P(zbody
), php_http_get_message_body_class_entry())) {
782 body_obj
= PHP_HTTP_OBJ(NULL
, zbody
);
788 body_str
= zval_get_string(zbody
);
789 s
= php_stream_temp_new();
790 php_stream_write(s
, body_str
->val
, body_str
->len
);
791 zend_string_release(body_str
);
796 if (!body_obj
->body
) {
797 body_obj
->body
= php_http_message_body_init(NULL
, NULL
);
800 zend_object_release(&msg_obj
->body
->zo
);
802 if (msg_obj
->message
) {
803 php_http_message_body_free(&msg_obj
->message
->body
);
804 msg_obj
->message
->body
= body_obj
->body
;
806 msg_obj
->message
= php_http_message_init(NULL
, 0, body_obj
->body
);
808 php_http_message_body_addref(body_obj
->body
);
809 msg_obj
->body
= body_obj
;
814 ZEND_RESULT_CODE
php_http_message_object_init_body_object(php_http_message_object_t
*obj
)
816 php_http_message_body_addref(obj
->message
->body
);
817 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
);
820 zend_object
*php_http_message_object_new(zend_class_entry
*ce
)
822 return &php_http_message_object_new_ex(ce
, NULL
)->zo
;
825 php_http_message_object_t
*php_http_message_object_new_ex(zend_class_entry
*ce
, php_http_message_t
*msg
)
827 php_http_message_object_t
*o
;
829 o
= ecalloc(1, sizeof(*o
) + zend_object_properties_size(ce
));
830 zend_object_std_init(&o
->zo
, ce
);
831 object_properties_init(&o
->zo
, ce
);
836 o
->parent
= php_http_message_object_new_ex(ce
, msg
->parent
);
838 o
->body
= php_http_message_body_object_new_ex(php_http_get_message_body_class_entry(), php_http_message_body_init(&msg
->body
, NULL
));
841 o
->zo
.handlers
= &php_http_message_object_handlers
;
846 zend_object
*php_http_message_object_clone(zval
*this_ptr
)
848 php_http_message_object_t
*new_obj
= NULL
;
849 php_http_message_object_t
*old_obj
= PHP_HTTP_OBJ(NULL
, this_ptr
);
851 new_obj
= php_http_message_object_new_ex(old_obj
->zo
.ce
, php_http_message_copy(old_obj
->message
, NULL
));
852 zend_objects_clone_members(&new_obj
->zo
, &old_obj
->zo
);
857 void php_http_message_object_free(zend_object
*object
)
859 php_http_message_object_t
*o
= PHP_HTTP_OBJ(object
, NULL
);
863 if (!Z_ISUNDEF(o
->iterator
)) {
864 zval_ptr_dtor(&o
->iterator
);
865 ZVAL_UNDEF(&o
->iterator
);
868 /* do NOT free recursivly */
869 php_http_message_dtor(o
->message
);
874 zend_object_release(&o
->parent
->zo
);
878 zend_object_release(&o
->body
->zo
);
881 zend_object_std_dtor(object
);
884 static zval
*php_http_message_object_read_prop(zval
*object
, zval
*member
, int type
, void **cache_slot
, zval
*tmp
)
887 zend_string
*member_name
= zval_get_string(member
);
888 php_http_message_object_prophandler_t
*handler
= php_http_message_object_get_prophandler(member_name
);
890 if (!handler
|| type
== BP_VAR_R
|| type
== BP_VAR_IS
) {
891 return_value
= zend_get_std_object_handlers()->read_property(object
, member
, type
, cache_slot
, tmp
);
894 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, object
);
896 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
897 handler
->read(obj
, tmp
);
899 zval_ptr_dtor(return_value
);
900 ZVAL_COPY_VALUE(return_value
, tmp
);
902 zend_string_release(member_name
);
905 php_property_proxy_t
*proxy
;
906 php_property_proxy_object_t
*proxy_obj
;
908 proxy
= php_property_proxy_init(object
, member_name
);
909 proxy_obj
= php_property_proxy_object_new_ex(NULL
, proxy
);
911 ZVAL_OBJ(tmp
, &proxy_obj
->zo
);
912 zend_string_release(member_name
);
917 static void php_http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value
, void **cache_slot
)
919 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, object
);
920 php_http_message_object_prophandler_t
*handler
;
921 zend_string
*member_name
= zval_get_string(member
);
923 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
925 if ((handler
= php_http_message_object_get_prophandler(member_name
))) {
926 handler
->write(obj
, value
);
928 zend_get_std_object_handlers()->write_property(object
, member
, value
, cache_slot
);
931 zend_string_release(member_name
);
934 static HashTable
*php_http_message_object_get_debug_info(zval
*object
, int *is_temp
)
937 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, object
);
938 HashTable
*props
= zend_get_std_object_handlers()->get_properties(object
);
939 char *ver_str
, *url_str
= NULL
;
940 size_t ver_len
, url_len
= 0;
942 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
945 #define UPDATE_PROP(name_str, action_with_tmp) \
947 zend_property_info *pi; \
948 if ((pi = zend_hash_str_find_ptr(&obj->zo.ce->properties_info, name_str, lenof(name_str)))) { \
950 zend_hash_update_ind(props, pi->name, &tmp); \
954 UPDATE_PROP("type", ZVAL_LONG(&tmp
, obj
->message
->type
));
956 ver_len
= spprintf(&ver_str
, 0, "%u.%u", obj
->message
->http
.version
.major
, obj
->message
->http
.version
.minor
);
957 UPDATE_PROP("httpVersion", ZVAL_STR(&tmp
, php_http_cs2zs(ver_str
, ver_len
)));
959 switch (obj
->message
->type
) {
960 case PHP_HTTP_REQUEST
:
961 UPDATE_PROP("responseCode", ZVAL_LONG(&tmp
, 0));
962 UPDATE_PROP("responseStatus", ZVAL_EMPTY_STRING(&tmp
));
963 UPDATE_PROP("requestMethod", ZVAL_STRING(&tmp
, STR_PTR(obj
->message
->http
.info
.request
.method
)));
964 if (obj
->message
->http
.info
.request
.url
) {
965 php_http_url_to_string(obj
->message
->http
.info
.request
.url
, &url_str
, &url_len
, 0);
966 UPDATE_PROP("requestUrl", ZVAL_STR(&tmp
, php_http_cs2zs(url_str
, url_len
)));
968 UPDATE_PROP("requestUrl", ZVAL_EMPTY_STRING(&tmp
));
973 case PHP_HTTP_RESPONSE
:
974 UPDATE_PROP("responseCode", ZVAL_LONG(&tmp
, obj
->message
->http
.info
.response
.code
));
975 UPDATE_PROP("responseStatus", ZVAL_STRING(&tmp
, STR_PTR(obj
->message
->http
.info
.response
.status
)));
976 UPDATE_PROP("requestMethod", ZVAL_EMPTY_STRING(&tmp
));
977 UPDATE_PROP("requestUrl", ZVAL_EMPTY_STRING(&tmp
));
982 UPDATE_PROP("responseCode", ZVAL_LONG(&tmp
, 0));
983 UPDATE_PROP("responseStatus", ZVAL_EMPTY_STRING(&tmp
));
984 UPDATE_PROP("requestMethod", ZVAL_EMPTY_STRING(&tmp
));
985 UPDATE_PROP("requestUrl", ZVAL_EMPTY_STRING(&tmp
));
989 UPDATE_PROP("headers",
991 array_copy(&obj
->message
->hdrs
, Z_ARRVAL(tmp
));
996 ZVAL_OBJECT(&tmp
, &obj
->body
->zo
, 1);
1002 UPDATE_PROP("parentMessage",
1003 if (obj
->message
->parent
) {
1004 ZVAL_OBJECT(&tmp
, &obj
->parent
->zo
, 1);
1013 static HashTable
*php_http_message_object_get_gc(zval
*object
, zval
**table
, int *n
)
1015 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, object
);
1016 HashTable
*props
= Z_OBJPROP_P(object
);
1017 uint32_t count
= 2 + zend_hash_num_elements(props
);
1021 *table
= obj
->gc
= erealloc(obj
->gc
, count
* sizeof(zval
));
1024 ZVAL_OBJ(&obj
->gc
[(*n
)++], &obj
->body
->zo
);
1027 ZVAL_OBJ(&obj
->gc
[(*n
)++], &obj
->parent
->zo
);
1030 ZEND_HASH_FOREACH_VAL(props
, val
)
1032 ZVAL_COPY_VALUE(&obj
->gc
[(*n
)++], val
);
1034 ZEND_HASH_FOREACH_END();
1039 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage___construct
, 0, 0, 0)
1040 ZEND_ARG_INFO(0, message
)
1041 ZEND_ARG_INFO(0, greedy
)
1042 ZEND_END_ARG_INFO();
1043 static PHP_METHOD(HttpMessage
, __construct
)
1045 zend_bool greedy
= 1;
1046 zval
*zmessage
= NULL
;
1047 php_http_message_t
*msg
= NULL
;
1048 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1049 zend_error_handling zeh
;
1051 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|z!b", &zmessage
, &greedy
), invalid_arg
, return);
1053 zend_replace_error_handling(EH_THROW
, php_http_get_exception_bad_message_class_entry(), &zeh
);
1054 if (zmessage
&& Z_TYPE_P(zmessage
) == IS_RESOURCE
) {
1056 php_http_message_parser_t p
;
1057 zend_error_handling zeh
;
1059 zend_replace_error_handling(EH_THROW
, php_http_get_exception_unexpected_val_class_entry(), &zeh
);
1060 php_stream_from_zval(s
, zmessage
);
1061 zend_restore_error_handling(&zeh
);
1063 if (s
&& php_http_message_parser_init(&p
)) {
1064 unsigned flags
= (greedy
? PHP_HTTP_MESSAGE_PARSER_GREEDY
: 0);
1065 php_http_buffer_t buf
;
1067 php_http_buffer_init_ex(&buf
, 0x1000, PHP_HTTP_BUFFER_INIT_PREALLOC
);
1068 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE
== php_http_message_parser_parse_stream(&p
, &buf
, s
, flags
, &msg
)) {
1069 if (!EG(exception
)) {
1070 php_http_throw(bad_message
, "Could not parse message from stream", NULL
);
1073 php_http_buffer_dtor(&buf
);
1074 php_http_message_parser_dtor(&p
);
1077 if (!msg
&& !EG(exception
)) {
1078 php_http_throw(bad_message
, "Empty message received from stream", NULL
);
1080 } else if (zmessage
) {
1081 zend_string
*zs_msg
= zval_get_string(zmessage
);
1083 msg
= php_http_message_parse(NULL
, zs_msg
->val
, zs_msg
->len
, greedy
);
1085 if (!msg
&& !EG(exception
)) {
1086 php_http_throw(bad_message
, "Could not parse message: %.*s", MIN(25, zs_msg
->len
), zs_msg
->val
);
1088 zend_string_release(zs_msg
);
1092 php_http_message_dtor(obj
->message
);
1094 if (obj
->message
->parent
) {
1095 obj
->parent
= php_http_message_object_new_ex(obj
->zo
.ce
, obj
->message
->parent
);
1098 zend_restore_error_handling(&zeh
);
1099 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1102 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getBody
, 0, 0, 0)
1103 ZEND_END_ARG_INFO();
1104 static PHP_METHOD(HttpMessage
, getBody
)
1106 php_http_message_object_t
*obj
;
1108 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
1110 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1111 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1114 php_http_message_object_init_body_object(obj
);
1118 RETVAL_OBJECT(&obj
->body
->zo
, 1);
1122 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setBody
, 0, 0, 1)
1123 ZEND_ARG_OBJ_INFO(0, body
, http
\\Message
\\Body
, 0)
1124 ZEND_END_ARG_INFO();
1125 static PHP_METHOD(HttpMessage
, setBody
)
1129 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zbody
, php_http_get_message_body_class_entry())) {
1130 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1132 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1133 php_http_message_object_prophandler_set_body(obj
, zbody
);
1135 RETVAL_ZVAL(getThis(), 1, 0);
1138 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addBody
, 0, 0, 1)
1139 ZEND_ARG_OBJ_INFO(0, body
, http
\\Message
\\Body
, 0)
1140 ZEND_END_ARG_INFO();
1141 static PHP_METHOD(HttpMessage
, addBody
)
1145 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O", &new_body
, php_http_get_message_body_class_entry())) {
1146 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1147 php_http_message_body_object_t
*new_obj
= PHP_HTTP_OBJ(NULL
, new_body
);
1149 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1150 php_http_message_body_to_callback(new_obj
->body
, (php_http_pass_callback_t
) php_http_message_body_append
, obj
->message
->body
, 0, 0);
1152 RETVAL_ZVAL(getThis(), 1, 0);
1155 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHeader
, 0, 0, 1)
1156 ZEND_ARG_INFO(0, header
)
1157 ZEND_ARG_INFO(0, into_class
)
1158 ZEND_END_ARG_INFO();
1159 static PHP_METHOD(HttpMessage
, getHeader
)
1163 zend_class_entry
*header_ce
= NULL
;
1165 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s|C!", &header_str
, &header_len
, &header_ce
)) {
1166 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1169 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1171 if ((header
= php_http_message_header(obj
->message
, header_str
, header_len
))) {
1173 RETURN_ZVAL(header
, 1, 0);
1174 } else if (instanceof_function(header_ce
, php_http_header_get_class_entry())) {
1175 php_http_object_method_t cb
;
1178 ZVAL_STRINGL(&argv
[0], header_str
, header_len
);
1179 ZVAL_COPY(&argv
[1], header
);
1181 object_init_ex(return_value
, header_ce
);
1182 php_http_object_method_init(&cb
, return_value
, ZEND_STRL("__construct"));
1183 php_http_object_method_call(&cb
, return_value
, NULL
, 2, argv
);
1184 php_http_object_method_dtor(&cb
);
1186 zval_ptr_dtor(&argv
[0]);
1187 zval_ptr_dtor(&argv
[1]);
1191 php_error_docref(NULL
, E_WARNING
, "Class '%s' is not as descendant of http\\Header", header_ce
->name
->val
);
1198 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHeaders
, 0, 0, 0)
1199 ZEND_END_ARG_INFO();
1200 static PHP_METHOD(HttpMessage
, getHeaders
)
1202 if (SUCCESS
== zend_parse_parameters_none()) {
1203 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1205 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1207 array_init(return_value
);
1208 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(return_value
));
1212 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHeader
, 0, 0, 1)
1213 ZEND_ARG_INFO(0, header
)
1214 ZEND_ARG_INFO(0, value
)
1215 ZEND_END_ARG_INFO();
1216 static PHP_METHOD(HttpMessage
, setHeader
)
1218 zval
*zvalue
= NULL
;
1222 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s|z!", &name_str
, &name_len
, &zvalue
)) {
1223 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1224 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1226 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1229 zend_symtable_str_del(&obj
->message
->hdrs
, name
, name_len
);
1231 Z_TRY_ADDREF_P(zvalue
);
1232 zend_symtable_str_update(&obj
->message
->hdrs
, name
, name_len
, zvalue
);
1236 RETVAL_ZVAL(getThis(), 1, 0);
1239 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHeaders
, 0, 0, 1)
1240 ZEND_ARG_ARRAY_INFO(0, headers
, 1)
1241 ZEND_END_ARG_INFO();
1242 static PHP_METHOD(HttpMessage
, setHeaders
)
1244 zval
*new_headers
= NULL
;
1246 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "a/!", &new_headers
)) {
1247 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1249 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1251 zend_hash_clean(&obj
->message
->hdrs
);
1253 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, 0, ARRAY_JOIN_PRETTIFY
|ARRAY_JOIN_STRONLY
);
1256 RETVAL_ZVAL(getThis(), 1, 0);
1259 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeader
, 0, 0, 2)
1260 ZEND_ARG_INFO(0, header
)
1261 ZEND_ARG_INFO(0, value
)
1262 ZEND_END_ARG_INFO();
1263 static PHP_METHOD(HttpMessage
, addHeader
)
1269 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "sz", &name_str
, &name_len
, &zvalue
)) {
1270 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1271 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1272 zend_string
*hstr
, *vstr
= php_http_header_value_to_string(zvalue
);
1275 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1277 if ((name_len
!= lenof("Set-Cookie") && strcmp(name
, "Set-Cookie"))
1278 && (hstr
= php_http_message_header_string(obj
->message
, name
, name_len
))) {
1280 size_t hdr_len
= spprintf(&hdr_str
, 0, "%s, %s", hstr
->val
, vstr
->val
);
1282 ZVAL_STR(&tmp
, php_http_cs2zs(hdr_str
, hdr_len
));
1283 zend_symtable_str_update(&obj
->message
->hdrs
, name
, name_len
, &tmp
);
1284 zend_string_release(hstr
);
1285 zend_string_release(vstr
);
1286 } else if ((header
= php_http_message_header(obj
->message
, name
, name_len
))) {
1287 convert_to_array(header
);
1288 ZVAL_STR(&tmp
, vstr
);
1289 zend_hash_next_index_insert(Z_ARRVAL_P(header
), &tmp
);
1291 ZVAL_STR(&tmp
, vstr
);
1292 zend_symtable_str_update(&obj
->message
->hdrs
, name
, name_len
, &tmp
);
1296 RETVAL_ZVAL(getThis(), 1, 0);
1299 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeaders
, 0, 0, 1)
1300 ZEND_ARG_ARRAY_INFO(0, headers
, 0)
1301 ZEND_ARG_INFO(0, append
)
1302 ZEND_END_ARG_INFO();
1303 static PHP_METHOD(HttpMessage
, addHeaders
)
1306 zend_bool append
= 0;
1308 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "a|b", &new_headers
, &append
)) {
1309 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1311 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1313 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, append
, ARRAY_JOIN_STRONLY
|ARRAY_JOIN_PRETTIFY
);
1315 RETVAL_ZVAL(getThis(), 1, 0);
1318 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getType
, 0, 0, 0)
1319 ZEND_END_ARG_INFO();
1320 static PHP_METHOD(HttpMessage
, getType
)
1322 if (SUCCESS
== zend_parse_parameters_none()) {
1323 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1325 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1327 RETURN_LONG(obj
->message
->type
);
1331 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setType
, 0, 0, 1)
1332 ZEND_ARG_INFO(0, type
)
1333 ZEND_END_ARG_INFO();
1334 static PHP_METHOD(HttpMessage
, setType
)
1338 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "l", &type
)) {
1339 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1341 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1343 php_http_message_set_type(obj
->message
, type
);
1345 RETVAL_ZVAL(getThis(), 1, 0);
1348 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getInfo
, 0, 0, 0)
1349 ZEND_END_ARG_INFO();
1350 static PHP_METHOD(HttpMessage
, getInfo
)
1352 if (SUCCESS
== zend_parse_parameters_none()) {
1353 char *str
, *tmp
= NULL
;
1355 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1357 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1359 switch (obj
->message
->type
) {
1360 case PHP_HTTP_REQUEST
:
1361 len
= spprintf(&str
, 0, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&obj
->message
->http
, tmp
, ""));
1364 case PHP_HTTP_RESPONSE
:
1365 len
= spprintf(&str
, 0, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&obj
->message
->http
, tmp
, ""));
1373 RETVAL_STR(php_http_cs2zs(str
, len
));
1377 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setInfo
, 0, 0, 1)
1378 ZEND_ARG_INFO(0, http_info
)
1379 ZEND_END_ARG_INFO();
1380 static PHP_METHOD(HttpMessage
, setInfo
)
1384 php_http_message_object_t
*obj
;
1385 php_http_info_t inf
;
1387 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
), invalid_arg
, return);
1389 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1390 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1392 if (!php_http_info_parse(&inf
, str
)) {
1393 php_http_throw(bad_header
, "Could not parse message info '%s'", str
);
1397 php_http_message_set_info(obj
->message
, &inf
);
1398 php_http_info_dtor(&inf
);
1400 RETVAL_ZVAL(getThis(), 1, 0);
1403 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHttpVersion
, 0, 0, 0)
1404 ZEND_END_ARG_INFO();
1405 static PHP_METHOD(HttpMessage
, getHttpVersion
)
1407 if (SUCCESS
== zend_parse_parameters_none()) {
1410 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1412 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1414 php_http_version_to_string(&obj
->message
->http
.version
, &str
, &len
, NULL
, NULL
);
1415 RETURN_STR(php_http_cs2zs(str
, len
));
1419 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHttpVersion
, 0, 0, 1)
1420 ZEND_ARG_INFO(0, http_version
)
1421 ZEND_END_ARG_INFO();
1422 static PHP_METHOD(HttpMessage
, setHttpVersion
)
1426 php_http_version_t version
;
1427 php_http_message_object_t
*obj
;
1429 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &v_str
, &v_len
), invalid_arg
, return);
1431 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1432 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1434 php_http_expect(php_http_version_parse(&version
, v_str
), unexpected_val
, return);
1436 obj
->message
->http
.version
= version
;
1438 RETVAL_ZVAL(getThis(), 1, 0);
1441 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getResponseCode
, 0, 0, 0)
1442 ZEND_END_ARG_INFO();
1443 static PHP_METHOD(HttpMessage
, getResponseCode
)
1445 if (SUCCESS
== zend_parse_parameters_none()) {
1446 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1448 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1450 if (obj
->message
->type
!= PHP_HTTP_RESPONSE
) {
1451 php_error_docref(NULL
, E_WARNING
, "http\\Message is not if type response");
1455 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
1459 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setResponseCode
, 0, 0, 1)
1460 ZEND_ARG_INFO(0, response_code
)
1461 ZEND_ARG_INFO(0, strict
)
1462 ZEND_END_ARG_INFO();
1463 static PHP_METHOD(HttpMessage
, setResponseCode
)
1466 zend_bool strict
= 1;
1467 php_http_message_object_t
*obj
;
1469 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &code
, &strict
), invalid_arg
, return);
1471 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1472 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1474 if (obj
->message
->type
!= PHP_HTTP_RESPONSE
) {
1475 php_http_throw(bad_method_call
, "http\\Message is not of type response", NULL
);
1479 if (strict
&& (code
< 100 || code
> 599)) {
1480 php_http_throw(invalid_arg
, "Invalid response code (100-599): %ld", code
);
1484 obj
->message
->http
.info
.response
.code
= code
;
1485 PTR_SET(obj
->message
->http
.info
.response
.status
, estrdup(php_http_env_get_response_status_for_code(code
)));
1487 RETVAL_ZVAL(getThis(), 1, 0);
1490 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getResponseStatus
, 0, 0, 0)
1491 ZEND_END_ARG_INFO();
1492 static PHP_METHOD(HttpMessage
, getResponseStatus
)
1494 if (SUCCESS
== zend_parse_parameters_none()) {
1495 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1497 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1499 if (obj
->message
->type
!= PHP_HTTP_RESPONSE
) {
1500 php_error_docref(NULL
, E_WARNING
, "http\\Message is not of type response");
1503 if (obj
->message
->http
.info
.response
.status
) {
1504 RETURN_STRING(obj
->message
->http
.info
.response
.status
);
1506 RETURN_EMPTY_STRING();
1511 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setResponseStatus
, 0, 0, 1)
1512 ZEND_ARG_INFO(0, response_status
)
1513 ZEND_END_ARG_INFO();
1514 static PHP_METHOD(HttpMessage
, setResponseStatus
)
1518 php_http_message_object_t
*obj
;
1520 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &status
, &status_len
), invalid_arg
, return);
1522 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1524 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1526 if (obj
->message
->type
!= PHP_HTTP_RESPONSE
) {
1527 php_http_throw(bad_method_call
, "http\\Message is not of type response", NULL
);
1530 PTR_SET(obj
->message
->http
.info
.response
.status
, estrndup(status
, status_len
));
1531 RETVAL_ZVAL(getThis(), 1, 0);
1534 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getRequestMethod
, 0, 0, 0)
1535 ZEND_END_ARG_INFO();
1536 static PHP_METHOD(HttpMessage
, getRequestMethod
)
1538 if (SUCCESS
== zend_parse_parameters_none()) {
1539 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1541 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1543 if (obj
->message
->type
!= PHP_HTTP_REQUEST
) {
1544 php_error_docref(NULL
, E_WARNING
, "http\\Message is not of type request");
1548 if (obj
->message
->http
.info
.request
.method
) {
1549 RETURN_STRING(obj
->message
->http
.info
.request
.method
);
1551 RETURN_EMPTY_STRING();
1556 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setRequestMethod
, 0, 0, 1)
1557 ZEND_ARG_INFO(0, request_method
)
1558 ZEND_END_ARG_INFO();
1559 static PHP_METHOD(HttpMessage
, setRequestMethod
)
1563 php_http_message_object_t
*obj
;
1565 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &method
, &method_len
), invalid_arg
, return);
1567 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1569 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1571 if (obj
->message
->type
!= PHP_HTTP_REQUEST
) {
1572 php_http_throw(bad_method_call
, "http\\Message is not of type request", NULL
);
1576 if (method_len
< 1) {
1577 php_http_throw(invalid_arg
, "Cannot set http\\Message's request method to an empty string", NULL
);
1581 PTR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
1582 RETVAL_ZVAL(getThis(), 1, 0);
1585 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getRequestUrl
, 0, 0, 0)
1586 ZEND_END_ARG_INFO();
1587 static PHP_METHOD(HttpMessage
, getRequestUrl
)
1589 if (SUCCESS
== zend_parse_parameters_none()) {
1590 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1592 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1594 if (obj
->message
->type
!= PHP_HTTP_REQUEST
) {
1595 php_error_docref(NULL
, E_WARNING
, "http\\Message is not of type request");
1599 if (obj
->message
->http
.info
.request
.url
) {
1603 php_http_url_to_string(obj
->message
->http
.info
.request
.url
, &url_str
, &url_len
, 0);
1604 RETURN_STR(php_http_cs2zs(url_str
, url_len
));
1606 RETURN_EMPTY_STRING();
1611 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setRequestUrl
, 0, 0, 1)
1612 ZEND_ARG_INFO(0, url
)
1613 ZEND_END_ARG_INFO();
1614 static PHP_METHOD(HttpMessage
, setRequestUrl
)
1617 php_http_url_t
*url
;
1618 php_http_message_object_t
*obj
;
1619 zend_error_handling zeh
;
1621 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zurl
), invalid_arg
, return);
1623 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1625 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1627 if (obj
->message
->type
!= PHP_HTTP_REQUEST
) {
1628 php_http_throw(bad_method_call
, "http\\Message is not of type request", NULL
);
1632 zend_replace_error_handling(EH_THROW
, php_http_get_exception_bad_url_class_entry(), &zeh
);
1633 url
= php_http_url_from_zval(zurl
, ~0);
1634 zend_restore_error_handling(&zeh
);
1636 if (url
&& php_http_url_is_empty(url
)) {
1637 php_http_url_free(&url
);
1638 php_http_throw(invalid_arg
, "Cannot set http\\Message's request url to an empty string", NULL
);
1640 PTR_SET(obj
->message
->http
.info
.request
.url
, url
);
1643 RETVAL_ZVAL(getThis(), 1, 0);
1646 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getParentMessage
, 0, 0, 0)
1647 ZEND_END_ARG_INFO();
1648 static PHP_METHOD(HttpMessage
, getParentMessage
)
1650 php_http_message_object_t
*obj
;
1652 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
1654 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1655 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1657 if (!obj
->message
->parent
) {
1658 php_http_throw(unexpected_val
, "http\\Message has not parent message", NULL
);
1662 RETVAL_OBJECT(&obj
->parent
->zo
, 1);
1665 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage___toString
, 0, 0, 0)
1666 ZEND_END_ARG_INFO();
1667 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toString
, 0, 0, 0)
1668 ZEND_ARG_INFO(0, include_parent
)
1669 ZEND_END_ARG_INFO();
1670 static PHP_METHOD(HttpMessage
, toString
)
1672 zend_bool include_parent
= 0;
1674 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &include_parent
)) {
1675 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1679 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1681 if (include_parent
) {
1682 php_http_message_serialize(obj
->message
, &string
, &length
);
1684 php_http_message_to_string(obj
->message
, &string
, &length
);
1687 RETURN_STR(php_http_cs2zs(string
, length
));
1690 RETURN_EMPTY_STRING();
1693 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toStream
, 0, 0, 1)
1694 ZEND_ARG_INFO(0, stream
)
1695 ZEND_END_ARG_INFO();
1696 static PHP_METHOD(HttpMessage
, toStream
)
1700 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zstream
)) {
1701 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1704 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1706 php_stream_from_zval(s
, zstream
);
1707 php_http_message_to_callback(obj
->message
, (php_http_pass_callback_t
) _php_stream_write
, s
);
1711 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toCallback
, 0, 0, 1)
1712 ZEND_ARG_INFO(0, callback
)
1713 ZEND_END_ARG_INFO();
1714 static PHP_METHOD(HttpMessage
, toCallback
)
1716 php_http_pass_fcall_arg_t fcd
;
1718 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "f", &fcd
.fci
, &fcd
.fcc
)) {
1719 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1721 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1723 ZVAL_COPY(&fcd
.fcz
, getThis());
1724 php_http_message_to_callback(obj
->message
, php_http_pass_fcall_callback
, &fcd
);
1725 zend_fcall_info_args_clear(&fcd
.fci
, 1);
1726 zval_ptr_dtor(&fcd
.fcz
);
1728 RETURN_ZVAL(getThis(), 1, 0);
1732 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_serialize
, 0, 0, 0)
1733 ZEND_END_ARG_INFO();
1734 static PHP_METHOD(HttpMessage
, serialize
)
1736 if (SUCCESS
== zend_parse_parameters_none()) {
1737 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1741 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1743 php_http_message_serialize(obj
->message
, &string
, &length
);
1744 RETURN_STR(php_http_cs2zs(string
, length
));
1746 RETURN_EMPTY_STRING();
1749 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_unserialize
, 0, 0, 1)
1750 ZEND_ARG_INFO(0, serialized
)
1751 ZEND_END_ARG_INFO();
1752 static PHP_METHOD(HttpMessage
, unserialize
)
1757 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &serialized
, &length
)) {
1758 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1759 php_http_message_t
*msg
;
1762 /* do not free recursively */
1763 php_http_message_dtor(obj
->message
);
1764 efree(obj
->message
);
1766 if ((msg
= php_http_message_parse(NULL
, serialized
, length
, 1))) {
1769 obj
->message
= php_http_message_init(NULL
, 0, NULL
);
1770 php_error_docref(NULL
, E_ERROR
, "Could not unserialize http\\Message");
1775 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_detach
, 0, 0, 0)
1776 ZEND_END_ARG_INFO();
1777 static PHP_METHOD(HttpMessage
, detach
)
1779 php_http_message_object_t
*obj
, *new_obj
;
1780 php_http_message_t
*msg_cpy
;
1782 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
1784 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1785 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1787 msg_cpy
= php_http_message_copy_ex(obj
->message
, NULL
, 0);
1788 new_obj
= php_http_message_object_new_ex(obj
->zo
.ce
, msg_cpy
);
1790 RETVAL_OBJ(&new_obj
->zo
);
1793 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_prepend
, 0, 0, 1)
1794 ZEND_ARG_OBJ_INFO(0, message
, http
\\Message
, 0)
1795 ZEND_ARG_INFO(0, top
)
1796 ZEND_END_ARG_INFO();
1797 static PHP_METHOD(HttpMessage
, prepend
)
1801 php_http_message_t
*msg
[2];
1802 php_http_message_object_t
*obj
, *prepend_obj
;
1804 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &prepend
, php_http_message_class_entry
, &top
), invalid_arg
, return);
1806 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1807 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1808 prepend_obj
= PHP_HTTP_OBJ(NULL
, prepend
);
1809 PHP_HTTP_MESSAGE_OBJECT_INIT(prepend_obj
);
1812 for (msg
[0] = obj
->message
; msg
[0]; msg
[0] = msg
[0]->parent
) {
1813 for (msg
[1] = prepend_obj
->message
; msg
[1]; msg
[1] = msg
[1]->parent
) {
1814 if (msg
[0] == msg
[1]) {
1815 php_http_throw(unexpected_val
, "Cannot prepend a message located within the same message chain", NULL
);
1821 php_http_message_object_prepend(getThis(), prepend
, top
);
1822 RETURN_ZVAL(getThis(), 1, 0);
1825 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_reverse
, 0, 0, 0)
1826 ZEND_END_ARG_INFO();
1827 static PHP_METHOD(HttpMessage
, reverse
)
1829 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
1831 php_http_message_object_reverse(getThis(), return_value
);
1834 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_isMultipart
, 0, 0, 0)
1835 ZEND_ARG_INFO(1, boundary
)
1836 ZEND_END_ARG_INFO();
1837 static PHP_METHOD(HttpMessage
, isMultipart
)
1839 zval
*zboundary
= NULL
;
1841 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|z!", &zboundary
)) {
1842 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1843 char *boundary
= NULL
;
1845 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1847 if (php_http_message_is_multipart(obj
->message
, zboundary
? &boundary
: NULL
)) {
1853 if (zboundary
&& boundary
) {
1854 ZVAL_DEREF(zboundary
);
1855 zval_dtor(zboundary
);
1856 ZVAL_STR(zboundary
, php_http_cs2zs(boundary
, strlen(boundary
)));
1861 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_splitMultipartBody
, 0, 0, 0)
1862 ZEND_END_ARG_INFO();
1863 static PHP_METHOD(HttpMessage
, splitMultipartBody
)
1865 php_http_message_object_t
*obj
;
1866 php_http_message_t
*msg
;
1867 char *boundary
= NULL
;
1869 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
1871 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1872 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1874 if (!php_http_message_is_multipart(obj
->message
, &boundary
)) {
1875 php_http_throw(bad_method_call
, "http\\Message is not a multipart message", NULL
);
1879 php_http_expect(msg
= php_http_message_body_split(obj
->message
->body
, boundary
), bad_message
, return);
1883 RETURN_OBJ(&php_http_message_object_new_ex(obj
->zo
.ce
, msg
)->zo
);
1886 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_count
, 0, 0, 0)
1887 ZEND_END_ARG_INFO();
1888 static PHP_METHOD(HttpMessage
, count
)
1890 zend_long count_mode
= -1;
1892 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &count_mode
)) {
1893 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1895 PHP_HTTP_MESSAGE_OBJECT_INIT(obj
);
1897 RETURN_LONG(php_http_message_count(obj
->message
));
1901 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_rewind
, 0, 0, 0)
1902 ZEND_END_ARG_INFO();
1903 static PHP_METHOD(HttpMessage
, rewind
)
1905 if (SUCCESS
== zend_parse_parameters_none()) {
1906 zval
*zobj
= getThis();
1907 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1909 if (!Z_ISUNDEF(obj
->iterator
)) {
1910 zval_ptr_dtor(&obj
->iterator
);
1912 ZVAL_COPY(&obj
->iterator
, zobj
);
1916 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_valid
, 0, 0, 0)
1917 ZEND_END_ARG_INFO();
1918 static PHP_METHOD(HttpMessage
, valid
)
1920 if (SUCCESS
== zend_parse_parameters_none()) {
1921 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1923 RETURN_BOOL(!Z_ISUNDEF(obj
->iterator
));
1927 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_next
, 0, 0, 0)
1928 ZEND_END_ARG_INFO();
1929 static PHP_METHOD(HttpMessage
, next
)
1931 if (SUCCESS
== zend_parse_parameters_none()) {
1932 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1934 if (!Z_ISUNDEF(obj
->iterator
)) {
1935 php_http_message_object_t
*itr
= PHP_HTTP_OBJ(NULL
, &obj
->iterator
);
1940 ZVAL_OBJECT(&tmp
, &itr
->parent
->zo
, 1);
1941 zval_ptr_dtor(&obj
->iterator
);
1942 obj
->iterator
= tmp
;
1944 zval_ptr_dtor(&obj
->iterator
);
1945 ZVAL_UNDEF(&obj
->iterator
);
1951 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_key
, 0, 0, 0)
1952 ZEND_END_ARG_INFO();
1953 static PHP_METHOD(HttpMessage
, key
)
1955 if (SUCCESS
== zend_parse_parameters_none()) {
1956 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1958 RETURN_LONG(Z_ISUNDEF(obj
->iterator
) ? 0 : Z_OBJ_HANDLE(obj
->iterator
));
1962 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_current
, 0, 0, 0)
1963 ZEND_END_ARG_INFO();
1964 static PHP_METHOD(HttpMessage
, current
)
1966 if (SUCCESS
== zend_parse_parameters_none()) {
1967 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1969 if (!Z_ISUNDEF(obj
->iterator
)) {
1970 RETURN_ZVAL(&obj
->iterator
, 1, 0);
1975 static zend_function_entry php_http_message_methods
[] = {
1976 PHP_ME(HttpMessage
, __construct
, ai_HttpMessage___construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
1977 PHP_ME(HttpMessage
, getBody
, ai_HttpMessage_getBody
, ZEND_ACC_PUBLIC
)
1978 PHP_ME(HttpMessage
, setBody
, ai_HttpMessage_setBody
, ZEND_ACC_PUBLIC
)
1979 PHP_ME(HttpMessage
, addBody
, ai_HttpMessage_addBody
, ZEND_ACC_PUBLIC
)
1980 PHP_ME(HttpMessage
, getHeader
, ai_HttpMessage_getHeader
, ZEND_ACC_PUBLIC
)
1981 PHP_ME(HttpMessage
, setHeader
, ai_HttpMessage_setHeader
, ZEND_ACC_PUBLIC
)
1982 PHP_ME(HttpMessage
, addHeader
, ai_HttpMessage_addHeader
, ZEND_ACC_PUBLIC
)
1983 PHP_ME(HttpMessage
, getHeaders
, ai_HttpMessage_getHeaders
, ZEND_ACC_PUBLIC
)
1984 PHP_ME(HttpMessage
, setHeaders
, ai_HttpMessage_setHeaders
, ZEND_ACC_PUBLIC
)
1985 PHP_ME(HttpMessage
, addHeaders
, ai_HttpMessage_addHeaders
, ZEND_ACC_PUBLIC
)
1986 PHP_ME(HttpMessage
, getType
, ai_HttpMessage_getType
, ZEND_ACC_PUBLIC
)
1987 PHP_ME(HttpMessage
, setType
, ai_HttpMessage_setType
, ZEND_ACC_PUBLIC
)
1988 PHP_ME(HttpMessage
, getInfo
, ai_HttpMessage_getInfo
, ZEND_ACC_PUBLIC
)
1989 PHP_ME(HttpMessage
, setInfo
, ai_HttpMessage_setInfo
, ZEND_ACC_PUBLIC
)
1990 PHP_ME(HttpMessage
, getResponseCode
, ai_HttpMessage_getResponseCode
, ZEND_ACC_PUBLIC
)
1991 PHP_ME(HttpMessage
, setResponseCode
, ai_HttpMessage_setResponseCode
, ZEND_ACC_PUBLIC
)
1992 PHP_ME(HttpMessage
, getResponseStatus
, ai_HttpMessage_getResponseStatus
, ZEND_ACC_PUBLIC
)
1993 PHP_ME(HttpMessage
, setResponseStatus
, ai_HttpMessage_setResponseStatus
, ZEND_ACC_PUBLIC
)
1994 PHP_ME(HttpMessage
, getRequestMethod
, ai_HttpMessage_getRequestMethod
, ZEND_ACC_PUBLIC
)
1995 PHP_ME(HttpMessage
, setRequestMethod
, ai_HttpMessage_setRequestMethod
, ZEND_ACC_PUBLIC
)
1996 PHP_ME(HttpMessage
, getRequestUrl
, ai_HttpMessage_getRequestUrl
, ZEND_ACC_PUBLIC
)
1997 PHP_ME(HttpMessage
, setRequestUrl
, ai_HttpMessage_setRequestUrl
, ZEND_ACC_PUBLIC
)
1998 PHP_ME(HttpMessage
, getHttpVersion
, ai_HttpMessage_getHttpVersion
, ZEND_ACC_PUBLIC
)
1999 PHP_ME(HttpMessage
, setHttpVersion
, ai_HttpMessage_setHttpVersion
, ZEND_ACC_PUBLIC
)
2000 PHP_ME(HttpMessage
, getParentMessage
, ai_HttpMessage_getParentMessage
, ZEND_ACC_PUBLIC
)
2001 PHP_ME(HttpMessage
, toString
, ai_HttpMessage_toString
, ZEND_ACC_PUBLIC
)
2002 PHP_ME(HttpMessage
, toCallback
, ai_HttpMessage_toCallback
, ZEND_ACC_PUBLIC
)
2003 PHP_ME(HttpMessage
, toStream
, ai_HttpMessage_toStream
, ZEND_ACC_PUBLIC
)
2005 /* implements Countable */
2006 PHP_ME(HttpMessage
, count
, ai_HttpMessage_count
, ZEND_ACC_PUBLIC
)
2008 /* implements Serializable */
2009 PHP_ME(HttpMessage
, serialize
, ai_HttpMessage_serialize
, ZEND_ACC_PUBLIC
)
2010 PHP_ME(HttpMessage
, unserialize
, ai_HttpMessage_unserialize
, ZEND_ACC_PUBLIC
)
2012 /* implements Iterator */
2013 PHP_ME(HttpMessage
, rewind
, ai_HttpMessage_rewind
, ZEND_ACC_PUBLIC
)
2014 PHP_ME(HttpMessage
, valid
, ai_HttpMessage_valid
, ZEND_ACC_PUBLIC
)
2015 PHP_ME(HttpMessage
, current
, ai_HttpMessage_current
, ZEND_ACC_PUBLIC
)
2016 PHP_ME(HttpMessage
, key
, ai_HttpMessage_key
, ZEND_ACC_PUBLIC
)
2017 PHP_ME(HttpMessage
, next
, ai_HttpMessage_next
, ZEND_ACC_PUBLIC
)
2019 ZEND_MALIAS(HttpMessage
, __toString
, toString
, ai_HttpMessage___toString
, ZEND_ACC_PUBLIC
)
2021 PHP_ME(HttpMessage
, detach
, ai_HttpMessage_detach
, ZEND_ACC_PUBLIC
)
2022 PHP_ME(HttpMessage
, prepend
, ai_HttpMessage_prepend
, ZEND_ACC_PUBLIC
)
2023 PHP_ME(HttpMessage
, reverse
, ai_HttpMessage_reverse
, ZEND_ACC_PUBLIC
)
2025 PHP_ME(HttpMessage
, isMultipart
, ai_HttpMessage_isMultipart
, ZEND_ACC_PUBLIC
)
2026 PHP_ME(HttpMessage
, splitMultipartBody
, ai_HttpMessage_splitMultipartBody
, ZEND_ACC_PUBLIC
)
2028 EMPTY_FUNCTION_ENTRY
2031 PHP_MINIT_FUNCTION(http_message
)
2033 zend_class_entry ce
= {0};
2035 INIT_NS_CLASS_ENTRY(ce
, "http", "Message", php_http_message_methods
);
2036 php_http_message_class_entry
= zend_register_internal_class(&ce
);
2037 php_http_message_class_entry
->create_object
= php_http_message_object_new
;
2038 memcpy(&php_http_message_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
2039 php_http_message_object_handlers
.offset
= XtOffsetOf(php_http_message_object_t
, zo
);
2040 php_http_message_object_handlers
.clone_obj
= php_http_message_object_clone
;
2041 php_http_message_object_handlers
.free_obj
= php_http_message_object_free
;
2042 php_http_message_object_handlers
.read_property
= php_http_message_object_read_prop
;
2043 php_http_message_object_handlers
.write_property
= php_http_message_object_write_prop
;
2044 php_http_message_object_handlers
.get_debug_info
= php_http_message_object_get_debug_info
;
2045 php_http_message_object_handlers
.get_property_ptr_ptr
= NULL
;
2046 php_http_message_object_handlers
.get_gc
= php_http_message_object_get_gc
;
2048 zend_class_implements(php_http_message_class_entry
, 3, spl_ce_Countable
, zend_ce_serializable
, zend_ce_iterator
);
2050 zend_hash_init(&php_http_message_object_prophandlers
, 9, NULL
, php_http_message_object_prophandler_hash_dtor
, 1);
2051 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("type"), PHP_HTTP_NONE
, ZEND_ACC_PROTECTED
);
2052 php_http_message_object_add_prophandler(ZEND_STRL("type"), php_http_message_object_prophandler_get_type
, php_http_message_object_prophandler_set_type
);
2053 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("body"), ZEND_ACC_PROTECTED
);
2054 php_http_message_object_add_prophandler(ZEND_STRL("body"), php_http_message_object_prophandler_get_body
, php_http_message_object_prophandler_set_body
);
2055 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestMethod"), "", ZEND_ACC_PROTECTED
);
2056 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
);
2057 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestUrl"), "", ZEND_ACC_PROTECTED
);
2058 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
);
2059 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("responseStatus"), "", ZEND_ACC_PROTECTED
);
2060 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
);
2061 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("responseCode"), 0, ZEND_ACC_PROTECTED
);
2062 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
);
2063 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("httpVersion"), ZEND_ACC_PROTECTED
);
2064 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
);
2065 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("headers"), ZEND_ACC_PROTECTED
);
2066 php_http_message_object_add_prophandler(ZEND_STRL("headers"), php_http_message_object_prophandler_get_headers
, php_http_message_object_prophandler_set_headers
);
2067 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("parentMessage"), ZEND_ACC_PROTECTED
);
2068 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
);
2070 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_NONE"), PHP_HTTP_NONE
);
2071 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_REQUEST"), PHP_HTTP_REQUEST
);
2072 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_RESPONSE"), PHP_HTTP_RESPONSE
);
2077 PHP_MSHUTDOWN_FUNCTION(http_message
)
2079 zend_hash_destroy(&php_http_message_object_prophandlers
);
2089 * vim600: noet sw=4 ts=4 fdm=marker
2090 * vim<600: noet sw=4 ts=4