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-2010, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
13 /* $Id: http_message_api.c 298689 2010-04-28 06:50:06Z mike $ */
17 #include <main/SAPI.h>
18 #include <ext/spl/spl_iterators.h>
19 #include <Zend/zend_interfaces.h>
21 PHP_HTTP_API zend_bool
php_http_message_info_callback(php_http_message_t
**message
, HashTable
**headers
, php_http_info_t
*info TSRMLS_DC
)
23 php_http_message_t
*old
= *message
;
26 if (old
->type
|| zend_hash_num_elements(&old
->hdrs
) || PHP_HTTP_BUFFER_LEN(old
)) {
27 (*message
) = php_http_message_init(NULL
, 0);
28 (*message
)->parent
= old
;
29 (*headers
) = &((*message
)->hdrs
);
32 php_http_message_set_info(*message
, info
);
34 return old
!= *message
;
37 PHP_HTTP_API php_http_message_t
*php_http_message_init(php_http_message_t
*message
, php_http_message_type_t type TSRMLS_DC
)
40 message
= emalloc(sizeof(*message
));
42 memset(message
, 0, sizeof(*message
));
43 TSRMLS_SET_CTX(message
->ts
);
45 php_http_message_set_type(message
, type
);
46 zend_hash_init(&message
->hdrs
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
47 php_http_message_body_init(&message
->body
, NULL TSRMLS_CC
);
52 PHP_HTTP_API php_http_message_t
*php_http_message_init_env(php_http_message_t
*message
, php_http_message_type_t type TSRMLS_DC
)
54 int free_msg
= !message
;
56 php_http_message_body_t
*mbody
;
58 message
= php_http_message_init(message
, type TSRMLS_CC
);
61 case PHP_HTTP_REQUEST
:
62 if ((sval
= php_http_env_get_server_var(ZEND_STRL("SERVER_PROTOCOL"), 1 TSRMLS_CC
)) && !strncmp(Z_STRVAL_P(sval
), "HTTP/", lenof("HTTP/"))) {
63 php_http_version_parse(&message
->http
.version
, Z_STRVAL_P(sval
));
65 message
->http
.version
.major
= 1;
66 message
->http
.version
.minor
= 1;
68 if ((sval
= php_http_env_get_server_var(ZEND_STRL("REQUEST_METHOD"), 1 TSRMLS_CC
))) {
69 message
->http
.info
.request
.method
= estrdup(Z_STRVAL_P(sval
));
71 if ((sval
= php_http_env_get_server_var(ZEND_STRL("REQUEST_URI"), 1 TSRMLS_CC
))) {
72 message
->http
.info
.request
.url
= estrdup(Z_STRVAL_P(sval
));
75 php_http_env_get_request_headers(&message
->hdrs TSRMLS_CC
);
77 if ((mbody
= php_http_env_get_request_body(TSRMLS_C
))) {
78 php_http_message_body_dtor(&message
->body
);
79 php_http_message_body_copy(mbody
, &message
->body
, 0 TSRMLS_CC
);
83 case PHP_HTTP_RESPONSE
:
84 if (!SG(sapi_headers
).http_status_line
|| !php_http_info_parse((php_http_info_t
*) &message
->http
, SG(sapi_headers
).http_status_line TSRMLS_CC
)) {
85 message
->http
.version
.major
= 1;
86 message
->http
.version
.minor
= 1;
87 switch ((message
->http
.info
.response
.code
= SG(sapi_headers
).http_response_code
)) {
89 message
->http
.info
.response
.code
= 200;
91 message
->http
.info
.response
.status
= estrdup("Ok");
94 message
->http
.info
.response
.status
= estrdup("");
100 php_http_env_get_response_headers(&message
->hdrs TSRMLS_CC
);
102 if (php_output_get_level()) {
103 if (php_output_get_status(TSRMLS_C
) & PHP_OUTPUT_SENT
) {
104 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "Could not fetch response body, output has already been sent at %s:%d", php_output_get_start_filename(TSRMLS_C
), php_output_get_start_lineno(TSRMLS_C
));
106 } else if (SUCCESS
!= php_output_get_contents(&tval TSRMLS_CC
)) {
107 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "Could not fetch response body");
110 php_http_message_body_append(&message
->body
, Z_STRVAL(tval
), Z_STRLEN(tval
));
119 php_http_message_free(&message
);
129 PHP_HTTP_API php_http_message_t
*php_http_message_parse(php_http_message_t
*msg
, const char *str
, size_t len TSRMLS_DC
)
131 php_http_message_parser_t p
;
132 php_http_buffer_t buf
;
135 msg
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
137 php_http_buffer_from_string_ex(&buf
, str
, len
);
138 php_http_message_parser_init(&p TSRMLS_CC
);
139 php_http_message_parser_parse(&p
, &buf
, PHP_HTTP_MESSAGE_PARSER_CLEANUP
, &msg
);
140 php_http_message_parser_dtor(&p
);
141 php_http_buffer_dtor(&buf
);
147 PHP_HTTP_API zval
*php_http_message_header(php_http_message_t
*msg
, char *key_str
, size_t key_len
, int join
)
149 zval
*ret
= NULL
, **header
;
150 char *key
= php_http_pretty_key(estrndup(key_str
, key_len
), key_len
, 1, 1);
152 if (SUCCESS
== zend_hash_find(&msg
->hdrs
, key
, key_len
+ 1, (void *) &header
)) {
153 if (join
&& Z_TYPE_PP(header
) == IS_ARRAY
) {
154 zval
*header_str
, **val
;
156 php_http_buffer_t str
;
158 php_http_buffer_init(&str
);
159 MAKE_STD_ZVAL(header_str
);
160 FOREACH_VAL(pos
, *header
, val
) {
161 zval
*strval
= php_http_zsep(IS_STRING
, *val
);
162 php_http_buffer_appendf(&str
, PHP_HTTP_BUFFER_LEN(&str
) ? ", %s":"%s", Z_STRVAL_P(strval
));
163 zval_ptr_dtor(&strval
);
165 php_http_buffer_fix(&str
);
166 ZVAL_STRINGL(header_str
, PHP_HTTP_BUFFER_VAL(&str
), PHP_HTTP_BUFFER_LEN(&str
), 0);
169 ret
= php_http_zsep(IS_STRING
, *header
);
180 PHP_HTTP_API
void php_http_message_set_type(php_http_message_t
*message
, php_http_message_type_t type
)
182 /* just act if different */
183 if (type
!= message
->type
) {
185 /* free request info */
186 switch (message
->type
= type
) {
187 case PHP_HTTP_REQUEST
:
188 STR_FREE(message
->http
.info
.request
.method
);
189 STR_FREE(message
->http
.info
.request
.url
);
192 case PHP_HTTP_RESPONSE
:
193 STR_FREE(message
->http
.info
.response
.status
);
200 memset(&message
->http
, 0, sizeof(message
->http
));
204 PHP_HTTP_API
void php_http_message_set_info(php_http_message_t
*message
, php_http_info_t
*info
)
206 php_http_message_set_type(message
, info
->type
);
207 message
->http
.version
= info
->http
.version
;
208 switch (message
->type
) {
209 case PHP_HTTP_REQUEST
:
210 STR_SET(PHP_HTTP_INFO(message
).request
.url
, PHP_HTTP_INFO(info
).request
.url
? estrdup(PHP_HTTP_INFO(info
).request
.url
) : NULL
);
211 STR_SET(PHP_HTTP_INFO(message
).request
.method
, PHP_HTTP_INFO(info
).request
.method
? estrdup(PHP_HTTP_INFO(info
).request
.method
) : NULL
);
214 case PHP_HTTP_RESPONSE
:
215 PHP_HTTP_INFO(message
).response
.code
= PHP_HTTP_INFO(info
).response
.code
;
216 STR_SET(PHP_HTTP_INFO(message
).response
.status
, PHP_HTTP_INFO(info
).response
.status
? estrdup(PHP_HTTP_INFO(info
).response
.status
) : NULL
);
224 static inline void message_headers(php_http_message_t
*msg
, php_http_buffer_t
*str
)
226 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
231 case PHP_HTTP_REQUEST
:
232 php_http_buffer_appendf(str
, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&msg
->http
, PHP_HTTP_CRLF
));
235 case PHP_HTTP_RESPONSE
:
236 php_http_buffer_appendf(str
, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&msg
->http
, PHP_HTTP_CRLF
));
243 FOREACH_HASH_KEYVAL(pos1
, &msg
->hdrs
, key
, header
) {
244 if (key
.type
== HASH_KEY_IS_STRING
) {
246 zval
**single_header
;
248 switch (Z_TYPE_PP(header
)) {
250 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_BVAL_PP(header
)?"true":"false");
254 php_http_buffer_appendf(str
, "%s: %ld" PHP_HTTP_CRLF
, key
.str
, Z_LVAL_PP(header
));
258 php_http_buffer_appendf(str
, "%s: %F" PHP_HTTP_CRLF
, key
.str
, Z_DVAL_PP(header
));
262 if (Z_STRVAL_PP(header
)[Z_STRLEN_PP(header
)-1] == '\r') fprintf(stderr
, "DOH!\n");
263 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_STRVAL_PP(header
));
267 FOREACH_VAL(pos2
, *header
, single_header
) {
268 switch (Z_TYPE_PP(single_header
)) {
270 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_BVAL_PP(single_header
)?"true":"false");
274 php_http_buffer_appendf(str
, "%s: %ld" PHP_HTTP_CRLF
, key
.str
, Z_LVAL_PP(single_header
));
278 php_http_buffer_appendf(str
, "%s: %F" PHP_HTTP_CRLF
, key
.str
, Z_DVAL_PP(single_header
));
282 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_STRVAL_PP(single_header
));
292 PHP_HTTP_API
void php_http_message_to_callback(php_http_message_t
*msg
, php_http_pass_callback_t cb
, void *cb_arg
)
294 php_http_buffer_t str
;
295 TSRMLS_FETCH_FROM_CTX(msg
->ts
);
297 php_http_buffer_init_ex(&str
, 0x1000, 0);
298 message_headers(msg
, &str
);
299 cb(cb_arg
, PHP_HTTP_BUFFER_VAL(&str
), PHP_HTTP_BUFFER_LEN(&str
));
300 php_http_buffer_dtor(&str
);
302 if (php_http_message_body_size(&msg
->body
)) {
303 cb(cb_arg
, ZEND_STRL(PHP_HTTP_CRLF
) TSRMLS_CC
);
304 php_http_message_body_to_callback(&msg
->body
, cb
, cb_arg
, 0, 0);
305 cb(cb_arg
, ZEND_STRL(PHP_HTTP_CRLF
) TSRMLS_CC
);
309 PHP_HTTP_API
void php_http_message_to_string(php_http_message_t
*msg
, char **string
, size_t *length
)
311 php_http_buffer_t str
;
314 php_http_buffer_init_ex(&str
, 0x1000, 0);
315 message_headers(msg
, &str
);
316 if (php_http_message_body_size(&msg
->body
)) {
317 php_http_buffer_appends(&str
, PHP_HTTP_CRLF
);
318 php_http_message_body_to_callback(&msg
->body
, (php_http_pass_callback_t
) php_http_buffer_append
, &str
, 0, 0);
319 php_http_buffer_appends(&str
, PHP_HTTP_CRLF
);
322 data
= php_http_buffer_data(&str
, string
, length
);
327 php_http_buffer_dtor(&str
);
330 PHP_HTTP_API
void php_http_message_serialize(php_http_message_t
*message
, char **string
, size_t *length
)
334 php_http_buffer_t str
;
336 php_http_buffer_init(&str
);
339 php_http_message_to_string(message
, &buf
, &len
);
340 php_http_buffer_prepend(&str
, buf
, len
);
342 } while ((message
= message
->parent
));
344 buf
= php_http_buffer_data(&str
, string
, length
);
349 php_http_buffer_dtor(&str
);
352 PHP_HTTP_API php_http_message_t
*php_http_message_reverse(php_http_message_t
*msg
)
356 php_http_message_count(c
, msg
);
359 php_http_message_t
*tmp
= msg
, **arr
= ecalloc(c
, sizeof(**arr
));
361 for (i
= 0; i
< c
; ++i
) {
365 arr
[0]->parent
= NULL
;
366 for (i
= 0; i
< c
-1; ++i
) {
367 arr
[i
+1]->parent
= arr
[i
];
377 PHP_HTTP_API php_http_message_t
*php_http_message_interconnect(php_http_message_t
*m1
, php_http_message_t
*m2
)
380 int i
= 0, c1
= 0, c2
= 0;
381 php_http_message_t
*t1
= m1
, *t2
= m2
, *p1
, *p2
;
383 php_http_message_count(c1
, m1
);
384 php_http_message_count(c2
, m2
);
386 while (i
++ < (c1
- c2
)) {
397 } else if (!m1
&& m2
) {
403 PHP_HTTP_API
void php_http_message_to_struct(php_http_message_t
*msg
, zval
*obj TSRMLS_DC
)
409 INIT_PZVAL_ARRAY(&strct
, HASH_OF(obj
));
411 add_assoc_long(&strct
, "type", msg
->type
);
412 spprintf(&version
, 0 ,"%u.%u", msg
->http
.version
.major
, msg
->http
.version
.minor
);
413 add_assoc_string_ex(&strct
, ZEND_STRL("httpVersion"), version
, 0);
416 case PHP_HTTP_RESPONSE
:
417 add_assoc_long(&strct
, "responseCode", msg
->http
.info
.response
.code
);
418 add_assoc_string(&strct
, "responseStatus", STR_PTR(msg
->http
.info
.response
.status
), 1);
421 case PHP_HTTP_REQUEST
:
422 add_assoc_string(&strct
, "requestMethod", STR_PTR(msg
->http
.info
.request
.method
), 1);
423 add_assoc_string(&strct
, "requestUrl", STR_PTR(msg
->http
.info
.request
.url
), 1);
427 /* avoid compiler warning */
431 MAKE_STD_ZVAL(headers
);
433 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
434 add_assoc_zval(&strct
, "headers", headers
);
436 add_assoc_stringl(&strct
, "body", PHP_HTTP_BUFFER_VAL(msg
), PHP_HTTP_BUFFER_LEN(msg
), 1);
441 MAKE_STD_ZVAL(parent
);
442 if (Z_TYPE_P(obj
) == IS_ARRAY
) {
447 add_assoc_zval(&strct
, "parentMessage", parent
);
448 php_http_message_to_struct(msg
->parent
, parent
);
450 add_assoc_null(&strct
, "parentMessage");
454 PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC)
458 switch (message->type) {
459 case PHP_HTTP_RESPONSE:
461 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
465 FOREACH_HASH_KEYVAL(pos, &message->hdrs, key, val) {
466 if (key.type == HASH_KEY_IS_STRING) {
467 http_send_header_zval_ex(key.str, key.len-1, val, 1);
470 rs = SUCCESS == http_send_status(message->http.info.response.code) &&
471 SUCCESS == http_send_data(PHP_HTTP_BUFFER_VAL(message), PHP_HTTP_BUFFER_LEN(message)) ?
476 case PHP_HTTP_REQUEST:
478 #ifdef PHP_HTTP_HAVE_CURL
480 http_request request;
481 zval **zhost, *options, *headers;
483 MAKE_STD_ZVAL(options);
484 MAKE_STD_ZVAL(headers);
487 zend_hash_copy(Z_ARRVAL_P(headers), &message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
488 add_assoc_zval(options, "headers", headers);
490 if (SUCCESS == zend_hash_find(&message->hdrs, "Host", sizeof("Host"), (void *) &zhost) && Z_TYPE_PP(zhost) == IS_STRING) {
492 php_url parts, *url = php_url_parse(message->http.info.request.url);
494 memset(&parts, 0, sizeof(php_url));
496 if ((colon = strchr(Z_STRVAL_PP(zhost), ':'))) {
497 parts.port = atoi(colon + 1);
498 parts.host = estrndup(Z_STRVAL_PP(zhost), (Z_STRVAL_PP(zhost) - colon - 1));
500 parts.host = estrndup(Z_STRVAL_PP(zhost), Z_STRLEN_PP(zhost));
503 http_build_url(PHP_HTTP_URL_REPLACE, url, &parts, NULL, &uri, NULL);
507 uri = http_absolute_url(message->http.info.request.url);
510 if ((request.meth = http_request_method_exists(1, 0, message->http.info.request.method))) {
511 http_request_body body;
513 http_request_init_ex(&request, NULL, request.meth, uri);
514 request.body = http_request_body_init_ex(&body, PHP_HTTP_REQUEST_BODY_CSTRING, PHP_HTTP_BUFFER_VAL(message), PHP_HTTP_BUFFER_LEN(message), 0);
515 if (SUCCESS == (rs = http_request_prepare(&request, Z_ARRVAL_P(options)))) {
516 http_request_exec(&request);
518 http_request_dtor(&request);
520 http_error_ex(HE_WARNING, PHP_HTTP_E_REQUEST_METHOD,
521 "Cannot send HttpMessage. Request method %s not supported",
522 message->http.info.request.method);
525 zval_ptr_dtor(&options);
527 http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "HTTP requests not supported - ext/http was not linked against libcurl.");
534 php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type PHP_HTTP_REQUEST nor PHP_HTTP_RESPONSE");
542 PHP_HTTP_API php_http_message_t
*php_http_message_copy(php_http_message_t
*from
, php_http_message_t
*to
)
544 php_http_message_t
*temp
, *copy
= NULL
;
545 php_http_info_t info
;
546 TSRMLS_FETCH_FROM_CTX(from
->ts
);
549 info
.type
= from
->type
;
550 info
.http
= from
->http
;
552 copy
= temp
= php_http_message_init(to
, 0);
553 php_http_message_set_info(temp
, &info
);
554 zend_hash_copy(&temp
->hdrs
, &from
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
555 php_http_message_body_copy(&from
->body
, &temp
->body
, 1);
557 while (from
->parent
) {
558 info
.type
= from
->parent
->type
;
559 info
.http
= from
->parent
->http
;
561 temp
->parent
= php_http_message_init(NULL
, 0);
562 php_http_message_set_info(temp
->parent
, &info
);
563 zend_hash_copy(&temp
->parent
->hdrs
, &from
->parent
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
564 php_http_message_body_copy(&from
->body
, &temp
->body
, 1);
574 PHP_HTTP_API
void php_http_message_dtor(php_http_message_t
*message
)
577 zend_hash_destroy(&message
->hdrs
);
578 php_http_message_body_dtor(&message
->body
);
580 switch (message
->type
) {
581 case PHP_HTTP_REQUEST
:
582 STR_SET(message
->http
.info
.request
.method
, NULL
);
583 STR_SET(message
->http
.info
.request
.url
, NULL
);
586 case PHP_HTTP_RESPONSE
:
587 STR_SET(message
->http
.info
.response
.status
, NULL
);
596 PHP_HTTP_API
void php_http_message_free(php_http_message_t
**message
)
599 if ((*message
)->parent
) {
600 php_http_message_free(&(*message
)->parent
);
602 php_http_message_dtor(*message
);
608 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpMessage, method, 0, req_args)
609 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpMessage, method, 0)
610 #define PHP_HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, PHP_HTTP_ARGS(HttpMessage, method), visibility)
612 PHP_HTTP_BEGIN_ARGS(__construct
, 0)
613 PHP_HTTP_ARG_VAL(message
, 0)
616 PHP_HTTP_EMPTY_ARGS(getBody
);
617 PHP_HTTP_BEGIN_ARGS(setBody
, 1)
618 PHP_HTTP_ARG_VAL(body
, 0)
621 PHP_HTTP_BEGIN_ARGS(getHeader
, 1)
622 PHP_HTTP_ARG_VAL(header
, 0)
625 PHP_HTTP_BEGIN_ARGS(setHeader
, 1)
626 PHP_HTTP_ARG_VAL(header
, 0)
627 PHP_HTTP_ARG_VAL(value
, 0)
630 PHP_HTTP_BEGIN_ARGS(addHeader
, 2)
631 PHP_HTTP_ARG_VAL(header
, 0)
632 PHP_HTTP_ARG_VAL(value
, 0)
635 PHP_HTTP_EMPTY_ARGS(getHeaders
);
636 PHP_HTTP_BEGIN_ARGS(setHeaders
, 1)
637 PHP_HTTP_ARG_VAL(headers
, 0)
640 PHP_HTTP_BEGIN_ARGS(addHeaders
, 1)
641 PHP_HTTP_ARG_VAL(headers
, 0)
642 PHP_HTTP_ARG_VAL(append
, 0)
645 PHP_HTTP_EMPTY_ARGS(getType
);
646 PHP_HTTP_BEGIN_ARGS(setType
, 1)
647 PHP_HTTP_ARG_VAL(type
, 0)
650 PHP_HTTP_EMPTY_ARGS(getInfo
);
651 PHP_HTTP_BEGIN_ARGS(setInfo
, 1)
652 PHP_HTTP_ARG_VAL(http_info
, 0)
655 PHP_HTTP_EMPTY_ARGS(getResponseCode
);
656 PHP_HTTP_BEGIN_ARGS(setResponseCode
, 1)
657 PHP_HTTP_ARG_VAL(response_code
, 0)
660 PHP_HTTP_EMPTY_ARGS(getResponseStatus
);
661 PHP_HTTP_BEGIN_ARGS(setResponseStatus
, 1)
662 PHP_HTTP_ARG_VAL(response_status
, 0)
665 PHP_HTTP_EMPTY_ARGS(getRequestMethod
);
666 PHP_HTTP_BEGIN_ARGS(setRequestMethod
, 1)
667 PHP_HTTP_ARG_VAL(request_method
, 0)
670 PHP_HTTP_EMPTY_ARGS(getRequestUrl
);
671 PHP_HTTP_BEGIN_ARGS(setRequestUrl
, 1)
672 PHP_HTTP_ARG_VAL(url
, 0)
675 PHP_HTTP_EMPTY_ARGS(getHttpVersion
);
676 PHP_HTTP_BEGIN_ARGS(setHttpVersion
, 1)
677 PHP_HTTP_ARG_VAL(http_version
, 0)
680 PHP_HTTP_BEGIN_ARGS(guessContentType
, 1)
681 PHP_HTTP_ARG_VAL(magic_file
, 0)
682 PHP_HTTP_ARG_VAL(magic_mode
, 0)
685 PHP_HTTP_EMPTY_ARGS(getParentMessage
);
686 PHP_HTTP_EMPTY_ARGS(send
);
687 PHP_HTTP_EMPTY_ARGS(__toString
);
688 PHP_HTTP_BEGIN_ARGS(toString
, 0)
689 PHP_HTTP_ARG_VAL(include_parent
, 0)
692 PHP_HTTP_EMPTY_ARGS(toMessageTypeObject
);
694 PHP_HTTP_EMPTY_ARGS(count
);
696 PHP_HTTP_EMPTY_ARGS(serialize
);
697 PHP_HTTP_BEGIN_ARGS(unserialize
, 1)
698 PHP_HTTP_ARG_VAL(serialized
, 0)
701 PHP_HTTP_EMPTY_ARGS(rewind
);
702 PHP_HTTP_EMPTY_ARGS(valid
);
703 PHP_HTTP_EMPTY_ARGS(key
);
704 PHP_HTTP_EMPTY_ARGS(current
);
705 PHP_HTTP_EMPTY_ARGS(next
);
707 PHP_HTTP_EMPTY_ARGS(detach
);
708 PHP_HTTP_BEGIN_ARGS(prepend
, 1)
709 PHP_HTTP_ARG_OBJ(http
\\Message
, message
, 0)
711 PHP_HTTP_EMPTY_ARGS(reverse
);
713 static zval
*php_http_message_object_read_prop(zval
*object
, zval
*member
, int type
, const zend_literal
*literal_key TSRMLS_DC
);
714 static void php_http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value
, const zend_literal
*literal_key TSRMLS_DC
);
715 static zval
**php_http_message_object_get_prop_ptr(zval
*object
, zval
*member
, const zend_literal
*literal_key TSRMLS_DC
);
716 static HashTable
*php_http_message_object_get_props(zval
*object TSRMLS_DC
);
718 zend_class_entry
*php_http_message_class_entry
;
719 zend_function_entry php_http_message_method_entry
[] = {
720 PHP_HTTP_MESSAGE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
721 PHP_HTTP_MESSAGE_ME(getBody
, ZEND_ACC_PUBLIC
)
722 PHP_HTTP_MESSAGE_ME(setBody
, ZEND_ACC_PUBLIC
)
723 PHP_HTTP_MESSAGE_ME(getHeader
, ZEND_ACC_PUBLIC
)
724 PHP_HTTP_MESSAGE_ME(setHeader
, ZEND_ACC_PUBLIC
)
725 PHP_HTTP_MESSAGE_ME(addHeader
, ZEND_ACC_PUBLIC
)
726 PHP_HTTP_MESSAGE_ME(getHeaders
, ZEND_ACC_PUBLIC
)
727 PHP_HTTP_MESSAGE_ME(setHeaders
, ZEND_ACC_PUBLIC
)
728 PHP_HTTP_MESSAGE_ME(addHeaders
, ZEND_ACC_PUBLIC
)
729 PHP_HTTP_MESSAGE_ME(getType
, ZEND_ACC_PUBLIC
)
730 PHP_HTTP_MESSAGE_ME(setType
, ZEND_ACC_PUBLIC
)
731 PHP_HTTP_MESSAGE_ME(getInfo
, ZEND_ACC_PUBLIC
)
732 PHP_HTTP_MESSAGE_ME(setInfo
, ZEND_ACC_PUBLIC
)
733 PHP_HTTP_MESSAGE_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
734 PHP_HTTP_MESSAGE_ME(setResponseCode
, ZEND_ACC_PUBLIC
)
735 PHP_HTTP_MESSAGE_ME(getResponseStatus
, ZEND_ACC_PUBLIC
)
736 PHP_HTTP_MESSAGE_ME(setResponseStatus
, ZEND_ACC_PUBLIC
)
737 PHP_HTTP_MESSAGE_ME(getRequestMethod
, ZEND_ACC_PUBLIC
)
738 PHP_HTTP_MESSAGE_ME(setRequestMethod
, ZEND_ACC_PUBLIC
)
739 PHP_HTTP_MESSAGE_ME(getRequestUrl
, ZEND_ACC_PUBLIC
)
740 PHP_HTTP_MESSAGE_ME(setRequestUrl
, ZEND_ACC_PUBLIC
)
741 PHP_HTTP_MESSAGE_ME(getHttpVersion
, ZEND_ACC_PUBLIC
)
742 PHP_HTTP_MESSAGE_ME(setHttpVersion
, ZEND_ACC_PUBLIC
)
743 PHP_HTTP_MESSAGE_ME(getParentMessage
, ZEND_ACC_PUBLIC
)
744 PHP_HTTP_MESSAGE_ME(toString
, ZEND_ACC_PUBLIC
)
746 /* implements Countable */
747 PHP_HTTP_MESSAGE_ME(count
, ZEND_ACC_PUBLIC
)
749 /* implements Serializable */
750 PHP_HTTP_MESSAGE_ME(serialize
, ZEND_ACC_PUBLIC
)
751 PHP_HTTP_MESSAGE_ME(unserialize
, ZEND_ACC_PUBLIC
)
753 /* implements Iterator */
754 PHP_HTTP_MESSAGE_ME(rewind
, ZEND_ACC_PUBLIC
)
755 PHP_HTTP_MESSAGE_ME(valid
, ZEND_ACC_PUBLIC
)
756 PHP_HTTP_MESSAGE_ME(current
, ZEND_ACC_PUBLIC
)
757 PHP_HTTP_MESSAGE_ME(key
, ZEND_ACC_PUBLIC
)
758 PHP_HTTP_MESSAGE_ME(next
, ZEND_ACC_PUBLIC
)
760 ZEND_MALIAS(HttpMessage
, __toString
, toString
, PHP_HTTP_ARGS(HttpMessage
, __toString
), ZEND_ACC_PUBLIC
)
762 PHP_HTTP_MESSAGE_ME(detach
, ZEND_ACC_PUBLIC
)
763 PHP_HTTP_MESSAGE_ME(prepend
, ZEND_ACC_PUBLIC
)
764 PHP_HTTP_MESSAGE_ME(reverse
, ZEND_ACC_PUBLIC
)
768 static zend_object_handlers php_http_message_object_handlers
;
769 static HashTable php_http_message_object_prophandlers
;
771 typedef void (*php_http_message_object_prophandler_func_t
)(php_http_message_object_t
*o
, zval
*v TSRMLS_DC
);
773 typedef struct php_http_message_object_prophandler
{
774 php_http_message_object_prophandler_func_t read
;
775 php_http_message_object_prophandler_func_t write
;
776 } php_http_message_object_prophandler_t
;
778 static STATUS
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
) {
779 php_http_message_object_prophandler_t h
= { read
, write
};
780 return zend_hash_add(&php_http_message_object_prophandlers
, prop_str
, prop_len
+ 1, (void *) &h
, sizeof(h
), NULL
);
782 static int php_http_message_object_has_prophandler(const char *prop_str
, size_t prop_len
) {
783 return zend_hash_exists(&php_http_message_object_prophandlers
, prop_str
, prop_len
+ 1);
785 static STATUS
php_http_message_object_get_prophandler(const char *prop_str
, size_t prop_len
, php_http_message_object_prophandler_t
**handler
) {
786 return zend_hash_find(&php_http_message_object_prophandlers
, prop_str
, prop_len
+ 1, (void *) handler
);
788 static void php_http_message_object_prophandler_get_type(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
789 RETVAL_LONG(obj
->message
->type
);
791 static void php_http_message_object_prophandler_set_type(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
792 zval
*cpy
= php_http_zsep(IS_LONG
, value
);
793 php_http_message_set_type(obj
->message
, Z_LVAL_P(cpy
));
796 static void php_http_message_object_prophandler_get_request_method(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
797 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
) && obj
->message
->http
.info
.request
.method
) {
798 RETVAL_STRING(obj
->message
->http
.info
.request
.method
, 1);
803 static void php_http_message_object_prophandler_set_request_method(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
804 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
805 zval
*cpy
= php_http_zsep(IS_STRING
, value
);
806 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
810 static void php_http_message_object_prophandler_get_request_url(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
811 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
) && obj
->message
->http
.info
.request
.url
) {
812 RETVAL_STRING(obj
->message
->http
.info
.request
.url
, 1);
817 static void php_http_message_object_prophandler_set_request_url(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
818 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
819 zval
*cpy
= php_http_zsep(IS_STRING
, value
);
820 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
824 static void php_http_message_object_prophandler_get_response_status(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
825 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
) && obj
->message
->http
.info
.response
.status
) {
826 RETVAL_STRING(obj
->message
->http
.info
.response
.status
, 1);
831 static void php_http_message_object_prophandler_set_response_status(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
832 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
833 zval
*cpy
= php_http_zsep(IS_STRING
, value
);
834 STR_SET(obj
->message
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
838 static void php_http_message_object_prophandler_get_response_code(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
839 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
840 RETVAL_LONG(obj
->message
->http
.info
.response
.code
);
845 static void php_http_message_object_prophandler_set_response_code(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
846 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
847 zval
*cpy
= php_http_zsep(IS_LONG
, value
);
848 obj
->message
->http
.info
.response
.code
= Z_LVAL_P(cpy
);
852 static void php_http_message_object_prophandler_get_http_version(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
856 php_http_version_to_string(&obj
->message
->http
.version
, &version_str
, &version_len
, NULL
, NULL TSRMLS_CC
);
857 RETVAL_STRINGL(version_str
, version_len
, 0);
859 static void php_http_message_object_prophandler_set_http_version(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
860 zval
*cpy
= php_http_zsep(IS_STRING
, value
);
861 php_http_version_parse(&obj
->message
->http
.version
, Z_STRVAL_P(cpy
) TSRMLS_CC
);
864 static void php_http_message_object_prophandler_get_headers(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
865 array_init(return_value
);
866 zend_hash_copy(Z_ARRVAL_P(return_value
), &obj
->message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
868 static void php_http_message_object_prophandler_set_headers(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
869 zval
*cpy
= php_http_zsep(IS_ARRAY
, value
);
871 zend_hash_clean(&obj
->message
->hdrs
);
872 zend_hash_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(cpy
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
875 static void php_http_message_object_prophandler_get_body(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
876 if (obj
->body
.handle
) {
877 RETVAL_OBJVAL(obj
->body
, 1);
882 static void php_http_message_object_prophandler_set_body(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
883 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), php_http_message_body_class_entry TSRMLS_CC
)) {
884 if (obj
->body
.handle
) {
885 zend_objects_store_del_ref_by_handle(obj
->body
.handle TSRMLS_CC
);
887 Z_OBJ_ADDREF_P(value
);
888 obj
->body
= Z_OBJVAL_P(value
);
891 static void php_http_message_object_prophandler_get_parent_message(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
892 if (obj
->message
->parent
) {
893 RETVAL_OBJVAL(obj
->parent
, 1);
898 static void php_http_message_object_prophandler_set_parent_message(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
899 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), php_http_message_class_entry TSRMLS_CC
)) {
900 if (obj
->message
->parent
) {
901 zend_objects_store_del_ref_by_handle(obj
->parent
.handle TSRMLS_CC
);
903 Z_OBJ_ADDREF_P(value
);
904 obj
->parent
= Z_OBJVAL_P(value
);
908 PHP_MINIT_FUNCTION(http_message
)
910 PHP_HTTP_REGISTER_CLASS(http
, Message
, http_message
, php_http_object_class_entry
, 0);
911 php_http_message_class_entry
->create_object
= php_http_message_object_new
;
912 memcpy(&php_http_message_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
913 php_http_message_object_handlers
.clone_obj
= php_http_message_object_clone
;
914 php_http_message_object_handlers
.read_property
= php_http_message_object_read_prop
;
915 php_http_message_object_handlers
.write_property
= php_http_message_object_write_prop
;
916 php_http_message_object_handlers
.get_properties
= php_http_message_object_get_props
;
917 php_http_message_object_handlers
.get_property_ptr_ptr
= php_http_message_object_get_prop_ptr
;
919 zend_class_implements(php_http_message_class_entry TSRMLS_CC
, 3, spl_ce_Countable
, zend_ce_serializable
, zend_ce_iterator
);
921 zend_hash_init(&php_http_message_object_prophandlers
, 9, NULL
, NULL
, 1);
922 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("type"), PHP_HTTP_NONE
, ZEND_ACC_PROTECTED TSRMLS_CC
);
923 php_http_message_object_add_prophandler(ZEND_STRL("type"), php_http_message_object_prophandler_get_type
, php_http_message_object_prophandler_set_type
);
924 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("body"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
925 php_http_message_object_add_prophandler(ZEND_STRL("body"), php_http_message_object_prophandler_get_body
, php_http_message_object_prophandler_set_body
);
926 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestMethod"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
927 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
);
928 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestUrl"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
929 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
);
930 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("responseStatus"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
931 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
);
932 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("responseCode"), 0, ZEND_ACC_PROTECTED TSRMLS_CC
);
933 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
);
934 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("httpVersion"), ZEND_ACC_PROTECTED TSRMLS_CC
);
935 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
);
936 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("headers"), ZEND_ACC_PROTECTED TSRMLS_CC
);
937 php_http_message_object_add_prophandler(ZEND_STRL("headers"), php_http_message_object_prophandler_get_headers
, php_http_message_object_prophandler_set_headers
);
938 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("parentMessage"), ZEND_ACC_PROTECTED TSRMLS_CC
);
939 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
);
941 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_NONE"), PHP_HTTP_NONE TSRMLS_CC
);
942 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_REQUEST"), PHP_HTTP_REQUEST TSRMLS_CC
);
943 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_RESPONSE"), PHP_HTTP_RESPONSE TSRMLS_CC
);
948 PHP_MSHUTDOWN_FUNCTION(http_message
)
950 zend_hash_destroy(&php_http_message_object_prophandlers
);
955 void php_http_message_object_reverse(zval
*this_ptr
, zval
*return_value TSRMLS_DC
)
958 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
961 php_http_message_count(i
, obj
->message
);
965 zend_object_value
*ovalues
= NULL
;
966 php_http_message_object_t
**objects
= NULL
;
969 objects
= ecalloc(i
, sizeof(**objects
));
970 ovalues
= ecalloc(i
, sizeof(*ovalues
));
972 /* we are the first message */
974 ovalues
[0] = getThis()->value
.obj
;
979 for (i
= 1; obj
->parent
.handle
; ++i
) {
980 o
.value
.obj
= obj
->parent
;
981 ovalues
[i
] = o
.value
.obj
;
982 objects
[i
] = obj
= zend_object_store_get_object(&o TSRMLS_CC
);
985 /* reorder parents */
986 for (last
= --i
; i
; --i
) {
987 objects
[i
]->message
->parent
= objects
[i
-1]->message
;
988 objects
[i
]->parent
= ovalues
[i
-1];
990 objects
[0]->message
->parent
= NULL
;
991 objects
[0]->parent
.handle
= 0;
992 objects
[0]->parent
.handlers
= NULL
;
995 Z_OBJ_ADDREF_P(getThis());
996 RETVAL_OBJVAL(ovalues
[last
], 1);
1001 RETURN_ZVAL(getThis(), 1, 0);
1005 void php_http_message_object_prepend(zval
*this_ptr
, zval
*prepend
, zend_bool top TSRMLS_DC
)
1008 php_http_message_t
*save_parent_msg
= NULL
;
1009 zend_object_value save_parent_obj
= {0, NULL
};
1010 php_http_message_object_t
*obj
= zend_object_store_get_object(this_ptr TSRMLS_CC
);
1011 php_http_message_object_t
*prepend_obj
= zend_object_store_get_object(prepend TSRMLS_CC
);
1017 save_parent_obj
= obj
->parent
;
1018 save_parent_msg
= obj
->message
->parent
;
1020 /* iterate to the most parent object */
1021 while (obj
->parent
.handle
) {
1022 m
.value
.obj
= obj
->parent
;
1023 obj
= zend_object_store_get_object(&m TSRMLS_CC
);
1028 obj
->parent
= prepend
->value
.obj
;
1029 obj
->message
->parent
= prepend_obj
->message
;
1032 zend_objects_store_add_ref(prepend TSRMLS_CC
);
1033 while (prepend_obj
->parent
.handle
) {
1034 m
.value
.obj
= prepend_obj
->parent
;
1035 zend_objects_store_add_ref(&m TSRMLS_CC
);
1036 prepend_obj
= zend_object_store_get_object(&m TSRMLS_CC
);
1040 prepend_obj
->parent
= save_parent_obj
;
1041 prepend_obj
->message
->parent
= save_parent_msg
;
1045 zend_object_value
php_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
1047 return php_http_message_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
1050 zend_object_value
php_http_message_object_new_ex(zend_class_entry
*ce
, php_http_message_t
*msg
, php_http_message_object_t
**ptr TSRMLS_DC
)
1052 zend_object_value ov
;
1053 php_http_message_object_t
*o
;
1055 o
= ecalloc(1, sizeof(php_http_message_object_t
));
1056 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
1057 object_properties_init((zend_object
*) o
, ce
);
1066 o
->parent
= php_http_message_object_new_ex(ce
, msg
->parent
, NULL TSRMLS_CC
);
1068 o
->body
= php_http_message_body_object_new_ex(php_http_message_body_class_entry
, php_http_message_body_copy(&msg
->body
, NULL
, 0), NULL TSRMLS_CC
);
1071 ov
.handle
= zend_objects_store_put((zend_object
*) o
, NULL
, php_http_message_object_free
, NULL TSRMLS_CC
);
1072 ov
.handlers
= &php_http_message_object_handlers
;
1077 zend_object_value
php_http_message_object_clone(zval
*this_ptr TSRMLS_DC
)
1079 zend_object_value new_ov
;
1080 php_http_message_object_t
*new_obj
= NULL
;
1081 php_http_message_object_t
*old_obj
= zend_object_store_get_object(this_ptr TSRMLS_CC
);
1083 new_ov
= php_http_message_object_new_ex(old_obj
->zo
.ce
, php_http_message_copy(old_obj
->message
, NULL
), &new_obj
);
1084 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
1089 void php_http_message_object_free(void *object TSRMLS_DC
)
1091 php_http_message_object_t
*o
= (php_http_message_object_t
*) object
;
1094 zval_ptr_dtor(&o
->iterator
);
1098 /* do NOT free recursivly */
1099 php_http_message_dtor(o
->message
);
1103 if (o
->parent
.handle
) {
1104 zend_objects_store_del_ref_by_handle(o
->parent
.handle TSRMLS_CC
);
1106 if (o
->body
.handle
) {
1107 zend_objects_store_del_ref_by_handle(o
->body
.handle TSRMLS_CC
);
1109 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
1114 static zval
**php_http_message_object_get_prop_ptr(zval
*object
, zval
*member
, const zend_literal
*literal_key TSRMLS_DC
) {
1115 php_http_message_object_prophandler_t
*handler
;
1116 zval
*copy
= php_http_zsep(IS_STRING
, member
);
1118 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1119 zval_ptr_dtor(©
);
1120 return &php_http_property_proxy_init(NULL
, object
, member TSRMLS_CC
)->myself
;
1122 zval_ptr_dtor(©
);
1124 return zend_get_std_object_handlers()->get_property_ptr_ptr(object
, member
, literal_key TSRMLS_CC
);
1127 static zval
*php_http_message_object_read_prop(zval
*object
, zval
*member
, int type
, const zend_literal
*literal_key TSRMLS_DC
)
1129 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1130 php_http_message_object_prophandler_t
*handler
;
1131 zval
*return_value
, *copy
= php_http_zsep(IS_STRING
, member
);
1133 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1134 if (type
== BP_VAR_W
) {
1135 zval_ptr_dtor(©
);
1136 zend_error(E_ERROR
, "Cannot access HttpMessage properties by reference or array key/index");
1140 ALLOC_ZVAL(return_value
);
1141 Z_SET_REFCOUNT_P(return_value
, 0);
1142 Z_UNSET_ISREF_P(return_value
);
1144 handler
->read(obj
, return_value TSRMLS_CC
);
1147 return_value
= zend_get_std_object_handlers()->read_property(object
, member
, type
, literal_key TSRMLS_CC
);
1150 zval_ptr_dtor(©
);
1152 return return_value
;
1155 static void php_http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value
, const zend_literal
*literal_key TSRMLS_DC
)
1157 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1158 php_http_message_object_prophandler_t
*handler
;
1159 zval
*copy
= php_http_zsep(IS_STRING
, member
);
1161 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1162 handler
->write(obj
, value TSRMLS_CC
);
1164 zend_get_std_object_handlers()->write_property(object
, member
, value
, literal_key TSRMLS_CC
);
1167 zval_ptr_dtor(©
);
1171 static HashTable
*php_http_message_object_get_props(zval
*object TSRMLS_DC
)
1174 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1175 php_http_message_t
*msg
= obj
->message
;
1176 HashTable
*props
= zend_get_std_object_handlers()->get_properties(object TSRMLS_CC
);
1177 zval array
, *parent
, *body
;
1180 INIT_PZVAL_ARRAY(&array
, props
);
1182 #define ASSOC_PROP(array, ptype, name, val) \
1184 char *m_prop_name; \
1186 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
1187 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
1188 efree(m_prop_name); \
1190 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
1191 #define ASSOC_STRINGL(array, name, val, len) ASSOC_STRINGL_EX(array, name, val, len, 1)
1192 #define ASSOC_STRINGL_EX(array, name, val, len, cpy) \
1194 char *m_prop_name; \
1196 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
1197 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, cpy); \
1198 efree(m_prop_name); \
1201 ASSOC_PROP(array
, long, "type", msg
->type
);
1202 ASSOC_STRINGL_EX(array
, "httpVersion", version
, spprintf(&version
, 0, "%u.%u", msg
->http
.version
.major
, msg
->http
.version
.minor
), 0);
1204 switch (msg
->type
) {
1205 case PHP_HTTP_REQUEST
:
1206 ASSOC_PROP(array
, long, "responseCode", 0);
1207 ASSOC_STRINGL(array
, "responseStatus", "", 0);
1208 ASSOC_STRING(array
, "requestMethod", STR_PTR(msg
->http
.info
.request
.method
));
1209 ASSOC_STRING(array
, "requestUrl", STR_PTR(msg
->http
.info
.request
.url
));
1212 case PHP_HTTP_RESPONSE
:
1213 ASSOC_PROP(array
, long, "responseCode", msg
->http
.info
.response
.code
);
1214 ASSOC_STRING(array
, "responseStatus", STR_PTR(msg
->http
.info
.response
.status
));
1215 ASSOC_STRINGL(array
, "requestMethod", "", 0);
1216 ASSOC_STRINGL(array
, "requestUrl", "", 0);
1221 ASSOC_PROP(array
, long, "responseCode", 0);
1222 ASSOC_STRINGL(array
, "responseStatus", "", 0);
1223 ASSOC_STRINGL(array
, "requestMethod", "", 0);
1224 ASSOC_STRINGL(array
, "requestUrl", "", 0);
1228 MAKE_STD_ZVAL(headers
);
1229 array_init(headers
);
1230 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1231 ASSOC_PROP(array
, zval
, "headers", headers
);
1233 MAKE_STD_ZVAL(body
);
1234 if (!obj
->body
.handle
) {
1235 php_http_new(&obj
->body
, php_http_message_body_class_entry
, (php_http_new_t
) php_http_message_body_object_new_ex
, NULL
, (void *) php_http_message_body_copy(&obj
->message
->body
, NULL
, 0), NULL TSRMLS_CC
);
1237 ZVAL_OBJVAL(body
, obj
->body
, 1);
1238 ASSOC_PROP(array
, zval
, "body", body
);
1240 MAKE_STD_ZVAL(parent
);
1242 ZVAL_OBJVAL(parent
, obj
->parent
, 1);
1246 ASSOC_PROP(array
, zval
, "parentMessage", parent
);
1253 PHP_METHOD(HttpMessage
, __construct
)
1256 char *message
= NULL
;
1257 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1259 with_error_handling(EH_THROW
, PHP_HTTP_EX_CE(runtime
)) {
1260 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
1261 php_http_message_t
*msg
= obj
->message
;
1263 php_http_message_dtor(msg
);
1264 with_error_handling(EH_THROW
, PHP_HTTP_EX_CE(message
)) {
1265 if ((obj
->message
= php_http_message_parse(msg
, message
, length
))) {
1266 if (obj
->message
->parent
) {
1267 obj
->parent
= php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj
->message
->parent
, NULL TSRMLS_CC
);
1270 obj
->message
= php_http_message_init(msg
, 0 TSRMLS_CC
);
1272 } end_error_handling();
1274 if (!obj
->message
) {
1275 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1277 } end_error_handling();
1281 PHP_METHOD(HttpMessage
, getBody
)
1283 with_error_handling(EH_THROW
, PHP_HTTP_EX_CE(message
)) {
1284 if (SUCCESS
== zend_parse_parameters_none()) {
1285 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1287 if (!obj
->message
) {
1288 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1291 if (obj
->body
.handle
|| SUCCESS
== php_http_new(&obj
->body
, php_http_message_body_class_entry
, (php_http_new_t
) php_http_message_body_object_new_ex
, NULL
, (void *) php_http_message_body_copy(&obj
->message
->body
, NULL
, 0), NULL TSRMLS_CC
)) {
1292 RETVAL_OBJVAL(obj
->body
, 1);
1295 } end_error_handling();
1298 PHP_METHOD(HttpMessage
, setBody
)
1302 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &zbody
, php_http_message_body_class_entry
)) {
1303 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1304 php_http_message_body_object_t
*body_obj
= zend_object_store_get_object(zbody TSRMLS_CC
);
1306 if (!obj
->message
) {
1307 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1310 php_http_message_body_dtor(&obj
->message
->body
);
1311 php_http_message_body_init(&obj
->message
->body
, php_http_message_body_stream(body_obj
->body
));
1312 Z_OBJ_ADDREF_P(zbody
);
1313 obj
->body
= Z_OBJVAL_P(zbody
);
1319 PHP_METHOD(HttpMessage
, getHeader
)
1324 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header_str
, &header_len
)) {
1325 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1328 if (!obj
->message
) {
1329 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1332 if ((header
= php_http_message_header(obj
->message
, header_str
, header_len
, 0))) {
1333 RETURN_ZVAL(header
, 1, 1);
1339 PHP_METHOD(HttpMessage
, getHeaders
)
1341 if (SUCCESS
== zend_parse_parameters_none()) {
1342 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1344 if (!obj
->message
) {
1345 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1348 array_init(return_value
);
1349 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(return_value
));
1353 PHP_METHOD(HttpMessage
, setHeader
)
1355 zval
*zvalue
= NULL
;
1359 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|z!", &name_str
, &name_len
, &zvalue
)) {
1360 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1361 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1363 if (!obj
->message
) {
1364 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1368 RETVAL_SUCCESS(zend_hash_del(&obj
->message
->hdrs
, name
, name_len
+ 1));
1371 RETVAL_SUCCESS(zend_hash_update(&obj
->message
->hdrs
, name
, name_len
+ 1, &zvalue
, sizeof(void *), NULL
));
1379 PHP_METHOD(HttpMessage
, setHeaders
)
1381 zval
*new_headers
= NULL
;
1383 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &new_headers
)) {
1384 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1386 if (!obj
->message
) {
1387 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1390 zend_hash_clean(&obj
->message
->hdrs
);
1392 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, 0, ARRAY_JOIN_PRETTIFY
|ARRAY_JOIN_STRONLY
);
1399 PHP_METHOD(HttpMessage
, addHeader
)
1405 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sz", &name_str
, &name_len
, &zvalue
)) {
1406 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1407 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1410 if (!obj
->message
) {
1411 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1415 if ((header
= php_http_message_header(obj
->message
, name
, name_len
, 0))) {
1416 convert_to_array(header
);
1417 RETVAL_SUCCESS(zend_hash_next_index_insert(Z_ARRVAL_P(header
), &zvalue
, sizeof(void *), NULL
));
1419 RETVAL_SUCCESS(zend_hash_update(&obj
->message
->hdrs
, name
, name_len
+ 1, &zvalue
, sizeof(void *), NULL
));
1427 PHP_METHOD(HttpMessage
, addHeaders
)
1430 zend_bool append
= 0;
1432 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
1433 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1435 if (!obj
->message
) {
1436 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1439 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, append
, ARRAY_JOIN_STRONLY
|ARRAY_JOIN_PRETTIFY
);
1445 PHP_METHOD(HttpMessage
, getType
)
1447 if (SUCCESS
== zend_parse_parameters_none()) {
1448 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1450 if (!obj
->message
) {
1451 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1454 RETURN_LONG(obj
->message
->type
);
1458 PHP_METHOD(HttpMessage
, setType
)
1462 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
1463 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1465 if (!obj
->message
) {
1466 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1469 php_http_message_set_type(obj
->message
, type
);
1475 PHP_METHOD(HttpMessage
, getInfo
)
1477 if (SUCCESS
== zend_parse_parameters_none()) {
1478 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1480 if (!obj
->message
) {
1481 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1484 switch (obj
->message
->type
) {
1485 case PHP_HTTP_REQUEST
:
1486 Z_STRLEN_P(return_value
) = spprintf(&Z_STRVAL_P(return_value
), 0, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&obj
->message
->http
, ""));
1488 case PHP_HTTP_RESPONSE
:
1489 Z_STRLEN_P(return_value
) = spprintf(&Z_STRVAL_P(return_value
), 0, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&obj
->message
->http
, ""));
1495 Z_TYPE_P(return_value
) = IS_STRING
;
1501 PHP_METHOD(HttpMessage
, setInfo
)
1505 php_http_info_t inf
;
1507 if ( SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)
1508 && php_http_info_parse(&inf
, str TSRMLS_CC
)) {
1509 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1511 if (!obj
->message
) {
1512 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1515 php_http_message_set_info(obj
->message
, &inf
);
1516 php_http_info_dtor(&inf
);
1522 PHP_METHOD(HttpMessage
, getHttpVersion
)
1524 if (SUCCESS
== zend_parse_parameters_none()) {
1527 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1529 if (!obj
->message
) {
1530 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1533 php_http_version_to_string(&obj
->message
->http
.version
, &str
, &len
, NULL
, NULL TSRMLS_CC
);
1534 RETURN_STRINGL(str
, len
, 0);
1540 PHP_METHOD(HttpMessage
, setHttpVersion
)
1545 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &v_str
, &v_len
)) {
1546 php_http_version_t version
;
1547 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1549 if (!obj
->message
) {
1550 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1553 if (php_http_version_parse(&version
, v_str TSRMLS_CC
)) {
1554 obj
->message
->http
.version
= version
;
1561 PHP_METHOD(HttpMessage
, getResponseCode
)
1563 if (SUCCESS
== zend_parse_parameters_none()) {
1564 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1566 if (!obj
->message
) {
1567 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1570 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1571 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
1576 PHP_METHOD(HttpMessage
, setResponseCode
)
1579 zend_bool strict
= 1;
1581 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l|b", &code
, &strict
)) {
1582 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1584 if (!obj
->message
) {
1585 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1588 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1589 if (strict
&& (code
< 100 || code
> 599)) {
1590 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Invalid response code (100-599): %ld", code
);
1594 obj
->message
->http
.info
.response
.code
= code
;
1601 PHP_METHOD(HttpMessage
, getResponseStatus
)
1603 if (SUCCESS
== zend_parse_parameters_none()) {
1604 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1606 if (!obj
->message
) {
1607 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1610 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1611 if (obj
->message
->http
.info
.response
.status
) {
1612 RETURN_STRING(obj
->message
->http
.info
.response
.status
, 1);
1614 RETURN_EMPTY_STRING();
1621 PHP_METHOD(HttpMessage
, setResponseStatus
)
1626 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &status
, &status_len
)) {
1627 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1629 if (!obj
->message
) {
1630 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1633 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1634 STR_SET(obj
->message
->http
.info
.response
.status
, estrndup(status
, status_len
));
1641 PHP_METHOD(HttpMessage
, getRequestMethod
)
1643 if (SUCCESS
== zend_parse_parameters_none()) {
1644 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1646 if (!obj
->message
) {
1647 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1650 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1651 if (obj
->message
->http
.info
.request
.method
) {
1652 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
1654 RETURN_EMPTY_STRING();
1661 PHP_METHOD(HttpMessage
, setRequestMethod
)
1666 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1667 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1669 if (!obj
->message
) {
1670 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1673 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1674 if (method_len
< 1) {
1675 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
1679 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
1686 PHP_METHOD(HttpMessage
, getRequestUrl
)
1688 if (SUCCESS
== zend_parse_parameters_none()) {
1689 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1691 if (!obj
->message
) {
1692 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1695 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1696 if (obj
->message
->http
.info
.request
.url
) {
1697 RETURN_STRING(obj
->message
->http
.info
.request
.url
, 1);
1699 RETURN_EMPTY_STRING();
1706 PHP_METHOD(HttpMessage
, setRequestUrl
)
1711 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &url_str
, &url_len
)) {
1712 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1714 if (!obj
->message
) {
1715 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1718 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1720 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUrl to an empty string");
1723 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(url_str
, url_len
));
1731 PHP_METHOD(HttpMessage
, getParentMessage
)
1733 with_error_handling(EH_THROW
, PHP_HTTP_EX_CE(message
)) {
1734 if (SUCCESS
== zend_parse_parameters_none()) {
1735 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1737 if (!obj
->message
) {
1738 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1741 if (obj
->message
->parent
) {
1742 RETVAL_OBJVAL(obj
->parent
, 1);
1744 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "HttpMessage does not have a parent message");
1747 } end_error_handling();
1750 PHP_METHOD(HttpMessage
, toString
)
1752 zend_bool include_parent
= 0;
1754 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1755 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1759 if (!obj
->message
) {
1760 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1763 if (include_parent
) {
1764 php_http_message_serialize(obj
->message
, &string
, &length
);
1766 php_http_message_to_string(obj
->message
, &string
, &length
);
1769 RETURN_STRINGL(string
, length
, 0);
1772 RETURN_EMPTY_STRING();
1775 PHP_METHOD(HttpMessage
, serialize
)
1777 if (SUCCESS
== zend_parse_parameters_none()) {
1778 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1782 if (!obj
->message
) {
1783 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1786 php_http_message_serialize(obj
->message
, &string
, &length
);
1787 RETURN_STRINGL(string
, length
, 0);
1789 RETURN_EMPTY_STRING();
1792 PHP_METHOD(HttpMessage
, unserialize
)
1797 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &serialized
, &length
)) {
1798 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1799 php_http_message_t
*msg
;
1802 php_http_message_dtor(obj
->message
);
1803 efree(obj
->message
);
1805 if ((msg
= php_http_message_parse(NULL
, serialized
, (size_t) length TSRMLS_CC
))) {
1808 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1809 php_http_error(HE_ERROR
, PHP_HTTP_E_RUNTIME
, "Could not unserialize HttpMessage");
1814 PHP_METHOD(HttpMessage
, detach
)
1816 with_error_handling(EH_THROW
, PHP_HTTP_EX_CE(message
)) {
1817 if (SUCCESS
== zend_parse_parameters_none()) {
1818 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1820 if (!obj
->message
) {
1821 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1824 RETVAL_OBJVAL(php_http_message_object_new_ex(obj
->zo
.ce
, php_http_message_copy(obj
->message
, NULL
), NULL TSRMLS_CC
), 0);
1826 } end_error_handling();
1829 PHP_METHOD(HttpMessage
, prepend
)
1834 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O|b", &prepend
, php_http_message_class_entry
, &top
)) {
1835 php_http_message_t
*msg
[2];
1836 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1837 php_http_message_object_t
*prepend_obj
= zend_object_store_get_object(prepend TSRMLS_CC
);
1839 if (!obj
->message
) {
1840 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1842 if (!prepend_obj
->message
) {
1843 prepend_obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1847 for (msg
[0] = obj
->message
; msg
[0]; msg
[0] = msg
[0]->parent
) {
1848 for (msg
[1] = prepend_obj
->message
; msg
[1]; msg
[1] = msg
[1]->parent
) {
1849 if (msg
[0] == msg
[1]) {
1850 php_http_error(HE_THROW
, PHP_HTTP_E_INVALID_PARAM
, "Cannot prepend a message located within the same message chain");
1856 php_http_message_object_prepend(getThis(), prepend
, top TSRMLS_CC
);
1860 PHP_METHOD(HttpMessage
, reverse
)
1862 if (SUCCESS
== zend_parse_parameters_none()) {
1863 php_http_message_object_reverse(getThis(), return_value TSRMLS_CC
);
1867 PHP_METHOD(HttpMessage
, count
)
1869 if (SUCCESS
== zend_parse_parameters_none()) {
1871 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1873 if (!obj
->message
) {
1874 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1877 php_http_message_count(i
, obj
->message
);
1883 PHP_METHOD(HttpMessage
, rewind
)
1885 if (SUCCESS
== zend_parse_parameters_none()) {
1886 zval
*zobj
= getThis();
1887 php_http_message_object_t
*obj
= zend_object_store_get_object(zobj TSRMLS_CC
);
1889 if (obj
->iterator
) {
1890 zval_ptr_dtor(&obj
->iterator
);
1893 obj
->iterator
= zobj
;
1897 PHP_METHOD(HttpMessage
, valid
)
1899 if (SUCCESS
== zend_parse_parameters_none()) {
1900 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1902 RETURN_BOOL(obj
->iterator
!= NULL
);
1906 PHP_METHOD(HttpMessage
, next
)
1908 if (SUCCESS
== zend_parse_parameters_none()) {
1909 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1911 if (obj
->iterator
) {
1912 php_http_message_object_t
*itr
= zend_object_store_get_object(obj
->iterator TSRMLS_CC
);
1914 if (itr
&& itr
->parent
.handle
) {
1915 zval
*old
= obj
->iterator
;
1916 MAKE_STD_ZVAL(obj
->iterator
);
1917 ZVAL_OBJVAL(obj
->iterator
, itr
->parent
, 1);
1918 zval_ptr_dtor(&old
);
1920 zval_ptr_dtor(&obj
->iterator
);
1921 obj
->iterator
= NULL
;
1927 PHP_METHOD(HttpMessage
, key
)
1929 if (SUCCESS
== zend_parse_parameters_none()) {
1930 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1932 RETURN_LONG(obj
->iterator
? obj
->iterator
->value
.obj
.handle
:0);
1936 PHP_METHOD(HttpMessage
, current
)
1938 if (SUCCESS
== zend_parse_parameters_none()) {
1939 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1941 if (obj
->iterator
) {
1942 RETURN_ZVAL(obj
->iterator
, 1, 0);
1952 * vim600: noet sw=4 ts=4 fdm=marker
1953 * vim<600: noet sw=4 ts=4