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
21 #include "ext/standard/url.h"
23 #include "php_http_api.h"
24 #include "php_http_send_api.h"
25 #include "php_http_url_api.h"
26 #include "php_http_message_api.h"
27 #include "php_http_message_object.h"
28 #include "php_http_exception_object.h"
29 #include "php_http_response_object.h"
30 #include "php_http_request_method_api.h"
31 #include "php_http_request_api.h"
32 #include "php_http_request_object.h"
35 # include "zend_interfaces.h"
37 /* SPL doesn't install its headers */
38 extern PHPAPI zend_class_entry
*spl_ce_Countable
;
42 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpMessage, method, ret_ref, req_args)
43 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpMessage, method, ret_ref)
44 #define HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, HTTP_ARGS(HttpMessage, method), visibility)
46 HTTP_BEGIN_ARGS(__construct
, 0, 0)
47 HTTP_ARG_VAL(message
, 0)
50 HTTP_BEGIN_ARGS(fromString
, 1, 1)
51 HTTP_ARG_VAL(message
, 0)
54 HTTP_EMPTY_ARGS(getBody
, 0);
55 HTTP_BEGIN_ARGS(setBody
, 0, 1)
59 HTTP_EMPTY_ARGS(getHeaders
, 0);
60 HTTP_BEGIN_ARGS(setHeaders
, 0, 1)
61 HTTP_ARG_VAL(headers
, 0)
64 HTTP_BEGIN_ARGS(addHeaders
, 0, 1)
65 HTTP_ARG_VAL(headers
, 0)
66 HTTP_ARG_VAL(append
, 0)
69 HTTP_EMPTY_ARGS(getType
, 0);
70 HTTP_BEGIN_ARGS(setType
, 0, 1)
74 HTTP_EMPTY_ARGS(getResponseCode
, 0);
75 HTTP_BEGIN_ARGS(setResponseCode
, 0, 1)
76 HTTP_ARG_VAL(response_code
, 0)
79 HTTP_EMPTY_ARGS(getRequestMethod
, 0);
80 HTTP_BEGIN_ARGS(setRequestMethod
, 0, 1)
81 HTTP_ARG_VAL(request_method
, 0)
84 HTTP_EMPTY_ARGS(getRequestUrl
, 0);
85 HTTP_BEGIN_ARGS(setRequestUrl
, 0, 1)
89 HTTP_EMPTY_ARGS(getHttpVersion
, 0);
90 HTTP_BEGIN_ARGS(setHttpVersion
, 0, 1)
91 HTTP_ARG_VAL(http_version
, 0)
94 HTTP_EMPTY_ARGS(getParentMessage
, 0);
95 HTTP_EMPTY_ARGS(send
, 0);
96 HTTP_BEGIN_ARGS(toString
, 0, 0)
97 HTTP_ARG_VAL(include_parent
, 0)
100 HTTP_EMPTY_ARGS(toMessageTypeObject
, 0);
102 HTTP_EMPTY_ARGS(count
, 0);
104 HTTP_EMPTY_ARGS(serialize
, 0);
105 HTTP_BEGIN_ARGS(unserialize
, 0, 1)
106 HTTP_ARG_VAL(serialized
, 0)
109 HTTP_EMPTY_ARGS(rewind
, 0);
110 HTTP_EMPTY_ARGS(valid
, 0);
111 HTTP_EMPTY_ARGS(key
, 0);
112 HTTP_EMPTY_ARGS(current
, 0);
113 HTTP_EMPTY_ARGS(next
, 0);
115 HTTP_EMPTY_ARGS(detach
, 0);
116 HTTP_BEGIN_ARGS(prepend
, 0, 1)
117 HTTP_ARG_OBJ(HttpMessage
, message
, 0)
120 #define http_message_object_declare_default_properties() _http_message_object_declare_default_properties(TSRMLS_C)
121 static inline void _http_message_object_declare_default_properties(TSRMLS_D
);
122 #define http_message_object_read_prop _http_message_object_read_prop
123 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
);
124 #define http_message_object_write_prop _http_message_object_write_prop
125 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
);
126 #define http_message_object_get_props _http_message_object_get_props
127 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
);
129 zend_class_entry
*http_message_object_ce
;
130 zend_function_entry http_message_object_fe
[] = {
131 HTTP_MESSAGE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
132 HTTP_MESSAGE_ME(getBody
, ZEND_ACC_PUBLIC
)
133 HTTP_MESSAGE_ME(setBody
, ZEND_ACC_PUBLIC
)
134 HTTP_MESSAGE_ME(getHeaders
, ZEND_ACC_PUBLIC
)
135 HTTP_MESSAGE_ME(setHeaders
, ZEND_ACC_PUBLIC
)
136 HTTP_MESSAGE_ME(addHeaders
, ZEND_ACC_PUBLIC
)
137 HTTP_MESSAGE_ME(getType
, ZEND_ACC_PUBLIC
)
138 HTTP_MESSAGE_ME(setType
, ZEND_ACC_PUBLIC
)
139 HTTP_MESSAGE_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
140 HTTP_MESSAGE_ME(setResponseCode
, ZEND_ACC_PUBLIC
)
141 HTTP_MESSAGE_ME(getRequestMethod
, ZEND_ACC_PUBLIC
)
142 HTTP_MESSAGE_ME(setRequestMethod
, ZEND_ACC_PUBLIC
)
143 HTTP_MESSAGE_ME(getRequestUrl
, ZEND_ACC_PUBLIC
)
144 HTTP_MESSAGE_ME(setRequestUrl
, ZEND_ACC_PUBLIC
)
145 HTTP_MESSAGE_ME(getHttpVersion
, ZEND_ACC_PUBLIC
)
146 HTTP_MESSAGE_ME(setHttpVersion
, ZEND_ACC_PUBLIC
)
147 HTTP_MESSAGE_ME(getParentMessage
, ZEND_ACC_PUBLIC
)
148 HTTP_MESSAGE_ME(send
, ZEND_ACC_PUBLIC
)
149 HTTP_MESSAGE_ME(toString
, ZEND_ACC_PUBLIC
)
150 HTTP_MESSAGE_ME(toMessageTypeObject
, ZEND_ACC_PUBLIC
)
152 /* implements Countable */
153 HTTP_MESSAGE_ME(count
, ZEND_ACC_PUBLIC
)
155 /* implements Serializable */
156 HTTP_MESSAGE_ME(serialize
, ZEND_ACC_PUBLIC
)
157 HTTP_MESSAGE_ME(unserialize
, ZEND_ACC_PUBLIC
)
159 /* implements Iterator */
160 HTTP_MESSAGE_ME(rewind
, ZEND_ACC_PUBLIC
)
161 HTTP_MESSAGE_ME(valid
, ZEND_ACC_PUBLIC
)
162 HTTP_MESSAGE_ME(current
, ZEND_ACC_PUBLIC
)
163 HTTP_MESSAGE_ME(key
, ZEND_ACC_PUBLIC
)
164 HTTP_MESSAGE_ME(next
, ZEND_ACC_PUBLIC
)
166 ZEND_MALIAS(HttpMessage
, __toString
, toString
, HTTP_ARGS(HttpMessage
, toString
), ZEND_ACC_PUBLIC
)
168 HTTP_MESSAGE_ME(fromString
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
170 HTTP_MESSAGE_ME(detach
, ZEND_ACC_PUBLIC
)
171 HTTP_MESSAGE_ME(prepend
, ZEND_ACC_PUBLIC
)
175 static zend_object_handlers http_message_object_handlers
;
177 PHP_MINIT_FUNCTION(http_message_object
)
179 HTTP_REGISTER_CLASS_EX(HttpMessage
, http_message_object
, NULL
, 0);
182 zend_class_implements(http_message_object_ce TSRMLS_CC
, 3, spl_ce_Countable
, zend_ce_serializable
, zend_ce_iterator
);
184 zend_class_implements(http_message_object_ce TSRMLS_CC
, 2, zend_ce_serializable
, zend_ce_iterator
);
187 zend_class_implements(http_message_object_ce TSRMLS_CC
, 1, zend_ce_iterator
);
190 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE
);
191 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST
);
192 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE
);
194 http_message_object_handlers
.clone_obj
= _http_message_object_clone_obj
;
195 http_message_object_handlers
.read_property
= http_message_object_read_prop
;
196 http_message_object_handlers
.write_property
= http_message_object_write_prop
;
197 http_message_object_handlers
.get_properties
= http_message_object_get_props
;
198 http_message_object_handlers
.get_property_ptr_ptr
= NULL
;
203 zend_object_value
_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
205 return http_message_object_new_ex(ce
, NULL
, NULL
);
208 zend_object_value
_http_message_object_new_ex(zend_class_entry
*ce
, http_message
*msg
, http_message_object
**ptr TSRMLS_DC
)
210 zend_object_value ov
;
211 http_message_object
*o
;
213 o
= ecalloc(1, sizeof(http_message_object
));
223 o
->parent
= http_message_object_new_ex(ce
, msg
->parent
, NULL
);
227 ALLOC_HASHTABLE(OBJ_PROP(o
));
228 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
230 ov
.handle
= putObject(http_message_object
, o
);
231 ov
.handlers
= &http_message_object_handlers
;
236 zend_object_value
_http_message_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
238 getObject(http_message_object
, obj
);
239 return http_message_object_new_ex(Z_OBJCE_P(this_ptr
), http_message_dup(obj
->message
), NULL
);
242 static inline void _http_message_object_declare_default_properties(TSRMLS_D
)
244 zend_class_entry
*ce
= http_message_object_ce
;
247 DCL_CONST(long, "TYPE_NONE", HTTP_MSG_NONE
);
248 DCL_CONST(long, "TYPE_REQUEST", HTTP_MSG_REQUEST
);
249 DCL_CONST(long, "TYPE_RESPONSE", HTTP_MSG_RESPONSE
);
252 DCL_PROP(PROTECTED
, long, type
, HTTP_MSG_NONE
);
253 DCL_PROP(PROTECTED
, string
, body
, "");
254 DCL_PROP(PROTECTED
, string
, requestMethod
, "");
255 DCL_PROP(PROTECTED
, string
, requestUrl
, "");
256 DCL_PROP(PROTECTED
, long, responseCode
, 0);
257 DCL_PROP_N(PROTECTED
, httpVersion
);
258 DCL_PROP_N(PROTECTED
, headers
);
259 DCL_PROP_N(PROTECTED
, parentMessage
);
262 void _http_message_object_free(zend_object
*object TSRMLS_DC
)
264 http_message_object
*o
= (http_message_object
*) object
;
267 zend_hash_destroy(OBJ_PROP(o
));
268 FREE_HASHTABLE(OBJ_PROP(o
));
271 http_message_dtor(o
->message
);
274 if (o
->parent
.handle
) {
279 p
.value
.obj
= o
->parent
;
280 zend_objects_store_del_ref(&p TSRMLS_CC
);
285 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
287 getObjectEx(http_message_object
, obj
, object
);
288 http_message
*msg
= obj
->message
;
291 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1);
293 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
295 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
296 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
300 if (type
== BP_VAR_W
) {
301 zend_error(E_ERROR
, "Cannot access HttpMessage properties by reference or array key/index");
305 ALLOC_ZVAL(return_value
);
306 return_value
->refcount
= 0;
307 return_value
->is_ref
= 0;
315 case HTTP_MSG_PROPHASH_TYPE
:
316 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
317 RETVAL_LONG(msg
->type
);
320 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
321 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
322 RETVAL_DOUBLE(msg
->http
.version
);
325 case HTTP_MSG_PROPHASH_BODY
:
326 case HTTP_MSG_CHILD_PROPHASH_BODY
:
327 phpstr_fix(PHPSTR(msg
));
328 RETVAL_PHPSTR(PHPSTR(msg
), 0, 1);
331 case HTTP_MSG_PROPHASH_HEADERS
:
332 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
333 array_init(return_value
);
334 zend_hash_copy(Z_ARRVAL_P(return_value
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
337 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
338 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
340 RETVAL_OBJVAL(obj
->parent
, 1);
346 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
347 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
348 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.method
) {
349 RETVAL_STRING(msg
->http
.info
.request
.method
, 1);
355 case HTTP_MSG_PROPHASH_REQUEST_URL
:
356 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL
:
357 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.url
) {
358 RETVAL_STRING(msg
->http
.info
.request
.url
, 1);
364 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
365 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
366 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
367 RETVAL_LONG(msg
->http
.info
.response
.code
);
373 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
374 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
375 if (HTTP_MSG_TYPE(RESPONSE
, msg
) && msg
->http
.info
.response
.status
) {
376 RETVAL_STRING(msg
->http
.info
.response
.status
, 1);
384 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
394 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
)
396 getObjectEx(http_message_object
, obj
, object
);
397 http_message
*msg
= obj
->message
;
400 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
) + 1);
402 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
404 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
405 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
410 cpy
= zval_copy(Z_TYPE_P(value
), value
);
418 case HTTP_MSG_PROPHASH_TYPE
:
419 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
420 convert_to_long(cpy
);
421 http_message_set_type(msg
, Z_LVAL_P(cpy
));
424 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
425 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
426 convert_to_double(cpy
);
427 msg
->http
.version
= Z_DVAL_P(cpy
);
430 case HTTP_MSG_PROPHASH_BODY
:
431 case HTTP_MSG_CHILD_PROPHASH_BODY
:
432 convert_to_string(cpy
);
433 phpstr_dtor(PHPSTR(msg
));
434 phpstr_from_string_ex(PHPSTR(msg
), Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
));
437 case HTTP_MSG_PROPHASH_HEADERS
:
438 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
439 convert_to_array(cpy
);
440 zend_hash_clean(&msg
->hdrs
);
441 zend_hash_copy(&msg
->hdrs
, Z_ARRVAL_P(cpy
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
444 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
445 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
446 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), http_message_object_ce TSRMLS_CC
)) {
449 tmp
.value
.obj
= obj
->parent
;
452 Z_OBJ_ADDREF_P(value
);
453 obj
->parent
= value
->value
.obj
;
457 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
458 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
459 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
460 convert_to_string(cpy
);
461 STR_SET(msg
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
465 case HTTP_MSG_PROPHASH_REQUEST_URL
:
466 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL
:
467 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
468 convert_to_string(cpy
);
469 STR_SET(msg
->http
.info
.request
.url
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
473 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
474 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
475 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
476 convert_to_long(cpy
);
477 msg
->http
.info
.response
.code
= Z_LVAL_P(cpy
);
481 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
482 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
483 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
484 convert_to_string(cpy
);
485 STR_SET(msg
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
491 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
498 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
)
501 getObjectEx(http_message_object
, obj
, object
);
502 http_message
*msg
= obj
->message
;
503 HashTable
*props
= OBJ_PROP(obj
);
506 INIT_ZARR(array
, props
);
508 #define ASSOC_PROP(array, ptype, name, val) \
512 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
513 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
514 efree(m_prop_name); \
516 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
517 #define ASSOC_STRINGL(array, name, val, len) \
521 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
522 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, 1); \
523 efree(m_prop_name); \
526 ASSOC_PROP(array
, long, "type", msg
->type
);
527 ASSOC_PROP(array
, double, "httpVersion", msg
->http
.version
);
531 case HTTP_MSG_REQUEST
:
532 ASSOC_PROP(array
, long, "responseCode", 0);
533 ASSOC_STRINGL(array
, "responseStatus", "", 0);
534 ASSOC_STRING(array
, "requestMethod", msg
->http
.info
.request
.method
);
535 ASSOC_STRING(array
, "requestUrl", msg
->http
.info
.request
.url
);
538 case HTTP_MSG_RESPONSE
:
539 ASSOC_PROP(array
, long, "responseCode", msg
->http
.info
.response
.code
);
540 ASSOC_STRING(array
, "responseStatus", msg
->http
.info
.response
.status
);
541 ASSOC_STRINGL(array
, "requestMethod", "", 0);
542 ASSOC_STRINGL(array
, "requestUrl", "", 0);
547 ASSOC_PROP(array
, long, "responseCode", 0);
548 ASSOC_STRINGL(array
, "responseStatus", "", 0);
549 ASSOC_STRINGL(array
, "requestMethod", "", 0);
550 ASSOC_STRINGL(array
, "requestUrl", "", 0);
554 MAKE_STD_ZVAL(headers
);
556 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
557 ASSOC_PROP(array
, zval
, "headers", headers
);
558 ASSOC_STRINGL(array
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
560 return OBJ_PROP(obj
);
563 /* ### USERLAND ### */
565 /* {{{ proto void HttpMessage::__construct([string message])
567 * Instantiate a new HttpMessage object.
569 * Accepts an optional string parameter containing a single or several
570 * consecutive HTTP messages. The constructed object will actually
571 * represent the *last* message of the passed string. If there were
572 * prior messages, those can be accessed by HttpMessage::getParentMessage().
574 * Throws HttpMalformedHeaderException.
576 PHP_METHOD(HttpMessage
, __construct
)
579 char *message
= NULL
;
581 getObject(http_message_object
, obj
);
584 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
585 http_message
*msg
= obj
->message
;
587 http_message_dtor(msg
);
588 if ((obj
->message
= http_message_parse_ex(msg
, message
, length
))) {
589 if (obj
->message
->parent
) {
590 obj
->parent
= http_message_object_new_ex(Z_OBJCE_P(getThis()), obj
->message
->parent
, NULL
);
593 obj
->message
= http_message_init(msg
);
597 obj
->message
= http_message_new();
603 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message[, string class_name = "HttpMessage"])
605 * Create an HttpMessage object from a string. Kind of a static constructor.
607 * Expects a string parameter containing a sinlge or several consecutive
608 * HTTP messages. Accepts an optionsl string parameter specifying the class to use.
610 * Returns an HttpMessage object on success or NULL on failure.
612 * Throws HttpMalformedHeadersException.
614 PHP_METHOD(HttpMessage
, fromString
)
616 char *string
= NULL
, *class_name
= NULL
;
617 int length
= 0, class_length
= 0;
618 http_message
*msg
= NULL
;
623 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|s", &string
, &length
, &class_name
, &class_length
)) {
624 if ((msg
= http_message_parse(string
, length
))) {
625 zend_class_entry
*ce
= http_message_object_ce
;
627 if (class_name
&& *class_name
) {
628 ce
= zend_fetch_class(class_name
, class_length
, ZEND_FETCH_CLASS_DEFAULT TSRMLS_CC
);
629 if (ce
&& !instanceof_function(ce
, http_message_object_ce TSRMLS_CC
)) {
630 http_error_ex(HE_WARNING
, HTTP_E_RUNTIME
, "Class %s does not extend HttpMessage", class_name
);
635 RETVAL_OBJVAL(http_message_object_new_ex(ce
, msg
, NULL
), 0);
643 /* {{{ proto string HttpMessage::getBody()
645 * Get the body of the parsed HttpMessage.
647 * Returns the message body as string.
649 PHP_METHOD(HttpMessage
, getBody
)
654 getObject(http_message_object
, obj
);
655 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
660 /* {{{ proto void HttpMessage::setBody(string body)
662 * Set the body of the HttpMessage.
663 * NOTE: Don't forget to update any headers accordingly.
665 * Expects a string parameter containing the new body of the message.
667 PHP_METHOD(HttpMessage
, setBody
)
671 getObject(http_message_object
, obj
);
673 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &body
, &len
)) {
674 phpstr_dtor(PHPSTR(obj
->message
));
675 phpstr_from_string_ex(PHPSTR(obj
->message
), body
, len
);
680 /* {{{ proto array HttpMessage::getHeaders()
682 * Get Message Headers.
684 * Returns an associative array containing the messages HTTP headers.
686 PHP_METHOD(HttpMessage
, getHeaders
)
692 getObject(http_message_object
, obj
);
694 INIT_ZARR(headers
, &obj
->message
->hdrs
);
695 array_init(return_value
);
696 array_copy(&headers
, return_value
);
701 /* {{{ proto void HttpMessage::setHeaders(array headers)
705 * Expects an associative array as parameter containing the new HTTP headers,
706 * which will replace *all* previous HTTP headers of the message.
708 PHP_METHOD(HttpMessage
, setHeaders
)
710 zval
*new_headers
, old_headers
;
711 getObject(http_message_object
, obj
);
713 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
717 zend_hash_clean(&obj
->message
->hdrs
);
718 INIT_ZARR(old_headers
, &obj
->message
->hdrs
);
719 array_copy(new_headers
, &old_headers
);
723 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
725 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
727 * Expects an associative array as parameter containing the additional HTTP headers
728 * to add to the messages existing headers. If the optional bool parameter is true,
729 * and a header with the same name of one to add exists already, this respective
730 * header will be converted to an array containing both header values, otherwise
731 * it will be overwritten with the new header value.
733 PHP_METHOD(HttpMessage
, addHeaders
)
735 zval old_headers
, *new_headers
;
736 zend_bool append
= 0;
737 getObject(http_message_object
, obj
);
739 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
743 INIT_ZARR(old_headers
, &obj
->message
->hdrs
);
745 array_append(new_headers
, &old_headers
);
747 array_merge(new_headers
, &old_headers
);
752 /* {{{ proto int HttpMessage::getType()
754 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
756 * Returns the HttpMessage::TYPE.
758 PHP_METHOD(HttpMessage
, getType
)
763 getObject(http_message_object
, obj
);
764 RETURN_LONG(obj
->message
->type
);
769 /* {{{ proto void HttpMessage::setType(int type)
771 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
773 * Exptects an int parameter, the HttpMessage::TYPE.
775 PHP_METHOD(HttpMessage
, setType
)
778 getObject(http_message_object
, obj
);
780 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
783 http_message_set_type(obj
->message
, type
);
787 /* {{{ proto int HttpMessage::getResponseCode()
789 * Get the Response Code of the Message.
791 * Returns the HTTP response code if the message is of type
792 * HttpMessage::TYPE_RESPONSE, else FALSE.
794 PHP_METHOD(HttpMessage
, getResponseCode
)
799 getObject(http_message_object
, obj
);
800 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
801 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
806 /* {{{ proto bool HttpMessage::setResponseCode(int code)
808 * Set the response code of an HTTP Response Message.
810 * Expects an int parameter with the HTTP response code.
812 * Returns TRUE on success, or FALSE if the message is not of type
813 * HttpMessage::TYPE_RESPONSE or the response code is out of range (100-510).
815 PHP_METHOD(HttpMessage
, setResponseCode
)
818 getObject(http_message_object
, obj
);
820 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
822 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
825 if (code
< 100 || code
> 510) {
826 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid response code (100-510): %ld", code
);
830 obj
->message
->http
.info
.response
.code
= code
;
835 /* {{{ proto string HttpMessage::getRequestMethod()
837 * Get the Request Method of the Message.
839 * Returns the request method name on success, or FALSE if the message is
840 * not of type HttpMessage::TYPE_REQUEST.
842 PHP_METHOD(HttpMessage
, getRequestMethod
)
847 getObject(http_message_object
, obj
);
848 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
849 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
854 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
856 * Set the Request Method of the HTTP Message.
858 * Expects a string parameter containing the request method name.
860 * Returns TRUE on success, or FALSE if the message is not of type
861 * HttpMessage::TYPE_REQUEST or an invalid request method was supplied.
863 PHP_METHOD(HttpMessage
, setRequestMethod
)
867 getObject(http_message_object
, obj
);
869 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
871 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
874 if (method_len
< 1) {
875 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
878 if (SUCCESS
!= http_check_method(method
)) {
879 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unkown request method: %s", method
);
883 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
888 /* {{{ proto string HttpMessage::getRequestUrl()
890 * Get the Request URL of the Message.
892 * Returns the request url as string on success, or FALSE if the message
893 * is not of type HttpMessage::TYPE_REQUEST.
895 PHP_METHOD(HttpMessage
, getRequestUrl
)
900 getObject(http_message_object
, obj
);
901 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
902 RETURN_STRING(obj
->message
->http
.info
.request
.url
, 1);
907 /* {{{ proto bool HttpMessage::setRequestUrl(string url)
909 * Set the Request URL of the HTTP Message.
911 * Expects a string parameters containing the request url.
913 * Returns TRUE on success, or FALSE if the message is not of type
914 * HttpMessage::TYPE_REQUEST or supplied URL was empty.
916 PHP_METHOD(HttpMessage
, setRequestUrl
)
920 getObject(http_message_object
, obj
);
922 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
925 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
927 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUrl to an empty string");
931 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(URI
, URIlen
));
936 /* {{{ proto string HttpMessage::getHttpVersion()
938 * Get the HTTP Protocol Version of the Message.
940 * Returns the HTTP protocol version as string.
942 PHP_METHOD(HttpMessage
, getHttpVersion
)
948 getObject(http_message_object
, obj
);
950 sprintf(ver
, "%1.1lf", obj
->message
->http
.version
);
951 RETURN_STRINGL(ver
, 3, 1);
956 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
958 * Set the HTTP Protocol version of the Message.
960 * Expects a string parameter containing the HTTP protocol version.
962 * Returns TRUE on success, or FALSE if supplied version is out of range (1.0/1.1).
964 PHP_METHOD(HttpMessage
, setHttpVersion
)
968 getObject(http_message_object
, obj
);
970 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
974 convert_to_double(zv
);
975 sprintf(v
, "%1.1lf", Z_DVAL_P(zv
));
976 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
977 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
981 obj
->message
->http
.version
= Z_DVAL_P(zv
);
986 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
988 * Get parent Message.
990 * Returns the parent HttpMessage on success, or NULL if there's none.
992 PHP_METHOD(HttpMessage
, getParentMessage
)
997 getObject(http_message_object
, obj
);
999 if (obj
->message
->parent
) {
1000 RETVAL_OBJVAL(obj
->parent
, 1);
1008 /* {{{ proto bool HttpMessage::send()
1010 * Send the Message according to its type as Response or Request.
1011 * This provides limited functionality compared to HttpRequest and HttpResponse.
1013 * Returns TRUE on success, or FALSE on failure.
1015 PHP_METHOD(HttpMessage
, send
)
1017 getObject(http_message_object
, obj
);
1021 RETURN_SUCCESS(http_message_send(obj
->message
));
1025 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
1027 * Get the string representation of the Message.
1029 * Accepts a bool parameter which specifies whether the returned string
1030 * should also contain any parent messages.
1032 * Returns the full message as string.
1034 PHP_METHOD(HttpMessage
, toString
)
1039 zend_bool include_parent
= 0;
1040 getObject(http_message_object
, obj
);
1042 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1046 if (include_parent
) {
1047 http_message_serialize(obj
->message
, &string
, &length
);
1049 http_message_tostring(obj
->message
, &string
, &length
);
1051 RETURN_STRINGL(string
, length
, 0);
1056 /* {{{ proto HttpRequest|HttpResponse HttpMessage::toMessageTypeObject(void)
1058 * Creates an object regarding to the type of the message.
1060 * Returns either an HttpRequest or HttpResponse object on success, or NULL on failure.
1062 * Throws HttpRuntimeException, HttpMessageTypeException, HttpHeaderException.
1064 PHP_METHOD(HttpMessage
, toMessageTypeObject
)
1066 SET_EH_THROW_HTTP();
1071 getObject(http_message_object
, obj
);
1073 switch (obj
->message
->type
)
1075 case HTTP_MSG_REQUEST
:
1077 #ifdef HTTP_HAVE_CURL
1080 zval tmp
, body
, *array
, *headers
, *host
= http_message_header(obj
->message
, "Host");
1081 php_url hurl
, *purl
= php_url_parse(obj
->message
->http
.info
.request
.url
);
1083 MAKE_STD_ZVAL(array
);
1086 memset(&hurl
, 0, sizeof(php_url
));
1087 hurl
.host
= host
? Z_STRVAL_P(host
) : NULL
;
1088 http_build_url(HTTP_URL_REPLACE
, purl
, &hurl
, NULL
, &url
, NULL
);
1090 add_assoc_string(array
, "url", url
, 0);
1092 if ( (method
= http_request_method_exists(1, 0, obj
->message
->http
.info
.request
.method
)) ||
1093 (method
= http_request_method_register(obj
->message
->http
.info
.request
.method
, strlen(obj
->message
->http
.info
.request
.method
)))) {
1094 add_assoc_long(array
, "method", method
);
1097 if (10 == (int) (obj
->message
->http
.version
* 10)) {
1098 add_assoc_long(array
, "protocol", CURL_HTTP_VERSION_1_0
);
1101 MAKE_STD_ZVAL(headers
);
1102 array_init(headers
);
1103 INIT_ZARR(tmp
, &obj
->message
->hdrs
);
1104 array_copy(&tmp
, headers
);
1105 add_assoc_zval(array
, "headers", headers
);
1107 object_init_ex(return_value
, http_request_object_ce
);
1108 zend_call_method_with_1_params(&return_value
, http_request_object_ce
, NULL
, "setoptions", NULL
, array
);
1109 zval_ptr_dtor(&array
);
1112 ZVAL_STRINGL(&body
, PHPSTR_VAL(obj
->message
), PHPSTR_LEN(obj
->message
), 0);
1113 zend_call_method_with_1_params(&return_value
, http_request_object_ce
, NULL
, "setrawpostdata", NULL
, &body
);
1115 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot transform HttpMessage to HttpRequest (missing curl support)");
1120 case HTTP_MSG_RESPONSE
:
1123 HashPosition pos1
, pos2
;
1127 zval
**header
, **h
, *body
;
1129 if (obj
->message
->http
.info
.response
.code
) {
1130 http_send_status(obj
->message
->http
.info
.response
.code
);
1133 object_init_ex(return_value
, http_response_object_ce
);
1135 FOREACH_HASH_KEYLENVAL(pos1
, &obj
->message
->hdrs
, key
, key_len
, idx
, header
) {
1140 ZVAL_STRINGL(&zkey
, key
, key_len
, 0);
1142 switch (Z_TYPE_PP(header
))
1146 FOREACH_HASH_VAL(pos2
, HASH_OF(*header
), h
) {
1148 zend_call_method_with_2_params(&return_value
, http_response_object_ce
, NULL
, "setheader", NULL
, &zkey
, *h
);
1154 ZVAL_ADDREF(*header
);
1155 zend_call_method_with_2_params(&return_value
, http_response_object_ce
, NULL
, "setheader", NULL
, &zkey
, *header
);
1156 zval_ptr_dtor(header
);
1163 MAKE_STD_ZVAL(body
);
1164 ZVAL_STRINGL(body
, PHPSTR_VAL(obj
->message
), PHPSTR_LEN(obj
->message
), 1);
1165 zend_call_method_with_1_params(&return_value
, http_response_object_ce
, NULL
, "setdata", NULL
, body
);
1166 zval_ptr_dtor(&body
);
1168 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot transform HttpMessage to HttpResponse (need PHP 5.1+)");
1174 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is neither of type HttpMessage::TYPE_REQUEST nor HttpMessage::TYPE_RESPONSE");
1182 /* {{{ proto int HttpMessage::count()
1184 * Implements Countable.
1186 * Returns the number of parent messages + 1.
1188 PHP_METHOD(HttpMessage
, count
)
1193 getObject(http_message_object
, obj
);
1195 for (i
= 0, msg
= obj
->message
; msg
; msg
= msg
->parent
, ++i
);
1201 /* {{{ proto string HttpMessage::serialize()
1203 * Implements Serializable.
1205 * Returns the serialized representation of the HttpMessage.
1207 PHP_METHOD(HttpMessage
, serialize
)
1212 getObject(http_message_object
, obj
);
1214 http_message_serialize(obj
->message
, &string
, &length
);
1215 RETURN_STRINGL(string
, length
, 0);
1220 /* {{{ proto void HttpMessage::unserialize(string serialized)
1222 * Implements Serializable.
1224 * Re-constructs the HttpMessage based upon the serialized string.
1226 PHP_METHOD(HttpMessage
, unserialize
)
1230 getObject(http_message_object
, obj
);
1232 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &serialized
, &length
)) {
1233 http_message_dtor(obj
->message
);
1234 if (!http_message_parse_ex(obj
->message
, serialized
, (size_t) length
)) {
1235 http_error(HE_ERROR
, HTTP_E_RUNTIME
, "Could not unserialize HttpMessage");
1236 http_message_init(obj
->message
);
1242 /* {{{ proto HttpMessage HttpMessage::detach(void)
1244 * Returns a clone of an HttpMessage object detached from any parent messages.
1246 PHP_METHOD(HttpMessage
, detach
)
1250 getObject(http_message_object
, obj
);
1254 info
.type
= obj
->message
->type
;
1255 memcpy(&HTTP_INFO(&info
), &HTTP_INFO(obj
->message
), sizeof(struct http_info
));
1257 msg
= http_message_new();
1258 http_message_set_info(msg
, &info
);
1260 zend_hash_copy(&msg
->hdrs
, &obj
->message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1261 phpstr_append(&msg
->body
, PHPSTR_VAL(obj
->message
), PHPSTR_LEN(obj
->message
));
1263 RETVAL_OBJVAL(http_message_object_new_ex(Z_OBJCE_P(getThis()), msg
, NULL
), 0);
1267 /* {{{ proto void HttpMessage::prepend(HttpMessage message)
1269 * Prepends message(s) to the HTTP message.
1271 * Expects an HttpMessage object as parameter.
1273 PHP_METHOD(HttpMessage
, prepend
)
1278 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O|b", &prepend
, http_message_object_ce
, &top
)) {
1280 http_message
*save_parent_msg
;
1281 zend_object_value save_parent_obj
;
1282 getObject(http_message_object
, obj
);
1283 getObjectEx(http_message_object
, prepend_obj
, prepend
);
1289 save_parent_obj
= obj
->parent
;
1290 save_parent_msg
= obj
->message
->parent
;
1292 /* iterate to the most parent object */
1293 while (obj
->parent
.handle
) {
1294 m
.value
.obj
= obj
->parent
;
1295 obj
= zend_object_store_get_object(&m TSRMLS_CC
);
1300 obj
->parent
= prepend
->value
.obj
;
1301 obj
->message
->parent
= prepend_obj
->message
;
1304 zend_objects_store_add_ref(prepend TSRMLS_CC
);
1305 while (prepend_obj
->parent
.handle
) {
1306 m
.value
.obj
= prepend_obj
->parent
;
1307 zend_objects_store_add_ref(&m TSRMLS_CC
);
1308 prepend_obj
= zend_object_store_get_object(&m TSRMLS_CC
);
1312 prepend_obj
->parent
= save_parent_obj
;
1313 prepend_obj
->message
->parent
= save_parent_msg
;
1319 /* {{{ proto void HttpMessage::rewind(void)
1321 * Implements Iterator.
1323 PHP_METHOD(HttpMessage
, rewind
)
1326 getObject(http_message_object
, obj
);
1328 if (obj
->iterator
) {
1329 zval_ptr_dtor(&obj
->iterator
);
1331 ZVAL_ADDREF(getThis());
1332 obj
->iterator
= getThis();
1337 /* {{{ proto bool HttpMessage::valid(void)
1339 * Implements Iterator.
1341 PHP_METHOD(HttpMessage
, valid
)
1344 getObject(http_message_object
, obj
);
1346 RETURN_BOOL(obj
->iterator
!= NULL
);
1351 /* {{{ proto void HttpMessage::next(void)
1353 * Implements Iterator.
1355 PHP_METHOD(HttpMessage
, next
)
1358 getObject(http_message_object
, obj
);
1359 getObjectEx(http_message_object
, itr
, obj
->iterator
);
1361 if (itr
&& itr
->parent
.handle
) {
1362 zval
*old
= obj
->iterator
;
1363 MAKE_STD_ZVAL(obj
->iterator
);
1364 ZVAL_OBJVAL(obj
->iterator
, itr
->parent
, 1);
1365 zval_ptr_dtor(&old
);
1367 zval_ptr_dtor(&obj
->iterator
);
1368 obj
->iterator
= NULL
;
1374 /* {{{ proto int HttpMessage::key(void)
1376 * Implements Iterator.
1378 PHP_METHOD(HttpMessage
, key
)
1381 getObject(http_message_object
, obj
);
1383 RETURN_LONG(obj
->iterator
? obj
->iterator
->value
.obj
.handle
:0);
1388 /* {{{ proto HttpMessage HttpMessage::current(void)
1390 * Implements Iterator.
1392 PHP_METHOD(HttpMessage
, current
)
1395 getObject(http_message_object
, obj
);
1397 if (obj
->iterator
) {
1398 RETURN_ZVAL(obj
->iterator
, 1, 0);
1404 #endif /* ZEND_ENGINE_2 */
1411 * vim600: noet sw=4 ts=4 fdm=marker
1412 * vim<600: noet sw=4 ts=4