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-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
24 #include "php_http_api.h"
25 #include "php_http_message_api.h"
26 #include "php_http_message_object.h"
27 #include "php_http_exception_object.h"
30 # include "zend_interfaces.h"
31 # if defined(HAVE_SPL)
32 /* SPL doesn't install its headers */
33 extern PHPAPI zend_class_entry
*spl_ce_Countable
;
37 ZEND_EXTERN_MODULE_GLOBALS(http
);
39 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpMessage, method, ret_ref, req_args)
40 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpMessage, method, ret_ref)
41 #define HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, HTTP_ARGS(HttpMessage, method), visibility)
43 HTTP_BEGIN_ARGS(__construct
, 0, 0)
44 HTTP_ARG_VAL(message
, 0)
47 HTTP_BEGIN_ARGS(fromString
, 1, 1)
48 HTTP_ARG_VAL(message
, 0)
51 HTTP_EMPTY_ARGS(getBody
, 0);
52 HTTP_BEGIN_ARGS(setBody
, 0, 1)
56 HTTP_EMPTY_ARGS(getHeaders
, 0);
57 HTTP_BEGIN_ARGS(setHeaders
, 0, 1)
58 HTTP_ARG_VAL(headers
, 0)
61 HTTP_BEGIN_ARGS(addHeaders
, 0, 1)
62 HTTP_ARG_VAL(headers
, 0)
63 HTTP_ARG_VAL(append
, 0)
66 HTTP_EMPTY_ARGS(getType
, 0);
67 HTTP_BEGIN_ARGS(setType
, 0, 1)
71 HTTP_EMPTY_ARGS(getResponseCode
, 0);
72 HTTP_BEGIN_ARGS(setResponseCode
, 0, 1)
73 HTTP_ARG_VAL(response_code
, 0)
76 HTTP_EMPTY_ARGS(getRequestMethod
, 0);
77 HTTP_BEGIN_ARGS(setRequestMethod
, 0, 1)
78 HTTP_ARG_VAL(request_method
, 0)
81 HTTP_EMPTY_ARGS(getRequestUri
, 0);
82 HTTP_BEGIN_ARGS(setRequestUri
, 0, 1)
86 HTTP_EMPTY_ARGS(getHttpVersion
, 0);
87 HTTP_BEGIN_ARGS(setHttpVersion
, 0, 1)
88 HTTP_ARG_VAL(http_version
, 0)
91 HTTP_EMPTY_ARGS(getParentMessage
, 1);
92 HTTP_EMPTY_ARGS(send
, 0);
93 HTTP_BEGIN_ARGS(toString
, 0, 0)
94 HTTP_ARG_VAL(include_parent
, 0)
97 HTTP_EMPTY_ARGS(count
, 0);
99 HTTP_EMPTY_ARGS(serialize
, 0);
100 HTTP_BEGIN_ARGS(unserialize
, 0, 1)
101 HTTP_ARG_VAL(serialized
, 0)
104 #define http_message_object_declare_default_properties() _http_message_object_declare_default_properties(TSRMLS_C)
105 static inline void _http_message_object_declare_default_properties(TSRMLS_D
);
106 #define http_message_object_read_prop _http_message_object_read_prop
107 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
);
108 #define http_message_object_write_prop _http_message_object_write_prop
109 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
);
110 #define http_message_object_get_props _http_message_object_get_props
111 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
);
113 zend_class_entry
*http_message_object_ce
;
114 zend_function_entry http_message_object_fe
[] = {
115 HTTP_MESSAGE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
116 HTTP_MESSAGE_ME(getBody
, ZEND_ACC_PUBLIC
)
117 HTTP_MESSAGE_ME(setBody
, ZEND_ACC_PUBLIC
)
118 HTTP_MESSAGE_ME(getHeaders
, ZEND_ACC_PUBLIC
)
119 HTTP_MESSAGE_ME(setHeaders
, ZEND_ACC_PUBLIC
)
120 HTTP_MESSAGE_ME(addHeaders
, ZEND_ACC_PUBLIC
)
121 HTTP_MESSAGE_ME(getType
, ZEND_ACC_PUBLIC
)
122 HTTP_MESSAGE_ME(setType
, ZEND_ACC_PUBLIC
)
123 HTTP_MESSAGE_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
124 HTTP_MESSAGE_ME(setResponseCode
, ZEND_ACC_PUBLIC
)
125 HTTP_MESSAGE_ME(getRequestMethod
, ZEND_ACC_PUBLIC
)
126 HTTP_MESSAGE_ME(setRequestMethod
, ZEND_ACC_PUBLIC
)
127 HTTP_MESSAGE_ME(getRequestUri
, ZEND_ACC_PUBLIC
)
128 HTTP_MESSAGE_ME(setRequestUri
, ZEND_ACC_PUBLIC
)
129 HTTP_MESSAGE_ME(getHttpVersion
, ZEND_ACC_PUBLIC
)
130 HTTP_MESSAGE_ME(setHttpVersion
, ZEND_ACC_PUBLIC
)
131 HTTP_MESSAGE_ME(getParentMessage
, ZEND_ACC_PUBLIC
)
132 HTTP_MESSAGE_ME(send
, ZEND_ACC_PUBLIC
)
133 HTTP_MESSAGE_ME(toString
, ZEND_ACC_PUBLIC
)
135 /* implements Countable */
136 HTTP_MESSAGE_ME(count
, ZEND_ACC_PUBLIC
)
138 /* implements Serializable */
139 HTTP_MESSAGE_ME(serialize
, ZEND_ACC_PUBLIC
)
140 HTTP_MESSAGE_ME(unserialize
, ZEND_ACC_PUBLIC
)
142 ZEND_MALIAS(HttpMessage
, __toString
, toString
, HTTP_ARGS(HttpMessage
, toString
), ZEND_ACC_PUBLIC
)
144 HTTP_MESSAGE_ME(fromString
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
148 static zend_object_handlers http_message_object_handlers
;
150 PHP_MINIT_FUNCTION(http_message_object
)
152 HTTP_REGISTER_CLASS_EX(HttpMessage
, http_message_object
, NULL
, 0);
155 zend_class_implements(http_message_object_ce TSRMLS_CC
, 2, spl_ce_Countable
, zend_ce_serializable
);
157 zend_class_implements(http_message_object_ce TSRMLS_CC
, 1, zend_ce_serializable
);
161 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE
);
162 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST
);
163 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE
);
165 http_message_object_handlers
.clone_obj
= _http_message_object_clone_obj
;
166 http_message_object_handlers
.read_property
= http_message_object_read_prop
;
167 http_message_object_handlers
.write_property
= http_message_object_write_prop
;
168 http_message_object_handlers
.get_properties
= http_message_object_get_props
;
169 http_message_object_handlers
.get_property_ptr_ptr
= NULL
;
174 zend_object_value
_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
176 return http_message_object_new_ex(ce
, NULL
, NULL
);
179 zend_object_value
_http_message_object_new_ex(zend_class_entry
*ce
, http_message
*msg
, http_message_object
**ptr TSRMLS_DC
)
181 zend_object_value ov
;
182 http_message_object
*o
;
184 o
= ecalloc(1, sizeof(http_message_object
));
194 o
->parent
= http_message_object_new_ex(ce
, msg
->parent
, NULL
);
198 ALLOC_HASHTABLE(OBJ_PROP(o
));
199 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
201 ov
.handle
= putObject(http_message_object
, o
);
202 ov
.handlers
= &http_message_object_handlers
;
207 zend_object_value
_http_message_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
209 getObject(http_message_object
, obj
);
210 return http_message_object_new_ex(Z_OBJCE_P(this_ptr
), http_message_dup(obj
->message
), NULL
);
213 static inline void _http_message_object_declare_default_properties(TSRMLS_D
)
215 zend_class_entry
*ce
= http_message_object_ce
;
218 DCL_CONST(long, "TYPE_NONE", HTTP_MSG_NONE
);
219 DCL_CONST(long, "TYPE_REQUEST", HTTP_MSG_REQUEST
);
220 DCL_CONST(long, "TYPE_RESPONSE", HTTP_MSG_RESPONSE
);
223 DCL_PROP(PROTECTED
, long, type
, HTTP_MSG_NONE
);
224 DCL_PROP(PROTECTED
, string
, body
, "");
225 DCL_PROP(PROTECTED
, string
, requestMethod
, "");
226 DCL_PROP(PROTECTED
, string
, requestUri
, "");
227 DCL_PROP(PROTECTED
, long, responseCode
, 0);
228 DCL_PROP_N(PROTECTED
, httpVersion
);
229 DCL_PROP_N(PROTECTED
, headers
);
230 DCL_PROP_N(PROTECTED
, parentMessage
);
233 void _http_message_object_free(zend_object
*object TSRMLS_DC
)
235 http_message_object
*o
= (http_message_object
*) object
;
238 zend_hash_destroy(OBJ_PROP(o
));
239 FREE_HASHTABLE(OBJ_PROP(o
));
242 http_message_dtor(o
->message
);
248 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
250 getObjectEx(http_message_object
, obj
, object
);
251 http_message
*msg
= obj
->message
;
254 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1);
256 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
258 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
259 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
263 if (type
== BP_VAR_W
) {
264 zend_error(E_ERROR
, "Cannot access HttpMessage properties by reference or array key/index");
268 ALLOC_ZVAL(return_value
);
269 return_value
->refcount
= 0;
270 return_value
->is_ref
= 0;
278 case HTTP_MSG_PROPHASH_TYPE
:
279 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
280 RETVAL_LONG(msg
->type
);
283 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
284 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
285 RETVAL_DOUBLE(msg
->http
.version
);
288 case HTTP_MSG_PROPHASH_BODY
:
289 case HTTP_MSG_CHILD_PROPHASH_BODY
:
290 phpstr_fix(PHPSTR(msg
));
291 RETVAL_PHPSTR(PHPSTR(msg
), 0, 1);
294 case HTTP_MSG_PROPHASH_HEADERS
:
295 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
296 array_init(return_value
);
297 zend_hash_copy(Z_ARRVAL_P(return_value
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
300 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
301 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
303 RETVAL_OBJVAL(obj
->parent
);
309 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
310 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
311 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.method
) {
312 RETVAL_STRING(msg
->http
.info
.request
.method
, 1);
318 case HTTP_MSG_PROPHASH_REQUEST_URI
:
319 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URI
:
320 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.URI
) {
321 RETVAL_STRING(msg
->http
.info
.request
.URI
, 1);
327 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
328 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
329 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
330 RETVAL_LONG(msg
->http
.info
.response
.code
);
336 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
337 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
338 if (HTTP_MSG_TYPE(RESPONSE
, msg
) && msg
->http
.info
.response
.status
) {
339 RETVAL_STRING(msg
->http
.info
.response
.status
, 1);
347 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
357 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
)
359 getObjectEx(http_message_object
, obj
, object
);
360 http_message
*msg
= obj
->message
;
363 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
) + 1);
365 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
367 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
368 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
384 case HTTP_MSG_PROPHASH_TYPE
:
385 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
386 convert_to_long(cpy
);
387 http_message_set_type(msg
, Z_LVAL_P(cpy
));
390 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
391 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
392 convert_to_double(cpy
);
393 msg
->http
.version
= Z_DVAL_P(cpy
);
396 case HTTP_MSG_PROPHASH_BODY
:
397 case HTTP_MSG_CHILD_PROPHASH_BODY
:
398 convert_to_string(cpy
);
399 phpstr_dtor(PHPSTR(msg
));
400 phpstr_from_string_ex(PHPSTR(msg
), Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
));
403 case HTTP_MSG_PROPHASH_HEADERS
:
404 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
405 convert_to_array(cpy
);
406 zend_hash_clean(&msg
->hdrs
);
407 zend_hash_copy(&msg
->hdrs
, Z_ARRVAL_P(cpy
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
410 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
411 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
412 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), http_message_object_ce TSRMLS_CC
)) {
415 tmp
.value
.obj
= obj
->parent
;
418 Z_OBJ_ADDREF_P(value
);
419 obj
->parent
= value
->value
.obj
;
423 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
424 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
425 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
426 convert_to_string(cpy
);
427 STR_SET(msg
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
431 case HTTP_MSG_PROPHASH_REQUEST_URI
:
432 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URI
:
433 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
434 convert_to_string(cpy
);
435 STR_SET(msg
->http
.info
.request
.URI
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
439 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
440 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
441 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
442 convert_to_long(cpy
);
443 msg
->http
.info
.response
.code
= Z_LVAL_P(cpy
);
447 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
448 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
449 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
450 convert_to_string(cpy
);
451 STR_SET(msg
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
457 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
464 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
)
467 getObjectEx(http_message_object
, obj
, object
);
468 http_message
*msg
= obj
->message
;
469 HashTable
*props
= OBJ_PROP(obj
);
472 INIT_ZARR(array
, props
);
474 #define ASSOC_PROP(array, ptype, name, val) \
478 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
479 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
480 efree(m_prop_name); \
482 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
483 #define ASSOC_STRINGL(array, name, val, len) \
487 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
488 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, 1); \
489 efree(m_prop_name); \
492 ASSOC_PROP(array
, long, "type", msg
->type
);
493 ASSOC_PROP(array
, double, "httpVersion", msg
->http
.version
);
497 case HTTP_MSG_REQUEST
:
498 ASSOC_PROP(array
, long, "responseCode", 0);
499 ASSOC_STRINGL(array
, "responseStatus", "", 0);
500 ASSOC_STRING(array
, "requestMethod", msg
->http
.info
.request
.method
);
501 ASSOC_STRING(array
, "requestUri", msg
->http
.info
.request
.URI
);
504 case HTTP_MSG_RESPONSE
:
505 ASSOC_PROP(array
, long, "responseCode", msg
->http
.info
.response
.code
);
506 ASSOC_STRING(array
, "responseStatus", msg
->http
.info
.response
.status
);
507 ASSOC_STRINGL(array
, "requestMethod", "", 0);
508 ASSOC_STRINGL(array
, "requestUri", "", 0);
513 ASSOC_PROP(array
, long, "responseCode", 0);
514 ASSOC_STRINGL(array
, "responseStatus", "", 0);
515 ASSOC_STRINGL(array
, "requestMethod", "", 0);
516 ASSOC_STRINGL(array
, "requestUri", "", 0);
520 MAKE_STD_ZVAL(headers
);
522 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
523 ASSOC_PROP(array
, zval
, "headers", headers
);
524 ASSOC_STRINGL(array
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
526 return OBJ_PROP(obj
);
529 /* ### USERLAND ### */
531 /* {{{ proto void HttpMessage::__construct([string message])
533 * Instantiate a new HttpMessage object.
535 * Accepts an optional string parameter containing a single or several
536 * consecutive HTTP messages. The constructed object will actually
537 * represent the *last* message of the passed string. If there were
538 * prior messages, those can be accessed by HttpMessage::getParentMessage().
540 * Throws HttpMalformedHeaderException.
542 PHP_METHOD(HttpMessage
, __construct
)
545 char *message
= NULL
;
547 getObject(http_message_object
, obj
);
550 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
551 http_message
*msg
= obj
->message
;
553 http_message_dtor(msg
);
554 if ((obj
->message
= http_message_parse_ex(msg
, message
, length
))) {
555 if (obj
->message
->parent
) {
556 obj
->parent
= http_message_object_new_ex(Z_OBJCE_P(getThis()), obj
->message
->parent
, NULL
);
559 obj
->message
= http_message_init(msg
);
563 obj
->message
= http_message_new();
569 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
571 * Create an HttpMessage object from a string. Kind of a static constructor.
573 * Expects a string parameter containing a sinlge or several consecutive
576 * Returns an HttpMessage object on success or NULL on failure.
578 * Throws HttpMalformedHeadersException.
580 PHP_METHOD(HttpMessage
, fromString
)
584 http_message
*msg
= NULL
;
589 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &string
, &length
)) {
590 if ((msg
= http_message_parse(string
, length
))) {
591 ZVAL_OBJVAL(return_value
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
598 /* {{{ proto string HttpMessage::getBody()
600 * Get the body of the parsed HttpMessage.
602 * Returns the message body as string.
604 PHP_METHOD(HttpMessage
, getBody
)
609 getObject(http_message_object
, obj
);
610 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
615 /* {{{ proto void HttpMessage::setBody(string body)
617 * Set the body of the HttpMessage.
618 * NOTE: Don't forget to update any headers accordingly.
620 * Expects a string parameter containing the new body of the message.
622 PHP_METHOD(HttpMessage
, setBody
)
626 getObject(http_message_object
, obj
);
628 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &body
, &len
)) {
629 phpstr_dtor(PHPSTR(obj
->message
));
630 phpstr_from_string_ex(PHPSTR(obj
->message
), body
, len
);
635 /* {{{ proto array HttpMessage::getHeaders()
637 * Get Message Headers.
639 * Returns an associative array containing the messages HTTP headers.
641 PHP_METHOD(HttpMessage
, getHeaders
)
647 getObject(http_message_object
, obj
);
649 INIT_ZARR(headers
, &obj
->message
->hdrs
);
650 array_init(return_value
);
651 array_copy(&headers
, return_value
);
656 /* {{{ proto void HttpMessage::setHeaders(array headers)
660 * Expects an associative array as parameter containing the new HTTP headers,
661 * which will replace *all* previous HTTP headers of the message.
663 PHP_METHOD(HttpMessage
, setHeaders
)
665 zval
*new_headers
, old_headers
;
666 getObject(http_message_object
, obj
);
668 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
672 zend_hash_clean(&obj
->message
->hdrs
);
673 INIT_ZARR(old_headers
, &obj
->message
->hdrs
);
674 array_copy(new_headers
, &old_headers
);
678 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
680 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
682 * Expects an associative array as parameter containing the additional HTTP headers
683 * to add to the messages existing headers. If the optional bool parameter is true,
684 * and a header with the same name of one to add exists already, this respective
685 * header will be converted to an array containing both header values, otherwise
686 * it will be overwritten with the new header value.
688 PHP_METHOD(HttpMessage
, addHeaders
)
690 zval old_headers
, *new_headers
;
691 zend_bool append
= 0;
692 getObject(http_message_object
, obj
);
694 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
698 INIT_ZARR(old_headers
, &obj
->message
->hdrs
);
700 array_append(new_headers
, &old_headers
);
702 array_merge(new_headers
, &old_headers
);
707 /* {{{ proto int HttpMessage::getType()
709 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
711 * Returns the HttpMessage::TYPE.
713 PHP_METHOD(HttpMessage
, getType
)
718 getObject(http_message_object
, obj
);
719 RETURN_LONG(obj
->message
->type
);
724 /* {{{ proto void HttpMessage::setType(int type)
726 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
728 * Exptects an int parameter, the HttpMessage::TYPE.
730 PHP_METHOD(HttpMessage
, setType
)
733 getObject(http_message_object
, obj
);
735 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
738 http_message_set_type(obj
->message
, type
);
742 /* {{{ proto int HttpMessage::getResponseCode()
744 * Get the Response Code of the Message.
746 * Returns the HTTP response code if the message is of type
747 * HttpMessage::TYPE_RESPONSE, else FALSE.
749 PHP_METHOD(HttpMessage
, getResponseCode
)
754 getObject(http_message_object
, obj
);
755 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
756 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
761 /* {{{ proto bool HttpMessage::setResponseCode(int code)
763 * Set the response code of an HTTP Response Message.
765 * Expects an int parameter with the HTTP response code.
767 * Returns TRUE on success, or FALSE if the message is not of type
768 * HttpMessage::TYPE_RESPONSE or the response code is out of range (100-510).
770 PHP_METHOD(HttpMessage
, setResponseCode
)
773 getObject(http_message_object
, obj
);
775 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
777 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
780 if (code
< 100 || code
> 510) {
781 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid response code (100-510): %ld", code
);
785 obj
->message
->http
.info
.response
.code
= code
;
790 /* {{{ proto string HttpMessage::getRequestMethod()
792 * Get the Request Method of the Message.
794 * Returns the request method name on success, or FALSE if the message is
795 * not of type HttpMessage::TYPE_REQUEST.
797 PHP_METHOD(HttpMessage
, getRequestMethod
)
802 getObject(http_message_object
, obj
);
803 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
804 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
809 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
811 * Set the Request Method of the HTTP Message.
813 * Expects a string parameter containing the request method name.
815 * Returns TRUE on success, or FALSE if the message is not of type
816 * HttpMessage::TYPE_REQUEST or an invalid request method was supplied.
818 PHP_METHOD(HttpMessage
, setRequestMethod
)
822 getObject(http_message_object
, obj
);
824 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
826 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
829 if (method_len
< 1) {
830 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
833 if (SUCCESS
!= http_check_method(method
)) {
834 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unkown request method: %s", method
);
838 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
843 /* {{{ proto string HttpMessage::getRequestUri()
845 * Get the Request URI of the Message.
847 * Returns the request uri as string on success, or FALSE if the message
848 * is not of type HttpMessage::TYPE_REQUEST.
850 PHP_METHOD(HttpMessage
, getRequestUri
)
855 getObject(http_message_object
, obj
);
856 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
857 RETURN_STRING(obj
->message
->http
.info
.request
.URI
, 1);
862 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
864 * Set the Request URI of the HTTP Message.
866 * Expects a string parameters containing the request uri.
868 * Returns TRUE on success, or FALSE if the message is not of type
869 * HttpMessage::TYPE_REQUEST or supplied URI was empty.
871 PHP_METHOD(HttpMessage
, setRequestUri
)
875 getObject(http_message_object
, obj
);
877 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
880 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
882 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUri to an empty string");
886 STR_SET(obj
->message
->http
.info
.request
.URI
, estrndup(URI
, URIlen
));
891 /* {{{ proto string HttpMessage::getHttpVersion()
893 * Get the HTTP Protocol Version of the Message.
895 * Returns the HTTP protocol version as string.
897 PHP_METHOD(HttpMessage
, getHttpVersion
)
903 getObject(http_message_object
, obj
);
905 sprintf(ver
, "%1.1lf", obj
->message
->http
.version
);
906 RETURN_STRINGL(ver
, 3, 1);
911 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
913 * Set the HTTP Protocol version of the Message.
915 * Expects a string parameter containing the HTTP protocol version.
917 * Returns TRUE on success, or FALSE if supplied version is out of range (1.0/1.1).
919 PHP_METHOD(HttpMessage
, setHttpVersion
)
923 getObject(http_message_object
, obj
);
925 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
929 convert_to_double(zv
);
930 sprintf(v
, "%1.1lf", Z_DVAL_P(zv
));
931 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
932 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
936 obj
->message
->http
.version
= Z_DVAL_P(zv
);
941 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
943 * Get parent Message.
945 * Returns the parent HttpMessage on success, or NULL if there's none.
947 PHP_METHOD(HttpMessage
, getParentMessage
)
952 getObject(http_message_object
, obj
);
954 if (obj
->message
->parent
) {
955 RETVAL_OBJVAL(obj
->parent
);
963 /* {{{ proto bool HttpMessage::send()
965 * Send the Message according to its type as Response or Request.
966 * This provides limited functionality compared to HttpRequest and HttpResponse.
968 * Returns TRUE on success, or FALSE on failure.
970 PHP_METHOD(HttpMessage
, send
)
972 getObject(http_message_object
, obj
);
976 RETURN_SUCCESS(http_message_send(obj
->message
));
980 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
982 * Get the string representation of the Message.
984 * Accepts a bool parameter which specifies whether the returned string
985 * should also contain any parent messages.
987 * Returns the full message as string.
989 PHP_METHOD(HttpMessage
, toString
)
994 zend_bool include_parent
= 0;
995 getObject(http_message_object
, obj
);
997 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1001 if (include_parent
) {
1002 http_message_serialize(obj
->message
, &string
, &length
);
1004 http_message_tostring(obj
->message
, &string
, &length
);
1006 RETURN_STRINGL(string
, length
, 0);
1011 /* {{{ proto int HttpMessage::count()
1013 * Implements Countable.
1015 * Returns the number of parent messages + 1.
1017 PHP_METHOD(HttpMessage
, count
)
1022 getObject(http_message_object
, obj
);
1024 for (i
= 0, msg
= obj
->message
; msg
; msg
= msg
->parent
, ++i
);
1030 /* {{{ proto string HttpMessage::serialize()
1032 * Implements Serializable.
1034 * Returns the serialized representation of the HttpMessage.
1036 PHP_METHOD(HttpMessage
, serialize
)
1041 getObject(http_message_object
, obj
);
1043 http_message_serialize(obj
->message
, &string
, &length
);
1044 RETURN_STRINGL(string
, length
, 0);
1049 /* {{{ proto void HttpMessage::unserialize(string serialized)
1051 * Implements Serializable.
1053 * Re-constructs the HttpMessage based upon the serialized string.
1055 PHP_METHOD(HttpMessage
, unserialize
)
1059 getObject(http_message_object
, obj
);
1061 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &serialized
, &length
)) {
1062 http_message_dtor(obj
->message
);
1063 if (!http_message_parse_ex(obj
->message
, serialized
, (size_t) length
)) {
1064 http_error(HE_ERROR
, HTTP_E_RUNTIME
, "Could not unserialize HttpMessage");
1065 http_message_init(obj
->message
);
1071 #endif /* ZEND_ENGINE_2 */
1078 * vim600: noet sw=4 ts=4 fdm=marker
1079 * vim<600: noet sw=4 ts=4