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_std_defs.h"
26 #include "php_http_message_object.h"
27 #include "php_http_exception_object.h"
29 #include "phpstr/phpstr.h"
33 # include "zend_interfaces.h"
36 # include "ext/spl/spl_array.h"
39 ZEND_EXTERN_MODULE_GLOBALS(http
);
41 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpMessage, method, ret_ref, req_args)
42 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpMessage, method, ret_ref)
43 #define HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, HTTP_ARGS(HttpMessage, method), visibility)
45 HTTP_BEGIN_ARGS(__construct
, 0, 0)
46 HTTP_ARG_VAL(message
, 0)
49 HTTP_BEGIN_ARGS(fromString
, 1, 1)
50 HTTP_ARG_VAL(message
, 0)
53 HTTP_EMPTY_ARGS(getBody
, 0);
54 HTTP_BEGIN_ARGS(setBody
, 0, 1)
58 HTTP_EMPTY_ARGS(getHeaders
, 0);
59 HTTP_BEGIN_ARGS(setHeaders
, 0, 1)
60 HTTP_ARG_VAL(headers
, 0)
63 HTTP_BEGIN_ARGS(addHeaders
, 0, 1)
64 HTTP_ARG_VAL(headers
, 0)
65 HTTP_ARG_VAL(append
, 0)
68 HTTP_EMPTY_ARGS(getType
, 0);
69 HTTP_BEGIN_ARGS(setType
, 0, 1)
73 HTTP_EMPTY_ARGS(getResponseCode
, 0);
74 HTTP_BEGIN_ARGS(setResponseCode
, 0, 1)
75 HTTP_ARG_VAL(response_code
, 0)
78 HTTP_EMPTY_ARGS(getRequestMethod
, 0);
79 HTTP_BEGIN_ARGS(setRequestMethod
, 0, 1)
80 HTTP_ARG_VAL(request_method
, 0)
83 HTTP_EMPTY_ARGS(getRequestUri
, 0);
84 HTTP_BEGIN_ARGS(setRequestUri
, 0, 1)
88 HTTP_EMPTY_ARGS(getHttpVersion
, 0);
89 HTTP_BEGIN_ARGS(setHttpVersion
, 0, 1)
90 HTTP_ARG_VAL(http_version
, 0)
93 HTTP_EMPTY_ARGS(getParentMessage
, 1);
94 HTTP_EMPTY_ARGS(send
, 0);
95 HTTP_BEGIN_ARGS(toString
, 0, 0)
96 HTTP_ARG_VAL(include_parent
, 0)
99 HTTP_EMPTY_ARGS(count
, 0);
101 HTTP_EMPTY_ARGS(serialize
, 0);
102 HTTP_BEGIN_ARGS(unserialize
, 0, 1)
103 HTTP_ARG_VAL(serialized
, 0)
106 #define http_message_object_declare_default_properties() _http_message_object_declare_default_properties(TSRMLS_C)
107 static inline void _http_message_object_declare_default_properties(TSRMLS_D
);
108 #define http_message_object_read_prop _http_message_object_read_prop
109 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
);
110 #define http_message_object_write_prop _http_message_object_write_prop
111 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
);
112 #define http_message_object_get_props _http_message_object_get_props
113 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
);
115 zend_class_entry
*http_message_object_ce
;
116 zend_function_entry http_message_object_fe
[] = {
117 HTTP_MESSAGE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
118 HTTP_MESSAGE_ME(getBody
, ZEND_ACC_PUBLIC
)
119 HTTP_MESSAGE_ME(setBody
, ZEND_ACC_PUBLIC
)
120 HTTP_MESSAGE_ME(getHeaders
, ZEND_ACC_PUBLIC
)
121 HTTP_MESSAGE_ME(setHeaders
, ZEND_ACC_PUBLIC
)
122 HTTP_MESSAGE_ME(addHeaders
, ZEND_ACC_PUBLIC
)
123 HTTP_MESSAGE_ME(getType
, ZEND_ACC_PUBLIC
)
124 HTTP_MESSAGE_ME(setType
, ZEND_ACC_PUBLIC
)
125 HTTP_MESSAGE_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
126 HTTP_MESSAGE_ME(setResponseCode
, ZEND_ACC_PUBLIC
)
127 HTTP_MESSAGE_ME(getRequestMethod
, ZEND_ACC_PUBLIC
)
128 HTTP_MESSAGE_ME(setRequestMethod
, ZEND_ACC_PUBLIC
)
129 HTTP_MESSAGE_ME(getRequestUri
, ZEND_ACC_PUBLIC
)
130 HTTP_MESSAGE_ME(setRequestUri
, ZEND_ACC_PUBLIC
)
131 HTTP_MESSAGE_ME(getHttpVersion
, ZEND_ACC_PUBLIC
)
132 HTTP_MESSAGE_ME(setHttpVersion
, ZEND_ACC_PUBLIC
)
133 HTTP_MESSAGE_ME(getParentMessage
, ZEND_ACC_PUBLIC
)
134 HTTP_MESSAGE_ME(send
, ZEND_ACC_PUBLIC
)
135 HTTP_MESSAGE_ME(toString
, ZEND_ACC_PUBLIC
)
137 /* implements Countable */
138 HTTP_MESSAGE_ME(count
, ZEND_ACC_PUBLIC
)
140 /* implements Serializable */
141 HTTP_MESSAGE_ME(serialize
, ZEND_ACC_PUBLIC
)
142 HTTP_MESSAGE_ME(unserialize
, ZEND_ACC_PUBLIC
)
144 ZEND_MALIAS(HttpMessage
, __toString
, toString
, HTTP_ARGS(HttpMessage
, toString
), ZEND_ACC_PUBLIC
)
146 HTTP_MESSAGE_ME(fromString
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
150 static zend_object_handlers http_message_object_handlers
;
152 PHP_MINIT_FUNCTION(http_message_object
)
154 HTTP_REGISTER_CLASS_EX(HttpMessage
, http_message_object
, NULL
, 0);
157 zend_class_implements(http_message_object_ce TSRMLS_CC
, 2, spl_ce_Countable
, zend_ce_serializable
);
159 zend_class_implements(http_message_object_ce TSRMLS_CC
, 1, zend_ce_serializable
);
163 zend_class_implements(http_message_object_ce TSRMLS_CC
, 1, spl_ce_Countable
);
167 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE
);
168 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST
);
169 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE
);
171 http_message_object_handlers
.clone_obj
= _http_message_object_clone_obj
;
172 http_message_object_handlers
.read_property
= http_message_object_read_prop
;
173 http_message_object_handlers
.write_property
= http_message_object_write_prop
;
174 http_message_object_handlers
.get_properties
= http_message_object_get_props
;
175 http_message_object_handlers
.get_property_ptr_ptr
= NULL
;
180 zend_object_value
_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
182 return http_message_object_new_ex(ce
, NULL
, NULL
);
185 zend_object_value
_http_message_object_new_ex(zend_class_entry
*ce
, http_message
*msg
, http_message_object
**ptr TSRMLS_DC
)
187 zend_object_value ov
;
188 http_message_object
*o
;
190 o
= ecalloc(1, sizeof(http_message_object
));
200 o
->parent
= http_message_object_new_ex(ce
, msg
->parent
, NULL
);
204 ALLOC_HASHTABLE(OBJ_PROP(o
));
205 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
207 ov
.handle
= putObject(http_message_object
, o
);
208 ov
.handlers
= &http_message_object_handlers
;
213 zend_object_value
_http_message_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
215 getObject(http_message_object
, obj
);
216 return http_message_object_new_ex(Z_OBJCE_P(this_ptr
), http_message_dup(obj
->message
), NULL
);
219 static inline void _http_message_object_declare_default_properties(TSRMLS_D
)
221 zend_class_entry
*ce
= http_message_object_ce
;
224 DCL_CONST(long, "TYPE_NONE", HTTP_MSG_NONE
);
225 DCL_CONST(long, "TYPE_REQUEST", HTTP_MSG_REQUEST
);
226 DCL_CONST(long, "TYPE_RESPONSE", HTTP_MSG_RESPONSE
);
229 DCL_PROP(PROTECTED
, long, type
, HTTP_MSG_NONE
);
230 DCL_PROP(PROTECTED
, string
, body
, "");
231 DCL_PROP(PROTECTED
, string
, requestMethod
, "");
232 DCL_PROP(PROTECTED
, string
, requestUri
, "");
233 DCL_PROP(PROTECTED
, long, responseCode
, 0);
234 DCL_PROP_N(PROTECTED
, httpVersion
);
235 DCL_PROP_N(PROTECTED
, headers
);
236 DCL_PROP_N(PROTECTED
, parentMessage
);
239 void _http_message_object_free(zend_object
*object TSRMLS_DC
)
241 http_message_object
*o
= (http_message_object
*) object
;
244 zend_hash_destroy(OBJ_PROP(o
));
245 FREE_HASHTABLE(OBJ_PROP(o
));
248 http_message_dtor(o
->message
);
254 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
256 getObjectEx(http_message_object
, obj
, object
);
257 http_message
*msg
= obj
->message
;
260 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1);
262 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
264 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
265 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
269 if (type
== BP_VAR_W
) {
270 zend_error(E_ERROR
, "Cannot access HttpMessage properties by reference or array key/index");
274 ALLOC_ZVAL(return_value
);
275 return_value
->refcount
= 0;
276 return_value
->is_ref
= 0;
284 case HTTP_MSG_PROPHASH_TYPE
:
285 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
286 RETVAL_LONG(msg
->type
);
289 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
290 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
291 RETVAL_DOUBLE(msg
->http
.version
);
294 case HTTP_MSG_PROPHASH_BODY
:
295 case HTTP_MSG_CHILD_PROPHASH_BODY
:
296 phpstr_fix(PHPSTR(msg
));
297 RETVAL_PHPSTR(PHPSTR(msg
), 0, 1);
300 case HTTP_MSG_PROPHASH_HEADERS
:
301 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
302 array_init(return_value
);
303 zend_hash_copy(Z_ARRVAL_P(return_value
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
306 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
307 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
309 RETVAL_OBJVAL(obj
->parent
);
315 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
316 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
317 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.method
) {
318 RETVAL_STRING(msg
->http
.info
.request
.method
, 1);
324 case HTTP_MSG_PROPHASH_REQUEST_URI
:
325 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URI
:
326 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.URI
) {
327 RETVAL_STRING(msg
->http
.info
.request
.URI
, 1);
333 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
334 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
335 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
336 RETVAL_LONG(msg
->http
.info
.response
.code
);
342 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
343 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
344 if (HTTP_MSG_TYPE(RESPONSE
, msg
) && msg
->http
.info
.response
.status
) {
345 RETVAL_STRING(msg
->http
.info
.response
.status
, 1);
353 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
363 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
)
365 getObjectEx(http_message_object
, obj
, object
);
366 http_message
*msg
= obj
->message
;
368 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
) + 1);
370 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
372 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
373 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_ex(&value
);
387 http_message_set_type(msg
, Z_LVAL_P(value
));
390 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
391 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
392 convert_to_double_ex(&value
);
393 msg
->http
.version
= Z_DVAL_P(value
);
396 case HTTP_MSG_PROPHASH_BODY
:
397 case HTTP_MSG_CHILD_PROPHASH_BODY
:
398 convert_to_string_ex(&value
);
399 phpstr_dtor(PHPSTR(msg
));
400 phpstr_from_string_ex(PHPSTR(msg
), Z_STRVAL_P(value
), Z_STRLEN_P(value
));
403 case HTTP_MSG_PROPHASH_HEADERS
:
404 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
405 convert_to_array_ex(&value
);
406 zend_hash_clean(&msg
->hdrs
);
407 zend_hash_copy(&msg
->hdrs
, Z_ARRVAL_P(value
), (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_ex(&value
);
427 STR_SET(msg
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
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_ex(&value
);
435 STR_SET(msg
->http
.info
.request
.URI
, estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
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_ex(&value
);
443 msg
->http
.info
.response
.code
= Z_LVAL_P(value
);
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_ex(&value
);
451 STR_SET(msg
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
457 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
463 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
)
466 getObjectEx(http_message_object
, obj
, object
);
467 http_message
*msg
= obj
->message
;
468 HashTable
*props
= OBJ_PROP(obj
);
471 INIT_ZARR(array
, props
);
473 #define ASSOC_PROP(array, ptype, name, val) \
477 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
478 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+4, val); \
479 efree(m_prop_name); \
481 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
482 #define ASSOC_STRINGL(array, name, val, len) \
486 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
487 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+4, val, len, 1); \
488 efree(m_prop_name); \
491 ASSOC_PROP(array
, long, "type", msg
->type
);
492 ASSOC_PROP(array
, double, "httpVersion", msg
->http
.version
);
496 case HTTP_MSG_REQUEST
:
497 ASSOC_PROP(array
, long, "responseCode", 0);
498 ASSOC_STRINGL(array
, "responseStatus", "", 0);
499 ASSOC_STRING(array
, "requestMethod", msg
->http
.info
.request
.method
);
500 ASSOC_STRING(array
, "requestUri", msg
->http
.info
.request
.URI
);
503 case HTTP_MSG_RESPONSE
:
504 ASSOC_PROP(array
, long, "responseCode", msg
->http
.info
.response
.code
);
505 ASSOC_STRING(array
, "responseStatus", msg
->http
.info
.response
.status
);
506 ASSOC_STRINGL(array
, "requestMethod", "", 0);
507 ASSOC_STRINGL(array
, "requestUri", "", 0);
512 ASSOC_PROP(array
, long, "responseCode", 0);
513 ASSOC_STRINGL(array
, "responseStatus", "", 0);
514 ASSOC_STRINGL(array
, "requestMethod", "", 0);
515 ASSOC_STRINGL(array
, "requestUri", "", 0);
519 MAKE_STD_ZVAL(headers
);
521 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
522 ASSOC_PROP(array
, zval
, "headers", headers
);
523 ASSOC_STRINGL(array
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
525 return OBJ_PROP(obj
);
528 /* ### USERLAND ### */
530 /* {{{ proto void HttpMessage::__construct([string message])
532 * Instantiate a new HttpMessage object.
534 * Accepts an optional string parameter containing a single or several
535 * consecutive HTTP messages. The constructed object will actually
536 * represent the *last* message of the passed string. If there were
537 * prior messages, those can be accessed by HttpMessage::getParentMessage().
539 * Throws HttpMalformedHeaderException.
541 PHP_METHOD(HttpMessage
, __construct
)
544 char *message
= NULL
;
546 getObject(http_message_object
, obj
);
549 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
550 http_message
*msg
= obj
->message
;
552 http_message_dtor(msg
);
553 if (obj
->message
= http_message_parse_ex(msg
, message
, length
)) {
554 if (obj
->message
->parent
) {
555 obj
->parent
= http_message_object_new_ex(Z_OBJCE_P(getThis()), obj
->message
->parent
, NULL
);
558 obj
->message
= http_message_init(msg
);
562 obj
->message
= http_message_new();
568 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
570 * Create an HttpMessage object from a string. Kind of a static constructor.
572 * Expects a string parameter containing a sinlge or several consecutive
575 * Returns an HttpMessage object on success or NULL on failure.
577 * Throws HttpMalformedHeadersException.
579 PHP_METHOD(HttpMessage
, fromString
)
583 http_message
*msg
= NULL
;
588 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &string
, &length
)) {
589 if (msg
= http_message_parse(string
, length
)) {
590 ZVAL_OBJVAL(return_value
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
597 /* {{{ proto string HttpMessage::getBody()
599 * Get the body of the parsed HttpMessage.
601 * Returns the message body as string.
603 PHP_METHOD(HttpMessage
, getBody
)
608 getObject(http_message_object
, obj
);
609 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
614 /* {{{ proto void HttpMessage::setBody(string body)
616 * Set the body of the HttpMessage.
617 * NOTE: Don't forget to update any headers accordingly.
619 * Expects a string parameter containing the new body of the message.
621 PHP_METHOD(HttpMessage
, setBody
)
625 getObject(http_message_object
, obj
);
627 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &body
, &len
)) {
628 phpstr_dtor(PHPSTR(obj
->message
));
629 phpstr_from_string_ex(PHPSTR(obj
->message
), body
, len
);
634 /* {{{ proto array HttpMessage::getHeaders()
636 * Get Message Headers.
638 * Returns an associative array containing the messages HTTP headers.
640 PHP_METHOD(HttpMessage
, getHeaders
)
646 getObject(http_message_object
, obj
);
648 INIT_ZARR(headers
, &obj
->message
->hdrs
);
649 array_init(return_value
);
650 array_copy(&headers
, return_value
);
655 /* {{{ proto void HttpMessage::setHeaders(array headers)
659 * Expects an associative array as parameter containing the new HTTP headers,
660 * which will replace *all* previous HTTP headers of the message.
662 PHP_METHOD(HttpMessage
, setHeaders
)
664 zval
*new_headers
, old_headers
;
665 getObject(http_message_object
, obj
);
667 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
671 zend_hash_clean(&obj
->message
->hdrs
);
672 INIT_ZARR(old_headers
, &obj
->message
->hdrs
);
673 array_copy(new_headers
, &old_headers
);
677 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
679 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
681 * Expects an associative array as parameter containing the additional HTTP headers
682 * to add to the messages existing headers. If the optional bool parameter is true,
683 * and a header with the same name of one to add exists already, this respective
684 * header will be converted to an array containing both header values, otherwise
685 * it will be overwritten with the new header value.
687 PHP_METHOD(HttpMessage
, addHeaders
)
689 zval old_headers
, *new_headers
;
690 zend_bool append
= 0;
691 getObject(http_message_object
, obj
);
693 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
697 INIT_ZARR(old_headers
, &obj
->message
->hdrs
);
699 array_append(new_headers
, &old_headers
);
701 array_merge(new_headers
, &old_headers
);
706 /* {{{ proto int HttpMessage::getType()
708 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
710 * Returns the HttpMessage::TYPE.
712 PHP_METHOD(HttpMessage
, getType
)
717 getObject(http_message_object
, obj
);
718 RETURN_LONG(obj
->message
->type
);
723 /* {{{ proto void HttpMessage::setType(int type)
725 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
727 * Exptects an int parameter, the HttpMessage::TYPE.
729 PHP_METHOD(HttpMessage
, setType
)
732 getObject(http_message_object
, obj
);
734 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
737 http_message_set_type(obj
->message
, type
);
741 /* {{{ proto int HttpMessage::getResponseCode()
743 * Get the Response Code of the Message.
745 * Returns the HTTP response code if the message is of type
746 * HttpMessage::TYPE_RESPONSE, else FALSE.
748 PHP_METHOD(HttpMessage
, getResponseCode
)
753 getObject(http_message_object
, obj
);
754 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
755 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
760 /* {{{ proto bool HttpMessage::setResponseCode(int code)
762 * Set the response code of an HTTP Response Message.
764 * Expects an int parameter with the HTTP response code.
766 * Returns TRUE on success, or FALSE if the message is not of type
767 * HttpMessage::TYPE_RESPONSE or the response code is out of range (100-510).
769 PHP_METHOD(HttpMessage
, setResponseCode
)
772 getObject(http_message_object
, obj
);
774 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
776 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
779 if (code
< 100 || code
> 510) {
780 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid response code (100-510): %ld", code
);
784 obj
->message
->http
.info
.response
.code
= code
;
789 /* {{{ proto string HttpMessage::getRequestMethod()
791 * Get the Request Method of the Message.
793 * Returns the request method name on success, or FALSE if the message is
794 * not of type HttpMessage::TYPE_REQUEST.
796 PHP_METHOD(HttpMessage
, getRequestMethod
)
801 getObject(http_message_object
, obj
);
802 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
803 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
808 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
810 * Set the Request Method of the HTTP Message.
812 * Expects a string parameter containing the request method name.
814 * Returns TRUE on success, or FALSE if the message is not of type
815 * HttpMessage::TYPE_REQUEST or an invalid request method was supplied.
817 PHP_METHOD(HttpMessage
, setRequestMethod
)
821 getObject(http_message_object
, obj
);
823 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
825 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
828 if (method_len
< 1) {
829 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
832 if (SUCCESS
!= http_check_method(method
)) {
833 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unkown request method: %s", method
);
837 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
842 /* {{{ proto string HttpMessage::getRequestUri()
844 * Get the Request URI of the Message.
846 * Returns the request uri as string on success, or FALSE if the message
847 * is not of type HttpMessage::TYPE_REQUEST.
849 PHP_METHOD(HttpMessage
, getRequestUri
)
854 getObject(http_message_object
, obj
);
855 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
856 RETURN_STRING(obj
->message
->http
.info
.request
.URI
, 1);
861 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
863 * Set the Request URI of the HTTP Message.
865 * Expects a string parameters containing the request uri.
867 * Returns TRUE on success, or FALSE if the message is not of type
868 * HttpMessage::TYPE_REQUEST or supplied URI was empty.
870 PHP_METHOD(HttpMessage
, setRequestUri
)
874 getObject(http_message_object
, obj
);
876 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
879 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
881 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUri to an empty string");
885 STR_SET(obj
->message
->http
.info
.request
.URI
, estrndup(URI
, URIlen
));
890 /* {{{ proto string HttpMessage::getHttpVersion()
892 * Get the HTTP Protocol Version of the Message.
894 * Returns the HTTP protocol version as string.
896 PHP_METHOD(HttpMessage
, getHttpVersion
)
902 getObject(http_message_object
, obj
);
904 sprintf(ver
, "%1.1lf", obj
->message
->http
.version
);
905 RETURN_STRINGL(ver
, 3, 1);
910 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
912 * Set the HTTP Protocol version of the Message.
914 * Expects a string parameter containing the HTTP protocol version.
916 * Returns TRUE on success, or FALSE if supplied version is out of range (1.0/1.1).
918 PHP_METHOD(HttpMessage
, setHttpVersion
)
922 getObject(http_message_object
, obj
);
924 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
928 convert_to_double(zv
);
929 sprintf(v
, "%1.1lf", Z_DVAL_P(zv
));
930 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
931 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
935 obj
->message
->http
.version
= Z_DVAL_P(zv
);
940 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
942 * Get parent Message.
944 * Returns the parent HttpMessage on success, or NULL if there's none.
946 PHP_METHOD(HttpMessage
, getParentMessage
)
951 getObject(http_message_object
, obj
);
953 if (obj
->message
->parent
) {
954 RETVAL_OBJVAL(obj
->parent
);
962 /* {{{ proto bool HttpMessage::send()
964 * Send the Message according to its type as Response or Request.
965 * This provides limited functionality compared to HttpRequest and HttpResponse.
967 * Returns TRUE on success, or FALSE on failure.
969 PHP_METHOD(HttpMessage
, send
)
971 getObject(http_message_object
, obj
);
975 RETURN_SUCCESS(http_message_send(obj
->message
));
979 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
981 * Get the string representation of the Message.
983 * Accepts a bool parameter which specifies whether the returned string
984 * should also contain any parent messages.
986 * Returns the full message as string.
988 PHP_METHOD(HttpMessage
, toString
)
993 zend_bool include_parent
= 0;
994 getObject(http_message_object
, obj
);
996 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1000 if (include_parent
) {
1001 http_message_serialize(obj
->message
, &string
, &length
);
1003 http_message_tostring(obj
->message
, &string
, &length
);
1005 RETURN_STRINGL(string
, length
, 0);
1010 /* {{{ proto int HttpMessage::count()
1012 * Implements Countable.
1014 * Returns the number of parent messages + 1.
1016 PHP_METHOD(HttpMessage
, count
)
1021 getObject(http_message_object
, obj
);
1023 for (i
= 0, msg
= obj
->message
; msg
; msg
= msg
->parent
, ++i
);
1029 /* {{{ proto string HttpMessage::serialize()
1031 * Implements Serializable.
1033 * Returns the serialized representation of the HttpMessage.
1035 PHP_METHOD(HttpMessage
, serialize
)
1040 getObject(http_message_object
, obj
);
1042 http_message_serialize(obj
->message
, &string
, &length
);
1043 RETURN_STRINGL(string
, length
, 0);
1048 /* {{{ proto void HttpMessage::unserialize(string serialized)
1050 * Implements Serializable.
1052 * Re-constructs the HttpMessage based upon the serialized string.
1054 PHP_METHOD(HttpMessage
, unserialize
)
1058 getObject(http_message_object
, obj
);
1060 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &serialized
, &length
)) {
1061 http_message_dtor(obj
->message
);
1062 if (!http_message_parse_ex(obj
->message
, serialized
, (size_t) length
)) {
1063 http_error(HE_ERROR
, HTTP_E_RUNTIME
, "Could not unserialize HttpMessage");
1064 http_message_init(obj
->message
);
1070 #endif /* ZEND_ENGINE_2 */
1077 * vim600: noet sw=4 ts=4 fdm=marker
1078 * vim<600: noet sw=4 ts=4