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
);
171 o
->message
= http_message_init(NULL
);
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_obj(zval
*this_ptr TSRMLS_DC
)
185 getObject(http_message_object
, obj
);
186 return http_message_object_new_ex(Z_OBJCE_P(this_ptr
), http_message_dup(obj
->message
), NULL
);
189 static inline void _http_message_object_declare_default_properties(TSRMLS_D
)
191 zend_class_entry
*ce
= http_message_object_ce
;
194 DCL_CONST(long, "TYPE_NONE", HTTP_MSG_NONE
);
195 DCL_CONST(long, "TYPE_REQUEST", HTTP_MSG_REQUEST
);
196 DCL_CONST(long, "TYPE_RESPONSE", HTTP_MSG_RESPONSE
);
199 DCL_PROP(PROTECTED
, long, type
, HTTP_MSG_NONE
);
200 DCL_PROP(PROTECTED
, string
, body
, "");
201 DCL_PROP(PROTECTED
, string
, requestMethod
, "");
202 DCL_PROP(PROTECTED
, string
, requestUri
, "");
203 DCL_PROP(PROTECTED
, long, responseCode
, 0);
204 DCL_PROP_N(PROTECTED
, httpVersion
);
205 DCL_PROP_N(PROTECTED
, headers
);
206 DCL_PROP_N(PROTECTED
, parentMessage
);
209 void _http_message_object_free(zend_object
*object TSRMLS_DC
)
211 http_message_object
*o
= (http_message_object
*) object
;
214 zend_hash_destroy(OBJ_PROP(o
));
215 FREE_HASHTABLE(OBJ_PROP(o
));
218 http_message_dtor(o
->message
);
224 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
226 getObjectEx(http_message_object
, obj
, object
);
227 http_message
*msg
= obj
->message
;
230 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1);
232 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
234 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
235 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
239 if (type
== BP_VAR_W
) {
240 zend_error(E_ERROR
, "Cannot access HttpMessage properties by reference or array key/index");
244 ALLOC_ZVAL(return_value
);
245 return_value
->refcount
= 0;
246 return_value
->is_ref
= 0;
254 case HTTP_MSG_PROPHASH_TYPE
:
255 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
256 RETVAL_LONG(msg
->type
);
259 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
260 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
261 RETVAL_DOUBLE(msg
->http
.version
);
264 case HTTP_MSG_PROPHASH_BODY
:
265 case HTTP_MSG_CHILD_PROPHASH_BODY
:
266 phpstr_fix(PHPSTR(msg
));
267 RETVAL_PHPSTR(PHPSTR(msg
), 0, 1);
270 case HTTP_MSG_PROPHASH_HEADERS
:
271 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
272 array_init(return_value
);
273 zend_hash_copy(Z_ARRVAL_P(return_value
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
276 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
277 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
279 RETVAL_OBJVAL(obj
->parent
);
285 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
286 case HTTP_MSG_CHILD_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 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URI
:
296 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.URI
) {
297 RETVAL_STRING(msg
->http
.info
.request
.URI
, 1);
303 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
304 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
305 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
306 RETVAL_LONG(msg
->http
.info
.response
.code
);
312 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
313 case HTTP_MSG_CHILD_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 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
356 convert_to_long_ex(&value
);
357 http_message_set_type(msg
, Z_LVAL_P(value
));
360 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
361 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
362 convert_to_double_ex(&value
);
363 msg
->http
.version
= Z_DVAL_P(value
);
366 case HTTP_MSG_PROPHASH_BODY
:
367 case HTTP_MSG_CHILD_PROPHASH_BODY
:
368 convert_to_string_ex(&value
);
369 phpstr_dtor(PHPSTR(msg
));
370 phpstr_from_string_ex(PHPSTR(msg
), Z_STRVAL_P(value
), Z_STRLEN_P(value
));
373 case HTTP_MSG_PROPHASH_HEADERS
:
374 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
375 convert_to_array_ex(&value
);
376 zend_hash_clean(&msg
->hdrs
);
377 zend_hash_copy(&msg
->hdrs
, Z_ARRVAL_P(value
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
380 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
381 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
382 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), http_message_object_ce TSRMLS_CC
)) {
385 tmp
.value
.obj
= obj
->parent
;
388 Z_OBJ_ADDREF_P(value
);
389 obj
->parent
= value
->value
.obj
;
393 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
394 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
395 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
396 convert_to_string_ex(&value
);
397 STR_SET(msg
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
401 case HTTP_MSG_PROPHASH_REQUEST_URI
:
402 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URI
:
403 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
404 convert_to_string_ex(&value
);
405 STR_SET(msg
->http
.info
.request
.URI
, estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
409 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
410 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
411 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
412 convert_to_long_ex(&value
);
413 msg
->http
.info
.response
.code
= Z_LVAL_P(value
);
417 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
418 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
419 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
420 convert_to_string_ex(&value
);
421 STR_SET(msg
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
427 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
433 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
)
436 getObjectEx(http_message_object
, obj
, object
);
437 http_message
*msg
= obj
->message
;
438 HashTable
*props
= OBJ_PROP(obj
);
441 INIT_ZARR(array
, props
);
443 #define ASSOC_PROP(array, ptype, name, val) \
447 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
448 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+4, val); \
449 efree(m_prop_name); \
451 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
452 #define ASSOC_STRINGL(array, name, val, len) \
456 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
457 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+4, val, len, 1); \
458 efree(m_prop_name); \
461 ASSOC_PROP(array
, long, "type", msg
->type
);
462 ASSOC_PROP(array
, double, "httpVersion", msg
->http
.version
);
466 case HTTP_MSG_REQUEST
:
467 ASSOC_PROP(array
, long, "responseCode", 0);
468 ASSOC_STRINGL(array
, "responseStatus", "", 0);
469 ASSOC_STRING(array
, "requestMethod", msg
->http
.info
.request
.method
);
470 ASSOC_STRING(array
, "requestUri", msg
->http
.info
.request
.URI
);
473 case HTTP_MSG_RESPONSE
:
474 ASSOC_PROP(array
, long, "responseCode", msg
->http
.info
.response
.code
);
475 ASSOC_STRING(array
, "responseStatus", msg
->http
.info
.response
.status
);
476 ASSOC_STRINGL(array
, "requestMethod", "", 0);
477 ASSOC_STRINGL(array
, "requestUri", "", 0);
482 ASSOC_PROP(array
, long, "responseCode", 0);
483 ASSOC_STRINGL(array
, "responseStatus", "", 0);
484 ASSOC_STRINGL(array
, "requestMethod", "", 0);
485 ASSOC_STRINGL(array
, "requestUri", "", 0);
489 MAKE_STD_ZVAL(headers
);
491 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
492 ASSOC_PROP(array
, zval
, "headers", headers
);
493 ASSOC_STRINGL(array
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
495 return OBJ_PROP(obj
);
498 /* ### USERLAND ### */
500 /* {{{ proto void HttpMessage::__construct([string message])
502 * Instantiate a new HttpMessage object.
504 * Accepts an optional string parameter containing a single or several
505 * consecutive HTTP messages. The constructed object will actually
506 * represent the *last* message of the passed string. If there were
507 * prior messages, those can be accessed by HttpMessage::getParentMessage().
509 * Throws HttpMalformedHeaderException.
511 PHP_METHOD(HttpMessage
, __construct
)
513 char *message
= NULL
;
515 getObject(http_message_object
, obj
);
518 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
519 if (obj
->message
= http_message_parse(message
, length
)) {
520 if (obj
->message
->parent
) {
521 obj
->parent
= http_message_object_new_ex(Z_OBJCE_P(getThis()), obj
->message
->parent
, NULL
);
524 } else if (!obj
->message
) {
525 obj
->message
= http_message_new();
531 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
533 * Create an HttpMessage object from a string. Kind of a static constructor.
535 * Expects a string parameter containing a sinlge or several consecutive
538 * Returns an HttpMessage object on success or NULL on failure.
540 * Throws HttpMalformedHeadersException.
542 PHP_METHOD(HttpMessage
, fromString
)
546 http_message
*msg
= NULL
;
551 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &string
, &length
)) {
552 if (msg
= http_message_parse(string
, length
)) {
553 Z_TYPE_P(return_value
) = IS_OBJECT
;
554 return_value
->value
.obj
= http_message_object_new_ex(http_message_object_ce
, msg
, NULL
);
561 /* {{{ proto string HttpMessage::getBody()
563 * Get the body of the parsed HttpMessage.
565 * Returns the message body as string.
567 PHP_METHOD(HttpMessage
, getBody
)
572 getObject(http_message_object
, obj
);
573 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
578 /* {{{ proto void HttpMessage::setBody(string body)
580 * Set the body of the HttpMessage.
581 * NOTE: Don't forget to update any headers accordingly.
583 * Expects a string parameter containing the new body of the message.
585 PHP_METHOD(HttpMessage
, setBody
)
589 getObject(http_message_object
, obj
);
591 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &body
, &len
)) {
592 phpstr_dtor(PHPSTR(obj
->message
));
593 phpstr_from_string_ex(PHPSTR(obj
->message
), body
, len
);
598 /* {{{ proto array HttpMessage::getHeaders()
600 * Get Message Headers.
602 * Returns an associative array containing the messages HTTP headers.
604 PHP_METHOD(HttpMessage
, getHeaders
)
610 getObject(http_message_object
, obj
);
612 INIT_ZARR(headers
, &obj
->message
->hdrs
);
613 array_init(return_value
);
614 array_copy(&headers
, return_value
);
619 /* {{{ proto void HttpMessage::setHeaders(array headers)
623 * Expects an associative array as parameter containing the new HTTP headers,
624 * which will replace *all* previous HTTP headers of the message.
626 PHP_METHOD(HttpMessage
, setHeaders
)
628 zval
*new_headers
, old_headers
;
629 getObject(http_message_object
, obj
);
631 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
635 zend_hash_clean(&obj
->message
->hdrs
);
636 INIT_ZARR(old_headers
, &obj
->message
->hdrs
);
637 array_copy(new_headers
, &old_headers
);
641 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
643 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
645 * Expects an associative array as parameter containing the additional HTTP headers
646 * to add to the messages existing headers. If the optional bool parameter is true,
647 * and a header with the same name of one to add exists already, this respective
648 * header will be converted to an array containing both header values, otherwise
649 * it will be overwritten with the new header value.
651 PHP_METHOD(HttpMessage
, addHeaders
)
653 zval old_headers
, *new_headers
;
654 zend_bool append
= 0;
655 getObject(http_message_object
, obj
);
657 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
661 INIT_ZARR(old_headers
, &obj
->message
->hdrs
);
663 array_append(new_headers
, &old_headers
);
665 array_merge(new_headers
, &old_headers
);
670 /* {{{ proto int HttpMessage::getType()
672 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
674 * Returns the HttpMessage::TYPE.
676 PHP_METHOD(HttpMessage
, getType
)
681 getObject(http_message_object
, obj
);
682 RETURN_LONG(obj
->message
->type
);
687 /* {{{ proto void HttpMessage::setType(int type)
689 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
691 * Exptects an int parameter, the HttpMessage::TYPE.
693 PHP_METHOD(HttpMessage
, setType
)
696 getObject(http_message_object
, obj
);
698 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
701 http_message_set_type(obj
->message
, type
);
705 /* {{{ proto int HttpMessage::getResponseCode()
707 * Get the Response Code of the Message.
709 * Returns the HTTP response code if the message is of type
710 * HttpMessage::TYPE_RESPONSE, else FALSE.
712 PHP_METHOD(HttpMessage
, getResponseCode
)
717 getObject(http_message_object
, obj
);
718 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
719 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
724 /* {{{ proto bool HttpMessage::setResponseCode(int code)
726 * Set the response code of an HTTP Response Message.
728 * Expects an int parameter with the HTTP response code.
730 * Returns TRUE on success, or FALSE if the message is not of type
731 * HttpMessage::TYPE_RESPONSE or the response code is out of range (100-510).
733 PHP_METHOD(HttpMessage
, setResponseCode
)
736 getObject(http_message_object
, obj
);
738 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
740 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
743 if (code
< 100 || code
> 510) {
744 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid response code (100-510): %ld", code
);
748 obj
->message
->http
.info
.response
.code
= code
;
753 /* {{{ proto string HttpMessage::getRequestMethod()
755 * Get the Request Method of the Message.
757 * Returns the request method name on success, or FALSE if the message is
758 * not of type HttpMessage::TYPE_REQUEST.
760 PHP_METHOD(HttpMessage
, getRequestMethod
)
765 getObject(http_message_object
, obj
);
766 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
767 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
772 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
774 * Set the Request Method of the HTTP Message.
776 * Expects a string parameter containing the request method name.
778 * Returns TRUE on success, or FALSE if the message is not of type
779 * HttpMessage::TYPE_REQUEST or an invalid request method was supplied.
781 PHP_METHOD(HttpMessage
, setRequestMethod
)
785 getObject(http_message_object
, obj
);
787 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
789 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
792 if (method_len
< 1) {
793 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
796 if (SUCCESS
!= http_check_method(method
)) {
797 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unkown request method: %s", method
);
801 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
806 /* {{{ proto string HttpMessage::getRequestUri()
808 * Get the Request URI of the Message.
810 * Returns the request uri as string on success, or FALSE if the message
811 * is not of type HttpMessage::TYPE_REQUEST.
813 PHP_METHOD(HttpMessage
, getRequestUri
)
818 getObject(http_message_object
, obj
);
819 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
820 RETURN_STRING(obj
->message
->http
.info
.request
.URI
, 1);
825 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
827 * Set the Request URI of the HTTP Message.
829 * Expects a string parameters containing the request uri.
831 * Returns TRUE on success, or FALSE if the message is not of type
832 * HttpMessage::TYPE_REQUEST or supplied URI was empty.
834 PHP_METHOD(HttpMessage
, setRequestUri
)
838 getObject(http_message_object
, obj
);
840 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
843 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
845 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUri to an empty string");
849 STR_SET(obj
->message
->http
.info
.request
.URI
, estrndup(URI
, URIlen
));
854 /* {{{ proto string HttpMessage::getHttpVersion()
856 * Get the HTTP Protocol Version of the Message.
858 * Returns the HTTP protocol version as string.
860 PHP_METHOD(HttpMessage
, getHttpVersion
)
866 getObject(http_message_object
, obj
);
868 sprintf(ver
, "%1.1lf", obj
->message
->http
.version
);
869 RETURN_STRINGL(ver
, 3, 1);
874 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
876 * Set the HTTP Protocol version of the Message.
878 * Expects a string parameter containing the HTTP protocol version.
880 * Returns TRUE on success, or FALSE if supplied version is out of range (1.0/1.1).
882 PHP_METHOD(HttpMessage
, setHttpVersion
)
886 getObject(http_message_object
, obj
);
888 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
892 convert_to_double(zv
);
893 sprintf(v
, "%1.1lf", Z_DVAL_P(zv
));
894 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
895 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
899 obj
->message
->http
.version
= Z_DVAL_P(zv
);
904 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
906 * Get parent Message.
908 * Returns the parent HttpMessage on success, or NULL if there's none.
910 PHP_METHOD(HttpMessage
, getParentMessage
)
915 getObject(http_message_object
, obj
);
917 if (obj
->message
->parent
) {
918 RETVAL_OBJVAL(obj
->parent
);
926 /* {{{ proto bool HttpMessage::send()
928 * Send the Message according to its type as Response or Request.
929 * This provides limited functionality compared to HttpRequest and HttpResponse.
931 * Returns TRUE on success, or FALSE on failure.
933 PHP_METHOD(HttpMessage
, send
)
935 getObject(http_message_object
, obj
);
939 RETURN_SUCCESS(http_message_send(obj
->message
));
943 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
945 * Get the string representation of the Message.
947 * Accepts a bool parameter which specifies whether the returned string
948 * should also contain any parent messages.
950 * Returns the full message as string.
952 PHP_METHOD(HttpMessage
, toString
)
957 zend_bool include_parent
= 0;
958 getObject(http_message_object
, obj
);
960 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
964 if (include_parent
) {
965 http_message_serialize(obj
->message
, &string
, &length
);
967 http_message_tostring(obj
->message
, &string
, &length
);
969 RETURN_STRINGL(string
, length
, 0);
974 #endif /* ZEND_ENGINE_2 */
981 * vim600: noet sw=4 ts=4 fdm=marker
982 * vim<600: noet sw=4 ts=4