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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 PHP_HTTP_API zend_bool
php_http_message_info_callback(php_http_message_t
**message
, HashTable
**headers
, php_http_info_t
*info TSRMLS_DC
)
17 php_http_message_t
*old
= *message
;
20 if (old
->type
|| zend_hash_num_elements(&old
->hdrs
) || PHP_HTTP_BUFFER_LEN(old
)) {
21 (*message
) = php_http_message_init(NULL
, 0 TSRMLS_CC
);
22 (*message
)->parent
= old
;
23 (*headers
) = &((*message
)->hdrs
);
26 php_http_message_set_info(*message
, info
);
28 return old
!= *message
;
31 PHP_HTTP_API php_http_message_t
*php_http_message_init(php_http_message_t
*message
, php_http_message_type_t type TSRMLS_DC
)
34 message
= emalloc(sizeof(*message
));
36 memset(message
, 0, sizeof(*message
));
37 TSRMLS_SET_CTX(message
->ts
);
39 php_http_message_set_type(message
, type
);
40 zend_hash_init(&message
->hdrs
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
41 php_http_message_body_init(&message
->body
, NULL TSRMLS_CC
);
46 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
)
48 int free_msg
= !message
;
50 php_http_message_body_t
*mbody
;
52 message
= php_http_message_init(message
, type TSRMLS_CC
);
55 case PHP_HTTP_REQUEST
:
56 if ((sval
= php_http_env_get_server_var(ZEND_STRL("SERVER_PROTOCOL"), 1 TSRMLS_CC
)) && !strncmp(Z_STRVAL_P(sval
), "HTTP/", lenof("HTTP/"))) {
57 php_http_version_parse(&message
->http
.version
, Z_STRVAL_P(sval
) TSRMLS_CC
);
59 message
->http
.version
.major
= 1;
60 message
->http
.version
.minor
= 1;
62 if ((sval
= php_http_env_get_server_var(ZEND_STRL("REQUEST_METHOD"), 1 TSRMLS_CC
))) {
63 message
->http
.info
.request
.method
= estrdup(Z_STRVAL_P(sval
));
65 if ((sval
= php_http_env_get_server_var(ZEND_STRL("REQUEST_URI"), 1 TSRMLS_CC
))) {
66 message
->http
.info
.request
.url
= estrdup(Z_STRVAL_P(sval
));
69 php_http_env_get_request_headers(&message
->hdrs TSRMLS_CC
);
71 if ((mbody
= php_http_env_get_request_body(TSRMLS_C
))) {
72 php_http_message_body_dtor(&message
->body
);
73 php_http_message_body_copy(mbody
, &message
->body
, 0);
77 case PHP_HTTP_RESPONSE
:
78 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
)) {
79 message
->http
.version
.major
= 1;
80 message
->http
.version
.minor
= 1;
81 switch ((message
->http
.info
.response
.code
= SG(sapi_headers
).http_response_code
)) {
83 message
->http
.info
.response
.code
= 200;
85 message
->http
.info
.response
.status
= estrdup("Ok");
88 message
->http
.info
.response
.status
= estrdup("");
94 php_http_env_get_response_headers(&message
->hdrs TSRMLS_CC
);
96 if (php_output_get_level(TSRMLS_C
)) {
97 if (php_output_get_status(TSRMLS_C
) & PHP_OUTPUT_SENT
) {
98 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
));
100 } else if (SUCCESS
!= php_output_get_contents(&tval TSRMLS_CC
)) {
101 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "Could not fetch response body");
104 php_http_message_body_append(&message
->body
, Z_STRVAL(tval
), Z_STRLEN(tval
));
113 php_http_message_free(&message
);
123 PHP_HTTP_API php_http_message_t
*php_http_message_parse(php_http_message_t
*msg
, const char *str
, size_t len TSRMLS_DC
)
125 php_http_message_parser_t p
;
126 php_http_buffer_t buf
;
129 php_http_buffer_from_string_ex(&buf
, str
, len
);
130 php_http_message_parser_init(&p TSRMLS_CC
);
132 if ((free_msg
= !msg
)) {
133 msg
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
136 if (FAILURE
== php_http_message_parser_parse(&p
, &buf
, PHP_HTTP_MESSAGE_PARSER_CLEANUP
, &msg
)) {
138 php_http_message_free(&msg
);
143 php_http_message_parser_dtor(&p
);
144 php_http_buffer_dtor(&buf
);
149 PHP_HTTP_API zval
*php_http_message_header(php_http_message_t
*msg
, char *key_str
, size_t key_len
, int join
)
151 zval
*ret
= NULL
, **header
;
152 char *key
= php_http_pretty_key(estrndup(key_str
, key_len
), key_len
, 1, 1);
154 if (SUCCESS
== zend_symtable_find(&msg
->hdrs
, key
, key_len
+ 1, (void *) &header
)) {
155 if (join
&& Z_TYPE_PP(header
) == IS_ARRAY
) {
156 zval
*header_str
, **val
;
158 php_http_buffer_t str
;
159 TSRMLS_FETCH_FROM_CTX(msg
->ts
);
161 php_http_buffer_init(&str
);
162 MAKE_STD_ZVAL(header_str
);
163 FOREACH_VAL(pos
, *header
, val
) {
164 zval
*strval
= php_http_ztyp(IS_STRING
, *val
);
165 php_http_buffer_appendf(&str
, PHP_HTTP_BUFFER_LEN(&str
) ? ", %s":"%s", Z_STRVAL_P(strval
));
166 zval_ptr_dtor(&strval
);
168 php_http_buffer_fix(&str
);
169 ZVAL_STRINGL(header_str
, PHP_HTTP_BUFFER_VAL(&str
), PHP_HTTP_BUFFER_LEN(&str
), 0);
184 PHP_HTTP_API
void php_http_message_set_type(php_http_message_t
*message
, php_http_message_type_t type
)
186 /* just act if different */
187 if (type
!= message
->type
) {
189 /* free request info */
190 switch (message
->type
) {
191 case PHP_HTTP_REQUEST
:
192 STR_FREE(message
->http
.info
.request
.method
);
193 STR_FREE(message
->http
.info
.request
.url
);
196 case PHP_HTTP_RESPONSE
:
197 STR_FREE(message
->http
.info
.response
.status
);
204 message
->type
= type
;
205 memset(&message
->http
, 0, sizeof(message
->http
));
209 PHP_HTTP_API
void php_http_message_set_info(php_http_message_t
*message
, php_http_info_t
*info
)
211 php_http_message_set_type(message
, info
->type
);
212 message
->http
.version
= info
->http
.version
;
213 switch (message
->type
) {
214 case PHP_HTTP_REQUEST
:
215 STR_SET(PHP_HTTP_INFO(message
).request
.url
, PHP_HTTP_INFO(info
).request
.url
? estrdup(PHP_HTTP_INFO(info
).request
.url
) : NULL
);
216 STR_SET(PHP_HTTP_INFO(message
).request
.method
, PHP_HTTP_INFO(info
).request
.method
? estrdup(PHP_HTTP_INFO(info
).request
.method
) : NULL
);
219 case PHP_HTTP_RESPONSE
:
220 PHP_HTTP_INFO(message
).response
.code
= PHP_HTTP_INFO(info
).response
.code
;
221 STR_SET(PHP_HTTP_INFO(message
).response
.status
, PHP_HTTP_INFO(info
).response
.status
? estrdup(PHP_HTTP_INFO(info
).response
.status
) : NULL
);
229 static inline void message_headers(php_http_message_t
*msg
, php_http_buffer_t
*str
)
231 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
235 TSRMLS_FETCH_FROM_CTX(msg
->ts
);
238 case PHP_HTTP_REQUEST
:
239 php_http_buffer_appendf(str
, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&msg
->http
, PHP_HTTP_CRLF
));
242 case PHP_HTTP_RESPONSE
:
243 php_http_buffer_appendf(str
, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&msg
->http
, PHP_HTTP_CRLF
));
250 if ((size
= php_http_message_body_size(&msg
->body
))) {
253 zend_hash_update(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"), &h
, sizeof(zval
*), NULL
);
255 if (msg
->body
.boundary
) {
259 if (!(h
= php_http_message_header(msg
, ZEND_STRL("Content-Type"), 1))) {
260 len
= spprintf(&str
, 0, "multipart/form-data; boundary=\"%s\"", msg
->body
.boundary
);
262 ZVAL_STRINGL(h
, str
, len
, 0);
263 zend_hash_update(&msg
->hdrs
, "Content-Type", sizeof("Content-Type"), &h
, sizeof(zval
*), NULL
);
264 } else if (!php_http_match(Z_STRVAL_P(h
), "boundary=", PHP_HTTP_MATCH_WORD
)) {
266 Z_STRLEN_P(h
) = spprintf(&Z_STRVAL_P(h
), 0, "%s; boundary=\"%s\"", Z_STRVAL_P(h
), msg
->body
.boundary
);
267 zend_hash_update(&msg
->hdrs
, "Content-Type", sizeof("Content-Type"), &h
, sizeof(zval
*), NULL
);
274 FOREACH_HASH_KEYVAL(pos1
, &msg
->hdrs
, key
, header
) {
275 if (key
.type
== HASH_KEY_IS_STRING
) {
277 zval
**single_header
;
279 switch (Z_TYPE_PP(header
)) {
281 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_BVAL_PP(header
)?"true":"false");
285 php_http_buffer_appendf(str
, "%s: %ld" PHP_HTTP_CRLF
, key
.str
, Z_LVAL_PP(header
));
289 php_http_buffer_appendf(str
, "%s: %F" PHP_HTTP_CRLF
, key
.str
, Z_DVAL_PP(header
));
293 if (Z_STRVAL_PP(header
)[Z_STRLEN_PP(header
)-1] == '\r') fprintf(stderr
, "DOH!\n");
294 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_STRVAL_PP(header
));
298 FOREACH_VAL(pos2
, *header
, single_header
) {
299 switch (Z_TYPE_PP(single_header
)) {
301 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_BVAL_PP(single_header
)?"true":"false");
305 php_http_buffer_appendf(str
, "%s: %ld" PHP_HTTP_CRLF
, key
.str
, Z_LVAL_PP(single_header
));
309 php_http_buffer_appendf(str
, "%s: %F" PHP_HTTP_CRLF
, key
.str
, Z_DVAL_PP(single_header
));
313 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_STRVAL_PP(single_header
));
323 PHP_HTTP_API
void php_http_message_to_callback(php_http_message_t
*msg
, php_http_pass_callback_t cb
, void *cb_arg
)
325 php_http_buffer_t str
;
327 php_http_buffer_init_ex(&str
, 0x1000, 0);
328 message_headers(msg
, &str
);
329 cb(cb_arg
, PHP_HTTP_BUFFER_VAL(&str
), PHP_HTTP_BUFFER_LEN(&str
));
330 php_http_buffer_dtor(&str
);
332 if (php_http_message_body_size(&msg
->body
)) {
333 cb(cb_arg
, ZEND_STRL(PHP_HTTP_CRLF
));
334 php_http_message_body_to_callback(&msg
->body
, cb
, cb_arg
, 0, 0);
335 cb(cb_arg
, ZEND_STRL(PHP_HTTP_CRLF
));
339 PHP_HTTP_API
void php_http_message_to_string(php_http_message_t
*msg
, char **string
, size_t *length
)
341 php_http_buffer_t str
;
344 php_http_buffer_init_ex(&str
, 0x1000, 0);
345 message_headers(msg
, &str
);
346 if (php_http_message_body_size(&msg
->body
)) {
347 php_http_buffer_appends(&str
, PHP_HTTP_CRLF
);
348 php_http_message_body_to_callback(&msg
->body
, (php_http_pass_callback_t
) php_http_buffer_append
, &str
, 0, 0);
349 php_http_buffer_appends(&str
, PHP_HTTP_CRLF
);
352 data
= php_http_buffer_data(&str
, string
, length
);
357 php_http_buffer_dtor(&str
);
360 PHP_HTTP_API
void php_http_message_serialize(php_http_message_t
*message
, char **string
, size_t *length
)
364 php_http_buffer_t str
;
366 php_http_buffer_init(&str
);
369 php_http_message_to_string(message
, &buf
, &len
);
370 php_http_buffer_prepend(&str
, buf
, len
);
372 } while ((message
= message
->parent
));
374 buf
= php_http_buffer_data(&str
, string
, length
);
379 php_http_buffer_dtor(&str
);
382 PHP_HTTP_API php_http_message_t
*php_http_message_reverse(php_http_message_t
*msg
)
386 php_http_message_count(c
, msg
);
389 php_http_message_t
*tmp
= msg
, **arr
;
391 arr
= ecalloc(c
, sizeof(**arr
));
392 for (i
= 0; i
< c
; ++i
) {
396 arr
[0]->parent
= NULL
;
397 for (i
= 0; i
< c
-1; ++i
) {
398 arr
[i
+1]->parent
= arr
[i
];
408 PHP_HTTP_API php_http_message_t
*php_http_message_interconnect(php_http_message_t
*m1
, php_http_message_t
*m2
)
411 int i
= 0, c1
= 0, c2
= 0;
412 php_http_message_t
*t1
= m1
, *t2
= m2
, *p1
, *p2
;
414 php_http_message_count(c1
, m1
);
415 php_http_message_count(c2
, m2
);
417 while (i
++ < (c1
- c2
)) {
428 } else if (!m1
&& m2
) {
434 PHP_HTTP_API
void php_http_message_to_struct(php_http_message_t
*msg
, zval
*obj
)
439 TSRMLS_FETCH_FROM_CTX(msg
->ts
);
441 INIT_PZVAL_ARRAY(&strct
, HASH_OF(obj
));
443 add_assoc_long(&strct
, "type", msg
->type
);
444 spprintf(&version
, 0 ,"%u.%u", msg
->http
.version
.major
, msg
->http
.version
.minor
);
445 add_assoc_string_ex(&strct
, ZEND_STRL("httpVersion"), version
, 0);
448 case PHP_HTTP_RESPONSE
:
449 add_assoc_long(&strct
, "responseCode", msg
->http
.info
.response
.code
);
450 add_assoc_string(&strct
, "responseStatus", STR_PTR(msg
->http
.info
.response
.status
), 1);
453 case PHP_HTTP_REQUEST
:
454 add_assoc_string(&strct
, "requestMethod", STR_PTR(msg
->http
.info
.request
.method
), 1);
455 add_assoc_string(&strct
, "requestUrl", STR_PTR(msg
->http
.info
.request
.url
), 1);
459 /* avoid compiler warning */
463 MAKE_STD_ZVAL(headers
);
465 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
466 add_assoc_zval(&strct
, "headers", headers
);
468 add_assoc_stringl(&strct
, "body", PHP_HTTP_BUFFER_VAL(msg
), PHP_HTTP_BUFFER_LEN(msg
), 1);
473 MAKE_STD_ZVAL(parent
);
474 if (Z_TYPE_P(obj
) == IS_ARRAY
) {
479 add_assoc_zval(&strct
, "parentMessage", parent
);
480 php_http_message_to_struct(msg
->parent
, parent
);
482 add_assoc_null(&strct
, "parentMessage");
486 PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC)
490 switch (message->type) {
491 case PHP_HTTP_RESPONSE:
493 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
497 FOREACH_HASH_KEYVAL(pos, &message->hdrs, key, val) {
498 if (key.type == HASH_KEY_IS_STRING) {
499 http_send_header_zval_ex(key.str, key.len-1, val, 1);
502 rs = SUCCESS == http_send_status(message->http.info.response.code) &&
503 SUCCESS == http_send_data(PHP_HTTP_BUFFER_VAL(message), PHP_HTTP_BUFFER_LEN(message)) ?
508 case PHP_HTTP_REQUEST:
510 #ifdef PHP_HTTP_HAVE_CURL
512 http_request request;
513 zval **zhost, *options, *headers;
515 MAKE_STD_ZVAL(options);
516 MAKE_STD_ZVAL(headers);
519 zend_hash_copy(Z_ARRVAL_P(headers), &message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
520 add_assoc_zval(options, "headers", headers);
522 if (SUCCESS == zend_hash_find(&message->hdrs, "Host", sizeof("Host"), (void *) &zhost) && Z_TYPE_PP(zhost) == IS_STRING) {
524 php_url parts, *url = php_url_parse(message->http.info.request.url);
526 memset(&parts, 0, sizeof(php_url));
528 if ((colon = strchr(Z_STRVAL_PP(zhost), ':'))) {
529 parts.port = atoi(colon + 1);
530 parts.host = estrndup(Z_STRVAL_PP(zhost), (Z_STRVAL_PP(zhost) - colon - 1));
532 parts.host = estrndup(Z_STRVAL_PP(zhost), Z_STRLEN_PP(zhost));
535 http_build_url(PHP_HTTP_URL_REPLACE, url, &parts, NULL, &uri, NULL);
539 uri = http_absolute_url(message->http.info.request.url);
542 if ((request.meth = http_request_method_exists(1, 0, message->http.info.request.method))) {
543 http_request_body body;
545 http_request_init_ex(&request, NULL, request.meth, uri);
546 request.body = http_request_body_init_ex(&body, PHP_HTTP_REQUEST_BODY_CSTRING, PHP_HTTP_BUFFER_VAL(message), PHP_HTTP_BUFFER_LEN(message), 0);
547 if (SUCCESS == (rs = http_request_prepare(&request, Z_ARRVAL_P(options)))) {
548 http_request_exec(&request);
550 http_request_dtor(&request);
552 http_error_ex(HE_WARNING, PHP_HTTP_E_REQUEST_METHOD,
553 "Cannot send HttpMessage. Request method %s not supported",
554 message->http.info.request.method);
557 zval_ptr_dtor(&options);
559 http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "HTTP requests not supported - ext/http was not linked against libcurl.");
566 php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type PHP_HTTP_REQUEST nor PHP_HTTP_RESPONSE");
574 PHP_HTTP_API php_http_message_t
*php_http_message_copy_ex(php_http_message_t
*from
, php_http_message_t
*to
, zend_bool parents
)
576 php_http_message_t
*temp
, *copy
= NULL
;
577 php_http_info_t info
;
578 TSRMLS_FETCH_FROM_CTX(from
->ts
);
581 info
.type
= from
->type
;
582 info
.http
= from
->http
;
584 copy
= temp
= php_http_message_init(to
, 0 TSRMLS_CC
);
585 php_http_message_set_info(temp
, &info
);
586 zend_hash_copy(&temp
->hdrs
, &from
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
587 php_http_message_body_copy(&from
->body
, &temp
->body
, 1);
589 if (parents
) while (from
->parent
) {
590 info
.type
= from
->parent
->type
;
591 info
.http
= from
->parent
->http
;
593 temp
->parent
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
594 php_http_message_set_info(temp
->parent
, &info
);
595 zend_hash_copy(&temp
->parent
->hdrs
, &from
->parent
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
596 php_http_message_body_copy(&from
->parent
->body
, &temp
->parent
->body
, 1);
606 PHP_HTTP_API php_http_message_t
*php_http_message_copy(php_http_message_t
*from
, php_http_message_t
*to
)
608 return php_http_message_copy_ex(from
, to
, 1);
611 PHP_HTTP_API
void php_http_message_dtor(php_http_message_t
*message
)
614 zend_hash_destroy(&message
->hdrs
);
615 php_http_message_body_dtor(&message
->body
);
617 switch (message
->type
) {
618 case PHP_HTTP_REQUEST
:
619 STR_SET(message
->http
.info
.request
.method
, NULL
);
620 STR_SET(message
->http
.info
.request
.url
, NULL
);
623 case PHP_HTTP_RESPONSE
:
624 STR_SET(message
->http
.info
.response
.status
, NULL
);
633 PHP_HTTP_API
void php_http_message_free(php_http_message_t
**message
)
636 if ((*message
)->parent
) {
637 php_http_message_free(&(*message
)->parent
);
639 php_http_message_dtor(*message
);
645 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpMessage, method, 0, req_args)
646 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpMessage, method, 0)
647 #define PHP_HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, PHP_HTTP_ARGS(HttpMessage, method), visibility)
649 PHP_HTTP_BEGIN_ARGS(__construct
, 0)
650 PHP_HTTP_ARG_VAL(message
, 0)
653 PHP_HTTP_EMPTY_ARGS(getBody
);
654 PHP_HTTP_BEGIN_ARGS(setBody
, 1)
655 PHP_HTTP_ARG_VAL(body
, 0)
658 PHP_HTTP_BEGIN_ARGS(getHeader
, 1)
659 PHP_HTTP_ARG_VAL(header
, 0)
662 PHP_HTTP_BEGIN_ARGS(setHeader
, 1)
663 PHP_HTTP_ARG_VAL(header
, 0)
664 PHP_HTTP_ARG_VAL(value
, 0)
667 PHP_HTTP_BEGIN_ARGS(addHeader
, 2)
668 PHP_HTTP_ARG_VAL(header
, 0)
669 PHP_HTTP_ARG_VAL(value
, 0)
672 PHP_HTTP_EMPTY_ARGS(getHeaders
);
673 PHP_HTTP_BEGIN_ARGS(setHeaders
, 1)
674 PHP_HTTP_ARG_VAL(headers
, 0)
677 PHP_HTTP_BEGIN_ARGS(addHeaders
, 1)
678 PHP_HTTP_ARG_VAL(headers
, 0)
679 PHP_HTTP_ARG_VAL(append
, 0)
682 PHP_HTTP_EMPTY_ARGS(getType
);
683 PHP_HTTP_BEGIN_ARGS(setType
, 1)
684 PHP_HTTP_ARG_VAL(type
, 0)
687 PHP_HTTP_EMPTY_ARGS(getInfo
);
688 PHP_HTTP_BEGIN_ARGS(setInfo
, 1)
689 PHP_HTTP_ARG_VAL(http_info
, 0)
692 PHP_HTTP_EMPTY_ARGS(getResponseCode
);
693 PHP_HTTP_BEGIN_ARGS(setResponseCode
, 1)
694 PHP_HTTP_ARG_VAL(response_code
, 0)
697 PHP_HTTP_EMPTY_ARGS(getResponseStatus
);
698 PHP_HTTP_BEGIN_ARGS(setResponseStatus
, 1)
699 PHP_HTTP_ARG_VAL(response_status
, 0)
702 PHP_HTTP_EMPTY_ARGS(getRequestMethod
);
703 PHP_HTTP_BEGIN_ARGS(setRequestMethod
, 1)
704 PHP_HTTP_ARG_VAL(request_method
, 0)
707 PHP_HTTP_EMPTY_ARGS(getRequestUrl
);
708 PHP_HTTP_BEGIN_ARGS(setRequestUrl
, 1)
709 PHP_HTTP_ARG_VAL(url
, 0)
712 PHP_HTTP_EMPTY_ARGS(getHttpVersion
);
713 PHP_HTTP_BEGIN_ARGS(setHttpVersion
, 1)
714 PHP_HTTP_ARG_VAL(http_version
, 0)
717 PHP_HTTP_EMPTY_ARGS(getParentMessage
);
718 PHP_HTTP_EMPTY_ARGS(__toString
);
719 PHP_HTTP_BEGIN_ARGS(toString
, 0)
720 PHP_HTTP_ARG_VAL(include_parent
, 0)
723 PHP_HTTP_EMPTY_ARGS(count
);
725 PHP_HTTP_EMPTY_ARGS(serialize
);
726 PHP_HTTP_BEGIN_ARGS(unserialize
, 1)
727 PHP_HTTP_ARG_VAL(serialized
, 0)
730 PHP_HTTP_EMPTY_ARGS(rewind
);
731 PHP_HTTP_EMPTY_ARGS(valid
);
732 PHP_HTTP_EMPTY_ARGS(key
);
733 PHP_HTTP_EMPTY_ARGS(current
);
734 PHP_HTTP_EMPTY_ARGS(next
);
736 PHP_HTTP_EMPTY_ARGS(detach
);
737 PHP_HTTP_BEGIN_ARGS(prepend
, 1)
738 PHP_HTTP_ARG_OBJ(http
\\Message
, message
, 0)
740 PHP_HTTP_EMPTY_ARGS(reverse
);
742 static zval
*php_http_message_object_read_prop(zval
*object
, zval
*member
, int type
, const zend_literal
*literal_key TSRMLS_DC
);
743 static void php_http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value
, const zend_literal
*literal_key TSRMLS_DC
);
744 static zval
**php_http_message_object_get_prop_ptr(zval
*object
, zval
*member
, const zend_literal
*literal_key TSRMLS_DC
);
745 static HashTable
*php_http_message_object_get_props(zval
*object TSRMLS_DC
);
747 zend_class_entry
*php_http_message_class_entry
;
748 zend_function_entry php_http_message_method_entry
[] = {
749 PHP_HTTP_MESSAGE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
750 PHP_HTTP_MESSAGE_ME(getBody
, ZEND_ACC_PUBLIC
)
751 PHP_HTTP_MESSAGE_ME(setBody
, ZEND_ACC_PUBLIC
)
752 PHP_HTTP_MESSAGE_ME(getHeader
, ZEND_ACC_PUBLIC
)
753 PHP_HTTP_MESSAGE_ME(setHeader
, ZEND_ACC_PUBLIC
)
754 PHP_HTTP_MESSAGE_ME(addHeader
, ZEND_ACC_PUBLIC
)
755 PHP_HTTP_MESSAGE_ME(getHeaders
, ZEND_ACC_PUBLIC
)
756 PHP_HTTP_MESSAGE_ME(setHeaders
, ZEND_ACC_PUBLIC
)
757 PHP_HTTP_MESSAGE_ME(addHeaders
, ZEND_ACC_PUBLIC
)
758 PHP_HTTP_MESSAGE_ME(getType
, ZEND_ACC_PUBLIC
)
759 PHP_HTTP_MESSAGE_ME(setType
, ZEND_ACC_PUBLIC
)
760 PHP_HTTP_MESSAGE_ME(getInfo
, ZEND_ACC_PUBLIC
)
761 PHP_HTTP_MESSAGE_ME(setInfo
, ZEND_ACC_PUBLIC
)
762 PHP_HTTP_MESSAGE_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
763 PHP_HTTP_MESSAGE_ME(setResponseCode
, ZEND_ACC_PUBLIC
)
764 PHP_HTTP_MESSAGE_ME(getResponseStatus
, ZEND_ACC_PUBLIC
)
765 PHP_HTTP_MESSAGE_ME(setResponseStatus
, ZEND_ACC_PUBLIC
)
766 PHP_HTTP_MESSAGE_ME(getRequestMethod
, ZEND_ACC_PUBLIC
)
767 PHP_HTTP_MESSAGE_ME(setRequestMethod
, ZEND_ACC_PUBLIC
)
768 PHP_HTTP_MESSAGE_ME(getRequestUrl
, ZEND_ACC_PUBLIC
)
769 PHP_HTTP_MESSAGE_ME(setRequestUrl
, ZEND_ACC_PUBLIC
)
770 PHP_HTTP_MESSAGE_ME(getHttpVersion
, ZEND_ACC_PUBLIC
)
771 PHP_HTTP_MESSAGE_ME(setHttpVersion
, ZEND_ACC_PUBLIC
)
772 PHP_HTTP_MESSAGE_ME(getParentMessage
, ZEND_ACC_PUBLIC
)
773 PHP_HTTP_MESSAGE_ME(toString
, ZEND_ACC_PUBLIC
)
775 /* implements Countable */
776 PHP_HTTP_MESSAGE_ME(count
, ZEND_ACC_PUBLIC
)
778 /* implements Serializable */
779 PHP_HTTP_MESSAGE_ME(serialize
, ZEND_ACC_PUBLIC
)
780 PHP_HTTP_MESSAGE_ME(unserialize
, ZEND_ACC_PUBLIC
)
782 /* implements Iterator */
783 PHP_HTTP_MESSAGE_ME(rewind
, ZEND_ACC_PUBLIC
)
784 PHP_HTTP_MESSAGE_ME(valid
, ZEND_ACC_PUBLIC
)
785 PHP_HTTP_MESSAGE_ME(current
, ZEND_ACC_PUBLIC
)
786 PHP_HTTP_MESSAGE_ME(key
, ZEND_ACC_PUBLIC
)
787 PHP_HTTP_MESSAGE_ME(next
, ZEND_ACC_PUBLIC
)
789 ZEND_MALIAS(HttpMessage
, __toString
, toString
, PHP_HTTP_ARGS(HttpMessage
, __toString
), ZEND_ACC_PUBLIC
)
791 PHP_HTTP_MESSAGE_ME(detach
, ZEND_ACC_PUBLIC
)
792 PHP_HTTP_MESSAGE_ME(prepend
, ZEND_ACC_PUBLIC
)
793 PHP_HTTP_MESSAGE_ME(reverse
, ZEND_ACC_PUBLIC
)
797 static zend_object_handlers php_http_message_object_handlers
;
798 static HashTable php_http_message_object_prophandlers
;
800 typedef void (*php_http_message_object_prophandler_func_t
)(php_http_message_object_t
*o
, zval
*v TSRMLS_DC
);
802 typedef struct php_http_message_object_prophandler
{
803 php_http_message_object_prophandler_func_t read
;
804 php_http_message_object_prophandler_func_t write
;
805 } php_http_message_object_prophandler_t
;
807 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
) {
808 php_http_message_object_prophandler_t h
= { read
, write
};
809 return zend_hash_add(&php_http_message_object_prophandlers
, prop_str
, prop_len
+ 1, (void *) &h
, sizeof(h
), NULL
);
812 static int php_http_message_object_has_prophandler(const char *prop_str, size_t prop_len) {
813 return zend_hash_exists(&php_http_message_object_prophandlers, prop_str, prop_len + 1);
816 static STATUS
php_http_message_object_get_prophandler(const char *prop_str
, size_t prop_len
, php_http_message_object_prophandler_t
**handler
) {
817 return zend_hash_find(&php_http_message_object_prophandlers
, prop_str
, prop_len
+ 1, (void *) handler
);
819 static void php_http_message_object_prophandler_get_type(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
820 RETVAL_LONG(obj
->message
->type
);
822 static void php_http_message_object_prophandler_set_type(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
823 zval
*cpy
= php_http_ztyp(IS_LONG
, value
);
824 php_http_message_set_type(obj
->message
, Z_LVAL_P(cpy
));
827 static void php_http_message_object_prophandler_get_request_method(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
828 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
) && obj
->message
->http
.info
.request
.method
) {
829 RETVAL_STRING(obj
->message
->http
.info
.request
.method
, 1);
834 static void php_http_message_object_prophandler_set_request_method(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
835 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
836 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
837 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
841 static void php_http_message_object_prophandler_get_request_url(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
842 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
) && obj
->message
->http
.info
.request
.url
) {
843 RETVAL_STRING(obj
->message
->http
.info
.request
.url
, 1);
848 static void php_http_message_object_prophandler_set_request_url(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
849 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
850 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
851 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
855 static void php_http_message_object_prophandler_get_response_status(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
856 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
) && obj
->message
->http
.info
.response
.status
) {
857 RETVAL_STRING(obj
->message
->http
.info
.response
.status
, 1);
862 static void php_http_message_object_prophandler_set_response_status(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
863 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
864 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
865 STR_SET(obj
->message
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
869 static void php_http_message_object_prophandler_get_response_code(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
870 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
871 RETVAL_LONG(obj
->message
->http
.info
.response
.code
);
876 static void php_http_message_object_prophandler_set_response_code(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
877 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
878 zval
*cpy
= php_http_ztyp(IS_LONG
, value
);
879 obj
->message
->http
.info
.response
.code
= Z_LVAL_P(cpy
);
883 static void php_http_message_object_prophandler_get_http_version(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
887 php_http_version_to_string(&obj
->message
->http
.version
, &version_str
, &version_len
, NULL
, NULL TSRMLS_CC
);
888 RETVAL_STRINGL(version_str
, version_len
, 0);
890 static void php_http_message_object_prophandler_set_http_version(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
891 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
892 php_http_version_parse(&obj
->message
->http
.version
, Z_STRVAL_P(cpy
) TSRMLS_CC
);
895 static void php_http_message_object_prophandler_get_headers(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
896 array_init(return_value
);
897 zend_hash_copy(Z_ARRVAL_P(return_value
), &obj
->message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
899 static void php_http_message_object_prophandler_set_headers(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
900 zval
*cpy
= php_http_ztyp(IS_ARRAY
, value
);
902 zend_hash_clean(&obj
->message
->hdrs
);
903 zend_hash_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(cpy
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
906 static void php_http_message_object_prophandler_get_body(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
907 if (obj
->body
.handle
) {
908 RETVAL_OBJVAL(obj
->body
, 1);
913 static void php_http_message_object_prophandler_set_body(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
914 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), php_http_message_body_class_entry TSRMLS_CC
)) {
915 if (obj
->body
.handle
) {
916 zend_objects_store_del_ref_by_handle(obj
->body
.handle TSRMLS_CC
);
918 Z_OBJ_ADDREF_P(value
);
919 obj
->body
= Z_OBJVAL_P(value
);
922 static void php_http_message_object_prophandler_get_parent_message(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
923 if (obj
->message
->parent
) {
924 RETVAL_OBJVAL(obj
->parent
, 1);
929 static void php_http_message_object_prophandler_set_parent_message(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
930 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), php_http_message_class_entry TSRMLS_CC
)) {
931 if (obj
->message
->parent
) {
932 zend_objects_store_del_ref_by_handle(obj
->parent
.handle TSRMLS_CC
);
934 Z_OBJ_ADDREF_P(value
);
935 obj
->parent
= Z_OBJVAL_P(value
);
939 PHP_MINIT_FUNCTION(http_message
)
941 PHP_HTTP_REGISTER_CLASS(http
, Message
, http_message
, php_http_object_class_entry
, 0);
942 php_http_message_class_entry
->create_object
= php_http_message_object_new
;
943 memcpy(&php_http_message_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
944 php_http_message_object_handlers
.clone_obj
= php_http_message_object_clone
;
945 php_http_message_object_handlers
.read_property
= php_http_message_object_read_prop
;
946 php_http_message_object_handlers
.write_property
= php_http_message_object_write_prop
;
947 php_http_message_object_handlers
.get_properties
= php_http_message_object_get_props
;
948 php_http_message_object_handlers
.get_property_ptr_ptr
= php_http_message_object_get_prop_ptr
;
950 zend_class_implements(php_http_message_class_entry TSRMLS_CC
, 3, spl_ce_Countable
, zend_ce_serializable
, zend_ce_iterator
);
952 zend_hash_init(&php_http_message_object_prophandlers
, 9, NULL
, NULL
, 1);
953 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("type"), PHP_HTTP_NONE
, ZEND_ACC_PROTECTED TSRMLS_CC
);
954 php_http_message_object_add_prophandler(ZEND_STRL("type"), php_http_message_object_prophandler_get_type
, php_http_message_object_prophandler_set_type
);
955 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("body"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
956 php_http_message_object_add_prophandler(ZEND_STRL("body"), php_http_message_object_prophandler_get_body
, php_http_message_object_prophandler_set_body
);
957 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestMethod"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
958 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
);
959 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestUrl"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
960 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
);
961 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("responseStatus"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
962 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
);
963 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("responseCode"), 0, ZEND_ACC_PROTECTED TSRMLS_CC
);
964 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
);
965 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("httpVersion"), ZEND_ACC_PROTECTED TSRMLS_CC
);
966 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
);
967 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("headers"), ZEND_ACC_PROTECTED TSRMLS_CC
);
968 php_http_message_object_add_prophandler(ZEND_STRL("headers"), php_http_message_object_prophandler_get_headers
, php_http_message_object_prophandler_set_headers
);
969 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("parentMessage"), ZEND_ACC_PROTECTED TSRMLS_CC
);
970 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
);
972 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_NONE"), PHP_HTTP_NONE TSRMLS_CC
);
973 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_REQUEST"), PHP_HTTP_REQUEST TSRMLS_CC
);
974 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_RESPONSE"), PHP_HTTP_RESPONSE TSRMLS_CC
);
979 PHP_MSHUTDOWN_FUNCTION(http_message
)
981 zend_hash_destroy(&php_http_message_object_prophandlers
);
986 void php_http_message_object_reverse(zval
*this_ptr
, zval
*return_value TSRMLS_DC
)
989 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
992 php_http_message_count(i
, obj
->message
);
996 zend_object_value
*ovalues
= NULL
;
997 php_http_message_object_t
**objects
= NULL
;
1000 objects
= ecalloc(i
, sizeof(**objects
));
1001 ovalues
= ecalloc(i
, sizeof(*ovalues
));
1003 /* we are the first message */
1005 ovalues
[0] = getThis()->value
.obj
;
1010 for (i
= 1; obj
->parent
.handle
; ++i
) {
1011 o
.value
.obj
= obj
->parent
;
1012 ovalues
[i
] = o
.value
.obj
;
1013 objects
[i
] = obj
= zend_object_store_get_object(&o TSRMLS_CC
);
1016 /* reorder parents */
1017 for (last
= --i
; i
; --i
) {
1018 objects
[i
]->message
->parent
= objects
[i
-1]->message
;
1019 objects
[i
]->parent
= ovalues
[i
-1];
1021 objects
[0]->message
->parent
= NULL
;
1022 objects
[0]->parent
.handle
= 0;
1023 objects
[0]->parent
.handlers
= NULL
;
1025 /* add ref (why?) */
1026 Z_OBJ_ADDREF_P(getThis());
1027 RETVAL_OBJVAL(ovalues
[last
], 1);
1032 RETURN_ZVAL(getThis(), 1, 0);
1036 void php_http_message_object_prepend(zval
*this_ptr
, zval
*prepend
, zend_bool top TSRMLS_DC
)
1039 php_http_message_t
*save_parent_msg
= NULL
;
1040 zend_object_value save_parent_obj
= {0, NULL
};
1041 php_http_message_object_t
*obj
= zend_object_store_get_object(this_ptr TSRMLS_CC
);
1042 php_http_message_object_t
*prepend_obj
= zend_object_store_get_object(prepend TSRMLS_CC
);
1048 save_parent_obj
= obj
->parent
;
1049 save_parent_msg
= obj
->message
->parent
;
1051 /* iterate to the most parent object */
1052 while (obj
->parent
.handle
) {
1053 m
.value
.obj
= obj
->parent
;
1054 obj
= zend_object_store_get_object(&m TSRMLS_CC
);
1059 obj
->parent
= prepend
->value
.obj
;
1060 obj
->message
->parent
= prepend_obj
->message
;
1063 zend_objects_store_add_ref(prepend TSRMLS_CC
);
1064 while (prepend_obj
->parent
.handle
) {
1065 m
.value
.obj
= prepend_obj
->parent
;
1066 zend_objects_store_add_ref(&m TSRMLS_CC
);
1067 prepend_obj
= zend_object_store_get_object(&m TSRMLS_CC
);
1071 prepend_obj
->parent
= save_parent_obj
;
1072 prepend_obj
->message
->parent
= save_parent_msg
;
1076 zend_object_value
php_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
1078 return php_http_message_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
1081 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
)
1083 zend_object_value ov
;
1084 php_http_message_object_t
*o
;
1086 o
= ecalloc(1, sizeof(php_http_message_object_t
));
1087 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
1088 object_properties_init((zend_object
*) o
, ce
);
1097 o
->parent
= php_http_message_object_new_ex(ce
, msg
->parent
, NULL TSRMLS_CC
);
1099 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
);
1102 ov
.handle
= zend_objects_store_put((zend_object
*) o
, NULL
, php_http_message_object_free
, NULL TSRMLS_CC
);
1103 ov
.handlers
= &php_http_message_object_handlers
;
1108 zend_object_value
php_http_message_object_clone(zval
*this_ptr TSRMLS_DC
)
1110 zend_object_value new_ov
;
1111 php_http_message_object_t
*new_obj
= NULL
;
1112 php_http_message_object_t
*old_obj
= zend_object_store_get_object(this_ptr TSRMLS_CC
);
1114 new_ov
= php_http_message_object_new_ex(old_obj
->zo
.ce
, php_http_message_copy(old_obj
->message
, NULL
), &new_obj TSRMLS_CC
);
1115 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
1120 void php_http_message_object_free(void *object TSRMLS_DC
)
1122 php_http_message_object_t
*o
= (php_http_message_object_t
*) object
;
1125 zval_ptr_dtor(&o
->iterator
);
1129 /* do NOT free recursivly */
1130 php_http_message_dtor(o
->message
);
1134 if (o
->parent
.handle
) {
1135 zend_objects_store_del_ref_by_handle(o
->parent
.handle TSRMLS_CC
);
1137 if (o
->body
.handle
) {
1138 zend_objects_store_del_ref_by_handle(o
->body
.handle TSRMLS_CC
);
1140 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
1145 static zval
**php_http_message_object_get_prop_ptr(zval
*object
, zval
*member
, const zend_literal
*literal_key TSRMLS_DC
)
1147 php_http_message_object_prophandler_t
*handler
;
1148 zval
*copy
= php_http_ztyp(IS_STRING
, member
);
1150 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1151 zval_ptr_dtor(©
);
1152 return &php_http_property_proxy_init(NULL
, object
, member
, NULL TSRMLS_CC
)->myself
;
1154 zval_ptr_dtor(©
);
1156 return zend_get_std_object_handlers()->get_property_ptr_ptr(object
, member
, literal_key TSRMLS_CC
);
1159 static zval
*php_http_message_object_read_prop(zval
*object
, zval
*member
, int type
, const zend_literal
*literal_key TSRMLS_DC
)
1161 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1162 php_http_message_object_prophandler_t
*handler
;
1163 zval
*return_value
, *copy
= php_http_ztyp(IS_STRING
, member
);
1165 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1166 if (type
== BP_VAR_R
) {
1167 ALLOC_ZVAL(return_value
);
1168 Z_SET_REFCOUNT_P(return_value
, 0);
1169 Z_UNSET_ISREF_P(return_value
);
1171 handler
->read(obj
, return_value TSRMLS_CC
);
1173 zend_error(E_ERROR
, "Cannot access HttpMessage properties by reference or array key/index");
1174 return_value
= NULL
;
1177 return_value
= zend_get_std_object_handlers()->read_property(object
, member
, type
, literal_key TSRMLS_CC
);
1180 zval_ptr_dtor(©
);
1182 return return_value
;
1185 static void php_http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value
, const zend_literal
*literal_key TSRMLS_DC
)
1187 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1188 php_http_message_object_prophandler_t
*handler
;
1189 zval
*copy
= php_http_ztyp(IS_STRING
, member
);
1191 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1192 handler
->write(obj
, value TSRMLS_CC
);
1194 zend_get_std_object_handlers()->write_property(object
, member
, value
, literal_key TSRMLS_CC
);
1197 zval_ptr_dtor(©
);
1201 static HashTable
*php_http_message_object_get_props(zval
*object TSRMLS_DC
)
1204 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1205 php_http_message_t
*msg
= obj
->message
;
1206 HashTable
*props
= zend_get_std_object_handlers()->get_properties(object TSRMLS_CC
);
1207 zval array
, *parent
, *body
;
1210 INIT_PZVAL_ARRAY(&array
, props
);
1212 #define ASSOC_PROP(array, ptype, name, val) \
1214 char *m_prop_name; \
1216 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
1217 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
1218 efree(m_prop_name); \
1220 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
1221 #define ASSOC_STRINGL(array, name, val, len) ASSOC_STRINGL_EX(array, name, val, len, 1)
1222 #define ASSOC_STRINGL_EX(array, name, val, len, cpy) \
1224 char *m_prop_name; \
1226 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
1227 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, cpy); \
1228 efree(m_prop_name); \
1231 ASSOC_PROP(array
, long, "type", msg
->type
);
1232 ASSOC_STRINGL_EX(array
, "httpVersion", version
, spprintf(&version
, 0, "%u.%u", msg
->http
.version
.major
, msg
->http
.version
.minor
), 0);
1234 switch (msg
->type
) {
1235 case PHP_HTTP_REQUEST
:
1236 ASSOC_PROP(array
, long, "responseCode", 0);
1237 ASSOC_STRINGL(array
, "responseStatus", "", 0);
1238 ASSOC_STRING(array
, "requestMethod", STR_PTR(msg
->http
.info
.request
.method
));
1239 ASSOC_STRING(array
, "requestUrl", STR_PTR(msg
->http
.info
.request
.url
));
1242 case PHP_HTTP_RESPONSE
:
1243 ASSOC_PROP(array
, long, "responseCode", msg
->http
.info
.response
.code
);
1244 ASSOC_STRING(array
, "responseStatus", STR_PTR(msg
->http
.info
.response
.status
));
1245 ASSOC_STRINGL(array
, "requestMethod", "", 0);
1246 ASSOC_STRINGL(array
, "requestUrl", "", 0);
1251 ASSOC_PROP(array
, long, "responseCode", 0);
1252 ASSOC_STRINGL(array
, "responseStatus", "", 0);
1253 ASSOC_STRINGL(array
, "requestMethod", "", 0);
1254 ASSOC_STRINGL(array
, "requestUrl", "", 0);
1258 MAKE_STD_ZVAL(headers
);
1259 array_init(headers
);
1260 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1261 ASSOC_PROP(array
, zval
, "headers", headers
);
1263 MAKE_STD_ZVAL(body
);
1264 if (!obj
->body
.handle
) {
1265 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
);
1267 ZVAL_OBJVAL(body
, obj
->body
, 1);
1268 ASSOC_PROP(array
, zval
, "body", body
);
1270 MAKE_STD_ZVAL(parent
);
1272 ZVAL_OBJVAL(parent
, obj
->parent
, 1);
1276 ASSOC_PROP(array
, zval
, "parentMessage", parent
);
1283 PHP_METHOD(HttpMessage
, __construct
)
1286 char *message
= NULL
;
1287 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1289 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1290 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
1291 if (message
&& length
) {
1292 php_http_message_t
*msg
= php_http_message_parse(NULL
, message
, length TSRMLS_CC
);
1295 php_http_error(HE_THROW
, PHP_HTTP_E_MESSAGE
, "could not parse message: %.*s", 25, message
);
1297 php_http_message_dtor(obj
->message
);
1298 obj
->message
= msg
;{
1299 if (obj
->message
->parent
) {
1300 obj
->parent
= php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj
->message
->parent
, NULL TSRMLS_CC
);
1303 } end_error_handling();
1305 if (!obj
->message
) {
1306 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1308 } end_error_handling();
1312 PHP_METHOD(HttpMessage
, getBody
)
1314 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1315 if (SUCCESS
== zend_parse_parameters_none()) {
1316 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1318 if (!obj
->message
) {
1319 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1322 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
)) {
1323 RETVAL_OBJVAL(obj
->body
, 1);
1326 } end_error_handling();
1329 PHP_METHOD(HttpMessage
, setBody
)
1333 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &zbody
, php_http_message_body_class_entry
)) {
1334 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1335 php_http_message_body_object_t
*body_obj
= zend_object_store_get_object(zbody TSRMLS_CC
);
1337 if (!obj
->message
) {
1338 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1341 php_http_message_body_dtor(&obj
->message
->body
);
1342 php_http_message_body_copy(body_obj
->body
, &obj
->message
->body
, 0);
1343 Z_OBJ_ADDREF_P(zbody
);
1344 obj
->body
= Z_OBJVAL_P(zbody
);
1346 RETVAL_ZVAL(getThis(), 1, 0);
1349 PHP_METHOD(HttpMessage
, getHeader
)
1354 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header_str
, &header_len
)) {
1355 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1358 if (!obj
->message
) {
1359 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1362 if ((header
= php_http_message_header(obj
->message
, header_str
, header_len
, 0))) {
1363 RETURN_ZVAL(header
, 1, 1);
1369 PHP_METHOD(HttpMessage
, getHeaders
)
1371 if (SUCCESS
== zend_parse_parameters_none()) {
1372 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1374 if (!obj
->message
) {
1375 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1378 array_init(return_value
);
1379 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(return_value
));
1383 PHP_METHOD(HttpMessage
, setHeader
)
1385 zval
*zvalue
= NULL
;
1389 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|z!", &name_str
, &name_len
, &zvalue
)) {
1390 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1391 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1393 if (!obj
->message
) {
1394 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1398 zend_symtable_del(&obj
->message
->hdrs
, name
, name_len
+ 1);
1401 zend_symtable_update(&obj
->message
->hdrs
, name
, name_len
+ 1, &zvalue
, sizeof(void *), NULL
);
1405 RETVAL_ZVAL(getThis(), 1, 0);
1408 PHP_METHOD(HttpMessage
, setHeaders
)
1410 zval
*new_headers
= NULL
;
1412 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &new_headers
)) {
1413 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1415 if (!obj
->message
) {
1416 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1419 zend_hash_clean(&obj
->message
->hdrs
);
1421 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, 0, ARRAY_JOIN_PRETTIFY
|ARRAY_JOIN_STRONLY
);
1424 RETVAL_ZVAL(getThis(), 1, 0);
1427 PHP_METHOD(HttpMessage
, addHeader
)
1433 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sz", &name_str
, &name_len
, &zvalue
)) {
1434 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1435 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1438 if (!obj
->message
) {
1439 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1443 if ((header
= php_http_message_header(obj
->message
, name
, name_len
, 0))) {
1444 convert_to_array(header
);
1445 zend_hash_next_index_insert(Z_ARRVAL_P(header
), &zvalue
, sizeof(void *), NULL
);
1447 zend_symtable_update(&obj
->message
->hdrs
, name
, name_len
+ 1, &zvalue
, sizeof(void *), NULL
);
1451 RETVAL_ZVAL(getThis(), 1, 0);
1454 PHP_METHOD(HttpMessage
, addHeaders
)
1457 zend_bool append
= 0;
1459 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
1460 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1462 if (!obj
->message
) {
1463 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1466 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, append
, ARRAY_JOIN_STRONLY
|ARRAY_JOIN_PRETTIFY
);
1468 RETVAL_ZVAL(getThis(), 1, 0);
1471 PHP_METHOD(HttpMessage
, getType
)
1473 if (SUCCESS
== zend_parse_parameters_none()) {
1474 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1476 if (!obj
->message
) {
1477 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1480 RETURN_LONG(obj
->message
->type
);
1484 PHP_METHOD(HttpMessage
, setType
)
1488 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
1489 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1491 if (!obj
->message
) {
1492 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1495 php_http_message_set_type(obj
->message
, type
);
1497 RETVAL_ZVAL(getThis(), 1, 0);
1500 PHP_METHOD(HttpMessage
, getInfo
)
1502 if (SUCCESS
== zend_parse_parameters_none()) {
1503 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1505 if (!obj
->message
) {
1506 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1509 switch (obj
->message
->type
) {
1510 case PHP_HTTP_REQUEST
:
1511 Z_STRLEN_P(return_value
) = spprintf(&Z_STRVAL_P(return_value
), 0, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&obj
->message
->http
, ""));
1513 case PHP_HTTP_RESPONSE
:
1514 Z_STRLEN_P(return_value
) = spprintf(&Z_STRVAL_P(return_value
), 0, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&obj
->message
->http
, ""));
1520 Z_TYPE_P(return_value
) = IS_STRING
;
1526 PHP_METHOD(HttpMessage
, setInfo
)
1530 php_http_info_t inf
;
1532 if ( SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)
1533 && php_http_info_parse(&inf
, str TSRMLS_CC
)) {
1534 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1536 if (!obj
->message
) {
1537 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1540 php_http_message_set_info(obj
->message
, &inf
);
1541 php_http_info_dtor(&inf
);
1543 RETVAL_ZVAL(getThis(), 1, 0);
1546 PHP_METHOD(HttpMessage
, getHttpVersion
)
1548 if (SUCCESS
== zend_parse_parameters_none()) {
1551 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1553 if (!obj
->message
) {
1554 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1557 php_http_version_to_string(&obj
->message
->http
.version
, &str
, &len
, NULL
, NULL TSRMLS_CC
);
1558 RETURN_STRINGL(str
, len
, 0);
1564 PHP_METHOD(HttpMessage
, setHttpVersion
)
1569 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &v_str
, &v_len
)) {
1570 php_http_version_t version
;
1571 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1573 if (!obj
->message
) {
1574 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1577 if (php_http_version_parse(&version
, v_str TSRMLS_CC
)) {
1578 obj
->message
->http
.version
= version
;
1581 RETVAL_ZVAL(getThis(), 1, 0);
1584 PHP_METHOD(HttpMessage
, getResponseCode
)
1586 if (SUCCESS
== zend_parse_parameters_none()) {
1587 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1589 if (!obj
->message
) {
1590 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1593 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1594 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
1599 PHP_METHOD(HttpMessage
, setResponseCode
)
1602 zend_bool strict
= 1;
1604 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l|b", &code
, &strict
)) {
1605 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1607 if (!obj
->message
) {
1608 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1611 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1612 if (strict
&& (code
< 100 || code
> 599)) {
1613 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Invalid response code (100-599): %ld", code
);
1617 obj
->message
->http
.info
.response
.code
= code
;
1619 RETVAL_ZVAL(getThis(), 1, 0);
1622 PHP_METHOD(HttpMessage
, getResponseStatus
)
1624 if (SUCCESS
== zend_parse_parameters_none()) {
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 if (obj
->message
->http
.info
.response
.status
) {
1633 RETURN_STRING(obj
->message
->http
.info
.response
.status
, 1);
1635 RETURN_EMPTY_STRING();
1642 PHP_METHOD(HttpMessage
, setResponseStatus
)
1647 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &status
, &status_len
)) {
1648 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1650 if (!obj
->message
) {
1651 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1654 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1655 STR_SET(obj
->message
->http
.info
.response
.status
, estrndup(status
, status_len
));
1657 RETVAL_ZVAL(getThis(), 1, 0);
1660 PHP_METHOD(HttpMessage
, getRequestMethod
)
1662 if (SUCCESS
== zend_parse_parameters_none()) {
1663 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1665 if (!obj
->message
) {
1666 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1669 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1670 if (obj
->message
->http
.info
.request
.method
) {
1671 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
1673 RETURN_EMPTY_STRING();
1680 PHP_METHOD(HttpMessage
, setRequestMethod
)
1685 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1686 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1688 if (!obj
->message
) {
1689 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1692 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1693 if (method_len
< 1) {
1694 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
1698 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
1700 RETVAL_ZVAL(getThis(), 1, 0);
1703 PHP_METHOD(HttpMessage
, getRequestUrl
)
1705 if (SUCCESS
== zend_parse_parameters_none()) {
1706 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1708 if (!obj
->message
) {
1709 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1712 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1713 if (obj
->message
->http
.info
.request
.url
) {
1714 RETURN_STRING(obj
->message
->http
.info
.request
.url
, 1);
1716 RETURN_EMPTY_STRING();
1723 PHP_METHOD(HttpMessage
, setRequestUrl
)
1728 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &url_str
, &url_len
)) {
1729 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1731 if (!obj
->message
) {
1732 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1735 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1737 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUrl to an empty string");
1740 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(url_str
, url_len
));
1742 RETVAL_ZVAL(getThis(), 1, 0);
1746 PHP_METHOD(HttpMessage
, getParentMessage
)
1748 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1749 if (SUCCESS
== zend_parse_parameters_none()) {
1750 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1752 if (!obj
->message
) {
1753 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1756 if (obj
->message
->parent
) {
1757 RETVAL_OBJVAL(obj
->parent
, 1);
1759 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "HttpMessage does not have a parent message");
1762 } end_error_handling();
1765 PHP_METHOD(HttpMessage
, toString
)
1767 zend_bool include_parent
= 0;
1769 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1770 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1774 if (!obj
->message
) {
1775 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1778 if (include_parent
) {
1779 php_http_message_serialize(obj
->message
, &string
, &length
);
1781 php_http_message_to_string(obj
->message
, &string
, &length
);
1784 RETURN_STRINGL(string
, length
, 0);
1787 RETURN_EMPTY_STRING();
1790 PHP_METHOD(HttpMessage
, serialize
)
1792 if (SUCCESS
== zend_parse_parameters_none()) {
1793 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1797 if (!obj
->message
) {
1798 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1801 php_http_message_serialize(obj
->message
, &string
, &length
);
1802 RETURN_STRINGL(string
, length
, 0);
1804 RETURN_EMPTY_STRING();
1807 PHP_METHOD(HttpMessage
, unserialize
)
1812 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &serialized
, &length
)) {
1813 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1814 php_http_message_t
*msg
;
1817 php_http_message_dtor(obj
->message
);
1818 efree(obj
->message
);
1820 if ((msg
= php_http_message_parse(NULL
, serialized
, (size_t) length TSRMLS_CC
))) {
1823 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1824 php_http_error(HE_ERROR
, PHP_HTTP_E_RUNTIME
, "Could not unserialize HttpMessage");
1829 PHP_METHOD(HttpMessage
, detach
)
1831 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1832 if (SUCCESS
== zend_parse_parameters_none()) {
1833 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1835 if (!obj
->message
) {
1836 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1839 RETVAL_OBJVAL(php_http_message_object_new_ex(obj
->zo
.ce
, php_http_message_copy(obj
->message
, NULL
), NULL TSRMLS_CC
), 0);
1841 } end_error_handling();
1844 PHP_METHOD(HttpMessage
, prepend
)
1849 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O|b", &prepend
, php_http_message_class_entry
, &top
)) {
1850 php_http_message_t
*msg
[2];
1851 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1852 php_http_message_object_t
*prepend_obj
= zend_object_store_get_object(prepend TSRMLS_CC
);
1854 if (!obj
->message
) {
1855 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1857 if (!prepend_obj
->message
) {
1858 prepend_obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1862 for (msg
[0] = obj
->message
; msg
[0]; msg
[0] = msg
[0]->parent
) {
1863 for (msg
[1] = prepend_obj
->message
; msg
[1]; msg
[1] = msg
[1]->parent
) {
1864 if (msg
[0] == msg
[1]) {
1865 php_http_error(HE_THROW
, PHP_HTTP_E_INVALID_PARAM
, "Cannot prepend a message located within the same message chain");
1871 php_http_message_object_prepend(getThis(), prepend
, top TSRMLS_CC
);
1875 PHP_METHOD(HttpMessage
, reverse
)
1877 if (SUCCESS
== zend_parse_parameters_none()) {
1878 php_http_message_object_reverse(getThis(), return_value TSRMLS_CC
);
1882 PHP_METHOD(HttpMessage
, count
)
1884 if (SUCCESS
== zend_parse_parameters_none()) {
1886 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1888 if (!obj
->message
) {
1889 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1892 php_http_message_count(i
, obj
->message
);
1898 PHP_METHOD(HttpMessage
, rewind
)
1900 if (SUCCESS
== zend_parse_parameters_none()) {
1901 zval
*zobj
= getThis();
1902 php_http_message_object_t
*obj
= zend_object_store_get_object(zobj TSRMLS_CC
);
1904 if (obj
->iterator
) {
1905 zval_ptr_dtor(&obj
->iterator
);
1908 obj
->iterator
= zobj
;
1912 PHP_METHOD(HttpMessage
, valid
)
1914 if (SUCCESS
== zend_parse_parameters_none()) {
1915 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1917 RETURN_BOOL(obj
->iterator
!= NULL
);
1921 PHP_METHOD(HttpMessage
, next
)
1923 if (SUCCESS
== zend_parse_parameters_none()) {
1924 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1926 if (obj
->iterator
) {
1927 php_http_message_object_t
*itr
= zend_object_store_get_object(obj
->iterator TSRMLS_CC
);
1929 if (itr
&& itr
->parent
.handle
) {
1930 zval
*old
= obj
->iterator
;
1931 MAKE_STD_ZVAL(obj
->iterator
);
1932 ZVAL_OBJVAL(obj
->iterator
, itr
->parent
, 1);
1933 zval_ptr_dtor(&old
);
1935 zval_ptr_dtor(&obj
->iterator
);
1936 obj
->iterator
= NULL
;
1942 PHP_METHOD(HttpMessage
, key
)
1944 if (SUCCESS
== zend_parse_parameters_none()) {
1945 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1947 RETURN_LONG(obj
->iterator
? obj
->iterator
->value
.obj
.handle
:0);
1951 PHP_METHOD(HttpMessage
, current
)
1953 if (SUCCESS
== zend_parse_parameters_none()) {
1954 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1956 if (obj
->iterator
) {
1957 RETURN_ZVAL(obj
->iterator
, 1, 0);
1967 * vim600: noet sw=4 ts=4 fdm=marker
1968 * vim<600: noet sw=4 ts=4