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"
34 ZEND_EXTERN_MODULE_GLOBALS(http
);
36 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpMessage, method, ret_ref, req_args)
37 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpMessage, method, ret_ref)
38 #define HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, HTTP_ARGS(HttpMessage, method), visibility)
40 HTTP_BEGIN_ARGS(__construct
, 0, 0)
41 HTTP_ARG_VAL(message
, 0)
44 HTTP_BEGIN_ARGS(fromString
, 1, 1)
45 HTTP_ARG_VAL(message
, 0)
48 HTTP_EMPTY_ARGS(getBody
, 0);
49 HTTP_EMPTY_ARGS(getHeaders
, 0);
50 HTTP_BEGIN_ARGS(setHeaders
, 0, 1)
51 HTTP_ARG_VAL(headers
, 0)
54 HTTP_BEGIN_ARGS(addHeaders
, 0, 1)
55 HTTP_ARG_VAL(headers
, 0)
56 HTTP_ARG_VAL(append
, 0)
59 HTTP_EMPTY_ARGS(getType
, 0);
60 HTTP_BEGIN_ARGS(setType
, 0, 1)
64 HTTP_EMPTY_ARGS(getResponseCode
, 0);
65 HTTP_BEGIN_ARGS(setResponseCode
, 0, 1)
66 HTTP_ARG_VAL(response_code
, 0)
69 HTTP_EMPTY_ARGS(getRequestMethod
, 0);
70 HTTP_BEGIN_ARGS(setRequestMethod
, 0, 1)
71 HTTP_ARG_VAL(request_method
, 0)
74 HTTP_EMPTY_ARGS(getRequestUri
, 0);
75 HTTP_BEGIN_ARGS(setRequestUri
, 0, 1)
79 HTTP_EMPTY_ARGS(getHttpVersion
, 0);
80 HTTP_BEGIN_ARGS(setHttpVersion
, 0, 1)
81 HTTP_ARG_VAL(http_version
, 0)
84 HTTP_EMPTY_ARGS(getParentMessage
, 1);
85 HTTP_EMPTY_ARGS(send
, 0);
86 HTTP_BEGIN_ARGS(toString
, 0, 0)
87 HTTP_ARG_VAL(include_parent
, 0)
90 #define http_message_object_declare_default_properties() _http_message_object_declare_default_properties(TSRMLS_C)
91 static inline void _http_message_object_declare_default_properties(TSRMLS_D
);
92 #define http_message_object_read_prop _http_message_object_read_prop
93 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
);
94 #define http_message_object_write_prop _http_message_object_write_prop
95 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
);
96 #define http_message_object_get_props _http_message_object_get_props
97 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
);
98 #define http_message_object_clone_obj _http_message_object_clone_obj
99 static inline zend_object_value
_http_message_object_clone_obj(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(getHeaders
, ZEND_ACC_PUBLIC
)
106 HTTP_MESSAGE_ME(setHeaders
, ZEND_ACC_PUBLIC
)
107 HTTP_MESSAGE_ME(addHeaders
, ZEND_ACC_PUBLIC
)
108 HTTP_MESSAGE_ME(getType
, ZEND_ACC_PUBLIC
)
109 HTTP_MESSAGE_ME(setType
, ZEND_ACC_PUBLIC
)
110 HTTP_MESSAGE_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
111 HTTP_MESSAGE_ME(setResponseCode
, ZEND_ACC_PUBLIC
)
112 HTTP_MESSAGE_ME(getRequestMethod
, ZEND_ACC_PUBLIC
)
113 HTTP_MESSAGE_ME(setRequestMethod
, ZEND_ACC_PUBLIC
)
114 HTTP_MESSAGE_ME(getRequestUri
, ZEND_ACC_PUBLIC
)
115 HTTP_MESSAGE_ME(setRequestUri
, ZEND_ACC_PUBLIC
)
116 HTTP_MESSAGE_ME(getHttpVersion
, ZEND_ACC_PUBLIC
)
117 HTTP_MESSAGE_ME(setHttpVersion
, ZEND_ACC_PUBLIC
)
118 HTTP_MESSAGE_ME(getParentMessage
, ZEND_ACC_PUBLIC
)
119 HTTP_MESSAGE_ME(send
, ZEND_ACC_PUBLIC
)
120 HTTP_MESSAGE_ME(toString
, ZEND_ACC_PUBLIC
)
122 ZEND_MALIAS(HttpMessage
, __toString
, toString
, HTTP_ARGS(HttpMessage
, toString
), ZEND_ACC_PUBLIC
)
124 HTTP_MESSAGE_ME(fromString
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
127 static zend_object_handlers http_message_object_handlers
;
129 void _http_message_object_init(INIT_FUNC_ARGS
)
131 HTTP_REGISTER_CLASS_EX(HttpMessage
, http_message_object
, NULL
, 0);
133 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE
);
134 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST
);
135 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE
);
137 http_message_object_handlers
.clone_obj
= http_message_object_clone_obj
;
138 http_message_object_handlers
.read_property
= http_message_object_read_prop
;
139 http_message_object_handlers
.write_property
= http_message_object_write_prop
;
140 http_message_object_handlers
.get_properties
= http_message_object_get_props
;
141 http_message_object_handlers
.get_property_ptr_ptr
= NULL
;
144 zend_object_value
_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
146 return http_message_object_new_ex(ce
, NULL
);
149 zend_object_value
_http_message_object_new_ex(zend_class_entry
*ce
, http_message
*msg TSRMLS_DC
)
151 zend_object_value ov
;
152 http_message_object
*o
;
154 o
= ecalloc(1, sizeof(http_message_object
));
157 o
->parent
.handle
= 0;
158 o
->parent
.handlers
= NULL
;
163 o
->parent
= http_message_object_from_msg(msg
->parent
);
167 ALLOC_HASHTABLE(OBJ_PROP(o
));
168 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
170 ov
.handle
= putObject(http_message_object
, o
);
171 ov
.handlers
= &http_message_object_handlers
;
176 zend_object_value
_http_message_object_clone(zval
*this_ptr TSRMLS_DC
)
178 return http_message_object_clone_obj(this_ptr TSRMLS_CC
);
181 static inline void _http_message_object_declare_default_properties(TSRMLS_D
)
183 zend_class_entry
*ce
= http_message_object_ce
;
185 DCL_PROP(PROTECTED
, long, type
, HTTP_MSG_NONE
);
186 DCL_PROP(PROTECTED
, string
, body
, "");
187 DCL_PROP(PROTECTED
, string
, requestMethod
, "");
188 DCL_PROP(PROTECTED
, string
, requestUri
, "");
189 DCL_PROP(PROTECTED
, long, responseCode
, 0);
190 DCL_PROP_N(PROTECTED
, httpVersion
);
191 DCL_PROP_N(PROTECTED
, headers
);
192 DCL_PROP_N(PROTECTED
, parentMessage
);
195 void _http_message_object_free(zend_object
*object TSRMLS_DC
)
197 http_message_object
*o
= (http_message_object
*) object
;
200 zend_hash_destroy(OBJ_PROP(o
));
201 FREE_HASHTABLE(OBJ_PROP(o
));
204 http_message_dtor(o
->message
);
210 static inline zend_object_value
_http_message_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
212 getObject(http_message_object
, obj
);
213 return http_message_object_from_msg(http_message_dup(obj
->message
));
216 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
218 getObjectEx(http_message_object
, obj
, object
);
219 http_message
*msg
= obj
->message
;
222 return_value
= &EG(uninitialized_zval
);
223 return_value
->refcount
= 0;
224 return_value
->is_ref
= 0;
227 fprintf(stderr
, "Read HttpMessage::$%s\n", Z_STRVAL_P(member
));
229 if (!EG(scope
) || !instanceof_function(EG(scope
), obj
->zo
.ce TSRMLS_CC
)) {
230 zend_error(HE_WARNING
, "Cannot access protected property %s::$%s", obj
->zo
.ce
->name
, Z_STRVAL_P(member
));
231 return EG(uninitialized_zval_ptr
);
234 switch (zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
) + 1))
236 case HTTP_MSG_PROPHASH_TYPE
:
237 RETVAL_LONG(msg
->type
);
240 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
243 case HTTP_MSG_REQUEST
:
244 RETVAL_DOUBLE(msg
->info
.request
.http_version
);
247 case HTTP_MSG_RESPONSE
:
248 RETVAL_DOUBLE(msg
->info
.response
.http_version
);
258 case HTTP_MSG_PROPHASH_BODY
:
259 phpstr_fix(PHPSTR(msg
));
260 RETVAL_PHPSTR(PHPSTR(msg
), 0, 1);
263 case HTTP_MSG_PROPHASH_HEADERS
:
264 /* This is needed for situations like
265 * $this->headers['foo'] = 'bar';
267 if (type
== BP_VAR_W
) {
268 return_value
->refcount
= 1;
269 return_value
->is_ref
= 1;
270 Z_TYPE_P(return_value
) = IS_ARRAY
;
271 Z_ARRVAL_P(return_value
) = &msg
->hdrs
;
273 array_init(return_value
);
274 zend_hash_copy(Z_ARRVAL_P(return_value
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
278 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
280 RETVAL_OBJVAL(obj
->parent
);
281 Z_TYPE_P(return_value
) = IS_OBJECT
;
282 return_value
->value
.obj
= obj
->parent
;
283 zend_objects_store_add_ref(return_value TSRMLS_CC
);
289 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
290 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->info
.request
.method
) {
291 RETVAL_STRING(msg
->info
.request
.method
, 1);
297 case HTTP_MSG_PROPHASH_REQUEST_URI
:
298 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->info
.request
.URI
) {
299 RETVAL_STRING(msg
->info
.request
.URI
, 1);
305 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
306 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
307 RETVAL_LONG(msg
->info
.response
.code
);
321 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
)
323 getObjectEx(http_message_object
, obj
, object
);
324 http_message
*msg
= obj
->message
;
327 fprintf(stderr
, "Write HttpMessage::$%s\n", Z_STRVAL_P(member
));
329 if (!EG(scope
) || !instanceof_function(EG(scope
), obj
->zo
.ce TSRMLS_CC
)) {
330 zend_error(HE_WARNING
, "Cannot access protected property %s::$%s", obj
->zo
.ce
->name
, Z_STRVAL_P(member
));
333 switch (zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
) + 1))
335 case HTTP_MSG_PROPHASH_TYPE
:
336 convert_to_long_ex(&value
);
337 if ((http_message_type
) Z_LVAL_P(value
) != msg
->type
) {
338 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
339 if (msg
->info
.request
.method
) {
340 efree(msg
->info
.request
.method
);
342 if (msg
->info
.request
.URI
) {
343 efree(msg
->info
.request
.URI
);
346 msg
->type
= Z_LVAL_P(value
);
347 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
348 msg
->info
.request
.method
= NULL
;
349 msg
->info
.request
.URI
= NULL
;
355 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
356 convert_to_double_ex(&value
);
359 case HTTP_MSG_REQUEST
:
360 msg
->info
.request
.http_version
= Z_DVAL_P(value
);
363 case HTTP_MSG_RESPONSE
:
364 msg
->info
.response
.http_version
= Z_DVAL_P(value
);
369 case HTTP_MSG_PROPHASH_BODY
:
370 convert_to_string_ex(&value
);
371 phpstr_dtor(PHPSTR(msg
));
372 phpstr_from_string_ex(PHPSTR(msg
), Z_STRVAL_P(value
), Z_STRLEN_P(value
));
375 case HTTP_MSG_PROPHASH_HEADERS
:
376 convert_to_array_ex(&value
);
377 zend_hash_clean(&msg
->hdrs
);
378 zend_hash_copy(&msg
->hdrs
, Z_ARRVAL_P(value
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
381 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
384 tmp
.value
.obj
= obj
->parent
;
385 zend_objects_store_del_ref(&tmp TSRMLS_CC
);
387 zend_objects_store_add_ref(value TSRMLS_CC
);
388 obj
->parent
= value
->value
.obj
;
391 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
392 convert_to_string_ex(&value
);
393 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
394 if (msg
->info
.request
.method
) {
395 efree(msg
->info
.request
.method
);
397 msg
->info
.request
.method
= estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
));
401 case HTTP_MSG_PROPHASH_REQUEST_URI
:
402 convert_to_string_ex(&value
);
403 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
404 if (msg
->info
.request
.URI
) {
405 efree(msg
->info
.request
.URI
);
407 msg
->info
.request
.URI
= estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
));
411 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
412 convert_to_long_ex(&value
);
413 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
414 msg
->info
.response
.code
= Z_LVAL_P(value
);
420 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
)
423 getObjectEx(http_message_object
, obj
, object
);
424 http_message
*msg
= obj
->message
;
426 #define ASSOC_PROP(obj, ptype, name, val) \
431 Z_ARRVAL(array) = OBJ_PROP(obj); \
432 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 1); \
433 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+4, val); \
435 #define ASSOC_STRING(obj, name, val) ASSOC_STRINGL(obj, name, val, strlen(val))
436 #define ASSOC_STRINGL(obj, name, val, len) \
441 Z_ARRVAL(array) = OBJ_PROP(obj); \
442 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 1); \
443 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+4, val, len, 1); \
446 zend_hash_clean(OBJ_PROP(obj
));
448 ASSOC_PROP(obj
, long, "type", msg
->type
);
449 ASSOC_STRINGL(obj
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
451 MAKE_STD_ZVAL(headers
);
454 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
455 ASSOC_PROP(obj
, zval
, "headers", headers
);
459 case HTTP_MSG_REQUEST
:
460 ASSOC_PROP(obj
, double, "httpVersion", msg
->info
.request
.http_version
);
461 ASSOC_PROP(obj
, long, "responseCode", 0);
462 ASSOC_STRING(obj
, "requestMethod", msg
->info
.request
.method
);
463 ASSOC_STRING(obj
, "requestUri", msg
->info
.request
.URI
);
466 case HTTP_MSG_RESPONSE
:
467 ASSOC_PROP(obj
, double, "httpVersion", msg
->info
.response
.http_version
);
468 ASSOC_PROP(obj
, long, "responseCode", msg
->info
.response
.code
);
469 ASSOC_STRING(obj
, "requestMethod", "");
470 ASSOC_STRING(obj
, "requestUri", "");
475 ASSOC_PROP(obj
, double, "httpVersion", 0.0);
476 ASSOC_PROP(obj
, long, "responseCode", 0);
477 ASSOC_STRING(obj
, "requestMethod", "");
478 ASSOC_STRING(obj
, "requestUri", "");
482 return OBJ_PROP(obj
);
485 /* ### USERLAND ### */
487 /* {{{ proto void HttpMessage::__construct([string message])
489 * Instantiate a new HttpMessage object.
491 PHP_METHOD(HttpMessage
, __construct
)
493 char *message
= NULL
;
495 getObject(http_message_object
, obj
);
498 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
499 if (obj
->message
= http_message_parse(message
, length
)) {
500 if (obj
->message
->parent
) {
501 obj
->parent
= http_message_object_from_msg(obj
->message
->parent
);
504 } else if (!obj
->message
) {
505 obj
->message
= http_message_new();
511 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
513 * Create an HttpMessage object from a string.
515 PHP_METHOD(HttpMessage
, fromString
)
519 http_message
*msg
= NULL
;
521 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &string
, &length
)) {
525 if (!(msg
= http_message_parse(string
, length
))) {
529 Z_TYPE_P(return_value
) = IS_OBJECT
;
530 return_value
->value
.obj
= http_message_object_from_msg(msg
);
534 /* {{{ proto string HttpMessage::getBody()
536 * Get the body of the parsed Message.
538 PHP_METHOD(HttpMessage
, getBody
)
543 getObject(http_message_object
, obj
);
544 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
549 /* {{{ proto array HttpMessage::getHeaders()
551 * Get Message Headers.
553 PHP_METHOD(HttpMessage
, getHeaders
)
559 getObject(http_message_object
, obj
);
561 Z_ARRVAL(headers
) = &obj
->message
->hdrs
;
562 array_init(return_value
);
563 array_copy(&headers
, return_value
);
568 /* {{{ proto void HttpMessage::setHeaders(array headers)
572 PHP_METHOD(HttpMessage
, setHeaders
)
574 zval
*new_headers
, old_headers
;
575 getObject(http_message_object
, obj
);
577 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
581 zend_hash_clean(&obj
->message
->hdrs
);
582 Z_ARRVAL(old_headers
) = &obj
->message
->hdrs
;
583 array_copy(new_headers
, &old_headers
);
587 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
589 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
591 PHP_METHOD(HttpMessage
, addHeaders
)
593 zval old_headers
, *new_headers
;
594 zend_bool append
= 0;
595 getObject(http_message_object
, obj
);
597 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
601 Z_ARRVAL(old_headers
) = &obj
->message
->hdrs
;
603 array_append(new_headers
, &old_headers
);
605 array_merge(new_headers
, &old_headers
);
610 /* {{{ proto long HttpMessage::getType()
612 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
614 PHP_METHOD(HttpMessage
, getType
)
619 getObject(http_message_object
, obj
);
620 RETURN_LONG(obj
->message
->type
);
625 /* {{{ proto void HttpMessage::setType(long type)
627 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
629 PHP_METHOD(HttpMessage
, setType
)
632 getObject(http_message_object
, obj
);
634 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
637 http_message_set_type(obj
->message
, type
);
641 /* {{{ proto long HttpMessage::getResponseCode()
643 * Get the Response Code of the Message.
645 PHP_METHOD(HttpMessage
, getResponseCode
)
650 getObject(http_message_object
, obj
);
652 if (!HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
653 http_error(HE_NOTICE
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
657 RETURN_LONG(obj
->message
->info
.response
.code
);
662 /* {{{ proto bool HttpMessage::setResponseCode(long code)
664 * Set the response code of an HTTP Response Message.
665 * Returns false if the Message is not of type HTTP_MSG_RESPONSE,
666 * or if the response code is out of range (100-510).
668 PHP_METHOD(HttpMessage
, setResponseCode
)
671 getObject(http_message_object
, obj
);
673 if (!HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
674 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
678 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
681 if (code
< 100 || code
> 510) {
682 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid response code (100-510): %ld", code
);
686 obj
->message
->info
.response
.code
= code
;
691 /* {{{ proto string HttpMessage::getRequestMethod()
693 * Get the Request Method of the Message.
694 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
696 PHP_METHOD(HttpMessage
, getRequestMethod
)
701 getObject(http_message_object
, obj
);
703 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
704 http_error(HE_NOTICE
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is not of type HTTP_MSG_REQUEST");
708 RETURN_STRING(obj
->message
->info
.request
.method
, 1);
713 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
715 * Set the Request Method of the HTTP Message.
716 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
718 PHP_METHOD(HttpMessage
, setRequestMethod
)
722 getObject(http_message_object
, obj
);
724 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
725 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is not of type HTTP_MSG_REQUEST");
729 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
732 if (method_len
< 1) {
733 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
736 if (SUCCESS
!= http_check_method(method
)) {
737 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unkown request method: %s", method
);
741 STR_SET(obj
->message
->info
.request
.method
, estrndup(method
, method_len
));
746 /* {{{ proto string HttpMessage::getRequestUri()
748 * Get the Request URI of the Message.
750 PHP_METHOD(HttpMessage
, getRequestUri
)
755 getObject(http_message_object
, obj
);
757 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
758 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is not of type HTTP_MSG_REQUEST");
762 RETURN_STRING(obj
->message
->info
.request
.URI
, 1);
767 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
769 * Set the Request URI of the HTTP Message.
770 * Returns false if the Message is not of type HTTP_MSG_REQUEST,
771 * or if paramtere URI was empty.
773 PHP_METHOD(HttpMessage
, setRequestUri
)
777 getObject(http_message_object
, obj
);
779 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
780 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is not of type HTTP_MSG_REQUEST");
783 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
787 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUri to an empty string");
791 STR_SET(obj
->message
->info
.request
.URI
, estrndup(URI
, URIlen
));
796 /* {{{ proto string HttpMessage::getHttpVersion()
798 * Get the HTTP Protocol Version of the Message.
800 PHP_METHOD(HttpMessage
, getHttpVersion
)
807 getObject(http_message_object
, obj
);
809 switch (obj
->message
->type
)
811 case HTTP_MSG_RESPONSE
:
812 version
= obj
->message
->info
.response
.http_version
;
815 case HTTP_MSG_REQUEST
:
816 version
= obj
->message
->info
.request
.http_version
;
823 sprintf(ver
, "%1.1lf", version
);
824 RETURN_STRINGL(ver
, 3, 1);
829 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
831 * Set the HTTP Protocol version of the Message.
832 * Returns false if version is invalid (1.0 and 1.1).
834 PHP_METHOD(HttpMessage
, setHttpVersion
)
838 getObject(http_message_object
, obj
);
840 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
844 if (HTTP_MSG_TYPE(NONE
, obj
->message
)) {
845 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "Message is neither of type HTTP_MSG_RESPONSE nor HTTP_MSG_REQUEST");
849 convert_to_double(zv
);
850 sprintf(v
, "%1.1lf", Z_DVAL_P(zv
));
851 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
852 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
856 if (HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
857 obj
->message
->info
.response
.http_version
= Z_DVAL_P(zv
);
859 obj
->message
->info
.request
.http_version
= Z_DVAL_P(zv
);
865 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
867 * Get parent Message.
869 PHP_METHOD(HttpMessage
, getParentMessage
)
874 getObject(http_message_object
, obj
);
876 if (obj
->message
->parent
) {
877 RETVAL_OBJVAL(obj
->parent
);
885 /* {{{ proto bool HttpMessage::send()
887 * Send the Message according to its type as Response or Request.
889 PHP_METHOD(HttpMessage
, send
)
891 getObject(http_message_object
, obj
);
895 RETURN_SUCCESS(http_message_send(obj
->message
));
899 /* {{{ proto string HttpMessage::toString([bool include_parent = true])
901 * Get the string representation of the Message.
903 PHP_METHOD(HttpMessage
, toString
)
908 zend_bool include_parent
= 1;
909 getObject(http_message_object
, obj
);
911 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
915 if (include_parent
) {
916 http_message_serialize(obj
->message
, &string
, &length
);
918 http_message_tostring(obj
->message
, &string
, &length
);
920 RETURN_STRINGL(string
, length
, 0);
925 #endif /* ZEND_ENGINE_2 */
932 * vim600: noet sw=4 ts=4 fdm=marker
933 * vim<600: noet sw=4 ts=4