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"
35 #include "php_http_headers_api.h"
37 #if defined(HTTP_HAVE_SPL) && !defined(WONKY)
38 /* SPL doesn't install its headers */
39 extern PHPAPI zend_class_entry
*spl_ce_Countable
;
42 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpMessage, method, 0, req_args)
43 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpMessage, method, 0)
44 #define HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, HTTP_ARGS(HttpMessage, method), visibility)
46 HTTP_BEGIN_ARGS(__construct
, 0)
47 HTTP_ARG_VAL(message
, 0)
50 HTTP_BEGIN_ARGS(factory
, 0)
51 HTTP_ARG_VAL(message
, 0)
52 HTTP_ARG_VAL(class_name
, 0)
55 HTTP_BEGIN_ARGS(fromEnv
, 1)
57 HTTP_ARG_VAL(class_name
, 0)
60 HTTP_EMPTY_ARGS(getBody
);
61 HTTP_BEGIN_ARGS(setBody
, 1)
65 HTTP_BEGIN_ARGS(getHeader
, 1)
66 HTTP_ARG_VAL(header
, 0)
69 HTTP_EMPTY_ARGS(getHeaders
);
70 HTTP_BEGIN_ARGS(setHeaders
, 1)
71 HTTP_ARG_VAL(headers
, 0)
74 HTTP_BEGIN_ARGS(addHeaders
, 1)
75 HTTP_ARG_VAL(headers
, 0)
76 HTTP_ARG_VAL(append
, 0)
79 HTTP_EMPTY_ARGS(getType
);
80 HTTP_BEGIN_ARGS(setType
, 1)
84 HTTP_EMPTY_ARGS(getResponseCode
);
85 HTTP_BEGIN_ARGS(setResponseCode
, 1)
86 HTTP_ARG_VAL(response_code
, 0)
89 HTTP_EMPTY_ARGS(getResponseStatus
);
90 HTTP_BEGIN_ARGS(setResponseStatus
, 1)
91 HTTP_ARG_VAL(response_status
, 0)
94 HTTP_EMPTY_ARGS(getRequestMethod
);
95 HTTP_BEGIN_ARGS(setRequestMethod
, 1)
96 HTTP_ARG_VAL(request_method
, 0)
99 HTTP_EMPTY_ARGS(getRequestUrl
);
100 HTTP_BEGIN_ARGS(setRequestUrl
, 1)
104 HTTP_EMPTY_ARGS(getHttpVersion
);
105 HTTP_BEGIN_ARGS(setHttpVersion
, 1)
106 HTTP_ARG_VAL(http_version
, 0)
109 HTTP_BEGIN_ARGS(guessContentType
, 1)
110 HTTP_ARG_VAL(magic_file
, 0)
111 HTTP_ARG_VAL(magic_mode
, 0)
114 HTTP_EMPTY_ARGS(getParentMessage
);
115 HTTP_EMPTY_ARGS(send
);
116 HTTP_BEGIN_ARGS(toString
, 0)
117 HTTP_ARG_VAL(include_parent
, 0)
120 HTTP_EMPTY_ARGS(toMessageTypeObject
);
122 HTTP_EMPTY_ARGS(count
);
124 HTTP_EMPTY_ARGS(serialize
);
125 HTTP_BEGIN_ARGS(unserialize
, 1)
126 HTTP_ARG_VAL(serialized
, 0)
129 HTTP_EMPTY_ARGS(rewind
);
130 HTTP_EMPTY_ARGS(valid
);
131 HTTP_EMPTY_ARGS(key
);
132 HTTP_EMPTY_ARGS(current
);
133 HTTP_EMPTY_ARGS(next
);
135 HTTP_EMPTY_ARGS(detach
);
136 HTTP_BEGIN_ARGS(prepend
, 1)
137 HTTP_ARG_OBJ(HttpMessage
, message
, 0)
139 HTTP_EMPTY_ARGS(reverse
);
141 #define http_message_object_read_prop _http_message_object_read_prop
142 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
);
143 #define http_message_object_write_prop _http_message_object_write_prop
144 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
);
145 #define http_message_object_get_props _http_message_object_get_props
146 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
);
148 #define OBJ_PROP_CE http_message_object_ce
149 zend_class_entry
*http_message_object_ce
;
150 zend_function_entry http_message_object_fe
[] = {
151 HTTP_MESSAGE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
152 HTTP_MESSAGE_ME(getBody
, ZEND_ACC_PUBLIC
)
153 HTTP_MESSAGE_ME(setBody
, ZEND_ACC_PUBLIC
)
154 HTTP_MESSAGE_ME(getHeader
, ZEND_ACC_PUBLIC
)
155 HTTP_MESSAGE_ME(getHeaders
, ZEND_ACC_PUBLIC
)
156 HTTP_MESSAGE_ME(setHeaders
, ZEND_ACC_PUBLIC
)
157 HTTP_MESSAGE_ME(addHeaders
, ZEND_ACC_PUBLIC
)
158 HTTP_MESSAGE_ME(getType
, ZEND_ACC_PUBLIC
)
159 HTTP_MESSAGE_ME(setType
, ZEND_ACC_PUBLIC
)
160 HTTP_MESSAGE_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
161 HTTP_MESSAGE_ME(setResponseCode
, ZEND_ACC_PUBLIC
)
162 HTTP_MESSAGE_ME(getResponseStatus
, ZEND_ACC_PUBLIC
)
163 HTTP_MESSAGE_ME(setResponseStatus
, ZEND_ACC_PUBLIC
)
164 HTTP_MESSAGE_ME(getRequestMethod
, ZEND_ACC_PUBLIC
)
165 HTTP_MESSAGE_ME(setRequestMethod
, ZEND_ACC_PUBLIC
)
166 HTTP_MESSAGE_ME(getRequestUrl
, ZEND_ACC_PUBLIC
)
167 HTTP_MESSAGE_ME(setRequestUrl
, ZEND_ACC_PUBLIC
)
168 HTTP_MESSAGE_ME(getHttpVersion
, ZEND_ACC_PUBLIC
)
169 HTTP_MESSAGE_ME(setHttpVersion
, ZEND_ACC_PUBLIC
)
170 HTTP_MESSAGE_ME(guessContentType
, ZEND_ACC_PUBLIC
)
171 HTTP_MESSAGE_ME(getParentMessage
, ZEND_ACC_PUBLIC
)
172 HTTP_MESSAGE_ME(send
, ZEND_ACC_PUBLIC
)
173 HTTP_MESSAGE_ME(toString
, ZEND_ACC_PUBLIC
)
174 HTTP_MESSAGE_ME(toMessageTypeObject
, ZEND_ACC_PUBLIC
)
176 /* implements Countable */
177 HTTP_MESSAGE_ME(count
, ZEND_ACC_PUBLIC
)
179 /* implements Serializable */
180 HTTP_MESSAGE_ME(serialize
, ZEND_ACC_PUBLIC
)
181 HTTP_MESSAGE_ME(unserialize
, ZEND_ACC_PUBLIC
)
183 /* implements Iterator */
184 HTTP_MESSAGE_ME(rewind
, ZEND_ACC_PUBLIC
)
185 HTTP_MESSAGE_ME(valid
, ZEND_ACC_PUBLIC
)
186 HTTP_MESSAGE_ME(current
, ZEND_ACC_PUBLIC
)
187 HTTP_MESSAGE_ME(key
, ZEND_ACC_PUBLIC
)
188 HTTP_MESSAGE_ME(next
, ZEND_ACC_PUBLIC
)
190 ZEND_MALIAS(HttpMessage
, __toString
, toString
, HTTP_ARGS(HttpMessage
, toString
), ZEND_ACC_PUBLIC
)
192 HTTP_MESSAGE_ME(factory
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
193 ZEND_MALIAS(HttpMessage
, fromString
, factory
, HTTP_ARGS(HttpMessage
, factory
), ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
194 HTTP_MESSAGE_ME(fromEnv
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
196 HTTP_MESSAGE_ME(detach
, ZEND_ACC_PUBLIC
)
197 HTTP_MESSAGE_ME(prepend
, ZEND_ACC_PUBLIC
)
198 HTTP_MESSAGE_ME(reverse
, ZEND_ACC_PUBLIC
)
202 static zend_object_handlers http_message_object_handlers
;
204 PHP_MINIT_FUNCTION(http_message_object
)
206 HTTP_REGISTER_CLASS_EX(HttpMessage
, http_message_object
, NULL
, 0);
209 # ifdef HTTP_HAVE_SPL
210 zend_class_implements(http_message_object_ce TSRMLS_CC
, 3, spl_ce_Countable
, zend_ce_serializable
, zend_ce_iterator
);
212 zend_class_implements(http_message_object_ce TSRMLS_CC
, 2, zend_ce_serializable
, zend_ce_iterator
);
215 zend_class_implements(http_message_object_ce TSRMLS_CC
, 1, zend_ce_iterator
);
218 http_message_object_handlers
.clone_obj
= _http_message_object_clone_obj
;
219 http_message_object_handlers
.read_property
= http_message_object_read_prop
;
220 http_message_object_handlers
.write_property
= http_message_object_write_prop
;
221 http_message_object_handlers
.get_properties
= http_message_object_get_props
;
222 http_message_object_handlers
.get_property_ptr_ptr
= NULL
;
224 DCL_PROP(PROTECTED
, long, type
, HTTP_MSG_NONE
);
225 DCL_PROP(PROTECTED
, string
, body
, "");
226 DCL_PROP(PROTECTED
, string
, requestMethod
, "");
227 DCL_PROP(PROTECTED
, string
, requestUrl
, "");
228 DCL_PROP(PROTECTED
, string
, responseStatus
, "");
229 DCL_PROP(PROTECTED
, long, responseCode
, 0);
230 DCL_PROP_N(PROTECTED
, httpVersion
);
231 DCL_PROP_N(PROTECTED
, headers
);
232 DCL_PROP_N(PROTECTED
, parentMessage
);
235 DCL_CONST(long, "TYPE_NONE", HTTP_MSG_NONE
);
236 DCL_CONST(long, "TYPE_REQUEST", HTTP_MSG_REQUEST
);
237 DCL_CONST(long, "TYPE_RESPONSE", HTTP_MSG_RESPONSE
);
240 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE
);
241 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST
);
242 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE
);
247 void _http_message_object_reverse(zval
*this_ptr
, zval
*return_value TSRMLS_DC
)
250 getObject(http_message_object
, obj
);
253 http_message_count(i
, obj
->message
);
257 zend_object_value
*ovalues
= NULL
;
258 http_message_object
**objects
= NULL
;
261 objects
= ecalloc(i
, sizeof(http_message_object
*));
262 ovalues
= ecalloc(i
, sizeof(zend_object_value
));
264 /* we are the first message */
266 ovalues
[0] = getThis()->value
.obj
;
271 for (i
= 1; obj
->parent
.handle
; ++i
) {
272 o
.value
.obj
= obj
->parent
;
273 ovalues
[i
] = o
.value
.obj
;
274 objects
[i
] = obj
= zend_object_store_get_object(&o TSRMLS_CC
);
277 /* reorder parents */
278 for (last
= --i
; i
; --i
) {
279 objects
[i
]->message
->parent
= objects
[i
-1]->message
;
280 objects
[i
]->parent
= ovalues
[i
-1];
282 objects
[0]->message
->parent
= NULL
;
283 objects
[0]->parent
.handle
= 0;
284 objects
[0]->parent
.handlers
= NULL
;
287 Z_OBJ_ADDREF_P(getThis());
288 RETVAL_OBJVAL(ovalues
[last
], 1);
293 RETURN_ZVAL(getThis(), 1, 0);
297 void _http_message_object_prepend_ex(zval
*this_ptr
, zval
*prepend
, zend_bool top TSRMLS_DC
)
300 http_message
*save_parent_msg
= NULL
;
301 zend_object_value save_parent_obj
= {0, NULL
};
302 getObject(http_message_object
, obj
);
303 getObjectEx(http_message_object
, prepend_obj
, prepend
);
309 save_parent_obj
= obj
->parent
;
310 save_parent_msg
= obj
->message
->parent
;
312 /* iterate to the most parent object */
313 while (obj
->parent
.handle
) {
314 m
.value
.obj
= obj
->parent
;
315 obj
= zend_object_store_get_object(&m TSRMLS_CC
);
320 obj
->parent
= prepend
->value
.obj
;
321 obj
->message
->parent
= prepend_obj
->message
;
324 zend_objects_store_add_ref(prepend TSRMLS_CC
);
325 while (prepend_obj
->parent
.handle
) {
326 m
.value
.obj
= prepend_obj
->parent
;
327 zend_objects_store_add_ref(&m TSRMLS_CC
);
328 prepend_obj
= zend_object_store_get_object(&m TSRMLS_CC
);
332 prepend_obj
->parent
= save_parent_obj
;
333 prepend_obj
->message
->parent
= save_parent_msg
;
337 zend_object_value
_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
339 return http_message_object_new_ex(ce
, NULL
, NULL
);
342 zend_object_value
_http_message_object_new_ex(zend_class_entry
*ce
, http_message
*msg
, http_message_object
**ptr TSRMLS_DC
)
344 zend_object_value ov
;
345 http_message_object
*o
;
347 o
= ecalloc(1, sizeof(http_message_object
));
357 o
->parent
= http_message_object_new_ex(ce
, msg
->parent
, NULL
);
361 ALLOC_HASHTABLE(OBJ_PROP(o
));
362 zend_hash_init(OBJ_PROP(o
), zend_hash_num_elements(&ce
->default_properties
), NULL
, ZVAL_PTR_DTOR
, 0);
363 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
365 ov
.handle
= putObject(http_message_object
, o
);
366 ov
.handlers
= &http_message_object_handlers
;
371 zend_object_value
_http_message_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
373 zend_object_value new_ov
;
374 http_message_object
*new_obj
= NULL
;
375 getObject(http_message_object
, old_obj
);
377 new_ov
= http_message_object_new_ex(old_obj
->zo
.ce
, http_message_dup(old_obj
->message
), &new_obj
);
378 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
383 void _http_message_object_free(zend_object
*object TSRMLS_DC
)
385 http_message_object
*o
= (http_message_object
*) object
;
388 http_message_dtor(o
->message
);
391 if (o
->parent
.handle
) {
396 p
.value
.obj
= o
->parent
;
397 zend_objects_store_del_ref(&p TSRMLS_CC
);
402 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
404 getObjectEx(http_message_object
, obj
, object
);
405 http_message
*msg
= obj
->message
;
408 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1);
410 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
412 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
413 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
417 if (type
== BP_VAR_W
) {
418 zend_error(E_ERROR
, "Cannot access HttpMessage properties by reference or array key/index");
422 ALLOC_ZVAL(return_value
);
423 return_value
->refcount
= 0;
424 return_value
->is_ref
= 0;
432 case HTTP_MSG_PROPHASH_TYPE
:
433 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
434 RETVAL_LONG(msg
->type
);
437 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
438 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
439 RETVAL_DOUBLE(msg
->http
.version
);
442 case HTTP_MSG_PROPHASH_BODY
:
443 case HTTP_MSG_CHILD_PROPHASH_BODY
:
444 phpstr_fix(PHPSTR(msg
));
445 RETVAL_PHPSTR(PHPSTR(msg
), 0, 1);
448 case HTTP_MSG_PROPHASH_HEADERS
:
449 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
450 array_init(return_value
);
451 zend_hash_copy(Z_ARRVAL_P(return_value
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
454 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
455 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
457 RETVAL_OBJVAL(obj
->parent
, 1);
463 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
464 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
465 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.method
) {
466 RETVAL_STRING(msg
->http
.info
.request
.method
, 1);
472 case HTTP_MSG_PROPHASH_REQUEST_URL
:
473 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL
:
474 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.url
) {
475 RETVAL_STRING(msg
->http
.info
.request
.url
, 1);
481 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
482 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
483 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
484 RETVAL_LONG(msg
->http
.info
.response
.code
);
490 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
491 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
492 if (HTTP_MSG_TYPE(RESPONSE
, msg
) && msg
->http
.info
.response
.status
) {
493 RETVAL_STRING(msg
->http
.info
.response
.status
, 1);
501 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
510 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
)
512 getObjectEx(http_message_object
, obj
, object
);
513 http_message
*msg
= obj
->message
;
516 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
) + 1);
518 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
520 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
521 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
526 cpy
= zval_copy(Z_TYPE_P(value
), value
);
534 case HTTP_MSG_PROPHASH_TYPE
:
535 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
536 convert_to_long(cpy
);
537 http_message_set_type(msg
, Z_LVAL_P(cpy
));
540 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
541 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
542 convert_to_double(cpy
);
543 msg
->http
.version
= Z_DVAL_P(cpy
);
546 case HTTP_MSG_PROPHASH_BODY
:
547 case HTTP_MSG_CHILD_PROPHASH_BODY
:
548 convert_to_string(cpy
);
549 phpstr_dtor(PHPSTR(msg
));
550 phpstr_from_string_ex(PHPSTR(msg
), Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
));
553 case HTTP_MSG_PROPHASH_HEADERS
:
554 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
555 convert_to_array(cpy
);
556 zend_hash_clean(&msg
->hdrs
);
557 zend_hash_copy(&msg
->hdrs
, Z_ARRVAL_P(cpy
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
560 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
561 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
562 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), http_message_object_ce TSRMLS_CC
)) {
565 tmp
.value
.obj
= obj
->parent
;
568 Z_OBJ_ADDREF_P(value
);
569 obj
->parent
= value
->value
.obj
;
573 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
574 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
575 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
576 convert_to_string(cpy
);
577 STR_SET(msg
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
581 case HTTP_MSG_PROPHASH_REQUEST_URL
:
582 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL
:
583 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
584 convert_to_string(cpy
);
585 STR_SET(msg
->http
.info
.request
.url
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
589 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
590 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
591 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
592 convert_to_long(cpy
);
593 msg
->http
.info
.response
.code
= Z_LVAL_P(cpy
);
597 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
598 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
599 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
600 convert_to_string(cpy
);
601 STR_SET(msg
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
607 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
614 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
)
617 getObjectEx(http_message_object
, obj
, object
);
618 http_message
*msg
= obj
->message
;
619 HashTable
*props
= OBJ_PROP(obj
);
622 INIT_ZARR(array
, props
);
624 #define ASSOC_PROP(array, ptype, name, val) \
628 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
629 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
630 efree(m_prop_name); \
632 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
633 #define ASSOC_STRINGL(array, name, val, len) \
637 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
638 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, 1); \
639 efree(m_prop_name); \
642 ASSOC_PROP(array
, long, "type", msg
->type
);
643 ASSOC_PROP(array
, double, "httpVersion", msg
->http
.version
);
646 case HTTP_MSG_REQUEST
:
647 ASSOC_PROP(array
, long, "responseCode", 0);
648 ASSOC_STRINGL(array
, "responseStatus", "", 0);
649 ASSOC_STRING(array
, "requestMethod", STR_PTR(msg
->http
.info
.request
.method
));
650 ASSOC_STRING(array
, "requestUrl", STR_PTR(msg
->http
.info
.request
.url
));
653 case HTTP_MSG_RESPONSE
:
654 ASSOC_PROP(array
, long, "responseCode", msg
->http
.info
.response
.code
);
655 ASSOC_STRING(array
, "responseStatus", STR_PTR(msg
->http
.info
.response
.status
));
656 ASSOC_STRINGL(array
, "requestMethod", "", 0);
657 ASSOC_STRINGL(array
, "requestUrl", "", 0);
662 ASSOC_PROP(array
, long, "responseCode", 0);
663 ASSOC_STRINGL(array
, "responseStatus", "", 0);
664 ASSOC_STRINGL(array
, "requestMethod", "", 0);
665 ASSOC_STRINGL(array
, "requestUrl", "", 0);
669 MAKE_STD_ZVAL(headers
);
671 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
672 ASSOC_PROP(array
, zval
, "headers", headers
);
673 ASSOC_STRINGL(array
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
675 MAKE_STD_ZVAL(parent
);
677 ZVAL_OBJVAL(parent
, obj
->parent
, 1);
681 ASSOC_PROP(array
, zval
, "parentMessage", parent
);
683 return OBJ_PROP(obj
);
686 /* ### USERLAND ### */
688 /* {{{ proto void HttpMessage::__construct([string message])
689 Create a new HttpMessage object instance. */
690 PHP_METHOD(HttpMessage
, __construct
)
693 char *message
= NULL
;
695 getObject(http_message_object
, obj
);
698 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
699 http_message
*msg
= obj
->message
;
701 http_message_dtor(msg
);
702 if ((obj
->message
= http_message_parse_ex(msg
, message
, length
))) {
703 if (obj
->message
->parent
) {
704 obj
->parent
= http_message_object_new_ex(Z_OBJCE_P(getThis()), obj
->message
->parent
, NULL
);
707 obj
->message
= http_message_init(msg
);
711 obj
->message
= http_message_new();
717 /* {{{ proto static HttpMessage HttpMessage::factory([string raw_message[, string class_name = "HttpMessage"]])
718 Create a new HttpMessage object instance. */
719 PHP_METHOD(HttpMessage
, factory
)
721 char *string
= NULL
, *cn
= NULL
;
722 int length
= 0, cl
= 0;
723 http_message
*msg
= NULL
;
724 zend_object_value ov
;
725 http_message_object
*obj
= NULL
;
730 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|ss", &string
, &length
, &cn
, &cl
)) {
732 msg
= http_message_parse(string
, length
);
734 if ((msg
|| !length
) && SUCCESS
== http_object_new(&ov
, cn
, cl
, _http_message_object_new_ex
, http_message_object_ce
, msg
, &obj
)) {
735 RETVAL_OBJVAL(ov
, 0);
737 if (obj
&& !obj
->message
) {
738 obj
->message
= http_message_new();
745 /* {{{ proto static HttpMessage HttpMessage::fromEnv(int type[, string class_name = "HttpMessage"])
746 Create a new HttpMessage object from environment representing either current request or response */
747 PHP_METHOD(HttpMessage
, fromEnv
)
752 http_message_object
*obj
= NULL
;
753 zend_object_value ov
;
757 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l|s", &type
, &cn
, &cl
)) {
758 if (SUCCESS
== http_object_new(&ov
, cn
, cl
, _http_message_object_new_ex
, http_message_object_ce
, http_message_init_env(NULL
, type
), &obj
)) {
759 RETVAL_OBJVAL(ov
, 0);
761 if (obj
&& !obj
->message
) {
762 obj
->message
= http_message_new();
769 /* {{{ proto string HttpMessage::getBody()
770 Get the body of the parsed HttpMessage. */
771 PHP_METHOD(HttpMessage
, getBody
)
775 if (return_value_used
) {
776 getObject(http_message_object
, obj
);
777 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
782 /* {{{ proto void HttpMessage::setBody(string body)
783 Set the body of the HttpMessage. NOTE: Don't forget to update any headers accordingly. */
784 PHP_METHOD(HttpMessage
, setBody
)
788 getObject(http_message_object
, obj
);
790 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &body
, &len
)) {
791 phpstr_dtor(PHPSTR(obj
->message
));
792 phpstr_from_string_ex(PHPSTR(obj
->message
), body
, len
);
797 /* {{{ proto string HttpMessage::getHeader(string header)
798 Get message header. */
799 PHP_METHOD(HttpMessage
, getHeader
)
802 char *orig_header
, *nice_header
;
804 getObject(http_message_object
, obj
);
806 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &orig_header
, &header_len
)) {
810 nice_header
= pretty_key(estrndup(orig_header
, header_len
), header_len
, 1, 1);
811 if ((header
= http_message_header_ex(obj
->message
, nice_header
, header_len
+ 1, 0))) {
812 RETVAL_ZVAL(header
, 1, 1);
818 /* {{{ proto array HttpMessage::getHeaders()
819 Get Message Headers. */
820 PHP_METHOD(HttpMessage
, getHeaders
)
824 if (return_value_used
) {
825 getObject(http_message_object
, obj
);
827 array_init(return_value
);
828 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(return_value
));
833 /* {{{ proto void HttpMessage::setHeaders(array headers)
835 PHP_METHOD(HttpMessage
, setHeaders
)
837 zval
*new_headers
= NULL
;
838 getObject(http_message_object
, obj
);
840 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &new_headers
)) {
844 zend_hash_clean(&obj
->message
->hdrs
);
846 array_copy(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
);
851 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
852 Add headers. If append is true, headers with the same name will be separated, else overwritten. */
853 PHP_METHOD(HttpMessage
, addHeaders
)
856 zend_bool append
= 0;
857 getObject(http_message_object
, obj
);
859 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
863 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, append
, ARRAY_JOIN_STRONLY
|ARRAY_JOIN_PRETTIFY
);
867 /* {{{ proto int HttpMessage::getType()
868 Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE) */
869 PHP_METHOD(HttpMessage
, getType
)
873 if (return_value_used
) {
874 getObject(http_message_object
, obj
);
875 RETURN_LONG(obj
->message
->type
);
880 /* {{{ proto void HttpMessage::setType(int type)
881 Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE) */
882 PHP_METHOD(HttpMessage
, setType
)
885 getObject(http_message_object
, obj
);
887 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
890 http_message_set_type(obj
->message
, type
);
894 /* {{{ proto int HttpMessage::getResponseCode()
895 Get the Response Code of the Message. */
896 PHP_METHOD(HttpMessage
, getResponseCode
)
900 if (return_value_used
) {
901 getObject(http_message_object
, obj
);
902 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
903 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
908 /* {{{ proto bool HttpMessage::setResponseCode(int code)
909 Set the response code of an HTTP Response Message. */
910 PHP_METHOD(HttpMessage
, setResponseCode
)
913 getObject(http_message_object
, obj
);
915 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
917 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
920 if (code
< 100 || code
> 599) {
921 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid response code (100-599): %ld", code
);
925 obj
->message
->http
.info
.response
.code
= code
;
930 /* {{{ proto string HttpMessage::getResponseStatus()
931 Get the Response Status of the message (i.e. the string following the response code). */
932 PHP_METHOD(HttpMessage
, getResponseStatus
)
936 if (return_value_used
) {
937 getObject(http_message_object
, obj
);
938 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
939 RETURN_STRING(obj
->message
->http
.info
.response
.status
, 1);
944 /* {{{ proto bool HttpMessage::setResponseStatus(string status)
945 Set the Response Status of the HTTP message (i.e. the string following the response code). */
946 PHP_METHOD(HttpMessage
, setResponseStatus
)
950 getObject(http_message_object
, obj
);
952 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
954 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &status
, &status_len
)) {
957 STR_SET(obj
->message
->http
.info
.response
.status
, estrdup(status
));
962 /* {{{ proto string HttpMessage::getRequestMethod()
963 Get the Request Method of the Message. */
964 PHP_METHOD(HttpMessage
, getRequestMethod
)
968 if (return_value_used
) {
969 getObject(http_message_object
, obj
);
970 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
971 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
976 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
977 Set the Request Method of the HTTP Message. */
978 PHP_METHOD(HttpMessage
, setRequestMethod
)
982 getObject(http_message_object
, obj
);
984 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
986 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
989 if (method_len
< 1) {
990 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
993 if (!http_request_method_exists(1, 0, method
)) {
994 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unknown request method: %s", method
);
998 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
1003 /* {{{ proto string HttpMessage::getRequestUrl()
1004 Get the Request URL of the Message. */
1005 PHP_METHOD(HttpMessage
, getRequestUrl
)
1009 if (return_value_used
) {
1010 getObject(http_message_object
, obj
);
1011 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
1012 RETURN_STRING(obj
->message
->http
.info
.request
.url
, 1);
1017 /* {{{ proto bool HttpMessage::setRequestUrl(string url)
1018 Set the Request URL of the HTTP Message. */
1019 PHP_METHOD(HttpMessage
, setRequestUrl
)
1023 getObject(http_message_object
, obj
);
1025 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
1028 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
1030 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUrl to an empty string");
1034 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(URI
, URIlen
));
1039 /* {{{ proto string HttpMessage::getHttpVersion()
1040 Get the HTTP Protocol Version of the Message. */
1041 PHP_METHOD(HttpMessage
, getHttpVersion
)
1045 if (return_value_used
) {
1047 getObject(http_message_object
, obj
);
1049 sprintf(ver
, "%1.1lf", obj
->message
->http
.version
);
1050 RETURN_STRINGL(ver
, 3, 1);
1055 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
1056 Set the HTTP Protocol version of the Message. */
1057 PHP_METHOD(HttpMessage
, setHttpVersion
)
1061 getObject(http_message_object
, obj
);
1063 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
1067 convert_to_double(zv
);
1068 sprintf(v
, "%1.1lf", Z_DVAL_P(zv
));
1069 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
1070 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
1074 obj
->message
->http
.version
= Z_DVAL_P(zv
);
1079 /* {{{ proto string HttpMessage::guessContentType(string magic_file[, int magic_mode = MAGIC_MIME])
1080 Attempts to guess the content type of supplied payload through libmagic. */
1081 PHP_METHOD(HttpMessage
, guessContentType
)
1083 #ifdef HTTP_HAVE_MAGIC
1084 char *magic_file
, *ct
= NULL
;
1086 long magic_mode
= MAGIC_MIME
;
1089 SET_EH_THROW_HTTP();
1090 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|l", &magic_file
, &magic_file_len
, &magic_mode
)) {
1091 getObject(http_message_object
, obj
);
1092 if ((ct
= http_guess_content_type(magic_file
, magic_mode
, PHPSTR_VAL(&obj
->message
->body
), PHPSTR_LEN(&obj
->message
->body
), SEND_DATA
))) {
1093 RETVAL_STRING(ct
, 0);
1098 http_error(HE_THROW
, HTTP_E_RUNTIME
, "Cannot guess Content-Type; libmagic not available");
1104 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
1105 Get parent Message. */
1106 PHP_METHOD(HttpMessage
, getParentMessage
)
1108 SET_EH_THROW_HTTP();
1110 getObject(http_message_object
, obj
);
1112 if (obj
->message
->parent
) {
1113 RETVAL_OBJVAL(obj
->parent
, 1);
1115 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "HttpMessage does not have a parent message");
1122 /* {{{ proto bool HttpMessage::send()
1123 Send the Message according to its type as Response or Request. */
1124 PHP_METHOD(HttpMessage
, send
)
1126 getObject(http_message_object
, obj
);
1130 RETURN_SUCCESS(http_message_send(obj
->message
));
1134 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
1135 Get the string representation of the Message. */
1136 PHP_METHOD(HttpMessage
, toString
)
1138 if (return_value_used
) {
1141 zend_bool include_parent
= 0;
1142 getObject(http_message_object
, obj
);
1144 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1148 if (include_parent
) {
1149 http_message_serialize(obj
->message
, &string
, &length
);
1151 http_message_tostring(obj
->message
, &string
, &length
);
1153 RETURN_STRINGL(string
, length
, 0);
1158 /* {{{ proto HttpRequest|HttpResponse HttpMessage::toMessageTypeObject(void)
1159 Creates an object regarding to the type of the message. Returns either an HttpRequest or HttpResponse object on success, or NULL on failure. */
1160 PHP_METHOD(HttpMessage
, toMessageTypeObject
)
1162 SET_EH_THROW_HTTP();
1166 if (return_value_used
) {
1167 getObject(http_message_object
, obj
);
1169 switch (obj
->message
->type
) {
1170 case HTTP_MSG_REQUEST
:
1172 #ifdef HTTP_HAVE_CURL
1175 zval body
, *array
, *headers
, *host
= http_message_header(obj
->message
, "Host");
1176 php_url hurl
, *purl
= php_url_parse(obj
->message
->http
.info
.request
.url
);
1178 MAKE_STD_ZVAL(array
);
1181 memset(&hurl
, 0, sizeof(php_url
));
1182 hurl
.host
= host
? Z_STRVAL_P(host
) : NULL
;
1183 zval_ptr_dtor(&host
);
1184 http_build_url(HTTP_URL_REPLACE
, purl
, &hurl
, NULL
, &url
, NULL
);
1186 add_assoc_string(array
, "url", url
, 0);
1188 if ( (method
= http_request_method_exists(1, 0, obj
->message
->http
.info
.request
.method
)) ||
1189 (method
= http_request_method_register(obj
->message
->http
.info
.request
.method
, strlen(obj
->message
->http
.info
.request
.method
)))) {
1190 add_assoc_long(array
, "method", method
);
1193 if (10 == (int) (obj
->message
->http
.version
* 10)) {
1194 add_assoc_long(array
, "protocol", CURL_HTTP_VERSION_1_0
);
1197 MAKE_STD_ZVAL(headers
);
1198 array_init(headers
);
1199 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(headers
));
1200 add_assoc_zval(array
, "headers", headers
);
1202 object_init_ex(return_value
, http_request_object_ce
);
1203 zend_call_method_with_1_params(&return_value
, http_request_object_ce
, NULL
, "setoptions", NULL
, array
);
1204 zval_ptr_dtor(&array
);
1207 ZVAL_STRINGL(&body
, PHPSTR_VAL(obj
->message
), PHPSTR_LEN(obj
->message
), 0);
1208 zend_call_method_with_1_params(&return_value
, http_request_object_ce
, NULL
, "setrawpostdata", NULL
, &body
);
1210 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot transform HttpMessage to HttpRequest (missing curl support)");
1215 case HTTP_MSG_RESPONSE
:
1218 HashPosition pos1
, pos2
;
1219 HashKey key
= initHashKey(0);
1220 zval
**header
, **h
, *body
;
1222 if (obj
->message
->http
.info
.response
.code
) {
1223 http_send_status(obj
->message
->http
.info
.response
.code
);
1226 object_init_ex(return_value
, http_response_object_ce
);
1228 FOREACH_HASH_KEYVAL(pos1
, &obj
->message
->hdrs
, key
, header
) {
1229 if (key
.type
== HASH_KEY_IS_STRING
) {
1232 MAKE_STD_ZVAL(zkey
);
1233 ZVAL_STRINGL(zkey
, key
.str
, key
.len
- 1, 1);
1235 switch (Z_TYPE_PP(header
)) {
1238 FOREACH_HASH_VAL(pos2
, HASH_OF(*header
), h
) {
1240 zend_call_method_with_2_params(&return_value
, http_response_object_ce
, NULL
, "setheader", NULL
, zkey
, *h
);
1246 ZVAL_ADDREF(*header
);
1247 zend_call_method_with_2_params(&return_value
, http_response_object_ce
, NULL
, "setheader", NULL
, zkey
, *header
);
1248 zval_ptr_dtor(header
);
1251 zval_ptr_dtor(&zkey
);
1255 MAKE_STD_ZVAL(body
);
1256 ZVAL_STRINGL(body
, PHPSTR_VAL(obj
->message
), PHPSTR_LEN(obj
->message
), 1);
1257 zend_call_method_with_1_params(&return_value
, http_response_object_ce
, NULL
, "setdata", NULL
, body
);
1258 zval_ptr_dtor(&body
);
1260 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot transform HttpMessage to HttpResponse (need PHP 5.1+)");
1266 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is neither of type HttpMessage::TYPE_REQUEST nor HttpMessage::TYPE_RESPONSE");
1274 /* {{{ proto int HttpMessage::count()
1275 Implements Countable::count(). Returns the number of parent messages + 1. */
1276 PHP_METHOD(HttpMessage
, count
)
1280 getObject(http_message_object
, obj
);
1282 http_message_count(i
, obj
->message
);
1288 /* {{{ proto string HttpMessage::serialize()
1289 Implements Serializable::serialize(). Returns the serialized representation of the HttpMessage. */
1290 PHP_METHOD(HttpMessage
, serialize
)
1295 getObject(http_message_object
, obj
);
1297 http_message_serialize(obj
->message
, &string
, &length
);
1298 RETURN_STRINGL(string
, length
, 0);
1303 /* {{{ proto void HttpMessage::unserialize(string serialized)
1304 Implements Serializable::unserialize(). Re-constructs the HttpMessage based upon the serialized string. */
1305 PHP_METHOD(HttpMessage
, unserialize
)
1309 getObject(http_message_object
, obj
);
1311 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &serialized
, &length
)) {
1314 http_message_dtor(obj
->message
);
1315 if ((msg
= http_message_parse_ex(obj
->message
, serialized
, (size_t) length
))) {
1318 http_error(HE_ERROR
, HTTP_E_RUNTIME
, "Could not unserialize HttpMessage");
1319 http_message_init(obj
->message
);
1325 /* {{{ proto HttpMessage HttpMessage::detach(void)
1326 Returns a clone of an HttpMessage object detached from any parent messages. */
1327 PHP_METHOD(HttpMessage
, detach
)
1331 getObject(http_message_object
, obj
);
1335 info
.type
= obj
->message
->type
;
1336 memcpy(&HTTP_INFO(&info
), &HTTP_INFO(obj
->message
), sizeof(struct http_info
));
1338 msg
= http_message_new();
1339 http_message_set_info(msg
, &info
);
1341 zend_hash_copy(&msg
->hdrs
, &obj
->message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1342 phpstr_append(&msg
->body
, PHPSTR_VAL(obj
->message
), PHPSTR_LEN(obj
->message
));
1344 RETVAL_OBJVAL(http_message_object_new_ex(Z_OBJCE_P(getThis()), msg
, NULL
), 0);
1348 /* {{{ proto void HttpMessage::prepend(HttpMessage message[, bool top = true])
1349 Prepends message(s) to the HTTP message. Throws HttpInvalidParamException if the message is located within the same message chain. */
1350 PHP_METHOD(HttpMessage
, prepend
)
1355 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O|b", &prepend
, http_message_object_ce
, &top
)) {
1356 http_message
*msg
[2];
1357 getObject(http_message_object
, obj
);
1358 getObjectEx(http_message_object
, prepend_obj
, prepend
);
1361 for (msg
[0] = obj
->message
; msg
[0]; msg
[0] = msg
[0]->parent
) {
1362 for (msg
[1] = prepend_obj
->message
; msg
[1]; msg
[1] = msg
[1]->parent
) {
1363 if (msg
[0] == msg
[1]) {
1364 http_error(HE_THROW
, HTTP_E_INVALID_PARAM
, "Cannot prepend a message located within the same message chain");
1370 http_message_object_prepend_ex(getThis(), prepend
, top
);
1375 /* {{{ proto HttpMessage HttpMessage::reverse()
1376 Reorders the message chain in reverse order. Returns the most parent HttpMessage object. */
1377 PHP_METHOD(HttpMessage
, reverse
)
1380 http_message_object_reverse(getThis(), return_value
);
1385 /* {{{ proto void HttpMessage::rewind(void)
1386 Implements Iterator::rewind(). */
1387 PHP_METHOD(HttpMessage
, rewind
)
1390 getObject(http_message_object
, obj
);
1392 if (obj
->iterator
) {
1393 zval_ptr_dtor(&obj
->iterator
);
1395 ZVAL_ADDREF(getThis());
1396 obj
->iterator
= getThis();
1401 /* {{{ proto bool HttpMessage::valid(void)
1402 Implements Iterator::valid(). */
1403 PHP_METHOD(HttpMessage
, valid
)
1406 getObject(http_message_object
, obj
);
1408 RETURN_BOOL(obj
->iterator
!= NULL
);
1413 /* {{{ proto void HttpMessage::next(void)
1414 Implements Iterator::next(). */
1415 PHP_METHOD(HttpMessage
, next
)
1418 getObject(http_message_object
, obj
);
1419 getObjectEx(http_message_object
, itr
, obj
->iterator
);
1421 if (itr
&& itr
->parent
.handle
) {
1422 zval
*old
= obj
->iterator
;
1423 MAKE_STD_ZVAL(obj
->iterator
);
1424 ZVAL_OBJVAL(obj
->iterator
, itr
->parent
, 1);
1425 zval_ptr_dtor(&old
);
1427 zval_ptr_dtor(&obj
->iterator
);
1428 obj
->iterator
= NULL
;
1434 /* {{{ proto int HttpMessage::key(void)
1435 Implements Iterator::key(). */
1436 PHP_METHOD(HttpMessage
, key
)
1439 getObject(http_message_object
, obj
);
1441 RETURN_LONG(obj
->iterator
? obj
->iterator
->value
.obj
.handle
:0);
1446 /* {{{ proto HttpMessage HttpMessage::current(void)
1447 Implements Iterator::current(). */
1448 PHP_METHOD(HttpMessage
, current
)
1451 getObject(http_message_object
, obj
);
1453 if (obj
->iterator
) {
1454 RETURN_ZVAL(obj
->iterator
, 1, 0);
1460 #endif /* ZEND_ENGINE_2 */
1467 * vim600: noet sw=4 ts=4 fdm=marker
1468 * vim<600: noet sw=4 ts=4