2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2010, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
13 /* $Id: http_message_api.c 298689 2010-04-28 06:50:06Z mike $ */
17 #include <main/SAPI.h>
18 #include <ext/spl/spl_iterators.h>
19 #include <Zend/zend_interfaces.h>
21 PHP_HTTP_API zend_bool
php_http_message_info_callback(php_http_message_t
**message
, HashTable
**headers
, php_http_info_t
*info TSRMLS_DC
)
23 php_http_message_t
*old
= *message
;
26 if (old
->type
|| zend_hash_num_elements(&old
->hdrs
) || PHP_HTTP_BUFFER_LEN(old
)) {
27 (*message
) = php_http_message_init(NULL
, 0 TSRMLS_CC
);
28 (*message
)->parent
= old
;
29 (*headers
) = &((*message
)->hdrs
);
32 php_http_message_set_info(*message
, info
);
34 return old
!= *message
;
37 PHP_HTTP_API php_http_message_t
*php_http_message_init(php_http_message_t
*message
, php_http_message_type_t type TSRMLS_DC
)
40 message
= emalloc(sizeof(*message
));
42 memset(message
, 0, sizeof(*message
));
43 TSRMLS_SET_CTX(message
->ts
);
45 php_http_message_set_type(message
, type
);
46 zend_hash_init(&message
->hdrs
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
47 php_http_message_body_init(&message
->body
, NULL TSRMLS_CC
);
52 PHP_HTTP_API php_http_message_t
*php_http_message_init_env(php_http_message_t
*message
, php_http_message_type_t type TSRMLS_DC
)
54 int free_msg
= !message
;
56 php_http_message_body_t
*mbody
;
58 message
= php_http_message_init(message
, type TSRMLS_CC
);
61 case PHP_HTTP_REQUEST
:
62 if ((sval
= php_http_env_get_server_var(ZEND_STRL("SERVER_PROTOCOL"), 1 TSRMLS_CC
)) && !strncmp(Z_STRVAL_P(sval
), "HTTP/", lenof("HTTP/"))) {
63 php_http_version_parse(&message
->http
.version
, Z_STRVAL_P(sval
) TSRMLS_CC
);
65 message
->http
.version
.major
= 1;
66 message
->http
.version
.minor
= 1;
68 if ((sval
= php_http_env_get_server_var(ZEND_STRL("REQUEST_METHOD"), 1 TSRMLS_CC
))) {
69 message
->http
.info
.request
.method
= estrdup(Z_STRVAL_P(sval
));
71 if ((sval
= php_http_env_get_server_var(ZEND_STRL("REQUEST_URI"), 1 TSRMLS_CC
))) {
72 message
->http
.info
.request
.url
= estrdup(Z_STRVAL_P(sval
));
75 php_http_env_get_request_headers(&message
->hdrs TSRMLS_CC
);
77 if ((mbody
= php_http_env_get_request_body(TSRMLS_C
))) {
78 php_http_message_body_dtor(&message
->body
);
79 php_http_message_body_copy(mbody
, &message
->body
, 0);
83 case PHP_HTTP_RESPONSE
:
84 if (!SG(sapi_headers
).http_status_line
|| !php_http_info_parse((php_http_info_t
*) &message
->http
, SG(sapi_headers
).http_status_line TSRMLS_CC
)) {
85 message
->http
.version
.major
= 1;
86 message
->http
.version
.minor
= 1;
87 switch ((message
->http
.info
.response
.code
= SG(sapi_headers
).http_response_code
)) {
89 message
->http
.info
.response
.code
= 200;
91 message
->http
.info
.response
.status
= estrdup("Ok");
94 message
->http
.info
.response
.status
= estrdup("");
100 php_http_env_get_response_headers(&message
->hdrs TSRMLS_CC
);
102 if (php_output_get_level(TSRMLS_C
)) {
103 if (php_output_get_status(TSRMLS_C
) & PHP_OUTPUT_SENT
) {
104 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "Could not fetch response body, output has already been sent at %s:%d", php_output_get_start_filename(TSRMLS_C
), php_output_get_start_lineno(TSRMLS_C
));
106 } else if (SUCCESS
!= php_output_get_contents(&tval TSRMLS_CC
)) {
107 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "Could not fetch response body");
110 php_http_message_body_append(&message
->body
, Z_STRVAL(tval
), Z_STRLEN(tval
));
119 php_http_message_free(&message
);
129 PHP_HTTP_API php_http_message_t
*php_http_message_parse(php_http_message_t
*msg
, const char *str
, size_t len TSRMLS_DC
)
131 php_http_message_parser_t p
;
132 php_http_buffer_t buf
;
135 msg
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
137 php_http_buffer_from_string_ex(&buf
, str
, len
);
138 php_http_message_parser_init(&p TSRMLS_CC
);
139 php_http_message_parser_parse(&p
, &buf
, PHP_HTTP_MESSAGE_PARSER_CLEANUP
, &msg
);
140 php_http_message_parser_dtor(&p
);
141 php_http_buffer_dtor(&buf
);
147 PHP_HTTP_API zval
*php_http_message_header(php_http_message_t
*msg
, char *key_str
, size_t key_len
, int join
)
149 zval
*ret
= NULL
, **header
;
150 char *key
= php_http_pretty_key(estrndup(key_str
, key_len
), key_len
, 1, 1);
152 if (SUCCESS
== zend_hash_find(&msg
->hdrs
, key
, key_len
+ 1, (void *) &header
)) {
153 if (join
&& Z_TYPE_PP(header
) == IS_ARRAY
) {
154 zval
*header_str
, **val
;
156 php_http_buffer_t str
;
157 TSRMLS_FETCH_FROM_CTX(msg
->ts
);
159 php_http_buffer_init(&str
);
160 MAKE_STD_ZVAL(header_str
);
161 FOREACH_VAL(pos
, *header
, val
) {
162 zval
*strval
= php_http_ztyp(IS_STRING
, *val
);
163 php_http_buffer_appendf(&str
, PHP_HTTP_BUFFER_LEN(&str
) ? ", %s":"%s", Z_STRVAL_P(strval
));
164 zval_ptr_dtor(&strval
);
166 php_http_buffer_fix(&str
);
167 ZVAL_STRINGL(header_str
, PHP_HTTP_BUFFER_VAL(&str
), PHP_HTTP_BUFFER_LEN(&str
), 0);
170 ret
= php_http_ztyp(IS_STRING
, *header
);
181 PHP_HTTP_API
void php_http_message_set_type(php_http_message_t
*message
, php_http_message_type_t type
)
183 /* just act if different */
184 if (type
!= message
->type
) {
186 /* free request info */
187 switch (message
->type
= type
) {
188 case PHP_HTTP_REQUEST
:
189 STR_FREE(message
->http
.info
.request
.method
);
190 STR_FREE(message
->http
.info
.request
.url
);
193 case PHP_HTTP_RESPONSE
:
194 STR_FREE(message
->http
.info
.response
.status
);
201 memset(&message
->http
, 0, sizeof(message
->http
));
205 PHP_HTTP_API
void php_http_message_set_info(php_http_message_t
*message
, php_http_info_t
*info
)
207 php_http_message_set_type(message
, info
->type
);
208 message
->http
.version
= info
->http
.version
;
209 switch (message
->type
) {
210 case PHP_HTTP_REQUEST
:
211 STR_SET(PHP_HTTP_INFO(message
).request
.url
, PHP_HTTP_INFO(info
).request
.url
? estrdup(PHP_HTTP_INFO(info
).request
.url
) : NULL
);
212 STR_SET(PHP_HTTP_INFO(message
).request
.method
, PHP_HTTP_INFO(info
).request
.method
? estrdup(PHP_HTTP_INFO(info
).request
.method
) : NULL
);
215 case PHP_HTTP_RESPONSE
:
216 PHP_HTTP_INFO(message
).response
.code
= PHP_HTTP_INFO(info
).response
.code
;
217 STR_SET(PHP_HTTP_INFO(message
).response
.status
, PHP_HTTP_INFO(info
).response
.status
? estrdup(PHP_HTTP_INFO(info
).response
.status
) : NULL
);
225 static inline void message_headers(php_http_message_t
*msg
, php_http_buffer_t
*str
)
227 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
231 TSRMLS_FETCH_FROM_CTX(msg
->ts
);
234 case PHP_HTTP_REQUEST
:
235 php_http_buffer_appendf(str
, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&msg
->http
, PHP_HTTP_CRLF
));
238 case PHP_HTTP_RESPONSE
:
239 php_http_buffer_appendf(str
, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&msg
->http
, PHP_HTTP_CRLF
));
246 if ((size
= php_http_message_body_size(&msg
->body
))) {
249 zend_hash_update(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"), &h
, sizeof(zval
*), NULL
);
251 if (msg
->body
.boundary
) {
255 if (!(h
= php_http_message_header(msg
, ZEND_STRL("Content-Type"), 1))) {
256 len
= spprintf(&str
, 0, "multipart/form-data; boundary=\"%s\"", msg
->body
.boundary
);
258 ZVAL_STRINGL(h
, str
, len
, 0);
259 zend_hash_update(&msg
->hdrs
, "Content-Type", sizeof("Content-Type"), &h
, sizeof(zval
*), NULL
);
260 } else if (!php_http_match(Z_STRVAL_P(h
), "boundary=", PHP_HTTP_MATCH_WORD
)) {
262 Z_STRLEN_P(h
) = spprintf(&Z_STRVAL_P(h
), 0, "%s; boundary=\"%s\"", Z_STRVAL_P(h
), msg
->body
.boundary
);
263 zend_hash_update(&msg
->hdrs
, "Content-Type", sizeof("Content-Type"), &h
, sizeof(zval
*), NULL
);
270 FOREACH_HASH_KEYVAL(pos1
, &msg
->hdrs
, key
, header
) {
271 if (key
.type
== HASH_KEY_IS_STRING
) {
273 zval
**single_header
;
275 switch (Z_TYPE_PP(header
)) {
277 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_BVAL_PP(header
)?"true":"false");
281 php_http_buffer_appendf(str
, "%s: %ld" PHP_HTTP_CRLF
, key
.str
, Z_LVAL_PP(header
));
285 php_http_buffer_appendf(str
, "%s: %F" PHP_HTTP_CRLF
, key
.str
, Z_DVAL_PP(header
));
289 if (Z_STRVAL_PP(header
)[Z_STRLEN_PP(header
)-1] == '\r') fprintf(stderr
, "DOH!\n");
290 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_STRVAL_PP(header
));
294 FOREACH_VAL(pos2
, *header
, single_header
) {
295 switch (Z_TYPE_PP(single_header
)) {
297 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_BVAL_PP(single_header
)?"true":"false");
301 php_http_buffer_appendf(str
, "%s: %ld" PHP_HTTP_CRLF
, key
.str
, Z_LVAL_PP(single_header
));
305 php_http_buffer_appendf(str
, "%s: %F" PHP_HTTP_CRLF
, key
.str
, Z_DVAL_PP(single_header
));
309 php_http_buffer_appendf(str
, "%s: %s" PHP_HTTP_CRLF
, key
.str
, Z_STRVAL_PP(single_header
));
319 PHP_HTTP_API
void php_http_message_to_callback(php_http_message_t
*msg
, php_http_pass_callback_t cb
, void *cb_arg
)
321 php_http_buffer_t str
;
323 php_http_buffer_init_ex(&str
, 0x1000, 0);
324 message_headers(msg
, &str
);
325 cb(cb_arg
, PHP_HTTP_BUFFER_VAL(&str
), PHP_HTTP_BUFFER_LEN(&str
));
326 php_http_buffer_dtor(&str
);
328 if (php_http_message_body_size(&msg
->body
)) {
329 cb(cb_arg
, ZEND_STRL(PHP_HTTP_CRLF
));
330 php_http_message_body_to_callback(&msg
->body
, cb
, cb_arg
, 0, 0);
331 cb(cb_arg
, ZEND_STRL(PHP_HTTP_CRLF
));
335 PHP_HTTP_API
void php_http_message_to_string(php_http_message_t
*msg
, char **string
, size_t *length
)
337 php_http_buffer_t str
;
340 php_http_buffer_init_ex(&str
, 0x1000, 0);
341 message_headers(msg
, &str
);
342 if (php_http_message_body_size(&msg
->body
)) {
343 php_http_buffer_appends(&str
, PHP_HTTP_CRLF
);
344 php_http_message_body_to_callback(&msg
->body
, (php_http_pass_callback_t
) php_http_buffer_append
, &str
, 0, 0);
345 php_http_buffer_appends(&str
, PHP_HTTP_CRLF
);
348 data
= php_http_buffer_data(&str
, string
, length
);
353 php_http_buffer_dtor(&str
);
356 PHP_HTTP_API
void php_http_message_serialize(php_http_message_t
*message
, char **string
, size_t *length
)
360 php_http_buffer_t str
;
362 php_http_buffer_init(&str
);
365 php_http_message_to_string(message
, &buf
, &len
);
366 php_http_buffer_prepend(&str
, buf
, len
);
368 } while ((message
= message
->parent
));
370 buf
= php_http_buffer_data(&str
, string
, length
);
375 php_http_buffer_dtor(&str
);
378 PHP_HTTP_API php_http_message_t
*php_http_message_reverse(php_http_message_t
*msg
)
382 php_http_message_count(c
, msg
);
385 php_http_message_t
*tmp
= msg
, **arr
= ecalloc(c
, sizeof(**arr
));
387 for (i
= 0; i
< c
; ++i
) {
391 arr
[0]->parent
= NULL
;
392 for (i
= 0; i
< c
-1; ++i
) {
393 arr
[i
+1]->parent
= arr
[i
];
403 PHP_HTTP_API php_http_message_t
*php_http_message_interconnect(php_http_message_t
*m1
, php_http_message_t
*m2
)
406 int i
= 0, c1
= 0, c2
= 0;
407 php_http_message_t
*t1
= m1
, *t2
= m2
, *p1
, *p2
;
409 php_http_message_count(c1
, m1
);
410 php_http_message_count(c2
, m2
);
412 while (i
++ < (c1
- c2
)) {
423 } else if (!m1
&& m2
) {
429 PHP_HTTP_API
void php_http_message_to_struct(php_http_message_t
*msg
, zval
*obj
)
434 TSRMLS_FETCH_FROM_CTX(msg
->ts
);
436 INIT_PZVAL_ARRAY(&strct
, HASH_OF(obj
));
438 add_assoc_long(&strct
, "type", msg
->type
);
439 spprintf(&version
, 0 ,"%u.%u", msg
->http
.version
.major
, msg
->http
.version
.minor
);
440 add_assoc_string_ex(&strct
, ZEND_STRL("httpVersion"), version
, 0);
443 case PHP_HTTP_RESPONSE
:
444 add_assoc_long(&strct
, "responseCode", msg
->http
.info
.response
.code
);
445 add_assoc_string(&strct
, "responseStatus", STR_PTR(msg
->http
.info
.response
.status
), 1);
448 case PHP_HTTP_REQUEST
:
449 add_assoc_string(&strct
, "requestMethod", STR_PTR(msg
->http
.info
.request
.method
), 1);
450 add_assoc_string(&strct
, "requestUrl", STR_PTR(msg
->http
.info
.request
.url
), 1);
454 /* avoid compiler warning */
458 MAKE_STD_ZVAL(headers
);
460 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
461 add_assoc_zval(&strct
, "headers", headers
);
463 add_assoc_stringl(&strct
, "body", PHP_HTTP_BUFFER_VAL(msg
), PHP_HTTP_BUFFER_LEN(msg
), 1);
468 MAKE_STD_ZVAL(parent
);
469 if (Z_TYPE_P(obj
) == IS_ARRAY
) {
474 add_assoc_zval(&strct
, "parentMessage", parent
);
475 php_http_message_to_struct(msg
->parent
, parent
);
477 add_assoc_null(&strct
, "parentMessage");
481 PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC)
485 switch (message->type) {
486 case PHP_HTTP_RESPONSE:
488 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
492 FOREACH_HASH_KEYVAL(pos, &message->hdrs, key, val) {
493 if (key.type == HASH_KEY_IS_STRING) {
494 http_send_header_zval_ex(key.str, key.len-1, val, 1);
497 rs = SUCCESS == http_send_status(message->http.info.response.code) &&
498 SUCCESS == http_send_data(PHP_HTTP_BUFFER_VAL(message), PHP_HTTP_BUFFER_LEN(message)) ?
503 case PHP_HTTP_REQUEST:
505 #ifdef PHP_HTTP_HAVE_CURL
507 http_request request;
508 zval **zhost, *options, *headers;
510 MAKE_STD_ZVAL(options);
511 MAKE_STD_ZVAL(headers);
514 zend_hash_copy(Z_ARRVAL_P(headers), &message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
515 add_assoc_zval(options, "headers", headers);
517 if (SUCCESS == zend_hash_find(&message->hdrs, "Host", sizeof("Host"), (void *) &zhost) && Z_TYPE_PP(zhost) == IS_STRING) {
519 php_url parts, *url = php_url_parse(message->http.info.request.url);
521 memset(&parts, 0, sizeof(php_url));
523 if ((colon = strchr(Z_STRVAL_PP(zhost), ':'))) {
524 parts.port = atoi(colon + 1);
525 parts.host = estrndup(Z_STRVAL_PP(zhost), (Z_STRVAL_PP(zhost) - colon - 1));
527 parts.host = estrndup(Z_STRVAL_PP(zhost), Z_STRLEN_PP(zhost));
530 http_build_url(PHP_HTTP_URL_REPLACE, url, &parts, NULL, &uri, NULL);
534 uri = http_absolute_url(message->http.info.request.url);
537 if ((request.meth = http_request_method_exists(1, 0, message->http.info.request.method))) {
538 http_request_body body;
540 http_request_init_ex(&request, NULL, request.meth, uri);
541 request.body = http_request_body_init_ex(&body, PHP_HTTP_REQUEST_BODY_CSTRING, PHP_HTTP_BUFFER_VAL(message), PHP_HTTP_BUFFER_LEN(message), 0);
542 if (SUCCESS == (rs = http_request_prepare(&request, Z_ARRVAL_P(options)))) {
543 http_request_exec(&request);
545 http_request_dtor(&request);
547 http_error_ex(HE_WARNING, PHP_HTTP_E_REQUEST_METHOD,
548 "Cannot send HttpMessage. Request method %s not supported",
549 message->http.info.request.method);
552 zval_ptr_dtor(&options);
554 http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "HTTP requests not supported - ext/http was not linked against libcurl.");
561 php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type PHP_HTTP_REQUEST nor PHP_HTTP_RESPONSE");
569 PHP_HTTP_API php_http_message_t
*php_http_message_copy(php_http_message_t
*from
, php_http_message_t
*to
)
571 php_http_message_t
*temp
, *copy
= NULL
;
572 php_http_info_t info
;
573 TSRMLS_FETCH_FROM_CTX(from
->ts
);
576 info
.type
= from
->type
;
577 info
.http
= from
->http
;
579 copy
= temp
= php_http_message_init(to
, 0 TSRMLS_CC
);
580 php_http_message_set_info(temp
, &info
);
581 zend_hash_copy(&temp
->hdrs
, &from
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
582 php_http_message_body_copy(&from
->body
, &temp
->body
, 1);
584 while (from
->parent
) {
585 info
.type
= from
->parent
->type
;
586 info
.http
= from
->parent
->http
;
588 temp
->parent
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
589 php_http_message_set_info(temp
->parent
, &info
);
590 zend_hash_copy(&temp
->parent
->hdrs
, &from
->parent
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
591 php_http_message_body_copy(&from
->body
, &temp
->body
, 1);
601 PHP_HTTP_API
void php_http_message_dtor(php_http_message_t
*message
)
604 zend_hash_destroy(&message
->hdrs
);
605 php_http_message_body_dtor(&message
->body
);
607 switch (message
->type
) {
608 case PHP_HTTP_REQUEST
:
609 STR_SET(message
->http
.info
.request
.method
, NULL
);
610 STR_SET(message
->http
.info
.request
.url
, NULL
);
613 case PHP_HTTP_RESPONSE
:
614 STR_SET(message
->http
.info
.response
.status
, NULL
);
623 PHP_HTTP_API
void php_http_message_free(php_http_message_t
**message
)
626 if ((*message
)->parent
) {
627 php_http_message_free(&(*message
)->parent
);
629 php_http_message_dtor(*message
);
635 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpMessage, method, 0, req_args)
636 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpMessage, method, 0)
637 #define PHP_HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, PHP_HTTP_ARGS(HttpMessage, method), visibility)
639 PHP_HTTP_BEGIN_ARGS(__construct
, 0)
640 PHP_HTTP_ARG_VAL(message
, 0)
643 PHP_HTTP_EMPTY_ARGS(getBody
);
644 PHP_HTTP_BEGIN_ARGS(setBody
, 1)
645 PHP_HTTP_ARG_VAL(body
, 0)
648 PHP_HTTP_BEGIN_ARGS(getHeader
, 1)
649 PHP_HTTP_ARG_VAL(header
, 0)
652 PHP_HTTP_BEGIN_ARGS(setHeader
, 1)
653 PHP_HTTP_ARG_VAL(header
, 0)
654 PHP_HTTP_ARG_VAL(value
, 0)
657 PHP_HTTP_BEGIN_ARGS(addHeader
, 2)
658 PHP_HTTP_ARG_VAL(header
, 0)
659 PHP_HTTP_ARG_VAL(value
, 0)
662 PHP_HTTP_EMPTY_ARGS(getHeaders
);
663 PHP_HTTP_BEGIN_ARGS(setHeaders
, 1)
664 PHP_HTTP_ARG_VAL(headers
, 0)
667 PHP_HTTP_BEGIN_ARGS(addHeaders
, 1)
668 PHP_HTTP_ARG_VAL(headers
, 0)
669 PHP_HTTP_ARG_VAL(append
, 0)
672 PHP_HTTP_EMPTY_ARGS(getType
);
673 PHP_HTTP_BEGIN_ARGS(setType
, 1)
674 PHP_HTTP_ARG_VAL(type
, 0)
677 PHP_HTTP_EMPTY_ARGS(getInfo
);
678 PHP_HTTP_BEGIN_ARGS(setInfo
, 1)
679 PHP_HTTP_ARG_VAL(http_info
, 0)
682 PHP_HTTP_EMPTY_ARGS(getResponseCode
);
683 PHP_HTTP_BEGIN_ARGS(setResponseCode
, 1)
684 PHP_HTTP_ARG_VAL(response_code
, 0)
687 PHP_HTTP_EMPTY_ARGS(getResponseStatus
);
688 PHP_HTTP_BEGIN_ARGS(setResponseStatus
, 1)
689 PHP_HTTP_ARG_VAL(response_status
, 0)
692 PHP_HTTP_EMPTY_ARGS(getRequestMethod
);
693 PHP_HTTP_BEGIN_ARGS(setRequestMethod
, 1)
694 PHP_HTTP_ARG_VAL(request_method
, 0)
697 PHP_HTTP_EMPTY_ARGS(getRequestUrl
);
698 PHP_HTTP_BEGIN_ARGS(setRequestUrl
, 1)
699 PHP_HTTP_ARG_VAL(url
, 0)
702 PHP_HTTP_EMPTY_ARGS(getHttpVersion
);
703 PHP_HTTP_BEGIN_ARGS(setHttpVersion
, 1)
704 PHP_HTTP_ARG_VAL(http_version
, 0)
707 PHP_HTTP_BEGIN_ARGS(guessContentType
, 1)
708 PHP_HTTP_ARG_VAL(magic_file
, 0)
709 PHP_HTTP_ARG_VAL(magic_mode
, 0)
712 PHP_HTTP_EMPTY_ARGS(getParentMessage
);
713 PHP_HTTP_EMPTY_ARGS(send
);
714 PHP_HTTP_EMPTY_ARGS(__toString
);
715 PHP_HTTP_BEGIN_ARGS(toString
, 0)
716 PHP_HTTP_ARG_VAL(include_parent
, 0)
719 PHP_HTTP_EMPTY_ARGS(toMessageTypeObject
);
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
);
809 static int php_http_message_object_has_prophandler(const char *prop_str
, size_t prop_len
) {
810 return zend_hash_exists(&php_http_message_object_prophandlers
, prop_str
, prop_len
+ 1);
812 static STATUS
php_http_message_object_get_prophandler(const char *prop_str
, size_t prop_len
, php_http_message_object_prophandler_t
**handler
) {
813 return zend_hash_find(&php_http_message_object_prophandlers
, prop_str
, prop_len
+ 1, (void *) handler
);
815 static void php_http_message_object_prophandler_get_type(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
816 RETVAL_LONG(obj
->message
->type
);
818 static void php_http_message_object_prophandler_set_type(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
819 zval
*cpy
= php_http_ztyp(IS_LONG
, value
);
820 php_http_message_set_type(obj
->message
, Z_LVAL_P(cpy
));
823 static void php_http_message_object_prophandler_get_request_method(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
824 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
) && obj
->message
->http
.info
.request
.method
) {
825 RETVAL_STRING(obj
->message
->http
.info
.request
.method
, 1);
830 static void php_http_message_object_prophandler_set_request_method(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
831 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
832 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
833 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
837 static void php_http_message_object_prophandler_get_request_url(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
838 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
) && obj
->message
->http
.info
.request
.url
) {
839 RETVAL_STRING(obj
->message
->http
.info
.request
.url
, 1);
844 static void php_http_message_object_prophandler_set_request_url(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
845 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
846 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
847 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
851 static void php_http_message_object_prophandler_get_response_status(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
852 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
) && obj
->message
->http
.info
.response
.status
) {
853 RETVAL_STRING(obj
->message
->http
.info
.response
.status
, 1);
858 static void php_http_message_object_prophandler_set_response_status(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
859 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
860 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
861 STR_SET(obj
->message
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
865 static void php_http_message_object_prophandler_get_response_code(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
866 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
867 RETVAL_LONG(obj
->message
->http
.info
.response
.code
);
872 static void php_http_message_object_prophandler_set_response_code(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
873 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
874 zval
*cpy
= php_http_ztyp(IS_LONG
, value
);
875 obj
->message
->http
.info
.response
.code
= Z_LVAL_P(cpy
);
879 static void php_http_message_object_prophandler_get_http_version(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
883 php_http_version_to_string(&obj
->message
->http
.version
, &version_str
, &version_len
, NULL
, NULL TSRMLS_CC
);
884 RETVAL_STRINGL(version_str
, version_len
, 0);
886 static void php_http_message_object_prophandler_set_http_version(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
887 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
888 php_http_version_parse(&obj
->message
->http
.version
, Z_STRVAL_P(cpy
) TSRMLS_CC
);
891 static void php_http_message_object_prophandler_get_headers(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
892 array_init(return_value
);
893 zend_hash_copy(Z_ARRVAL_P(return_value
), &obj
->message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
895 static void php_http_message_object_prophandler_set_headers(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
896 zval
*cpy
= php_http_ztyp(IS_ARRAY
, value
);
898 zend_hash_clean(&obj
->message
->hdrs
);
899 zend_hash_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(cpy
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
902 static void php_http_message_object_prophandler_get_body(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
903 if (obj
->body
.handle
) {
904 RETVAL_OBJVAL(obj
->body
, 1);
909 static void php_http_message_object_prophandler_set_body(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
910 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), php_http_message_body_class_entry TSRMLS_CC
)) {
911 if (obj
->body
.handle
) {
912 zend_objects_store_del_ref_by_handle(obj
->body
.handle TSRMLS_CC
);
914 Z_OBJ_ADDREF_P(value
);
915 obj
->body
= Z_OBJVAL_P(value
);
918 static void php_http_message_object_prophandler_get_parent_message(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
919 if (obj
->message
->parent
) {
920 RETVAL_OBJVAL(obj
->parent
, 1);
925 static void php_http_message_object_prophandler_set_parent_message(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
926 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), php_http_message_class_entry TSRMLS_CC
)) {
927 if (obj
->message
->parent
) {
928 zend_objects_store_del_ref_by_handle(obj
->parent
.handle TSRMLS_CC
);
930 Z_OBJ_ADDREF_P(value
);
931 obj
->parent
= Z_OBJVAL_P(value
);
935 PHP_MINIT_FUNCTION(http_message
)
937 PHP_HTTP_REGISTER_CLASS(http
, Message
, http_message
, php_http_object_class_entry
, 0);
938 php_http_message_class_entry
->create_object
= php_http_message_object_new
;
939 memcpy(&php_http_message_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
940 php_http_message_object_handlers
.clone_obj
= php_http_message_object_clone
;
941 php_http_message_object_handlers
.read_property
= php_http_message_object_read_prop
;
942 php_http_message_object_handlers
.write_property
= php_http_message_object_write_prop
;
943 php_http_message_object_handlers
.get_properties
= php_http_message_object_get_props
;
944 php_http_message_object_handlers
.get_property_ptr_ptr
= php_http_message_object_get_prop_ptr
;
946 zend_class_implements(php_http_message_class_entry TSRMLS_CC
, 3, spl_ce_Countable
, zend_ce_serializable
, zend_ce_iterator
, php_http_fluently_callable_class_entry
);
948 zend_hash_init(&php_http_message_object_prophandlers
, 9, NULL
, NULL
, 1);
949 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("type"), PHP_HTTP_NONE
, ZEND_ACC_PROTECTED TSRMLS_CC
);
950 php_http_message_object_add_prophandler(ZEND_STRL("type"), php_http_message_object_prophandler_get_type
, php_http_message_object_prophandler_set_type
);
951 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("body"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
952 php_http_message_object_add_prophandler(ZEND_STRL("body"), php_http_message_object_prophandler_get_body
, php_http_message_object_prophandler_set_body
);
953 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestMethod"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
954 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
);
955 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestUrl"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
956 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
);
957 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("responseStatus"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
958 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
);
959 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("responseCode"), 0, ZEND_ACC_PROTECTED TSRMLS_CC
);
960 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
);
961 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("httpVersion"), ZEND_ACC_PROTECTED TSRMLS_CC
);
962 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
);
963 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("headers"), ZEND_ACC_PROTECTED TSRMLS_CC
);
964 php_http_message_object_add_prophandler(ZEND_STRL("headers"), php_http_message_object_prophandler_get_headers
, php_http_message_object_prophandler_set_headers
);
965 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("parentMessage"), ZEND_ACC_PROTECTED TSRMLS_CC
);
966 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
);
968 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_NONE"), PHP_HTTP_NONE TSRMLS_CC
);
969 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_REQUEST"), PHP_HTTP_REQUEST TSRMLS_CC
);
970 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_RESPONSE"), PHP_HTTP_RESPONSE TSRMLS_CC
);
975 PHP_MSHUTDOWN_FUNCTION(http_message
)
977 zend_hash_destroy(&php_http_message_object_prophandlers
);
982 void php_http_message_object_reverse(zval
*this_ptr
, zval
*return_value TSRMLS_DC
)
985 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
988 php_http_message_count(i
, obj
->message
);
992 zend_object_value
*ovalues
= NULL
;
993 php_http_message_object_t
**objects
= NULL
;
996 objects
= ecalloc(i
, sizeof(**objects
));
997 ovalues
= ecalloc(i
, sizeof(*ovalues
));
999 /* we are the first message */
1001 ovalues
[0] = getThis()->value
.obj
;
1006 for (i
= 1; obj
->parent
.handle
; ++i
) {
1007 o
.value
.obj
= obj
->parent
;
1008 ovalues
[i
] = o
.value
.obj
;
1009 objects
[i
] = obj
= zend_object_store_get_object(&o TSRMLS_CC
);
1012 /* reorder parents */
1013 for (last
= --i
; i
; --i
) {
1014 objects
[i
]->message
->parent
= objects
[i
-1]->message
;
1015 objects
[i
]->parent
= ovalues
[i
-1];
1017 objects
[0]->message
->parent
= NULL
;
1018 objects
[0]->parent
.handle
= 0;
1019 objects
[0]->parent
.handlers
= NULL
;
1021 /* add ref (why?) */
1022 Z_OBJ_ADDREF_P(getThis());
1023 RETVAL_OBJVAL(ovalues
[last
], 1);
1028 RETURN_ZVAL(getThis(), 1, 0);
1032 void php_http_message_object_prepend(zval
*this_ptr
, zval
*prepend
, zend_bool top TSRMLS_DC
)
1035 php_http_message_t
*save_parent_msg
= NULL
;
1036 zend_object_value save_parent_obj
= {0, NULL
};
1037 php_http_message_object_t
*obj
= zend_object_store_get_object(this_ptr TSRMLS_CC
);
1038 php_http_message_object_t
*prepend_obj
= zend_object_store_get_object(prepend TSRMLS_CC
);
1044 save_parent_obj
= obj
->parent
;
1045 save_parent_msg
= obj
->message
->parent
;
1047 /* iterate to the most parent object */
1048 while (obj
->parent
.handle
) {
1049 m
.value
.obj
= obj
->parent
;
1050 obj
= zend_object_store_get_object(&m TSRMLS_CC
);
1055 obj
->parent
= prepend
->value
.obj
;
1056 obj
->message
->parent
= prepend_obj
->message
;
1059 zend_objects_store_add_ref(prepend TSRMLS_CC
);
1060 while (prepend_obj
->parent
.handle
) {
1061 m
.value
.obj
= prepend_obj
->parent
;
1062 zend_objects_store_add_ref(&m TSRMLS_CC
);
1063 prepend_obj
= zend_object_store_get_object(&m TSRMLS_CC
);
1067 prepend_obj
->parent
= save_parent_obj
;
1068 prepend_obj
->message
->parent
= save_parent_msg
;
1072 zend_object_value
php_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
1074 return php_http_message_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
1077 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
)
1079 zend_object_value ov
;
1080 php_http_message_object_t
*o
;
1082 o
= ecalloc(1, sizeof(php_http_message_object_t
));
1083 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
1084 object_properties_init((zend_object
*) o
, ce
);
1093 o
->parent
= php_http_message_object_new_ex(ce
, msg
->parent
, NULL TSRMLS_CC
);
1095 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
);
1098 ov
.handle
= zend_objects_store_put((zend_object
*) o
, NULL
, php_http_message_object_free
, NULL TSRMLS_CC
);
1099 ov
.handlers
= &php_http_message_object_handlers
;
1104 zend_object_value
php_http_message_object_clone(zval
*this_ptr TSRMLS_DC
)
1106 zend_object_value new_ov
;
1107 php_http_message_object_t
*new_obj
= NULL
;
1108 php_http_message_object_t
*old_obj
= zend_object_store_get_object(this_ptr TSRMLS_CC
);
1110 new_ov
= php_http_message_object_new_ex(old_obj
->zo
.ce
, php_http_message_copy(old_obj
->message
, NULL
), &new_obj TSRMLS_CC
);
1111 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
1116 void php_http_message_object_free(void *object TSRMLS_DC
)
1118 php_http_message_object_t
*o
= (php_http_message_object_t
*) object
;
1121 zval_ptr_dtor(&o
->iterator
);
1125 /* do NOT free recursivly */
1126 php_http_message_dtor(o
->message
);
1130 if (o
->parent
.handle
) {
1131 zend_objects_store_del_ref_by_handle(o
->parent
.handle TSRMLS_CC
);
1133 if (o
->body
.handle
) {
1134 zend_objects_store_del_ref_by_handle(o
->body
.handle TSRMLS_CC
);
1136 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
1141 static zval
**php_http_message_object_get_prop_ptr(zval
*object
, zval
*member
, const zend_literal
*literal_key TSRMLS_DC
)
1143 php_http_message_object_prophandler_t
*handler
;
1144 zval
*copy
= php_http_ztyp(IS_STRING
, member
);
1146 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1147 zval_ptr_dtor(©
);
1148 return &php_http_property_proxy_init(NULL
, object
, member TSRMLS_CC
)->myself
;
1150 zval_ptr_dtor(©
);
1152 return zend_get_std_object_handlers()->get_property_ptr_ptr(object
, member
, literal_key TSRMLS_CC
);
1155 static zval
*php_http_message_object_read_prop(zval
*object
, zval
*member
, int type
, const zend_literal
*literal_key TSRMLS_DC
)
1157 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1158 php_http_message_object_prophandler_t
*handler
;
1159 zval
*return_value
, *copy
= php_http_ztyp(IS_STRING
, member
);
1161 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1162 if (type
== BP_VAR_W
) {
1163 zval_ptr_dtor(©
);
1164 zend_error(E_ERROR
, "Cannot access HttpMessage properties by reference or array key/index");
1168 ALLOC_ZVAL(return_value
);
1169 Z_SET_REFCOUNT_P(return_value
, 0);
1170 Z_UNSET_ISREF_P(return_value
);
1172 handler
->read(obj
, return_value TSRMLS_CC
);
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 php_http_message_t
*msg
= obj
->message
;
1291 php_http_message_dtor(msg
);
1292 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1293 if ((obj
->message
= php_http_message_parse(msg
, message
, length TSRMLS_CC
))) {
1294 if (obj
->message
->parent
) {
1295 obj
->parent
= php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj
->message
->parent
, NULL TSRMLS_CC
);
1298 obj
->message
= php_http_message_init(msg
, 0 TSRMLS_CC
);
1300 } end_error_handling();
1302 if (!obj
->message
) {
1303 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1305 } end_error_handling();
1309 PHP_METHOD(HttpMessage
, getBody
)
1311 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1312 if (SUCCESS
== zend_parse_parameters_none()) {
1313 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1315 if (!obj
->message
) {
1316 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1319 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
)) {
1320 RETVAL_OBJVAL(obj
->body
, 1);
1323 } end_error_handling();
1326 PHP_METHOD(HttpMessage
, setBody
)
1330 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &zbody
, php_http_message_body_class_entry
)) {
1331 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1332 php_http_message_body_object_t
*body_obj
= zend_object_store_get_object(zbody TSRMLS_CC
);
1334 if (!obj
->message
) {
1335 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1338 php_http_message_body_dtor(&obj
->message
->body
);
1339 php_http_message_body_copy(body_obj
->body
, &obj
->message
->body
, 0);
1340 Z_OBJ_ADDREF_P(zbody
);
1341 obj
->body
= Z_OBJVAL_P(zbody
);
1343 RETVAL_ZVAL(getThis(), 1, 0);
1346 PHP_METHOD(HttpMessage
, getHeader
)
1351 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header_str
, &header_len
)) {
1352 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1355 if (!obj
->message
) {
1356 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1359 if ((header
= php_http_message_header(obj
->message
, header_str
, header_len
, 0))) {
1360 RETURN_ZVAL(header
, 1, 1);
1366 PHP_METHOD(HttpMessage
, getHeaders
)
1368 if (SUCCESS
== zend_parse_parameters_none()) {
1369 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1371 if (!obj
->message
) {
1372 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1375 array_init(return_value
);
1376 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(return_value
));
1380 PHP_METHOD(HttpMessage
, setHeader
)
1382 zval
*zvalue
= NULL
;
1386 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|z!", &name_str
, &name_len
, &zvalue
)) {
1387 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1388 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1390 if (!obj
->message
) {
1391 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1395 zend_hash_del(&obj
->message
->hdrs
, name
, name_len
+ 1);
1398 zend_hash_update(&obj
->message
->hdrs
, name
, name_len
+ 1, &zvalue
, sizeof(void *), NULL
);
1402 RETVAL_ZVAL(getThis(), 1, 0);
1405 PHP_METHOD(HttpMessage
, setHeaders
)
1407 zval
*new_headers
= NULL
;
1409 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &new_headers
)) {
1410 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1412 if (!obj
->message
) {
1413 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1416 zend_hash_clean(&obj
->message
->hdrs
);
1418 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, 0, ARRAY_JOIN_PRETTIFY
|ARRAY_JOIN_STRONLY
);
1421 RETVAL_ZVAL(getThis(), 1, 0);
1424 PHP_METHOD(HttpMessage
, addHeader
)
1430 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sz", &name_str
, &name_len
, &zvalue
)) {
1431 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1432 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1435 if (!obj
->message
) {
1436 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1440 if ((header
= php_http_message_header(obj
->message
, name
, name_len
, 0))) {
1441 convert_to_array(header
);
1442 zend_hash_next_index_insert(Z_ARRVAL_P(header
), &zvalue
, sizeof(void *), NULL
);
1444 zend_hash_update(&obj
->message
->hdrs
, name
, name_len
+ 1, &zvalue
, sizeof(void *), NULL
);
1448 RETVAL_ZVAL(getThis(), 1, 0);
1451 PHP_METHOD(HttpMessage
, addHeaders
)
1454 zend_bool append
= 0;
1456 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
1457 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1459 if (!obj
->message
) {
1460 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1463 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, append
, ARRAY_JOIN_STRONLY
|ARRAY_JOIN_PRETTIFY
);
1465 RETVAL_ZVAL(getThis(), 1, 0);
1468 PHP_METHOD(HttpMessage
, getType
)
1470 if (SUCCESS
== zend_parse_parameters_none()) {
1471 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1473 if (!obj
->message
) {
1474 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1477 RETURN_LONG(obj
->message
->type
);
1481 PHP_METHOD(HttpMessage
, setType
)
1485 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
1486 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1488 if (!obj
->message
) {
1489 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1492 php_http_message_set_type(obj
->message
, type
);
1494 RETVAL_ZVAL(getThis(), 1, 0);
1497 PHP_METHOD(HttpMessage
, getInfo
)
1499 if (SUCCESS
== zend_parse_parameters_none()) {
1500 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1502 if (!obj
->message
) {
1503 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1506 switch (obj
->message
->type
) {
1507 case PHP_HTTP_REQUEST
:
1508 Z_STRLEN_P(return_value
) = spprintf(&Z_STRVAL_P(return_value
), 0, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&obj
->message
->http
, ""));
1510 case PHP_HTTP_RESPONSE
:
1511 Z_STRLEN_P(return_value
) = spprintf(&Z_STRVAL_P(return_value
), 0, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&obj
->message
->http
, ""));
1517 Z_TYPE_P(return_value
) = IS_STRING
;
1523 PHP_METHOD(HttpMessage
, setInfo
)
1527 php_http_info_t inf
;
1529 if ( SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)
1530 && php_http_info_parse(&inf
, str TSRMLS_CC
)) {
1531 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1533 if (!obj
->message
) {
1534 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1537 php_http_message_set_info(obj
->message
, &inf
);
1538 php_http_info_dtor(&inf
);
1540 RETVAL_ZVAL(getThis(), 1, 0);
1543 PHP_METHOD(HttpMessage
, getHttpVersion
)
1545 if (SUCCESS
== zend_parse_parameters_none()) {
1548 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1550 if (!obj
->message
) {
1551 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1554 php_http_version_to_string(&obj
->message
->http
.version
, &str
, &len
, NULL
, NULL TSRMLS_CC
);
1555 RETURN_STRINGL(str
, len
, 0);
1561 PHP_METHOD(HttpMessage
, setHttpVersion
)
1566 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &v_str
, &v_len
)) {
1567 php_http_version_t version
;
1568 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1570 if (!obj
->message
) {
1571 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1574 if (php_http_version_parse(&version
, v_str TSRMLS_CC
)) {
1575 obj
->message
->http
.version
= version
;
1578 RETVAL_ZVAL(getThis(), 1, 0);
1581 PHP_METHOD(HttpMessage
, getResponseCode
)
1583 if (SUCCESS
== zend_parse_parameters_none()) {
1584 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1586 if (!obj
->message
) {
1587 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1590 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1591 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
1596 PHP_METHOD(HttpMessage
, setResponseCode
)
1599 zend_bool strict
= 1;
1601 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l|b", &code
, &strict
)) {
1602 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1604 if (!obj
->message
) {
1605 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1608 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1609 if (strict
&& (code
< 100 || code
> 599)) {
1610 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Invalid response code (100-599): %ld", code
);
1614 obj
->message
->http
.info
.response
.code
= code
;
1616 RETVAL_ZVAL(getThis(), 1, 0);
1619 PHP_METHOD(HttpMessage
, getResponseStatus
)
1621 if (SUCCESS
== zend_parse_parameters_none()) {
1622 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1624 if (!obj
->message
) {
1625 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1628 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1629 if (obj
->message
->http
.info
.response
.status
) {
1630 RETURN_STRING(obj
->message
->http
.info
.response
.status
, 1);
1632 RETURN_EMPTY_STRING();
1639 PHP_METHOD(HttpMessage
, setResponseStatus
)
1644 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &status
, &status_len
)) {
1645 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1647 if (!obj
->message
) {
1648 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1651 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1652 STR_SET(obj
->message
->http
.info
.response
.status
, estrndup(status
, status_len
));
1654 RETVAL_ZVAL(getThis(), 1, 0);
1657 PHP_METHOD(HttpMessage
, getRequestMethod
)
1659 if (SUCCESS
== zend_parse_parameters_none()) {
1660 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1662 if (!obj
->message
) {
1663 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1666 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1667 if (obj
->message
->http
.info
.request
.method
) {
1668 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
1670 RETURN_EMPTY_STRING();
1677 PHP_METHOD(HttpMessage
, setRequestMethod
)
1682 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1683 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1685 if (!obj
->message
) {
1686 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1689 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1690 if (method_len
< 1) {
1691 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
1695 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
1697 RETVAL_ZVAL(getThis(), 1, 0);
1700 PHP_METHOD(HttpMessage
, getRequestUrl
)
1702 if (SUCCESS
== zend_parse_parameters_none()) {
1703 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1705 if (!obj
->message
) {
1706 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1709 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1710 if (obj
->message
->http
.info
.request
.url
) {
1711 RETURN_STRING(obj
->message
->http
.info
.request
.url
, 1);
1713 RETURN_EMPTY_STRING();
1720 PHP_METHOD(HttpMessage
, setRequestUrl
)
1725 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &url_str
, &url_len
)) {
1726 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1728 if (!obj
->message
) {
1729 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1732 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1734 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUrl to an empty string");
1737 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(url_str
, url_len
));
1739 RETVAL_ZVAL(getThis(), 1, 0);
1743 PHP_METHOD(HttpMessage
, getParentMessage
)
1745 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1746 if (SUCCESS
== zend_parse_parameters_none()) {
1747 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1749 if (!obj
->message
) {
1750 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1753 if (obj
->message
->parent
) {
1754 RETVAL_OBJVAL(obj
->parent
, 1);
1756 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "HttpMessage does not have a parent message");
1759 } end_error_handling();
1762 PHP_METHOD(HttpMessage
, toString
)
1764 zend_bool include_parent
= 0;
1766 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1767 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1771 if (!obj
->message
) {
1772 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1775 if (include_parent
) {
1776 php_http_message_serialize(obj
->message
, &string
, &length
);
1778 php_http_message_to_string(obj
->message
, &string
, &length
);
1781 RETURN_STRINGL(string
, length
, 0);
1784 RETURN_EMPTY_STRING();
1787 PHP_METHOD(HttpMessage
, serialize
)
1789 if (SUCCESS
== zend_parse_parameters_none()) {
1790 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1794 if (!obj
->message
) {
1795 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1798 php_http_message_serialize(obj
->message
, &string
, &length
);
1799 RETURN_STRINGL(string
, length
, 0);
1801 RETURN_EMPTY_STRING();
1804 PHP_METHOD(HttpMessage
, unserialize
)
1809 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &serialized
, &length
)) {
1810 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1811 php_http_message_t
*msg
;
1814 php_http_message_dtor(obj
->message
);
1815 efree(obj
->message
);
1817 if ((msg
= php_http_message_parse(NULL
, serialized
, (size_t) length TSRMLS_CC
))) {
1820 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1821 php_http_error(HE_ERROR
, PHP_HTTP_E_RUNTIME
, "Could not unserialize HttpMessage");
1826 PHP_METHOD(HttpMessage
, detach
)
1828 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1829 if (SUCCESS
== zend_parse_parameters_none()) {
1830 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1832 if (!obj
->message
) {
1833 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1836 RETVAL_OBJVAL(php_http_message_object_new_ex(obj
->zo
.ce
, php_http_message_copy(obj
->message
, NULL
), NULL TSRMLS_CC
), 0);
1838 } end_error_handling();
1841 PHP_METHOD(HttpMessage
, prepend
)
1846 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O|b", &prepend
, php_http_message_class_entry
, &top
)) {
1847 php_http_message_t
*msg
[2];
1848 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1849 php_http_message_object_t
*prepend_obj
= zend_object_store_get_object(prepend TSRMLS_CC
);
1851 if (!obj
->message
) {
1852 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1854 if (!prepend_obj
->message
) {
1855 prepend_obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1859 for (msg
[0] = obj
->message
; msg
[0]; msg
[0] = msg
[0]->parent
) {
1860 for (msg
[1] = prepend_obj
->message
; msg
[1]; msg
[1] = msg
[1]->parent
) {
1861 if (msg
[0] == msg
[1]) {
1862 php_http_error(HE_THROW
, PHP_HTTP_E_INVALID_PARAM
, "Cannot prepend a message located within the same message chain");
1868 php_http_message_object_prepend(getThis(), prepend
, top TSRMLS_CC
);
1872 PHP_METHOD(HttpMessage
, reverse
)
1874 if (SUCCESS
== zend_parse_parameters_none()) {
1875 php_http_message_object_reverse(getThis(), return_value TSRMLS_CC
);
1879 PHP_METHOD(HttpMessage
, count
)
1881 if (SUCCESS
== zend_parse_parameters_none()) {
1883 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1885 if (!obj
->message
) {
1886 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1889 php_http_message_count(i
, obj
->message
);
1895 PHP_METHOD(HttpMessage
, rewind
)
1897 if (SUCCESS
== zend_parse_parameters_none()) {
1898 zval
*zobj
= getThis();
1899 php_http_message_object_t
*obj
= zend_object_store_get_object(zobj TSRMLS_CC
);
1901 if (obj
->iterator
) {
1902 zval_ptr_dtor(&obj
->iterator
);
1905 obj
->iterator
= zobj
;
1909 PHP_METHOD(HttpMessage
, valid
)
1911 if (SUCCESS
== zend_parse_parameters_none()) {
1912 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1914 RETURN_BOOL(obj
->iterator
!= NULL
);
1918 PHP_METHOD(HttpMessage
, next
)
1920 if (SUCCESS
== zend_parse_parameters_none()) {
1921 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1923 if (obj
->iterator
) {
1924 php_http_message_object_t
*itr
= zend_object_store_get_object(obj
->iterator TSRMLS_CC
);
1926 if (itr
&& itr
->parent
.handle
) {
1927 zval
*old
= obj
->iterator
;
1928 MAKE_STD_ZVAL(obj
->iterator
);
1929 ZVAL_OBJVAL(obj
->iterator
, itr
->parent
, 1);
1930 zval_ptr_dtor(&old
);
1932 zval_ptr_dtor(&obj
->iterator
);
1933 obj
->iterator
= NULL
;
1939 PHP_METHOD(HttpMessage
, key
)
1941 if (SUCCESS
== zend_parse_parameters_none()) {
1942 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1944 RETURN_LONG(obj
->iterator
? obj
->iterator
->value
.obj
.handle
:0);
1948 PHP_METHOD(HttpMessage
, current
)
1950 if (SUCCESS
== zend_parse_parameters_none()) {
1951 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1953 if (obj
->iterator
) {
1954 RETURN_ZVAL(obj
->iterator
, 1, 0);
1964 * vim600: noet sw=4 ts=4 fdm=marker
1965 * vim<600: noet sw=4 ts=4