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 php_http_buffer_appendf(&str
, PHP_HTTP_BUFFER_LEN(&str
) ? ", %s":"%s", Z_STRVAL_PP(val
));
163 php_http_buffer_fix(&str
);
164 ZVAL_STRINGL(header_str
, PHP_HTTP_BUFFER_VAL(&str
), PHP_HTTP_BUFFER_LEN(&str
), 0);
167 ret
= php_http_zsep(IS_STRING
, *header
);
178 PHP_HTTP_API
void php_http_message_set_type(php_http_message_t
*message
, php_http_message_type_t type
)
180 /* just act if different */
181 if (type
!= message
->type
) {
183 /* free request info */
184 switch (message
->type
= type
) {
185 case PHP_HTTP_REQUEST
:
186 STR_FREE(message
->http
.info
.request
.method
);
187 STR_FREE(message
->http
.info
.request
.url
);
190 case PHP_HTTP_RESPONSE
:
191 STR_FREE(message
->http
.info
.response
.status
);
198 memset(&message
->http
, 0, sizeof(message
->http
));
202 PHP_HTTP_API
void php_http_message_set_info(php_http_message_t
*message
, php_http_info_t
*info
)
204 php_http_message_set_type(message
, info
->type
);
205 message
->http
.version
= info
->http
.version
;
206 switch (message
->type
) {
207 case PHP_HTTP_REQUEST
:
208 STR_SET(PHP_HTTP_INFO(message
).request
.url
, PHP_HTTP_INFO(info
).request
.url
? estrdup(PHP_HTTP_INFO(info
).request
.url
) : NULL
);
209 STR_SET(PHP_HTTP_INFO(message
).request
.method
, PHP_HTTP_INFO(info
).request
.method
? estrdup(PHP_HTTP_INFO(info
).request
.method
) : NULL
);
212 case PHP_HTTP_RESPONSE
:
213 PHP_HTTP_INFO(message
).response
.code
= PHP_HTTP_INFO(info
).response
.code
;
214 STR_SET(PHP_HTTP_INFO(message
).response
.status
, PHP_HTTP_INFO(info
).response
.status
? estrdup(PHP_HTTP_INFO(info
).response
.status
) : NULL
);
222 static inline void message_headers(php_http_message_t
*msg
, php_http_buffer_t
*str
)
224 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
229 case PHP_HTTP_REQUEST
:
230 php_http_buffer_appendf(str
, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&msg
->http
, PHP_HTTP_CRLF
));
233 case PHP_HTTP_RESPONSE
:
234 php_http_buffer_appendf(str
, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&msg
->http
, PHP_HTTP_CRLF
));
241 FOREACH_HASH_KEYVAL(pos1
, &msg
->hdrs
, key
, header
) {
242 if (key
.type
== HASH_KEY_IS_STRING
) {
244 zval
**single_header
;
246 switch (Z_TYPE_PP(header
)) {
248 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_BVAL_PP(header
)?"true":"false");
252 php_http_buffer_appendf(str
, "%s: %ld" PHP_HTTP_CRLF
, key
.str
, Z_LVAL_PP(header
));
256 php_http_buffer_appendf(str
, "%s: %F" PHP_HTTP_CRLF
, key
.str
, Z_DVAL_PP(header
));
260 if (Z_STRVAL_PP(header
)[Z_STRLEN_PP(header
)-1] == '\r') fprintf(stderr
, "DOH!\n");
261 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_STRVAL_PP(header
));
265 FOREACH_VAL(pos2
, *header
, single_header
) {
266 switch (Z_TYPE_PP(single_header
)) {
268 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_BVAL_PP(single_header
)?"true":"false");
272 php_http_buffer_appendf(str
, "%s: %ld" PHP_HTTP_CRLF
, key
.str
, Z_LVAL_PP(single_header
));
276 php_http_buffer_appendf(str
, "%s: %F" PHP_HTTP_CRLF
, key
.str
, Z_DVAL_PP(single_header
));
280 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_STRVAL_PP(single_header
));
290 PHP_HTTP_API
void php_http_message_to_callback(php_http_message_t
*msg
, php_http_pass_callback_t cb
, void *cb_arg
)
292 php_http_buffer_t str
;
293 TSRMLS_FETCH_FROM_CTX(msg
->ts
);
295 php_http_buffer_init_ex(&str
, 0x1000, 0);
296 message_headers(msg
, &str
);
297 cb(cb_arg
, PHP_HTTP_BUFFER_VAL(&str
), PHP_HTTP_BUFFER_LEN(&str
));
298 php_http_buffer_dtor(&str
);
300 if (php_http_message_body_size(&msg
->body
)) {
301 cb(cb_arg
, ZEND_STRL(PHP_HTTP_CRLF
) TSRMLS_CC
);
302 php_http_message_body_to_callback(&msg
->body
, cb
, cb_arg
, 0, 0);
303 cb(cb_arg
, ZEND_STRL(PHP_HTTP_CRLF
) TSRMLS_CC
);
307 PHP_HTTP_API
void php_http_message_to_string(php_http_message_t
*msg
, char **string
, size_t *length
)
309 php_http_buffer_t str
;
312 php_http_buffer_init_ex(&str
, 0x1000, 0);
313 message_headers(msg
, &str
);
314 if (php_http_message_body_size(&msg
->body
)) {
315 php_http_buffer_appends(&str
, PHP_HTTP_CRLF
);
316 php_http_message_body_to_callback(&msg
->body
, (php_http_pass_callback_t
) php_http_buffer_append
, &str
, 0, 0);
317 php_http_buffer_appends(&str
, PHP_HTTP_CRLF
);
320 data
= php_http_buffer_data(&str
, string
, length
);
325 php_http_buffer_dtor(&str
);
328 PHP_HTTP_API
void php_http_message_serialize(php_http_message_t
*message
, char **string
, size_t *length
)
332 php_http_buffer_t str
;
334 php_http_buffer_init(&str
);
337 php_http_message_to_string(message
, &buf
, &len
);
338 php_http_buffer_prepend(&str
, buf
, len
);
340 } while ((message
= message
->parent
));
342 buf
= php_http_buffer_data(&str
, string
, length
);
347 php_http_buffer_dtor(&str
);
350 PHP_HTTP_API php_http_message_t
*php_http_message_reverse(php_http_message_t
*msg
)
354 php_http_message_count(c
, msg
);
357 php_http_message_t
*tmp
= msg
, **arr
= ecalloc(c
, sizeof(**arr
));
359 for (i
= 0; i
< c
; ++i
) {
363 arr
[0]->parent
= NULL
;
364 for (i
= 0; i
< c
-1; ++i
) {
365 arr
[i
+1]->parent
= arr
[i
];
375 PHP_HTTP_API php_http_message_t
*php_http_message_interconnect(php_http_message_t
*m1
, php_http_message_t
*m2
)
378 int i
= 0, c1
= 0, c2
= 0;
379 php_http_message_t
*t1
= m1
, *t2
= m2
, *p1
, *p2
;
381 php_http_message_count(c1
, m1
);
382 php_http_message_count(c2
, m2
);
384 while (i
++ < (c1
- c2
)) {
395 } else if (!m1
&& m2
) {
401 PHP_HTTP_API
void php_http_message_to_struct(php_http_message_t
*msg
, zval
*obj TSRMLS_DC
)
407 INIT_PZVAL_ARRAY(&strct
, HASH_OF(obj
));
409 add_assoc_long(&strct
, "type", msg
->type
);
410 spprintf(&version
, 0 ,"%u.%u", msg
->http
.version
.major
, msg
->http
.version
.minor
);
411 add_assoc_string_ex(&strct
, ZEND_STRL("httpVersion"), version
, 0);
414 case PHP_HTTP_RESPONSE
:
415 add_assoc_long(&strct
, "responseCode", msg
->http
.info
.response
.code
);
416 add_assoc_string(&strct
, "responseStatus", STR_PTR(msg
->http
.info
.response
.status
), 1);
419 case PHP_HTTP_REQUEST
:
420 add_assoc_string(&strct
, "requestMethod", STR_PTR(msg
->http
.info
.request
.method
), 1);
421 add_assoc_string(&strct
, "requestUrl", STR_PTR(msg
->http
.info
.request
.url
), 1);
425 /* avoid compiler warning */
429 MAKE_STD_ZVAL(headers
);
431 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
432 add_assoc_zval(&strct
, "headers", headers
);
434 add_assoc_stringl(&strct
, "body", PHP_HTTP_BUFFER_VAL(msg
), PHP_HTTP_BUFFER_LEN(msg
), 1);
439 MAKE_STD_ZVAL(parent
);
440 if (Z_TYPE_P(obj
) == IS_ARRAY
) {
445 add_assoc_zval(&strct
, "parentMessage", parent
);
446 php_http_message_to_struct(msg
->parent
, parent
);
448 add_assoc_null(&strct
, "parentMessage");
452 PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC)
456 switch (message->type) {
457 case PHP_HTTP_RESPONSE:
459 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
463 FOREACH_HASH_KEYVAL(pos, &message->hdrs, key, val) {
464 if (key.type == HASH_KEY_IS_STRING) {
465 http_send_header_zval_ex(key.str, key.len-1, val, 1);
468 rs = SUCCESS == http_send_status(message->http.info.response.code) &&
469 SUCCESS == http_send_data(PHP_HTTP_BUFFER_VAL(message), PHP_HTTP_BUFFER_LEN(message)) ?
474 case PHP_HTTP_REQUEST:
476 #ifdef PHP_HTTP_HAVE_CURL
478 http_request request;
479 zval **zhost, *options, *headers;
481 MAKE_STD_ZVAL(options);
482 MAKE_STD_ZVAL(headers);
485 zend_hash_copy(Z_ARRVAL_P(headers), &message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
486 add_assoc_zval(options, "headers", headers);
488 if (SUCCESS == zend_hash_find(&message->hdrs, "Host", sizeof("Host"), (void *) &zhost) && Z_TYPE_PP(zhost) == IS_STRING) {
490 php_url parts, *url = php_url_parse(message->http.info.request.url);
492 memset(&parts, 0, sizeof(php_url));
494 if ((colon = strchr(Z_STRVAL_PP(zhost), ':'))) {
495 parts.port = atoi(colon + 1);
496 parts.host = estrndup(Z_STRVAL_PP(zhost), (Z_STRVAL_PP(zhost) - colon - 1));
498 parts.host = estrndup(Z_STRVAL_PP(zhost), Z_STRLEN_PP(zhost));
501 http_build_url(PHP_HTTP_URL_REPLACE, url, &parts, NULL, &uri, NULL);
505 uri = http_absolute_url(message->http.info.request.url);
508 if ((request.meth = http_request_method_exists(1, 0, message->http.info.request.method))) {
509 http_request_body body;
511 http_request_init_ex(&request, NULL, request.meth, uri);
512 request.body = http_request_body_init_ex(&body, PHP_HTTP_REQUEST_BODY_CSTRING, PHP_HTTP_BUFFER_VAL(message), PHP_HTTP_BUFFER_LEN(message), 0);
513 if (SUCCESS == (rs = http_request_prepare(&request, Z_ARRVAL_P(options)))) {
514 http_request_exec(&request);
516 http_request_dtor(&request);
518 http_error_ex(HE_WARNING, PHP_HTTP_E_REQUEST_METHOD,
519 "Cannot send HttpMessage. Request method %s not supported",
520 message->http.info.request.method);
523 zval_ptr_dtor(&options);
525 http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "HTTP requests not supported - ext/http was not linked against libcurl.");
532 php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type PHP_HTTP_REQUEST nor PHP_HTTP_RESPONSE");
540 PHP_HTTP_API php_http_message_t
*php_http_message_copy(php_http_message_t
*from
, php_http_message_t
*to
)
542 php_http_message_t
*temp
, *copy
= NULL
;
543 php_http_info_t info
;
544 TSRMLS_FETCH_FROM_CTX(from
->ts
);
547 info
.type
= from
->type
;
548 info
.http
= from
->http
;
550 copy
= temp
= php_http_message_init(to
, 0);
551 php_http_message_set_info(temp
, &info
);
552 zend_hash_copy(&temp
->hdrs
, &from
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
553 php_http_message_body_copy(&from
->body
, &temp
->body
, 1);
555 while (from
->parent
) {
556 info
.type
= from
->parent
->type
;
557 info
.http
= from
->parent
->http
;
559 temp
->parent
= php_http_message_init(NULL
, 0);
560 php_http_message_set_info(temp
->parent
, &info
);
561 zend_hash_copy(&temp
->parent
->hdrs
, &from
->parent
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
562 php_http_message_body_copy(&from
->body
, &temp
->body
, 1);
572 PHP_HTTP_API
void php_http_message_dtor(php_http_message_t
*message
)
575 zend_hash_destroy(&message
->hdrs
);
576 php_http_message_body_dtor(&message
->body
);
578 switch (message
->type
) {
579 case PHP_HTTP_REQUEST
:
580 STR_SET(message
->http
.info
.request
.method
, NULL
);
581 STR_SET(message
->http
.info
.request
.url
, NULL
);
584 case PHP_HTTP_RESPONSE
:
585 STR_SET(message
->http
.info
.response
.status
, NULL
);
594 PHP_HTTP_API
void php_http_message_free(php_http_message_t
**message
)
597 if ((*message
)->parent
) {
598 php_http_message_free(&(*message
)->parent
);
600 php_http_message_dtor(*message
);
606 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpMessage, method, 0, req_args)
607 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpMessage, method, 0)
608 #define PHP_HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, PHP_HTTP_ARGS(HttpMessage, method), visibility)
610 PHP_HTTP_BEGIN_ARGS(__construct
, 0)
611 PHP_HTTP_ARG_VAL(message
, 0)
614 PHP_HTTP_EMPTY_ARGS(getBody
);
615 PHP_HTTP_BEGIN_ARGS(setBody
, 1)
616 PHP_HTTP_ARG_VAL(body
, 0)
619 PHP_HTTP_BEGIN_ARGS(getHeader
, 1)
620 PHP_HTTP_ARG_VAL(header
, 0)
623 PHP_HTTP_BEGIN_ARGS(setHeader
, 1)
624 PHP_HTTP_ARG_VAL(header
, 0)
625 PHP_HTTP_ARG_VAL(value
, 0)
628 PHP_HTTP_BEGIN_ARGS(addHeader
, 2)
629 PHP_HTTP_ARG_VAL(header
, 0)
630 PHP_HTTP_ARG_VAL(value
, 0)
633 PHP_HTTP_EMPTY_ARGS(getHeaders
);
634 PHP_HTTP_BEGIN_ARGS(setHeaders
, 1)
635 PHP_HTTP_ARG_VAL(headers
, 0)
638 PHP_HTTP_BEGIN_ARGS(addHeaders
, 1)
639 PHP_HTTP_ARG_VAL(headers
, 0)
640 PHP_HTTP_ARG_VAL(append
, 0)
643 PHP_HTTP_EMPTY_ARGS(getType
);
644 PHP_HTTP_BEGIN_ARGS(setType
, 1)
645 PHP_HTTP_ARG_VAL(type
, 0)
648 PHP_HTTP_EMPTY_ARGS(getInfo
);
649 PHP_HTTP_BEGIN_ARGS(setInfo
, 1)
650 PHP_HTTP_ARG_VAL(http_info
, 0)
653 PHP_HTTP_EMPTY_ARGS(getResponseCode
);
654 PHP_HTTP_BEGIN_ARGS(setResponseCode
, 1)
655 PHP_HTTP_ARG_VAL(response_code
, 0)
658 PHP_HTTP_EMPTY_ARGS(getResponseStatus
);
659 PHP_HTTP_BEGIN_ARGS(setResponseStatus
, 1)
660 PHP_HTTP_ARG_VAL(response_status
, 0)
663 PHP_HTTP_EMPTY_ARGS(getRequestMethod
);
664 PHP_HTTP_BEGIN_ARGS(setRequestMethod
, 1)
665 PHP_HTTP_ARG_VAL(request_method
, 0)
668 PHP_HTTP_EMPTY_ARGS(getRequestUrl
);
669 PHP_HTTP_BEGIN_ARGS(setRequestUrl
, 1)
670 PHP_HTTP_ARG_VAL(url
, 0)
673 PHP_HTTP_EMPTY_ARGS(getHttpVersion
);
674 PHP_HTTP_BEGIN_ARGS(setHttpVersion
, 1)
675 PHP_HTTP_ARG_VAL(http_version
, 0)
678 PHP_HTTP_BEGIN_ARGS(guessContentType
, 1)
679 PHP_HTTP_ARG_VAL(magic_file
, 0)
680 PHP_HTTP_ARG_VAL(magic_mode
, 0)
683 PHP_HTTP_EMPTY_ARGS(getParentMessage
);
684 PHP_HTTP_EMPTY_ARGS(send
);
685 PHP_HTTP_EMPTY_ARGS(__toString
);
686 PHP_HTTP_BEGIN_ARGS(toString
, 0)
687 PHP_HTTP_ARG_VAL(include_parent
, 0)
690 PHP_HTTP_EMPTY_ARGS(toMessageTypeObject
);
692 PHP_HTTP_EMPTY_ARGS(count
);
694 PHP_HTTP_EMPTY_ARGS(serialize
);
695 PHP_HTTP_BEGIN_ARGS(unserialize
, 1)
696 PHP_HTTP_ARG_VAL(serialized
, 0)
699 PHP_HTTP_EMPTY_ARGS(rewind
);
700 PHP_HTTP_EMPTY_ARGS(valid
);
701 PHP_HTTP_EMPTY_ARGS(key
);
702 PHP_HTTP_EMPTY_ARGS(current
);
703 PHP_HTTP_EMPTY_ARGS(next
);
705 PHP_HTTP_EMPTY_ARGS(detach
);
706 PHP_HTTP_BEGIN_ARGS(prepend
, 1)
707 PHP_HTTP_ARG_OBJ(http
\\Message
, message
, 0)
709 PHP_HTTP_EMPTY_ARGS(reverse
);
711 static zval
*php_http_message_object_read_prop(zval
*object
, zval
*member
, int type
, const zend_literal
*literal_key TSRMLS_DC
);
712 static void php_http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value
, const zend_literal
*literal_key TSRMLS_DC
);
713 static zval
**php_http_message_object_get_prop_ptr(zval
*object
, zval
*member
, const zend_literal
*literal_key TSRMLS_DC
);
714 static HashTable
*php_http_message_object_get_props(zval
*object TSRMLS_DC
);
716 zend_class_entry
*php_http_message_class_entry
;
717 zend_function_entry php_http_message_method_entry
[] = {
718 PHP_HTTP_MESSAGE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
719 PHP_HTTP_MESSAGE_ME(getBody
, ZEND_ACC_PUBLIC
)
720 PHP_HTTP_MESSAGE_ME(setBody
, ZEND_ACC_PUBLIC
)
721 PHP_HTTP_MESSAGE_ME(getHeader
, ZEND_ACC_PUBLIC
)
722 PHP_HTTP_MESSAGE_ME(setHeader
, ZEND_ACC_PUBLIC
)
723 PHP_HTTP_MESSAGE_ME(addHeader
, ZEND_ACC_PUBLIC
)
724 PHP_HTTP_MESSAGE_ME(getHeaders
, ZEND_ACC_PUBLIC
)
725 PHP_HTTP_MESSAGE_ME(setHeaders
, ZEND_ACC_PUBLIC
)
726 PHP_HTTP_MESSAGE_ME(addHeaders
, ZEND_ACC_PUBLIC
)
727 PHP_HTTP_MESSAGE_ME(getType
, ZEND_ACC_PUBLIC
)
728 PHP_HTTP_MESSAGE_ME(setType
, ZEND_ACC_PUBLIC
)
729 PHP_HTTP_MESSAGE_ME(getInfo
, ZEND_ACC_PUBLIC
)
730 PHP_HTTP_MESSAGE_ME(setInfo
, ZEND_ACC_PUBLIC
)
731 PHP_HTTP_MESSAGE_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
732 PHP_HTTP_MESSAGE_ME(setResponseCode
, ZEND_ACC_PUBLIC
)
733 PHP_HTTP_MESSAGE_ME(getResponseStatus
, ZEND_ACC_PUBLIC
)
734 PHP_HTTP_MESSAGE_ME(setResponseStatus
, ZEND_ACC_PUBLIC
)
735 PHP_HTTP_MESSAGE_ME(getRequestMethod
, ZEND_ACC_PUBLIC
)
736 PHP_HTTP_MESSAGE_ME(setRequestMethod
, ZEND_ACC_PUBLIC
)
737 PHP_HTTP_MESSAGE_ME(getRequestUrl
, ZEND_ACC_PUBLIC
)
738 PHP_HTTP_MESSAGE_ME(setRequestUrl
, ZEND_ACC_PUBLIC
)
739 PHP_HTTP_MESSAGE_ME(getHttpVersion
, ZEND_ACC_PUBLIC
)
740 PHP_HTTP_MESSAGE_ME(setHttpVersion
, ZEND_ACC_PUBLIC
)
741 PHP_HTTP_MESSAGE_ME(getParentMessage
, ZEND_ACC_PUBLIC
)
742 PHP_HTTP_MESSAGE_ME(toString
, ZEND_ACC_PUBLIC
)
744 /* implements Countable */
745 PHP_HTTP_MESSAGE_ME(count
, ZEND_ACC_PUBLIC
)
747 /* implements Serializable */
748 PHP_HTTP_MESSAGE_ME(serialize
, ZEND_ACC_PUBLIC
)
749 PHP_HTTP_MESSAGE_ME(unserialize
, ZEND_ACC_PUBLIC
)
751 /* implements Iterator */
752 PHP_HTTP_MESSAGE_ME(rewind
, ZEND_ACC_PUBLIC
)
753 PHP_HTTP_MESSAGE_ME(valid
, ZEND_ACC_PUBLIC
)
754 PHP_HTTP_MESSAGE_ME(current
, ZEND_ACC_PUBLIC
)
755 PHP_HTTP_MESSAGE_ME(key
, ZEND_ACC_PUBLIC
)
756 PHP_HTTP_MESSAGE_ME(next
, ZEND_ACC_PUBLIC
)
758 ZEND_MALIAS(HttpMessage
, __toString
, toString
, PHP_HTTP_ARGS(HttpMessage
, __toString
), ZEND_ACC_PUBLIC
)
760 PHP_HTTP_MESSAGE_ME(detach
, ZEND_ACC_PUBLIC
)
761 PHP_HTTP_MESSAGE_ME(prepend
, ZEND_ACC_PUBLIC
)
762 PHP_HTTP_MESSAGE_ME(reverse
, ZEND_ACC_PUBLIC
)
766 static zend_object_handlers php_http_message_object_handlers
;
767 static HashTable php_http_message_object_prophandlers
;
769 typedef void (*php_http_message_object_prophandler_func_t
)(php_http_message_object_t
*o
, zval
*v TSRMLS_DC
);
771 typedef struct php_http_message_object_prophandler
{
772 php_http_message_object_prophandler_func_t read
;
773 php_http_message_object_prophandler_func_t write
;
774 } php_http_message_object_prophandler_t
;
776 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
) {
777 php_http_message_object_prophandler_t h
= { read
, write
};
778 return zend_hash_add(&php_http_message_object_prophandlers
, prop_str
, prop_len
+ 1, (void *) &h
, sizeof(h
), NULL
);
780 static int php_http_message_object_has_prophandler(const char *prop_str
, size_t prop_len
) {
781 return zend_hash_exists(&php_http_message_object_prophandlers
, prop_str
, prop_len
+ 1);
783 static STATUS
php_http_message_object_get_prophandler(const char *prop_str
, size_t prop_len
, php_http_message_object_prophandler_t
**handler
) {
784 return zend_hash_find(&php_http_message_object_prophandlers
, prop_str
, prop_len
+ 1, (void *) handler
);
786 static void php_http_message_object_prophandler_get_type(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
787 RETVAL_LONG(obj
->message
->type
);
789 static void php_http_message_object_prophandler_set_type(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
790 zval
*cpy
= php_http_zsep(IS_LONG
, value
);
791 php_http_message_set_type(obj
->message
, Z_LVAL_P(cpy
));
794 static void php_http_message_object_prophandler_get_request_method(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
795 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
) && obj
->message
->http
.info
.request
.method
) {
796 RETVAL_STRING(obj
->message
->http
.info
.request
.method
, 1);
801 static void php_http_message_object_prophandler_set_request_method(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
802 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
803 zval
*cpy
= php_http_zsep(IS_STRING
, value
);
804 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
808 static void php_http_message_object_prophandler_get_request_url(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
809 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
) && obj
->message
->http
.info
.request
.url
) {
810 RETVAL_STRING(obj
->message
->http
.info
.request
.url
, 1);
815 static void php_http_message_object_prophandler_set_request_url(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
816 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
817 zval
*cpy
= php_http_zsep(IS_STRING
, value
);
818 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
822 static void php_http_message_object_prophandler_get_response_status(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
823 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
) && obj
->message
->http
.info
.response
.status
) {
824 RETVAL_STRING(obj
->message
->http
.info
.response
.status
, 1);
829 static void php_http_message_object_prophandler_set_response_status(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
830 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
831 zval
*cpy
= php_http_zsep(IS_STRING
, value
);
832 STR_SET(obj
->message
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
836 static void php_http_message_object_prophandler_get_response_code(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
837 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
838 RETVAL_LONG(obj
->message
->http
.info
.response
.code
);
843 static void php_http_message_object_prophandler_set_response_code(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
844 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
845 zval
*cpy
= php_http_zsep(IS_LONG
, value
);
846 obj
->message
->http
.info
.response
.code
= Z_LVAL_P(cpy
);
850 static void php_http_message_object_prophandler_get_http_version(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
854 php_http_version_to_string(&obj
->message
->http
.version
, &version_str
, &version_len
, NULL
, NULL TSRMLS_CC
);
855 RETVAL_STRINGL(version_str
, version_len
, 0);
857 static void php_http_message_object_prophandler_set_http_version(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
858 zval
*cpy
= php_http_zsep(IS_STRING
, value
);
859 php_http_version_parse(&obj
->message
->http
.version
, Z_STRVAL_P(cpy
) TSRMLS_CC
);
862 static void php_http_message_object_prophandler_get_headers(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
863 array_init(return_value
);
864 zend_hash_copy(Z_ARRVAL_P(return_value
), &obj
->message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
866 static void php_http_message_object_prophandler_set_headers(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
867 zval
*cpy
= php_http_zsep(IS_ARRAY
, value
);
869 zend_hash_clean(&obj
->message
->hdrs
);
870 zend_hash_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(cpy
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
873 static void php_http_message_object_prophandler_get_body(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
874 if (obj
->body
.handle
) {
875 RETVAL_OBJVAL(obj
->body
, 1);
880 static void php_http_message_object_prophandler_set_body(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
881 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), php_http_message_body_class_entry TSRMLS_CC
)) {
882 if (obj
->body
.handle
) {
883 zend_objects_store_del_ref_by_handle(obj
->body
.handle TSRMLS_CC
);
885 Z_OBJ_ADDREF_P(value
);
886 obj
->body
= Z_OBJVAL_P(value
);
889 static void php_http_message_object_prophandler_get_parent_message(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
890 if (obj
->message
->parent
) {
891 RETVAL_OBJVAL(obj
->parent
, 1);
896 static void php_http_message_object_prophandler_set_parent_message(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
897 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), php_http_message_class_entry TSRMLS_CC
)) {
898 if (obj
->message
->parent
) {
899 zend_objects_store_del_ref_by_handle(obj
->parent
.handle TSRMLS_CC
);
901 Z_OBJ_ADDREF_P(value
);
902 obj
->parent
= Z_OBJVAL_P(value
);
906 PHP_MINIT_FUNCTION(http_message
)
908 PHP_HTTP_REGISTER_CLASS(http
, Message
, http_message
, php_http_object_class_entry
, 0);
909 php_http_message_class_entry
->create_object
= php_http_message_object_new
;
910 memcpy(&php_http_message_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
911 php_http_message_object_handlers
.clone_obj
= php_http_message_object_clone
;
912 php_http_message_object_handlers
.read_property
= php_http_message_object_read_prop
;
913 php_http_message_object_handlers
.write_property
= php_http_message_object_write_prop
;
914 php_http_message_object_handlers
.get_properties
= php_http_message_object_get_props
;
915 php_http_message_object_handlers
.get_property_ptr_ptr
= php_http_message_object_get_prop_ptr
;
917 zend_class_implements(php_http_message_class_entry TSRMLS_CC
, 3, spl_ce_Countable
, zend_ce_serializable
, zend_ce_iterator
);
919 zend_hash_init(&php_http_message_object_prophandlers
, 9, NULL
, NULL
, 1);
920 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("type"), PHP_HTTP_NONE
, ZEND_ACC_PROTECTED TSRMLS_CC
);
921 php_http_message_object_add_prophandler(ZEND_STRL("type"), php_http_message_object_prophandler_get_type
, php_http_message_object_prophandler_set_type
);
922 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("body"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
923 php_http_message_object_add_prophandler(ZEND_STRL("body"), php_http_message_object_prophandler_get_body
, php_http_message_object_prophandler_set_body
);
924 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestMethod"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
925 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
);
926 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestUrl"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
927 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
);
928 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("responseStatus"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
929 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
);
930 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("responseCode"), 0, ZEND_ACC_PROTECTED TSRMLS_CC
);
931 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
);
932 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("httpVersion"), ZEND_ACC_PROTECTED TSRMLS_CC
);
933 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
);
934 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("headers"), ZEND_ACC_PROTECTED TSRMLS_CC
);
935 php_http_message_object_add_prophandler(ZEND_STRL("headers"), php_http_message_object_prophandler_get_headers
, php_http_message_object_prophandler_set_headers
);
936 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("parentMessage"), ZEND_ACC_PROTECTED TSRMLS_CC
);
937 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
);
939 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_NONE"), PHP_HTTP_NONE TSRMLS_CC
);
940 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_REQUEST"), PHP_HTTP_REQUEST TSRMLS_CC
);
941 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_RESPONSE"), PHP_HTTP_RESPONSE TSRMLS_CC
);
946 PHP_MSHUTDOWN_FUNCTION(http_message
)
948 zend_hash_destroy(&php_http_message_object_prophandlers
);
953 void php_http_message_object_reverse(zval
*this_ptr
, zval
*return_value TSRMLS_DC
)
956 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
959 php_http_message_count(i
, obj
->message
);
963 zend_object_value
*ovalues
= NULL
;
964 php_http_message_object_t
**objects
= NULL
;
967 objects
= ecalloc(i
, sizeof(**objects
));
968 ovalues
= ecalloc(i
, sizeof(*ovalues
));
970 /* we are the first message */
972 ovalues
[0] = getThis()->value
.obj
;
977 for (i
= 1; obj
->parent
.handle
; ++i
) {
978 o
.value
.obj
= obj
->parent
;
979 ovalues
[i
] = o
.value
.obj
;
980 objects
[i
] = obj
= zend_object_store_get_object(&o TSRMLS_CC
);
983 /* reorder parents */
984 for (last
= --i
; i
; --i
) {
985 objects
[i
]->message
->parent
= objects
[i
-1]->message
;
986 objects
[i
]->parent
= ovalues
[i
-1];
988 objects
[0]->message
->parent
= NULL
;
989 objects
[0]->parent
.handle
= 0;
990 objects
[0]->parent
.handlers
= NULL
;
993 Z_OBJ_ADDREF_P(getThis());
994 RETVAL_OBJVAL(ovalues
[last
], 1);
999 RETURN_ZVAL(getThis(), 1, 0);
1003 void php_http_message_object_prepend(zval
*this_ptr
, zval
*prepend
, zend_bool top TSRMLS_DC
)
1006 php_http_message_t
*save_parent_msg
= NULL
;
1007 zend_object_value save_parent_obj
= {0, NULL
};
1008 php_http_message_object_t
*obj
= zend_object_store_get_object(this_ptr TSRMLS_CC
);
1009 php_http_message_object_t
*prepend_obj
= zend_object_store_get_object(prepend TSRMLS_CC
);
1015 save_parent_obj
= obj
->parent
;
1016 save_parent_msg
= obj
->message
->parent
;
1018 /* iterate to the most parent object */
1019 while (obj
->parent
.handle
) {
1020 m
.value
.obj
= obj
->parent
;
1021 obj
= zend_object_store_get_object(&m TSRMLS_CC
);
1026 obj
->parent
= prepend
->value
.obj
;
1027 obj
->message
->parent
= prepend_obj
->message
;
1030 zend_objects_store_add_ref(prepend TSRMLS_CC
);
1031 while (prepend_obj
->parent
.handle
) {
1032 m
.value
.obj
= prepend_obj
->parent
;
1033 zend_objects_store_add_ref(&m TSRMLS_CC
);
1034 prepend_obj
= zend_object_store_get_object(&m TSRMLS_CC
);
1038 prepend_obj
->parent
= save_parent_obj
;
1039 prepend_obj
->message
->parent
= save_parent_msg
;
1043 zend_object_value
php_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
1045 return php_http_message_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
1048 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
)
1050 zend_object_value ov
;
1051 php_http_message_object_t
*o
;
1053 o
= ecalloc(1, sizeof(php_http_message_object_t
));
1054 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
1055 object_properties_init((zend_object
*) o
, ce
);
1064 o
->parent
= php_http_message_object_new_ex(ce
, msg
->parent
, NULL TSRMLS_CC
);
1066 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
);
1069 ov
.handle
= zend_objects_store_put((zend_object
*) o
, NULL
, php_http_message_object_free
, NULL TSRMLS_CC
);
1070 ov
.handlers
= &php_http_message_object_handlers
;
1075 zend_object_value
php_http_message_object_clone(zval
*this_ptr TSRMLS_DC
)
1077 zend_object_value new_ov
;
1078 php_http_message_object_t
*new_obj
= NULL
;
1079 php_http_message_object_t
*old_obj
= zend_object_store_get_object(this_ptr TSRMLS_CC
);
1081 new_ov
= php_http_message_object_new_ex(old_obj
->zo
.ce
, php_http_message_copy(old_obj
->message
, NULL
), &new_obj
);
1082 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
1087 void php_http_message_object_free(void *object TSRMLS_DC
)
1089 php_http_message_object_t
*o
= (php_http_message_object_t
*) object
;
1092 zval_ptr_dtor(&o
->iterator
);
1096 /* do NOT free recursivly */
1097 php_http_message_dtor(o
->message
);
1101 if (o
->parent
.handle
) {
1102 zend_objects_store_del_ref_by_handle(o
->parent
.handle TSRMLS_CC
);
1104 if (o
->body
.handle
) {
1105 zend_objects_store_del_ref_by_handle(o
->body
.handle TSRMLS_CC
);
1107 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
1112 static zval
**php_http_message_object_get_prop_ptr(zval
*object
, zval
*member
, const zend_literal
*literal_key TSRMLS_DC
) {
1113 php_http_message_object_prophandler_t
*handler
;
1114 zval
*copy
= php_http_zsep(IS_STRING
, member
);
1116 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1117 zval_ptr_dtor(©
);
1118 return &php_http_property_proxy_init(NULL
, object
, member TSRMLS_CC
)->myself
;
1120 zval_ptr_dtor(©
);
1122 return zend_get_std_object_handlers()->get_property_ptr_ptr(object
, member
, literal_key TSRMLS_CC
);
1125 static zval
*php_http_message_object_read_prop(zval
*object
, zval
*member
, int type
, const zend_literal
*literal_key TSRMLS_DC
)
1127 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1128 php_http_message_object_prophandler_t
*handler
;
1129 zval
*return_value
, *copy
= php_http_zsep(IS_STRING
, member
);
1131 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1132 if (type
== BP_VAR_W
) {
1133 zval_ptr_dtor(©
);
1134 zend_error(E_ERROR
, "Cannot access HttpMessage properties by reference or array key/index");
1138 ALLOC_ZVAL(return_value
);
1139 Z_SET_REFCOUNT_P(return_value
, 0);
1140 Z_UNSET_ISREF_P(return_value
);
1142 handler
->read(obj
, return_value TSRMLS_CC
);
1145 return_value
= zend_get_std_object_handlers()->read_property(object
, member
, type
, literal_key TSRMLS_CC
);
1148 zval_ptr_dtor(©
);
1150 return return_value
;
1153 static void php_http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value
, const zend_literal
*literal_key TSRMLS_DC
)
1155 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1156 php_http_message_object_prophandler_t
*handler
;
1157 zval
*copy
= php_http_zsep(IS_STRING
, member
);
1159 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1160 handler
->write(obj
, value TSRMLS_CC
);
1162 zend_get_std_object_handlers()->write_property(object
, member
, value
, literal_key TSRMLS_CC
);
1165 zval_ptr_dtor(©
);
1169 static HashTable
*php_http_message_object_get_props(zval
*object TSRMLS_DC
)
1172 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1173 php_http_message_t
*msg
= obj
->message
;
1174 HashTable
*props
= zend_get_std_object_handlers()->get_properties(object TSRMLS_CC
);
1175 zval array
, *parent
, *body
;
1178 INIT_PZVAL_ARRAY(&array
, props
);
1180 #define ASSOC_PROP(array, ptype, name, val) \
1182 char *m_prop_name; \
1184 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
1185 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
1186 efree(m_prop_name); \
1188 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
1189 #define ASSOC_STRINGL(array, name, val, len) ASSOC_STRINGL_EX(array, name, val, len, 1)
1190 #define ASSOC_STRINGL_EX(array, name, val, len, cpy) \
1192 char *m_prop_name; \
1194 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
1195 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, cpy); \
1196 efree(m_prop_name); \
1199 ASSOC_PROP(array
, long, "type", msg
->type
);
1200 ASSOC_STRINGL_EX(array
, "httpVersion", version
, spprintf(&version
, 0, "%u.%u", msg
->http
.version
.major
, msg
->http
.version
.minor
), 0);
1202 switch (msg
->type
) {
1203 case PHP_HTTP_REQUEST
:
1204 ASSOC_PROP(array
, long, "responseCode", 0);
1205 ASSOC_STRINGL(array
, "responseStatus", "", 0);
1206 ASSOC_STRING(array
, "requestMethod", STR_PTR(msg
->http
.info
.request
.method
));
1207 ASSOC_STRING(array
, "requestUrl", STR_PTR(msg
->http
.info
.request
.url
));
1210 case PHP_HTTP_RESPONSE
:
1211 ASSOC_PROP(array
, long, "responseCode", msg
->http
.info
.response
.code
);
1212 ASSOC_STRING(array
, "responseStatus", STR_PTR(msg
->http
.info
.response
.status
));
1213 ASSOC_STRINGL(array
, "requestMethod", "", 0);
1214 ASSOC_STRINGL(array
, "requestUrl", "", 0);
1219 ASSOC_PROP(array
, long, "responseCode", 0);
1220 ASSOC_STRINGL(array
, "responseStatus", "", 0);
1221 ASSOC_STRINGL(array
, "requestMethod", "", 0);
1222 ASSOC_STRINGL(array
, "requestUrl", "", 0);
1226 MAKE_STD_ZVAL(headers
);
1227 array_init(headers
);
1228 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1229 ASSOC_PROP(array
, zval
, "headers", headers
);
1231 MAKE_STD_ZVAL(body
);
1232 if (!obj
->body
.handle
) {
1233 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
);
1235 ZVAL_OBJVAL(body
, obj
->body
, 1);
1236 ASSOC_PROP(array
, zval
, "body", body
);
1238 MAKE_STD_ZVAL(parent
);
1240 ZVAL_OBJVAL(parent
, obj
->parent
, 1);
1244 ASSOC_PROP(array
, zval
, "parentMessage", parent
);
1251 PHP_METHOD(HttpMessage
, __construct
)
1254 char *message
= NULL
;
1255 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1257 with_error_handling(EH_THROW
, PHP_HTTP_EX_CE(runtime
)) {
1258 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
1259 php_http_message_t
*msg
= obj
->message
;
1261 php_http_message_dtor(msg
);
1262 with_error_handling(EH_THROW
, PHP_HTTP_EX_CE(message
)) {
1263 if ((obj
->message
= php_http_message_parse(msg
, message
, length
))) {
1264 if (obj
->message
->parent
) {
1265 obj
->parent
= php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj
->message
->parent
, NULL TSRMLS_CC
);
1268 obj
->message
= php_http_message_init(msg
, 0 TSRMLS_CC
);
1270 } end_error_handling();
1272 if (!obj
->message
) {
1273 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1275 } end_error_handling();
1279 PHP_METHOD(HttpMessage
, getBody
)
1281 with_error_handling(EH_THROW
, PHP_HTTP_EX_CE(message
)) {
1282 if (SUCCESS
== zend_parse_parameters_none()) {
1283 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1285 if (!obj
->message
) {
1286 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1289 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
)) {
1290 RETVAL_OBJVAL(obj
->body
, 1);
1293 } end_error_handling();
1296 PHP_METHOD(HttpMessage
, setBody
)
1300 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &zbody
, php_http_message_body_class_entry
)) {
1301 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1302 php_http_message_body_object_t
*body_obj
= zend_object_store_get_object(zbody TSRMLS_CC
);
1304 if (!obj
->message
) {
1305 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1308 php_http_message_body_dtor(&obj
->message
->body
);
1309 php_http_message_body_init(&obj
->message
->body
, php_http_message_body_stream(body_obj
->body
));
1310 Z_OBJ_ADDREF_P(zbody
);
1311 obj
->body
= Z_OBJVAL_P(zbody
);
1317 PHP_METHOD(HttpMessage
, getHeader
)
1322 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header_str
, &header_len
)) {
1323 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1326 if (!obj
->message
) {
1327 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1330 if ((header
= php_http_message_header(obj
->message
, header_str
, header_len
, 0))) {
1331 RETURN_ZVAL(header
, 1, 1);
1337 PHP_METHOD(HttpMessage
, getHeaders
)
1339 if (SUCCESS
== zend_parse_parameters_none()) {
1340 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1342 if (!obj
->message
) {
1343 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1346 array_init(return_value
);
1347 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(return_value
));
1351 PHP_METHOD(HttpMessage
, setHeader
)
1353 zval
*zvalue
= NULL
;
1357 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|z!", &name_str
, &name_len
, &zvalue
)) {
1358 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1359 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1361 if (!obj
->message
) {
1362 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1366 RETVAL_SUCCESS(zend_hash_del(&obj
->message
->hdrs
, name
, name_len
+ 1));
1369 RETVAL_SUCCESS(zend_hash_update(&obj
->message
->hdrs
, name
, name_len
+ 1, &zvalue
, sizeof(void *), NULL
));
1377 PHP_METHOD(HttpMessage
, setHeaders
)
1379 zval
*new_headers
= NULL
;
1381 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &new_headers
)) {
1382 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1384 if (!obj
->message
) {
1385 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1388 zend_hash_clean(&obj
->message
->hdrs
);
1390 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, 0, ARRAY_JOIN_PRETTIFY
|ARRAY_JOIN_STRONLY
);
1397 PHP_METHOD(HttpMessage
, addHeader
)
1403 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sz", &name_str
, &name_len
, &zvalue
)) {
1404 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1405 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1408 if (!obj
->message
) {
1409 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1413 if ((header
= php_http_message_header(obj
->message
, name
, name_len
, 0))) {
1414 convert_to_array(header
);
1415 RETVAL_SUCCESS(zend_hash_next_index_insert(Z_ARRVAL_P(header
), &zvalue
, sizeof(void *), NULL
));
1417 RETVAL_SUCCESS(zend_hash_update(&obj
->message
->hdrs
, name
, name_len
+ 1, &zvalue
, sizeof(void *), NULL
));
1425 PHP_METHOD(HttpMessage
, addHeaders
)
1428 zend_bool append
= 0;
1430 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
1431 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1433 if (!obj
->message
) {
1434 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1437 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, append
, ARRAY_JOIN_STRONLY
|ARRAY_JOIN_PRETTIFY
);
1443 PHP_METHOD(HttpMessage
, getType
)
1445 if (SUCCESS
== zend_parse_parameters_none()) {
1446 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1448 if (!obj
->message
) {
1449 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1452 RETURN_LONG(obj
->message
->type
);
1456 PHP_METHOD(HttpMessage
, setType
)
1460 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
1461 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1463 if (!obj
->message
) {
1464 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1467 php_http_message_set_type(obj
->message
, type
);
1473 PHP_METHOD(HttpMessage
, getInfo
)
1475 if (SUCCESS
== zend_parse_parameters_none()) {
1476 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1478 if (!obj
->message
) {
1479 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1482 switch (obj
->message
->type
) {
1483 case PHP_HTTP_REQUEST
:
1484 Z_STRLEN_P(return_value
) = spprintf(&Z_STRVAL_P(return_value
), 0, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&obj
->message
->http
, ""));
1486 case PHP_HTTP_RESPONSE
:
1487 Z_STRLEN_P(return_value
) = spprintf(&Z_STRVAL_P(return_value
), 0, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&obj
->message
->http
, ""));
1493 Z_TYPE_P(return_value
) = IS_STRING
;
1499 PHP_METHOD(HttpMessage
, setInfo
)
1503 php_http_info_t inf
;
1505 if ( SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)
1506 && php_http_info_parse(&inf
, str TSRMLS_CC
)) {
1507 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1509 if (!obj
->message
) {
1510 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1513 php_http_message_set_info(obj
->message
, &inf
);
1514 php_http_info_dtor(&inf
);
1520 PHP_METHOD(HttpMessage
, getHttpVersion
)
1522 if (SUCCESS
== zend_parse_parameters_none()) {
1525 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1527 if (!obj
->message
) {
1528 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1531 php_http_version_to_string(&obj
->message
->http
.version
, &str
, &len
, NULL
, NULL TSRMLS_CC
);
1532 RETURN_STRINGL(str
, len
, 0);
1538 PHP_METHOD(HttpMessage
, setHttpVersion
)
1543 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &v_str
, &v_len
)) {
1544 php_http_version_t version
;
1545 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1547 if (!obj
->message
) {
1548 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1551 if (php_http_version_parse(&version
, v_str TSRMLS_CC
)) {
1552 obj
->message
->http
.version
= version
;
1559 PHP_METHOD(HttpMessage
, getResponseCode
)
1561 if (SUCCESS
== zend_parse_parameters_none()) {
1562 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1564 if (!obj
->message
) {
1565 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1568 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1569 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
1574 PHP_METHOD(HttpMessage
, setResponseCode
)
1577 zend_bool strict
= 1;
1579 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l|b", &code
, &strict
)) {
1580 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1582 if (!obj
->message
) {
1583 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1586 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1587 if (strict
&& (code
< 100 || code
> 599)) {
1588 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Invalid response code (100-599): %ld", code
);
1592 obj
->message
->http
.info
.response
.code
= code
;
1599 PHP_METHOD(HttpMessage
, getResponseStatus
)
1601 if (SUCCESS
== zend_parse_parameters_none()) {
1602 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1604 if (!obj
->message
) {
1605 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1608 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1609 if (obj
->message
->http
.info
.response
.status
) {
1610 RETURN_STRING(obj
->message
->http
.info
.response
.status
, 1);
1612 RETURN_EMPTY_STRING();
1619 PHP_METHOD(HttpMessage
, setResponseStatus
)
1624 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &status
, &status_len
)) {
1625 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1627 if (!obj
->message
) {
1628 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1631 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1632 STR_SET(obj
->message
->http
.info
.response
.status
, estrndup(status
, status_len
));
1639 PHP_METHOD(HttpMessage
, getRequestMethod
)
1641 if (SUCCESS
== zend_parse_parameters_none()) {
1642 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1644 if (!obj
->message
) {
1645 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1648 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1649 if (obj
->message
->http
.info
.request
.method
) {
1650 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
1652 RETURN_EMPTY_STRING();
1659 PHP_METHOD(HttpMessage
, setRequestMethod
)
1664 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1665 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1667 if (!obj
->message
) {
1668 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1671 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1672 if (method_len
< 1) {
1673 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
1677 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
1684 PHP_METHOD(HttpMessage
, getRequestUrl
)
1686 if (SUCCESS
== zend_parse_parameters_none()) {
1687 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1689 if (!obj
->message
) {
1690 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1693 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1694 if (obj
->message
->http
.info
.request
.url
) {
1695 RETURN_STRING(obj
->message
->http
.info
.request
.url
, 1);
1697 RETURN_EMPTY_STRING();
1704 PHP_METHOD(HttpMessage
, setRequestUrl
)
1709 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &url_str
, &url_len
)) {
1710 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1712 if (!obj
->message
) {
1713 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1716 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1718 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUrl to an empty string");
1721 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(url_str
, url_len
));
1729 PHP_METHOD(HttpMessage
, getParentMessage
)
1731 with_error_handling(EH_THROW
, PHP_HTTP_EX_CE(message
)) {
1732 if (SUCCESS
== zend_parse_parameters_none()) {
1733 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1735 if (!obj
->message
) {
1736 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1739 if (obj
->message
->parent
) {
1740 RETVAL_OBJVAL(obj
->parent
, 1);
1742 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "HttpMessage does not have a parent message");
1745 } end_error_handling();
1748 PHP_METHOD(HttpMessage
, toString
)
1750 zend_bool include_parent
= 0;
1752 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1753 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1757 if (!obj
->message
) {
1758 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1761 if (include_parent
) {
1762 php_http_message_serialize(obj
->message
, &string
, &length
);
1764 php_http_message_to_string(obj
->message
, &string
, &length
);
1767 RETURN_STRINGL(string
, length
, 0);
1770 RETURN_EMPTY_STRING();
1773 PHP_METHOD(HttpMessage
, serialize
)
1775 if (SUCCESS
== zend_parse_parameters_none()) {
1776 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1780 if (!obj
->message
) {
1781 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1784 php_http_message_serialize(obj
->message
, &string
, &length
);
1785 RETURN_STRINGL(string
, length
, 0);
1787 RETURN_EMPTY_STRING();
1790 PHP_METHOD(HttpMessage
, unserialize
)
1795 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &serialized
, &length
)) {
1796 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1797 php_http_message_t
*msg
;
1800 php_http_message_dtor(obj
->message
);
1801 efree(obj
->message
);
1803 if ((msg
= php_http_message_parse(NULL
, serialized
, (size_t) length TSRMLS_CC
))) {
1806 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1807 php_http_error(HE_ERROR
, PHP_HTTP_E_RUNTIME
, "Could not unserialize HttpMessage");
1812 PHP_METHOD(HttpMessage
, detach
)
1814 with_error_handling(EH_THROW
, PHP_HTTP_EX_CE(message
)) {
1815 if (SUCCESS
== zend_parse_parameters_none()) {
1816 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1818 if (!obj
->message
) {
1819 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1822 RETVAL_OBJVAL(php_http_message_object_new_ex(obj
->zo
.ce
, php_http_message_copy(obj
->message
, NULL
), NULL TSRMLS_CC
), 0);
1824 } end_error_handling();
1827 PHP_METHOD(HttpMessage
, prepend
)
1832 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O|b", &prepend
, php_http_message_class_entry
, &top
)) {
1833 php_http_message_t
*msg
[2];
1834 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1835 php_http_message_object_t
*prepend_obj
= zend_object_store_get_object(prepend TSRMLS_CC
);
1837 if (!obj
->message
) {
1838 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1840 if (!prepend_obj
->message
) {
1841 prepend_obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1845 for (msg
[0] = obj
->message
; msg
[0]; msg
[0] = msg
[0]->parent
) {
1846 for (msg
[1] = prepend_obj
->message
; msg
[1]; msg
[1] = msg
[1]->parent
) {
1847 if (msg
[0] == msg
[1]) {
1848 php_http_error(HE_THROW
, PHP_HTTP_E_INVALID_PARAM
, "Cannot prepend a message located within the same message chain");
1854 php_http_message_object_prepend(getThis(), prepend
, top TSRMLS_CC
);
1858 PHP_METHOD(HttpMessage
, reverse
)
1860 if (SUCCESS
== zend_parse_parameters_none()) {
1861 php_http_message_object_reverse(getThis(), return_value TSRMLS_CC
);
1865 PHP_METHOD(HttpMessage
, count
)
1867 if (SUCCESS
== zend_parse_parameters_none()) {
1869 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1871 if (!obj
->message
) {
1872 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1875 php_http_message_count(i
, obj
->message
);
1881 PHP_METHOD(HttpMessage
, rewind
)
1883 if (SUCCESS
== zend_parse_parameters_none()) {
1884 zval
*zobj
= getThis();
1885 php_http_message_object_t
*obj
= zend_object_store_get_object(zobj TSRMLS_CC
);
1887 if (obj
->iterator
) {
1888 zval_ptr_dtor(&obj
->iterator
);
1891 obj
->iterator
= zobj
;
1895 PHP_METHOD(HttpMessage
, valid
)
1897 if (SUCCESS
== zend_parse_parameters_none()) {
1898 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1900 RETURN_BOOL(obj
->iterator
!= NULL
);
1904 PHP_METHOD(HttpMessage
, next
)
1906 if (SUCCESS
== zend_parse_parameters_none()) {
1907 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1909 if (obj
->iterator
) {
1910 php_http_message_object_t
*itr
= zend_object_store_get_object(obj
->iterator TSRMLS_CC
);
1912 if (itr
&& itr
->parent
.handle
) {
1913 zval
*old
= obj
->iterator
;
1914 MAKE_STD_ZVAL(obj
->iterator
);
1915 ZVAL_OBJVAL(obj
->iterator
, itr
->parent
, 1);
1916 zval_ptr_dtor(&old
);
1918 zval_ptr_dtor(&obj
->iterator
);
1919 obj
->iterator
= NULL
;
1925 PHP_METHOD(HttpMessage
, key
)
1927 if (SUCCESS
== zend_parse_parameters_none()) {
1928 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1930 RETURN_LONG(obj
->iterator
? obj
->iterator
->value
.obj
.handle
:0);
1934 PHP_METHOD(HttpMessage
, current
)
1936 if (SUCCESS
== zend_parse_parameters_none()) {
1937 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1939 if (obj
->iterator
) {
1940 RETURN_ZVAL(obj
->iterator
, 1, 0);
1950 * vim600: noet sw=4 ts=4 fdm=marker
1951 * vim<600: noet sw=4 ts=4