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 +--------------------------------------------------------------------+
15 #include <main/SAPI.h>
16 #include <ext/spl/spl_iterators.h>
17 #include <Zend/zend_interfaces.h>
19 PHP_HTTP_API zend_bool
php_http_message_info_callback(php_http_message_t
**message
, HashTable
**headers
, php_http_info_t
*info TSRMLS_DC
)
21 php_http_message_t
*old
= *message
;
24 if (old
->type
|| zend_hash_num_elements(&old
->hdrs
) || PHP_HTTP_BUFFER_LEN(old
)) {
25 (*message
) = php_http_message_init(NULL
, 0 TSRMLS_CC
);
26 (*message
)->parent
= old
;
27 (*headers
) = &((*message
)->hdrs
);
30 php_http_message_set_info(*message
, info
);
32 return old
!= *message
;
35 PHP_HTTP_API php_http_message_t
*php_http_message_init(php_http_message_t
*message
, php_http_message_type_t type TSRMLS_DC
)
38 message
= emalloc(sizeof(*message
));
40 memset(message
, 0, sizeof(*message
));
41 TSRMLS_SET_CTX(message
->ts
);
43 php_http_message_set_type(message
, type
);
44 zend_hash_init(&message
->hdrs
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
45 php_http_message_body_init(&message
->body
, NULL TSRMLS_CC
);
50 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
)
52 int free_msg
= !message
;
54 php_http_message_body_t
*mbody
;
56 message
= php_http_message_init(message
, type TSRMLS_CC
);
59 case PHP_HTTP_REQUEST
:
60 if ((sval
= php_http_env_get_server_var(ZEND_STRL("SERVER_PROTOCOL"), 1 TSRMLS_CC
)) && !strncmp(Z_STRVAL_P(sval
), "HTTP/", lenof("HTTP/"))) {
61 php_http_version_parse(&message
->http
.version
, Z_STRVAL_P(sval
) TSRMLS_CC
);
63 message
->http
.version
.major
= 1;
64 message
->http
.version
.minor
= 1;
66 if ((sval
= php_http_env_get_server_var(ZEND_STRL("REQUEST_METHOD"), 1 TSRMLS_CC
))) {
67 message
->http
.info
.request
.method
= estrdup(Z_STRVAL_P(sval
));
69 if ((sval
= php_http_env_get_server_var(ZEND_STRL("REQUEST_URI"), 1 TSRMLS_CC
))) {
70 message
->http
.info
.request
.url
= estrdup(Z_STRVAL_P(sval
));
73 php_http_env_get_request_headers(&message
->hdrs TSRMLS_CC
);
75 if ((mbody
= php_http_env_get_request_body(TSRMLS_C
))) {
76 php_http_message_body_dtor(&message
->body
);
77 php_http_message_body_copy(mbody
, &message
->body
, 0);
81 case PHP_HTTP_RESPONSE
:
82 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
)) {
83 message
->http
.version
.major
= 1;
84 message
->http
.version
.minor
= 1;
85 switch ((message
->http
.info
.response
.code
= SG(sapi_headers
).http_response_code
)) {
87 message
->http
.info
.response
.code
= 200;
89 message
->http
.info
.response
.status
= estrdup("Ok");
92 message
->http
.info
.response
.status
= estrdup("");
98 php_http_env_get_response_headers(&message
->hdrs TSRMLS_CC
);
100 if (php_output_get_level(TSRMLS_C
)) {
101 if (php_output_get_status(TSRMLS_C
) & PHP_OUTPUT_SENT
) {
102 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
));
104 } else if (SUCCESS
!= php_output_get_contents(&tval TSRMLS_CC
)) {
105 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "Could not fetch response body");
108 php_http_message_body_append(&message
->body
, Z_STRVAL(tval
), Z_STRLEN(tval
));
117 php_http_message_free(&message
);
127 PHP_HTTP_API php_http_message_t
*php_http_message_parse(php_http_message_t
*msg
, const char *str
, size_t len TSRMLS_DC
)
129 php_http_message_parser_t p
;
130 php_http_buffer_t buf
;
133 msg
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
135 php_http_buffer_from_string_ex(&buf
, str
, len
);
136 php_http_message_parser_init(&p TSRMLS_CC
);
137 php_http_message_parser_parse(&p
, &buf
, PHP_HTTP_MESSAGE_PARSER_CLEANUP
, &msg
);
138 php_http_message_parser_dtor(&p
);
139 php_http_buffer_dtor(&buf
);
145 PHP_HTTP_API zval
*php_http_message_header(php_http_message_t
*msg
, char *key_str
, size_t key_len
, int join
)
147 zval
*ret
= NULL
, **header
;
148 char *key
= php_http_pretty_key(estrndup(key_str
, key_len
), key_len
, 1, 1);
150 if (SUCCESS
== zend_symtable_find(&msg
->hdrs
, key
, key_len
+ 1, (void *) &header
)) {
151 if (join
&& Z_TYPE_PP(header
) == IS_ARRAY
) {
152 zval
*header_str
, **val
;
154 php_http_buffer_t str
;
155 TSRMLS_FETCH_FROM_CTX(msg
->ts
);
157 php_http_buffer_init(&str
);
158 MAKE_STD_ZVAL(header_str
);
159 FOREACH_VAL(pos
, *header
, val
) {
160 zval
*strval
= php_http_ztyp(IS_STRING
, *val
);
161 php_http_buffer_appendf(&str
, PHP_HTTP_BUFFER_LEN(&str
) ? ", %s":"%s", Z_STRVAL_P(strval
));
162 zval_ptr_dtor(&strval
);
164 php_http_buffer_fix(&str
);
165 ZVAL_STRINGL(header_str
, PHP_HTTP_BUFFER_VAL(&str
), PHP_HTTP_BUFFER_LEN(&str
), 0);
168 ret
= php_http_ztyp(IS_STRING
, *header
);
179 PHP_HTTP_API
void php_http_message_set_type(php_http_message_t
*message
, php_http_message_type_t type
)
181 /* just act if different */
182 if (type
!= message
->type
) {
184 /* free request info */
185 switch (message
->type
= type
) {
186 case PHP_HTTP_REQUEST
:
187 STR_FREE(message
->http
.info
.request
.method
);
188 STR_FREE(message
->http
.info
.request
.url
);
191 case PHP_HTTP_RESPONSE
:
192 STR_FREE(message
->http
.info
.response
.status
);
199 memset(&message
->http
, 0, sizeof(message
->http
));
203 PHP_HTTP_API
void php_http_message_set_info(php_http_message_t
*message
, php_http_info_t
*info
)
205 php_http_message_set_type(message
, info
->type
);
206 message
->http
.version
= info
->http
.version
;
207 switch (message
->type
) {
208 case PHP_HTTP_REQUEST
:
209 STR_SET(PHP_HTTP_INFO(message
).request
.url
, PHP_HTTP_INFO(info
).request
.url
? estrdup(PHP_HTTP_INFO(info
).request
.url
) : NULL
);
210 STR_SET(PHP_HTTP_INFO(message
).request
.method
, PHP_HTTP_INFO(info
).request
.method
? estrdup(PHP_HTTP_INFO(info
).request
.method
) : NULL
);
213 case PHP_HTTP_RESPONSE
:
214 PHP_HTTP_INFO(message
).response
.code
= PHP_HTTP_INFO(info
).response
.code
;
215 STR_SET(PHP_HTTP_INFO(message
).response
.status
, PHP_HTTP_INFO(info
).response
.status
? estrdup(PHP_HTTP_INFO(info
).response
.status
) : NULL
);
223 static inline void message_headers(php_http_message_t
*msg
, php_http_buffer_t
*str
)
225 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
229 TSRMLS_FETCH_FROM_CTX(msg
->ts
);
232 case PHP_HTTP_REQUEST
:
233 php_http_buffer_appendf(str
, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&msg
->http
, PHP_HTTP_CRLF
));
236 case PHP_HTTP_RESPONSE
:
237 php_http_buffer_appendf(str
, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&msg
->http
, PHP_HTTP_CRLF
));
244 if ((size
= php_http_message_body_size(&msg
->body
))) {
247 zend_hash_update(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"), &h
, sizeof(zval
*), NULL
);
249 if (msg
->body
.boundary
) {
253 if (!(h
= php_http_message_header(msg
, ZEND_STRL("Content-Type"), 1))) {
254 len
= spprintf(&str
, 0, "multipart/form-data; boundary=\"%s\"", msg
->body
.boundary
);
256 ZVAL_STRINGL(h
, str
, len
, 0);
257 zend_hash_update(&msg
->hdrs
, "Content-Type", sizeof("Content-Type"), &h
, sizeof(zval
*), NULL
);
258 } else if (!php_http_match(Z_STRVAL_P(h
), "boundary=", PHP_HTTP_MATCH_WORD
)) {
260 Z_STRLEN_P(h
) = spprintf(&Z_STRVAL_P(h
), 0, "%s; boundary=\"%s\"", Z_STRVAL_P(h
), msg
->body
.boundary
);
261 zend_hash_update(&msg
->hdrs
, "Content-Type", sizeof("Content-Type"), &h
, sizeof(zval
*), NULL
);
268 FOREACH_HASH_KEYVAL(pos1
, &msg
->hdrs
, key
, header
) {
269 if (key
.type
== HASH_KEY_IS_STRING
) {
271 zval
**single_header
;
273 switch (Z_TYPE_PP(header
)) {
275 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_BVAL_PP(header
)?"true":"false");
279 php_http_buffer_appendf(str
, "%s: %ld" PHP_HTTP_CRLF
, key
.str
, Z_LVAL_PP(header
));
283 php_http_buffer_appendf(str
, "%s: %F" PHP_HTTP_CRLF
, key
.str
, Z_DVAL_PP(header
));
287 if (Z_STRVAL_PP(header
)[Z_STRLEN_PP(header
)-1] == '\r') fprintf(stderr
, "DOH!\n");
288 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_STRVAL_PP(header
));
292 FOREACH_VAL(pos2
, *header
, single_header
) {
293 switch (Z_TYPE_PP(single_header
)) {
295 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_BVAL_PP(single_header
)?"true":"false");
299 php_http_buffer_appendf(str
, "%s: %ld" PHP_HTTP_CRLF
, key
.str
, Z_LVAL_PP(single_header
));
303 php_http_buffer_appendf(str
, "%s: %F" PHP_HTTP_CRLF
, key
.str
, Z_DVAL_PP(single_header
));
307 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_STRVAL_PP(single_header
));
317 PHP_HTTP_API
void php_http_message_to_callback(php_http_message_t
*msg
, php_http_pass_callback_t cb
, void *cb_arg
)
319 php_http_buffer_t str
;
321 php_http_buffer_init_ex(&str
, 0x1000, 0);
322 message_headers(msg
, &str
);
323 cb(cb_arg
, PHP_HTTP_BUFFER_VAL(&str
), PHP_HTTP_BUFFER_LEN(&str
));
324 php_http_buffer_dtor(&str
);
326 if (php_http_message_body_size(&msg
->body
)) {
327 cb(cb_arg
, ZEND_STRL(PHP_HTTP_CRLF
));
328 php_http_message_body_to_callback(&msg
->body
, cb
, cb_arg
, 0, 0);
329 cb(cb_arg
, ZEND_STRL(PHP_HTTP_CRLF
));
333 PHP_HTTP_API
void php_http_message_to_string(php_http_message_t
*msg
, char **string
, size_t *length
)
335 php_http_buffer_t str
;
338 php_http_buffer_init_ex(&str
, 0x1000, 0);
339 message_headers(msg
, &str
);
340 if (php_http_message_body_size(&msg
->body
)) {
341 php_http_buffer_appends(&str
, PHP_HTTP_CRLF
);
342 php_http_message_body_to_callback(&msg
->body
, (php_http_pass_callback_t
) php_http_buffer_append
, &str
, 0, 0);
343 php_http_buffer_appends(&str
, PHP_HTTP_CRLF
);
346 data
= php_http_buffer_data(&str
, string
, length
);
351 php_http_buffer_dtor(&str
);
354 PHP_HTTP_API
void php_http_message_serialize(php_http_message_t
*message
, char **string
, size_t *length
)
358 php_http_buffer_t str
;
360 php_http_buffer_init(&str
);
363 php_http_message_to_string(message
, &buf
, &len
);
364 php_http_buffer_prepend(&str
, buf
, len
);
366 } while ((message
= message
->parent
));
368 buf
= php_http_buffer_data(&str
, string
, length
);
373 php_http_buffer_dtor(&str
);
376 PHP_HTTP_API php_http_message_t
*php_http_message_reverse(php_http_message_t
*msg
)
380 php_http_message_count(c
, msg
);
383 php_http_message_t
*tmp
= msg
, **arr
;
385 arr
= ecalloc(c
, sizeof(**arr
));
386 for (i
= 0; i
< c
; ++i
) {
390 arr
[0]->parent
= NULL
;
391 for (i
= 0; i
< c
-1; ++i
) {
392 arr
[i
+1]->parent
= arr
[i
];
402 PHP_HTTP_API php_http_message_t
*php_http_message_interconnect(php_http_message_t
*m1
, php_http_message_t
*m2
)
405 int i
= 0, c1
= 0, c2
= 0;
406 php_http_message_t
*t1
= m1
, *t2
= m2
, *p1
, *p2
;
408 php_http_message_count(c1
, m1
);
409 php_http_message_count(c2
, m2
);
411 while (i
++ < (c1
- c2
)) {
422 } else if (!m1
&& m2
) {
428 PHP_HTTP_API
void php_http_message_to_struct(php_http_message_t
*msg
, zval
*obj
)
433 TSRMLS_FETCH_FROM_CTX(msg
->ts
);
435 INIT_PZVAL_ARRAY(&strct
, HASH_OF(obj
));
437 add_assoc_long(&strct
, "type", msg
->type
);
438 spprintf(&version
, 0 ,"%u.%u", msg
->http
.version
.major
, msg
->http
.version
.minor
);
439 add_assoc_string_ex(&strct
, ZEND_STRL("httpVersion"), version
, 0);
442 case PHP_HTTP_RESPONSE
:
443 add_assoc_long(&strct
, "responseCode", msg
->http
.info
.response
.code
);
444 add_assoc_string(&strct
, "responseStatus", STR_PTR(msg
->http
.info
.response
.status
), 1);
447 case PHP_HTTP_REQUEST
:
448 add_assoc_string(&strct
, "requestMethod", STR_PTR(msg
->http
.info
.request
.method
), 1);
449 add_assoc_string(&strct
, "requestUrl", STR_PTR(msg
->http
.info
.request
.url
), 1);
453 /* avoid compiler warning */
457 MAKE_STD_ZVAL(headers
);
459 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
460 add_assoc_zval(&strct
, "headers", headers
);
462 add_assoc_stringl(&strct
, "body", PHP_HTTP_BUFFER_VAL(msg
), PHP_HTTP_BUFFER_LEN(msg
), 1);
467 MAKE_STD_ZVAL(parent
);
468 if (Z_TYPE_P(obj
) == IS_ARRAY
) {
473 add_assoc_zval(&strct
, "parentMessage", parent
);
474 php_http_message_to_struct(msg
->parent
, parent
);
476 add_assoc_null(&strct
, "parentMessage");
480 PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC)
484 switch (message->type) {
485 case PHP_HTTP_RESPONSE:
487 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
491 FOREACH_HASH_KEYVAL(pos, &message->hdrs, key, val) {
492 if (key.type == HASH_KEY_IS_STRING) {
493 http_send_header_zval_ex(key.str, key.len-1, val, 1);
496 rs = SUCCESS == http_send_status(message->http.info.response.code) &&
497 SUCCESS == http_send_data(PHP_HTTP_BUFFER_VAL(message), PHP_HTTP_BUFFER_LEN(message)) ?
502 case PHP_HTTP_REQUEST:
504 #ifdef PHP_HTTP_HAVE_CURL
506 http_request request;
507 zval **zhost, *options, *headers;
509 MAKE_STD_ZVAL(options);
510 MAKE_STD_ZVAL(headers);
513 zend_hash_copy(Z_ARRVAL_P(headers), &message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
514 add_assoc_zval(options, "headers", headers);
516 if (SUCCESS == zend_hash_find(&message->hdrs, "Host", sizeof("Host"), (void *) &zhost) && Z_TYPE_PP(zhost) == IS_STRING) {
518 php_url parts, *url = php_url_parse(message->http.info.request.url);
520 memset(&parts, 0, sizeof(php_url));
522 if ((colon = strchr(Z_STRVAL_PP(zhost), ':'))) {
523 parts.port = atoi(colon + 1);
524 parts.host = estrndup(Z_STRVAL_PP(zhost), (Z_STRVAL_PP(zhost) - colon - 1));
526 parts.host = estrndup(Z_STRVAL_PP(zhost), Z_STRLEN_PP(zhost));
529 http_build_url(PHP_HTTP_URL_REPLACE, url, &parts, NULL, &uri, NULL);
533 uri = http_absolute_url(message->http.info.request.url);
536 if ((request.meth = http_request_method_exists(1, 0, message->http.info.request.method))) {
537 http_request_body body;
539 http_request_init_ex(&request, NULL, request.meth, uri);
540 request.body = http_request_body_init_ex(&body, PHP_HTTP_REQUEST_BODY_CSTRING, PHP_HTTP_BUFFER_VAL(message), PHP_HTTP_BUFFER_LEN(message), 0);
541 if (SUCCESS == (rs = http_request_prepare(&request, Z_ARRVAL_P(options)))) {
542 http_request_exec(&request);
544 http_request_dtor(&request);
546 http_error_ex(HE_WARNING, PHP_HTTP_E_REQUEST_METHOD,
547 "Cannot send HttpMessage. Request method %s not supported",
548 message->http.info.request.method);
551 zval_ptr_dtor(&options);
553 http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "HTTP requests not supported - ext/http was not linked against libcurl.");
560 php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type PHP_HTTP_REQUEST nor PHP_HTTP_RESPONSE");
568 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
)
570 php_http_message_t
*temp
, *copy
= NULL
;
571 php_http_info_t info
;
572 TSRMLS_FETCH_FROM_CTX(from
->ts
);
575 info
.type
= from
->type
;
576 info
.http
= from
->http
;
578 copy
= temp
= php_http_message_init(to
, 0 TSRMLS_CC
);
579 php_http_message_set_info(temp
, &info
);
580 zend_hash_copy(&temp
->hdrs
, &from
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
581 php_http_message_body_copy(&from
->body
, &temp
->body
, 1);
583 if (parents
) while (from
->parent
) {
584 info
.type
= from
->parent
->type
;
585 info
.http
= from
->parent
->http
;
587 temp
->parent
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
588 php_http_message_set_info(temp
->parent
, &info
);
589 zend_hash_copy(&temp
->parent
->hdrs
, &from
->parent
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
590 php_http_message_body_copy(&from
->body
, &temp
->body
, 1);
600 PHP_HTTP_API php_http_message_t
*php_http_message_copy(php_http_message_t
*from
, php_http_message_t
*to
)
602 return php_http_message_copy_ex(from
, to
, 1);
605 PHP_HTTP_API
void php_http_message_dtor(php_http_message_t
*message
)
608 zend_hash_destroy(&message
->hdrs
);
609 php_http_message_body_dtor(&message
->body
);
611 switch (message
->type
) {
612 case PHP_HTTP_REQUEST
:
613 STR_SET(message
->http
.info
.request
.method
, NULL
);
614 STR_SET(message
->http
.info
.request
.url
, NULL
);
617 case PHP_HTTP_RESPONSE
:
618 STR_SET(message
->http
.info
.response
.status
, NULL
);
627 PHP_HTTP_API
void php_http_message_free(php_http_message_t
**message
)
630 if ((*message
)->parent
) {
631 php_http_message_free(&(*message
)->parent
);
633 php_http_message_dtor(*message
);
639 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpMessage, method, 0, req_args)
640 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpMessage, method, 0)
641 #define PHP_HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, PHP_HTTP_ARGS(HttpMessage, method), visibility)
643 PHP_HTTP_BEGIN_ARGS(__construct
, 0)
644 PHP_HTTP_ARG_VAL(message
, 0)
647 PHP_HTTP_EMPTY_ARGS(getBody
);
648 PHP_HTTP_BEGIN_ARGS(setBody
, 1)
649 PHP_HTTP_ARG_VAL(body
, 0)
652 PHP_HTTP_BEGIN_ARGS(getHeader
, 1)
653 PHP_HTTP_ARG_VAL(header
, 0)
656 PHP_HTTP_BEGIN_ARGS(setHeader
, 1)
657 PHP_HTTP_ARG_VAL(header
, 0)
658 PHP_HTTP_ARG_VAL(value
, 0)
661 PHP_HTTP_BEGIN_ARGS(addHeader
, 2)
662 PHP_HTTP_ARG_VAL(header
, 0)
663 PHP_HTTP_ARG_VAL(value
, 0)
666 PHP_HTTP_EMPTY_ARGS(getHeaders
);
667 PHP_HTTP_BEGIN_ARGS(setHeaders
, 1)
668 PHP_HTTP_ARG_VAL(headers
, 0)
671 PHP_HTTP_BEGIN_ARGS(addHeaders
, 1)
672 PHP_HTTP_ARG_VAL(headers
, 0)
673 PHP_HTTP_ARG_VAL(append
, 0)
676 PHP_HTTP_EMPTY_ARGS(getType
);
677 PHP_HTTP_BEGIN_ARGS(setType
, 1)
678 PHP_HTTP_ARG_VAL(type
, 0)
681 PHP_HTTP_EMPTY_ARGS(getInfo
);
682 PHP_HTTP_BEGIN_ARGS(setInfo
, 1)
683 PHP_HTTP_ARG_VAL(http_info
, 0)
686 PHP_HTTP_EMPTY_ARGS(getResponseCode
);
687 PHP_HTTP_BEGIN_ARGS(setResponseCode
, 1)
688 PHP_HTTP_ARG_VAL(response_code
, 0)
691 PHP_HTTP_EMPTY_ARGS(getResponseStatus
);
692 PHP_HTTP_BEGIN_ARGS(setResponseStatus
, 1)
693 PHP_HTTP_ARG_VAL(response_status
, 0)
696 PHP_HTTP_EMPTY_ARGS(getRequestMethod
);
697 PHP_HTTP_BEGIN_ARGS(setRequestMethod
, 1)
698 PHP_HTTP_ARG_VAL(request_method
, 0)
701 PHP_HTTP_EMPTY_ARGS(getRequestUrl
);
702 PHP_HTTP_BEGIN_ARGS(setRequestUrl
, 1)
703 PHP_HTTP_ARG_VAL(url
, 0)
706 PHP_HTTP_EMPTY_ARGS(getHttpVersion
);
707 PHP_HTTP_BEGIN_ARGS(setHttpVersion
, 1)
708 PHP_HTTP_ARG_VAL(http_version
, 0)
711 PHP_HTTP_EMPTY_ARGS(getParentMessage
);
712 PHP_HTTP_EMPTY_ARGS(__toString
);
713 PHP_HTTP_BEGIN_ARGS(toString
, 0)
714 PHP_HTTP_ARG_VAL(include_parent
, 0)
717 PHP_HTTP_EMPTY_ARGS(count
);
719 PHP_HTTP_EMPTY_ARGS(serialize
);
720 PHP_HTTP_BEGIN_ARGS(unserialize
, 1)
721 PHP_HTTP_ARG_VAL(serialized
, 0)
724 PHP_HTTP_EMPTY_ARGS(rewind
);
725 PHP_HTTP_EMPTY_ARGS(valid
);
726 PHP_HTTP_EMPTY_ARGS(key
);
727 PHP_HTTP_EMPTY_ARGS(current
);
728 PHP_HTTP_EMPTY_ARGS(next
);
730 PHP_HTTP_EMPTY_ARGS(detach
);
731 PHP_HTTP_BEGIN_ARGS(prepend
, 1)
732 PHP_HTTP_ARG_OBJ(http
\\Message
, message
, 0)
734 PHP_HTTP_EMPTY_ARGS(reverse
);
736 static zval
*php_http_message_object_read_prop(zval
*object
, zval
*member
, int type
, const zend_literal
*literal_key TSRMLS_DC
);
737 static void php_http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value
, const zend_literal
*literal_key TSRMLS_DC
);
738 static zval
**php_http_message_object_get_prop_ptr(zval
*object
, zval
*member
, const zend_literal
*literal_key TSRMLS_DC
);
739 static HashTable
*php_http_message_object_get_props(zval
*object TSRMLS_DC
);
741 zend_class_entry
*php_http_message_class_entry
;
742 zend_function_entry php_http_message_method_entry
[] = {
743 PHP_HTTP_MESSAGE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
744 PHP_HTTP_MESSAGE_ME(getBody
, ZEND_ACC_PUBLIC
)
745 PHP_HTTP_MESSAGE_ME(setBody
, ZEND_ACC_PUBLIC
)
746 PHP_HTTP_MESSAGE_ME(getHeader
, ZEND_ACC_PUBLIC
)
747 PHP_HTTP_MESSAGE_ME(setHeader
, ZEND_ACC_PUBLIC
)
748 PHP_HTTP_MESSAGE_ME(addHeader
, ZEND_ACC_PUBLIC
)
749 PHP_HTTP_MESSAGE_ME(getHeaders
, ZEND_ACC_PUBLIC
)
750 PHP_HTTP_MESSAGE_ME(setHeaders
, ZEND_ACC_PUBLIC
)
751 PHP_HTTP_MESSAGE_ME(addHeaders
, ZEND_ACC_PUBLIC
)
752 PHP_HTTP_MESSAGE_ME(getType
, ZEND_ACC_PUBLIC
)
753 PHP_HTTP_MESSAGE_ME(setType
, ZEND_ACC_PUBLIC
)
754 PHP_HTTP_MESSAGE_ME(getInfo
, ZEND_ACC_PUBLIC
)
755 PHP_HTTP_MESSAGE_ME(setInfo
, ZEND_ACC_PUBLIC
)
756 PHP_HTTP_MESSAGE_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
757 PHP_HTTP_MESSAGE_ME(setResponseCode
, ZEND_ACC_PUBLIC
)
758 PHP_HTTP_MESSAGE_ME(getResponseStatus
, ZEND_ACC_PUBLIC
)
759 PHP_HTTP_MESSAGE_ME(setResponseStatus
, ZEND_ACC_PUBLIC
)
760 PHP_HTTP_MESSAGE_ME(getRequestMethod
, ZEND_ACC_PUBLIC
)
761 PHP_HTTP_MESSAGE_ME(setRequestMethod
, ZEND_ACC_PUBLIC
)
762 PHP_HTTP_MESSAGE_ME(getRequestUrl
, ZEND_ACC_PUBLIC
)
763 PHP_HTTP_MESSAGE_ME(setRequestUrl
, ZEND_ACC_PUBLIC
)
764 PHP_HTTP_MESSAGE_ME(getHttpVersion
, ZEND_ACC_PUBLIC
)
765 PHP_HTTP_MESSAGE_ME(setHttpVersion
, ZEND_ACC_PUBLIC
)
766 PHP_HTTP_MESSAGE_ME(getParentMessage
, ZEND_ACC_PUBLIC
)
767 PHP_HTTP_MESSAGE_ME(toString
, ZEND_ACC_PUBLIC
)
769 /* implements Countable */
770 PHP_HTTP_MESSAGE_ME(count
, ZEND_ACC_PUBLIC
)
772 /* implements Serializable */
773 PHP_HTTP_MESSAGE_ME(serialize
, ZEND_ACC_PUBLIC
)
774 PHP_HTTP_MESSAGE_ME(unserialize
, ZEND_ACC_PUBLIC
)
776 /* implements Iterator */
777 PHP_HTTP_MESSAGE_ME(rewind
, ZEND_ACC_PUBLIC
)
778 PHP_HTTP_MESSAGE_ME(valid
, ZEND_ACC_PUBLIC
)
779 PHP_HTTP_MESSAGE_ME(current
, ZEND_ACC_PUBLIC
)
780 PHP_HTTP_MESSAGE_ME(key
, ZEND_ACC_PUBLIC
)
781 PHP_HTTP_MESSAGE_ME(next
, ZEND_ACC_PUBLIC
)
783 ZEND_MALIAS(HttpMessage
, __toString
, toString
, PHP_HTTP_ARGS(HttpMessage
, __toString
), ZEND_ACC_PUBLIC
)
785 PHP_HTTP_MESSAGE_ME(detach
, ZEND_ACC_PUBLIC
)
786 PHP_HTTP_MESSAGE_ME(prepend
, ZEND_ACC_PUBLIC
)
787 PHP_HTTP_MESSAGE_ME(reverse
, ZEND_ACC_PUBLIC
)
791 static zend_object_handlers php_http_message_object_handlers
;
792 static HashTable php_http_message_object_prophandlers
;
794 typedef void (*php_http_message_object_prophandler_func_t
)(php_http_message_object_t
*o
, zval
*v TSRMLS_DC
);
796 typedef struct php_http_message_object_prophandler
{
797 php_http_message_object_prophandler_func_t read
;
798 php_http_message_object_prophandler_func_t write
;
799 } php_http_message_object_prophandler_t
;
801 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
) {
802 php_http_message_object_prophandler_t h
= { read
, write
};
803 return zend_hash_add(&php_http_message_object_prophandlers
, prop_str
, prop_len
+ 1, (void *) &h
, sizeof(h
), NULL
);
806 static int php_http_message_object_has_prophandler(const char *prop_str, size_t prop_len) {
807 return zend_hash_exists(&php_http_message_object_prophandlers, prop_str, prop_len + 1);
810 static STATUS
php_http_message_object_get_prophandler(const char *prop_str
, size_t prop_len
, php_http_message_object_prophandler_t
**handler
) {
811 return zend_hash_find(&php_http_message_object_prophandlers
, prop_str
, prop_len
+ 1, (void *) handler
);
813 static void php_http_message_object_prophandler_get_type(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
814 RETVAL_LONG(obj
->message
->type
);
816 static void php_http_message_object_prophandler_set_type(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
817 zval
*cpy
= php_http_ztyp(IS_LONG
, value
);
818 php_http_message_set_type(obj
->message
, Z_LVAL_P(cpy
));
821 static void php_http_message_object_prophandler_get_request_method(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
822 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
) && obj
->message
->http
.info
.request
.method
) {
823 RETVAL_STRING(obj
->message
->http
.info
.request
.method
, 1);
828 static void php_http_message_object_prophandler_set_request_method(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
829 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
830 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
831 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
835 static void php_http_message_object_prophandler_get_request_url(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
836 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
) && obj
->message
->http
.info
.request
.url
) {
837 RETVAL_STRING(obj
->message
->http
.info
.request
.url
, 1);
842 static void php_http_message_object_prophandler_set_request_url(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
843 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
844 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
845 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
849 static void php_http_message_object_prophandler_get_response_status(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
850 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
) && obj
->message
->http
.info
.response
.status
) {
851 RETVAL_STRING(obj
->message
->http
.info
.response
.status
, 1);
856 static void php_http_message_object_prophandler_set_response_status(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
857 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
858 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
859 STR_SET(obj
->message
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
863 static void php_http_message_object_prophandler_get_response_code(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
864 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
865 RETVAL_LONG(obj
->message
->http
.info
.response
.code
);
870 static void php_http_message_object_prophandler_set_response_code(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
871 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
872 zval
*cpy
= php_http_ztyp(IS_LONG
, value
);
873 obj
->message
->http
.info
.response
.code
= Z_LVAL_P(cpy
);
877 static void php_http_message_object_prophandler_get_http_version(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
881 php_http_version_to_string(&obj
->message
->http
.version
, &version_str
, &version_len
, NULL
, NULL TSRMLS_CC
);
882 RETVAL_STRINGL(version_str
, version_len
, 0);
884 static void php_http_message_object_prophandler_set_http_version(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
885 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
886 php_http_version_parse(&obj
->message
->http
.version
, Z_STRVAL_P(cpy
) TSRMLS_CC
);
889 static void php_http_message_object_prophandler_get_headers(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
890 array_init(return_value
);
891 zend_hash_copy(Z_ARRVAL_P(return_value
), &obj
->message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
893 static void php_http_message_object_prophandler_set_headers(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
894 zval
*cpy
= php_http_ztyp(IS_ARRAY
, value
);
896 zend_hash_clean(&obj
->message
->hdrs
);
897 zend_hash_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(cpy
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
900 static void php_http_message_object_prophandler_get_body(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
901 if (obj
->body
.handle
) {
902 RETVAL_OBJVAL(obj
->body
, 1);
907 static void php_http_message_object_prophandler_set_body(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
908 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), php_http_message_body_class_entry TSRMLS_CC
)) {
909 if (obj
->body
.handle
) {
910 zend_objects_store_del_ref_by_handle(obj
->body
.handle TSRMLS_CC
);
912 Z_OBJ_ADDREF_P(value
);
913 obj
->body
= Z_OBJVAL_P(value
);
916 static void php_http_message_object_prophandler_get_parent_message(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
917 if (obj
->message
->parent
) {
918 RETVAL_OBJVAL(obj
->parent
, 1);
923 static void php_http_message_object_prophandler_set_parent_message(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
924 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), php_http_message_class_entry TSRMLS_CC
)) {
925 if (obj
->message
->parent
) {
926 zend_objects_store_del_ref_by_handle(obj
->parent
.handle TSRMLS_CC
);
928 Z_OBJ_ADDREF_P(value
);
929 obj
->parent
= Z_OBJVAL_P(value
);
933 PHP_MINIT_FUNCTION(http_message
)
935 PHP_HTTP_REGISTER_CLASS(http
, Message
, http_message
, php_http_object_class_entry
, 0);
936 php_http_message_class_entry
->create_object
= php_http_message_object_new
;
937 memcpy(&php_http_message_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
938 php_http_message_object_handlers
.clone_obj
= php_http_message_object_clone
;
939 php_http_message_object_handlers
.read_property
= php_http_message_object_read_prop
;
940 php_http_message_object_handlers
.write_property
= php_http_message_object_write_prop
;
941 php_http_message_object_handlers
.get_properties
= php_http_message_object_get_props
;
942 php_http_message_object_handlers
.get_property_ptr_ptr
= php_http_message_object_get_prop_ptr
;
944 zend_class_implements(php_http_message_class_entry TSRMLS_CC
, 3, spl_ce_Countable
, zend_ce_serializable
, zend_ce_iterator
);
946 zend_hash_init(&php_http_message_object_prophandlers
, 9, NULL
, NULL
, 1);
947 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("type"), PHP_HTTP_NONE
, ZEND_ACC_PROTECTED TSRMLS_CC
);
948 php_http_message_object_add_prophandler(ZEND_STRL("type"), php_http_message_object_prophandler_get_type
, php_http_message_object_prophandler_set_type
);
949 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("body"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
950 php_http_message_object_add_prophandler(ZEND_STRL("body"), php_http_message_object_prophandler_get_body
, php_http_message_object_prophandler_set_body
);
951 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestMethod"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
952 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
);
953 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestUrl"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
954 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
);
955 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("responseStatus"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
956 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
);
957 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("responseCode"), 0, ZEND_ACC_PROTECTED TSRMLS_CC
);
958 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
);
959 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("httpVersion"), ZEND_ACC_PROTECTED TSRMLS_CC
);
960 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
);
961 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("headers"), ZEND_ACC_PROTECTED TSRMLS_CC
);
962 php_http_message_object_add_prophandler(ZEND_STRL("headers"), php_http_message_object_prophandler_get_headers
, php_http_message_object_prophandler_set_headers
);
963 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("parentMessage"), ZEND_ACC_PROTECTED TSRMLS_CC
);
964 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
);
966 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_NONE"), PHP_HTTP_NONE TSRMLS_CC
);
967 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_REQUEST"), PHP_HTTP_REQUEST TSRMLS_CC
);
968 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_RESPONSE"), PHP_HTTP_RESPONSE TSRMLS_CC
);
973 PHP_MSHUTDOWN_FUNCTION(http_message
)
975 zend_hash_destroy(&php_http_message_object_prophandlers
);
980 void php_http_message_object_reverse(zval
*this_ptr
, zval
*return_value TSRMLS_DC
)
983 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
986 php_http_message_count(i
, obj
->message
);
990 zend_object_value
*ovalues
= NULL
;
991 php_http_message_object_t
**objects
= NULL
;
994 objects
= ecalloc(i
, sizeof(**objects
));
995 ovalues
= ecalloc(i
, sizeof(*ovalues
));
997 /* we are the first message */
999 ovalues
[0] = getThis()->value
.obj
;
1004 for (i
= 1; obj
->parent
.handle
; ++i
) {
1005 o
.value
.obj
= obj
->parent
;
1006 ovalues
[i
] = o
.value
.obj
;
1007 objects
[i
] = obj
= zend_object_store_get_object(&o TSRMLS_CC
);
1010 /* reorder parents */
1011 for (last
= --i
; i
; --i
) {
1012 objects
[i
]->message
->parent
= objects
[i
-1]->message
;
1013 objects
[i
]->parent
= ovalues
[i
-1];
1015 objects
[0]->message
->parent
= NULL
;
1016 objects
[0]->parent
.handle
= 0;
1017 objects
[0]->parent
.handlers
= NULL
;
1019 /* add ref (why?) */
1020 Z_OBJ_ADDREF_P(getThis());
1021 RETVAL_OBJVAL(ovalues
[last
], 1);
1026 RETURN_ZVAL(getThis(), 1, 0);
1030 void php_http_message_object_prepend(zval
*this_ptr
, zval
*prepend
, zend_bool top TSRMLS_DC
)
1033 php_http_message_t
*save_parent_msg
= NULL
;
1034 zend_object_value save_parent_obj
= {0, NULL
};
1035 php_http_message_object_t
*obj
= zend_object_store_get_object(this_ptr TSRMLS_CC
);
1036 php_http_message_object_t
*prepend_obj
= zend_object_store_get_object(prepend TSRMLS_CC
);
1042 save_parent_obj
= obj
->parent
;
1043 save_parent_msg
= obj
->message
->parent
;
1045 /* iterate to the most parent object */
1046 while (obj
->parent
.handle
) {
1047 m
.value
.obj
= obj
->parent
;
1048 obj
= zend_object_store_get_object(&m TSRMLS_CC
);
1053 obj
->parent
= prepend
->value
.obj
;
1054 obj
->message
->parent
= prepend_obj
->message
;
1057 zend_objects_store_add_ref(prepend TSRMLS_CC
);
1058 while (prepend_obj
->parent
.handle
) {
1059 m
.value
.obj
= prepend_obj
->parent
;
1060 zend_objects_store_add_ref(&m TSRMLS_CC
);
1061 prepend_obj
= zend_object_store_get_object(&m TSRMLS_CC
);
1065 prepend_obj
->parent
= save_parent_obj
;
1066 prepend_obj
->message
->parent
= save_parent_msg
;
1070 zend_object_value
php_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
1072 return php_http_message_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
1075 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
)
1077 zend_object_value ov
;
1078 php_http_message_object_t
*o
;
1080 o
= ecalloc(1, sizeof(php_http_message_object_t
));
1081 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
1082 object_properties_init((zend_object
*) o
, ce
);
1091 o
->parent
= php_http_message_object_new_ex(ce
, msg
->parent
, NULL TSRMLS_CC
);
1093 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
);
1096 ov
.handle
= zend_objects_store_put((zend_object
*) o
, NULL
, php_http_message_object_free
, NULL TSRMLS_CC
);
1097 ov
.handlers
= &php_http_message_object_handlers
;
1102 zend_object_value
php_http_message_object_clone(zval
*this_ptr TSRMLS_DC
)
1104 zend_object_value new_ov
;
1105 php_http_message_object_t
*new_obj
= NULL
;
1106 php_http_message_object_t
*old_obj
= zend_object_store_get_object(this_ptr TSRMLS_CC
);
1108 new_ov
= php_http_message_object_new_ex(old_obj
->zo
.ce
, php_http_message_copy(old_obj
->message
, NULL
), &new_obj TSRMLS_CC
);
1109 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
1114 void php_http_message_object_free(void *object TSRMLS_DC
)
1116 php_http_message_object_t
*o
= (php_http_message_object_t
*) object
;
1119 zval_ptr_dtor(&o
->iterator
);
1123 /* do NOT free recursivly */
1124 php_http_message_dtor(o
->message
);
1128 if (o
->parent
.handle
) {
1129 zend_objects_store_del_ref_by_handle(o
->parent
.handle TSRMLS_CC
);
1131 if (o
->body
.handle
) {
1132 zend_objects_store_del_ref_by_handle(o
->body
.handle TSRMLS_CC
);
1134 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
1139 static zval
**php_http_message_object_get_prop_ptr(zval
*object
, zval
*member
, const zend_literal
*literal_key TSRMLS_DC
)
1141 php_http_message_object_prophandler_t
*handler
;
1142 zval
*copy
= php_http_ztyp(IS_STRING
, member
);
1144 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1145 zval_ptr_dtor(©
);
1146 return &php_http_property_proxy_init(NULL
, object
, member TSRMLS_CC
)->myself
;
1148 zval_ptr_dtor(©
);
1150 return zend_get_std_object_handlers()->get_property_ptr_ptr(object
, member
, literal_key TSRMLS_CC
);
1153 static zval
*php_http_message_object_read_prop(zval
*object
, zval
*member
, int type
, const zend_literal
*literal_key TSRMLS_DC
)
1155 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1156 php_http_message_object_prophandler_t
*handler
;
1157 zval
*return_value
, *copy
= php_http_ztyp(IS_STRING
, member
);
1159 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1160 if (type
== BP_VAR_W
) {
1161 zval_ptr_dtor(©
);
1162 zend_error(E_ERROR
, "Cannot access HttpMessage properties by reference or array key/index");
1166 ALLOC_ZVAL(return_value
);
1167 Z_SET_REFCOUNT_P(return_value
, 0);
1168 Z_UNSET_ISREF_P(return_value
);
1170 handler
->read(obj
, return_value TSRMLS_CC
);
1173 return_value
= zend_get_std_object_handlers()->read_property(object
, member
, type
, literal_key TSRMLS_CC
);
1176 zval_ptr_dtor(©
);
1178 return return_value
;
1181 static void php_http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value
, const zend_literal
*literal_key TSRMLS_DC
)
1183 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1184 php_http_message_object_prophandler_t
*handler
;
1185 zval
*copy
= php_http_ztyp(IS_STRING
, member
);
1187 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1188 handler
->write(obj
, value TSRMLS_CC
);
1190 zend_get_std_object_handlers()->write_property(object
, member
, value
, literal_key TSRMLS_CC
);
1193 zval_ptr_dtor(©
);
1197 static HashTable
*php_http_message_object_get_props(zval
*object TSRMLS_DC
)
1200 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1201 php_http_message_t
*msg
= obj
->message
;
1202 HashTable
*props
= zend_get_std_object_handlers()->get_properties(object TSRMLS_CC
);
1203 zval array
, *parent
, *body
;
1206 INIT_PZVAL_ARRAY(&array
, props
);
1208 #define ASSOC_PROP(array, ptype, name, val) \
1210 char *m_prop_name; \
1212 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
1213 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
1214 efree(m_prop_name); \
1216 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
1217 #define ASSOC_STRINGL(array, name, val, len) ASSOC_STRINGL_EX(array, name, val, len, 1)
1218 #define ASSOC_STRINGL_EX(array, name, val, len, cpy) \
1220 char *m_prop_name; \
1222 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
1223 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, cpy); \
1224 efree(m_prop_name); \
1227 ASSOC_PROP(array
, long, "type", msg
->type
);
1228 ASSOC_STRINGL_EX(array
, "httpVersion", version
, spprintf(&version
, 0, "%u.%u", msg
->http
.version
.major
, msg
->http
.version
.minor
), 0);
1230 switch (msg
->type
) {
1231 case PHP_HTTP_REQUEST
:
1232 ASSOC_PROP(array
, long, "responseCode", 0);
1233 ASSOC_STRINGL(array
, "responseStatus", "", 0);
1234 ASSOC_STRING(array
, "requestMethod", STR_PTR(msg
->http
.info
.request
.method
));
1235 ASSOC_STRING(array
, "requestUrl", STR_PTR(msg
->http
.info
.request
.url
));
1238 case PHP_HTTP_RESPONSE
:
1239 ASSOC_PROP(array
, long, "responseCode", msg
->http
.info
.response
.code
);
1240 ASSOC_STRING(array
, "responseStatus", STR_PTR(msg
->http
.info
.response
.status
));
1241 ASSOC_STRINGL(array
, "requestMethod", "", 0);
1242 ASSOC_STRINGL(array
, "requestUrl", "", 0);
1247 ASSOC_PROP(array
, long, "responseCode", 0);
1248 ASSOC_STRINGL(array
, "responseStatus", "", 0);
1249 ASSOC_STRINGL(array
, "requestMethod", "", 0);
1250 ASSOC_STRINGL(array
, "requestUrl", "", 0);
1254 MAKE_STD_ZVAL(headers
);
1255 array_init(headers
);
1256 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1257 ASSOC_PROP(array
, zval
, "headers", headers
);
1259 MAKE_STD_ZVAL(body
);
1260 if (!obj
->body
.handle
) {
1261 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
);
1263 ZVAL_OBJVAL(body
, obj
->body
, 1);
1264 ASSOC_PROP(array
, zval
, "body", body
);
1266 MAKE_STD_ZVAL(parent
);
1268 ZVAL_OBJVAL(parent
, obj
->parent
, 1);
1272 ASSOC_PROP(array
, zval
, "parentMessage", parent
);
1279 PHP_METHOD(HttpMessage
, __construct
)
1282 char *message
= NULL
;
1283 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1285 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1286 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
1287 php_http_message_t
*msg
= obj
->message
;
1289 php_http_message_dtor(msg
);
1290 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1291 if ((obj
->message
= php_http_message_parse(msg
, message
, length TSRMLS_CC
))) {
1292 if (obj
->message
->parent
) {
1293 obj
->parent
= php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj
->message
->parent
, NULL TSRMLS_CC
);
1296 obj
->message
= php_http_message_init(msg
, 0 TSRMLS_CC
);
1298 } end_error_handling();
1300 if (!obj
->message
) {
1301 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1303 } end_error_handling();
1307 PHP_METHOD(HttpMessage
, getBody
)
1309 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1310 if (SUCCESS
== zend_parse_parameters_none()) {
1311 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1313 if (!obj
->message
) {
1314 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1317 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
)) {
1318 RETVAL_OBJVAL(obj
->body
, 1);
1321 } end_error_handling();
1324 PHP_METHOD(HttpMessage
, setBody
)
1328 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &zbody
, php_http_message_body_class_entry
)) {
1329 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1330 php_http_message_body_object_t
*body_obj
= zend_object_store_get_object(zbody TSRMLS_CC
);
1332 if (!obj
->message
) {
1333 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1336 php_http_message_body_dtor(&obj
->message
->body
);
1337 php_http_message_body_copy(body_obj
->body
, &obj
->message
->body
, 0);
1338 Z_OBJ_ADDREF_P(zbody
);
1339 obj
->body
= Z_OBJVAL_P(zbody
);
1341 RETVAL_ZVAL(getThis(), 1, 0);
1344 PHP_METHOD(HttpMessage
, getHeader
)
1349 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header_str
, &header_len
)) {
1350 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1353 if (!obj
->message
) {
1354 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1357 if ((header
= php_http_message_header(obj
->message
, header_str
, header_len
, 0))) {
1358 RETURN_ZVAL(header
, 1, 1);
1364 PHP_METHOD(HttpMessage
, getHeaders
)
1366 if (SUCCESS
== zend_parse_parameters_none()) {
1367 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1369 if (!obj
->message
) {
1370 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1373 array_init(return_value
);
1374 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(return_value
));
1378 PHP_METHOD(HttpMessage
, setHeader
)
1380 zval
*zvalue
= NULL
;
1384 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|z!", &name_str
, &name_len
, &zvalue
)) {
1385 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1386 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1388 if (!obj
->message
) {
1389 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1393 zend_symtable_del(&obj
->message
->hdrs
, name
, name_len
+ 1);
1396 zend_symtable_update(&obj
->message
->hdrs
, name
, name_len
+ 1, &zvalue
, sizeof(void *), NULL
);
1400 RETVAL_ZVAL(getThis(), 1, 0);
1403 PHP_METHOD(HttpMessage
, setHeaders
)
1405 zval
*new_headers
= NULL
;
1407 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &new_headers
)) {
1408 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1410 if (!obj
->message
) {
1411 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1414 zend_hash_clean(&obj
->message
->hdrs
);
1416 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, 0, ARRAY_JOIN_PRETTIFY
|ARRAY_JOIN_STRONLY
);
1419 RETVAL_ZVAL(getThis(), 1, 0);
1422 PHP_METHOD(HttpMessage
, addHeader
)
1428 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sz", &name_str
, &name_len
, &zvalue
)) {
1429 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1430 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1433 if (!obj
->message
) {
1434 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1438 if ((header
= php_http_message_header(obj
->message
, name
, name_len
, 0))) {
1439 convert_to_array(header
);
1440 zend_hash_next_index_insert(Z_ARRVAL_P(header
), &zvalue
, sizeof(void *), NULL
);
1442 zend_symtable_update(&obj
->message
->hdrs
, name
, name_len
+ 1, &zvalue
, sizeof(void *), NULL
);
1446 RETVAL_ZVAL(getThis(), 1, 0);
1449 PHP_METHOD(HttpMessage
, addHeaders
)
1452 zend_bool append
= 0;
1454 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
1455 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1457 if (!obj
->message
) {
1458 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1461 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, append
, ARRAY_JOIN_STRONLY
|ARRAY_JOIN_PRETTIFY
);
1463 RETVAL_ZVAL(getThis(), 1, 0);
1466 PHP_METHOD(HttpMessage
, getType
)
1468 if (SUCCESS
== zend_parse_parameters_none()) {
1469 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1471 if (!obj
->message
) {
1472 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1475 RETURN_LONG(obj
->message
->type
);
1479 PHP_METHOD(HttpMessage
, setType
)
1483 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
1484 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1486 if (!obj
->message
) {
1487 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1490 php_http_message_set_type(obj
->message
, type
);
1492 RETVAL_ZVAL(getThis(), 1, 0);
1495 PHP_METHOD(HttpMessage
, getInfo
)
1497 if (SUCCESS
== zend_parse_parameters_none()) {
1498 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1500 if (!obj
->message
) {
1501 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1504 switch (obj
->message
->type
) {
1505 case PHP_HTTP_REQUEST
:
1506 Z_STRLEN_P(return_value
) = spprintf(&Z_STRVAL_P(return_value
), 0, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&obj
->message
->http
, ""));
1508 case PHP_HTTP_RESPONSE
:
1509 Z_STRLEN_P(return_value
) = spprintf(&Z_STRVAL_P(return_value
), 0, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&obj
->message
->http
, ""));
1515 Z_TYPE_P(return_value
) = IS_STRING
;
1521 PHP_METHOD(HttpMessage
, setInfo
)
1525 php_http_info_t inf
;
1527 if ( SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)
1528 && php_http_info_parse(&inf
, str TSRMLS_CC
)) {
1529 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1531 if (!obj
->message
) {
1532 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1535 php_http_message_set_info(obj
->message
, &inf
);
1536 php_http_info_dtor(&inf
);
1538 RETVAL_ZVAL(getThis(), 1, 0);
1541 PHP_METHOD(HttpMessage
, getHttpVersion
)
1543 if (SUCCESS
== zend_parse_parameters_none()) {
1546 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1548 if (!obj
->message
) {
1549 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1552 php_http_version_to_string(&obj
->message
->http
.version
, &str
, &len
, NULL
, NULL TSRMLS_CC
);
1553 RETURN_STRINGL(str
, len
, 0);
1559 PHP_METHOD(HttpMessage
, setHttpVersion
)
1564 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &v_str
, &v_len
)) {
1565 php_http_version_t version
;
1566 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1568 if (!obj
->message
) {
1569 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1572 if (php_http_version_parse(&version
, v_str TSRMLS_CC
)) {
1573 obj
->message
->http
.version
= version
;
1576 RETVAL_ZVAL(getThis(), 1, 0);
1579 PHP_METHOD(HttpMessage
, getResponseCode
)
1581 if (SUCCESS
== zend_parse_parameters_none()) {
1582 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1584 if (!obj
->message
) {
1585 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1588 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1589 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
1594 PHP_METHOD(HttpMessage
, setResponseCode
)
1597 zend_bool strict
= 1;
1599 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l|b", &code
, &strict
)) {
1600 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1602 if (!obj
->message
) {
1603 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1606 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1607 if (strict
&& (code
< 100 || code
> 599)) {
1608 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Invalid response code (100-599): %ld", code
);
1612 obj
->message
->http
.info
.response
.code
= code
;
1614 RETVAL_ZVAL(getThis(), 1, 0);
1617 PHP_METHOD(HttpMessage
, getResponseStatus
)
1619 if (SUCCESS
== zend_parse_parameters_none()) {
1620 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1622 if (!obj
->message
) {
1623 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1626 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1627 if (obj
->message
->http
.info
.response
.status
) {
1628 RETURN_STRING(obj
->message
->http
.info
.response
.status
, 1);
1630 RETURN_EMPTY_STRING();
1637 PHP_METHOD(HttpMessage
, setResponseStatus
)
1642 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &status
, &status_len
)) {
1643 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1645 if (!obj
->message
) {
1646 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1649 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1650 STR_SET(obj
->message
->http
.info
.response
.status
, estrndup(status
, status_len
));
1652 RETVAL_ZVAL(getThis(), 1, 0);
1655 PHP_METHOD(HttpMessage
, getRequestMethod
)
1657 if (SUCCESS
== zend_parse_parameters_none()) {
1658 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1660 if (!obj
->message
) {
1661 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1664 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1665 if (obj
->message
->http
.info
.request
.method
) {
1666 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
1668 RETURN_EMPTY_STRING();
1675 PHP_METHOD(HttpMessage
, setRequestMethod
)
1680 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1681 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1683 if (!obj
->message
) {
1684 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1687 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1688 if (method_len
< 1) {
1689 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
1693 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
1695 RETVAL_ZVAL(getThis(), 1, 0);
1698 PHP_METHOD(HttpMessage
, getRequestUrl
)
1700 if (SUCCESS
== zend_parse_parameters_none()) {
1701 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1703 if (!obj
->message
) {
1704 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1707 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1708 if (obj
->message
->http
.info
.request
.url
) {
1709 RETURN_STRING(obj
->message
->http
.info
.request
.url
, 1);
1711 RETURN_EMPTY_STRING();
1718 PHP_METHOD(HttpMessage
, setRequestUrl
)
1723 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &url_str
, &url_len
)) {
1724 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1726 if (!obj
->message
) {
1727 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1730 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1732 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUrl to an empty string");
1735 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(url_str
, url_len
));
1737 RETVAL_ZVAL(getThis(), 1, 0);
1741 PHP_METHOD(HttpMessage
, getParentMessage
)
1743 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1744 if (SUCCESS
== zend_parse_parameters_none()) {
1745 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1747 if (!obj
->message
) {
1748 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1751 if (obj
->message
->parent
) {
1752 RETVAL_OBJVAL(obj
->parent
, 1);
1754 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "HttpMessage does not have a parent message");
1757 } end_error_handling();
1760 PHP_METHOD(HttpMessage
, toString
)
1762 zend_bool include_parent
= 0;
1764 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1765 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1769 if (!obj
->message
) {
1770 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1773 if (include_parent
) {
1774 php_http_message_serialize(obj
->message
, &string
, &length
);
1776 php_http_message_to_string(obj
->message
, &string
, &length
);
1779 RETURN_STRINGL(string
, length
, 0);
1782 RETURN_EMPTY_STRING();
1785 PHP_METHOD(HttpMessage
, serialize
)
1787 if (SUCCESS
== zend_parse_parameters_none()) {
1788 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1792 if (!obj
->message
) {
1793 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1796 php_http_message_serialize(obj
->message
, &string
, &length
);
1797 RETURN_STRINGL(string
, length
, 0);
1799 RETURN_EMPTY_STRING();
1802 PHP_METHOD(HttpMessage
, unserialize
)
1807 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &serialized
, &length
)) {
1808 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1809 php_http_message_t
*msg
;
1812 php_http_message_dtor(obj
->message
);
1813 efree(obj
->message
);
1815 if ((msg
= php_http_message_parse(NULL
, serialized
, (size_t) length TSRMLS_CC
))) {
1818 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1819 php_http_error(HE_ERROR
, PHP_HTTP_E_RUNTIME
, "Could not unserialize HttpMessage");
1824 PHP_METHOD(HttpMessage
, detach
)
1826 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1827 if (SUCCESS
== zend_parse_parameters_none()) {
1828 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1830 if (!obj
->message
) {
1831 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1834 RETVAL_OBJVAL(php_http_message_object_new_ex(obj
->zo
.ce
, php_http_message_copy(obj
->message
, NULL
), NULL TSRMLS_CC
), 0);
1836 } end_error_handling();
1839 PHP_METHOD(HttpMessage
, prepend
)
1844 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O|b", &prepend
, php_http_message_class_entry
, &top
)) {
1845 php_http_message_t
*msg
[2];
1846 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1847 php_http_message_object_t
*prepend_obj
= zend_object_store_get_object(prepend TSRMLS_CC
);
1849 if (!obj
->message
) {
1850 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1852 if (!prepend_obj
->message
) {
1853 prepend_obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1857 for (msg
[0] = obj
->message
; msg
[0]; msg
[0] = msg
[0]->parent
) {
1858 for (msg
[1] = prepend_obj
->message
; msg
[1]; msg
[1] = msg
[1]->parent
) {
1859 if (msg
[0] == msg
[1]) {
1860 php_http_error(HE_THROW
, PHP_HTTP_E_INVALID_PARAM
, "Cannot prepend a message located within the same message chain");
1866 php_http_message_object_prepend(getThis(), prepend
, top TSRMLS_CC
);
1870 PHP_METHOD(HttpMessage
, reverse
)
1872 if (SUCCESS
== zend_parse_parameters_none()) {
1873 php_http_message_object_reverse(getThis(), return_value TSRMLS_CC
);
1877 PHP_METHOD(HttpMessage
, count
)
1879 if (SUCCESS
== zend_parse_parameters_none()) {
1881 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1883 if (!obj
->message
) {
1884 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1887 php_http_message_count(i
, obj
->message
);
1893 PHP_METHOD(HttpMessage
, rewind
)
1895 if (SUCCESS
== zend_parse_parameters_none()) {
1896 zval
*zobj
= getThis();
1897 php_http_message_object_t
*obj
= zend_object_store_get_object(zobj TSRMLS_CC
);
1899 if (obj
->iterator
) {
1900 zval_ptr_dtor(&obj
->iterator
);
1903 obj
->iterator
= zobj
;
1907 PHP_METHOD(HttpMessage
, valid
)
1909 if (SUCCESS
== zend_parse_parameters_none()) {
1910 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1912 RETURN_BOOL(obj
->iterator
!= NULL
);
1916 PHP_METHOD(HttpMessage
, next
)
1918 if (SUCCESS
== zend_parse_parameters_none()) {
1919 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1921 if (obj
->iterator
) {
1922 php_http_message_object_t
*itr
= zend_object_store_get_object(obj
->iterator TSRMLS_CC
);
1924 if (itr
&& itr
->parent
.handle
) {
1925 zval
*old
= obj
->iterator
;
1926 MAKE_STD_ZVAL(obj
->iterator
);
1927 ZVAL_OBJVAL(obj
->iterator
, itr
->parent
, 1);
1928 zval_ptr_dtor(&old
);
1930 zval_ptr_dtor(&obj
->iterator
);
1931 obj
->iterator
= NULL
;
1937 PHP_METHOD(HttpMessage
, key
)
1939 if (SUCCESS
== zend_parse_parameters_none()) {
1940 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1942 RETURN_LONG(obj
->iterator
? obj
->iterator
->value
.obj
.handle
:0);
1946 PHP_METHOD(HttpMessage
, current
)
1948 if (SUCCESS
== zend_parse_parameters_none()) {
1949 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1951 if (obj
->iterator
) {
1952 RETURN_ZVAL(obj
->iterator
, 1, 0);
1962 * vim600: noet sw=4 ts=4 fdm=marker
1963 * vim<600: noet sw=4 ts=4