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_EMPTY_ARGS(__toString
);
123 HTTP_BEGIN_ARGS(toString
, 0)
124 HTTP_ARG_VAL(include_parent
, 0)
127 HTTP_EMPTY_ARGS(toMessageTypeObject
);
129 HTTP_EMPTY_ARGS(count
);
131 HTTP_EMPTY_ARGS(serialize
);
132 HTTP_BEGIN_ARGS(unserialize
, 1)
133 HTTP_ARG_VAL(serialized
, 0)
136 HTTP_EMPTY_ARGS(rewind
);
137 HTTP_EMPTY_ARGS(valid
);
138 HTTP_EMPTY_ARGS(key
);
139 HTTP_EMPTY_ARGS(current
);
140 HTTP_EMPTY_ARGS(next
);
142 HTTP_EMPTY_ARGS(detach
);
143 HTTP_BEGIN_ARGS(prepend
, 1)
144 HTTP_ARG_OBJ(HttpMessage
, message
, 0)
146 HTTP_EMPTY_ARGS(reverse
);
148 #define http_message_object_read_prop _http_message_object_read_prop
149 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
);
150 #define http_message_object_write_prop _http_message_object_write_prop
151 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
);
152 #define http_message_object_get_props _http_message_object_get_props
153 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
);
155 #define THIS_CE http_message_object_ce
156 zend_class_entry
*http_message_object_ce
;
157 zend_function_entry http_message_object_fe
[] = {
158 HTTP_MESSAGE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
159 HTTP_MESSAGE_ME(getBody
, ZEND_ACC_PUBLIC
)
160 HTTP_MESSAGE_ME(setBody
, ZEND_ACC_PUBLIC
)
161 HTTP_MESSAGE_ME(getHeader
, ZEND_ACC_PUBLIC
)
162 HTTP_MESSAGE_ME(getHeaders
, ZEND_ACC_PUBLIC
)
163 HTTP_MESSAGE_ME(setHeaders
, ZEND_ACC_PUBLIC
)
164 HTTP_MESSAGE_ME(addHeaders
, ZEND_ACC_PUBLIC
)
165 HTTP_MESSAGE_ME(getType
, ZEND_ACC_PUBLIC
)
166 HTTP_MESSAGE_ME(setType
, ZEND_ACC_PUBLIC
)
167 HTTP_MESSAGE_ME(getInfo
, ZEND_ACC_PUBLIC
)
168 HTTP_MESSAGE_ME(setInfo
, ZEND_ACC_PUBLIC
)
169 HTTP_MESSAGE_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
170 HTTP_MESSAGE_ME(setResponseCode
, ZEND_ACC_PUBLIC
)
171 HTTP_MESSAGE_ME(getResponseStatus
, ZEND_ACC_PUBLIC
)
172 HTTP_MESSAGE_ME(setResponseStatus
, ZEND_ACC_PUBLIC
)
173 HTTP_MESSAGE_ME(getRequestMethod
, ZEND_ACC_PUBLIC
)
174 HTTP_MESSAGE_ME(setRequestMethod
, ZEND_ACC_PUBLIC
)
175 HTTP_MESSAGE_ME(getRequestUrl
, ZEND_ACC_PUBLIC
)
176 HTTP_MESSAGE_ME(setRequestUrl
, ZEND_ACC_PUBLIC
)
177 HTTP_MESSAGE_ME(getHttpVersion
, ZEND_ACC_PUBLIC
)
178 HTTP_MESSAGE_ME(setHttpVersion
, ZEND_ACC_PUBLIC
)
179 HTTP_MESSAGE_ME(guessContentType
, ZEND_ACC_PUBLIC
)
180 HTTP_MESSAGE_ME(getParentMessage
, ZEND_ACC_PUBLIC
)
181 HTTP_MESSAGE_ME(send
, ZEND_ACC_PUBLIC
)
182 HTTP_MESSAGE_ME(toString
, ZEND_ACC_PUBLIC
)
183 HTTP_MESSAGE_ME(toMessageTypeObject
, ZEND_ACC_PUBLIC
)
185 /* implements Countable */
186 HTTP_MESSAGE_ME(count
, ZEND_ACC_PUBLIC
)
188 /* implements Serializable */
189 HTTP_MESSAGE_ME(serialize
, ZEND_ACC_PUBLIC
)
190 HTTP_MESSAGE_ME(unserialize
, ZEND_ACC_PUBLIC
)
192 /* implements Iterator */
193 HTTP_MESSAGE_ME(rewind
, ZEND_ACC_PUBLIC
)
194 HTTP_MESSAGE_ME(valid
, ZEND_ACC_PUBLIC
)
195 HTTP_MESSAGE_ME(current
, ZEND_ACC_PUBLIC
)
196 HTTP_MESSAGE_ME(key
, ZEND_ACC_PUBLIC
)
197 HTTP_MESSAGE_ME(next
, ZEND_ACC_PUBLIC
)
199 ZEND_MALIAS(HttpMessage
, __toString
, toString
, HTTP_ARGS(HttpMessage
, __toString
), ZEND_ACC_PUBLIC
)
201 HTTP_MESSAGE_ME(factory
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
202 ZEND_MALIAS(HttpMessage
, fromString
, factory
, HTTP_ARGS(HttpMessage
, factory
), ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
203 HTTP_MESSAGE_ME(fromEnv
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
205 HTTP_MESSAGE_ME(detach
, ZEND_ACC_PUBLIC
)
206 HTTP_MESSAGE_ME(prepend
, ZEND_ACC_PUBLIC
)
207 HTTP_MESSAGE_ME(reverse
, ZEND_ACC_PUBLIC
)
211 static zend_object_handlers http_message_object_handlers
;
213 PHP_MINIT_FUNCTION(http_message_object
)
215 HTTP_REGISTER_CLASS_EX(HttpMessage
, http_message_object
, NULL
, 0);
218 # ifdef HTTP_HAVE_SPL
219 zend_class_implements(http_message_object_ce TSRMLS_CC
, 3, spl_ce_Countable
, zend_ce_serializable
, zend_ce_iterator
);
221 zend_class_implements(http_message_object_ce TSRMLS_CC
, 2, zend_ce_serializable
, zend_ce_iterator
);
224 zend_class_implements(http_message_object_ce TSRMLS_CC
, 1, zend_ce_iterator
);
227 http_message_object_handlers
.clone_obj
= _http_message_object_clone_obj
;
228 http_message_object_handlers
.read_property
= http_message_object_read_prop
;
229 http_message_object_handlers
.write_property
= http_message_object_write_prop
;
230 http_message_object_handlers
.get_properties
= http_message_object_get_props
;
231 http_message_object_handlers
.get_property_ptr_ptr
= NULL
;
233 zend_declare_property_long(THIS_CE
, ZEND_STRS("type")-1, HTTP_MSG_NONE
, ZEND_ACC_PROTECTED TSRMLS_CC
);
234 zend_declare_property_string(THIS_CE
, ZEND_STRS("body")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC
);
235 zend_declare_property_string(THIS_CE
, ZEND_STRS("requestMethod")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC
);
236 zend_declare_property_string(THIS_CE
, ZEND_STRS("requestUrl")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC
);
237 zend_declare_property_string(THIS_CE
, ZEND_STRS("responseStatus")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC
);
238 zend_declare_property_long(THIS_CE
, ZEND_STRS("responseCode")-1, 0, ZEND_ACC_PROTECTED TSRMLS_CC
);
239 zend_declare_property_null(THIS_CE
, ZEND_STRS("httpVersion")-1, ZEND_ACC_PROTECTED TSRMLS_CC
);
240 zend_declare_property_null(THIS_CE
, ZEND_STRS("headers")-1, ZEND_ACC_PROTECTED TSRMLS_CC
);
241 zend_declare_property_null(THIS_CE
, ZEND_STRS("parentMessage")-1, ZEND_ACC_PROTECTED TSRMLS_CC
);
244 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("TYPE_NONE")-1, HTTP_MSG_NONE TSRMLS_CC
);
245 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("TYPE_REQUEST")-1, HTTP_MSG_REQUEST TSRMLS_CC
);
246 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("TYPE_RESPONSE")-1, HTTP_MSG_RESPONSE TSRMLS_CC
);
249 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE
);
250 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST
);
251 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE
);
256 void _http_message_object_reverse(zval
*this_ptr
, zval
*return_value TSRMLS_DC
)
259 getObject(http_message_object
, obj
);
262 http_message_count(i
, obj
->message
);
266 zend_object_value
*ovalues
= NULL
;
267 http_message_object
**objects
= NULL
;
270 objects
= ecalloc(i
, sizeof(http_message_object
*));
271 ovalues
= ecalloc(i
, sizeof(zend_object_value
));
273 /* we are the first message */
275 ovalues
[0] = getThis()->value
.obj
;
280 for (i
= 1; obj
->parent
.handle
; ++i
) {
281 o
.value
.obj
= obj
->parent
;
282 ovalues
[i
] = o
.value
.obj
;
283 objects
[i
] = obj
= zend_object_store_get_object(&o TSRMLS_CC
);
286 /* reorder parents */
287 for (last
= --i
; i
; --i
) {
288 objects
[i
]->message
->parent
= objects
[i
-1]->message
;
289 objects
[i
]->parent
= ovalues
[i
-1];
291 objects
[0]->message
->parent
= NULL
;
292 objects
[0]->parent
.handle
= 0;
293 objects
[0]->parent
.handlers
= NULL
;
296 Z_OBJ_ADDREF_P(getThis());
297 RETVAL_OBJVAL(ovalues
[last
], 1);
302 RETURN_ZVAL(getThis(), 1, 0);
306 void _http_message_object_prepend_ex(zval
*this_ptr
, zval
*prepend
, zend_bool top TSRMLS_DC
)
309 http_message
*save_parent_msg
= NULL
;
310 zend_object_value save_parent_obj
= {0, NULL
};
311 getObject(http_message_object
, obj
);
312 getObjectEx(http_message_object
, prepend_obj
, prepend
);
318 save_parent_obj
= obj
->parent
;
319 save_parent_msg
= obj
->message
->parent
;
321 /* iterate to the most parent object */
322 while (obj
->parent
.handle
) {
323 m
.value
.obj
= obj
->parent
;
324 obj
= zend_object_store_get_object(&m TSRMLS_CC
);
329 obj
->parent
= prepend
->value
.obj
;
330 obj
->message
->parent
= prepend_obj
->message
;
333 zend_objects_store_add_ref(prepend TSRMLS_CC
);
334 while (prepend_obj
->parent
.handle
) {
335 m
.value
.obj
= prepend_obj
->parent
;
336 zend_objects_store_add_ref(&m TSRMLS_CC
);
337 prepend_obj
= zend_object_store_get_object(&m TSRMLS_CC
);
341 prepend_obj
->parent
= save_parent_obj
;
342 prepend_obj
->message
->parent
= save_parent_msg
;
346 zend_object_value
_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
348 return http_message_object_new_ex(ce
, NULL
, NULL
);
351 zend_object_value
_http_message_object_new_ex(zend_class_entry
*ce
, http_message
*msg
, http_message_object
**ptr TSRMLS_DC
)
353 zend_object_value ov
;
354 http_message_object
*o
;
356 o
= ecalloc(1, sizeof(http_message_object
));
366 o
->parent
= http_message_object_new_ex(ce
, msg
->parent
, NULL
);
370 ALLOC_HASHTABLE(OBJ_PROP(o
));
371 zend_hash_init(OBJ_PROP(o
), zend_hash_num_elements(&ce
->default_properties
), NULL
, ZVAL_PTR_DTOR
, 0);
372 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
374 ov
.handle
= putObject(http_message_object
, o
);
375 ov
.handlers
= &http_message_object_handlers
;
380 zend_object_value
_http_message_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
382 zend_object_value new_ov
;
383 http_message_object
*new_obj
= NULL
;
384 getObject(http_message_object
, old_obj
);
386 new_ov
= http_message_object_new_ex(old_obj
->zo
.ce
, http_message_dup(old_obj
->message
), &new_obj
);
387 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
392 void _http_message_object_free(zend_object
*object TSRMLS_DC
)
394 http_message_object
*o
= (http_message_object
*) object
;
397 http_message_dtor(o
->message
);
400 if (o
->parent
.handle
) {
405 p
.value
.obj
= o
->parent
;
406 zend_objects_store_del_ref(&p TSRMLS_CC
);
411 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
413 getObjectEx(http_message_object
, obj
, object
);
414 http_message
*msg
= obj
->message
;
417 ulong h
= zend_hash_func(Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1);
419 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
421 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
422 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
426 if (type
== BP_VAR_W
) {
427 zend_error(E_ERROR
, "Cannot access HttpMessage properties by reference or array key/index");
431 ALLOC_ZVAL(return_value
);
432 #ifdef Z_SET_REFCOUNT
433 Z_SET_REFCOUNT_P(return_value
, 0);
434 Z_UNSET_ISREF_P(return_value
);
436 return_value
->refcount
= 0;
437 return_value
->is_ref
= 0;
446 case HTTP_MSG_PROPHASH_TYPE
:
447 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
448 RETVAL_LONG(msg
->type
);
451 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
452 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
453 RETVAL_DOUBLE(msg
->http
.version
);
456 case HTTP_MSG_PROPHASH_BODY
:
457 case HTTP_MSG_CHILD_PROPHASH_BODY
:
458 phpstr_fix(PHPSTR(msg
));
459 RETVAL_PHPSTR(PHPSTR(msg
), 0, 1);
462 case HTTP_MSG_PROPHASH_HEADERS
:
463 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
464 array_init(return_value
);
465 zend_hash_copy(Z_ARRVAL_P(return_value
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
468 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
469 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
471 RETVAL_OBJVAL(obj
->parent
, 1);
477 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
478 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
479 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.method
) {
480 RETVAL_STRING(msg
->http
.info
.request
.method
, 1);
486 case HTTP_MSG_PROPHASH_REQUEST_URL
:
487 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL
:
488 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.url
) {
489 RETVAL_STRING(msg
->http
.info
.request
.url
, 1);
495 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
496 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
497 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
498 RETVAL_LONG(msg
->http
.info
.response
.code
);
504 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
505 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
506 if (HTTP_MSG_TYPE(RESPONSE
, msg
) && msg
->http
.info
.response
.status
) {
507 RETVAL_STRING(msg
->http
.info
.response
.status
, 1);
515 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
524 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
)
526 getObjectEx(http_message_object
, obj
, object
);
527 http_message
*msg
= obj
->message
;
530 ulong h
= zend_hash_func(Z_STRVAL_P(member
), Z_STRLEN_P(member
) + 1);
532 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
534 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
535 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
548 case HTTP_MSG_PROPHASH_TYPE
:
549 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
550 convert_to_long_ex(&cpy
);
551 http_message_set_type(msg
, Z_LVAL_P(cpy
));
554 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
555 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
556 convert_to_double_ex(&cpy
);
557 msg
->http
.version
= Z_DVAL_P(cpy
);
560 case HTTP_MSG_PROPHASH_BODY
:
561 case HTTP_MSG_CHILD_PROPHASH_BODY
:
562 convert_to_string_ex(&cpy
);
563 phpstr_dtor(PHPSTR(msg
));
564 phpstr_from_string_ex(PHPSTR(msg
), Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
));
567 case HTTP_MSG_PROPHASH_HEADERS
:
568 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
569 convert_to_array_ex(&cpy
);
570 zend_hash_clean(&msg
->hdrs
);
571 zend_hash_copy(&msg
->hdrs
, Z_ARRVAL_P(cpy
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
574 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
575 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
576 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), http_message_object_ce TSRMLS_CC
)) {
579 tmp
.value
.obj
= obj
->parent
;
582 Z_OBJ_ADDREF_P(value
);
583 obj
->parent
= value
->value
.obj
;
587 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
588 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
589 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
590 convert_to_string_ex(&cpy
);
591 STR_SET(msg
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
595 case HTTP_MSG_PROPHASH_REQUEST_URL
:
596 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL
:
597 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
598 convert_to_string_ex(&cpy
);
599 STR_SET(msg
->http
.info
.request
.url
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
603 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
604 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
605 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
606 convert_to_long_ex(&cpy
);
607 msg
->http
.info
.response
.code
= Z_LVAL_P(cpy
);
611 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
612 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
613 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
614 convert_to_string_ex(&cpy
);
615 STR_SET(msg
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
621 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
630 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
)
633 getObjectEx(http_message_object
, obj
, object
);
634 http_message
*msg
= obj
->message
;
635 HashTable
*props
= OBJ_PROP(obj
);
638 INIT_ZARR(array
, props
);
640 #define ASSOC_PROP(array, ptype, name, val) \
644 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
645 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
646 efree(m_prop_name); \
648 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
649 #define ASSOC_STRINGL(array, name, val, len) \
653 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
654 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, 1); \
655 efree(m_prop_name); \
658 ASSOC_PROP(array
, long, "type", msg
->type
);
659 ASSOC_PROP(array
, double, "httpVersion", msg
->http
.version
);
662 case HTTP_MSG_REQUEST
:
663 ASSOC_PROP(array
, long, "responseCode", 0);
664 ASSOC_STRINGL(array
, "responseStatus", "", 0);
665 ASSOC_STRING(array
, "requestMethod", STR_PTR(msg
->http
.info
.request
.method
));
666 ASSOC_STRING(array
, "requestUrl", STR_PTR(msg
->http
.info
.request
.url
));
669 case HTTP_MSG_RESPONSE
:
670 ASSOC_PROP(array
, long, "responseCode", msg
->http
.info
.response
.code
);
671 ASSOC_STRING(array
, "responseStatus", STR_PTR(msg
->http
.info
.response
.status
));
672 ASSOC_STRINGL(array
, "requestMethod", "", 0);
673 ASSOC_STRINGL(array
, "requestUrl", "", 0);
678 ASSOC_PROP(array
, long, "responseCode", 0);
679 ASSOC_STRINGL(array
, "responseStatus", "", 0);
680 ASSOC_STRINGL(array
, "requestMethod", "", 0);
681 ASSOC_STRINGL(array
, "requestUrl", "", 0);
685 MAKE_STD_ZVAL(headers
);
687 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
688 ASSOC_PROP(array
, zval
, "headers", headers
);
689 ASSOC_STRINGL(array
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
691 MAKE_STD_ZVAL(parent
);
693 ZVAL_OBJVAL(parent
, obj
->parent
, 1);
697 ASSOC_PROP(array
, zval
, "parentMessage", parent
);
699 return OBJ_PROP(obj
);
702 /* ### USERLAND ### */
704 /* {{{ proto void HttpMessage::__construct([string message])
705 Create a new HttpMessage object instance. */
706 PHP_METHOD(HttpMessage
, __construct
)
709 char *message
= NULL
;
711 getObject(http_message_object
, obj
);
714 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
715 http_message
*msg
= obj
->message
;
717 http_message_dtor(msg
);
718 if ((obj
->message
= http_message_parse_ex(msg
, message
, length
))) {
719 if (obj
->message
->parent
) {
720 obj
->parent
= http_message_object_new_ex(Z_OBJCE_P(getThis()), obj
->message
->parent
, NULL
);
723 obj
->message
= http_message_init(msg
);
727 obj
->message
= http_message_new();
733 /* {{{ proto static HttpMessage HttpMessage::factory([string raw_message[, string class_name = "HttpMessage"]])
734 Create a new HttpMessage object instance. */
735 PHP_METHOD(HttpMessage
, factory
)
737 char *string
= NULL
, *cn
= NULL
;
738 int length
= 0, cl
= 0;
739 http_message
*msg
= NULL
;
740 zend_object_value ov
;
741 http_message_object
*obj
= NULL
;
746 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|ss", &string
, &length
, &cn
, &cl
)) {
748 msg
= http_message_parse(string
, length
);
750 if ((msg
|| !length
) && SUCCESS
== http_object_new(&ov
, cn
, cl
, _http_message_object_new_ex
, http_message_object_ce
, msg
, &obj
)) {
751 RETVAL_OBJVAL(ov
, 0);
753 if (obj
&& !obj
->message
) {
754 obj
->message
= http_message_new();
761 /* {{{ proto static HttpMessage HttpMessage::fromEnv(int type[, string class_name = "HttpMessage"])
762 Create a new HttpMessage object from environment representing either current request or response */
763 PHP_METHOD(HttpMessage
, fromEnv
)
768 http_message_object
*obj
= NULL
;
769 zend_object_value ov
;
773 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l|s", &type
, &cn
, &cl
)) {
774 if (SUCCESS
== http_object_new(&ov
, cn
, cl
, _http_message_object_new_ex
, http_message_object_ce
, http_message_init_env(NULL
, type
), &obj
)) {
775 RETVAL_OBJVAL(ov
, 0);
777 if (obj
&& !obj
->message
) {
778 obj
->message
= http_message_new();
785 /* {{{ proto string HttpMessage::getBody()
786 Get the body of the parsed HttpMessage. */
787 PHP_METHOD(HttpMessage
, getBody
)
791 if (return_value_used
) {
792 getObject(http_message_object
, obj
);
793 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
798 /* {{{ proto void HttpMessage::setBody(string body)
799 Set the body of the HttpMessage. NOTE: Don't forget to update any headers accordingly. */
800 PHP_METHOD(HttpMessage
, setBody
)
804 getObject(http_message_object
, obj
);
806 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &body
, &len
)) {
807 phpstr_dtor(PHPSTR(obj
->message
));
808 phpstr_from_string_ex(PHPSTR(obj
->message
), body
, len
);
813 /* {{{ proto string HttpMessage::getHeader(string header)
814 Get message header. */
815 PHP_METHOD(HttpMessage
, getHeader
)
818 char *orig_header
, *nice_header
;
820 getObject(http_message_object
, obj
);
822 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &orig_header
, &header_len
)) {
826 nice_header
= pretty_key(estrndup(orig_header
, header_len
), header_len
, 1, 1);
827 if ((header
= http_message_header_ex(obj
->message
, nice_header
, header_len
+ 1, 0))) {
828 RETVAL_ZVAL(header
, 1, 1);
834 /* {{{ proto array HttpMessage::getHeaders()
835 Get Message Headers. */
836 PHP_METHOD(HttpMessage
, getHeaders
)
840 if (return_value_used
) {
841 getObject(http_message_object
, obj
);
843 array_init(return_value
);
844 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(return_value
));
849 /* {{{ proto void HttpMessage::setHeaders(array headers)
851 PHP_METHOD(HttpMessage
, setHeaders
)
853 zval
*new_headers
= NULL
;
854 getObject(http_message_object
, obj
);
856 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &new_headers
)) {
860 zend_hash_clean(&obj
->message
->hdrs
);
862 array_copy(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
);
867 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
868 Add headers. If append is true, headers with the same name will be separated, else overwritten. */
869 PHP_METHOD(HttpMessage
, addHeaders
)
872 zend_bool append
= 0;
873 getObject(http_message_object
, obj
);
875 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
879 array_join(Z_ARRVAL_P(new_headers
), &obj
->message
->hdrs
, append
, ARRAY_JOIN_STRONLY
|ARRAY_JOIN_PRETTIFY
);
883 /* {{{ proto int HttpMessage::getType()
884 Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE) */
885 PHP_METHOD(HttpMessage
, getType
)
889 if (return_value_used
) {
890 getObject(http_message_object
, obj
);
891 RETURN_LONG(obj
->message
->type
);
896 /* {{{ proto void HttpMessage::setType(int type)
897 Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE) */
898 PHP_METHOD(HttpMessage
, setType
)
901 getObject(http_message_object
, obj
);
903 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
906 http_message_set_type(obj
->message
, type
);
910 /* {{{ proto string HttpMessage::getInfo(void)
911 Get the HTTP request/response line */
912 PHP_METHOD(HttpMessage
, getInfo
)
916 if (return_value_used
) {
917 getObject(http_message_object
, obj
);
919 switch (obj
->message
->type
) {
920 case HTTP_MSG_REQUEST
:
921 Z_STRLEN_P(return_value
) = spprintf(&Z_STRVAL_P(return_value
), 0, HTTP_INFO_REQUEST_FMT_ARGS(&obj
->message
->http
, ""));
923 case HTTP_MSG_RESPONSE
:
924 Z_STRLEN_P(return_value
) = spprintf(&Z_STRVAL_P(return_value
), 0, HTTP_INFO_RESPONSE_FMT_ARGS(&obj
->message
->http
, ""));
930 Z_TYPE_P(return_value
) = IS_STRING
;
935 /* {{{ proto bool HttpMessage::setInfo(string http_info)
936 Set type and request or response info with a standard HTTP request or response line */
937 PHP_METHOD(HttpMessage
, setInfo
)
943 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
) && SUCCESS
== http_info_parse_ex(str
, &inf
, 0)) {
944 getObject(http_message_object
, obj
);
946 http_message_set_info(obj
->message
, &inf
);
947 http_info_dtor(&inf
);
954 /* {{{ proto int HttpMessage::getResponseCode()
955 Get the Response Code of the Message. */
956 PHP_METHOD(HttpMessage
, getResponseCode
)
960 if (return_value_used
) {
961 getObject(http_message_object
, obj
);
962 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
963 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
968 /* {{{ proto bool HttpMessage::setResponseCode(int code)
969 Set the response code of an HTTP Response Message. */
970 PHP_METHOD(HttpMessage
, setResponseCode
)
973 getObject(http_message_object
, obj
);
975 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
977 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
980 if (code
< 100 || code
> 599) {
981 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid response code (100-599): %ld", code
);
985 obj
->message
->http
.info
.response
.code
= code
;
990 /* {{{ proto string HttpMessage::getResponseStatus()
991 Get the Response Status of the message (i.e. the string following the response code). */
992 PHP_METHOD(HttpMessage
, getResponseStatus
)
996 if (return_value_used
) {
997 getObject(http_message_object
, obj
);
998 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
999 if (obj
->message
->http
.info
.response
.status
) {
1000 RETURN_STRING(obj
->message
->http
.info
.response
.status
, 1);
1002 RETURN_EMPTY_STRING();
1008 /* {{{ proto bool HttpMessage::setResponseStatus(string status)
1009 Set the Response Status of the HTTP message (i.e. the string following the response code). */
1010 PHP_METHOD(HttpMessage
, setResponseStatus
)
1014 getObject(http_message_object
, obj
);
1016 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
1018 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &status
, &status_len
)) {
1021 STR_SET(obj
->message
->http
.info
.response
.status
, estrndup(status
, status_len
));
1026 /* {{{ proto string HttpMessage::getRequestMethod()
1027 Get the Request Method of the Message. */
1028 PHP_METHOD(HttpMessage
, getRequestMethod
)
1032 if (return_value_used
) {
1033 getObject(http_message_object
, obj
);
1034 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
1035 if (obj
->message
->http
.info
.request
.method
) {
1036 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
1038 RETURN_EMPTY_STRING();
1044 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
1045 Set the Request Method of the HTTP Message. */
1046 PHP_METHOD(HttpMessage
, setRequestMethod
)
1050 getObject(http_message_object
, obj
);
1052 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
1054 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1057 if (method_len
< 1) {
1058 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
1061 if (!http_request_method_exists(1, 0, method
)) {
1062 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unknown request method: %s", method
);
1066 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
1071 /* {{{ proto string HttpMessage::getRequestUrl()
1072 Get the Request URL of the Message. */
1073 PHP_METHOD(HttpMessage
, getRequestUrl
)
1077 if (return_value_used
) {
1078 getObject(http_message_object
, obj
);
1079 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
1080 if (obj
->message
->http
.info
.request
.url
) {
1081 RETURN_STRING(obj
->message
->http
.info
.request
.url
, 1);
1083 RETURN_EMPTY_STRING();
1089 /* {{{ proto bool HttpMessage::setRequestUrl(string url)
1090 Set the Request URL of the HTTP Message. */
1091 PHP_METHOD(HttpMessage
, setRequestUrl
)
1095 getObject(http_message_object
, obj
);
1097 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
1100 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
1102 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUrl to an empty string");
1106 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(URI
, URIlen
));
1111 /* {{{ proto string HttpMessage::getHttpVersion()
1112 Get the HTTP Protocol Version of the Message. */
1113 PHP_METHOD(HttpMessage
, getHttpVersion
)
1117 if (return_value_used
) {
1119 getObject(http_message_object
, obj
);
1121 sprintf(ver
, "%1.1lf", obj
->message
->http
.version
);
1122 RETURN_STRINGL(ver
, 3, 1);
1127 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
1128 Set the HTTP Protocol version of the Message. */
1129 PHP_METHOD(HttpMessage
, setHttpVersion
)
1133 getObject(http_message_object
, obj
);
1135 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
1139 convert_to_double(zv
);
1140 sprintf(v
, "%1.1lf", Z_DVAL_P(zv
));
1141 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
1142 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
1146 obj
->message
->http
.version
= Z_DVAL_P(zv
);
1151 /* {{{ proto string HttpMessage::guessContentType(string magic_file[, int magic_mode = MAGIC_MIME])
1152 Attempts to guess the content type of supplied payload through libmagic. */
1153 PHP_METHOD(HttpMessage
, guessContentType
)
1155 #ifdef HTTP_HAVE_MAGIC
1156 char *magic_file
, *ct
= NULL
;
1158 long magic_mode
= MAGIC_MIME
;
1161 SET_EH_THROW_HTTP();
1162 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|l", &magic_file
, &magic_file_len
, &magic_mode
)) {
1163 getObject(http_message_object
, obj
);
1164 if ((ct
= http_guess_content_type(magic_file
, magic_mode
, PHPSTR_VAL(&obj
->message
->body
), PHPSTR_LEN(&obj
->message
->body
), SEND_DATA
))) {
1165 RETVAL_STRING(ct
, 0);
1170 http_error(HE_THROW
, HTTP_E_RUNTIME
, "Cannot guess Content-Type; libmagic not available");
1176 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
1177 Get parent Message. */
1178 PHP_METHOD(HttpMessage
, getParentMessage
)
1180 SET_EH_THROW_HTTP();
1182 getObject(http_message_object
, obj
);
1184 if (obj
->message
->parent
) {
1185 RETVAL_OBJVAL(obj
->parent
, 1);
1187 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "HttpMessage does not have a parent message");
1194 /* {{{ proto bool HttpMessage::send()
1195 Send the Message according to its type as Response or Request. */
1196 PHP_METHOD(HttpMessage
, send
)
1198 getObject(http_message_object
, obj
);
1202 RETURN_SUCCESS(http_message_send(obj
->message
));
1206 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
1207 Get the string representation of the Message. */
1208 PHP_METHOD(HttpMessage
, toString
)
1210 if (return_value_used
) {
1213 zend_bool include_parent
= 0;
1214 getObject(http_message_object
, obj
);
1216 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1220 if (include_parent
) {
1221 http_message_serialize(obj
->message
, &string
, &length
);
1223 http_message_tostring(obj
->message
, &string
, &length
);
1225 RETURN_STRINGL(string
, length
, 0);
1230 /* {{{ proto HttpRequest|HttpResponse HttpMessage::toMessageTypeObject(void)
1231 Creates an object regarding to the type of the message. Returns either an HttpRequest or HttpResponse object on success, or NULL on failure. */
1232 PHP_METHOD(HttpMessage
, toMessageTypeObject
)
1234 SET_EH_THROW_HTTP();
1238 if (return_value_used
) {
1239 getObject(http_message_object
, obj
);
1241 switch (obj
->message
->type
) {
1242 case HTTP_MSG_REQUEST
:
1244 #ifdef HTTP_HAVE_CURL
1247 zval post
, body
, *array
, *headers
, *host
= http_message_header(obj
->message
, "Host");
1248 php_url hurl
, *purl
= php_url_parse(STR_PTR(obj
->message
->http
.info
.request
.url
));
1250 MAKE_STD_ZVAL(array
);
1253 memset(&hurl
, 0, sizeof(php_url
));
1255 hurl
.host
= Z_STRVAL_P(host
);
1256 zval_ptr_dtor(&host
);
1258 http_build_url(HTTP_URL_REPLACE
, purl
, &hurl
, NULL
, &url
, NULL
);
1260 add_assoc_string(array
, "url", url
, 0);
1262 if ( obj
->message
->http
.info
.request
.method
&&
1263 ((method
= http_request_method_exists(1, 0, obj
->message
->http
.info
.request
.method
)) ||
1264 (method
= http_request_method_register(obj
->message
->http
.info
.request
.method
, strlen(obj
->message
->http
.info
.request
.method
))))) {
1265 add_assoc_long(array
, "method", method
);
1268 if (10 == (int) (obj
->message
->http
.version
* 10)) {
1269 add_assoc_long(array
, "protocol", CURL_HTTP_VERSION_1_0
);
1272 MAKE_STD_ZVAL(headers
);
1273 array_init(headers
);
1274 array_copy(&obj
->message
->hdrs
, Z_ARRVAL_P(headers
));
1275 add_assoc_zval(array
, "headers", headers
);
1277 object_init_ex(return_value
, http_request_object_ce
);
1278 zend_call_method_with_1_params(&return_value
, http_request_object_ce
, NULL
, "setoptions", NULL
, array
);
1279 zval_ptr_dtor(&array
);
1281 if (PHPSTR_VAL(obj
->message
) && PHPSTR_LEN(obj
->message
)) {
1283 ZVAL_STRINGL(&body
, PHPSTR_VAL(obj
->message
), PHPSTR_LEN(obj
->message
), 0);
1284 if (method
!= HTTP_POST
) {
1285 zend_call_method_with_1_params(&return_value
, http_request_object_ce
, NULL
, "setbody", NULL
, &body
);
1290 zval_copy_ctor(&body
);
1291 sapi_module
.treat_data(PARSE_STRING
, Z_STRVAL(body
), &post TSRMLS_CC
);
1292 zend_call_method_with_1_params(&return_value
, http_request_object_ce
, NULL
, "setpostfields", NULL
, &post
);
1296 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot transform HttpMessage to HttpRequest (missing curl support)");
1301 case HTTP_MSG_RESPONSE
:
1304 HashPosition pos1
, pos2
;
1305 HashKey key
= initHashKey(0);
1306 zval
**header
, **h
, *body
;
1308 if (obj
->message
->http
.info
.response
.code
) {
1309 http_send_status(obj
->message
->http
.info
.response
.code
);
1312 object_init_ex(return_value
, http_response_object_ce
);
1314 FOREACH_HASH_KEYVAL(pos1
, &obj
->message
->hdrs
, key
, header
) {
1315 if (key
.type
== HASH_KEY_IS_STRING
) {
1318 MAKE_STD_ZVAL(zkey
);
1319 ZVAL_STRINGL(zkey
, key
.str
, key
.len
- 1, 1);
1321 switch (Z_TYPE_PP(header
)) {
1324 FOREACH_HASH_VAL(pos2
, HASH_OF(*header
), h
) {
1326 zend_call_method_with_2_params(&return_value
, http_response_object_ce
, NULL
, "setheader", NULL
, zkey
, *h
);
1332 ZVAL_ADDREF(*header
);
1333 zend_call_method_with_2_params(&return_value
, http_response_object_ce
, NULL
, "setheader", NULL
, zkey
, *header
);
1334 zval_ptr_dtor(header
);
1337 zval_ptr_dtor(&zkey
);
1341 MAKE_STD_ZVAL(body
);
1342 ZVAL_STRINGL(body
, PHPSTR_VAL(obj
->message
), PHPSTR_LEN(obj
->message
), 1);
1343 zend_call_method_with_1_params(&return_value
, http_response_object_ce
, NULL
, "setdata", NULL
, body
);
1344 zval_ptr_dtor(&body
);
1346 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot transform HttpMessage to HttpResponse (need PHP 5.1+)");
1352 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is neither of type HttpMessage::TYPE_REQUEST nor HttpMessage::TYPE_RESPONSE");
1360 /* {{{ proto int HttpMessage::count()
1361 Implements Countable::count(). Returns the number of parent messages + 1. */
1362 PHP_METHOD(HttpMessage
, count
)
1366 getObject(http_message_object
, obj
);
1368 http_message_count(i
, obj
->message
);
1374 /* {{{ proto string HttpMessage::serialize()
1375 Implements Serializable::serialize(). Returns the serialized representation of the HttpMessage. */
1376 PHP_METHOD(HttpMessage
, serialize
)
1381 getObject(http_message_object
, obj
);
1383 http_message_serialize(obj
->message
, &string
, &length
);
1384 RETURN_STRINGL(string
, length
, 0);
1389 /* {{{ proto void HttpMessage::unserialize(string serialized)
1390 Implements Serializable::unserialize(). Re-constructs the HttpMessage based upon the serialized string. */
1391 PHP_METHOD(HttpMessage
, unserialize
)
1395 getObject(http_message_object
, obj
);
1397 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &serialized
, &length
)) {
1400 http_message_dtor(obj
->message
);
1401 if ((msg
= http_message_parse_ex(obj
->message
, serialized
, (size_t) length
))) {
1404 http_error(HE_ERROR
, HTTP_E_RUNTIME
, "Could not unserialize HttpMessage");
1405 http_message_init(obj
->message
);
1411 /* {{{ proto HttpMessage HttpMessage::detach(void)
1412 Returns a clone of an HttpMessage object detached from any parent messages. */
1413 PHP_METHOD(HttpMessage
, detach
)
1417 getObject(http_message_object
, obj
);
1421 info
.type
= obj
->message
->type
;
1422 memcpy(&HTTP_INFO(&info
), &HTTP_INFO(obj
->message
), sizeof(struct http_info
));
1424 msg
= http_message_new();
1425 http_message_set_info(msg
, &info
);
1427 zend_hash_copy(&msg
->hdrs
, &obj
->message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1428 phpstr_append(&msg
->body
, PHPSTR_VAL(obj
->message
), PHPSTR_LEN(obj
->message
));
1430 RETVAL_OBJVAL(http_message_object_new_ex(Z_OBJCE_P(getThis()), msg
, NULL
), 0);
1434 /* {{{ proto void HttpMessage::prepend(HttpMessage message[, bool top = true])
1435 Prepends message(s) to the HTTP message. Throws HttpInvalidParamException if the message is located within the same message chain. */
1436 PHP_METHOD(HttpMessage
, prepend
)
1441 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O|b", &prepend
, http_message_object_ce
, &top
)) {
1442 http_message
*msg
[2];
1443 getObject(http_message_object
, obj
);
1444 getObjectEx(http_message_object
, prepend_obj
, prepend
);
1447 for (msg
[0] = obj
->message
; msg
[0]; msg
[0] = msg
[0]->parent
) {
1448 for (msg
[1] = prepend_obj
->message
; msg
[1]; msg
[1] = msg
[1]->parent
) {
1449 if (msg
[0] == msg
[1]) {
1450 http_error(HE_THROW
, HTTP_E_INVALID_PARAM
, "Cannot prepend a message located within the same message chain");
1456 http_message_object_prepend_ex(getThis(), prepend
, top
);
1461 /* {{{ proto HttpMessage HttpMessage::reverse()
1462 Reorders the message chain in reverse order. Returns the most parent HttpMessage object. */
1463 PHP_METHOD(HttpMessage
, reverse
)
1466 http_message_object_reverse(getThis(), return_value
);
1471 /* {{{ proto void HttpMessage::rewind(void)
1472 Implements Iterator::rewind(). */
1473 PHP_METHOD(HttpMessage
, rewind
)
1476 getObject(http_message_object
, obj
);
1478 if (obj
->iterator
) {
1479 zval_ptr_dtor(&obj
->iterator
);
1481 ZVAL_ADDREF(getThis());
1482 obj
->iterator
= getThis();
1487 /* {{{ proto bool HttpMessage::valid(void)
1488 Implements Iterator::valid(). */
1489 PHP_METHOD(HttpMessage
, valid
)
1492 getObject(http_message_object
, obj
);
1494 RETURN_BOOL(obj
->iterator
!= NULL
);
1499 /* {{{ proto void HttpMessage::next(void)
1500 Implements Iterator::next(). */
1501 PHP_METHOD(HttpMessage
, next
)
1504 getObject(http_message_object
, obj
);
1505 if (obj
->iterator
) {
1506 getObjectEx(http_message_object
, itr
, obj
->iterator
);
1508 if (itr
&& itr
->parent
.handle
) {
1509 zval
*old
= obj
->iterator
;
1510 MAKE_STD_ZVAL(obj
->iterator
);
1511 ZVAL_OBJVAL(obj
->iterator
, itr
->parent
, 1);
1512 zval_ptr_dtor(&old
);
1514 zval_ptr_dtor(&obj
->iterator
);
1515 obj
->iterator
= NULL
;
1522 /* {{{ proto int HttpMessage::key(void)
1523 Implements Iterator::key(). */
1524 PHP_METHOD(HttpMessage
, key
)
1527 getObject(http_message_object
, obj
);
1529 RETURN_LONG(obj
->iterator
? obj
->iterator
->value
.obj
.handle
:0);
1534 /* {{{ proto HttpMessage HttpMessage::current(void)
1535 Implements Iterator::current(). */
1536 PHP_METHOD(HttpMessage
, current
)
1539 getObject(http_message_object
, obj
);
1541 if (obj
->iterator
) {
1542 RETURN_ZVAL(obj
->iterator
, 1, 0);
1548 #endif /* ZEND_ENGINE_2 */
1555 * vim600: noet sw=4 ts=4 fdm=marker
1556 * vim<600: noet sw=4 ts=4