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 void _http_message_object_init(INIT_FUNC_ARGS
)
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
;
151 zend_object_value
_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
153 return http_message_object_new_ex(ce
, NULL
);
156 zend_object_value
_http_message_object_new_ex(zend_class_entry
*ce
, http_message
*msg TSRMLS_DC
)
158 zend_object_value ov
;
159 http_message_object
*o
;
161 o
= ecalloc(1, sizeof(http_message_object
));
164 o
->parent
.handle
= 0;
165 o
->parent
.handlers
= NULL
;
170 o
->parent
= http_message_object_from_msg(msg
->parent
);
174 ALLOC_HASHTABLE(OBJ_PROP(o
));
175 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
177 ov
.handle
= putObject(http_message_object
, o
);
178 ov
.handlers
= &http_message_object_handlers
;
183 zend_object_value
_http_message_object_clone(zval
*this_ptr TSRMLS_DC
)
185 return http_message_object_clone_obj(this_ptr TSRMLS_CC
);
188 static inline void _http_message_object_declare_default_properties(TSRMLS_D
)
190 zend_class_entry
*ce
= http_message_object_ce
;
193 DCL_CONST(long, "TYPE_NONE", HTTP_MSG_NONE
);
194 DCL_CONST(long, "TYPE_REQUEST", HTTP_MSG_REQUEST
);
195 DCL_CONST(long, "TYPE_RESPONSE", HTTP_MSG_RESPONSE
);
198 DCL_PROP(PROTECTED
, long, type
, HTTP_MSG_NONE
);
199 DCL_PROP(PROTECTED
, string
, body
, "");
200 DCL_PROP(PROTECTED
, string
, requestMethod
, "");
201 DCL_PROP(PROTECTED
, string
, requestUri
, "");
202 DCL_PROP(PROTECTED
, long, responseCode
, 0);
203 DCL_PROP_N(PROTECTED
, httpVersion
);
204 DCL_PROP_N(PROTECTED
, headers
);
205 DCL_PROP_N(PROTECTED
, parentMessage
);
208 void _http_message_object_free(zend_object
*object TSRMLS_DC
)
210 http_message_object
*o
= (http_message_object
*) object
;
213 zend_hash_destroy(OBJ_PROP(o
));
214 FREE_HASHTABLE(OBJ_PROP(o
));
217 http_message_dtor(o
->message
);
223 static inline zend_object_value
_http_message_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
225 getObject(http_message_object
, obj
);
226 return http_message_object_from_msg(http_message_dup(obj
->message
));
229 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
231 getObjectEx(http_message_object
, obj
, object
);
232 http_message
*msg
= obj
->message
;
234 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
236 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
237 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
240 return_value
= &EG(uninitialized_zval
);
241 return_value
->refcount
= 0;
242 return_value
->is_ref
= 0;
245 fprintf(stderr
, "Read HttpMessage::$%s\n", Z_STRVAL_P(member
));
250 case HTTP_MSG_PROPHASH_TYPE
:
251 RETVAL_LONG(msg
->type
);
254 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
255 RETVAL_DOUBLE(msg
->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
);
286 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
287 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.method
) {
288 RETVAL_STRING(msg
->http
.info
.request
.method
, 1);
294 case HTTP_MSG_PROPHASH_REQUEST_URI
:
295 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.URI
) {
296 RETVAL_STRING(msg
->http
.info
.request
.URI
, 1);
302 case HTTP_MSG_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 if (HTTP_MSG_TYPE(RESPONSE
, msg
) && msg
->http
.info
.response
.status
) {
312 RETVAL_STRING(msg
->http
.info
.response
.status
, 1);
326 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
)
328 getObjectEx(http_message_object
, obj
, object
);
329 http_message
*msg
= obj
->message
;
330 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
332 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
333 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
338 fprintf(stderr
, "Write HttpMessage::$%s\n", Z_STRVAL_P(member
));
343 case HTTP_MSG_PROPHASH_TYPE
:
344 convert_to_long_ex(&value
);
345 http_message_set_type(msg
, Z_LVAL_P(value
));
348 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
349 convert_to_double_ex(&value
);
350 msg
->http
.version
= Z_DVAL_P(value
);
353 case HTTP_MSG_PROPHASH_BODY
:
354 convert_to_string_ex(&value
);
355 phpstr_dtor(PHPSTR(msg
));
356 phpstr_from_string_ex(PHPSTR(msg
), Z_STRVAL_P(value
), Z_STRLEN_P(value
));
359 case HTTP_MSG_PROPHASH_HEADERS
:
360 convert_to_array_ex(&value
);
361 zend_hash_clean(&msg
->hdrs
);
362 zend_hash_copy(&msg
->hdrs
, Z_ARRVAL_P(value
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
365 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
368 tmp
.value
.obj
= obj
->parent
;
369 zend_objects_store_del_ref(&tmp TSRMLS_CC
);
371 zend_objects_store_add_ref(value TSRMLS_CC
);
372 obj
->parent
= value
->value
.obj
;
375 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
376 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
377 convert_to_string_ex(&value
);
378 STR_SET(msg
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
382 case HTTP_MSG_PROPHASH_REQUEST_URI
:
383 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
384 convert_to_string_ex(&value
);
385 STR_SET(msg
->http
.info
.request
.URI
, estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
389 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
390 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
391 convert_to_long_ex(&value
);
392 msg
->http
.info
.response
.code
= Z_LVAL_P(value
);
396 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
397 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
398 convert_to_string_ex(&value
);
399 STR_SET(msg
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
405 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
)
408 getObjectEx(http_message_object
, obj
, object
);
409 http_message
*msg
= obj
->message
;
411 #define ASSOC_PROP(obj, ptype, name, val) \
416 INIT_ZARR(array, OBJ_PROP(obj)); \
417 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 1); \
418 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+4, val); \
420 #define ASSOC_STRING(obj, name, val) ASSOC_STRINGL(obj, name, val, strlen(val))
421 #define ASSOC_STRINGL(obj, name, val, len) \
426 INIT_ZARR(array, OBJ_PROP(obj)); \
427 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 1); \
428 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+4, val, len, 1); \
431 //zend_hash_clean(OBJ_PROP(obj));
433 ASSOC_PROP(obj
, long, "type", msg
->type
);
434 ASSOC_PROP(obj
, double, "httpVersion", msg
->http
.version
);
438 case HTTP_MSG_REQUEST
:
439 ASSOC_PROP(obj
, long, "responseCode", 0);
440 ASSOC_STRINGL(obj
, "responseStatus", "", 0);
441 ASSOC_STRING(obj
, "requestMethod", msg
->http
.info
.request
.method
);
442 ASSOC_STRING(obj
, "requestUri", msg
->http
.info
.request
.URI
);
445 case HTTP_MSG_RESPONSE
:
446 ASSOC_PROP(obj
, long, "responseCode", msg
->http
.info
.response
.code
);
447 ASSOC_STRING(obj
, "responseStatus", msg
->http
.info
.response
.status
);
448 ASSOC_STRINGL(obj
, "requestMethod", "", 0);
449 ASSOC_STRINGL(obj
, "requestUri", "", 0);
454 ASSOC_PROP(obj
, long, "responseCode", 0);
455 ASSOC_STRINGL(obj
, "responseStatus", "", 0);
456 ASSOC_STRINGL(obj
, "requestMethod", "", 0);
457 ASSOC_STRINGL(obj
, "requestUri", "", 0);
461 MAKE_STD_ZVAL(headers
);
463 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
464 ASSOC_PROP(obj
, zval
, "headers", headers
);
465 ASSOC_STRINGL(obj
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
467 return OBJ_PROP(obj
);
470 /* ### USERLAND ### */
472 /* {{{ proto void HttpMessage::__construct([string message])
474 * Instantiate a new HttpMessage object.
476 PHP_METHOD(HttpMessage
, __construct
)
478 char *message
= NULL
;
480 getObject(http_message_object
, obj
);
483 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
484 if (obj
->message
= http_message_parse(message
, length
)) {
485 if (obj
->message
->parent
) {
486 obj
->parent
= http_message_object_from_msg(obj
->message
->parent
);
489 } else if (!obj
->message
) {
490 obj
->message
= http_message_new();
496 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
498 * Create an HttpMessage object from a string.
500 PHP_METHOD(HttpMessage
, fromString
)
504 http_message
*msg
= NULL
;
506 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &string
, &length
)) {
510 if (!(msg
= http_message_parse(string
, length
))) {
514 Z_TYPE_P(return_value
) = IS_OBJECT
;
515 return_value
->value
.obj
= http_message_object_from_msg(msg
);
519 /* {{{ proto string HttpMessage::getBody()
521 * Get the body of the parsed Message.
523 PHP_METHOD(HttpMessage
, getBody
)
528 getObject(http_message_object
, obj
);
529 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
534 /* {{{ proto void HttpMessage::setBody(string body)
536 * Set the body of the HttpMessage.
537 * NOTE: Don't forget to update any headers accordingly.
539 PHP_METHOD(HttpMessage
, setBody
)
543 getObject(http_message_object
, obj
);
545 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &body
, &len
)) {
546 phpstr_dtor(PHPSTR(obj
->message
));
547 phpstr_from_string_ex(PHPSTR(obj
->message
), body
, len
);
552 /* {{{ proto array HttpMessage::getHeaders()
554 * Get Message Headers.
556 PHP_METHOD(HttpMessage
, getHeaders
)
562 getObject(http_message_object
, obj
);
564 INIT_ZARR(headers
, &obj
->message
->hdrs
);
565 array_init(return_value
);
566 array_copy(&headers
, return_value
);
571 /* {{{ proto void HttpMessage::setHeaders(array headers)
575 PHP_METHOD(HttpMessage
, setHeaders
)
577 zval
*new_headers
, old_headers
;
578 getObject(http_message_object
, obj
);
580 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
584 zend_hash_clean(&obj
->message
->hdrs
);
585 INIT_ZARR(old_headers
, &obj
->message
->hdrs
);
586 array_copy(new_headers
, &old_headers
);
590 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
592 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
594 PHP_METHOD(HttpMessage
, addHeaders
)
596 zval old_headers
, *new_headers
;
597 zend_bool append
= 0;
598 getObject(http_message_object
, obj
);
600 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
604 INIT_ZARR(old_headers
, &obj
->message
->hdrs
);
606 array_append(new_headers
, &old_headers
);
608 array_merge(new_headers
, &old_headers
);
613 /* {{{ proto long HttpMessage::getType()
615 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
617 PHP_METHOD(HttpMessage
, getType
)
622 getObject(http_message_object
, obj
);
623 RETURN_LONG(obj
->message
->type
);
628 /* {{{ proto void HttpMessage::setType(long type)
630 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
632 PHP_METHOD(HttpMessage
, setType
)
635 getObject(http_message_object
, obj
);
637 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
640 http_message_set_type(obj
->message
, type
);
644 /* {{{ proto long HttpMessage::getResponseCode()
646 * Get the Response Code of the Message.
648 PHP_METHOD(HttpMessage
, getResponseCode
)
653 getObject(http_message_object
, obj
);
655 if (!HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
656 http_error(HE_NOTICE
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
660 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
665 /* {{{ proto bool HttpMessage::setResponseCode(long code)
667 * Set the response code of an HTTP Response Message.
668 * Returns false if the Message is not of type HTTP_MSG_RESPONSE,
669 * or if the response code is out of range (100-510).
671 PHP_METHOD(HttpMessage
, setResponseCode
)
674 getObject(http_message_object
, obj
);
676 if (!HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
677 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
681 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
684 if (code
< 100 || code
> 510) {
685 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid response code (100-510): %ld", code
);
689 obj
->message
->http
.info
.response
.code
= code
;
694 /* {{{ proto string HttpMessage::getRequestMethod()
696 * Get the Request Method of the Message.
697 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
699 PHP_METHOD(HttpMessage
, getRequestMethod
)
704 getObject(http_message_object
, obj
);
706 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
707 http_error(HE_NOTICE
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is not of type HTTP_MSG_REQUEST");
711 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
716 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
718 * Set the Request Method of the HTTP Message.
719 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
721 PHP_METHOD(HttpMessage
, setRequestMethod
)
725 getObject(http_message_object
, obj
);
727 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
728 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is not of type HTTP_MSG_REQUEST");
732 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
735 if (method_len
< 1) {
736 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
739 if (SUCCESS
!= http_check_method(method
)) {
740 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unkown request method: %s", method
);
744 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
749 /* {{{ proto string HttpMessage::getRequestUri()
751 * Get the Request URI of the Message.
753 PHP_METHOD(HttpMessage
, getRequestUri
)
758 getObject(http_message_object
, obj
);
760 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
761 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is not of type HTTP_MSG_REQUEST");
765 RETURN_STRING(obj
->message
->http
.info
.request
.URI
, 1);
770 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
772 * Set the Request URI of the HTTP Message.
773 * Returns false if the Message is not of type HTTP_MSG_REQUEST,
774 * or if paramtere URI was empty.
776 PHP_METHOD(HttpMessage
, setRequestUri
)
780 getObject(http_message_object
, obj
);
782 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
783 http_error(HE_WARNING
, HTTP_E_MESSAGE_TYPE
, "HttpMessage is not of type HTTP_MSG_REQUEST");
786 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
790 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUri to an empty string");
794 STR_SET(obj
->message
->http
.info
.request
.URI
, estrndup(URI
, URIlen
));
799 /* {{{ proto string HttpMessage::getHttpVersion()
801 * Get the HTTP Protocol Version of the Message.
803 PHP_METHOD(HttpMessage
, getHttpVersion
)
809 getObject(http_message_object
, obj
);
811 sprintf(ver
, "%1.1lf", obj
->message
->http
.version
);
812 RETURN_STRINGL(ver
, 3, 1);
817 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
819 * Set the HTTP Protocol version of the Message.
820 * Returns false if version is invalid (1.0 and 1.1).
822 PHP_METHOD(HttpMessage
, setHttpVersion
)
826 getObject(http_message_object
, obj
);
828 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
832 convert_to_double(zv
);
833 sprintf(v
, "%1.1lf", Z_DVAL_P(zv
));
834 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
835 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
839 obj
->message
->http
.version
= Z_DVAL_P(zv
);
844 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
846 * Get parent Message.
848 PHP_METHOD(HttpMessage
, getParentMessage
)
853 getObject(http_message_object
, obj
);
855 if (obj
->message
->parent
) {
856 RETVAL_OBJVAL(obj
->parent
);
864 /* {{{ proto bool HttpMessage::send()
866 * Send the Message according to its type as Response or Request.
868 PHP_METHOD(HttpMessage
, send
)
870 getObject(http_message_object
, obj
);
874 RETURN_SUCCESS(http_message_send(obj
->message
));
878 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
880 * Get the string representation of the Message.
882 PHP_METHOD(HttpMessage
, toString
)
887 zend_bool include_parent
= 0;
888 getObject(http_message_object
, obj
);
890 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
894 if (include_parent
) {
895 http_message_serialize(obj
->message
, &string
, &length
);
897 http_message_tostring(obj
->message
, &string
, &length
);
899 RETURN_STRINGL(string
, length
, 0);
904 #endif /* ZEND_ENGINE_2 */
911 * vim600: noet sw=4 ts=4 fdm=marker
912 * vim<600: noet sw=4 ts=4