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-2007, 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"
24 #include "php_variables.h"
26 #include "php_http_api.h"
27 #include "php_http_send_api.h"
28 #include "php_http_url_api.h"
29 #include "php_http_message_api.h"
30 #include "php_http_message_object.h"
31 #include "php_http_exception_object.h"
32 #include "php_http_response_object.h"
33 #include "php_http_request_method_api.h"
34 #include "php_http_request_api.h"
35 #include "php_http_request_object.h"
36 #include "php_http_headers_api.h"
38 #if defined(HTTP_HAVE_SPL) && !defined(WONKY)
39 /* SPL doesn't install its headers */
40 extern PHPAPI zend_class_entry
*spl_ce_Countable
;
43 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpMessage, method, 0, req_args)
44 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpMessage, method, 0)
45 #define HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, HTTP_ARGS(HttpMessage, method), visibility)
47 HTTP_BEGIN_ARGS(__construct
, 0)
48 HTTP_ARG_VAL(message
, 0)
51 HTTP_BEGIN_ARGS(factory
, 0)
52 HTTP_ARG_VAL(message
, 0)
53 HTTP_ARG_VAL(class_name
, 0)
56 HTTP_BEGIN_ARGS(fromEnv
, 1)
58 HTTP_ARG_VAL(class_name
, 0)
61 HTTP_EMPTY_ARGS(getBody
);
62 HTTP_BEGIN_ARGS(setBody
, 1)
66 HTTP_BEGIN_ARGS(getHeader
, 1)
67 HTTP_ARG_VAL(header
, 0)
70 HTTP_EMPTY_ARGS(getHeaders
);
71 HTTP_BEGIN_ARGS(setHeaders
, 1)
72 HTTP_ARG_VAL(headers
, 0)
75 HTTP_BEGIN_ARGS(addHeaders
, 1)
76 HTTP_ARG_VAL(headers
, 0)
77 HTTP_ARG_VAL(append
, 0)
80 HTTP_EMPTY_ARGS(getType
);
81 HTTP_BEGIN_ARGS(setType
, 1)
85 HTTP_EMPTY_ARGS(getInfo
);
86 HTTP_BEGIN_ARGS(setInfo
, 1)
87 HTTP_ARG_VAL(http_info
, 0)
90 HTTP_EMPTY_ARGS(getResponseCode
);
91 HTTP_BEGIN_ARGS(setResponseCode
, 1)
92 HTTP_ARG_VAL(response_code
, 0)
95 HTTP_EMPTY_ARGS(getResponseStatus
);
96 HTTP_BEGIN_ARGS(setResponseStatus
, 1)
97 HTTP_ARG_VAL(response_status
, 0)
100 HTTP_EMPTY_ARGS(getRequestMethod
);
101 HTTP_BEGIN_ARGS(setRequestMethod
, 1)
102 HTTP_ARG_VAL(request_method
, 0)
105 HTTP_EMPTY_ARGS(getRequestUrl
);
106 HTTP_BEGIN_ARGS(setRequestUrl
, 1)
110 HTTP_EMPTY_ARGS(getHttpVersion
);
111 HTTP_BEGIN_ARGS(setHttpVersion
, 1)
112 HTTP_ARG_VAL(http_version
, 0)
115 HTTP_BEGIN_ARGS(guessContentType
, 1)
116 HTTP_ARG_VAL(magic_file
, 0)
117 HTTP_ARG_VAL(magic_mode
, 0)
120 HTTP_EMPTY_ARGS(getParentMessage
);
121 HTTP_EMPTY_ARGS(send
);
122 HTTP_BEGIN_ARGS(toString
, 0)
123 HTTP_ARG_VAL(include_parent
, 0)
126 HTTP_EMPTY_ARGS(toMessageTypeObject
);
128 HTTP_EMPTY_ARGS(count
);
130 HTTP_EMPTY_ARGS(serialize
);
131 HTTP_BEGIN_ARGS(unserialize
, 1)
132 HTTP_ARG_VAL(serialized
, 0)
135 HTTP_EMPTY_ARGS(rewind
);
136 HTTP_EMPTY_ARGS(valid
);
137 HTTP_EMPTY_ARGS(key
);
138 HTTP_EMPTY_ARGS(current
);
139 HTTP_EMPTY_ARGS(next
);
141 HTTP_EMPTY_ARGS(detach
);
142 HTTP_BEGIN_ARGS(prepend
, 1)
143 HTTP_ARG_OBJ(HttpMessage
, message
, 0)
145 HTTP_EMPTY_ARGS(reverse
);
147 #define http_message_object_read_prop _http_message_object_read_prop
148 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
);
149 #define http_message_object_write_prop _http_message_object_write_prop
150 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
);
151 #define http_message_object_get_props _http_message_object_get_props
152 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
);
154 #define THIS_CE http_message_object_ce
155 zend_class_entry
*http_message_object_ce
;
156 zend_function_entry http_message_object_fe
[] = {
157 HTTP_MESSAGE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
158 HTTP_MESSAGE_ME(getBody
, ZEND_ACC_PUBLIC
)
159 HTTP_MESSAGE_ME(setBody
, ZEND_ACC_PUBLIC
)
160 HTTP_MESSAGE_ME(getHeader
, ZEND_ACC_PUBLIC
)
161 HTTP_MESSAGE_ME(getHeaders
, ZEND_ACC_PUBLIC
)
162 HTTP_MESSAGE_ME(setHeaders
, ZEND_ACC_PUBLIC
)
163 HTTP_MESSAGE_ME(addHeaders
, ZEND_ACC_PUBLIC
)
164 HTTP_MESSAGE_ME(getType
, ZEND_ACC_PUBLIC
)
165 HTTP_MESSAGE_ME(setType
, ZEND_ACC_PUBLIC
)
166 HTTP_MESSAGE_ME(getInfo
, ZEND_ACC_PUBLIC
)
167 HTTP_MESSAGE_ME(setInfo
, ZEND_ACC_PUBLIC
)
168 HTTP_MESSAGE_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
169 HTTP_MESSAGE_ME(setResponseCode
, ZEND_ACC_PUBLIC
)
170 HTTP_MESSAGE_ME(getResponseStatus
, ZEND_ACC_PUBLIC
)
171 HTTP_MESSAGE_ME(setResponseStatus
, ZEND_ACC_PUBLIC
)
172 HTTP_MESSAGE_ME(getRequestMethod
, ZEND_ACC_PUBLIC
)
173 HTTP_MESSAGE_ME(setRequestMethod
, ZEND_ACC_PUBLIC
)
174 HTTP_MESSAGE_ME(getRequestUrl
, ZEND_ACC_PUBLIC
)
175 HTTP_MESSAGE_ME(setRequestUrl
, ZEND_ACC_PUBLIC
)
176 HTTP_MESSAGE_ME(getHttpVersion
, ZEND_ACC_PUBLIC
)
177 HTTP_MESSAGE_ME(setHttpVersion
, ZEND_ACC_PUBLIC
)
178 HTTP_MESSAGE_ME(guessContentType
, ZEND_ACC_PUBLIC
)
179 HTTP_MESSAGE_ME(getParentMessage
, ZEND_ACC_PUBLIC
)
180 HTTP_MESSAGE_ME(send
, ZEND_ACC_PUBLIC
)
181 HTTP_MESSAGE_ME(toString
, ZEND_ACC_PUBLIC
)
182 HTTP_MESSAGE_ME(toMessageTypeObject
, ZEND_ACC_PUBLIC
)
184 /* implements Countable */
185 HTTP_MESSAGE_ME(count
, ZEND_ACC_PUBLIC
)
187 /* implements Serializable */
188 HTTP_MESSAGE_ME(serialize
, ZEND_ACC_PUBLIC
)
189 HTTP_MESSAGE_ME(unserialize
, ZEND_ACC_PUBLIC
)
191 /* implements Iterator */
192 HTTP_MESSAGE_ME(rewind
, ZEND_ACC_PUBLIC
)
193 HTTP_MESSAGE_ME(valid
, ZEND_ACC_PUBLIC
)
194 HTTP_MESSAGE_ME(current
, ZEND_ACC_PUBLIC
)
195 HTTP_MESSAGE_ME(key
, ZEND_ACC_PUBLIC
)
196 HTTP_MESSAGE_ME(next
, ZEND_ACC_PUBLIC
)
198 ZEND_MALIAS(HttpMessage
, __toString
, toString
, HTTP_ARGS(HttpMessage
, toString
), ZEND_ACC_PUBLIC
)
200 HTTP_MESSAGE_ME(factory
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
201 ZEND_MALIAS(HttpMessage
, fromString
, factory
, HTTP_ARGS(HttpMessage
, factory
), ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
202 HTTP_MESSAGE_ME(fromEnv
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
204 HTTP_MESSAGE_ME(detach
, ZEND_ACC_PUBLIC
)
205 HTTP_MESSAGE_ME(prepend
, ZEND_ACC_PUBLIC
)
206 HTTP_MESSAGE_ME(reverse
, ZEND_ACC_PUBLIC
)
210 static zend_object_handlers http_message_object_handlers
;
212 PHP_MINIT_FUNCTION(http_message_object
)
214 HTTP_REGISTER_CLASS_EX(HttpMessage
, http_message_object
, NULL
, 0);
217 # ifdef HTTP_HAVE_SPL
218 zend_class_implements(http_message_object_ce TSRMLS_CC
, 3, spl_ce_Countable
, zend_ce_serializable
, zend_ce_iterator
);
220 zend_class_implements(http_message_object_ce TSRMLS_CC
, 2, zend_ce_serializable
, zend_ce_iterator
);
223 zend_class_implements(http_message_object_ce TSRMLS_CC
, 1, zend_ce_iterator
);
226 http_message_object_handlers
.clone_obj
= _http_message_object_clone_obj
;
227 http_message_object_handlers
.read_property
= http_message_object_read_prop
;
228 http_message_object_handlers
.write_property
= http_message_object_write_prop
;
229 http_message_object_handlers
.get_properties
= http_message_object_get_props
;
230 http_message_object_handlers
.get_property_ptr_ptr
= NULL
;
232 zend_declare_property_long(THIS_CE
, ZEND_STRS("type")-1, HTTP_MSG_NONE
, ZEND_ACC_PROTECTED TSRMLS_CC
);
233 zend_declare_property_string(THIS_CE
, ZEND_STRS("body")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC
);
234 zend_declare_property_string(THIS_CE
, ZEND_STRS("requestMethod")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC
);
235 zend_declare_property_string(THIS_CE
, ZEND_STRS("requestUrl")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC
);
236 zend_declare_property_string(THIS_CE
, ZEND_STRS("responseStatus")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC
);
237 zend_declare_property_long(THIS_CE
, ZEND_STRS("responseCode")-1, 0, ZEND_ACC_PROTECTED TSRMLS_CC
);
238 zend_declare_property_null(THIS_CE
, ZEND_STRS("httpVersion")-1, ZEND_ACC_PROTECTED TSRMLS_CC
);
239 zend_declare_property_null(THIS_CE
, ZEND_STRS("headers")-1, ZEND_ACC_PROTECTED TSRMLS_CC
);
240 zend_declare_property_null(THIS_CE
, ZEND_STRS("parentMessage")-1, ZEND_ACC_PROTECTED TSRMLS_CC
);
243 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("TYPE_NONE")-1, HTTP_MSG_NONE TSRMLS_CC
);
244 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("TYPE_REQUEST")-1, HTTP_MSG_REQUEST TSRMLS_CC
);
245 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("TYPE_RESPONSE")-1, HTTP_MSG_RESPONSE TSRMLS_CC
);
248 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE
);
249 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST
);
250 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE
);
255 void _http_message_object_reverse(zval
*this_ptr
, zval
*return_value TSRMLS_DC
)
258 getObject(http_message_object
, obj
);
261 http_message_count(i
, obj
->message
);
265 zend_object_value
*ovalues
= NULL
;
266 http_message_object
**objects
= NULL
;
269 objects
= ecalloc(i
, sizeof(http_message_object
*));
270 ovalues
= ecalloc(i
, sizeof(zend_object_value
));
272 /* we are the first message */
274 ovalues
[0] = getThis()->value
.obj
;
279 for (i
= 1; obj
->parent
.handle
; ++i
) {
280 o
.value
.obj
= obj
->parent
;
281 ovalues
[i
] = o
.value
.obj
;
282 objects
[i
] = obj
= zend_object_store_get_object(&o TSRMLS_CC
);
285 /* reorder parents */
286 for (last
= --i
; i
; --i
) {
287 objects
[i
]->message
->parent
= objects
[i
-1]->message
;
288 objects
[i
]->parent
= ovalues
[i
-1];
290 objects
[0]->message
->parent
= NULL
;
291 objects
[0]->parent
.handle
= 0;
292 objects
[0]->parent
.handlers
= NULL
;
295 Z_OBJ_ADDREF_P(getThis());
296 RETVAL_OBJVAL(ovalues
[last
], 1);
301 RETURN_ZVAL(getThis(), 1, 0);
305 void _http_message_object_prepend_ex(zval
*this_ptr
, zval
*prepend
, zend_bool top TSRMLS_DC
)
308 http_message
*save_parent_msg
= NULL
;
309 zend_object_value save_parent_obj
= {0, NULL
};
310 getObject(http_message_object
, obj
);
311 getObjectEx(http_message_object
, prepend_obj
, prepend
);
317 save_parent_obj
= obj
->parent
;
318 save_parent_msg
= obj
->message
->parent
;
320 /* iterate to the most parent object */
321 while (obj
->parent
.handle
) {
322 m
.value
.obj
= obj
->parent
;
323 obj
= zend_object_store_get_object(&m TSRMLS_CC
);
328 obj
->parent
= prepend
->value
.obj
;
329 obj
->message
->parent
= prepend_obj
->message
;
332 zend_objects_store_add_ref(prepend TSRMLS_CC
);
333 while (prepend_obj
->parent
.handle
) {
334 m
.value
.obj
= prepend_obj
->parent
;
335 zend_objects_store_add_ref(&m TSRMLS_CC
);
336 prepend_obj
= zend_object_store_get_object(&m TSRMLS_CC
);
340 prepend_obj
->parent
= save_parent_obj
;
341 prepend_obj
->message
->parent
= save_parent_msg
;
345 zend_object_value
_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
347 return http_message_object_new_ex(ce
, NULL
, NULL
);
350 zend_object_value
_http_message_object_new_ex(zend_class_entry
*ce
, http_message
*msg
, http_message_object
**ptr TSRMLS_DC
)
352 zend_object_value ov
;
353 http_message_object
*o
;
355 o
= ecalloc(1, sizeof(http_message_object
));
365 o
->parent
= http_message_object_new_ex(ce
, msg
->parent
, NULL
);
369 ALLOC_HASHTABLE(OBJ_PROP(o
));
370 zend_hash_init(OBJ_PROP(o
), zend_hash_num_elements(&ce
->default_properties
), NULL
, ZVAL_PTR_DTOR
, 0);
371 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
373 ov
.handle
= putObject(http_message_object
, o
);
374 ov
.handlers
= &http_message_object_handlers
;
379 zend_object_value
_http_message_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
381 zend_object_value new_ov
;
382 http_message_object
*new_obj
= NULL
;
383 getObject(http_message_object
, old_obj
);
385 new_ov
= http_message_object_new_ex(old_obj
->zo
.ce
, http_message_dup(old_obj
->message
), &new_obj
);
386 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
391 void _http_message_object_free(zend_object
*object TSRMLS_DC
)
393 http_message_object
*o
= (http_message_object
*) object
;
396 http_message_dtor(o
->message
);
399 if (o
->parent
.handle
) {
404 p
.value
.obj
= o
->parent
;
405 zend_objects_store_del_ref(&p TSRMLS_CC
);
410 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
412 getObjectEx(http_message_object
, obj
, object
);
413 http_message
*msg
= obj
->message
;
416 ulong h
= zend_hash_func(Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1);
418 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
420 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
421 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
425 if (type
== BP_VAR_W
) {
426 zend_error(E_ERROR
, "Cannot access HttpMessage properties by reference or array key/index");
430 ALLOC_ZVAL(return_value
);
431 return_value
->refcount
= 0;
432 return_value
->is_ref
= 0;
440 case HTTP_MSG_PROPHASH_TYPE
:
441 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
442 RETVAL_LONG(msg
->type
);
445 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
446 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
447 RETVAL_DOUBLE(msg
->http
.version
);
450 case HTTP_MSG_PROPHASH_BODY
:
451 case HTTP_MSG_CHILD_PROPHASH_BODY
:
452 phpstr_fix(PHPSTR(msg
));
453 RETVAL_PHPSTR(PHPSTR(msg
), 0, 1);
456 case HTTP_MSG_PROPHASH_HEADERS
:
457 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
458 array_init(return_value
);
459 zend_hash_copy(Z_ARRVAL_P(return_value
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
462 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
463 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
465 RETVAL_OBJVAL(obj
->parent
, 1);
471 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
472 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
473 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.method
) {
474 RETVAL_STRING(msg
->http
.info
.request
.method
, 1);
480 case HTTP_MSG_PROPHASH_REQUEST_URL
:
481 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL
:
482 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.url
) {
483 RETVAL_STRING(msg
->http
.info
.request
.url
, 1);
489 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
490 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
491 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
492 RETVAL_LONG(msg
->http
.info
.response
.code
);
498 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
499 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
500 if (HTTP_MSG_TYPE(RESPONSE
, msg
) && msg
->http
.info
.response
.status
) {
501 RETVAL_STRING(msg
->http
.info
.response
.status
, 1);
509 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
518 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
)
520 getObjectEx(http_message_object
, obj
, object
);
521 http_message
*msg
= obj
->message
;
524 ulong h
= zend_hash_func(Z_STRVAL_P(member
), Z_STRLEN_P(member
) + 1);
526 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
528 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
529 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
534 cpy
= zval_copy(Z_TYPE_P(value
), value
);
542 case HTTP_MSG_PROPHASH_TYPE
:
543 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
544 convert_to_long(cpy
);
545 http_message_set_type(msg
, Z_LVAL_P(cpy
));
548 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
549 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
550 convert_to_double(cpy
);
551 msg
->http
.version
= Z_DVAL_P(cpy
);
554 case HTTP_MSG_PROPHASH_BODY
:
555 case HTTP_MSG_CHILD_PROPHASH_BODY
:
556 convert_to_string(cpy
);
557 phpstr_dtor(PHPSTR(msg
));
558 phpstr_from_string_ex(PHPSTR(msg
), Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
));
561 case HTTP_MSG_PROPHASH_HEADERS
:
562 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
563 convert_to_array(cpy
);
564 zend_hash_clean(&msg
->hdrs
);
565 zend_hash_copy(&msg
->hdrs
, Z_ARRVAL_P(cpy
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
568 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
569 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
570 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), http_message_object_ce TSRMLS_CC
)) {
573 tmp
.value
.obj
= obj
->parent
;
576 Z_OBJ_ADDREF_P(value
);
577 obj
->parent
= value
->value
.obj
;
581 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
582 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
583 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
584 convert_to_string(cpy
);
585 STR_SET(msg
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
589 case HTTP_MSG_PROPHASH_REQUEST_URL
:
590 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL
:
591 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
592 convert_to_string(cpy
);
593 STR_SET(msg
->http
.info
.request
.url
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
597 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
598 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
599 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
600 convert_to_long(cpy
);
601 msg
->http
.info
.response
.code
= Z_LVAL_P(cpy
);
605 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
606 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
607 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
608 convert_to_string(cpy
);
609 STR_SET(msg
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
615 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
622 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
)
625 getObjectEx(http_message_object
, obj
, object
);
626 http_message
*msg
= obj
->message
;
627 HashTable
*props
= OBJ_PROP(obj
);
630 INIT_ZARR(array
, props
);
632 #define ASSOC_PROP(array, ptype, name, val) \
636 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
637 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
638 efree(m_prop_name); \
640 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
641 #define ASSOC_STRINGL(array, name, val, len) \
645 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
646 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, 1); \
647 efree(m_prop_name); \
650 ASSOC_PROP(array
, long, "type", msg
->type
);
651 ASSOC_PROP(array
, double, "httpVersion", msg
->http
.version
);
654 case HTTP_MSG_REQUEST
:
655 ASSOC_PROP(array
, long, "responseCode", 0);
656 ASSOC_STRINGL(array
, "responseStatus", "", 0);
657 ASSOC_STRING(array
, "requestMethod", STR_PTR(msg
->http
.info
.request
.method
));
658 ASSOC_STRING(array
, "requestUrl", STR_PTR(msg
->http
.info
.request
.url
));
661 case HTTP_MSG_RESPONSE
:
662 ASSOC_PROP(array
, long, "responseCode", msg
->http
.info
.response
.code
);
663 ASSOC_STRING(array
, "responseStatus", STR_PTR(msg
->http
.info
.response
.status
));
664 ASSOC_STRINGL(array
, "requestMethod", "", 0);
665 ASSOC_STRINGL(array
, "requestUrl", "", 0);
670 ASSOC_PROP(array
, long, "responseCode", 0);
671 ASSOC_STRINGL(array
, "responseStatus", "", 0);
672 ASSOC_STRINGL(array
, "requestMethod", "", 0);
673 ASSOC_STRINGL(array
, "requestUrl", "", 0);
677 MAKE_STD_ZVAL(headers
);
679 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
680 ASSOC_PROP(array
, zval
, "headers", headers
);
681 ASSOC_STRINGL(array
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
683 MAKE_STD_ZVAL(parent
);
685 ZVAL_OBJVAL(parent
, obj
->parent
, 1);
689 ASSOC_PROP(array
, zval
, "parentMessage", parent
);
691 return OBJ_PROP(obj
);
694 /* ### USERLAND ### */
696 /* {{{ proto void HttpMessage::__construct([string message])
697 Create a new HttpMessage object instance. */
698 PHP_METHOD(HttpMessage
, __construct
)
701 char *message
= NULL
;
703 getObject(http_message_object
, obj
);
706 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
707 http_message
*msg
= obj
->message
;
709 http_message_dtor(msg
);
710 if ((obj
->message
= http_message_parse_ex(msg
, message
, length
))) {
711 if (obj
->message
->parent
) {
712 obj
->parent
= http_message_object_new_ex(Z_OBJCE_P(getThis()), obj
->message
->parent
, NULL
);
715 obj
->message
= http_message_init(msg
);
719 obj
->message
= http_message_new();
725 /* {{{ proto static HttpMessage HttpMessage::factory([string raw_message[, string class_name = "HttpMessage"]])
726 Create a new HttpMessage object instance. */
727 PHP_METHOD(HttpMessage
, factory
)
729 char *string
= NULL
, *cn
= NULL
;
730 int length
= 0, cl
= 0;
731 http_message
*msg
= NULL
;
732 zend_object_value ov
;
733 http_message_object
*obj
= NULL
;
738 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|ss", &string
, &length
, &cn
, &cl
)) {
740 msg
= http_message_parse(string
, length
);
742 if ((msg
|| !length
) && SUCCESS
== http_object_new(&ov
, cn
, cl
, _http_message_object_new_ex
, http_message_object_ce
, msg
, &obj
)) {
743 RETVAL_OBJVAL(ov
, 0);
745 if (obj
&& !obj
->message
) {
746 obj
->message
= http_message_new();
753 /* {{{ proto static HttpMessage HttpMessage::fromEnv(int type[, string class_name = "HttpMessage"])
754 Create a new HttpMessage object from environment representing either current request or response */
755 PHP_METHOD(HttpMessage
, fromEnv
)
760 http_message_object
*obj
= NULL
;
761 zend_object_value ov
;
765 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l|s", &type
, &cn
, &cl
)) {
766 if (SUCCESS
== http_object_new(&ov
, cn
, cl
, _http_message_object_new_ex
, http_message_object_ce
, http_message_init_env(NULL
, type
), &obj
)) {
767 RETVAL_OBJVAL(ov
, 0);
769 if (obj
&& !obj
->message
) {
770 obj
->message
= http_message_new();
777 /* {{{ proto string HttpMessage::getBody()
778 Get the body of the parsed HttpMessage. */
779 PHP_METHOD(HttpMessage
, getBody
)
783 if (return_value_used
) {
784 getObject(http_message_object
, obj
);
785 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
790 /* {{{ proto void HttpMessage::setBody(string body)
791 Set the body of the HttpMessage. NOTE: Don't forget to update any headers accordingly. */
792 PHP_METHOD(HttpMessage
, setBody
)
796 getObject(http_message_object
, obj
);
798 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &body
, &len
)) {
799 phpstr_dtor(PHPSTR(obj
->message
));
800 phpstr_from_string_ex(PHPSTR(obj
->message
), body
, len
);
805 /* {{{ proto string HttpMessage::getHeader(string header)
806 Get message header. */
807 PHP_METHOD(HttpMessage
, getHeader
)
810 char *orig_header
, *nice_header
;
812 getObject(http_message_object
, obj
);
814 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &orig_header
, &header_len
)) {
818 nice_header
= pretty_key(estrndup(orig_header
, header_len
), header_len
, 1, 1);
819 if ((header
= http_message_header_ex(obj
->message
, nice_header
, header_len
+ 1, 0))) {
820 RETVAL_ZVAL(header
, 1, 1);
826 /* {{{ proto array HttpMessage::getHeaders()
827 Get Message Headers. */
828 PHP_METHOD(HttpMessage
, getHeaders
)
832 if (return_value_used
) {
833 getObject(http_message_object
, obj
);
835 array_init(return_value
);
836 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(return_value
));
841 /* {{{ proto void HttpMessage::setHeaders(array headers)
843 PHP_METHOD(HttpMessage
, setHeaders
)
845 zval
*new_headers
= NULL
;
846 getObject(http_message_object
, obj
);
848 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &new_headers
)) {
852 zend_hash_clean(&obj
->message
->hdrs
);
854 array_copy(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
);
859 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
860 Add headers. If append is true, headers with the same name will be separated, else overwritten. */
861 PHP_METHOD(HttpMessage
, addHeaders
)
864 zend_bool append
= 0;
865 getObject(http_message_object
, obj
);
867 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
871 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, append
, ARRAY_JOIN_STRONLY
|ARRAY_JOIN_PRETTIFY
);
875 /* {{{ proto int HttpMessage::getType()
876 Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE) */
877 PHP_METHOD(HttpMessage
, getType
)
881 if (return_value_used
) {
882 getObject(http_message_object
, obj
);
883 RETURN_LONG(obj
->message
->type
);
888 /* {{{ proto void HttpMessage::setType(int type)
889 Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE) */
890 PHP_METHOD(HttpMessage
, setType
)
893 getObject(http_message_object
, obj
);
895 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
898 http_message_set_type(obj
->message
, type
);
902 /* {{{ proto string HttpMessage::getInfo(void)
903 Get the HTTP request/response line */
904 PHP_METHOD(HttpMessage
, getInfo
)
908 if (return_value_used
) {
909 getObject(http_message_object
, obj
);
911 switch (obj
->message
->type
) {
912 case HTTP_MSG_REQUEST
:
913 Z_STRLEN_P(return_value
) = spprintf(&Z_STRVAL_P(return_value
), 0, HTTP_INFO_REQUEST_FMT_ARGS(&obj
->message
->http
, ""));
915 case HTTP_MSG_RESPONSE
:
916 Z_STRLEN_P(return_value
) = spprintf(&Z_STRVAL_P(return_value
), 0, HTTP_INFO_RESPONSE_FMT_ARGS(&obj
->message
->http
, ""));
922 Z_TYPE_P(return_value
) = IS_STRING
;
927 /* {{{ proto bool HttpMessage::setInfo(string http_info)
928 Set type and request or response info with a standard HTTP request or response line */
929 PHP_METHOD(HttpMessage
, setInfo
)
935 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
) && SUCCESS
== http_info_parse_ex(str
, &inf
, 0)) {
936 getObject(http_message_object
, obj
);
938 http_message_set_info(obj
->message
, &inf
);
939 http_info_dtor(&inf
);
946 /* {{{ proto int HttpMessage::getResponseCode()
947 Get the Response Code of the Message. */
948 PHP_METHOD(HttpMessage
, getResponseCode
)
952 if (return_value_used
) {
953 getObject(http_message_object
, obj
);
954 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
955 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
960 /* {{{ proto bool HttpMessage::setResponseCode(int code)
961 Set the response code of an HTTP Response Message. */
962 PHP_METHOD(HttpMessage
, setResponseCode
)
965 getObject(http_message_object
, obj
);
967 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
969 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
972 if (code
< 100 || code
> 599) {
973 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid response code (100-599): %ld", code
);
977 obj
->message
->http
.info
.response
.code
= code
;
982 /* {{{ proto string HttpMessage::getResponseStatus()
983 Get the Response Status of the message (i.e. the string following the response code). */
984 PHP_METHOD(HttpMessage
, getResponseStatus
)
988 if (return_value_used
) {
989 getObject(http_message_object
, obj
);
990 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
991 if (obj
->message
->http
.info
.response
.status
) {
992 RETURN_STRING(obj
->message
->http
.info
.response
.status
, 1);
994 RETURN_EMPTY_STRING();
1000 /* {{{ proto bool HttpMessage::setResponseStatus(string status)
1001 Set the Response Status of the HTTP message (i.e. the string following the response code). */
1002 PHP_METHOD(HttpMessage
, setResponseStatus
)
1006 getObject(http_message_object
, obj
);
1008 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
1010 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &status
, &status_len
)) {
1013 STR_SET(obj
->message
->http
.info
.response
.status
, estrndup(status
, status_len
));
1018 /* {{{ proto string HttpMessage::getRequestMethod()
1019 Get the Request Method of the Message. */
1020 PHP_METHOD(HttpMessage
, getRequestMethod
)
1024 if (return_value_used
) {
1025 getObject(http_message_object
, obj
);
1026 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
1027 if (obj
->message
->http
.info
.request
.method
) {
1028 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
1030 RETURN_EMPTY_STRING();
1036 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
1037 Set the Request Method of the HTTP Message. */
1038 PHP_METHOD(HttpMessage
, setRequestMethod
)
1042 getObject(http_message_object
, obj
);
1044 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
1046 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1049 if (method_len
< 1) {
1050 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
1053 if (!http_request_method_exists(1, 0, method
)) {
1054 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unknown request method: %s", method
);
1058 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
1063 /* {{{ proto string HttpMessage::getRequestUrl()
1064 Get the Request URL of the Message. */
1065 PHP_METHOD(HttpMessage
, getRequestUrl
)
1069 if (return_value_used
) {
1070 getObject(http_message_object
, obj
);
1071 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
1072 if (obj
->message
->http
.info
.request
.url
) {
1073 RETURN_STRING(obj
->message
->http
.info
.request
.url
, 1);
1075 RETURN_EMPTY_STRING();
1081 /* {{{ proto bool HttpMessage::setRequestUrl(string url)
1082 Set the Request URL of the HTTP Message. */
1083 PHP_METHOD(HttpMessage
, setRequestUrl
)
1087 getObject(http_message_object
, obj
);
1089 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
1092 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
1094 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUrl to an empty string");
1098 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(URI
, URIlen
));
1103 /* {{{ proto string HttpMessage::getHttpVersion()
1104 Get the HTTP Protocol Version of the Message. */
1105 PHP_METHOD(HttpMessage
, getHttpVersion
)
1109 if (return_value_used
) {
1111 getObject(http_message_object
, obj
);
1113 sprintf(ver
, "%1.1lf", obj
->message
->http
.version
);
1114 RETURN_STRINGL(ver
, 3, 1);
1119 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
1120 Set the HTTP Protocol version of the Message. */
1121 PHP_METHOD(HttpMessage
, setHttpVersion
)
1125 getObject(http_message_object
, obj
);
1127 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
1131 convert_to_double(zv
);
1132 sprintf(v
, "%1.1lf", Z_DVAL_P(zv
));
1133 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
1134 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
1138 obj
->message
->http
.version
= Z_DVAL_P(zv
);
1143 /* {{{ proto string HttpMessage::guessContentType(string magic_file[, int magic_mode = MAGIC_MIME])
1144 Attempts to guess the content type of supplied payload through libmagic. */
1145 PHP_METHOD(HttpMessage
, guessContentType
)
1147 #ifdef HTTP_HAVE_MAGIC
1148 char *magic_file
, *ct
= NULL
;
1150 long magic_mode
= MAGIC_MIME
;
1153 SET_EH_THROW_HTTP();
1154 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|l", &magic_file
, &magic_file_len
, &magic_mode
)) {
1155 getObject(http_message_object
, obj
);
1156 if ((ct
= http_guess_content_type(magic_file
, magic_mode
, PHPSTR_VAL(&obj
->message
->body
), PHPSTR_LEN(&obj
->message
->body
), SEND_DATA
))) {
1157 RETVAL_STRING(ct
, 0);
1162 http_error(HE_THROW
, HTTP_E_RUNTIME
, "Cannot guess Content-Type; libmagic not available");
1168 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
1169 Get parent Message. */
1170 PHP_METHOD(HttpMessage
, getParentMessage
)
1172 SET_EH_THROW_HTTP();
1174 getObject(http_message_object
, obj
);
1176 if (obj
->message
->parent
) {
1177 RETVAL_OBJVAL(obj
->parent
, 1);
1179 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "HttpMessage does not have a parent message");
1186 /* {{{ proto bool HttpMessage::send()
1187 Send the Message according to its type as Response or Request. */
1188 PHP_METHOD(HttpMessage
, send
)
1190 getObject(http_message_object
, obj
);
1194 RETURN_SUCCESS(http_message_send(obj
->message
));
1198 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
1199 Get the string representation of the Message. */
1200 PHP_METHOD(HttpMessage
, toString
)
1202 if (return_value_used
) {
1205 zend_bool include_parent
= 0;
1206 getObject(http_message_object
, obj
);
1208 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1212 if (include_parent
) {
1213 http_message_serialize(obj
->message
, &string
, &length
);
1215 http_message_tostring(obj
->message
, &string
, &length
);
1217 RETURN_STRINGL(string
, length
, 0);
1222 /* {{{ proto HttpRequest|HttpResponse HttpMessage::toMessageTypeObject(void)
1223 Creates an object regarding to the type of the message. Returns either an HttpRequest or HttpResponse object on success, or NULL on failure. */
1224 PHP_METHOD(HttpMessage
, toMessageTypeObject
)
1226 SET_EH_THROW_HTTP();
1230 if (return_value_used
) {
1231 getObject(http_message_object
, obj
);
1233 switch (obj
->message
->type
) {
1234 case HTTP_MSG_REQUEST
:
1236 #ifdef HTTP_HAVE_CURL
1239 zval post
, body
, *array
, *headers
, *host
= http_message_header(obj
->message
, "Host");
1240 php_url hurl
, *purl
= php_url_parse(STR_PTR(obj
->message
->http
.info
.request
.url
));
1242 MAKE_STD_ZVAL(array
);
1245 memset(&hurl
, 0, sizeof(php_url
));
1247 hurl
.host
= Z_STRVAL_P(host
);
1248 zval_ptr_dtor(&host
);
1250 http_build_url(HTTP_URL_REPLACE
, purl
, &hurl
, NULL
, &url
, NULL
);
1252 add_assoc_string(array
, "url", url
, 0);
1254 if ( obj
->message
->http
.info
.request
.method
&&
1255 ((method
= http_request_method_exists(1, 0, obj
->message
->http
.info
.request
.method
)) ||
1256 (method
= http_request_method_register(obj
->message
->http
.info
.request
.method
, strlen(obj
->message
->http
.info
.request
.method
))))) {
1257 add_assoc_long(array
, "method", method
);
1260 if (10 == (int) (obj
->message
->http
.version
* 10)) {
1261 add_assoc_long(array
, "protocol", CURL_HTTP_VERSION_1_0
);
1264 MAKE_STD_ZVAL(headers
);
1265 array_init(headers
);
1266 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(headers
));
1267 add_assoc_zval(array
, "headers", headers
);
1269 object_init_ex(return_value
, http_request_object_ce
);
1270 zend_call_method_with_1_params(&return_value
, http_request_object_ce
, NULL
, "setoptions", NULL
, array
);
1271 zval_ptr_dtor(&array
);
1273 if (PHPSTR_VAL(obj
->message
) && PHPSTR_LEN(obj
->message
)) {
1275 ZVAL_STRINGL(&body
, PHPSTR_VAL(obj
->message
), PHPSTR_LEN(obj
->message
), 0);
1276 if (method
!= HTTP_POST
) {
1277 zend_call_method_with_1_params(&return_value
, http_request_object_ce
, NULL
, "setbody", NULL
, &body
);
1282 zval_copy_ctor(&body
);
1283 sapi_module
.treat_data(PARSE_STRING
, Z_STRVAL(body
), &post TSRMLS_CC
);
1284 zend_call_method_with_1_params(&return_value
, http_request_object_ce
, NULL
, "setpostfields", NULL
, &post
);
1288 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot transform HttpMessage to HttpRequest (missing curl support)");
1293 case HTTP_MSG_RESPONSE
:
1296 HashPosition pos1
, pos2
;
1297 HashKey key
= initHashKey(0);
1298 zval
**header
, **h
, *body
;
1300 if (obj
->message
->http
.info
.response
.code
) {
1301 http_send_status(obj
->message
->http
.info
.response
.code
);
1304 object_init_ex(return_value
, http_response_object_ce
);
1306 FOREACH_HASH_KEYVAL(pos1
, &obj
->message
->hdrs
, key
, header
) {
1307 if (key
.type
== HASH_KEY_IS_STRING
) {
1310 MAKE_STD_ZVAL(zkey
);
1311 ZVAL_STRINGL(zkey
, key
.str
, key
.len
- 1, 1);
1313 switch (Z_TYPE_PP(header
)) {
1316 FOREACH_HASH_VAL(pos2
, HASH_OF(*header
), h
) {
1318 zend_call_method_with_2_params(&return_value
, http_response_object_ce
, NULL
, "setheader", NULL
, zkey
, *h
);
1324 ZVAL_ADDREF(*header
);
1325 zend_call_method_with_2_params(&return_value
, http_response_object_ce
, NULL
, "setheader", NULL
, zkey
, *header
);
1326 zval_ptr_dtor(header
);
1329 zval_ptr_dtor(&zkey
);
1333 MAKE_STD_ZVAL(body
);
1334 ZVAL_STRINGL(body
, PHPSTR_VAL(obj
->message
), PHPSTR_LEN(obj
->message
), 1);
1335 zend_call_method_with_1_params(&return_value
, http_response_object_ce
, NULL
, "setdata", NULL
, body
);
1336 zval_ptr_dtor(&body
);
1338 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot transform HttpMessage to HttpResponse (need PHP 5.1+)");
1344 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is neither of type HttpMessage::TYPE_REQUEST nor HttpMessage::TYPE_RESPONSE");
1352 /* {{{ proto int HttpMessage::count()
1353 Implements Countable::count(). Returns the number of parent messages + 1. */
1354 PHP_METHOD(HttpMessage
, count
)
1358 getObject(http_message_object
, obj
);
1360 http_message_count(i
, obj
->message
);
1366 /* {{{ proto string HttpMessage::serialize()
1367 Implements Serializable::serialize(). Returns the serialized representation of the HttpMessage. */
1368 PHP_METHOD(HttpMessage
, serialize
)
1373 getObject(http_message_object
, obj
);
1375 http_message_serialize(obj
->message
, &string
, &length
);
1376 RETURN_STRINGL(string
, length
, 0);
1381 /* {{{ proto void HttpMessage::unserialize(string serialized)
1382 Implements Serializable::unserialize(). Re-constructs the HttpMessage based upon the serialized string. */
1383 PHP_METHOD(HttpMessage
, unserialize
)
1387 getObject(http_message_object
, obj
);
1389 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &serialized
, &length
)) {
1392 http_message_dtor(obj
->message
);
1393 if ((msg
= http_message_parse_ex(obj
->message
, serialized
, (size_t) length
))) {
1396 http_error(HE_ERROR
, HTTP_E_RUNTIME
, "Could not unserialize HttpMessage");
1397 http_message_init(obj
->message
);
1403 /* {{{ proto HttpMessage HttpMessage::detach(void)
1404 Returns a clone of an HttpMessage object detached from any parent messages. */
1405 PHP_METHOD(HttpMessage
, detach
)
1409 getObject(http_message_object
, obj
);
1413 info
.type
= obj
->message
->type
;
1414 memcpy(&HTTP_INFO(&info
), &HTTP_INFO(obj
->message
), sizeof(struct http_info
));
1416 msg
= http_message_new();
1417 http_message_set_info(msg
, &info
);
1419 zend_hash_copy(&msg
->hdrs
, &obj
->message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1420 phpstr_append(&msg
->body
, PHPSTR_VAL(obj
->message
), PHPSTR_LEN(obj
->message
));
1422 RETVAL_OBJVAL(http_message_object_new_ex(Z_OBJCE_P(getThis()), msg
, NULL
), 0);
1426 /* {{{ proto void HttpMessage::prepend(HttpMessage message[, bool top = true])
1427 Prepends message(s) to the HTTP message. Throws HttpInvalidParamException if the message is located within the same message chain. */
1428 PHP_METHOD(HttpMessage
, prepend
)
1433 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O|b", &prepend
, http_message_object_ce
, &top
)) {
1434 http_message
*msg
[2];
1435 getObject(http_message_object
, obj
);
1436 getObjectEx(http_message_object
, prepend_obj
, prepend
);
1439 for (msg
[0] = obj
->message
; msg
[0]; msg
[0] = msg
[0]->parent
) {
1440 for (msg
[1] = prepend_obj
->message
; msg
[1]; msg
[1] = msg
[1]->parent
) {
1441 if (msg
[0] == msg
[1]) {
1442 http_error(HE_THROW
, HTTP_E_INVALID_PARAM
, "Cannot prepend a message located within the same message chain");
1448 http_message_object_prepend_ex(getThis(), prepend
, top
);
1453 /* {{{ proto HttpMessage HttpMessage::reverse()
1454 Reorders the message chain in reverse order. Returns the most parent HttpMessage object. */
1455 PHP_METHOD(HttpMessage
, reverse
)
1458 http_message_object_reverse(getThis(), return_value
);
1463 /* {{{ proto void HttpMessage::rewind(void)
1464 Implements Iterator::rewind(). */
1465 PHP_METHOD(HttpMessage
, rewind
)
1468 getObject(http_message_object
, obj
);
1470 if (obj
->iterator
) {
1471 zval_ptr_dtor(&obj
->iterator
);
1473 ZVAL_ADDREF(getThis());
1474 obj
->iterator
= getThis();
1479 /* {{{ proto bool HttpMessage::valid(void)
1480 Implements Iterator::valid(). */
1481 PHP_METHOD(HttpMessage
, valid
)
1484 getObject(http_message_object
, obj
);
1486 RETURN_BOOL(obj
->iterator
!= NULL
);
1491 /* {{{ proto void HttpMessage::next(void)
1492 Implements Iterator::next(). */
1493 PHP_METHOD(HttpMessage
, next
)
1496 getObject(http_message_object
, obj
);
1497 getObjectEx(http_message_object
, itr
, obj
->iterator
);
1499 if (itr
&& itr
->parent
.handle
) {
1500 zval
*old
= obj
->iterator
;
1501 MAKE_STD_ZVAL(obj
->iterator
);
1502 ZVAL_OBJVAL(obj
->iterator
, itr
->parent
, 1);
1503 zval_ptr_dtor(&old
);
1505 zval_ptr_dtor(&obj
->iterator
);
1506 obj
->iterator
= NULL
;
1512 /* {{{ proto int HttpMessage::key(void)
1513 Implements Iterator::key(). */
1514 PHP_METHOD(HttpMessage
, key
)
1517 getObject(http_message_object
, obj
);
1519 RETURN_LONG(obj
->iterator
? obj
->iterator
->value
.obj
.handle
:0);
1524 /* {{{ proto HttpMessage HttpMessage::current(void)
1525 Implements Iterator::current(). */
1526 PHP_METHOD(HttpMessage
, current
)
1529 getObject(http_message_object
, obj
);
1531 if (obj
->iterator
) {
1532 RETURN_ZVAL(obj
->iterator
, 1, 0);
1538 #endif /* ZEND_ENGINE_2 */
1545 * vim600: noet sw=4 ts=4 fdm=marker
1546 * vim<600: noet sw=4 ts=4