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);
172 ret
= php_http_ztyp(IS_STRING
, *header
);
183 PHP_HTTP_API
void php_http_message_set_type(php_http_message_t
*message
, php_http_message_type_t type
)
185 /* just act if different */
186 if (type
!= message
->type
) {
188 /* free request info */
189 switch (message
->type
= type
) {
190 case PHP_HTTP_REQUEST
:
191 STR_FREE(message
->http
.info
.request
.method
);
192 STR_FREE(message
->http
.info
.request
.url
);
195 case PHP_HTTP_RESPONSE
:
196 STR_FREE(message
->http
.info
.response
.status
);
203 memset(&message
->http
, 0, sizeof(message
->http
));
207 PHP_HTTP_API
void php_http_message_set_info(php_http_message_t
*message
, php_http_info_t
*info
)
209 php_http_message_set_type(message
, info
->type
);
210 message
->http
.version
= info
->http
.version
;
211 switch (message
->type
) {
212 case PHP_HTTP_REQUEST
:
213 STR_SET(PHP_HTTP_INFO(message
).request
.url
, PHP_HTTP_INFO(info
).request
.url
? estrdup(PHP_HTTP_INFO(info
).request
.url
) : NULL
);
214 STR_SET(PHP_HTTP_INFO(message
).request
.method
, PHP_HTTP_INFO(info
).request
.method
? estrdup(PHP_HTTP_INFO(info
).request
.method
) : NULL
);
217 case PHP_HTTP_RESPONSE
:
218 PHP_HTTP_INFO(message
).response
.code
= PHP_HTTP_INFO(info
).response
.code
;
219 STR_SET(PHP_HTTP_INFO(message
).response
.status
, PHP_HTTP_INFO(info
).response
.status
? estrdup(PHP_HTTP_INFO(info
).response
.status
) : NULL
);
227 static inline void message_headers(php_http_message_t
*msg
, php_http_buffer_t
*str
)
229 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
233 TSRMLS_FETCH_FROM_CTX(msg
->ts
);
236 case PHP_HTTP_REQUEST
:
237 php_http_buffer_appendf(str
, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&msg
->http
, PHP_HTTP_CRLF
));
240 case PHP_HTTP_RESPONSE
:
241 php_http_buffer_appendf(str
, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&msg
->http
, PHP_HTTP_CRLF
));
248 if ((size
= php_http_message_body_size(&msg
->body
))) {
251 zend_hash_update(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"), &h
, sizeof(zval
*), NULL
);
253 if (msg
->body
.boundary
) {
257 if (!(h
= php_http_message_header(msg
, ZEND_STRL("Content-Type"), 1))) {
258 len
= spprintf(&str
, 0, "multipart/form-data; boundary=\"%s\"", msg
->body
.boundary
);
260 ZVAL_STRINGL(h
, str
, len
, 0);
261 zend_hash_update(&msg
->hdrs
, "Content-Type", sizeof("Content-Type"), &h
, sizeof(zval
*), NULL
);
262 } else if (!php_http_match(Z_STRVAL_P(h
), "boundary=", PHP_HTTP_MATCH_WORD
)) {
264 Z_STRLEN_P(h
) = spprintf(&Z_STRVAL_P(h
), 0, "%s; boundary=\"%s\"", Z_STRVAL_P(h
), msg
->body
.boundary
);
265 zend_hash_update(&msg
->hdrs
, "Content-Type", sizeof("Content-Type"), &h
, sizeof(zval
*), NULL
);
272 FOREACH_HASH_KEYVAL(pos1
, &msg
->hdrs
, key
, header
) {
273 if (key
.type
== HASH_KEY_IS_STRING
) {
275 zval
**single_header
;
277 switch (Z_TYPE_PP(header
)) {
279 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_BVAL_PP(header
)?"true":"false");
283 php_http_buffer_appendf(str
, "%s: %ld" PHP_HTTP_CRLF
, key
.str
, Z_LVAL_PP(header
));
287 php_http_buffer_appendf(str
, "%s: %F" PHP_HTTP_CRLF
, key
.str
, Z_DVAL_PP(header
));
291 if (Z_STRVAL_PP(header
)[Z_STRLEN_PP(header
)-1] == '\r') fprintf(stderr
, "DOH!\n");
292 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_STRVAL_PP(header
));
296 FOREACH_VAL(pos2
, *header
, single_header
) {
297 switch (Z_TYPE_PP(single_header
)) {
299 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_BVAL_PP(single_header
)?"true":"false");
303 php_http_buffer_appendf(str
, "%s: %ld" PHP_HTTP_CRLF
, key
.str
, Z_LVAL_PP(single_header
));
307 php_http_buffer_appendf(str
, "%s: %F" PHP_HTTP_CRLF
, key
.str
, Z_DVAL_PP(single_header
));
311 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_STRVAL_PP(single_header
));
321 PHP_HTTP_API
void php_http_message_to_callback(php_http_message_t
*msg
, php_http_pass_callback_t cb
, void *cb_arg
)
323 php_http_buffer_t str
;
325 php_http_buffer_init_ex(&str
, 0x1000, 0);
326 message_headers(msg
, &str
);
327 cb(cb_arg
, PHP_HTTP_BUFFER_VAL(&str
), PHP_HTTP_BUFFER_LEN(&str
));
328 php_http_buffer_dtor(&str
);
330 if (php_http_message_body_size(&msg
->body
)) {
331 cb(cb_arg
, ZEND_STRL(PHP_HTTP_CRLF
));
332 php_http_message_body_to_callback(&msg
->body
, cb
, cb_arg
, 0, 0);
333 cb(cb_arg
, ZEND_STRL(PHP_HTTP_CRLF
));
337 PHP_HTTP_API
void php_http_message_to_string(php_http_message_t
*msg
, char **string
, size_t *length
)
339 php_http_buffer_t str
;
342 php_http_buffer_init_ex(&str
, 0x1000, 0);
343 message_headers(msg
, &str
);
344 if (php_http_message_body_size(&msg
->body
)) {
345 php_http_buffer_appends(&str
, PHP_HTTP_CRLF
);
346 php_http_message_body_to_callback(&msg
->body
, (php_http_pass_callback_t
) php_http_buffer_append
, &str
, 0, 0);
347 php_http_buffer_appends(&str
, PHP_HTTP_CRLF
);
350 data
= php_http_buffer_data(&str
, string
, length
);
355 php_http_buffer_dtor(&str
);
358 PHP_HTTP_API
void php_http_message_serialize(php_http_message_t
*message
, char **string
, size_t *length
)
362 php_http_buffer_t str
;
364 php_http_buffer_init(&str
);
367 php_http_message_to_string(message
, &buf
, &len
);
368 php_http_buffer_prepend(&str
, buf
, len
);
370 } while ((message
= message
->parent
));
372 buf
= php_http_buffer_data(&str
, string
, length
);
377 php_http_buffer_dtor(&str
);
380 PHP_HTTP_API php_http_message_t
*php_http_message_reverse(php_http_message_t
*msg
)
384 php_http_message_count(c
, msg
);
387 php_http_message_t
*tmp
= msg
, **arr
;
389 arr
= ecalloc(c
, sizeof(**arr
));
390 for (i
= 0; i
< c
; ++i
) {
394 arr
[0]->parent
= NULL
;
395 for (i
= 0; i
< c
-1; ++i
) {
396 arr
[i
+1]->parent
= arr
[i
];
406 PHP_HTTP_API php_http_message_t
*php_http_message_interconnect(php_http_message_t
*m1
, php_http_message_t
*m2
)
409 int i
= 0, c1
= 0, c2
= 0;
410 php_http_message_t
*t1
= m1
, *t2
= m2
, *p1
, *p2
;
412 php_http_message_count(c1
, m1
);
413 php_http_message_count(c2
, m2
);
415 while (i
++ < (c1
- c2
)) {
426 } else if (!m1
&& m2
) {
432 PHP_HTTP_API
void php_http_message_to_struct(php_http_message_t
*msg
, zval
*obj
)
437 TSRMLS_FETCH_FROM_CTX(msg
->ts
);
439 INIT_PZVAL_ARRAY(&strct
, HASH_OF(obj
));
441 add_assoc_long(&strct
, "type", msg
->type
);
442 spprintf(&version
, 0 ,"%u.%u", msg
->http
.version
.major
, msg
->http
.version
.minor
);
443 add_assoc_string_ex(&strct
, ZEND_STRL("httpVersion"), version
, 0);
446 case PHP_HTTP_RESPONSE
:
447 add_assoc_long(&strct
, "responseCode", msg
->http
.info
.response
.code
);
448 add_assoc_string(&strct
, "responseStatus", STR_PTR(msg
->http
.info
.response
.status
), 1);
451 case PHP_HTTP_REQUEST
:
452 add_assoc_string(&strct
, "requestMethod", STR_PTR(msg
->http
.info
.request
.method
), 1);
453 add_assoc_string(&strct
, "requestUrl", STR_PTR(msg
->http
.info
.request
.url
), 1);
457 /* avoid compiler warning */
461 MAKE_STD_ZVAL(headers
);
463 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
464 add_assoc_zval(&strct
, "headers", headers
);
466 add_assoc_stringl(&strct
, "body", PHP_HTTP_BUFFER_VAL(msg
), PHP_HTTP_BUFFER_LEN(msg
), 1);
471 MAKE_STD_ZVAL(parent
);
472 if (Z_TYPE_P(obj
) == IS_ARRAY
) {
477 add_assoc_zval(&strct
, "parentMessage", parent
);
478 php_http_message_to_struct(msg
->parent
, parent
);
480 add_assoc_null(&strct
, "parentMessage");
484 PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC)
488 switch (message->type) {
489 case PHP_HTTP_RESPONSE:
491 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
495 FOREACH_HASH_KEYVAL(pos, &message->hdrs, key, val) {
496 if (key.type == HASH_KEY_IS_STRING) {
497 http_send_header_zval_ex(key.str, key.len-1, val, 1);
500 rs = SUCCESS == http_send_status(message->http.info.response.code) &&
501 SUCCESS == http_send_data(PHP_HTTP_BUFFER_VAL(message), PHP_HTTP_BUFFER_LEN(message)) ?
506 case PHP_HTTP_REQUEST:
508 #ifdef PHP_HTTP_HAVE_CURL
510 http_request request;
511 zval **zhost, *options, *headers;
513 MAKE_STD_ZVAL(options);
514 MAKE_STD_ZVAL(headers);
517 zend_hash_copy(Z_ARRVAL_P(headers), &message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
518 add_assoc_zval(options, "headers", headers);
520 if (SUCCESS == zend_hash_find(&message->hdrs, "Host", sizeof("Host"), (void *) &zhost) && Z_TYPE_PP(zhost) == IS_STRING) {
522 php_url parts, *url = php_url_parse(message->http.info.request.url);
524 memset(&parts, 0, sizeof(php_url));
526 if ((colon = strchr(Z_STRVAL_PP(zhost), ':'))) {
527 parts.port = atoi(colon + 1);
528 parts.host = estrndup(Z_STRVAL_PP(zhost), (Z_STRVAL_PP(zhost) - colon - 1));
530 parts.host = estrndup(Z_STRVAL_PP(zhost), Z_STRLEN_PP(zhost));
533 http_build_url(PHP_HTTP_URL_REPLACE, url, &parts, NULL, &uri, NULL);
537 uri = http_absolute_url(message->http.info.request.url);
540 if ((request.meth = http_request_method_exists(1, 0, message->http.info.request.method))) {
541 http_request_body body;
543 http_request_init_ex(&request, NULL, request.meth, uri);
544 request.body = http_request_body_init_ex(&body, PHP_HTTP_REQUEST_BODY_CSTRING, PHP_HTTP_BUFFER_VAL(message), PHP_HTTP_BUFFER_LEN(message), 0);
545 if (SUCCESS == (rs = http_request_prepare(&request, Z_ARRVAL_P(options)))) {
546 http_request_exec(&request);
548 http_request_dtor(&request);
550 http_error_ex(HE_WARNING, PHP_HTTP_E_REQUEST_METHOD,
551 "Cannot send HttpMessage. Request method %s not supported",
552 message->http.info.request.method);
555 zval_ptr_dtor(&options);
557 http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "HTTP requests not supported - ext/http was not linked against libcurl.");
564 php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type PHP_HTTP_REQUEST nor PHP_HTTP_RESPONSE");
572 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
)
574 php_http_message_t
*temp
, *copy
= NULL
;
575 php_http_info_t info
;
576 TSRMLS_FETCH_FROM_CTX(from
->ts
);
579 info
.type
= from
->type
;
580 info
.http
= from
->http
;
582 copy
= temp
= php_http_message_init(to
, 0 TSRMLS_CC
);
583 php_http_message_set_info(temp
, &info
);
584 zend_hash_copy(&temp
->hdrs
, &from
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
585 php_http_message_body_copy(&from
->body
, &temp
->body
, 1);
587 if (parents
) while (from
->parent
) {
588 info
.type
= from
->parent
->type
;
589 info
.http
= from
->parent
->http
;
591 temp
->parent
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
592 php_http_message_set_info(temp
->parent
, &info
);
593 zend_hash_copy(&temp
->parent
->hdrs
, &from
->parent
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
594 php_http_message_body_copy(&from
->body
, &temp
->body
, 1);
604 PHP_HTTP_API php_http_message_t
*php_http_message_copy(php_http_message_t
*from
, php_http_message_t
*to
)
606 return php_http_message_copy_ex(from
, to
, 1);
609 PHP_HTTP_API
void php_http_message_dtor(php_http_message_t
*message
)
612 zend_hash_destroy(&message
->hdrs
);
613 php_http_message_body_dtor(&message
->body
);
615 switch (message
->type
) {
616 case PHP_HTTP_REQUEST
:
617 STR_SET(message
->http
.info
.request
.method
, NULL
);
618 STR_SET(message
->http
.info
.request
.url
, NULL
);
621 case PHP_HTTP_RESPONSE
:
622 STR_SET(message
->http
.info
.response
.status
, NULL
);
631 PHP_HTTP_API
void php_http_message_free(php_http_message_t
**message
)
634 if ((*message
)->parent
) {
635 php_http_message_free(&(*message
)->parent
);
637 php_http_message_dtor(*message
);
643 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpMessage, method, 0, req_args)
644 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpMessage, method, 0)
645 #define PHP_HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, PHP_HTTP_ARGS(HttpMessage, method), visibility)
647 PHP_HTTP_BEGIN_ARGS(__construct
, 0)
648 PHP_HTTP_ARG_VAL(message
, 0)
651 PHP_HTTP_EMPTY_ARGS(getBody
);
652 PHP_HTTP_BEGIN_ARGS(setBody
, 1)
653 PHP_HTTP_ARG_VAL(body
, 0)
656 PHP_HTTP_BEGIN_ARGS(getHeader
, 1)
657 PHP_HTTP_ARG_VAL(header
, 0)
660 PHP_HTTP_BEGIN_ARGS(setHeader
, 1)
661 PHP_HTTP_ARG_VAL(header
, 0)
662 PHP_HTTP_ARG_VAL(value
, 0)
665 PHP_HTTP_BEGIN_ARGS(addHeader
, 2)
666 PHP_HTTP_ARG_VAL(header
, 0)
667 PHP_HTTP_ARG_VAL(value
, 0)
670 PHP_HTTP_EMPTY_ARGS(getHeaders
);
671 PHP_HTTP_BEGIN_ARGS(setHeaders
, 1)
672 PHP_HTTP_ARG_VAL(headers
, 0)
675 PHP_HTTP_BEGIN_ARGS(addHeaders
, 1)
676 PHP_HTTP_ARG_VAL(headers
, 0)
677 PHP_HTTP_ARG_VAL(append
, 0)
680 PHP_HTTP_EMPTY_ARGS(getType
);
681 PHP_HTTP_BEGIN_ARGS(setType
, 1)
682 PHP_HTTP_ARG_VAL(type
, 0)
685 PHP_HTTP_EMPTY_ARGS(getInfo
);
686 PHP_HTTP_BEGIN_ARGS(setInfo
, 1)
687 PHP_HTTP_ARG_VAL(http_info
, 0)
690 PHP_HTTP_EMPTY_ARGS(getResponseCode
);
691 PHP_HTTP_BEGIN_ARGS(setResponseCode
, 1)
692 PHP_HTTP_ARG_VAL(response_code
, 0)
695 PHP_HTTP_EMPTY_ARGS(getResponseStatus
);
696 PHP_HTTP_BEGIN_ARGS(setResponseStatus
, 1)
697 PHP_HTTP_ARG_VAL(response_status
, 0)
700 PHP_HTTP_EMPTY_ARGS(getRequestMethod
);
701 PHP_HTTP_BEGIN_ARGS(setRequestMethod
, 1)
702 PHP_HTTP_ARG_VAL(request_method
, 0)
705 PHP_HTTP_EMPTY_ARGS(getRequestUrl
);
706 PHP_HTTP_BEGIN_ARGS(setRequestUrl
, 1)
707 PHP_HTTP_ARG_VAL(url
, 0)
710 PHP_HTTP_EMPTY_ARGS(getHttpVersion
);
711 PHP_HTTP_BEGIN_ARGS(setHttpVersion
, 1)
712 PHP_HTTP_ARG_VAL(http_version
, 0)
715 PHP_HTTP_EMPTY_ARGS(getParentMessage
);
716 PHP_HTTP_EMPTY_ARGS(__toString
);
717 PHP_HTTP_BEGIN_ARGS(toString
, 0)
718 PHP_HTTP_ARG_VAL(include_parent
, 0)
721 PHP_HTTP_EMPTY_ARGS(count
);
723 PHP_HTTP_EMPTY_ARGS(serialize
);
724 PHP_HTTP_BEGIN_ARGS(unserialize
, 1)
725 PHP_HTTP_ARG_VAL(serialized
, 0)
728 PHP_HTTP_EMPTY_ARGS(rewind
);
729 PHP_HTTP_EMPTY_ARGS(valid
);
730 PHP_HTTP_EMPTY_ARGS(key
);
731 PHP_HTTP_EMPTY_ARGS(current
);
732 PHP_HTTP_EMPTY_ARGS(next
);
734 PHP_HTTP_EMPTY_ARGS(detach
);
735 PHP_HTTP_BEGIN_ARGS(prepend
, 1)
736 PHP_HTTP_ARG_OBJ(http
\\Message
, message
, 0)
738 PHP_HTTP_EMPTY_ARGS(reverse
);
740 static zval
*php_http_message_object_read_prop(zval
*object
, zval
*member
, int type
, const zend_literal
*literal_key TSRMLS_DC
);
741 static void php_http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value
, const zend_literal
*literal_key TSRMLS_DC
);
742 static zval
**php_http_message_object_get_prop_ptr(zval
*object
, zval
*member
, const zend_literal
*literal_key TSRMLS_DC
);
743 static HashTable
*php_http_message_object_get_props(zval
*object TSRMLS_DC
);
745 zend_class_entry
*php_http_message_class_entry
;
746 zend_function_entry php_http_message_method_entry
[] = {
747 PHP_HTTP_MESSAGE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
748 PHP_HTTP_MESSAGE_ME(getBody
, ZEND_ACC_PUBLIC
)
749 PHP_HTTP_MESSAGE_ME(setBody
, ZEND_ACC_PUBLIC
)
750 PHP_HTTP_MESSAGE_ME(getHeader
, ZEND_ACC_PUBLIC
)
751 PHP_HTTP_MESSAGE_ME(setHeader
, ZEND_ACC_PUBLIC
)
752 PHP_HTTP_MESSAGE_ME(addHeader
, ZEND_ACC_PUBLIC
)
753 PHP_HTTP_MESSAGE_ME(getHeaders
, ZEND_ACC_PUBLIC
)
754 PHP_HTTP_MESSAGE_ME(setHeaders
, ZEND_ACC_PUBLIC
)
755 PHP_HTTP_MESSAGE_ME(addHeaders
, ZEND_ACC_PUBLIC
)
756 PHP_HTTP_MESSAGE_ME(getType
, ZEND_ACC_PUBLIC
)
757 PHP_HTTP_MESSAGE_ME(setType
, ZEND_ACC_PUBLIC
)
758 PHP_HTTP_MESSAGE_ME(getInfo
, ZEND_ACC_PUBLIC
)
759 PHP_HTTP_MESSAGE_ME(setInfo
, ZEND_ACC_PUBLIC
)
760 PHP_HTTP_MESSAGE_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
761 PHP_HTTP_MESSAGE_ME(setResponseCode
, ZEND_ACC_PUBLIC
)
762 PHP_HTTP_MESSAGE_ME(getResponseStatus
, ZEND_ACC_PUBLIC
)
763 PHP_HTTP_MESSAGE_ME(setResponseStatus
, ZEND_ACC_PUBLIC
)
764 PHP_HTTP_MESSAGE_ME(getRequestMethod
, ZEND_ACC_PUBLIC
)
765 PHP_HTTP_MESSAGE_ME(setRequestMethod
, ZEND_ACC_PUBLIC
)
766 PHP_HTTP_MESSAGE_ME(getRequestUrl
, ZEND_ACC_PUBLIC
)
767 PHP_HTTP_MESSAGE_ME(setRequestUrl
, ZEND_ACC_PUBLIC
)
768 PHP_HTTP_MESSAGE_ME(getHttpVersion
, ZEND_ACC_PUBLIC
)
769 PHP_HTTP_MESSAGE_ME(setHttpVersion
, ZEND_ACC_PUBLIC
)
770 PHP_HTTP_MESSAGE_ME(getParentMessage
, ZEND_ACC_PUBLIC
)
771 PHP_HTTP_MESSAGE_ME(toString
, ZEND_ACC_PUBLIC
)
773 /* implements Countable */
774 PHP_HTTP_MESSAGE_ME(count
, ZEND_ACC_PUBLIC
)
776 /* implements Serializable */
777 PHP_HTTP_MESSAGE_ME(serialize
, ZEND_ACC_PUBLIC
)
778 PHP_HTTP_MESSAGE_ME(unserialize
, ZEND_ACC_PUBLIC
)
780 /* implements Iterator */
781 PHP_HTTP_MESSAGE_ME(rewind
, ZEND_ACC_PUBLIC
)
782 PHP_HTTP_MESSAGE_ME(valid
, ZEND_ACC_PUBLIC
)
783 PHP_HTTP_MESSAGE_ME(current
, ZEND_ACC_PUBLIC
)
784 PHP_HTTP_MESSAGE_ME(key
, ZEND_ACC_PUBLIC
)
785 PHP_HTTP_MESSAGE_ME(next
, ZEND_ACC_PUBLIC
)
787 ZEND_MALIAS(HttpMessage
, __toString
, toString
, PHP_HTTP_ARGS(HttpMessage
, __toString
), ZEND_ACC_PUBLIC
)
789 PHP_HTTP_MESSAGE_ME(detach
, ZEND_ACC_PUBLIC
)
790 PHP_HTTP_MESSAGE_ME(prepend
, ZEND_ACC_PUBLIC
)
791 PHP_HTTP_MESSAGE_ME(reverse
, ZEND_ACC_PUBLIC
)
795 static zend_object_handlers php_http_message_object_handlers
;
796 static HashTable php_http_message_object_prophandlers
;
798 typedef void (*php_http_message_object_prophandler_func_t
)(php_http_message_object_t
*o
, zval
*v TSRMLS_DC
);
800 typedef struct php_http_message_object_prophandler
{
801 php_http_message_object_prophandler_func_t read
;
802 php_http_message_object_prophandler_func_t write
;
803 } php_http_message_object_prophandler_t
;
805 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
) {
806 php_http_message_object_prophandler_t h
= { read
, write
};
807 return zend_hash_add(&php_http_message_object_prophandlers
, prop_str
, prop_len
+ 1, (void *) &h
, sizeof(h
), NULL
);
810 static int php_http_message_object_has_prophandler(const char *prop_str, size_t prop_len) {
811 return zend_hash_exists(&php_http_message_object_prophandlers, prop_str, prop_len + 1);
814 static STATUS
php_http_message_object_get_prophandler(const char *prop_str
, size_t prop_len
, php_http_message_object_prophandler_t
**handler
) {
815 return zend_hash_find(&php_http_message_object_prophandlers
, prop_str
, prop_len
+ 1, (void *) handler
);
817 static void php_http_message_object_prophandler_get_type(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
818 RETVAL_LONG(obj
->message
->type
);
820 static void php_http_message_object_prophandler_set_type(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
821 zval
*cpy
= php_http_ztyp(IS_LONG
, value
);
822 php_http_message_set_type(obj
->message
, Z_LVAL_P(cpy
));
825 static void php_http_message_object_prophandler_get_request_method(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
826 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
) && obj
->message
->http
.info
.request
.method
) {
827 RETVAL_STRING(obj
->message
->http
.info
.request
.method
, 1);
832 static void php_http_message_object_prophandler_set_request_method(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
833 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
834 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
835 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
839 static void php_http_message_object_prophandler_get_request_url(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
840 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
) && obj
->message
->http
.info
.request
.url
) {
841 RETVAL_STRING(obj
->message
->http
.info
.request
.url
, 1);
846 static void php_http_message_object_prophandler_set_request_url(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
847 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
848 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
849 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
853 static void php_http_message_object_prophandler_get_response_status(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
854 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
) && obj
->message
->http
.info
.response
.status
) {
855 RETVAL_STRING(obj
->message
->http
.info
.response
.status
, 1);
860 static void php_http_message_object_prophandler_set_response_status(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
861 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
862 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
863 STR_SET(obj
->message
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
867 static void php_http_message_object_prophandler_get_response_code(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
868 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
869 RETVAL_LONG(obj
->message
->http
.info
.response
.code
);
874 static void php_http_message_object_prophandler_set_response_code(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
875 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
876 zval
*cpy
= php_http_ztyp(IS_LONG
, value
);
877 obj
->message
->http
.info
.response
.code
= Z_LVAL_P(cpy
);
881 static void php_http_message_object_prophandler_get_http_version(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
885 php_http_version_to_string(&obj
->message
->http
.version
, &version_str
, &version_len
, NULL
, NULL TSRMLS_CC
);
886 RETVAL_STRINGL(version_str
, version_len
, 0);
888 static void php_http_message_object_prophandler_set_http_version(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
889 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
890 php_http_version_parse(&obj
->message
->http
.version
, Z_STRVAL_P(cpy
) TSRMLS_CC
);
893 static void php_http_message_object_prophandler_get_headers(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
894 array_init(return_value
);
895 zend_hash_copy(Z_ARRVAL_P(return_value
), &obj
->message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
897 static void php_http_message_object_prophandler_set_headers(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
898 zval
*cpy
= php_http_ztyp(IS_ARRAY
, value
);
900 zend_hash_clean(&obj
->message
->hdrs
);
901 zend_hash_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(cpy
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
904 static void php_http_message_object_prophandler_get_body(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
905 if (obj
->body
.handle
) {
906 RETVAL_OBJVAL(obj
->body
, 1);
911 static void php_http_message_object_prophandler_set_body(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
912 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), php_http_message_body_class_entry TSRMLS_CC
)) {
913 if (obj
->body
.handle
) {
914 zend_objects_store_del_ref_by_handle(obj
->body
.handle TSRMLS_CC
);
916 Z_OBJ_ADDREF_P(value
);
917 obj
->body
= Z_OBJVAL_P(value
);
920 static void php_http_message_object_prophandler_get_parent_message(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
921 if (obj
->message
->parent
) {
922 RETVAL_OBJVAL(obj
->parent
, 1);
927 static void php_http_message_object_prophandler_set_parent_message(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
928 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), php_http_message_class_entry TSRMLS_CC
)) {
929 if (obj
->message
->parent
) {
930 zend_objects_store_del_ref_by_handle(obj
->parent
.handle TSRMLS_CC
);
932 Z_OBJ_ADDREF_P(value
);
933 obj
->parent
= Z_OBJVAL_P(value
);
937 PHP_MINIT_FUNCTION(http_message
)
939 PHP_HTTP_REGISTER_CLASS(http
, Message
, http_message
, php_http_object_class_entry
, 0);
940 php_http_message_class_entry
->create_object
= php_http_message_object_new
;
941 memcpy(&php_http_message_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
942 php_http_message_object_handlers
.clone_obj
= php_http_message_object_clone
;
943 php_http_message_object_handlers
.read_property
= php_http_message_object_read_prop
;
944 php_http_message_object_handlers
.write_property
= php_http_message_object_write_prop
;
945 php_http_message_object_handlers
.get_properties
= php_http_message_object_get_props
;
946 php_http_message_object_handlers
.get_property_ptr_ptr
= php_http_message_object_get_prop_ptr
;
948 zend_class_implements(php_http_message_class_entry TSRMLS_CC
, 3, spl_ce_Countable
, zend_ce_serializable
, zend_ce_iterator
);
950 zend_hash_init(&php_http_message_object_prophandlers
, 9, NULL
, NULL
, 1);
951 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("type"), PHP_HTTP_NONE
, ZEND_ACC_PROTECTED TSRMLS_CC
);
952 php_http_message_object_add_prophandler(ZEND_STRL("type"), php_http_message_object_prophandler_get_type
, php_http_message_object_prophandler_set_type
);
953 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("body"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
954 php_http_message_object_add_prophandler(ZEND_STRL("body"), php_http_message_object_prophandler_get_body
, php_http_message_object_prophandler_set_body
);
955 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestMethod"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
956 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
);
957 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestUrl"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
958 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
);
959 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("responseStatus"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
960 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
);
961 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("responseCode"), 0, ZEND_ACC_PROTECTED TSRMLS_CC
);
962 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
);
963 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("httpVersion"), ZEND_ACC_PROTECTED TSRMLS_CC
);
964 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
);
965 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("headers"), ZEND_ACC_PROTECTED TSRMLS_CC
);
966 php_http_message_object_add_prophandler(ZEND_STRL("headers"), php_http_message_object_prophandler_get_headers
, php_http_message_object_prophandler_set_headers
);
967 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("parentMessage"), ZEND_ACC_PROTECTED TSRMLS_CC
);
968 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
);
970 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_NONE"), PHP_HTTP_NONE TSRMLS_CC
);
971 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_REQUEST"), PHP_HTTP_REQUEST TSRMLS_CC
);
972 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_RESPONSE"), PHP_HTTP_RESPONSE TSRMLS_CC
);
977 PHP_MSHUTDOWN_FUNCTION(http_message
)
979 zend_hash_destroy(&php_http_message_object_prophandlers
);
984 void php_http_message_object_reverse(zval
*this_ptr
, zval
*return_value TSRMLS_DC
)
987 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
990 php_http_message_count(i
, obj
->message
);
994 zend_object_value
*ovalues
= NULL
;
995 php_http_message_object_t
**objects
= NULL
;
998 objects
= ecalloc(i
, sizeof(**objects
));
999 ovalues
= ecalloc(i
, sizeof(*ovalues
));
1001 /* we are the first message */
1003 ovalues
[0] = getThis()->value
.obj
;
1008 for (i
= 1; obj
->parent
.handle
; ++i
) {
1009 o
.value
.obj
= obj
->parent
;
1010 ovalues
[i
] = o
.value
.obj
;
1011 objects
[i
] = obj
= zend_object_store_get_object(&o TSRMLS_CC
);
1014 /* reorder parents */
1015 for (last
= --i
; i
; --i
) {
1016 objects
[i
]->message
->parent
= objects
[i
-1]->message
;
1017 objects
[i
]->parent
= ovalues
[i
-1];
1019 objects
[0]->message
->parent
= NULL
;
1020 objects
[0]->parent
.handle
= 0;
1021 objects
[0]->parent
.handlers
= NULL
;
1023 /* add ref (why?) */
1024 Z_OBJ_ADDREF_P(getThis());
1025 RETVAL_OBJVAL(ovalues
[last
], 1);
1030 RETURN_ZVAL(getThis(), 1, 0);
1034 void php_http_message_object_prepend(zval
*this_ptr
, zval
*prepend
, zend_bool top TSRMLS_DC
)
1037 php_http_message_t
*save_parent_msg
= NULL
;
1038 zend_object_value save_parent_obj
= {0, NULL
};
1039 php_http_message_object_t
*obj
= zend_object_store_get_object(this_ptr TSRMLS_CC
);
1040 php_http_message_object_t
*prepend_obj
= zend_object_store_get_object(prepend TSRMLS_CC
);
1046 save_parent_obj
= obj
->parent
;
1047 save_parent_msg
= obj
->message
->parent
;
1049 /* iterate to the most parent object */
1050 while (obj
->parent
.handle
) {
1051 m
.value
.obj
= obj
->parent
;
1052 obj
= zend_object_store_get_object(&m TSRMLS_CC
);
1057 obj
->parent
= prepend
->value
.obj
;
1058 obj
->message
->parent
= prepend_obj
->message
;
1061 zend_objects_store_add_ref(prepend TSRMLS_CC
);
1062 while (prepend_obj
->parent
.handle
) {
1063 m
.value
.obj
= prepend_obj
->parent
;
1064 zend_objects_store_add_ref(&m TSRMLS_CC
);
1065 prepend_obj
= zend_object_store_get_object(&m TSRMLS_CC
);
1069 prepend_obj
->parent
= save_parent_obj
;
1070 prepend_obj
->message
->parent
= save_parent_msg
;
1074 zend_object_value
php_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
1076 return php_http_message_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
1079 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
)
1081 zend_object_value ov
;
1082 php_http_message_object_t
*o
;
1084 o
= ecalloc(1, sizeof(php_http_message_object_t
));
1085 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
1086 object_properties_init((zend_object
*) o
, ce
);
1095 o
->parent
= php_http_message_object_new_ex(ce
, msg
->parent
, NULL TSRMLS_CC
);
1097 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
);
1100 ov
.handle
= zend_objects_store_put((zend_object
*) o
, NULL
, php_http_message_object_free
, NULL TSRMLS_CC
);
1101 ov
.handlers
= &php_http_message_object_handlers
;
1106 zend_object_value
php_http_message_object_clone(zval
*this_ptr TSRMLS_DC
)
1108 zend_object_value new_ov
;
1109 php_http_message_object_t
*new_obj
= NULL
;
1110 php_http_message_object_t
*old_obj
= zend_object_store_get_object(this_ptr TSRMLS_CC
);
1112 new_ov
= php_http_message_object_new_ex(old_obj
->zo
.ce
, php_http_message_copy(old_obj
->message
, NULL
), &new_obj TSRMLS_CC
);
1113 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
1118 void php_http_message_object_free(void *object TSRMLS_DC
)
1120 php_http_message_object_t
*o
= (php_http_message_object_t
*) object
;
1123 zval_ptr_dtor(&o
->iterator
);
1127 /* do NOT free recursivly */
1128 php_http_message_dtor(o
->message
);
1132 if (o
->parent
.handle
) {
1133 zend_objects_store_del_ref_by_handle(o
->parent
.handle TSRMLS_CC
);
1135 if (o
->body
.handle
) {
1136 zend_objects_store_del_ref_by_handle(o
->body
.handle TSRMLS_CC
);
1138 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
1143 static zval
**php_http_message_object_get_prop_ptr(zval
*object
, zval
*member
, const zend_literal
*literal_key TSRMLS_DC
)
1145 php_http_message_object_prophandler_t
*handler
;
1146 zval
*copy
= php_http_ztyp(IS_STRING
, member
);
1148 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1149 zval_ptr_dtor(©
);
1150 return &php_http_property_proxy_init(NULL
, object
, member
, NULL TSRMLS_CC
)->myself
;
1152 zval_ptr_dtor(©
);
1154 return zend_get_std_object_handlers()->get_property_ptr_ptr(object
, member
, literal_key TSRMLS_CC
);
1157 static zval
*php_http_message_object_read_prop(zval
*object
, zval
*member
, int type
, const zend_literal
*literal_key TSRMLS_DC
)
1159 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1160 php_http_message_object_prophandler_t
*handler
;
1161 zval
*return_value
, *copy
= php_http_ztyp(IS_STRING
, member
);
1163 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1164 if (type
== BP_VAR_R
) {
1165 ALLOC_ZVAL(return_value
);
1166 Z_SET_REFCOUNT_P(return_value
, 0);
1167 Z_UNSET_ISREF_P(return_value
);
1169 handler
->read(obj
, return_value TSRMLS_CC
);
1171 zend_error(E_ERROR
, "Cannot access HttpMessage properties by reference or array key/index");
1172 return_value
= NULL
;
1175 return_value
= zend_get_std_object_handlers()->read_property(object
, member
, type
, literal_key TSRMLS_CC
);
1178 zval_ptr_dtor(©
);
1180 return return_value
;
1183 static void php_http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value
, const zend_literal
*literal_key TSRMLS_DC
)
1185 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1186 php_http_message_object_prophandler_t
*handler
;
1187 zval
*copy
= php_http_ztyp(IS_STRING
, member
);
1189 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1190 handler
->write(obj
, value TSRMLS_CC
);
1192 zend_get_std_object_handlers()->write_property(object
, member
, value
, literal_key TSRMLS_CC
);
1195 zval_ptr_dtor(©
);
1199 static HashTable
*php_http_message_object_get_props(zval
*object TSRMLS_DC
)
1202 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1203 php_http_message_t
*msg
= obj
->message
;
1204 HashTable
*props
= zend_get_std_object_handlers()->get_properties(object TSRMLS_CC
);
1205 zval array
, *parent
, *body
;
1208 INIT_PZVAL_ARRAY(&array
, props
);
1210 #define ASSOC_PROP(array, ptype, name, val) \
1212 char *m_prop_name; \
1214 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
1215 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
1216 efree(m_prop_name); \
1218 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
1219 #define ASSOC_STRINGL(array, name, val, len) ASSOC_STRINGL_EX(array, name, val, len, 1)
1220 #define ASSOC_STRINGL_EX(array, name, val, len, cpy) \
1222 char *m_prop_name; \
1224 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
1225 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, cpy); \
1226 efree(m_prop_name); \
1229 ASSOC_PROP(array
, long, "type", msg
->type
);
1230 ASSOC_STRINGL_EX(array
, "httpVersion", version
, spprintf(&version
, 0, "%u.%u", msg
->http
.version
.major
, msg
->http
.version
.minor
), 0);
1232 switch (msg
->type
) {
1233 case PHP_HTTP_REQUEST
:
1234 ASSOC_PROP(array
, long, "responseCode", 0);
1235 ASSOC_STRINGL(array
, "responseStatus", "", 0);
1236 ASSOC_STRING(array
, "requestMethod", STR_PTR(msg
->http
.info
.request
.method
));
1237 ASSOC_STRING(array
, "requestUrl", STR_PTR(msg
->http
.info
.request
.url
));
1240 case PHP_HTTP_RESPONSE
:
1241 ASSOC_PROP(array
, long, "responseCode", msg
->http
.info
.response
.code
);
1242 ASSOC_STRING(array
, "responseStatus", STR_PTR(msg
->http
.info
.response
.status
));
1243 ASSOC_STRINGL(array
, "requestMethod", "", 0);
1244 ASSOC_STRINGL(array
, "requestUrl", "", 0);
1249 ASSOC_PROP(array
, long, "responseCode", 0);
1250 ASSOC_STRINGL(array
, "responseStatus", "", 0);
1251 ASSOC_STRINGL(array
, "requestMethod", "", 0);
1252 ASSOC_STRINGL(array
, "requestUrl", "", 0);
1256 MAKE_STD_ZVAL(headers
);
1257 array_init(headers
);
1258 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1259 ASSOC_PROP(array
, zval
, "headers", headers
);
1261 MAKE_STD_ZVAL(body
);
1262 if (!obj
->body
.handle
) {
1263 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
);
1265 ZVAL_OBJVAL(body
, obj
->body
, 1);
1266 ASSOC_PROP(array
, zval
, "body", body
);
1268 MAKE_STD_ZVAL(parent
);
1270 ZVAL_OBJVAL(parent
, obj
->parent
, 1);
1274 ASSOC_PROP(array
, zval
, "parentMessage", parent
);
1281 PHP_METHOD(HttpMessage
, __construct
)
1284 char *message
= NULL
;
1285 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1287 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1288 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
1289 if (message
&& length
) {
1290 php_http_message_t
*msg
= php_http_message_parse(NULL
, message
, length TSRMLS_CC
);
1293 php_http_error(HE_THROW
, PHP_HTTP_E_MESSAGE
, "could not parse message: %.*s", 25, message
);
1295 php_http_message_dtor(obj
->message
);
1296 obj
->message
= msg
;{
1297 if (obj
->message
->parent
) {
1298 obj
->parent
= php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj
->message
->parent
, NULL TSRMLS_CC
);
1301 } end_error_handling();
1303 if (!obj
->message
) {
1304 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1306 } end_error_handling();
1310 PHP_METHOD(HttpMessage
, getBody
)
1312 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1313 if (SUCCESS
== zend_parse_parameters_none()) {
1314 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1316 if (!obj
->message
) {
1317 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1320 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
)) {
1321 RETVAL_OBJVAL(obj
->body
, 1);
1324 } end_error_handling();
1327 PHP_METHOD(HttpMessage
, setBody
)
1331 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &zbody
, php_http_message_body_class_entry
)) {
1332 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1333 php_http_message_body_object_t
*body_obj
= zend_object_store_get_object(zbody TSRMLS_CC
);
1335 if (!obj
->message
) {
1336 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1339 php_http_message_body_dtor(&obj
->message
->body
);
1340 php_http_message_body_copy(body_obj
->body
, &obj
->message
->body
, 0);
1341 Z_OBJ_ADDREF_P(zbody
);
1342 obj
->body
= Z_OBJVAL_P(zbody
);
1344 RETVAL_ZVAL(getThis(), 1, 0);
1347 PHP_METHOD(HttpMessage
, getHeader
)
1352 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header_str
, &header_len
)) {
1353 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1356 if (!obj
->message
) {
1357 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1360 if ((header
= php_http_message_header(obj
->message
, header_str
, header_len
, 0))) {
1361 RETURN_ZVAL(header
, 1, 1);
1367 PHP_METHOD(HttpMessage
, getHeaders
)
1369 if (SUCCESS
== zend_parse_parameters_none()) {
1370 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1372 if (!obj
->message
) {
1373 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1376 array_init(return_value
);
1377 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(return_value
));
1381 PHP_METHOD(HttpMessage
, setHeader
)
1383 zval
*zvalue
= NULL
;
1387 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|z!", &name_str
, &name_len
, &zvalue
)) {
1388 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1389 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1391 if (!obj
->message
) {
1392 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1396 zend_symtable_del(&obj
->message
->hdrs
, name
, name_len
+ 1);
1399 zend_symtable_update(&obj
->message
->hdrs
, name
, name_len
+ 1, &zvalue
, sizeof(void *), NULL
);
1403 RETVAL_ZVAL(getThis(), 1, 0);
1406 PHP_METHOD(HttpMessage
, setHeaders
)
1408 zval
*new_headers
= NULL
;
1410 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &new_headers
)) {
1411 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1413 if (!obj
->message
) {
1414 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1417 zend_hash_clean(&obj
->message
->hdrs
);
1419 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, 0, ARRAY_JOIN_PRETTIFY
|ARRAY_JOIN_STRONLY
);
1422 RETVAL_ZVAL(getThis(), 1, 0);
1425 PHP_METHOD(HttpMessage
, addHeader
)
1431 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sz", &name_str
, &name_len
, &zvalue
)) {
1432 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1433 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1436 if (!obj
->message
) {
1437 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1441 if ((header
= php_http_message_header(obj
->message
, name
, name_len
, 0))) {
1442 convert_to_array(header
);
1443 zend_hash_next_index_insert(Z_ARRVAL_P(header
), &zvalue
, sizeof(void *), NULL
);
1445 zend_symtable_update(&obj
->message
->hdrs
, name
, name_len
+ 1, &zvalue
, sizeof(void *), NULL
);
1449 RETVAL_ZVAL(getThis(), 1, 0);
1452 PHP_METHOD(HttpMessage
, addHeaders
)
1455 zend_bool append
= 0;
1457 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
1458 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1460 if (!obj
->message
) {
1461 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1464 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, append
, ARRAY_JOIN_STRONLY
|ARRAY_JOIN_PRETTIFY
);
1466 RETVAL_ZVAL(getThis(), 1, 0);
1469 PHP_METHOD(HttpMessage
, getType
)
1471 if (SUCCESS
== zend_parse_parameters_none()) {
1472 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1474 if (!obj
->message
) {
1475 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1478 RETURN_LONG(obj
->message
->type
);
1482 PHP_METHOD(HttpMessage
, setType
)
1486 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
1487 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1489 if (!obj
->message
) {
1490 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1493 php_http_message_set_type(obj
->message
, type
);
1495 RETVAL_ZVAL(getThis(), 1, 0);
1498 PHP_METHOD(HttpMessage
, getInfo
)
1500 if (SUCCESS
== zend_parse_parameters_none()) {
1501 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1503 if (!obj
->message
) {
1504 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1507 switch (obj
->message
->type
) {
1508 case PHP_HTTP_REQUEST
:
1509 Z_STRLEN_P(return_value
) = spprintf(&Z_STRVAL_P(return_value
), 0, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&obj
->message
->http
, ""));
1511 case PHP_HTTP_RESPONSE
:
1512 Z_STRLEN_P(return_value
) = spprintf(&Z_STRVAL_P(return_value
), 0, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&obj
->message
->http
, ""));
1518 Z_TYPE_P(return_value
) = IS_STRING
;
1524 PHP_METHOD(HttpMessage
, setInfo
)
1528 php_http_info_t inf
;
1530 if ( SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)
1531 && php_http_info_parse(&inf
, str TSRMLS_CC
)) {
1532 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1534 if (!obj
->message
) {
1535 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1538 php_http_message_set_info(obj
->message
, &inf
);
1539 php_http_info_dtor(&inf
);
1541 RETVAL_ZVAL(getThis(), 1, 0);
1544 PHP_METHOD(HttpMessage
, getHttpVersion
)
1546 if (SUCCESS
== zend_parse_parameters_none()) {
1549 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1551 if (!obj
->message
) {
1552 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1555 php_http_version_to_string(&obj
->message
->http
.version
, &str
, &len
, NULL
, NULL TSRMLS_CC
);
1556 RETURN_STRINGL(str
, len
, 0);
1562 PHP_METHOD(HttpMessage
, setHttpVersion
)
1567 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &v_str
, &v_len
)) {
1568 php_http_version_t version
;
1569 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1571 if (!obj
->message
) {
1572 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1575 if (php_http_version_parse(&version
, v_str TSRMLS_CC
)) {
1576 obj
->message
->http
.version
= version
;
1579 RETVAL_ZVAL(getThis(), 1, 0);
1582 PHP_METHOD(HttpMessage
, getResponseCode
)
1584 if (SUCCESS
== zend_parse_parameters_none()) {
1585 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1587 if (!obj
->message
) {
1588 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1591 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1592 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
1597 PHP_METHOD(HttpMessage
, setResponseCode
)
1600 zend_bool strict
= 1;
1602 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l|b", &code
, &strict
)) {
1603 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1605 if (!obj
->message
) {
1606 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1609 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1610 if (strict
&& (code
< 100 || code
> 599)) {
1611 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Invalid response code (100-599): %ld", code
);
1615 obj
->message
->http
.info
.response
.code
= code
;
1617 RETVAL_ZVAL(getThis(), 1, 0);
1620 PHP_METHOD(HttpMessage
, getResponseStatus
)
1622 if (SUCCESS
== zend_parse_parameters_none()) {
1623 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1625 if (!obj
->message
) {
1626 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1629 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1630 if (obj
->message
->http
.info
.response
.status
) {
1631 RETURN_STRING(obj
->message
->http
.info
.response
.status
, 1);
1633 RETURN_EMPTY_STRING();
1640 PHP_METHOD(HttpMessage
, setResponseStatus
)
1645 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &status
, &status_len
)) {
1646 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1648 if (!obj
->message
) {
1649 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1652 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1653 STR_SET(obj
->message
->http
.info
.response
.status
, estrndup(status
, status_len
));
1655 RETVAL_ZVAL(getThis(), 1, 0);
1658 PHP_METHOD(HttpMessage
, getRequestMethod
)
1660 if (SUCCESS
== zend_parse_parameters_none()) {
1661 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1663 if (!obj
->message
) {
1664 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1667 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1668 if (obj
->message
->http
.info
.request
.method
) {
1669 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
1671 RETURN_EMPTY_STRING();
1678 PHP_METHOD(HttpMessage
, setRequestMethod
)
1683 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1684 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1686 if (!obj
->message
) {
1687 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1690 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1691 if (method_len
< 1) {
1692 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
1696 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
1698 RETVAL_ZVAL(getThis(), 1, 0);
1701 PHP_METHOD(HttpMessage
, getRequestUrl
)
1703 if (SUCCESS
== zend_parse_parameters_none()) {
1704 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1706 if (!obj
->message
) {
1707 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1710 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1711 if (obj
->message
->http
.info
.request
.url
) {
1712 RETURN_STRING(obj
->message
->http
.info
.request
.url
, 1);
1714 RETURN_EMPTY_STRING();
1721 PHP_METHOD(HttpMessage
, setRequestUrl
)
1726 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &url_str
, &url_len
)) {
1727 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1729 if (!obj
->message
) {
1730 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1733 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1735 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUrl to an empty string");
1738 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(url_str
, url_len
));
1740 RETVAL_ZVAL(getThis(), 1, 0);
1744 PHP_METHOD(HttpMessage
, getParentMessage
)
1746 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1747 if (SUCCESS
== zend_parse_parameters_none()) {
1748 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1750 if (!obj
->message
) {
1751 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1754 if (obj
->message
->parent
) {
1755 RETVAL_OBJVAL(obj
->parent
, 1);
1757 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "HttpMessage does not have a parent message");
1760 } end_error_handling();
1763 PHP_METHOD(HttpMessage
, toString
)
1765 zend_bool include_parent
= 0;
1767 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1768 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1772 if (!obj
->message
) {
1773 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1776 if (include_parent
) {
1777 php_http_message_serialize(obj
->message
, &string
, &length
);
1779 php_http_message_to_string(obj
->message
, &string
, &length
);
1782 RETURN_STRINGL(string
, length
, 0);
1785 RETURN_EMPTY_STRING();
1788 PHP_METHOD(HttpMessage
, serialize
)
1790 if (SUCCESS
== zend_parse_parameters_none()) {
1791 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1795 if (!obj
->message
) {
1796 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1799 php_http_message_serialize(obj
->message
, &string
, &length
);
1800 RETURN_STRINGL(string
, length
, 0);
1802 RETURN_EMPTY_STRING();
1805 PHP_METHOD(HttpMessage
, unserialize
)
1810 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &serialized
, &length
)) {
1811 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1812 php_http_message_t
*msg
;
1815 php_http_message_dtor(obj
->message
);
1816 efree(obj
->message
);
1818 if ((msg
= php_http_message_parse(NULL
, serialized
, (size_t) length TSRMLS_CC
))) {
1821 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1822 php_http_error(HE_ERROR
, PHP_HTTP_E_RUNTIME
, "Could not unserialize HttpMessage");
1827 PHP_METHOD(HttpMessage
, detach
)
1829 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1830 if (SUCCESS
== zend_parse_parameters_none()) {
1831 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1833 if (!obj
->message
) {
1834 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1837 RETVAL_OBJVAL(php_http_message_object_new_ex(obj
->zo
.ce
, php_http_message_copy(obj
->message
, NULL
), NULL TSRMLS_CC
), 0);
1839 } end_error_handling();
1842 PHP_METHOD(HttpMessage
, prepend
)
1847 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O|b", &prepend
, php_http_message_class_entry
, &top
)) {
1848 php_http_message_t
*msg
[2];
1849 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1850 php_http_message_object_t
*prepend_obj
= zend_object_store_get_object(prepend TSRMLS_CC
);
1852 if (!obj
->message
) {
1853 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1855 if (!prepend_obj
->message
) {
1856 prepend_obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1860 for (msg
[0] = obj
->message
; msg
[0]; msg
[0] = msg
[0]->parent
) {
1861 for (msg
[1] = prepend_obj
->message
; msg
[1]; msg
[1] = msg
[1]->parent
) {
1862 if (msg
[0] == msg
[1]) {
1863 php_http_error(HE_THROW
, PHP_HTTP_E_INVALID_PARAM
, "Cannot prepend a message located within the same message chain");
1869 php_http_message_object_prepend(getThis(), prepend
, top TSRMLS_CC
);
1873 PHP_METHOD(HttpMessage
, reverse
)
1875 if (SUCCESS
== zend_parse_parameters_none()) {
1876 php_http_message_object_reverse(getThis(), return_value TSRMLS_CC
);
1880 PHP_METHOD(HttpMessage
, count
)
1882 if (SUCCESS
== zend_parse_parameters_none()) {
1884 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1886 if (!obj
->message
) {
1887 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1890 php_http_message_count(i
, obj
->message
);
1896 PHP_METHOD(HttpMessage
, rewind
)
1898 if (SUCCESS
== zend_parse_parameters_none()) {
1899 zval
*zobj
= getThis();
1900 php_http_message_object_t
*obj
= zend_object_store_get_object(zobj TSRMLS_CC
);
1902 if (obj
->iterator
) {
1903 zval_ptr_dtor(&obj
->iterator
);
1906 obj
->iterator
= zobj
;
1910 PHP_METHOD(HttpMessage
, valid
)
1912 if (SUCCESS
== zend_parse_parameters_none()) {
1913 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1915 RETURN_BOOL(obj
->iterator
!= NULL
);
1919 PHP_METHOD(HttpMessage
, next
)
1921 if (SUCCESS
== zend_parse_parameters_none()) {
1922 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1924 if (obj
->iterator
) {
1925 php_http_message_object_t
*itr
= zend_object_store_get_object(obj
->iterator TSRMLS_CC
);
1927 if (itr
&& itr
->parent
.handle
) {
1928 zval
*old
= obj
->iterator
;
1929 MAKE_STD_ZVAL(obj
->iterator
);
1930 ZVAL_OBJVAL(obj
->iterator
, itr
->parent
, 1);
1931 zval_ptr_dtor(&old
);
1933 zval_ptr_dtor(&obj
->iterator
);
1934 obj
->iterator
= NULL
;
1940 PHP_METHOD(HttpMessage
, key
)
1942 if (SUCCESS
== zend_parse_parameters_none()) {
1943 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1945 RETURN_LONG(obj
->iterator
? obj
->iterator
->value
.obj
.handle
:0);
1949 PHP_METHOD(HttpMessage
, current
)
1951 if (SUCCESS
== zend_parse_parameters_none()) {
1952 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1954 if (obj
->iterator
) {
1955 RETURN_ZVAL(obj
->iterator
, 1, 0);
1965 * vim600: noet sw=4 ts=4 fdm=marker
1966 * vim<600: noet sw=4 ts=4