2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
27 #include "php_http_api.h"
28 #include "php_http_std_defs.h"
29 #include "php_http_message_object.h"
30 #include "php_http_exception_object.h"
32 #include "phpstr/phpstr.h"
35 ZEND_EXTERN_MODULE_GLOBALS(http
);
37 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpMessage, method, ret_ref, req_args)
38 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpMessage, method, ret_ref)
39 #define HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, HTTP_ARGS(HttpMessage, method), visibility)
41 HTTP_BEGIN_ARGS(__construct
, 0, 0)
42 HTTP_ARG_VAL(message
, 0)
45 HTTP_BEGIN_ARGS(fromString
, 1, 1)
46 HTTP_ARG_VAL(message
, 0)
49 HTTP_EMPTY_ARGS(getBody
, 0);
50 HTTP_BEGIN_ARGS(setBody
, 0, 1)
54 HTTP_EMPTY_ARGS(getHeaders
, 0);
55 HTTP_BEGIN_ARGS(setHeaders
, 0, 1)
56 HTTP_ARG_VAL(headers
, 0)
59 HTTP_BEGIN_ARGS(addHeaders
, 0, 1)
60 HTTP_ARG_VAL(headers
, 0)
61 HTTP_ARG_VAL(append
, 0)
64 HTTP_EMPTY_ARGS(getType
, 0);
65 HTTP_BEGIN_ARGS(setType
, 0, 1)
69 HTTP_EMPTY_ARGS(getResponseCode
, 0);
70 HTTP_BEGIN_ARGS(setResponseCode
, 0, 1)
71 HTTP_ARG_VAL(response_code
, 0)
74 HTTP_EMPTY_ARGS(getRequestMethod
, 0);
75 HTTP_BEGIN_ARGS(setRequestMethod
, 0, 1)
76 HTTP_ARG_VAL(request_method
, 0)
79 HTTP_EMPTY_ARGS(getRequestUri
, 0);
80 HTTP_BEGIN_ARGS(setRequestUri
, 0, 1)
84 HTTP_EMPTY_ARGS(getHttpVersion
, 0);
85 HTTP_BEGIN_ARGS(setHttpVersion
, 0, 1)
86 HTTP_ARG_VAL(http_version
, 0)
89 HTTP_EMPTY_ARGS(getParentMessage
, 1);
90 HTTP_EMPTY_ARGS(send
, 0);
91 HTTP_BEGIN_ARGS(toString
, 0, 0)
92 HTTP_ARG_VAL(include_parent
, 0)
95 #define http_message_object_declare_default_properties() _http_message_object_declare_default_properties(TSRMLS_C)
96 static inline void _http_message_object_declare_default_properties(TSRMLS_D
);
97 #define http_message_object_read_prop _http_message_object_read_prop
98 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
);
99 #define http_message_object_write_prop _http_message_object_write_prop
100 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
);
101 #define http_message_object_get_props _http_message_object_get_props
102 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
);
103 #define http_message_object_clone_obj _http_message_object_clone_obj
104 static inline zend_object_value
_http_message_object_clone_obj(zval
*object TSRMLS_DC
);
106 zend_class_entry
*http_message_object_ce
;
107 zend_function_entry http_message_object_fe
[] = {
108 HTTP_MESSAGE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
109 HTTP_MESSAGE_ME(getBody
, ZEND_ACC_PUBLIC
)
110 HTTP_MESSAGE_ME(setBody
, ZEND_ACC_PUBLIC
)
111 HTTP_MESSAGE_ME(getHeaders
, ZEND_ACC_PUBLIC
)
112 HTTP_MESSAGE_ME(setHeaders
, ZEND_ACC_PUBLIC
)
113 HTTP_MESSAGE_ME(addHeaders
, ZEND_ACC_PUBLIC
)
114 HTTP_MESSAGE_ME(getType
, ZEND_ACC_PUBLIC
)
115 HTTP_MESSAGE_ME(setType
, ZEND_ACC_PUBLIC
)
116 HTTP_MESSAGE_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
117 HTTP_MESSAGE_ME(setResponseCode
, ZEND_ACC_PUBLIC
)
118 HTTP_MESSAGE_ME(getRequestMethod
, ZEND_ACC_PUBLIC
)
119 HTTP_MESSAGE_ME(setRequestMethod
, ZEND_ACC_PUBLIC
)
120 HTTP_MESSAGE_ME(getRequestUri
, ZEND_ACC_PUBLIC
)
121 HTTP_MESSAGE_ME(setRequestUri
, ZEND_ACC_PUBLIC
)
122 HTTP_MESSAGE_ME(getHttpVersion
, ZEND_ACC_PUBLIC
)
123 HTTP_MESSAGE_ME(setHttpVersion
, ZEND_ACC_PUBLIC
)
124 HTTP_MESSAGE_ME(getParentMessage
, ZEND_ACC_PUBLIC
)
125 HTTP_MESSAGE_ME(send
, ZEND_ACC_PUBLIC
)
126 HTTP_MESSAGE_ME(toString
, ZEND_ACC_PUBLIC
)
128 ZEND_MALIAS(HttpMessage
, __toString
, toString
, HTTP_ARGS(HttpMessage
, toString
), ZEND_ACC_PUBLIC
)
130 HTTP_MESSAGE_ME(fromString
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
134 static zend_object_handlers http_message_object_handlers
;
136 PHP_MINIT_FUNCTION(http_message_object
)
138 HTTP_REGISTER_CLASS_EX(HttpMessage
, http_message_object
, NULL
, 0);
140 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE
);
141 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST
);
142 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE
);
144 http_message_object_handlers
.clone_obj
= http_message_object_clone_obj
;
145 http_message_object_handlers
.read_property
= http_message_object_read_prop
;
146 http_message_object_handlers
.write_property
= http_message_object_write_prop
;
147 http_message_object_handlers
.get_properties
= http_message_object_get_props
;
148 http_message_object_handlers
.get_property_ptr_ptr
= NULL
;
153 zend_object_value
_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
155 return http_message_object_new_ex(ce
, NULL
);
158 zend_object_value
_http_message_object_new_ex(zend_class_entry
*ce
, http_message
*msg TSRMLS_DC
)
160 zend_object_value ov
;
161 http_message_object
*o
;
163 o
= ecalloc(1, sizeof(http_message_object
));
169 o
->parent
= http_message_object_from_msg(msg
->parent
);
173 ALLOC_HASHTABLE(OBJ_PROP(o
));
174 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
176 ov
.handle
= putObject(http_message_object
, o
);
177 ov
.handlers
= &http_message_object_handlers
;
182 zend_object_value
_http_message_object_clone(zval
*this_ptr TSRMLS_DC
)
184 return http_message_object_clone_obj(this_ptr TSRMLS_CC
);
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 inline zend_object_value
_http_message_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
224 getObject(http_message_object
, obj
);
225 return http_message_object_from_msg(http_message_dup(obj
->message
));
228 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
230 getObjectEx(http_message_object
, obj
, object
);
231 http_message
*msg
= obj
->message
;
234 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1);
236 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
238 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
239 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
243 return_value
= &EG(uninitialized_zval
);
244 return_value
->refcount
= 0;
245 return_value
->is_ref
= 0;
253 case HTTP_MSG_PROPHASH_TYPE
:
254 RETVAL_LONG(msg
->type
);
257 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
258 RETVAL_DOUBLE(msg
->http
.version
);
261 case HTTP_MSG_PROPHASH_BODY
:
262 phpstr_fix(PHPSTR(msg
));
263 RETVAL_PHPSTR(PHPSTR(msg
), 0, 1);
266 case HTTP_MSG_PROPHASH_HEADERS
:
267 /* This is needed for situations like
268 * $this->headers['foo'] = 'bar';
270 if (type
== BP_VAR_W
) {
271 return_value
->refcount
= 1;
272 return_value
->is_ref
= 1;
273 Z_TYPE_P(return_value
) = IS_ARRAY
;
274 Z_ARRVAL_P(return_value
) = &msg
->hdrs
;
276 array_init(return_value
);
277 zend_hash_copy(Z_ARRVAL_P(return_value
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
281 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
283 RETVAL_OBJVAL(obj
->parent
);
289 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
290 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.method
) {
291 RETVAL_STRING(msg
->http
.info
.request
.method
, 1);
297 case HTTP_MSG_PROPHASH_REQUEST_URI
:
298 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.URI
) {
299 RETVAL_STRING(msg
->http
.info
.request
.URI
, 1);
305 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
306 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
307 RETVAL_LONG(msg
->http
.info
.response
.code
);
313 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
314 if (HTTP_MSG_TYPE(RESPONSE
, msg
) && msg
->http
.info
.response
.status
) {
315 RETVAL_STRING(msg
->http
.info
.response
.status
, 1);
323 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
333 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
)
335 getObjectEx(http_message_object
, obj
, object
);
336 http_message
*msg
= obj
->message
;
338 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
) + 1);
340 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
342 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
343 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
354 case HTTP_MSG_PROPHASH_TYPE
:
355 convert_to_long_ex(&value
);
356 http_message_set_type(msg
, Z_LVAL_P(value
));
359 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
360 convert_to_double_ex(&value
);
361 msg
->http
.version
= Z_DVAL_P(value
);
364 case HTTP_MSG_PROPHASH_BODY
:
365 convert_to_string_ex(&value
);
366 phpstr_dtor(PHPSTR(msg
));
367 phpstr_from_string_ex(PHPSTR(msg
), Z_STRVAL_P(value
), Z_STRLEN_P(value
));
370 case HTTP_MSG_PROPHASH_HEADERS
:
371 convert_to_array_ex(&value
);
372 zend_hash_clean(&msg
->hdrs
);
373 zend_hash_copy(&msg
->hdrs
, Z_ARRVAL_P(value
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
376 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
379 tmp
.value
.obj
= obj
->parent
;
380 zend_objects_store_del_ref(&tmp TSRMLS_CC
);
382 zend_objects_store_add_ref(value TSRMLS_CC
);
383 obj
->parent
= value
->value
.obj
;
386 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
387 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
388 convert_to_string_ex(&value
);
389 STR_SET(msg
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
393 case HTTP_MSG_PROPHASH_REQUEST_URI
:
394 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
395 convert_to_string_ex(&value
);
396 STR_SET(msg
->http
.info
.request
.URI
, estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
400 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
401 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
402 convert_to_long_ex(&value
);
403 msg
->http
.info
.response
.code
= Z_LVAL_P(value
);
407 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
408 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
409 convert_to_string_ex(&value
);
410 STR_SET(msg
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
416 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
422 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
)
425 getObjectEx(http_message_object
, obj
, object
);
426 http_message
*msg
= obj
->message
;
427 HashTable
*props
= OBJ_PROP(obj
);
430 INIT_ZARR(array
, props
);
432 #define ASSOC_PROP(array, ptype, name, val) \
436 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
437 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+4, val); \
438 efree(m_prop_name); \
440 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
441 #define ASSOC_STRINGL(array, name, val, len) \
445 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
446 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+4, val, len, 1); \
447 efree(m_prop_name); \
450 ASSOC_PROP(array
, long, "type", msg
->type
);
451 ASSOC_PROP(array
, double, "httpVersion", msg
->http
.version
);
455 case HTTP_MSG_REQUEST
:
456 ASSOC_PROP(array
, long, "responseCode", 0);
457 ASSOC_STRINGL(array
, "responseStatus", "", 0);
458 ASSOC_STRING(array
, "requestMethod", msg
->http
.info
.request
.method
);
459 ASSOC_STRING(array
, "requestUri", msg
->http
.info
.request
.URI
);
462 case HTTP_MSG_RESPONSE
:
463 ASSOC_PROP(array
, long, "responseCode", msg
->http
.info
.response
.code
);
464 ASSOC_STRING(array
, "responseStatus", msg
->http
.info
.response
.status
);
465 ASSOC_STRINGL(array
, "requestMethod", "", 0);
466 ASSOC_STRINGL(array
, "requestUri", "", 0);
471 ASSOC_PROP(array
, long, "responseCode", 0);
472 ASSOC_STRINGL(array
, "responseStatus", "", 0);
473 ASSOC_STRINGL(array
, "requestMethod", "", 0);
474 ASSOC_STRINGL(array
, "requestUri", "", 0);
478 MAKE_STD_ZVAL(headers
);
480 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
481 ASSOC_PROP(array
, zval
, "headers", headers
);
482 ASSOC_STRINGL(array
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
484 return OBJ_PROP(obj
);
487 /* ### USERLAND ### */
489 /* {{{ proto void HttpMessage::__construct([string message])
491 * Instantiate a new HttpMessage object.
493 * Accepts an optional string parameter containing a single or several
494 * consecutive HTTP messages. The constructed object will actually
495 * represent the *last* message of the passed string. If there were
496 * prior messages, those can be accessed by HttpMessage::getParentMessage().
498 * Throws HttpMalformedHeaderException.
500 PHP_METHOD(HttpMessage
, __construct
)
502 char *message
= NULL
;
504 getObject(http_message_object
, obj
);
507 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
508 if (obj
->message
= http_message_parse(message
, length
)) {
509 if (obj
->message
->parent
) {
510 obj
->parent
= http_message_object_from_msg(obj
->message
->parent
);
513 } else if (!obj
->message
) {
514 obj
->message
= http_message_new();
520 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
522 * Create an HttpMessage object from a string. Kind of a static constructor.
524 * Expects a string parameter containing a sinlge or several consecutive
527 * Returns an HttpMessage object on success or NULL on failure.
529 * Throws HttpMalformedHeadersException.
531 PHP_METHOD(HttpMessage
, fromString
)
535 http_message
*msg
= NULL
;
540 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &string
, &length
)) {
541 if (msg
= http_message_parse(string
, length
)) {
542 Z_TYPE_P(return_value
) = IS_OBJECT
;
543 return_value
->value
.obj
= http_message_object_from_msg(msg
);
550 /* {{{ proto string HttpMessage::getBody()
552 * Get the body of the parsed HttpMessage.
554 * Returns the message body as string.
556 PHP_METHOD(HttpMessage
, getBody
)
561 getObject(http_message_object
, obj
);
562 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
567 /* {{{ proto void HttpMessage::setBody(string body)
569 * Set the body of the HttpMessage.
570 * NOTE: Don't forget to update any headers accordingly.
572 * Expects a string parameter containing the new body of the message.
574 PHP_METHOD(HttpMessage
, setBody
)
578 getObject(http_message_object
, obj
);
580 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &body
, &len
)) {
581 phpstr_dtor(PHPSTR(obj
->message
));
582 phpstr_from_string_ex(PHPSTR(obj
->message
), body
, len
);
587 /* {{{ proto array HttpMessage::getHeaders()
589 * Get Message Headers.
591 * Returns an associative array containing the messages HTTP headers.
593 PHP_METHOD(HttpMessage
, getHeaders
)
599 getObject(http_message_object
, obj
);
601 INIT_ZARR(headers
, &obj
->message
->hdrs
);
602 array_init(return_value
);
603 array_copy(&headers
, return_value
);
608 /* {{{ proto void HttpMessage::setHeaders(array headers)
612 * Expects an associative array as parameter containing the new HTTP headers,
613 * which will replace *all* previous HTTP headers of the message.
615 PHP_METHOD(HttpMessage
, setHeaders
)
617 zval
*new_headers
, old_headers
;
618 getObject(http_message_object
, obj
);
620 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
624 zend_hash_clean(&obj
->message
->hdrs
);
625 INIT_ZARR(old_headers
, &obj
->message
->hdrs
);
626 array_copy(new_headers
, &old_headers
);
630 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
632 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
634 * Expects an associative array as parameter containing the additional HTTP headers
635 * to add to the messages existing headers. If the optional bool parameter is true,
636 * and a header with the same name of one to add exists already, this respective
637 * header will be converted to an array containing both header values, otherwise
638 * it will be overwritten with the new header value.
640 PHP_METHOD(HttpMessage
, addHeaders
)
642 zval old_headers
, *new_headers
;
643 zend_bool append
= 0;
644 getObject(http_message_object
, obj
);
646 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
650 INIT_ZARR(old_headers
, &obj
->message
->hdrs
);
652 array_append(new_headers
, &old_headers
);
654 array_merge(new_headers
, &old_headers
);
659 /* {{{ proto int HttpMessage::getType()
661 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
663 * Returns the HttpMessage::TYPE.
665 PHP_METHOD(HttpMessage
, getType
)
670 getObject(http_message_object
, obj
);
671 RETURN_LONG(obj
->message
->type
);
676 /* {{{ proto void HttpMessage::setType(int type)
678 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
680 * Exptects an int parameter, the HttpMessage::TYPE.
682 PHP_METHOD(HttpMessage
, setType
)
685 getObject(http_message_object
, obj
);
687 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
690 http_message_set_type(obj
->message
, type
);
694 /* {{{ proto int HttpMessage::getResponseCode()
696 * Get the Response Code of the Message.
698 * Returns the HTTP response code if the message is of type
699 * HttpMessage::TYPE_RESPONSE, else FALSE.
701 PHP_METHOD(HttpMessage
, getResponseCode
)
706 getObject(http_message_object
, obj
);
707 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
708 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
713 /* {{{ proto bool HttpMessage::setResponseCode(int code)
715 * Set the response code of an HTTP Response Message.
717 * Expects an int parameter with the HTTP response code.
719 * Returns TRUE on success, or FALSE if the message is not of type
720 * HttpMessage::TYPE_RESPONSE or the response code is out of range (100-510).
722 PHP_METHOD(HttpMessage
, setResponseCode
)
725 getObject(http_message_object
, obj
);
727 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
729 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
732 if (code
< 100 || code
> 510) {
733 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid response code (100-510): %ld", code
);
737 obj
->message
->http
.info
.response
.code
= code
;
742 /* {{{ proto string HttpMessage::getRequestMethod()
744 * Get the Request Method of the Message.
746 * Returns the request method name on success, or FALSE if the message is
747 * not of type HttpMessage::TYPE_REQUEST.
749 PHP_METHOD(HttpMessage
, getRequestMethod
)
754 getObject(http_message_object
, obj
);
755 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
756 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
761 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
763 * Set the Request Method of the HTTP Message.
765 * Expects a string parameter containing the request method name.
767 * Returns TRUE on success, or FALSE if the message is not of type
768 * HttpMessage::TYPE_REQUEST or an invalid request method was supplied.
770 PHP_METHOD(HttpMessage
, setRequestMethod
)
774 getObject(http_message_object
, obj
);
776 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
778 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
781 if (method_len
< 1) {
782 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
785 if (SUCCESS
!= http_check_method(method
)) {
786 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unkown request method: %s", method
);
790 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
795 /* {{{ proto string HttpMessage::getRequestUri()
797 * Get the Request URI of the Message.
799 * Returns the request uri as string on success, or FALSE if the message
800 * is not of type HttpMessage::TYPE_REQUEST.
802 PHP_METHOD(HttpMessage
, getRequestUri
)
807 getObject(http_message_object
, obj
);
808 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
809 RETURN_STRING(obj
->message
->http
.info
.request
.URI
, 1);
814 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
816 * Set the Request URI of the HTTP Message.
818 * Expects a string parameters containing the request uri.
820 * Returns TRUE on success, or FALSE if the message is not of type
821 * HttpMessage::TYPE_REQUEST or supplied URI was empty.
823 PHP_METHOD(HttpMessage
, setRequestUri
)
827 getObject(http_message_object
, obj
);
829 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
832 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
834 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUri to an empty string");
838 STR_SET(obj
->message
->http
.info
.request
.URI
, estrndup(URI
, URIlen
));
843 /* {{{ proto string HttpMessage::getHttpVersion()
845 * Get the HTTP Protocol Version of the Message.
847 * Returns the HTTP protocol version as string.
849 PHP_METHOD(HttpMessage
, getHttpVersion
)
855 getObject(http_message_object
, obj
);
857 sprintf(ver
, "%1.1lf", obj
->message
->http
.version
);
858 RETURN_STRINGL(ver
, 3, 1);
863 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
865 * Set the HTTP Protocol version of the Message.
867 * Expects a string parameter containing the HTTP protocol version.
869 * Returns TRUE on success, or FALSE if supplied version is out of range (1.0/1.1).
871 PHP_METHOD(HttpMessage
, setHttpVersion
)
875 getObject(http_message_object
, obj
);
877 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
881 convert_to_double(zv
);
882 sprintf(v
, "%1.1lf", Z_DVAL_P(zv
));
883 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
884 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
888 obj
->message
->http
.version
= Z_DVAL_P(zv
);
893 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
895 * Get parent Message.
897 * Returns the parent HttpMessage on success, or NULL if there's none.
899 PHP_METHOD(HttpMessage
, getParentMessage
)
904 getObject(http_message_object
, obj
);
906 if (obj
->message
->parent
) {
907 RETVAL_OBJVAL(obj
->parent
);
915 /* {{{ proto bool HttpMessage::send()
917 * Send the Message according to its type as Response or Request.
918 * This provides limited functionality compared to HttpRequest and HttpResponse.
920 * Returns TRUE on success, or FALSE on failure.
922 PHP_METHOD(HttpMessage
, send
)
924 getObject(http_message_object
, obj
);
928 RETURN_SUCCESS(http_message_send(obj
->message
));
932 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
934 * Get the string representation of the Message.
936 * Accepts a bool parameter which specifies whether the returned string
937 * should also contain any parent messages.
939 * Returns the full message as string.
941 PHP_METHOD(HttpMessage
, toString
)
946 zend_bool include_parent
= 0;
947 getObject(http_message_object
, obj
);
949 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
953 if (include_parent
) {
954 http_message_serialize(obj
->message
, &string
, &length
);
956 http_message_tostring(obj
->message
, &string
, &length
);
958 RETURN_STRINGL(string
, length
, 0);
963 #endif /* ZEND_ENGINE_2 */
970 * vim600: noet sw=4 ts=4 fdm=marker
971 * vim<600: noet sw=4 ts=4