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"
33 # include "zend_interfaces.h"
34 # if defined(HAVE_SPL)
35 /* SPL doesn't install its headers */
36 extern PHPAPI zend_class_entry
*spl_ce_Countable
;
40 ZEND_EXTERN_MODULE_GLOBALS(http
);
42 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpMessage, method, ret_ref, req_args)
43 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpMessage, method, ret_ref)
44 #define HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, HTTP_ARGS(HttpMessage, method), visibility)
46 HTTP_BEGIN_ARGS(__construct
, 0, 0)
47 HTTP_ARG_VAL(message
, 0)
50 HTTP_BEGIN_ARGS(fromString
, 1, 1)
51 HTTP_ARG_VAL(message
, 0)
54 HTTP_EMPTY_ARGS(getBody
, 0);
55 HTTP_BEGIN_ARGS(setBody
, 0, 1)
59 HTTP_EMPTY_ARGS(getHeaders
, 0);
60 HTTP_BEGIN_ARGS(setHeaders
, 0, 1)
61 HTTP_ARG_VAL(headers
, 0)
64 HTTP_BEGIN_ARGS(addHeaders
, 0, 1)
65 HTTP_ARG_VAL(headers
, 0)
66 HTTP_ARG_VAL(append
, 0)
69 HTTP_EMPTY_ARGS(getType
, 0);
70 HTTP_BEGIN_ARGS(setType
, 0, 1)
74 HTTP_EMPTY_ARGS(getResponseCode
, 0);
75 HTTP_BEGIN_ARGS(setResponseCode
, 0, 1)
76 HTTP_ARG_VAL(response_code
, 0)
79 HTTP_EMPTY_ARGS(getRequestMethod
, 0);
80 HTTP_BEGIN_ARGS(setRequestMethod
, 0, 1)
81 HTTP_ARG_VAL(request_method
, 0)
84 HTTP_EMPTY_ARGS(getRequestUri
, 0);
85 HTTP_BEGIN_ARGS(setRequestUri
, 0, 1)
89 HTTP_EMPTY_ARGS(getHttpVersion
, 0);
90 HTTP_BEGIN_ARGS(setHttpVersion
, 0, 1)
91 HTTP_ARG_VAL(http_version
, 0)
94 HTTP_EMPTY_ARGS(getParentMessage
, 1);
95 HTTP_EMPTY_ARGS(send
, 0);
96 HTTP_BEGIN_ARGS(toString
, 0, 0)
97 HTTP_ARG_VAL(include_parent
, 0)
100 HTTP_EMPTY_ARGS(count
, 0);
102 HTTP_EMPTY_ARGS(serialize
, 0);
103 HTTP_BEGIN_ARGS(unserialize
, 0, 1)
104 HTTP_ARG_VAL(serialized
, 0)
107 #define http_message_object_declare_default_properties() _http_message_object_declare_default_properties(TSRMLS_C)
108 static inline void _http_message_object_declare_default_properties(TSRMLS_D
);
109 #define http_message_object_read_prop _http_message_object_read_prop
110 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
);
111 #define http_message_object_write_prop _http_message_object_write_prop
112 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
);
113 #define http_message_object_get_props _http_message_object_get_props
114 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
);
116 zend_class_entry
*http_message_object_ce
;
117 zend_function_entry http_message_object_fe
[] = {
118 HTTP_MESSAGE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
119 HTTP_MESSAGE_ME(getBody
, ZEND_ACC_PUBLIC
)
120 HTTP_MESSAGE_ME(setBody
, ZEND_ACC_PUBLIC
)
121 HTTP_MESSAGE_ME(getHeaders
, ZEND_ACC_PUBLIC
)
122 HTTP_MESSAGE_ME(setHeaders
, ZEND_ACC_PUBLIC
)
123 HTTP_MESSAGE_ME(addHeaders
, ZEND_ACC_PUBLIC
)
124 HTTP_MESSAGE_ME(getType
, ZEND_ACC_PUBLIC
)
125 HTTP_MESSAGE_ME(setType
, ZEND_ACC_PUBLIC
)
126 HTTP_MESSAGE_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
127 HTTP_MESSAGE_ME(setResponseCode
, ZEND_ACC_PUBLIC
)
128 HTTP_MESSAGE_ME(getRequestMethod
, ZEND_ACC_PUBLIC
)
129 HTTP_MESSAGE_ME(setRequestMethod
, ZEND_ACC_PUBLIC
)
130 HTTP_MESSAGE_ME(getRequestUri
, ZEND_ACC_PUBLIC
)
131 HTTP_MESSAGE_ME(setRequestUri
, ZEND_ACC_PUBLIC
)
132 HTTP_MESSAGE_ME(getHttpVersion
, ZEND_ACC_PUBLIC
)
133 HTTP_MESSAGE_ME(setHttpVersion
, ZEND_ACC_PUBLIC
)
134 HTTP_MESSAGE_ME(getParentMessage
, ZEND_ACC_PUBLIC
)
135 HTTP_MESSAGE_ME(send
, ZEND_ACC_PUBLIC
)
136 HTTP_MESSAGE_ME(toString
, ZEND_ACC_PUBLIC
)
138 /* implements Countable */
139 HTTP_MESSAGE_ME(count
, ZEND_ACC_PUBLIC
)
141 /* implements Serializable */
142 HTTP_MESSAGE_ME(serialize
, ZEND_ACC_PUBLIC
)
143 HTTP_MESSAGE_ME(unserialize
, ZEND_ACC_PUBLIC
)
145 ZEND_MALIAS(HttpMessage
, __toString
, toString
, HTTP_ARGS(HttpMessage
, toString
), ZEND_ACC_PUBLIC
)
147 HTTP_MESSAGE_ME(fromString
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
151 static zend_object_handlers http_message_object_handlers
;
153 PHP_MINIT_FUNCTION(http_message_object
)
155 HTTP_REGISTER_CLASS_EX(HttpMessage
, http_message_object
, NULL
, 0);
158 zend_class_implements(http_message_object_ce TSRMLS_CC
, 2, spl_ce_Countable
, zend_ce_serializable
);
160 zend_class_implements(http_message_object_ce TSRMLS_CC
, 1, zend_ce_serializable
);
164 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE
);
165 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST
);
166 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE
);
168 http_message_object_handlers
.clone_obj
= _http_message_object_clone_obj
;
169 http_message_object_handlers
.read_property
= http_message_object_read_prop
;
170 http_message_object_handlers
.write_property
= http_message_object_write_prop
;
171 http_message_object_handlers
.get_properties
= http_message_object_get_props
;
172 http_message_object_handlers
.get_property_ptr_ptr
= NULL
;
177 zend_object_value
_http_message_object_new(zend_class_entry
*ce TSRMLS_DC
)
179 return http_message_object_new_ex(ce
, NULL
, NULL
);
182 zend_object_value
_http_message_object_new_ex(zend_class_entry
*ce
, http_message
*msg
, http_message_object
**ptr TSRMLS_DC
)
184 zend_object_value ov
;
185 http_message_object
*o
;
187 o
= ecalloc(1, sizeof(http_message_object
));
197 o
->parent
= http_message_object_new_ex(ce
, msg
->parent
, NULL
);
201 ALLOC_HASHTABLE(OBJ_PROP(o
));
202 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
204 ov
.handle
= putObject(http_message_object
, o
);
205 ov
.handlers
= &http_message_object_handlers
;
210 zend_object_value
_http_message_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
212 getObject(http_message_object
, obj
);
213 return http_message_object_new_ex(Z_OBJCE_P(this_ptr
), http_message_dup(obj
->message
), NULL
);
216 static inline void _http_message_object_declare_default_properties(TSRMLS_D
)
218 zend_class_entry
*ce
= http_message_object_ce
;
221 DCL_CONST(long, "TYPE_NONE", HTTP_MSG_NONE
);
222 DCL_CONST(long, "TYPE_REQUEST", HTTP_MSG_REQUEST
);
223 DCL_CONST(long, "TYPE_RESPONSE", HTTP_MSG_RESPONSE
);
226 DCL_PROP(PROTECTED
, long, type
, HTTP_MSG_NONE
);
227 DCL_PROP(PROTECTED
, string
, body
, "");
228 DCL_PROP(PROTECTED
, string
, requestMethod
, "");
229 DCL_PROP(PROTECTED
, string
, requestUri
, "");
230 DCL_PROP(PROTECTED
, long, responseCode
, 0);
231 DCL_PROP_N(PROTECTED
, httpVersion
);
232 DCL_PROP_N(PROTECTED
, headers
);
233 DCL_PROP_N(PROTECTED
, parentMessage
);
236 void _http_message_object_free(zend_object
*object TSRMLS_DC
)
238 http_message_object
*o
= (http_message_object
*) object
;
241 zend_hash_destroy(OBJ_PROP(o
));
242 FREE_HASHTABLE(OBJ_PROP(o
));
245 http_message_dtor(o
->message
);
251 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
253 getObjectEx(http_message_object
, obj
, object
);
254 http_message
*msg
= obj
->message
;
257 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1);
259 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
261 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
262 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
266 if (type
== BP_VAR_W
) {
267 zend_error(E_ERROR
, "Cannot access HttpMessage properties by reference or array key/index");
271 ALLOC_ZVAL(return_value
);
272 return_value
->refcount
= 0;
273 return_value
->is_ref
= 0;
281 case HTTP_MSG_PROPHASH_TYPE
:
282 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
283 RETVAL_LONG(msg
->type
);
286 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
287 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
288 RETVAL_DOUBLE(msg
->http
.version
);
291 case HTTP_MSG_PROPHASH_BODY
:
292 case HTTP_MSG_CHILD_PROPHASH_BODY
:
293 phpstr_fix(PHPSTR(msg
));
294 RETVAL_PHPSTR(PHPSTR(msg
), 0, 1);
297 case HTTP_MSG_PROPHASH_HEADERS
:
298 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
299 array_init(return_value
);
300 zend_hash_copy(Z_ARRVAL_P(return_value
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
303 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
304 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
306 RETVAL_OBJVAL(obj
->parent
);
312 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
313 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
314 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.method
) {
315 RETVAL_STRING(msg
->http
.info
.request
.method
, 1);
321 case HTTP_MSG_PROPHASH_REQUEST_URI
:
322 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URI
:
323 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.URI
) {
324 RETVAL_STRING(msg
->http
.info
.request
.URI
, 1);
330 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
331 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
332 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
333 RETVAL_LONG(msg
->http
.info
.response
.code
);
339 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
340 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
341 if (HTTP_MSG_TYPE(RESPONSE
, msg
) && msg
->http
.info
.response
.status
) {
342 RETVAL_STRING(msg
->http
.info
.response
.status
, 1);
350 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
360 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
)
362 getObjectEx(http_message_object
, obj
, object
);
363 http_message
*msg
= obj
->message
;
366 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
) + 1);
368 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
370 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
371 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
387 case HTTP_MSG_PROPHASH_TYPE
:
388 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
389 convert_to_long(cpy
);
390 http_message_set_type(msg
, Z_LVAL_P(cpy
));
393 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
394 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
395 convert_to_double(cpy
);
396 msg
->http
.version
= Z_DVAL_P(cpy
);
399 case HTTP_MSG_PROPHASH_BODY
:
400 case HTTP_MSG_CHILD_PROPHASH_BODY
:
401 convert_to_string(cpy
);
402 phpstr_dtor(PHPSTR(msg
));
403 phpstr_from_string_ex(PHPSTR(msg
), Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
));
406 case HTTP_MSG_PROPHASH_HEADERS
:
407 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
408 convert_to_array(cpy
);
409 zend_hash_clean(&msg
->hdrs
);
410 zend_hash_copy(&msg
->hdrs
, Z_ARRVAL_P(cpy
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
413 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
414 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
415 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), http_message_object_ce TSRMLS_CC
)) {
418 tmp
.value
.obj
= obj
->parent
;
421 Z_OBJ_ADDREF_P(value
);
422 obj
->parent
= value
->value
.obj
;
426 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
427 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
428 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
429 convert_to_string(cpy
);
430 STR_SET(msg
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
434 case HTTP_MSG_PROPHASH_REQUEST_URI
:
435 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URI
:
436 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
437 convert_to_string(cpy
);
438 STR_SET(msg
->http
.info
.request
.URI
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
442 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
443 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
444 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
445 convert_to_long(cpy
);
446 msg
->http
.info
.response
.code
= Z_LVAL_P(cpy
);
450 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
451 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
452 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
453 convert_to_string(cpy
);
454 STR_SET(msg
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
460 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
467 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
)
470 getObjectEx(http_message_object
, obj
, object
);
471 http_message
*msg
= obj
->message
;
472 HashTable
*props
= OBJ_PROP(obj
);
475 INIT_ZARR(array
, props
);
477 #define ASSOC_PROP(array, ptype, name, val) \
481 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
482 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+4, val); \
483 efree(m_prop_name); \
485 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
486 #define ASSOC_STRINGL(array, name, val, len) \
490 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
491 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+4, val, len, 1); \
492 efree(m_prop_name); \
495 ASSOC_PROP(array
, long, "type", msg
->type
);
496 ASSOC_PROP(array
, double, "httpVersion", msg
->http
.version
);
500 case HTTP_MSG_REQUEST
:
501 ASSOC_PROP(array
, long, "responseCode", 0);
502 ASSOC_STRINGL(array
, "responseStatus", "", 0);
503 ASSOC_STRING(array
, "requestMethod", msg
->http
.info
.request
.method
);
504 ASSOC_STRING(array
, "requestUri", msg
->http
.info
.request
.URI
);
507 case HTTP_MSG_RESPONSE
:
508 ASSOC_PROP(array
, long, "responseCode", msg
->http
.info
.response
.code
);
509 ASSOC_STRING(array
, "responseStatus", msg
->http
.info
.response
.status
);
510 ASSOC_STRINGL(array
, "requestMethod", "", 0);
511 ASSOC_STRINGL(array
, "requestUri", "", 0);
516 ASSOC_PROP(array
, long, "responseCode", 0);
517 ASSOC_STRINGL(array
, "responseStatus", "", 0);
518 ASSOC_STRINGL(array
, "requestMethod", "", 0);
519 ASSOC_STRINGL(array
, "requestUri", "", 0);
523 MAKE_STD_ZVAL(headers
);
525 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
526 ASSOC_PROP(array
, zval
, "headers", headers
);
527 ASSOC_STRINGL(array
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
529 return OBJ_PROP(obj
);
532 /* ### USERLAND ### */
534 /* {{{ proto void HttpMessage::__construct([string message])
536 * Instantiate a new HttpMessage object.
538 * Accepts an optional string parameter containing a single or several
539 * consecutive HTTP messages. The constructed object will actually
540 * represent the *last* message of the passed string. If there were
541 * prior messages, those can be accessed by HttpMessage::getParentMessage().
543 * Throws HttpMalformedHeaderException.
545 PHP_METHOD(HttpMessage
, __construct
)
548 char *message
= NULL
;
550 getObject(http_message_object
, obj
);
553 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
554 http_message
*msg
= obj
->message
;
556 http_message_dtor(msg
);
557 if ((obj
->message
= http_message_parse_ex(msg
, message
, length
))) {
558 if (obj
->message
->parent
) {
559 obj
->parent
= http_message_object_new_ex(Z_OBJCE_P(getThis()), obj
->message
->parent
, NULL
);
562 obj
->message
= http_message_init(msg
);
566 obj
->message
= http_message_new();
572 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
574 * Create an HttpMessage object from a string. Kind of a static constructor.
576 * Expects a string parameter containing a sinlge or several consecutive
579 * Returns an HttpMessage object on success or NULL on failure.
581 * Throws HttpMalformedHeadersException.
583 PHP_METHOD(HttpMessage
, fromString
)
587 http_message
*msg
= NULL
;
592 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &string
, &length
)) {
593 if ((msg
= http_message_parse(string
, length
))) {
594 ZVAL_OBJVAL(return_value
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
601 /* {{{ proto string HttpMessage::getBody()
603 * Get the body of the parsed HttpMessage.
605 * Returns the message body as string.
607 PHP_METHOD(HttpMessage
, getBody
)
612 getObject(http_message_object
, obj
);
613 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
618 /* {{{ proto void HttpMessage::setBody(string body)
620 * Set the body of the HttpMessage.
621 * NOTE: Don't forget to update any headers accordingly.
623 * Expects a string parameter containing the new body of the message.
625 PHP_METHOD(HttpMessage
, setBody
)
629 getObject(http_message_object
, obj
);
631 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &body
, &len
)) {
632 phpstr_dtor(PHPSTR(obj
->message
));
633 phpstr_from_string_ex(PHPSTR(obj
->message
), body
, len
);
638 /* {{{ proto array HttpMessage::getHeaders()
640 * Get Message Headers.
642 * Returns an associative array containing the messages HTTP headers.
644 PHP_METHOD(HttpMessage
, getHeaders
)
650 getObject(http_message_object
, obj
);
652 INIT_ZARR(headers
, &obj
->message
->hdrs
);
653 array_init(return_value
);
654 array_copy(&headers
, return_value
);
659 /* {{{ proto void HttpMessage::setHeaders(array headers)
663 * Expects an associative array as parameter containing the new HTTP headers,
664 * which will replace *all* previous HTTP headers of the message.
666 PHP_METHOD(HttpMessage
, setHeaders
)
668 zval
*new_headers
, old_headers
;
669 getObject(http_message_object
, obj
);
671 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
675 zend_hash_clean(&obj
->message
->hdrs
);
676 INIT_ZARR(old_headers
, &obj
->message
->hdrs
);
677 array_copy(new_headers
, &old_headers
);
681 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
683 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
685 * Expects an associative array as parameter containing the additional HTTP headers
686 * to add to the messages existing headers. If the optional bool parameter is true,
687 * and a header with the same name of one to add exists already, this respective
688 * header will be converted to an array containing both header values, otherwise
689 * it will be overwritten with the new header value.
691 PHP_METHOD(HttpMessage
, addHeaders
)
693 zval old_headers
, *new_headers
;
694 zend_bool append
= 0;
695 getObject(http_message_object
, obj
);
697 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
701 INIT_ZARR(old_headers
, &obj
->message
->hdrs
);
703 array_append(new_headers
, &old_headers
);
705 array_merge(new_headers
, &old_headers
);
710 /* {{{ proto int HttpMessage::getType()
712 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
714 * Returns the HttpMessage::TYPE.
716 PHP_METHOD(HttpMessage
, getType
)
721 getObject(http_message_object
, obj
);
722 RETURN_LONG(obj
->message
->type
);
727 /* {{{ proto void HttpMessage::setType(int type)
729 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
731 * Exptects an int parameter, the HttpMessage::TYPE.
733 PHP_METHOD(HttpMessage
, setType
)
736 getObject(http_message_object
, obj
);
738 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
741 http_message_set_type(obj
->message
, type
);
745 /* {{{ proto int HttpMessage::getResponseCode()
747 * Get the Response Code of the Message.
749 * Returns the HTTP response code if the message is of type
750 * HttpMessage::TYPE_RESPONSE, else FALSE.
752 PHP_METHOD(HttpMessage
, getResponseCode
)
757 getObject(http_message_object
, obj
);
758 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
759 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
764 /* {{{ proto bool HttpMessage::setResponseCode(int code)
766 * Set the response code of an HTTP Response Message.
768 * Expects an int parameter with the HTTP response code.
770 * Returns TRUE on success, or FALSE if the message is not of type
771 * HttpMessage::TYPE_RESPONSE or the response code is out of range (100-510).
773 PHP_METHOD(HttpMessage
, setResponseCode
)
776 getObject(http_message_object
, obj
);
778 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
780 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
783 if (code
< 100 || code
> 510) {
784 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid response code (100-510): %ld", code
);
788 obj
->message
->http
.info
.response
.code
= code
;
793 /* {{{ proto string HttpMessage::getRequestMethod()
795 * Get the Request Method of the Message.
797 * Returns the request method name on success, or FALSE if the message is
798 * not of type HttpMessage::TYPE_REQUEST.
800 PHP_METHOD(HttpMessage
, getRequestMethod
)
805 getObject(http_message_object
, obj
);
806 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
807 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
812 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
814 * Set the Request Method of the HTTP Message.
816 * Expects a string parameter containing the request method name.
818 * Returns TRUE on success, or FALSE if the message is not of type
819 * HttpMessage::TYPE_REQUEST or an invalid request method was supplied.
821 PHP_METHOD(HttpMessage
, setRequestMethod
)
825 getObject(http_message_object
, obj
);
827 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
829 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
832 if (method_len
< 1) {
833 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
836 if (SUCCESS
!= http_check_method(method
)) {
837 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unkown request method: %s", method
);
841 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
846 /* {{{ proto string HttpMessage::getRequestUri()
848 * Get the Request URI of the Message.
850 * Returns the request uri as string on success, or FALSE if the message
851 * is not of type HttpMessage::TYPE_REQUEST.
853 PHP_METHOD(HttpMessage
, getRequestUri
)
858 getObject(http_message_object
, obj
);
859 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
860 RETURN_STRING(obj
->message
->http
.info
.request
.URI
, 1);
865 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
867 * Set the Request URI of the HTTP Message.
869 * Expects a string parameters containing the request uri.
871 * Returns TRUE on success, or FALSE if the message is not of type
872 * HttpMessage::TYPE_REQUEST or supplied URI was empty.
874 PHP_METHOD(HttpMessage
, setRequestUri
)
878 getObject(http_message_object
, obj
);
880 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
883 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
885 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUri to an empty string");
889 STR_SET(obj
->message
->http
.info
.request
.URI
, estrndup(URI
, URIlen
));
894 /* {{{ proto string HttpMessage::getHttpVersion()
896 * Get the HTTP Protocol Version of the Message.
898 * Returns the HTTP protocol version as string.
900 PHP_METHOD(HttpMessage
, getHttpVersion
)
906 getObject(http_message_object
, obj
);
908 sprintf(ver
, "%1.1lf", obj
->message
->http
.version
);
909 RETURN_STRINGL(ver
, 3, 1);
914 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
916 * Set the HTTP Protocol version of the Message.
918 * Expects a string parameter containing the HTTP protocol version.
920 * Returns TRUE on success, or FALSE if supplied version is out of range (1.0/1.1).
922 PHP_METHOD(HttpMessage
, setHttpVersion
)
926 getObject(http_message_object
, obj
);
928 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
932 convert_to_double(zv
);
933 sprintf(v
, "%1.1lf", Z_DVAL_P(zv
));
934 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
935 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
939 obj
->message
->http
.version
= Z_DVAL_P(zv
);
944 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
946 * Get parent Message.
948 * Returns the parent HttpMessage on success, or NULL if there's none.
950 PHP_METHOD(HttpMessage
, getParentMessage
)
955 getObject(http_message_object
, obj
);
957 if (obj
->message
->parent
) {
958 RETVAL_OBJVAL(obj
->parent
);
966 /* {{{ proto bool HttpMessage::send()
968 * Send the Message according to its type as Response or Request.
969 * This provides limited functionality compared to HttpRequest and HttpResponse.
971 * Returns TRUE on success, or FALSE on failure.
973 PHP_METHOD(HttpMessage
, send
)
975 getObject(http_message_object
, obj
);
979 RETURN_SUCCESS(http_message_send(obj
->message
));
983 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
985 * Get the string representation of the Message.
987 * Accepts a bool parameter which specifies whether the returned string
988 * should also contain any parent messages.
990 * Returns the full message as string.
992 PHP_METHOD(HttpMessage
, toString
)
997 zend_bool include_parent
= 0;
998 getObject(http_message_object
, obj
);
1000 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1004 if (include_parent
) {
1005 http_message_serialize(obj
->message
, &string
, &length
);
1007 http_message_tostring(obj
->message
, &string
, &length
);
1009 RETURN_STRINGL(string
, length
, 0);
1014 /* {{{ proto int HttpMessage::count()
1016 * Implements Countable.
1018 * Returns the number of parent messages + 1.
1020 PHP_METHOD(HttpMessage
, count
)
1025 getObject(http_message_object
, obj
);
1027 for (i
= 0, msg
= obj
->message
; msg
; msg
= msg
->parent
, ++i
);
1033 /* {{{ proto string HttpMessage::serialize()
1035 * Implements Serializable.
1037 * Returns the serialized representation of the HttpMessage.
1039 PHP_METHOD(HttpMessage
, serialize
)
1044 getObject(http_message_object
, obj
);
1046 http_message_serialize(obj
->message
, &string
, &length
);
1047 RETURN_STRINGL(string
, length
, 0);
1052 /* {{{ proto void HttpMessage::unserialize(string serialized)
1054 * Implements Serializable.
1056 * Re-constructs the HttpMessage based upon the serialized string.
1058 PHP_METHOD(HttpMessage
, unserialize
)
1062 getObject(http_message_object
, obj
);
1064 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &serialized
, &length
)) {
1065 http_message_dtor(obj
->message
);
1066 if (!http_message_parse_ex(obj
->message
, serialized
, (size_t) length
)) {
1067 http_error(HE_ERROR
, HTTP_E_RUNTIME
, "Could not unserialize HttpMessage");
1068 http_message_init(obj
->message
);
1074 #endif /* ZEND_ENGINE_2 */
1081 * vim600: noet sw=4 ts=4 fdm=marker
1082 * vim<600: noet sw=4 ts=4