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-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #define HTTP_WANT_SAPI
16 #define HTTP_WANT_CURL
17 #define HTTP_WANT_MAGIC
22 #include "zend_interfaces.h"
23 #include "ext/standard/url.h"
25 #include "php_http_api.h"
26 #include "php_http_send_api.h"
27 #include "php_http_url_api.h"
28 #include "php_http_message_api.h"
29 #include "php_http_message_object.h"
30 #include "php_http_exception_object.h"
31 #include "php_http_response_object.h"
32 #include "php_http_request_method_api.h"
33 #include "php_http_request_api.h"
34 #include "php_http_request_object.h"
36 #if defined(HTTP_HAVE_SPL) && !defined(WONKY)
37 /* SPL doesn't install its headers */
38 extern PHPAPI zend_class_entry
*spl_ce_Countable
;
41 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpMessage, method, 0, req_args)
42 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpMessage, method, 0)
43 #define HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, HTTP_ARGS(HttpMessage, method), visibility)
45 HTTP_BEGIN_ARGS(__construct
, 0)
46 HTTP_ARG_VAL(message
, 0)
49 HTTP_BEGIN_ARGS(factory
, 0)
50 HTTP_ARG_VAL(message
, 0)
51 HTTP_ARG_VAL(class_name
, 0)
54 HTTP_EMPTY_ARGS(getBody
);
55 HTTP_BEGIN_ARGS(setBody
, 1)
59 HTTP_BEGIN_ARGS(getHeader
, 1)
60 HTTP_ARG_VAL(header
, 0)
63 HTTP_EMPTY_ARGS(getHeaders
);
64 HTTP_BEGIN_ARGS(setHeaders
, 1)
65 HTTP_ARG_VAL(headers
, 0)
68 HTTP_BEGIN_ARGS(addHeaders
, 1)
69 HTTP_ARG_VAL(headers
, 0)
70 HTTP_ARG_VAL(append
, 0)
73 HTTP_EMPTY_ARGS(getType
);
74 HTTP_BEGIN_ARGS(setType
, 1)
78 HTTP_EMPTY_ARGS(getResponseCode
);
79 HTTP_BEGIN_ARGS(setResponseCode
, 1)
80 HTTP_ARG_VAL(response_code
, 0)
83 HTTP_EMPTY_ARGS(getResponseStatus
);
84 HTTP_BEGIN_ARGS(setResponseStatus
, 1)
85 HTTP_ARG_VAL(response_status
, 0)
88 HTTP_EMPTY_ARGS(getRequestMethod
);
89 HTTP_BEGIN_ARGS(setRequestMethod
, 1)
90 HTTP_ARG_VAL(request_method
, 0)
93 HTTP_EMPTY_ARGS(getRequestUrl
);
94 HTTP_BEGIN_ARGS(setRequestUrl
, 1)
98 HTTP_EMPTY_ARGS(getHttpVersion
);
99 HTTP_BEGIN_ARGS(setHttpVersion
, 1)
100 HTTP_ARG_VAL(http_version
, 0)
103 HTTP_BEGIN_ARGS(guessContentType
, 1)
104 HTTP_ARG_VAL(magic_file
, 0)
105 HTTP_ARG_VAL(magic_mode
, 0)
108 HTTP_EMPTY_ARGS(getParentMessage
);
109 HTTP_EMPTY_ARGS(send
);
110 HTTP_BEGIN_ARGS(toString
, 0)
111 HTTP_ARG_VAL(include_parent
, 0)
114 HTTP_EMPTY_ARGS(toMessageTypeObject
);
116 HTTP_EMPTY_ARGS(count
);
118 HTTP_EMPTY_ARGS(serialize
);
119 HTTP_BEGIN_ARGS(unserialize
, 1)
120 HTTP_ARG_VAL(serialized
, 0)
123 HTTP_EMPTY_ARGS(rewind
);
124 HTTP_EMPTY_ARGS(valid
);
125 HTTP_EMPTY_ARGS(key
);
126 HTTP_EMPTY_ARGS(current
);
127 HTTP_EMPTY_ARGS(next
);
129 HTTP_EMPTY_ARGS(detach
);
130 HTTP_BEGIN_ARGS(prepend
, 1)
131 HTTP_ARG_OBJ(HttpMessage
, message
, 0)
133 HTTP_EMPTY_ARGS(reverse
);
135 #define http_message_object_read_prop _http_message_object_read_prop
136 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
);
137 #define http_message_object_write_prop _http_message_object_write_prop
138 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
);
139 #define http_message_object_get_props _http_message_object_get_props
140 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
);
142 #define OBJ_PROP_CE http_message_object_ce
143 zend_class_entry
*http_message_object_ce
;
144 zend_function_entry http_message_object_fe
[] = {
145 HTTP_MESSAGE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
146 HTTP_MESSAGE_ME(getBody
, ZEND_ACC_PUBLIC
)
147 HTTP_MESSAGE_ME(setBody
, ZEND_ACC_PUBLIC
)
148 HTTP_MESSAGE_ME(getHeader
, ZEND_ACC_PUBLIC
)
149 HTTP_MESSAGE_ME(getHeaders
, ZEND_ACC_PUBLIC
)
150 HTTP_MESSAGE_ME(setHeaders
, ZEND_ACC_PUBLIC
)
151 HTTP_MESSAGE_ME(addHeaders
, ZEND_ACC_PUBLIC
)
152 HTTP_MESSAGE_ME(getType
, ZEND_ACC_PUBLIC
)
153 HTTP_MESSAGE_ME(setType
, ZEND_ACC_PUBLIC
)
154 HTTP_MESSAGE_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
155 HTTP_MESSAGE_ME(setResponseCode
, ZEND_ACC_PUBLIC
)
156 HTTP_MESSAGE_ME(getResponseStatus
, ZEND_ACC_PUBLIC
)
157 HTTP_MESSAGE_ME(setResponseStatus
, ZEND_ACC_PUBLIC
)
158 HTTP_MESSAGE_ME(getRequestMethod
, ZEND_ACC_PUBLIC
)
159 HTTP_MESSAGE_ME(setRequestMethod
, ZEND_ACC_PUBLIC
)
160 HTTP_MESSAGE_ME(getRequestUrl
, ZEND_ACC_PUBLIC
)
161 HTTP_MESSAGE_ME(setRequestUrl
, ZEND_ACC_PUBLIC
)
162 HTTP_MESSAGE_ME(getHttpVersion
, ZEND_ACC_PUBLIC
)
163 HTTP_MESSAGE_ME(setHttpVersion
, ZEND_ACC_PUBLIC
)
164 HTTP_MESSAGE_ME(guessContentType
, ZEND_ACC_PUBLIC
)
165 HTTP_MESSAGE_ME(getParentMessage
, ZEND_ACC_PUBLIC
)
166 HTTP_MESSAGE_ME(send
, ZEND_ACC_PUBLIC
)
167 HTTP_MESSAGE_ME(toString
, ZEND_ACC_PUBLIC
)
168 HTTP_MESSAGE_ME(toMessageTypeObject
, ZEND_ACC_PUBLIC
)
170 /* implements Countable */
171 HTTP_MESSAGE_ME(count
, ZEND_ACC_PUBLIC
)
173 /* implements Serializable */
174 HTTP_MESSAGE_ME(serialize
, ZEND_ACC_PUBLIC
)
175 HTTP_MESSAGE_ME(unserialize
, ZEND_ACC_PUBLIC
)
177 /* implements Iterator */
178 HTTP_MESSAGE_ME(rewind
, ZEND_ACC_PUBLIC
)
179 HTTP_MESSAGE_ME(valid
, ZEND_ACC_PUBLIC
)
180 HTTP_MESSAGE_ME(current
, ZEND_ACC_PUBLIC
)
181 HTTP_MESSAGE_ME(key
, ZEND_ACC_PUBLIC
)
182 HTTP_MESSAGE_ME(next
, ZEND_ACC_PUBLIC
)
184 ZEND_MALIAS(HttpMessage
, __toString
, toString
, HTTP_ARGS(HttpMessage
, toString
), ZEND_ACC_PUBLIC
)
186 HTTP_MESSAGE_ME(factory
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
187 ZEND_MALIAS(HttpMessage
, fromString
, factory
, HTTP_ARGS(HttpMessage
, factory
), ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
189 HTTP_MESSAGE_ME(detach
, ZEND_ACC_PUBLIC
)
190 HTTP_MESSAGE_ME(prepend
, ZEND_ACC_PUBLIC
)
191 HTTP_MESSAGE_ME(reverse
, ZEND_ACC_PUBLIC
)
195 static zend_object_handlers http_message_object_handlers
;
197 PHP_MINIT_FUNCTION(http_message_object
)
199 HTTP_REGISTER_CLASS_EX(HttpMessage
, http_message_object
, NULL
, 0);
202 # ifdef HTTP_HAVE_SPL
203 zend_class_implements(http_message_object_ce TSRMLS_CC
, 3, spl_ce_Countable
, zend_ce_serializable
, zend_ce_iterator
);
205 zend_class_implements(http_message_object_ce TSRMLS_CC
, 2, zend_ce_serializable
, zend_ce_iterator
);
208 zend_class_implements(http_message_object_ce TSRMLS_CC
, 1, zend_ce_iterator
);
211 http_message_object_handlers
.clone_obj
= _http_message_object_clone_obj
;
212 http_message_object_handlers
.read_property
= http_message_object_read_prop
;
213 http_message_object_handlers
.write_property
= http_message_object_write_prop
;
214 http_message_object_handlers
.get_properties
= http_message_object_get_props
;
215 http_message_object_handlers
.get_property_ptr_ptr
= NULL
;
217 DCL_PROP(PROTECTED
, long, type
, HTTP_MSG_NONE
);
218 DCL_PROP(PROTECTED
, string
, body
, "");
219 DCL_PROP(PROTECTED
, string
, requestMethod
, "");
220 DCL_PROP(PROTECTED
, string
, requestUrl
, "");
221 DCL_PROP(PROTECTED
, string
, responseStatus
, "");
222 DCL_PROP(PROTECTED
, long, responseCode
, 0);
223 DCL_PROP_N(PROTECTED
, httpVersion
);
224 DCL_PROP_N(PROTECTED
, headers
);
225 DCL_PROP_N(PROTECTED
, parentMessage
);
228 DCL_CONST(long, "TYPE_NONE", HTTP_MSG_NONE
);
229 DCL_CONST(long, "TYPE_REQUEST", HTTP_MSG_REQUEST
);
230 DCL_CONST(long, "TYPE_RESPONSE", HTTP_MSG_RESPONSE
);
233 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE
);
234 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST
);
235 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE
);
240 void _http_message_object_reverse(zval
*this_ptr
, zval
*return_value TSRMLS_DC
)
243 getObject(http_message_object
, obj
);
246 http_message_count(i
, obj
->message
);
250 zend_object_value
*ovalues
= NULL
;
251 http_message_object
**objects
= NULL
;
254 objects
= ecalloc(i
, sizeof(http_message_object
*));
255 ovalues
= ecalloc(i
, sizeof(zend_object_value
));
257 /* we are the first message */
259 ovalues
[0] = getThis()->value
.obj
;
264 for (i
= 1; obj
->parent
.handle
; ++i
) {
265 o
.value
.obj
= obj
->parent
;
266 ovalues
[i
] = o
.value
.obj
;
267 objects
[i
] = obj
= zend_object_store_get_object(&o TSRMLS_CC
);
270 /* reorder parents */
271 for (last
= --i
; i
; --i
) {
272 objects
[i
]->message
->parent
= objects
[i
-1]->message
;
273 objects
[i
]->parent
= ovalues
[i
-1];
275 objects
[0]->message
->parent
= NULL
;
276 objects
[0]->parent
.handle
= 0;
277 objects
[0]->parent
.handlers
= NULL
;
280 Z_OBJ_ADDREF_P(getThis());
281 RETVAL_OBJVAL(ovalues
[last
], 1);
286 RETURN_ZVAL(getThis(), 1, 0);
290 void _http_message_object_prepend_ex(zval
*this_ptr
, zval
*prepend
, zend_bool top TSRMLS_DC
)
293 http_message
*save_parent_msg
= NULL
;
294 zend_object_value save_parent_obj
= {0, NULL
};
295 getObject(http_message_object
, obj
);
296 getObjectEx(http_message_object
, prepend_obj
, prepend
);
302 save_parent_obj
= obj
->parent
;
303 save_parent_msg
= obj
->message
->parent
;
305 /* iterate to the most parent object */
306 while (obj
->parent
.handle
) {
307 m
.value
.obj
= obj
->parent
;
308 obj
= zend_object_store_get_object(&m TSRMLS_CC
);
313 obj
->parent
= prepend
->value
.obj
;
314 obj
->message
->parent
= prepend_obj
->message
;
317 zend_objects_store_add_ref(prepend TSRMLS_CC
);
318 while (prepend_obj
->parent
.handle
) {
319 m
.value
.obj
= prepend_obj
->parent
;
320 zend_objects_store_add_ref(&m TSRMLS_CC
);
321 prepend_obj
= zend_object_store_get_object(&m TSRMLS_CC
);
325 prepend_obj
->parent
= save_parent_obj
;
326 prepend_obj
->message
->parent
= save_parent_msg
;
330 zend_object_value
_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
332 return http_message_object_new_ex(ce
, NULL
, NULL
);
335 zend_object_value
_http_message_object_new_ex(zend_class_entry
*ce
, http_message
*msg
, http_message_object
**ptr TSRMLS_DC
)
337 zend_object_value ov
;
338 http_message_object
*o
;
340 o
= ecalloc(1, sizeof(http_message_object
));
350 o
->parent
= http_message_object_new_ex(ce
, msg
->parent
, NULL
);
354 ALLOC_HASHTABLE(OBJ_PROP(o
));
355 zend_hash_init(OBJ_PROP(o
), zend_hash_num_elements(&ce
->default_properties
), NULL
, ZVAL_PTR_DTOR
, 0);
356 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
358 ov
.handle
= putObject(http_message_object
, o
);
359 ov
.handlers
= &http_message_object_handlers
;
364 zend_object_value
_http_message_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
366 zend_object_value new_ov
;
367 http_message_object
*new_obj
= NULL
;
368 getObject(http_message_object
, old_obj
);
370 new_ov
= http_message_object_new_ex(old_obj
->zo
.ce
, http_message_dup(old_obj
->message
), &new_obj
);
371 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
376 void _http_message_object_free(zend_object
*object TSRMLS_DC
)
378 http_message_object
*o
= (http_message_object
*) object
;
381 http_message_dtor(o
->message
);
384 if (o
->parent
.handle
) {
389 p
.value
.obj
= o
->parent
;
390 zend_objects_store_del_ref(&p TSRMLS_CC
);
395 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
397 getObjectEx(http_message_object
, obj
, object
);
398 http_message
*msg
= obj
->message
;
401 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1);
403 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
405 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
406 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
410 if (type
== BP_VAR_W
) {
411 zend_error(E_ERROR
, "Cannot access HttpMessage properties by reference or array key/index");
415 ALLOC_ZVAL(return_value
);
416 return_value
->refcount
= 0;
417 return_value
->is_ref
= 0;
425 case HTTP_MSG_PROPHASH_TYPE
:
426 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
427 RETVAL_LONG(msg
->type
);
430 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
431 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
432 RETVAL_DOUBLE(msg
->http
.version
);
435 case HTTP_MSG_PROPHASH_BODY
:
436 case HTTP_MSG_CHILD_PROPHASH_BODY
:
437 phpstr_fix(PHPSTR(msg
));
438 RETVAL_PHPSTR(PHPSTR(msg
), 0, 1);
441 case HTTP_MSG_PROPHASH_HEADERS
:
442 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
443 array_init(return_value
);
444 zend_hash_copy(Z_ARRVAL_P(return_value
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
447 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
448 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
450 RETVAL_OBJVAL(obj
->parent
, 1);
456 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
457 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
458 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.method
) {
459 RETVAL_STRING(msg
->http
.info
.request
.method
, 1);
465 case HTTP_MSG_PROPHASH_REQUEST_URL
:
466 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL
:
467 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.url
) {
468 RETVAL_STRING(msg
->http
.info
.request
.url
, 1);
474 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
475 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
476 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
477 RETVAL_LONG(msg
->http
.info
.response
.code
);
483 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
484 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
485 if (HTTP_MSG_TYPE(RESPONSE
, msg
) && msg
->http
.info
.response
.status
) {
486 RETVAL_STRING(msg
->http
.info
.response
.status
, 1);
494 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
503 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
)
505 getObjectEx(http_message_object
, obj
, object
);
506 http_message
*msg
= obj
->message
;
509 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
) + 1);
511 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
513 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
514 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
519 cpy
= zval_copy(Z_TYPE_P(value
), value
);
527 case HTTP_MSG_PROPHASH_TYPE
:
528 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
529 convert_to_long(cpy
);
530 http_message_set_type(msg
, Z_LVAL_P(cpy
));
533 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
534 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
535 convert_to_double(cpy
);
536 msg
->http
.version
= Z_DVAL_P(cpy
);
539 case HTTP_MSG_PROPHASH_BODY
:
540 case HTTP_MSG_CHILD_PROPHASH_BODY
:
541 convert_to_string(cpy
);
542 phpstr_dtor(PHPSTR(msg
));
543 phpstr_from_string_ex(PHPSTR(msg
), Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
));
546 case HTTP_MSG_PROPHASH_HEADERS
:
547 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
548 convert_to_array(cpy
);
549 zend_hash_clean(&msg
->hdrs
);
550 zend_hash_copy(&msg
->hdrs
, Z_ARRVAL_P(cpy
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
553 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
554 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
555 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), http_message_object_ce TSRMLS_CC
)) {
558 tmp
.value
.obj
= obj
->parent
;
561 Z_OBJ_ADDREF_P(value
);
562 obj
->parent
= value
->value
.obj
;
566 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
567 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
568 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
569 convert_to_string(cpy
);
570 STR_SET(msg
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
574 case HTTP_MSG_PROPHASH_REQUEST_URL
:
575 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL
:
576 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
577 convert_to_string(cpy
);
578 STR_SET(msg
->http
.info
.request
.url
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
582 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
583 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
584 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
585 convert_to_long(cpy
);
586 msg
->http
.info
.response
.code
= Z_LVAL_P(cpy
);
590 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
591 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
592 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
593 convert_to_string(cpy
);
594 STR_SET(msg
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
600 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
607 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
)
610 getObjectEx(http_message_object
, obj
, object
);
611 http_message
*msg
= obj
->message
;
612 HashTable
*props
= OBJ_PROP(obj
);
615 INIT_ZARR(array
, props
);
617 #define ASSOC_PROP(array, ptype, name, val) \
621 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
622 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
623 efree(m_prop_name); \
625 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
626 #define ASSOC_STRINGL(array, name, val, len) \
630 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
631 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, 1); \
632 efree(m_prop_name); \
635 ASSOC_PROP(array
, long, "type", msg
->type
);
636 ASSOC_PROP(array
, double, "httpVersion", msg
->http
.version
);
639 case HTTP_MSG_REQUEST
:
640 ASSOC_PROP(array
, long, "responseCode", 0);
641 ASSOC_STRINGL(array
, "responseStatus", "", 0);
642 ASSOC_STRING(array
, "requestMethod", STR_PTR(msg
->http
.info
.request
.method
));
643 ASSOC_STRING(array
, "requestUrl", STR_PTR(msg
->http
.info
.request
.url
));
646 case HTTP_MSG_RESPONSE
:
647 ASSOC_PROP(array
, long, "responseCode", msg
->http
.info
.response
.code
);
648 ASSOC_STRING(array
, "responseStatus", STR_PTR(msg
->http
.info
.response
.status
));
649 ASSOC_STRINGL(array
, "requestMethod", "", 0);
650 ASSOC_STRINGL(array
, "requestUrl", "", 0);
655 ASSOC_PROP(array
, long, "responseCode", 0);
656 ASSOC_STRINGL(array
, "responseStatus", "", 0);
657 ASSOC_STRINGL(array
, "requestMethod", "", 0);
658 ASSOC_STRINGL(array
, "requestUrl", "", 0);
662 MAKE_STD_ZVAL(headers
);
664 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
665 ASSOC_PROP(array
, zval
, "headers", headers
);
666 ASSOC_STRINGL(array
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
668 MAKE_STD_ZVAL(parent
);
670 ZVAL_OBJVAL(parent
, obj
->parent
, 1);
674 ASSOC_PROP(array
, zval
, "parentMessage", parent
);
676 return OBJ_PROP(obj
);
679 /* ### USERLAND ### */
681 /* {{{ proto void HttpMessage::__construct([string message])
682 Create a new HttpMessage object instance. */
683 PHP_METHOD(HttpMessage
, __construct
)
686 char *message
= NULL
;
688 getObject(http_message_object
, obj
);
691 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
692 http_message
*msg
= obj
->message
;
694 http_message_dtor(msg
);
695 if ((obj
->message
= http_message_parse_ex(msg
, message
, length
))) {
696 if (obj
->message
->parent
) {
697 obj
->parent
= http_message_object_new_ex(Z_OBJCE_P(getThis()), obj
->message
->parent
, NULL
);
700 obj
->message
= http_message_init(msg
);
704 obj
->message
= http_message_new();
710 /* {{{ proto static HttpMessage HttpMessage::factory([string raw_message[, string class_name = "HttpMessage"]])
711 Create a new HttpMessage object instance. */
712 PHP_METHOD(HttpMessage
, factory
)
714 char *string
= NULL
, *cn
= NULL
;
715 int length
= 0, cl
= 0;
716 http_message
*msg
= NULL
;
717 zend_object_value ov
;
718 http_message_object
*obj
= NULL
;
723 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|ss", &string
, &length
, &cn
, &cl
)) {
725 msg
= http_message_parse(string
, length
);
727 if ((msg
|| !length
) && SUCCESS
== http_object_new(&ov
, cn
, cl
, _http_message_object_new_ex
, http_message_object_ce
, msg
, &obj
)) {
728 RETVAL_OBJVAL(ov
, 0);
730 if (obj
&& !obj
->message
) {
731 obj
->message
= http_message_new();
738 /* {{{ proto string HttpMessage::getBody()
739 Get the body of the parsed HttpMessage. */
740 PHP_METHOD(HttpMessage
, getBody
)
744 if (return_value_used
) {
745 getObject(http_message_object
, obj
);
746 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
751 /* {{{ proto void HttpMessage::setBody(string body)
752 Set the body of the HttpMessage. NOTE: Don't forget to update any headers accordingly. */
753 PHP_METHOD(HttpMessage
, setBody
)
757 getObject(http_message_object
, obj
);
759 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &body
, &len
)) {
760 phpstr_dtor(PHPSTR(obj
->message
));
761 phpstr_from_string_ex(PHPSTR(obj
->message
), body
, len
);
766 /* {{{ proto string HttpMessage::getHeader(string header)
767 Get message header. */
768 PHP_METHOD(HttpMessage
, getHeader
)
771 char *orig_header
, *nice_header
;
773 getObject(http_message_object
, obj
);
775 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &orig_header
, &header_len
)) {
779 nice_header
= pretty_key(estrndup(orig_header
, header_len
), header_len
, 1, 1);
780 if ((header
= http_message_header_ex(obj
->message
, nice_header
, header_len
+ 1, 0))) {
781 RETVAL_ZVAL(header
, 1, 1);
787 /* {{{ proto array HttpMessage::getHeaders()
788 Get Message Headers. */
789 PHP_METHOD(HttpMessage
, getHeaders
)
793 if (return_value_used
) {
794 getObject(http_message_object
, obj
);
796 array_init(return_value
);
797 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(return_value
));
802 /* {{{ proto void HttpMessage::setHeaders(array headers)
804 PHP_METHOD(HttpMessage
, setHeaders
)
806 zval
*new_headers
= NULL
;
807 getObject(http_message_object
, obj
);
809 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &new_headers
)) {
813 zend_hash_clean(&obj
->message
->hdrs
);
815 array_copy(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
);
820 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
821 Add headers. If append is true, headers with the same name will be separated, else overwritten. */
822 PHP_METHOD(HttpMessage
, addHeaders
)
825 zend_bool append
= 0;
826 getObject(http_message_object
, obj
);
828 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
832 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, append
, ARRAY_JOIN_STRONLY
|ARRAY_JOIN_PRETTIFY
);
836 /* {{{ proto int HttpMessage::getType()
837 Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE) */
838 PHP_METHOD(HttpMessage
, getType
)
842 if (return_value_used
) {
843 getObject(http_message_object
, obj
);
844 RETURN_LONG(obj
->message
->type
);
849 /* {{{ proto void HttpMessage::setType(int type)
850 Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE) */
851 PHP_METHOD(HttpMessage
, setType
)
854 getObject(http_message_object
, obj
);
856 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
859 http_message_set_type(obj
->message
, type
);
863 /* {{{ proto int HttpMessage::getResponseCode()
864 Get the Response Code of the Message. */
865 PHP_METHOD(HttpMessage
, getResponseCode
)
869 if (return_value_used
) {
870 getObject(http_message_object
, obj
);
871 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
872 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
877 /* {{{ proto bool HttpMessage::setResponseCode(int code)
878 Set the response code of an HTTP Response Message. */
879 PHP_METHOD(HttpMessage
, setResponseCode
)
882 getObject(http_message_object
, obj
);
884 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
886 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
889 if (code
< 100 || code
> 599) {
890 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid response code (100-599): %ld", code
);
894 obj
->message
->http
.info
.response
.code
= code
;
899 /* {{{ proto string HttpMessage::getResponseStatus()
900 Get the Response Status of the message (i.e. the string following the response code). */
901 PHP_METHOD(HttpMessage
, getResponseStatus
)
905 if (return_value_used
) {
906 getObject(http_message_object
, obj
);
907 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
908 if (obj
->message
->http
.info
.response
.status
) {
909 RETURN_STRING(obj
->message
->http
.info
.response
.status
, 1);
911 RETURN_EMPTY_STRING();
917 /* {{{ proto bool HttpMessage::setResponseStatus(string status)
918 Set the Response Status of the HTTP message (i.e. the string following the response code). */
919 PHP_METHOD(HttpMessage
, setResponseStatus
)
923 getObject(http_message_object
, obj
);
925 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
927 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &status
, &status_len
)) {
930 STR_SET(obj
->message
->http
.info
.response
.status
, estrndup(status
, status_len
));
935 /* {{{ proto string HttpMessage::getRequestMethod()
936 Get the Request Method of the Message. */
937 PHP_METHOD(HttpMessage
, getRequestMethod
)
941 if (return_value_used
) {
942 getObject(http_message_object
, obj
);
943 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
944 if (obj
->message
->http
.info
.request
.method
) {
945 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
947 RETURN_EMPTY_STRING();
953 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
954 Set the Request Method of the HTTP Message. */
955 PHP_METHOD(HttpMessage
, setRequestMethod
)
959 getObject(http_message_object
, obj
);
961 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
963 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
966 if (method_len
< 1) {
967 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
970 if (!http_request_method_exists(1, 0, method
)) {
971 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unknown request method: %s", method
);
975 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
980 /* {{{ proto string HttpMessage::getRequestUrl()
981 Get the Request URL of the Message. */
982 PHP_METHOD(HttpMessage
, getRequestUrl
)
986 if (return_value_used
) {
987 getObject(http_message_object
, obj
);
988 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
989 if (obj
->message
->http
.info
.request
.url
) {
990 RETURN_STRING(obj
->message
->http
.info
.request
.url
, 1);
992 RETURN_EMPTY_STRING();
998 /* {{{ proto bool HttpMessage::setRequestUrl(string url)
999 Set the Request URL of the HTTP Message. */
1000 PHP_METHOD(HttpMessage
, setRequestUrl
)
1004 getObject(http_message_object
, obj
);
1006 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
1009 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
1011 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUrl to an empty string");
1015 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(URI
, URIlen
));
1020 /* {{{ proto string HttpMessage::getHttpVersion()
1021 Get the HTTP Protocol Version of the Message. */
1022 PHP_METHOD(HttpMessage
, getHttpVersion
)
1026 if (return_value_used
) {
1028 getObject(http_message_object
, obj
);
1030 sprintf(ver
, "%1.1lf", obj
->message
->http
.version
);
1031 RETURN_STRINGL(ver
, 3, 1);
1036 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
1037 Set the HTTP Protocol version of the Message. */
1038 PHP_METHOD(HttpMessage
, setHttpVersion
)
1042 getObject(http_message_object
, obj
);
1044 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
1048 convert_to_double(zv
);
1049 sprintf(v
, "%1.1lf", Z_DVAL_P(zv
));
1050 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
1051 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
1055 obj
->message
->http
.version
= Z_DVAL_P(zv
);
1060 /* {{{ proto string HttpMessage::guessContentType(string magic_file[, int magic_mode = MAGIC_MIME])
1061 Attempts to guess the content type of supplied payload through libmagic. */
1062 PHP_METHOD(HttpMessage
, guessContentType
)
1064 #ifdef HTTP_HAVE_MAGIC
1065 char *magic_file
, *ct
= NULL
;
1067 long magic_mode
= MAGIC_MIME
;
1070 SET_EH_THROW_HTTP();
1071 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|l", &magic_file
, &magic_file_len
, &magic_mode
)) {
1072 getObject(http_message_object
, obj
);
1073 if ((ct
= http_guess_content_type(magic_file
, magic_mode
, PHPSTR_VAL(&obj
->message
->body
), PHPSTR_LEN(&obj
->message
->body
), SEND_DATA
))) {
1074 RETVAL_STRING(ct
, 0);
1079 http_error(HE_THROW
, HTTP_E_RUNTIME
, "Cannot guess Content-Type; libmagic not available");
1085 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
1086 Get parent Message. */
1087 PHP_METHOD(HttpMessage
, getParentMessage
)
1089 SET_EH_THROW_HTTP();
1091 getObject(http_message_object
, obj
);
1093 if (obj
->message
->parent
) {
1094 RETVAL_OBJVAL(obj
->parent
, 1);
1096 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "HttpMessage does not have a parent message");
1103 /* {{{ proto bool HttpMessage::send()
1104 Send the Message according to its type as Response or Request. */
1105 PHP_METHOD(HttpMessage
, send
)
1107 getObject(http_message_object
, obj
);
1111 RETURN_SUCCESS(http_message_send(obj
->message
));
1115 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
1116 Get the string representation of the Message. */
1117 PHP_METHOD(HttpMessage
, toString
)
1119 if (return_value_used
) {
1122 zend_bool include_parent
= 0;
1123 getObject(http_message_object
, obj
);
1125 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1129 if (include_parent
) {
1130 http_message_serialize(obj
->message
, &string
, &length
);
1132 http_message_tostring(obj
->message
, &string
, &length
);
1134 RETURN_STRINGL(string
, length
, 0);
1139 /* {{{ proto HttpRequest|HttpResponse HttpMessage::toMessageTypeObject(void)
1140 Creates an object regarding to the type of the message. Returns either an HttpRequest or HttpResponse object on success, or NULL on failure. */
1141 PHP_METHOD(HttpMessage
, toMessageTypeObject
)
1143 SET_EH_THROW_HTTP();
1147 if (return_value_used
) {
1148 getObject(http_message_object
, obj
);
1150 switch (obj
->message
->type
) {
1151 case HTTP_MSG_REQUEST
:
1153 #ifdef HTTP_HAVE_CURL
1156 zval body
, *array
, *headers
, *host
= http_message_header(obj
->message
, "Host");
1157 php_url hurl
, *purl
= php_url_parse(obj
->message
->http
.info
.request
.url
);
1159 MAKE_STD_ZVAL(array
);
1162 memset(&hurl
, 0, sizeof(php_url
));
1163 hurl
.host
= host
? Z_STRVAL_P(host
) : NULL
;
1164 zval_ptr_dtor(&host
);
1165 http_build_url(HTTP_URL_REPLACE
, purl
, &hurl
, NULL
, &url
, NULL
);
1167 add_assoc_string(array
, "url", url
, 0);
1169 if ( (method
= http_request_method_exists(1, 0, obj
->message
->http
.info
.request
.method
)) ||
1170 (method
= http_request_method_register(obj
->message
->http
.info
.request
.method
, strlen(obj
->message
->http
.info
.request
.method
)))) {
1171 add_assoc_long(array
, "method", method
);
1174 if (10 == (int) (obj
->message
->http
.version
* 10)) {
1175 add_assoc_long(array
, "protocol", CURL_HTTP_VERSION_1_0
);
1178 MAKE_STD_ZVAL(headers
);
1179 array_init(headers
);
1180 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(headers
));
1181 add_assoc_zval(array
, "headers", headers
);
1183 object_init_ex(return_value
, http_request_object_ce
);
1184 zend_call_method_with_1_params(&return_value
, http_request_object_ce
, NULL
, "setoptions", NULL
, array
);
1185 zval_ptr_dtor(&array
);
1188 ZVAL_STRINGL(&body
, PHPSTR_VAL(obj
->message
), PHPSTR_LEN(obj
->message
), 0);
1189 zend_call_method_with_1_params(&return_value
, http_request_object_ce
, NULL
, "setrawpostdata", NULL
, &body
);
1191 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot transform HttpMessage to HttpRequest (missing curl support)");
1196 case HTTP_MSG_RESPONSE
:
1199 HashPosition pos1
, pos2
;
1200 HashKey key
= initHashKey(0);
1201 zval
**header
, **h
, *body
;
1203 if (obj
->message
->http
.info
.response
.code
) {
1204 http_send_status(obj
->message
->http
.info
.response
.code
);
1207 object_init_ex(return_value
, http_response_object_ce
);
1209 FOREACH_HASH_KEYVAL(pos1
, &obj
->message
->hdrs
, key
, header
) {
1210 if (key
.type
== HASH_KEY_IS_STRING
) {
1213 MAKE_STD_ZVAL(zkey
);
1214 ZVAL_STRINGL(zkey
, key
.str
, key
.len
- 1, 1);
1216 switch (Z_TYPE_PP(header
)) {
1219 FOREACH_HASH_VAL(pos2
, HASH_OF(*header
), h
) {
1221 zend_call_method_with_2_params(&return_value
, http_response_object_ce
, NULL
, "setheader", NULL
, zkey
, *h
);
1227 ZVAL_ADDREF(*header
);
1228 zend_call_method_with_2_params(&return_value
, http_response_object_ce
, NULL
, "setheader", NULL
, zkey
, *header
);
1229 zval_ptr_dtor(header
);
1232 zval_ptr_dtor(&zkey
);
1236 MAKE_STD_ZVAL(body
);
1237 ZVAL_STRINGL(body
, PHPSTR_VAL(obj
->message
), PHPSTR_LEN(obj
->message
), 1);
1238 zend_call_method_with_1_params(&return_value
, http_response_object_ce
, NULL
, "setdata", NULL
, body
);
1239 zval_ptr_dtor(&body
);
1241 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot transform HttpMessage to HttpResponse (need PHP 5.1+)");
1247 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is neither of type HttpMessage::TYPE_REQUEST nor HttpMessage::TYPE_RESPONSE");
1255 /* {{{ proto int HttpMessage::count()
1256 Implements Countable::count(). Returns the number of parent messages + 1. */
1257 PHP_METHOD(HttpMessage
, count
)
1261 getObject(http_message_object
, obj
);
1263 http_message_count(i
, obj
->message
);
1269 /* {{{ proto string HttpMessage::serialize()
1270 Implements Serializable::serialize(). Returns the serialized representation of the HttpMessage. */
1271 PHP_METHOD(HttpMessage
, serialize
)
1276 getObject(http_message_object
, obj
);
1278 http_message_serialize(obj
->message
, &string
, &length
);
1279 RETURN_STRINGL(string
, length
, 0);
1284 /* {{{ proto void HttpMessage::unserialize(string serialized)
1285 Implements Serializable::unserialize(). Re-constructs the HttpMessage based upon the serialized string. */
1286 PHP_METHOD(HttpMessage
, unserialize
)
1290 getObject(http_message_object
, obj
);
1292 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &serialized
, &length
)) {
1295 http_message_dtor(obj
->message
);
1296 if ((msg
= http_message_parse_ex(obj
->message
, serialized
, (size_t) length
))) {
1299 http_error(HE_ERROR
, HTTP_E_RUNTIME
, "Could not unserialize HttpMessage");
1300 http_message_init(obj
->message
);
1306 /* {{{ proto HttpMessage HttpMessage::detach(void)
1307 Returns a clone of an HttpMessage object detached from any parent messages. */
1308 PHP_METHOD(HttpMessage
, detach
)
1312 getObject(http_message_object
, obj
);
1316 info
.type
= obj
->message
->type
;
1317 memcpy(&HTTP_INFO(&info
), &HTTP_INFO(obj
->message
), sizeof(struct http_info
));
1319 msg
= http_message_new();
1320 http_message_set_info(msg
, &info
);
1322 zend_hash_copy(&msg
->hdrs
, &obj
->message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1323 phpstr_append(&msg
->body
, PHPSTR_VAL(obj
->message
), PHPSTR_LEN(obj
->message
));
1325 RETVAL_OBJVAL(http_message_object_new_ex(Z_OBJCE_P(getThis()), msg
, NULL
), 0);
1329 /* {{{ proto void HttpMessage::prepend(HttpMessage message[, bool top = true])
1330 Prepends message(s) to the HTTP message. Throws HttpInvalidParamException if the message is located within the same message chain. */
1331 PHP_METHOD(HttpMessage
, prepend
)
1336 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O|b", &prepend
, http_message_object_ce
, &top
)) {
1337 http_message
*msg
[2];
1338 getObject(http_message_object
, obj
);
1339 getObjectEx(http_message_object
, prepend_obj
, prepend
);
1342 for (msg
[0] = obj
->message
; msg
[0]; msg
[0] = msg
[0]->parent
) {
1343 for (msg
[1] = prepend_obj
->message
; msg
[1]; msg
[1] = msg
[1]->parent
) {
1344 if (msg
[0] == msg
[1]) {
1345 http_error(HE_THROW
, HTTP_E_INVALID_PARAM
, "Cannot prepend a message located within the same message chain");
1351 http_message_object_prepend_ex(getThis(), prepend
, top
);
1356 /* {{{ proto HttpMessage HttpMessage::reverse()
1357 Reorders the message chain in reverse order. Returns the most parent HttpMessage object. */
1358 PHP_METHOD(HttpMessage
, reverse
)
1361 http_message_object_reverse(getThis(), return_value
);
1366 /* {{{ proto void HttpMessage::rewind(void)
1367 Implements Iterator::rewind(). */
1368 PHP_METHOD(HttpMessage
, rewind
)
1371 getObject(http_message_object
, obj
);
1373 if (obj
->iterator
) {
1374 zval_ptr_dtor(&obj
->iterator
);
1376 ZVAL_ADDREF(getThis());
1377 obj
->iterator
= getThis();
1382 /* {{{ proto bool HttpMessage::valid(void)
1383 Implements Iterator::valid(). */
1384 PHP_METHOD(HttpMessage
, valid
)
1387 getObject(http_message_object
, obj
);
1389 RETURN_BOOL(obj
->iterator
!= NULL
);
1394 /* {{{ proto void HttpMessage::next(void)
1395 Implements Iterator::next(). */
1396 PHP_METHOD(HttpMessage
, next
)
1399 getObject(http_message_object
, obj
);
1400 getObjectEx(http_message_object
, itr
, obj
->iterator
);
1402 if (itr
&& itr
->parent
.handle
) {
1403 zval
*old
= obj
->iterator
;
1404 MAKE_STD_ZVAL(obj
->iterator
);
1405 ZVAL_OBJVAL(obj
->iterator
, itr
->parent
, 1);
1406 zval_ptr_dtor(&old
);
1408 zval_ptr_dtor(&obj
->iterator
);
1409 obj
->iterator
= NULL
;
1415 /* {{{ proto int HttpMessage::key(void)
1416 Implements Iterator::key(). */
1417 PHP_METHOD(HttpMessage
, key
)
1420 getObject(http_message_object
, obj
);
1422 RETURN_LONG(obj
->iterator
? obj
->iterator
->value
.obj
.handle
:0);
1427 /* {{{ proto HttpMessage HttpMessage::current(void)
1428 Implements Iterator::current(). */
1429 PHP_METHOD(HttpMessage
, current
)
1432 getObject(http_message_object
, obj
);
1434 if (obj
->iterator
) {
1435 RETURN_ZVAL(obj
->iterator
, 1, 0);
1441 #endif /* ZEND_ENGINE_2 */
1448 * vim600: noet sw=4 ts=4 fdm=marker
1449 * vim<600: noet sw=4 ts=4