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"
32 ZEND_EXTERN_MODULE_GLOBALS(http
);
34 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpMessage, method, ret_ref, req_args)
35 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpMessage, method, ret_ref)
36 #define HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, HTTP_ARGS(HttpMessage, method), visibility)
38 HTTP_BEGIN_ARGS(__construct
, 0, 0)
39 HTTP_ARG_VAL(message
, 0)
42 HTTP_BEGIN_ARGS(fromString
, 1, 1)
43 HTTP_ARG_VAL(message
, 0)
46 HTTP_EMPTY_ARGS(getBody
, 0);
47 HTTP_BEGIN_ARGS(setBody
, 0, 1)
51 HTTP_EMPTY_ARGS(getHeaders
, 0);
52 HTTP_BEGIN_ARGS(setHeaders
, 0, 1)
53 HTTP_ARG_VAL(headers
, 0)
56 HTTP_BEGIN_ARGS(addHeaders
, 0, 1)
57 HTTP_ARG_VAL(headers
, 0)
58 HTTP_ARG_VAL(append
, 0)
61 HTTP_EMPTY_ARGS(getType
, 0);
62 HTTP_BEGIN_ARGS(setType
, 0, 1)
66 HTTP_EMPTY_ARGS(getResponseCode
, 0);
67 HTTP_BEGIN_ARGS(setResponseCode
, 0, 1)
68 HTTP_ARG_VAL(response_code
, 0)
71 HTTP_EMPTY_ARGS(getRequestMethod
, 0);
72 HTTP_BEGIN_ARGS(setRequestMethod
, 0, 1)
73 HTTP_ARG_VAL(request_method
, 0)
76 HTTP_EMPTY_ARGS(getRequestUri
, 0);
77 HTTP_BEGIN_ARGS(setRequestUri
, 0, 1)
81 HTTP_EMPTY_ARGS(getHttpVersion
, 0);
82 HTTP_BEGIN_ARGS(setHttpVersion
, 0, 1)
83 HTTP_ARG_VAL(http_version
, 0)
86 HTTP_EMPTY_ARGS(getParentMessage
, 1);
87 HTTP_EMPTY_ARGS(send
, 0);
88 HTTP_BEGIN_ARGS(toString
, 0, 0)
89 HTTP_ARG_VAL(include_parent
, 0)
92 #define http_message_object_declare_default_properties() _http_message_object_declare_default_properties(TSRMLS_C)
93 static inline void _http_message_object_declare_default_properties(TSRMLS_D
);
94 #define http_message_object_read_prop _http_message_object_read_prop
95 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
);
96 #define http_message_object_write_prop _http_message_object_write_prop
97 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
);
98 #define http_message_object_get_props _http_message_object_get_props
99 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
);
101 zend_class_entry
*http_message_object_ce
;
102 zend_function_entry http_message_object_fe
[] = {
103 HTTP_MESSAGE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
104 HTTP_MESSAGE_ME(getBody
, ZEND_ACC_PUBLIC
)
105 HTTP_MESSAGE_ME(setBody
, ZEND_ACC_PUBLIC
)
106 HTTP_MESSAGE_ME(getHeaders
, ZEND_ACC_PUBLIC
)
107 HTTP_MESSAGE_ME(setHeaders
, ZEND_ACC_PUBLIC
)
108 HTTP_MESSAGE_ME(addHeaders
, ZEND_ACC_PUBLIC
)
109 HTTP_MESSAGE_ME(getType
, ZEND_ACC_PUBLIC
)
110 HTTP_MESSAGE_ME(setType
, ZEND_ACC_PUBLIC
)
111 HTTP_MESSAGE_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
112 HTTP_MESSAGE_ME(setResponseCode
, ZEND_ACC_PUBLIC
)
113 HTTP_MESSAGE_ME(getRequestMethod
, ZEND_ACC_PUBLIC
)
114 HTTP_MESSAGE_ME(setRequestMethod
, ZEND_ACC_PUBLIC
)
115 HTTP_MESSAGE_ME(getRequestUri
, ZEND_ACC_PUBLIC
)
116 HTTP_MESSAGE_ME(setRequestUri
, ZEND_ACC_PUBLIC
)
117 HTTP_MESSAGE_ME(getHttpVersion
, ZEND_ACC_PUBLIC
)
118 HTTP_MESSAGE_ME(setHttpVersion
, ZEND_ACC_PUBLIC
)
119 HTTP_MESSAGE_ME(getParentMessage
, ZEND_ACC_PUBLIC
)
120 HTTP_MESSAGE_ME(send
, ZEND_ACC_PUBLIC
)
121 HTTP_MESSAGE_ME(toString
, ZEND_ACC_PUBLIC
)
123 ZEND_MALIAS(HttpMessage
, __toString
, toString
, HTTP_ARGS(HttpMessage
, toString
), ZEND_ACC_PUBLIC
)
125 HTTP_MESSAGE_ME(fromString
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
129 static zend_object_handlers http_message_object_handlers
;
131 PHP_MINIT_FUNCTION(http_message_object
)
133 HTTP_REGISTER_CLASS_EX(HttpMessage
, http_message_object
, NULL
, 0);
135 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE
);
136 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST
);
137 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE
);
139 http_message_object_handlers
.clone_obj
= _http_message_object_clone_obj
;
140 http_message_object_handlers
.read_property
= http_message_object_read_prop
;
141 http_message_object_handlers
.write_property
= http_message_object_write_prop
;
142 http_message_object_handlers
.get_properties
= http_message_object_get_props
;
143 http_message_object_handlers
.get_property_ptr_ptr
= NULL
;
148 zend_object_value
_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
150 return http_message_object_new_ex(ce
, NULL
, NULL
);
153 zend_object_value
_http_message_object_new_ex(zend_class_entry
*ce
, http_message
*msg
, http_message_object
**ptr TSRMLS_DC
)
155 zend_object_value ov
;
156 http_message_object
*o
;
158 o
= ecalloc(1, sizeof(http_message_object
));
168 o
->parent
= http_message_object_new_ex(ce
, msg
->parent
, NULL
);
172 ALLOC_HASHTABLE(OBJ_PROP(o
));
173 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
175 ov
.handle
= putObject(http_message_object
, o
);
176 ov
.handlers
= &http_message_object_handlers
;
181 zend_object_value
_http_message_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
183 getObject(http_message_object
, obj
);
184 return http_message_object_new_ex(Z_OBJCE_P(this_ptr
), http_message_dup(obj
->message
), NULL
);
187 static inline void _http_message_object_declare_default_properties(TSRMLS_D
)
189 zend_class_entry
*ce
= http_message_object_ce
;
192 DCL_CONST(long, "TYPE_NONE", HTTP_MSG_NONE
);
193 DCL_CONST(long, "TYPE_REQUEST", HTTP_MSG_REQUEST
);
194 DCL_CONST(long, "TYPE_RESPONSE", HTTP_MSG_RESPONSE
);
197 DCL_PROP(PROTECTED
, long, type
, HTTP_MSG_NONE
);
198 DCL_PROP(PROTECTED
, string
, body
, "");
199 DCL_PROP(PROTECTED
, string
, requestMethod
, "");
200 DCL_PROP(PROTECTED
, string
, requestUri
, "");
201 DCL_PROP(PROTECTED
, long, responseCode
, 0);
202 DCL_PROP_N(PROTECTED
, httpVersion
);
203 DCL_PROP_N(PROTECTED
, headers
);
204 DCL_PROP_N(PROTECTED
, parentMessage
);
207 void _http_message_object_free(zend_object
*object TSRMLS_DC
)
209 http_message_object
*o
= (http_message_object
*) object
;
212 zend_hash_destroy(OBJ_PROP(o
));
213 FREE_HASHTABLE(OBJ_PROP(o
));
216 http_message_dtor(o
->message
);
222 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
224 getObjectEx(http_message_object
, obj
, object
);
225 http_message
*msg
= obj
->message
;
228 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1);
230 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
232 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
233 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
237 if (type
== BP_VAR_W
) {
238 zend_error(E_ERROR
, "Cannot access HttpMessage properties by reference or array key/index");
242 ALLOC_ZVAL(return_value
);
243 return_value
->refcount
= 0;
244 return_value
->is_ref
= 0;
252 case HTTP_MSG_PROPHASH_TYPE
:
253 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
254 RETVAL_LONG(msg
->type
);
257 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
258 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
259 RETVAL_DOUBLE(msg
->http
.version
);
262 case HTTP_MSG_PROPHASH_BODY
:
263 case HTTP_MSG_CHILD_PROPHASH_BODY
:
264 phpstr_fix(PHPSTR(msg
));
265 RETVAL_PHPSTR(PHPSTR(msg
), 0, 1);
268 case HTTP_MSG_PROPHASH_HEADERS
:
269 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
270 array_init(return_value
);
271 zend_hash_copy(Z_ARRVAL_P(return_value
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
274 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
275 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
277 RETVAL_OBJVAL(obj
->parent
);
283 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
284 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
285 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.method
) {
286 RETVAL_STRING(msg
->http
.info
.request
.method
, 1);
292 case HTTP_MSG_PROPHASH_REQUEST_URI
:
293 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URI
:
294 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.URI
) {
295 RETVAL_STRING(msg
->http
.info
.request
.URI
, 1);
301 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
302 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
303 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
304 RETVAL_LONG(msg
->http
.info
.response
.code
);
310 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
311 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
312 if (HTTP_MSG_TYPE(RESPONSE
, msg
) && msg
->http
.info
.response
.status
) {
313 RETVAL_STRING(msg
->http
.info
.response
.status
, 1);
321 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
331 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
)
333 getObjectEx(http_message_object
, obj
, object
);
334 http_message
*msg
= obj
->message
;
336 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
) + 1);
338 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
340 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
341 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
352 case HTTP_MSG_PROPHASH_TYPE
:
353 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
354 convert_to_long_ex(&value
);
355 http_message_set_type(msg
, Z_LVAL_P(value
));
358 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
359 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
360 convert_to_double_ex(&value
);
361 msg
->http
.version
= Z_DVAL_P(value
);
364 case HTTP_MSG_PROPHASH_BODY
:
365 case HTTP_MSG_CHILD_PROPHASH_BODY
:
366 convert_to_string_ex(&value
);
367 phpstr_dtor(PHPSTR(msg
));
368 phpstr_from_string_ex(PHPSTR(msg
), Z_STRVAL_P(value
), Z_STRLEN_P(value
));
371 case HTTP_MSG_PROPHASH_HEADERS
:
372 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
373 convert_to_array_ex(&value
);
374 zend_hash_clean(&msg
->hdrs
);
375 zend_hash_copy(&msg
->hdrs
, Z_ARRVAL_P(value
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
378 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
379 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
380 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), http_message_object_ce TSRMLS_CC
)) {
383 tmp
.value
.obj
= obj
->parent
;
386 Z_OBJ_ADDREF_P(value
);
387 obj
->parent
= value
->value
.obj
;
391 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
392 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
393 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
394 convert_to_string_ex(&value
);
395 STR_SET(msg
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
399 case HTTP_MSG_PROPHASH_REQUEST_URI
:
400 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URI
:
401 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
402 convert_to_string_ex(&value
);
403 STR_SET(msg
->http
.info
.request
.URI
, estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
407 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
408 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
409 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
410 convert_to_long_ex(&value
);
411 msg
->http
.info
.response
.code
= Z_LVAL_P(value
);
415 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
416 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
417 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
418 convert_to_string_ex(&value
);
419 STR_SET(msg
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
425 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
431 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
)
434 getObjectEx(http_message_object
, obj
, object
);
435 http_message
*msg
= obj
->message
;
436 HashTable
*props
= OBJ_PROP(obj
);
439 INIT_ZARR(array
, props
);
441 #define ASSOC_PROP(array, ptype, name, val) \
445 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
446 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+4, val); \
447 efree(m_prop_name); \
449 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
450 #define ASSOC_STRINGL(array, name, val, len) \
454 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
455 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+4, val, len, 1); \
456 efree(m_prop_name); \
459 ASSOC_PROP(array
, long, "type", msg
->type
);
460 ASSOC_PROP(array
, double, "httpVersion", msg
->http
.version
);
464 case HTTP_MSG_REQUEST
:
465 ASSOC_PROP(array
, long, "responseCode", 0);
466 ASSOC_STRINGL(array
, "responseStatus", "", 0);
467 ASSOC_STRING(array
, "requestMethod", msg
->http
.info
.request
.method
);
468 ASSOC_STRING(array
, "requestUri", msg
->http
.info
.request
.URI
);
471 case HTTP_MSG_RESPONSE
:
472 ASSOC_PROP(array
, long, "responseCode", msg
->http
.info
.response
.code
);
473 ASSOC_STRING(array
, "responseStatus", msg
->http
.info
.response
.status
);
474 ASSOC_STRINGL(array
, "requestMethod", "", 0);
475 ASSOC_STRINGL(array
, "requestUri", "", 0);
480 ASSOC_PROP(array
, long, "responseCode", 0);
481 ASSOC_STRINGL(array
, "responseStatus", "", 0);
482 ASSOC_STRINGL(array
, "requestMethod", "", 0);
483 ASSOC_STRINGL(array
, "requestUri", "", 0);
487 MAKE_STD_ZVAL(headers
);
489 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
490 ASSOC_PROP(array
, zval
, "headers", headers
);
491 ASSOC_STRINGL(array
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
493 return OBJ_PROP(obj
);
496 /* ### USERLAND ### */
498 /* {{{ proto void HttpMessage::__construct([string message])
500 * Instantiate a new HttpMessage object.
502 * Accepts an optional string parameter containing a single or several
503 * consecutive HTTP messages. The constructed object will actually
504 * represent the *last* message of the passed string. If there were
505 * prior messages, those can be accessed by HttpMessage::getParentMessage().
507 * Throws HttpMalformedHeaderException.
509 PHP_METHOD(HttpMessage
, __construct
)
511 char *message
= NULL
;
513 getObject(http_message_object
, obj
);
516 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
517 if (obj
->message
= http_message_parse(message
, length
)) {
518 if (obj
->message
->parent
) {
519 obj
->parent
= http_message_object_new_ex(Z_OBJCE_P(getThis()), obj
->message
->parent
, NULL
);
522 } else if (!obj
->message
) {
523 obj
->message
= http_message_new();
529 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
531 * Create an HttpMessage object from a string. Kind of a static constructor.
533 * Expects a string parameter containing a sinlge or several consecutive
536 * Returns an HttpMessage object on success or NULL on failure.
538 * Throws HttpMalformedHeadersException.
540 PHP_METHOD(HttpMessage
, fromString
)
544 http_message
*msg
= NULL
;
549 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &string
, &length
)) {
550 if (msg
= http_message_parse(string
, length
)) {
551 Z_TYPE_P(return_value
) = IS_OBJECT
;
552 return_value
->value
.obj
= http_message_object_new_ex(http_message_object_ce
, msg
, NULL
);
559 /* {{{ proto string HttpMessage::getBody()
561 * Get the body of the parsed HttpMessage.
563 * Returns the message body as string.
565 PHP_METHOD(HttpMessage
, getBody
)
570 getObject(http_message_object
, obj
);
571 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
576 /* {{{ proto void HttpMessage::setBody(string body)
578 * Set the body of the HttpMessage.
579 * NOTE: Don't forget to update any headers accordingly.
581 * Expects a string parameter containing the new body of the message.
583 PHP_METHOD(HttpMessage
, setBody
)
587 getObject(http_message_object
, obj
);
589 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &body
, &len
)) {
590 phpstr_dtor(PHPSTR(obj
->message
));
591 phpstr_from_string_ex(PHPSTR(obj
->message
), body
, len
);
596 /* {{{ proto array HttpMessage::getHeaders()
598 * Get Message Headers.
600 * Returns an associative array containing the messages HTTP headers.
602 PHP_METHOD(HttpMessage
, getHeaders
)
608 getObject(http_message_object
, obj
);
610 INIT_ZARR(headers
, &obj
->message
->hdrs
);
611 array_init(return_value
);
612 array_copy(&headers
, return_value
);
617 /* {{{ proto void HttpMessage::setHeaders(array headers)
621 * Expects an associative array as parameter containing the new HTTP headers,
622 * which will replace *all* previous HTTP headers of the message.
624 PHP_METHOD(HttpMessage
, setHeaders
)
626 zval
*new_headers
, old_headers
;
627 getObject(http_message_object
, obj
);
629 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
633 zend_hash_clean(&obj
->message
->hdrs
);
634 INIT_ZARR(old_headers
, &obj
->message
->hdrs
);
635 array_copy(new_headers
, &old_headers
);
639 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
641 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
643 * Expects an associative array as parameter containing the additional HTTP headers
644 * to add to the messages existing headers. If the optional bool parameter is true,
645 * and a header with the same name of one to add exists already, this respective
646 * header will be converted to an array containing both header values, otherwise
647 * it will be overwritten with the new header value.
649 PHP_METHOD(HttpMessage
, addHeaders
)
651 zval old_headers
, *new_headers
;
652 zend_bool append
= 0;
653 getObject(http_message_object
, obj
);
655 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
659 INIT_ZARR(old_headers
, &obj
->message
->hdrs
);
661 array_append(new_headers
, &old_headers
);
663 array_merge(new_headers
, &old_headers
);
668 /* {{{ proto int HttpMessage::getType()
670 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
672 * Returns the HttpMessage::TYPE.
674 PHP_METHOD(HttpMessage
, getType
)
679 getObject(http_message_object
, obj
);
680 RETURN_LONG(obj
->message
->type
);
685 /* {{{ proto void HttpMessage::setType(int type)
687 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
689 * Exptects an int parameter, the HttpMessage::TYPE.
691 PHP_METHOD(HttpMessage
, setType
)
694 getObject(http_message_object
, obj
);
696 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
699 http_message_set_type(obj
->message
, type
);
703 /* {{{ proto int HttpMessage::getResponseCode()
705 * Get the Response Code of the Message.
707 * Returns the HTTP response code if the message is of type
708 * HttpMessage::TYPE_RESPONSE, else FALSE.
710 PHP_METHOD(HttpMessage
, getResponseCode
)
715 getObject(http_message_object
, obj
);
716 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
717 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
722 /* {{{ proto bool HttpMessage::setResponseCode(int code)
724 * Set the response code of an HTTP Response Message.
726 * Expects an int parameter with the HTTP response code.
728 * Returns TRUE on success, or FALSE if the message is not of type
729 * HttpMessage::TYPE_RESPONSE or the response code is out of range (100-510).
731 PHP_METHOD(HttpMessage
, setResponseCode
)
734 getObject(http_message_object
, obj
);
736 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
738 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
741 if (code
< 100 || code
> 510) {
742 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid response code (100-510): %ld", code
);
746 obj
->message
->http
.info
.response
.code
= code
;
751 /* {{{ proto string HttpMessage::getRequestMethod()
753 * Get the Request Method of the Message.
755 * Returns the request method name on success, or FALSE if the message is
756 * not of type HttpMessage::TYPE_REQUEST.
758 PHP_METHOD(HttpMessage
, getRequestMethod
)
763 getObject(http_message_object
, obj
);
764 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
765 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
770 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
772 * Set the Request Method of the HTTP Message.
774 * Expects a string parameter containing the request method name.
776 * Returns TRUE on success, or FALSE if the message is not of type
777 * HttpMessage::TYPE_REQUEST or an invalid request method was supplied.
779 PHP_METHOD(HttpMessage
, setRequestMethod
)
783 getObject(http_message_object
, obj
);
785 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
787 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
790 if (method_len
< 1) {
791 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
794 if (SUCCESS
!= http_check_method(method
)) {
795 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unkown request method: %s", method
);
799 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
804 /* {{{ proto string HttpMessage::getRequestUri()
806 * Get the Request URI of the Message.
808 * Returns the request uri as string on success, or FALSE if the message
809 * is not of type HttpMessage::TYPE_REQUEST.
811 PHP_METHOD(HttpMessage
, getRequestUri
)
816 getObject(http_message_object
, obj
);
817 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
818 RETURN_STRING(obj
->message
->http
.info
.request
.URI
, 1);
823 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
825 * Set the Request URI of the HTTP Message.
827 * Expects a string parameters containing the request uri.
829 * Returns TRUE on success, or FALSE if the message is not of type
830 * HttpMessage::TYPE_REQUEST or supplied URI was empty.
832 PHP_METHOD(HttpMessage
, setRequestUri
)
836 getObject(http_message_object
, obj
);
838 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
841 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
843 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUri to an empty string");
847 STR_SET(obj
->message
->http
.info
.request
.URI
, estrndup(URI
, URIlen
));
852 /* {{{ proto string HttpMessage::getHttpVersion()
854 * Get the HTTP Protocol Version of the Message.
856 * Returns the HTTP protocol version as string.
858 PHP_METHOD(HttpMessage
, getHttpVersion
)
864 getObject(http_message_object
, obj
);
866 sprintf(ver
, "%1.1lf", obj
->message
->http
.version
);
867 RETURN_STRINGL(ver
, 3, 1);
872 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
874 * Set the HTTP Protocol version of the Message.
876 * Expects a string parameter containing the HTTP protocol version.
878 * Returns TRUE on success, or FALSE if supplied version is out of range (1.0/1.1).
880 PHP_METHOD(HttpMessage
, setHttpVersion
)
884 getObject(http_message_object
, obj
);
886 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
890 convert_to_double(zv
);
891 sprintf(v
, "%1.1lf", Z_DVAL_P(zv
));
892 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
893 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
897 obj
->message
->http
.version
= Z_DVAL_P(zv
);
902 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
904 * Get parent Message.
906 * Returns the parent HttpMessage on success, or NULL if there's none.
908 PHP_METHOD(HttpMessage
, getParentMessage
)
913 getObject(http_message_object
, obj
);
915 if (obj
->message
->parent
) {
916 RETVAL_OBJVAL(obj
->parent
);
924 /* {{{ proto bool HttpMessage::send()
926 * Send the Message according to its type as Response or Request.
927 * This provides limited functionality compared to HttpRequest and HttpResponse.
929 * Returns TRUE on success, or FALSE on failure.
931 PHP_METHOD(HttpMessage
, send
)
933 getObject(http_message_object
, obj
);
937 RETURN_SUCCESS(http_message_send(obj
->message
));
941 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
943 * Get the string representation of the Message.
945 * Accepts a bool parameter which specifies whether the returned string
946 * should also contain any parent messages.
948 * Returns the full message as string.
950 PHP_METHOD(HttpMessage
, toString
)
955 zend_bool include_parent
= 0;
956 getObject(http_message_object
, obj
);
958 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
962 if (include_parent
) {
963 http_message_serialize(obj
->message
, &string
, &length
);
965 http_message_tostring(obj
->message
, &string
, &length
);
967 RETURN_STRINGL(string
, length
, 0);
972 #endif /* ZEND_ENGINE_2 */
979 * vim600: noet sw=4 ts=4 fdm=marker
980 * vim<600: noet sw=4 ts=4