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_ex(php_http_message_t
*from
, php_http_message_t
*to
, zend_bool parents
)
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 if (parents
) 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 php_http_message_t
*php_http_message_copy(php_http_message_t
*from
, php_http_message_t
*to
)
603 return php_http_message_copy_ex(from
, to
, 1);
606 PHP_HTTP_API
void php_http_message_dtor(php_http_message_t
*message
)
609 zend_hash_destroy(&message
->hdrs
);
610 php_http_message_body_dtor(&message
->body
);
612 switch (message
->type
) {
613 case PHP_HTTP_REQUEST
:
614 STR_SET(message
->http
.info
.request
.method
, NULL
);
615 STR_SET(message
->http
.info
.request
.url
, NULL
);
618 case PHP_HTTP_RESPONSE
:
619 STR_SET(message
->http
.info
.response
.status
, NULL
);
628 PHP_HTTP_API
void php_http_message_free(php_http_message_t
**message
)
631 if ((*message
)->parent
) {
632 php_http_message_free(&(*message
)->parent
);
634 php_http_message_dtor(*message
);
640 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpMessage, method, 0, req_args)
641 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpMessage, method, 0)
642 #define PHP_HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, PHP_HTTP_ARGS(HttpMessage, method), visibility)
644 PHP_HTTP_BEGIN_ARGS(__construct
, 0)
645 PHP_HTTP_ARG_VAL(message
, 0)
648 PHP_HTTP_EMPTY_ARGS(getBody
);
649 PHP_HTTP_BEGIN_ARGS(setBody
, 1)
650 PHP_HTTP_ARG_VAL(body
, 0)
653 PHP_HTTP_BEGIN_ARGS(getHeader
, 1)
654 PHP_HTTP_ARG_VAL(header
, 0)
657 PHP_HTTP_BEGIN_ARGS(setHeader
, 1)
658 PHP_HTTP_ARG_VAL(header
, 0)
659 PHP_HTTP_ARG_VAL(value
, 0)
662 PHP_HTTP_BEGIN_ARGS(addHeader
, 2)
663 PHP_HTTP_ARG_VAL(header
, 0)
664 PHP_HTTP_ARG_VAL(value
, 0)
667 PHP_HTTP_EMPTY_ARGS(getHeaders
);
668 PHP_HTTP_BEGIN_ARGS(setHeaders
, 1)
669 PHP_HTTP_ARG_VAL(headers
, 0)
672 PHP_HTTP_BEGIN_ARGS(addHeaders
, 1)
673 PHP_HTTP_ARG_VAL(headers
, 0)
674 PHP_HTTP_ARG_VAL(append
, 0)
677 PHP_HTTP_EMPTY_ARGS(getType
);
678 PHP_HTTP_BEGIN_ARGS(setType
, 1)
679 PHP_HTTP_ARG_VAL(type
, 0)
682 PHP_HTTP_EMPTY_ARGS(getInfo
);
683 PHP_HTTP_BEGIN_ARGS(setInfo
, 1)
684 PHP_HTTP_ARG_VAL(http_info
, 0)
687 PHP_HTTP_EMPTY_ARGS(getResponseCode
);
688 PHP_HTTP_BEGIN_ARGS(setResponseCode
, 1)
689 PHP_HTTP_ARG_VAL(response_code
, 0)
692 PHP_HTTP_EMPTY_ARGS(getResponseStatus
);
693 PHP_HTTP_BEGIN_ARGS(setResponseStatus
, 1)
694 PHP_HTTP_ARG_VAL(response_status
, 0)
697 PHP_HTTP_EMPTY_ARGS(getRequestMethod
);
698 PHP_HTTP_BEGIN_ARGS(setRequestMethod
, 1)
699 PHP_HTTP_ARG_VAL(request_method
, 0)
702 PHP_HTTP_EMPTY_ARGS(getRequestUrl
);
703 PHP_HTTP_BEGIN_ARGS(setRequestUrl
, 1)
704 PHP_HTTP_ARG_VAL(url
, 0)
707 PHP_HTTP_EMPTY_ARGS(getHttpVersion
);
708 PHP_HTTP_BEGIN_ARGS(setHttpVersion
, 1)
709 PHP_HTTP_ARG_VAL(http_version
, 0)
712 PHP_HTTP_BEGIN_ARGS(guessContentType
, 1)
713 PHP_HTTP_ARG_VAL(magic_file
, 0)
714 PHP_HTTP_ARG_VAL(magic_mode
, 0)
717 PHP_HTTP_EMPTY_ARGS(getParentMessage
);
718 PHP_HTTP_EMPTY_ARGS(send
);
719 PHP_HTTP_EMPTY_ARGS(__toString
);
720 PHP_HTTP_BEGIN_ARGS(toString
, 0)
721 PHP_HTTP_ARG_VAL(include_parent
, 0)
724 PHP_HTTP_EMPTY_ARGS(toMessageTypeObject
);
726 PHP_HTTP_EMPTY_ARGS(count
);
728 PHP_HTTP_EMPTY_ARGS(serialize
);
729 PHP_HTTP_BEGIN_ARGS(unserialize
, 1)
730 PHP_HTTP_ARG_VAL(serialized
, 0)
733 PHP_HTTP_EMPTY_ARGS(rewind
);
734 PHP_HTTP_EMPTY_ARGS(valid
);
735 PHP_HTTP_EMPTY_ARGS(key
);
736 PHP_HTTP_EMPTY_ARGS(current
);
737 PHP_HTTP_EMPTY_ARGS(next
);
739 PHP_HTTP_EMPTY_ARGS(detach
);
740 PHP_HTTP_BEGIN_ARGS(prepend
, 1)
741 PHP_HTTP_ARG_OBJ(http
\\Message
, message
, 0)
743 PHP_HTTP_EMPTY_ARGS(reverse
);
745 static zval
*php_http_message_object_read_prop(zval
*object
, zval
*member
, int type
, const zend_literal
*literal_key TSRMLS_DC
);
746 static void php_http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value
, const zend_literal
*literal_key TSRMLS_DC
);
747 static zval
**php_http_message_object_get_prop_ptr(zval
*object
, zval
*member
, const zend_literal
*literal_key TSRMLS_DC
);
748 static HashTable
*php_http_message_object_get_props(zval
*object TSRMLS_DC
);
750 zend_class_entry
*php_http_message_class_entry
;
751 zend_function_entry php_http_message_method_entry
[] = {
752 PHP_HTTP_MESSAGE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
753 PHP_HTTP_MESSAGE_ME(getBody
, ZEND_ACC_PUBLIC
)
754 PHP_HTTP_MESSAGE_ME(setBody
, ZEND_ACC_PUBLIC
)
755 PHP_HTTP_MESSAGE_ME(getHeader
, ZEND_ACC_PUBLIC
)
756 PHP_HTTP_MESSAGE_ME(setHeader
, ZEND_ACC_PUBLIC
)
757 PHP_HTTP_MESSAGE_ME(addHeader
, ZEND_ACC_PUBLIC
)
758 PHP_HTTP_MESSAGE_ME(getHeaders
, ZEND_ACC_PUBLIC
)
759 PHP_HTTP_MESSAGE_ME(setHeaders
, ZEND_ACC_PUBLIC
)
760 PHP_HTTP_MESSAGE_ME(addHeaders
, ZEND_ACC_PUBLIC
)
761 PHP_HTTP_MESSAGE_ME(getType
, ZEND_ACC_PUBLIC
)
762 PHP_HTTP_MESSAGE_ME(setType
, ZEND_ACC_PUBLIC
)
763 PHP_HTTP_MESSAGE_ME(getInfo
, ZEND_ACC_PUBLIC
)
764 PHP_HTTP_MESSAGE_ME(setInfo
, ZEND_ACC_PUBLIC
)
765 PHP_HTTP_MESSAGE_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
766 PHP_HTTP_MESSAGE_ME(setResponseCode
, ZEND_ACC_PUBLIC
)
767 PHP_HTTP_MESSAGE_ME(getResponseStatus
, ZEND_ACC_PUBLIC
)
768 PHP_HTTP_MESSAGE_ME(setResponseStatus
, ZEND_ACC_PUBLIC
)
769 PHP_HTTP_MESSAGE_ME(getRequestMethod
, ZEND_ACC_PUBLIC
)
770 PHP_HTTP_MESSAGE_ME(setRequestMethod
, ZEND_ACC_PUBLIC
)
771 PHP_HTTP_MESSAGE_ME(getRequestUrl
, ZEND_ACC_PUBLIC
)
772 PHP_HTTP_MESSAGE_ME(setRequestUrl
, ZEND_ACC_PUBLIC
)
773 PHP_HTTP_MESSAGE_ME(getHttpVersion
, ZEND_ACC_PUBLIC
)
774 PHP_HTTP_MESSAGE_ME(setHttpVersion
, ZEND_ACC_PUBLIC
)
775 PHP_HTTP_MESSAGE_ME(getParentMessage
, ZEND_ACC_PUBLIC
)
776 PHP_HTTP_MESSAGE_ME(toString
, ZEND_ACC_PUBLIC
)
778 /* implements Countable */
779 PHP_HTTP_MESSAGE_ME(count
, ZEND_ACC_PUBLIC
)
781 /* implements Serializable */
782 PHP_HTTP_MESSAGE_ME(serialize
, ZEND_ACC_PUBLIC
)
783 PHP_HTTP_MESSAGE_ME(unserialize
, ZEND_ACC_PUBLIC
)
785 /* implements Iterator */
786 PHP_HTTP_MESSAGE_ME(rewind
, ZEND_ACC_PUBLIC
)
787 PHP_HTTP_MESSAGE_ME(valid
, ZEND_ACC_PUBLIC
)
788 PHP_HTTP_MESSAGE_ME(current
, ZEND_ACC_PUBLIC
)
789 PHP_HTTP_MESSAGE_ME(key
, ZEND_ACC_PUBLIC
)
790 PHP_HTTP_MESSAGE_ME(next
, ZEND_ACC_PUBLIC
)
792 ZEND_MALIAS(HttpMessage
, __toString
, toString
, PHP_HTTP_ARGS(HttpMessage
, __toString
), ZEND_ACC_PUBLIC
)
794 PHP_HTTP_MESSAGE_ME(detach
, ZEND_ACC_PUBLIC
)
795 PHP_HTTP_MESSAGE_ME(prepend
, ZEND_ACC_PUBLIC
)
796 PHP_HTTP_MESSAGE_ME(reverse
, ZEND_ACC_PUBLIC
)
800 static zend_object_handlers php_http_message_object_handlers
;
801 static HashTable php_http_message_object_prophandlers
;
803 typedef void (*php_http_message_object_prophandler_func_t
)(php_http_message_object_t
*o
, zval
*v TSRMLS_DC
);
805 typedef struct php_http_message_object_prophandler
{
806 php_http_message_object_prophandler_func_t read
;
807 php_http_message_object_prophandler_func_t write
;
808 } php_http_message_object_prophandler_t
;
810 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
) {
811 php_http_message_object_prophandler_t h
= { read
, write
};
812 return zend_hash_add(&php_http_message_object_prophandlers
, prop_str
, prop_len
+ 1, (void *) &h
, sizeof(h
), NULL
);
814 static int php_http_message_object_has_prophandler(const char *prop_str
, size_t prop_len
) {
815 return zend_hash_exists(&php_http_message_object_prophandlers
, prop_str
, prop_len
+ 1);
817 static STATUS
php_http_message_object_get_prophandler(const char *prop_str
, size_t prop_len
, php_http_message_object_prophandler_t
**handler
) {
818 return zend_hash_find(&php_http_message_object_prophandlers
, prop_str
, prop_len
+ 1, (void *) handler
);
820 static void php_http_message_object_prophandler_get_type(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
821 RETVAL_LONG(obj
->message
->type
);
823 static void php_http_message_object_prophandler_set_type(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
824 zval
*cpy
= php_http_ztyp(IS_LONG
, value
);
825 php_http_message_set_type(obj
->message
, Z_LVAL_P(cpy
));
828 static void php_http_message_object_prophandler_get_request_method(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
829 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
) && obj
->message
->http
.info
.request
.method
) {
830 RETVAL_STRING(obj
->message
->http
.info
.request
.method
, 1);
835 static void php_http_message_object_prophandler_set_request_method(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
836 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
837 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
838 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
842 static void php_http_message_object_prophandler_get_request_url(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
843 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
) && obj
->message
->http
.info
.request
.url
) {
844 RETVAL_STRING(obj
->message
->http
.info
.request
.url
, 1);
849 static void php_http_message_object_prophandler_set_request_url(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
850 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, obj
->message
)) {
851 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
852 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
856 static void php_http_message_object_prophandler_get_response_status(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
857 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
) && obj
->message
->http
.info
.response
.status
) {
858 RETVAL_STRING(obj
->message
->http
.info
.response
.status
, 1);
863 static void php_http_message_object_prophandler_set_response_status(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
864 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
865 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
866 STR_SET(obj
->message
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
870 static void php_http_message_object_prophandler_get_response_code(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
871 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
872 RETVAL_LONG(obj
->message
->http
.info
.response
.code
);
877 static void php_http_message_object_prophandler_set_response_code(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
878 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, obj
->message
)) {
879 zval
*cpy
= php_http_ztyp(IS_LONG
, value
);
880 obj
->message
->http
.info
.response
.code
= Z_LVAL_P(cpy
);
884 static void php_http_message_object_prophandler_get_http_version(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
888 php_http_version_to_string(&obj
->message
->http
.version
, &version_str
, &version_len
, NULL
, NULL TSRMLS_CC
);
889 RETVAL_STRINGL(version_str
, version_len
, 0);
891 static void php_http_message_object_prophandler_set_http_version(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
892 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
893 php_http_version_parse(&obj
->message
->http
.version
, Z_STRVAL_P(cpy
) TSRMLS_CC
);
896 static void php_http_message_object_prophandler_get_headers(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
897 array_init(return_value
);
898 zend_hash_copy(Z_ARRVAL_P(return_value
), &obj
->message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
900 static void php_http_message_object_prophandler_set_headers(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
901 zval
*cpy
= php_http_ztyp(IS_ARRAY
, value
);
903 zend_hash_clean(&obj
->message
->hdrs
);
904 zend_hash_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(cpy
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
907 static void php_http_message_object_prophandler_get_body(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
908 if (obj
->body
.handle
) {
909 RETVAL_OBJVAL(obj
->body
, 1);
914 static void php_http_message_object_prophandler_set_body(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
915 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), php_http_message_body_class_entry TSRMLS_CC
)) {
916 if (obj
->body
.handle
) {
917 zend_objects_store_del_ref_by_handle(obj
->body
.handle TSRMLS_CC
);
919 Z_OBJ_ADDREF_P(value
);
920 obj
->body
= Z_OBJVAL_P(value
);
923 static void php_http_message_object_prophandler_get_parent_message(php_http_message_object_t
*obj
, zval
*return_value TSRMLS_DC
) {
924 if (obj
->message
->parent
) {
925 RETVAL_OBJVAL(obj
->parent
, 1);
930 static void php_http_message_object_prophandler_set_parent_message(php_http_message_object_t
*obj
, zval
*value TSRMLS_DC
) {
931 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), php_http_message_class_entry TSRMLS_CC
)) {
932 if (obj
->message
->parent
) {
933 zend_objects_store_del_ref_by_handle(obj
->parent
.handle TSRMLS_CC
);
935 Z_OBJ_ADDREF_P(value
);
936 obj
->parent
= Z_OBJVAL_P(value
);
940 PHP_MINIT_FUNCTION(http_message
)
942 PHP_HTTP_REGISTER_CLASS(http
, Message
, http_message
, php_http_object_class_entry
, 0);
943 php_http_message_class_entry
->create_object
= php_http_message_object_new
;
944 memcpy(&php_http_message_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
945 php_http_message_object_handlers
.clone_obj
= php_http_message_object_clone
;
946 php_http_message_object_handlers
.read_property
= php_http_message_object_read_prop
;
947 php_http_message_object_handlers
.write_property
= php_http_message_object_write_prop
;
948 php_http_message_object_handlers
.get_properties
= php_http_message_object_get_props
;
949 php_http_message_object_handlers
.get_property_ptr_ptr
= php_http_message_object_get_prop_ptr
;
951 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
);
953 zend_hash_init(&php_http_message_object_prophandlers
, 9, NULL
, NULL
, 1);
954 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("type"), PHP_HTTP_NONE
, ZEND_ACC_PROTECTED TSRMLS_CC
);
955 php_http_message_object_add_prophandler(ZEND_STRL("type"), php_http_message_object_prophandler_get_type
, php_http_message_object_prophandler_set_type
);
956 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("body"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
957 php_http_message_object_add_prophandler(ZEND_STRL("body"), php_http_message_object_prophandler_get_body
, php_http_message_object_prophandler_set_body
);
958 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestMethod"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
959 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
);
960 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("requestUrl"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
961 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
);
962 zend_declare_property_string(php_http_message_class_entry
, ZEND_STRL("responseStatus"), "", ZEND_ACC_PROTECTED TSRMLS_CC
);
963 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
);
964 zend_declare_property_long(php_http_message_class_entry
, ZEND_STRL("responseCode"), 0, ZEND_ACC_PROTECTED TSRMLS_CC
);
965 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
);
966 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("httpVersion"), ZEND_ACC_PROTECTED TSRMLS_CC
);
967 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
);
968 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("headers"), ZEND_ACC_PROTECTED TSRMLS_CC
);
969 php_http_message_object_add_prophandler(ZEND_STRL("headers"), php_http_message_object_prophandler_get_headers
, php_http_message_object_prophandler_set_headers
);
970 zend_declare_property_null(php_http_message_class_entry
, ZEND_STRL("parentMessage"), ZEND_ACC_PROTECTED TSRMLS_CC
);
971 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
);
973 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_NONE"), PHP_HTTP_NONE TSRMLS_CC
);
974 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_REQUEST"), PHP_HTTP_REQUEST TSRMLS_CC
);
975 zend_declare_class_constant_long(php_http_message_class_entry
, ZEND_STRL("TYPE_RESPONSE"), PHP_HTTP_RESPONSE TSRMLS_CC
);
980 PHP_MSHUTDOWN_FUNCTION(http_message
)
982 zend_hash_destroy(&php_http_message_object_prophandlers
);
987 void php_http_message_object_reverse(zval
*this_ptr
, zval
*return_value TSRMLS_DC
)
990 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
993 php_http_message_count(i
, obj
->message
);
997 zend_object_value
*ovalues
= NULL
;
998 php_http_message_object_t
**objects
= NULL
;
1001 objects
= ecalloc(i
, sizeof(**objects
));
1002 ovalues
= ecalloc(i
, sizeof(*ovalues
));
1004 /* we are the first message */
1006 ovalues
[0] = getThis()->value
.obj
;
1011 for (i
= 1; obj
->parent
.handle
; ++i
) {
1012 o
.value
.obj
= obj
->parent
;
1013 ovalues
[i
] = o
.value
.obj
;
1014 objects
[i
] = obj
= zend_object_store_get_object(&o TSRMLS_CC
);
1017 /* reorder parents */
1018 for (last
= --i
; i
; --i
) {
1019 objects
[i
]->message
->parent
= objects
[i
-1]->message
;
1020 objects
[i
]->parent
= ovalues
[i
-1];
1022 objects
[0]->message
->parent
= NULL
;
1023 objects
[0]->parent
.handle
= 0;
1024 objects
[0]->parent
.handlers
= NULL
;
1026 /* add ref (why?) */
1027 Z_OBJ_ADDREF_P(getThis());
1028 RETVAL_OBJVAL(ovalues
[last
], 1);
1033 RETURN_ZVAL(getThis(), 1, 0);
1037 void php_http_message_object_prepend(zval
*this_ptr
, zval
*prepend
, zend_bool top TSRMLS_DC
)
1040 php_http_message_t
*save_parent_msg
= NULL
;
1041 zend_object_value save_parent_obj
= {0, NULL
};
1042 php_http_message_object_t
*obj
= zend_object_store_get_object(this_ptr TSRMLS_CC
);
1043 php_http_message_object_t
*prepend_obj
= zend_object_store_get_object(prepend TSRMLS_CC
);
1049 save_parent_obj
= obj
->parent
;
1050 save_parent_msg
= obj
->message
->parent
;
1052 /* iterate to the most parent object */
1053 while (obj
->parent
.handle
) {
1054 m
.value
.obj
= obj
->parent
;
1055 obj
= zend_object_store_get_object(&m TSRMLS_CC
);
1060 obj
->parent
= prepend
->value
.obj
;
1061 obj
->message
->parent
= prepend_obj
->message
;
1064 zend_objects_store_add_ref(prepend TSRMLS_CC
);
1065 while (prepend_obj
->parent
.handle
) {
1066 m
.value
.obj
= prepend_obj
->parent
;
1067 zend_objects_store_add_ref(&m TSRMLS_CC
);
1068 prepend_obj
= zend_object_store_get_object(&m TSRMLS_CC
);
1072 prepend_obj
->parent
= save_parent_obj
;
1073 prepend_obj
->message
->parent
= save_parent_msg
;
1077 zend_object_value
php_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
1079 return php_http_message_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
1082 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
)
1084 zend_object_value ov
;
1085 php_http_message_object_t
*o
;
1087 o
= ecalloc(1, sizeof(php_http_message_object_t
));
1088 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
1089 object_properties_init((zend_object
*) o
, ce
);
1098 o
->parent
= php_http_message_object_new_ex(ce
, msg
->parent
, NULL TSRMLS_CC
);
1100 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
);
1103 ov
.handle
= zend_objects_store_put((zend_object
*) o
, NULL
, php_http_message_object_free
, NULL TSRMLS_CC
);
1104 ov
.handlers
= &php_http_message_object_handlers
;
1109 zend_object_value
php_http_message_object_clone(zval
*this_ptr TSRMLS_DC
)
1111 zend_object_value new_ov
;
1112 php_http_message_object_t
*new_obj
= NULL
;
1113 php_http_message_object_t
*old_obj
= zend_object_store_get_object(this_ptr TSRMLS_CC
);
1115 new_ov
= php_http_message_object_new_ex(old_obj
->zo
.ce
, php_http_message_copy(old_obj
->message
, NULL
), &new_obj TSRMLS_CC
);
1116 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
1121 void php_http_message_object_free(void *object TSRMLS_DC
)
1123 php_http_message_object_t
*o
= (php_http_message_object_t
*) object
;
1126 zval_ptr_dtor(&o
->iterator
);
1130 /* do NOT free recursivly */
1131 php_http_message_dtor(o
->message
);
1135 if (o
->parent
.handle
) {
1136 zend_objects_store_del_ref_by_handle(o
->parent
.handle TSRMLS_CC
);
1138 if (o
->body
.handle
) {
1139 zend_objects_store_del_ref_by_handle(o
->body
.handle TSRMLS_CC
);
1141 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
1146 static zval
**php_http_message_object_get_prop_ptr(zval
*object
, zval
*member
, const zend_literal
*literal_key TSRMLS_DC
)
1148 php_http_message_object_prophandler_t
*handler
;
1149 zval
*copy
= php_http_ztyp(IS_STRING
, member
);
1151 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1152 zval_ptr_dtor(©
);
1153 return &php_http_property_proxy_init(NULL
, object
, member TSRMLS_CC
)->myself
;
1155 zval_ptr_dtor(©
);
1157 return zend_get_std_object_handlers()->get_property_ptr_ptr(object
, member
, literal_key TSRMLS_CC
);
1160 static zval
*php_http_message_object_read_prop(zval
*object
, zval
*member
, int type
, const zend_literal
*literal_key TSRMLS_DC
)
1162 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1163 php_http_message_object_prophandler_t
*handler
;
1164 zval
*return_value
, *copy
= php_http_ztyp(IS_STRING
, member
);
1166 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1167 if (type
== BP_VAR_W
) {
1168 zval_ptr_dtor(©
);
1169 zend_error(E_ERROR
, "Cannot access HttpMessage properties by reference or array key/index");
1173 ALLOC_ZVAL(return_value
);
1174 Z_SET_REFCOUNT_P(return_value
, 0);
1175 Z_UNSET_ISREF_P(return_value
);
1177 handler
->read(obj
, return_value TSRMLS_CC
);
1180 return_value
= zend_get_std_object_handlers()->read_property(object
, member
, type
, literal_key TSRMLS_CC
);
1183 zval_ptr_dtor(©
);
1185 return return_value
;
1188 static void php_http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value
, const zend_literal
*literal_key TSRMLS_DC
)
1190 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1191 php_http_message_object_prophandler_t
*handler
;
1192 zval
*copy
= php_http_ztyp(IS_STRING
, member
);
1194 if (SUCCESS
== php_http_message_object_get_prophandler(Z_STRVAL_P(copy
), Z_STRLEN_P(copy
), &handler
)) {
1195 handler
->write(obj
, value TSRMLS_CC
);
1197 zend_get_std_object_handlers()->write_property(object
, member
, value
, literal_key TSRMLS_CC
);
1200 zval_ptr_dtor(©
);
1204 static HashTable
*php_http_message_object_get_props(zval
*object TSRMLS_DC
)
1207 php_http_message_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
1208 php_http_message_t
*msg
= obj
->message
;
1209 HashTable
*props
= zend_get_std_object_handlers()->get_properties(object TSRMLS_CC
);
1210 zval array
, *parent
, *body
;
1213 INIT_PZVAL_ARRAY(&array
, props
);
1215 #define ASSOC_PROP(array, ptype, name, val) \
1217 char *m_prop_name; \
1219 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
1220 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
1221 efree(m_prop_name); \
1223 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
1224 #define ASSOC_STRINGL(array, name, val, len) ASSOC_STRINGL_EX(array, name, val, len, 1)
1225 #define ASSOC_STRINGL_EX(array, name, val, len, cpy) \
1227 char *m_prop_name; \
1229 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
1230 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, cpy); \
1231 efree(m_prop_name); \
1234 ASSOC_PROP(array
, long, "type", msg
->type
);
1235 ASSOC_STRINGL_EX(array
, "httpVersion", version
, spprintf(&version
, 0, "%u.%u", msg
->http
.version
.major
, msg
->http
.version
.minor
), 0);
1237 switch (msg
->type
) {
1238 case PHP_HTTP_REQUEST
:
1239 ASSOC_PROP(array
, long, "responseCode", 0);
1240 ASSOC_STRINGL(array
, "responseStatus", "", 0);
1241 ASSOC_STRING(array
, "requestMethod", STR_PTR(msg
->http
.info
.request
.method
));
1242 ASSOC_STRING(array
, "requestUrl", STR_PTR(msg
->http
.info
.request
.url
));
1245 case PHP_HTTP_RESPONSE
:
1246 ASSOC_PROP(array
, long, "responseCode", msg
->http
.info
.response
.code
);
1247 ASSOC_STRING(array
, "responseStatus", STR_PTR(msg
->http
.info
.response
.status
));
1248 ASSOC_STRINGL(array
, "requestMethod", "", 0);
1249 ASSOC_STRINGL(array
, "requestUrl", "", 0);
1254 ASSOC_PROP(array
, long, "responseCode", 0);
1255 ASSOC_STRINGL(array
, "responseStatus", "", 0);
1256 ASSOC_STRINGL(array
, "requestMethod", "", 0);
1257 ASSOC_STRINGL(array
, "requestUrl", "", 0);
1261 MAKE_STD_ZVAL(headers
);
1262 array_init(headers
);
1263 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1264 ASSOC_PROP(array
, zval
, "headers", headers
);
1266 MAKE_STD_ZVAL(body
);
1267 if (!obj
->body
.handle
) {
1268 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
);
1270 ZVAL_OBJVAL(body
, obj
->body
, 1);
1271 ASSOC_PROP(array
, zval
, "body", body
);
1273 MAKE_STD_ZVAL(parent
);
1275 ZVAL_OBJVAL(parent
, obj
->parent
, 1);
1279 ASSOC_PROP(array
, zval
, "parentMessage", parent
);
1286 PHP_METHOD(HttpMessage
, __construct
)
1289 char *message
= NULL
;
1290 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1292 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1293 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
1294 php_http_message_t
*msg
= obj
->message
;
1296 php_http_message_dtor(msg
);
1297 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1298 if ((obj
->message
= php_http_message_parse(msg
, message
, length TSRMLS_CC
))) {
1299 if (obj
->message
->parent
) {
1300 obj
->parent
= php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj
->message
->parent
, NULL TSRMLS_CC
);
1303 obj
->message
= php_http_message_init(msg
, 0 TSRMLS_CC
);
1305 } end_error_handling();
1307 if (!obj
->message
) {
1308 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1310 } end_error_handling();
1314 PHP_METHOD(HttpMessage
, getBody
)
1316 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1317 if (SUCCESS
== zend_parse_parameters_none()) {
1318 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1320 if (!obj
->message
) {
1321 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1324 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
)) {
1325 RETVAL_OBJVAL(obj
->body
, 1);
1328 } end_error_handling();
1331 PHP_METHOD(HttpMessage
, setBody
)
1335 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &zbody
, php_http_message_body_class_entry
)) {
1336 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1337 php_http_message_body_object_t
*body_obj
= zend_object_store_get_object(zbody TSRMLS_CC
);
1339 if (!obj
->message
) {
1340 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1343 php_http_message_body_dtor(&obj
->message
->body
);
1344 php_http_message_body_copy(body_obj
->body
, &obj
->message
->body
, 0);
1345 Z_OBJ_ADDREF_P(zbody
);
1346 obj
->body
= Z_OBJVAL_P(zbody
);
1348 RETVAL_ZVAL(getThis(), 1, 0);
1351 PHP_METHOD(HttpMessage
, getHeader
)
1356 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header_str
, &header_len
)) {
1357 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1360 if (!obj
->message
) {
1361 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1364 if ((header
= php_http_message_header(obj
->message
, header_str
, header_len
, 0))) {
1365 RETURN_ZVAL(header
, 1, 1);
1371 PHP_METHOD(HttpMessage
, getHeaders
)
1373 if (SUCCESS
== zend_parse_parameters_none()) {
1374 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1376 if (!obj
->message
) {
1377 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1380 array_init(return_value
);
1381 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(return_value
));
1385 PHP_METHOD(HttpMessage
, setHeader
)
1387 zval
*zvalue
= NULL
;
1391 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|z!", &name_str
, &name_len
, &zvalue
)) {
1392 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1393 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1395 if (!obj
->message
) {
1396 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1400 zend_hash_del(&obj
->message
->hdrs
, name
, name_len
+ 1);
1403 zend_hash_update(&obj
->message
->hdrs
, name
, name_len
+ 1, &zvalue
, sizeof(void *), NULL
);
1407 RETVAL_ZVAL(getThis(), 1, 0);
1410 PHP_METHOD(HttpMessage
, setHeaders
)
1412 zval
*new_headers
= NULL
;
1414 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &new_headers
)) {
1415 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1417 if (!obj
->message
) {
1418 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1421 zend_hash_clean(&obj
->message
->hdrs
);
1423 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, 0, ARRAY_JOIN_PRETTIFY
|ARRAY_JOIN_STRONLY
);
1426 RETVAL_ZVAL(getThis(), 1, 0);
1429 PHP_METHOD(HttpMessage
, addHeader
)
1435 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sz", &name_str
, &name_len
, &zvalue
)) {
1436 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1437 char *name
= php_http_pretty_key(estrndup(name_str
, name_len
), name_len
, 1, 1);
1440 if (!obj
->message
) {
1441 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1445 if ((header
= php_http_message_header(obj
->message
, name
, name_len
, 0))) {
1446 convert_to_array(header
);
1447 zend_hash_next_index_insert(Z_ARRVAL_P(header
), &zvalue
, sizeof(void *), NULL
);
1449 zend_hash_update(&obj
->message
->hdrs
, name
, name_len
+ 1, &zvalue
, sizeof(void *), NULL
);
1453 RETVAL_ZVAL(getThis(), 1, 0);
1456 PHP_METHOD(HttpMessage
, addHeaders
)
1459 zend_bool append
= 0;
1461 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
1462 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1464 if (!obj
->message
) {
1465 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1468 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, append
, ARRAY_JOIN_STRONLY
|ARRAY_JOIN_PRETTIFY
);
1470 RETVAL_ZVAL(getThis(), 1, 0);
1473 PHP_METHOD(HttpMessage
, getType
)
1475 if (SUCCESS
== zend_parse_parameters_none()) {
1476 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1478 if (!obj
->message
) {
1479 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1482 RETURN_LONG(obj
->message
->type
);
1486 PHP_METHOD(HttpMessage
, setType
)
1490 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
1491 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1493 if (!obj
->message
) {
1494 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1497 php_http_message_set_type(obj
->message
, type
);
1499 RETVAL_ZVAL(getThis(), 1, 0);
1502 PHP_METHOD(HttpMessage
, getInfo
)
1504 if (SUCCESS
== zend_parse_parameters_none()) {
1505 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1507 if (!obj
->message
) {
1508 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1511 switch (obj
->message
->type
) {
1512 case PHP_HTTP_REQUEST
:
1513 Z_STRLEN_P(return_value
) = spprintf(&Z_STRVAL_P(return_value
), 0, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&obj
->message
->http
, ""));
1515 case PHP_HTTP_RESPONSE
:
1516 Z_STRLEN_P(return_value
) = spprintf(&Z_STRVAL_P(return_value
), 0, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&obj
->message
->http
, ""));
1522 Z_TYPE_P(return_value
) = IS_STRING
;
1528 PHP_METHOD(HttpMessage
, setInfo
)
1532 php_http_info_t inf
;
1534 if ( SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)
1535 && php_http_info_parse(&inf
, str TSRMLS_CC
)) {
1536 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1538 if (!obj
->message
) {
1539 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1542 php_http_message_set_info(obj
->message
, &inf
);
1543 php_http_info_dtor(&inf
);
1545 RETVAL_ZVAL(getThis(), 1, 0);
1548 PHP_METHOD(HttpMessage
, getHttpVersion
)
1550 if (SUCCESS
== zend_parse_parameters_none()) {
1553 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1555 if (!obj
->message
) {
1556 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1559 php_http_version_to_string(&obj
->message
->http
.version
, &str
, &len
, NULL
, NULL TSRMLS_CC
);
1560 RETURN_STRINGL(str
, len
, 0);
1566 PHP_METHOD(HttpMessage
, setHttpVersion
)
1571 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &v_str
, &v_len
)) {
1572 php_http_version_t version
;
1573 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1575 if (!obj
->message
) {
1576 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1579 if (php_http_version_parse(&version
, v_str TSRMLS_CC
)) {
1580 obj
->message
->http
.version
= version
;
1583 RETVAL_ZVAL(getThis(), 1, 0);
1586 PHP_METHOD(HttpMessage
, getResponseCode
)
1588 if (SUCCESS
== zend_parse_parameters_none()) {
1589 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1591 if (!obj
->message
) {
1592 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1595 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1596 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
1601 PHP_METHOD(HttpMessage
, setResponseCode
)
1604 zend_bool strict
= 1;
1606 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l|b", &code
, &strict
)) {
1607 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1609 if (!obj
->message
) {
1610 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1613 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1614 if (strict
&& (code
< 100 || code
> 599)) {
1615 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Invalid response code (100-599): %ld", code
);
1619 obj
->message
->http
.info
.response
.code
= code
;
1621 RETVAL_ZVAL(getThis(), 1, 0);
1624 PHP_METHOD(HttpMessage
, getResponseStatus
)
1626 if (SUCCESS
== zend_parse_parameters_none()) {
1627 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1629 if (!obj
->message
) {
1630 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1633 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1634 if (obj
->message
->http
.info
.response
.status
) {
1635 RETURN_STRING(obj
->message
->http
.info
.response
.status
, 1);
1637 RETURN_EMPTY_STRING();
1644 PHP_METHOD(HttpMessage
, setResponseStatus
)
1649 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &status
, &status_len
)) {
1650 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1652 if (!obj
->message
) {
1653 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1656 PHP_HTTP_MESSAGE_TYPE_CHECK(RESPONSE
, obj
->message
, RETURN_FALSE
);
1657 STR_SET(obj
->message
->http
.info
.response
.status
, estrndup(status
, status_len
));
1659 RETVAL_ZVAL(getThis(), 1, 0);
1662 PHP_METHOD(HttpMessage
, getRequestMethod
)
1664 if (SUCCESS
== zend_parse_parameters_none()) {
1665 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1667 if (!obj
->message
) {
1668 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1671 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1672 if (obj
->message
->http
.info
.request
.method
) {
1673 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
1675 RETURN_EMPTY_STRING();
1682 PHP_METHOD(HttpMessage
, setRequestMethod
)
1687 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1688 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1690 if (!obj
->message
) {
1691 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1694 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1695 if (method_len
< 1) {
1696 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
1700 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
1702 RETVAL_ZVAL(getThis(), 1, 0);
1705 PHP_METHOD(HttpMessage
, getRequestUrl
)
1707 if (SUCCESS
== zend_parse_parameters_none()) {
1708 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1710 if (!obj
->message
) {
1711 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1714 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1715 if (obj
->message
->http
.info
.request
.url
) {
1716 RETURN_STRING(obj
->message
->http
.info
.request
.url
, 1);
1718 RETURN_EMPTY_STRING();
1725 PHP_METHOD(HttpMessage
, setRequestUrl
)
1730 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &url_str
, &url_len
)) {
1731 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1733 if (!obj
->message
) {
1734 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1737 PHP_HTTP_MESSAGE_TYPE_CHECK(REQUEST
, obj
->message
, RETURN_FALSE
);
1739 php_http_error(HE_WARNING
, PHP_HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUrl to an empty string");
1742 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(url_str
, url_len
));
1744 RETVAL_ZVAL(getThis(), 1, 0);
1748 PHP_METHOD(HttpMessage
, getParentMessage
)
1750 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1751 if (SUCCESS
== zend_parse_parameters_none()) {
1752 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1754 if (!obj
->message
) {
1755 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1758 if (obj
->message
->parent
) {
1759 RETVAL_OBJVAL(obj
->parent
, 1);
1761 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "HttpMessage does not have a parent message");
1764 } end_error_handling();
1767 PHP_METHOD(HttpMessage
, toString
)
1769 zend_bool include_parent
= 0;
1771 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1772 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1776 if (!obj
->message
) {
1777 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1780 if (include_parent
) {
1781 php_http_message_serialize(obj
->message
, &string
, &length
);
1783 php_http_message_to_string(obj
->message
, &string
, &length
);
1786 RETURN_STRINGL(string
, length
, 0);
1789 RETURN_EMPTY_STRING();
1792 PHP_METHOD(HttpMessage
, serialize
)
1794 if (SUCCESS
== zend_parse_parameters_none()) {
1795 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1799 if (!obj
->message
) {
1800 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1803 php_http_message_serialize(obj
->message
, &string
, &length
);
1804 RETURN_STRINGL(string
, length
, 0);
1806 RETURN_EMPTY_STRING();
1809 PHP_METHOD(HttpMessage
, unserialize
)
1814 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &serialized
, &length
)) {
1815 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1816 php_http_message_t
*msg
;
1819 php_http_message_dtor(obj
->message
);
1820 efree(obj
->message
);
1822 if ((msg
= php_http_message_parse(NULL
, serialized
, (size_t) length TSRMLS_CC
))) {
1825 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1826 php_http_error(HE_ERROR
, PHP_HTTP_E_RUNTIME
, "Could not unserialize HttpMessage");
1831 PHP_METHOD(HttpMessage
, detach
)
1833 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
1834 if (SUCCESS
== zend_parse_parameters_none()) {
1835 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1837 if (!obj
->message
) {
1838 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1841 RETVAL_OBJVAL(php_http_message_object_new_ex(obj
->zo
.ce
, php_http_message_copy(obj
->message
, NULL
), NULL TSRMLS_CC
), 0);
1843 } end_error_handling();
1846 PHP_METHOD(HttpMessage
, prepend
)
1851 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O|b", &prepend
, php_http_message_class_entry
, &top
)) {
1852 php_http_message_t
*msg
[2];
1853 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1854 php_http_message_object_t
*prepend_obj
= zend_object_store_get_object(prepend TSRMLS_CC
);
1856 if (!obj
->message
) {
1857 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1859 if (!prepend_obj
->message
) {
1860 prepend_obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1864 for (msg
[0] = obj
->message
; msg
[0]; msg
[0] = msg
[0]->parent
) {
1865 for (msg
[1] = prepend_obj
->message
; msg
[1]; msg
[1] = msg
[1]->parent
) {
1866 if (msg
[0] == msg
[1]) {
1867 php_http_error(HE_THROW
, PHP_HTTP_E_INVALID_PARAM
, "Cannot prepend a message located within the same message chain");
1873 php_http_message_object_prepend(getThis(), prepend
, top TSRMLS_CC
);
1877 PHP_METHOD(HttpMessage
, reverse
)
1879 if (SUCCESS
== zend_parse_parameters_none()) {
1880 php_http_message_object_reverse(getThis(), return_value TSRMLS_CC
);
1884 PHP_METHOD(HttpMessage
, count
)
1886 if (SUCCESS
== zend_parse_parameters_none()) {
1888 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1890 if (!obj
->message
) {
1891 obj
->message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
1894 php_http_message_count(i
, obj
->message
);
1900 PHP_METHOD(HttpMessage
, rewind
)
1902 if (SUCCESS
== zend_parse_parameters_none()) {
1903 zval
*zobj
= getThis();
1904 php_http_message_object_t
*obj
= zend_object_store_get_object(zobj TSRMLS_CC
);
1906 if (obj
->iterator
) {
1907 zval_ptr_dtor(&obj
->iterator
);
1910 obj
->iterator
= zobj
;
1914 PHP_METHOD(HttpMessage
, valid
)
1916 if (SUCCESS
== zend_parse_parameters_none()) {
1917 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1919 RETURN_BOOL(obj
->iterator
!= NULL
);
1923 PHP_METHOD(HttpMessage
, next
)
1925 if (SUCCESS
== zend_parse_parameters_none()) {
1926 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1928 if (obj
->iterator
) {
1929 php_http_message_object_t
*itr
= zend_object_store_get_object(obj
->iterator TSRMLS_CC
);
1931 if (itr
&& itr
->parent
.handle
) {
1932 zval
*old
= obj
->iterator
;
1933 MAKE_STD_ZVAL(obj
->iterator
);
1934 ZVAL_OBJVAL(obj
->iterator
, itr
->parent
, 1);
1935 zval_ptr_dtor(&old
);
1937 zval_ptr_dtor(&obj
->iterator
);
1938 obj
->iterator
= NULL
;
1944 PHP_METHOD(HttpMessage
, key
)
1946 if (SUCCESS
== zend_parse_parameters_none()) {
1947 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1949 RETURN_LONG(obj
->iterator
? obj
->iterator
->value
.obj
.handle
:0);
1953 PHP_METHOD(HttpMessage
, current
)
1955 if (SUCCESS
== zend_parse_parameters_none()) {
1956 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1958 if (obj
->iterator
) {
1959 RETURN_ZVAL(obj
->iterator
, 1, 0);
1969 * vim600: noet sw=4 ts=4 fdm=marker
1970 * vim<600: noet sw=4 ts=4