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-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
19 #include "php_http_api.h"
20 #include "php_http_message_api.h"
21 #include "php_http_message_object.h"
22 #include "php_http_exception_object.h"
25 # include "zend_interfaces.h"
26 # if defined(HAVE_SPL)
27 /* SPL doesn't install its headers */
28 extern PHPAPI zend_class_entry
*spl_ce_Countable
;
32 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpMessage, method, ret_ref, req_args)
33 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpMessage, method, ret_ref)
34 #define HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, HTTP_ARGS(HttpMessage, method), visibility)
36 HTTP_BEGIN_ARGS(__construct
, 0, 0)
37 HTTP_ARG_VAL(message
, 0)
40 HTTP_BEGIN_ARGS(fromString
, 1, 1)
41 HTTP_ARG_VAL(message
, 0)
44 HTTP_EMPTY_ARGS(getBody
, 0);
45 HTTP_BEGIN_ARGS(setBody
, 0, 1)
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(getRequestUrl
, 0);
75 HTTP_BEGIN_ARGS(setRequestUrl
, 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
, 0);
85 HTTP_EMPTY_ARGS(send
, 0);
86 HTTP_BEGIN_ARGS(toString
, 0, 0)
87 HTTP_ARG_VAL(include_parent
, 0)
90 HTTP_EMPTY_ARGS(count
, 0);
92 HTTP_EMPTY_ARGS(serialize
, 0);
93 HTTP_BEGIN_ARGS(unserialize
, 0, 1)
94 HTTP_ARG_VAL(serialized
, 0)
97 HTTP_EMPTY_ARGS(detach
, 0);
99 HTTP_BEGIN_ARGS(prepend
, 0, 1)
100 HTTP_ARG_OBJ(HttpMessage
, message
, 0)
103 #define http_message_object_declare_default_properties() _http_message_object_declare_default_properties(TSRMLS_C)
104 static inline void _http_message_object_declare_default_properties(TSRMLS_D
);
105 #define http_message_object_read_prop _http_message_object_read_prop
106 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
);
107 #define http_message_object_write_prop _http_message_object_write_prop
108 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
);
109 #define http_message_object_get_props _http_message_object_get_props
110 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
);
112 zend_class_entry
*http_message_object_ce
;
113 zend_function_entry http_message_object_fe
[] = {
114 HTTP_MESSAGE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
115 HTTP_MESSAGE_ME(getBody
, ZEND_ACC_PUBLIC
)
116 HTTP_MESSAGE_ME(setBody
, ZEND_ACC_PUBLIC
)
117 HTTP_MESSAGE_ME(getHeaders
, ZEND_ACC_PUBLIC
)
118 HTTP_MESSAGE_ME(setHeaders
, ZEND_ACC_PUBLIC
)
119 HTTP_MESSAGE_ME(addHeaders
, ZEND_ACC_PUBLIC
)
120 HTTP_MESSAGE_ME(getType
, ZEND_ACC_PUBLIC
)
121 HTTP_MESSAGE_ME(setType
, ZEND_ACC_PUBLIC
)
122 HTTP_MESSAGE_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
123 HTTP_MESSAGE_ME(setResponseCode
, ZEND_ACC_PUBLIC
)
124 HTTP_MESSAGE_ME(getRequestMethod
, ZEND_ACC_PUBLIC
)
125 HTTP_MESSAGE_ME(setRequestMethod
, ZEND_ACC_PUBLIC
)
126 HTTP_MESSAGE_ME(getRequestUrl
, ZEND_ACC_PUBLIC
)
127 HTTP_MESSAGE_ME(setRequestUrl
, ZEND_ACC_PUBLIC
)
128 HTTP_MESSAGE_ME(getHttpVersion
, ZEND_ACC_PUBLIC
)
129 HTTP_MESSAGE_ME(setHttpVersion
, ZEND_ACC_PUBLIC
)
130 HTTP_MESSAGE_ME(getParentMessage
, ZEND_ACC_PUBLIC
)
131 HTTP_MESSAGE_ME(send
, ZEND_ACC_PUBLIC
)
132 HTTP_MESSAGE_ME(toString
, ZEND_ACC_PUBLIC
)
134 /* implements Countable */
135 HTTP_MESSAGE_ME(count
, ZEND_ACC_PUBLIC
)
137 /* implements Serializable */
138 HTTP_MESSAGE_ME(serialize
, ZEND_ACC_PUBLIC
)
139 HTTP_MESSAGE_ME(unserialize
, ZEND_ACC_PUBLIC
)
141 ZEND_MALIAS(HttpMessage
, __toString
, toString
, HTTP_ARGS(HttpMessage
, toString
), ZEND_ACC_PUBLIC
)
143 HTTP_MESSAGE_ME(fromString
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
145 HTTP_MESSAGE_ME(detach
, ZEND_ACC_PUBLIC
)
147 HTTP_MESSAGE_ME(prepend
, ZEND_ACC_PUBLIC
)
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
, requestUrl
, "");
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
);
248 if (o
->parent
.handle
) {
253 p
.value
.obj
= o
->parent
;
254 zend_objects_store_del_ref(&p TSRMLS_CC
);
259 static zval
*_http_message_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
261 getObjectEx(http_message_object
, obj
, object
);
262 http_message
*msg
= obj
->message
;
265 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1);
267 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
269 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
270 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
274 if (type
== BP_VAR_W
) {
275 zend_error(E_ERROR
, "Cannot access HttpMessage properties by reference or array key/index");
279 ALLOC_ZVAL(return_value
);
280 return_value
->refcount
= 0;
281 return_value
->is_ref
= 0;
289 case HTTP_MSG_PROPHASH_TYPE
:
290 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
291 RETVAL_LONG(msg
->type
);
294 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
295 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
296 RETVAL_DOUBLE(msg
->http
.version
);
299 case HTTP_MSG_PROPHASH_BODY
:
300 case HTTP_MSG_CHILD_PROPHASH_BODY
:
301 phpstr_fix(PHPSTR(msg
));
302 RETVAL_PHPSTR(PHPSTR(msg
), 0, 1);
305 case HTTP_MSG_PROPHASH_HEADERS
:
306 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
307 array_init(return_value
);
308 zend_hash_copy(Z_ARRVAL_P(return_value
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
311 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
312 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
314 RETVAL_OBJVAL(obj
->parent
);
320 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
321 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
322 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.method
) {
323 RETVAL_STRING(msg
->http
.info
.request
.method
, 1);
329 case HTTP_MSG_PROPHASH_REQUEST_URL
:
330 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL
:
331 if (HTTP_MSG_TYPE(REQUEST
, msg
) && msg
->http
.info
.request
.url
) {
332 RETVAL_STRING(msg
->http
.info
.request
.url
, 1);
338 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
339 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
340 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
341 RETVAL_LONG(msg
->http
.info
.response
.code
);
347 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
348 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
349 if (HTTP_MSG_TYPE(RESPONSE
, msg
) && msg
->http
.info
.response
.status
) {
350 RETVAL_STRING(msg
->http
.info
.response
.status
, 1);
358 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
368 static void _http_message_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
)
370 getObjectEx(http_message_object
, obj
, object
);
371 http_message
*msg
= obj
->message
;
374 ulong h
= zend_get_hash_value(Z_STRVAL_P(member
), Z_STRLEN_P(member
) + 1);
376 zend_property_info
*pinfo
= zend_get_property_info(obj
->zo
.ce
, member
, 1 TSRMLS_CC
);
378 if (!pinfo
|| ACC_PROP_PUBLIC(pinfo
->flags
)) {
379 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
384 cpy
= zval_copy(Z_TYPE_P(value
), value
);
392 case HTTP_MSG_PROPHASH_TYPE
:
393 case HTTP_MSG_CHILD_PROPHASH_TYPE
:
394 convert_to_long(cpy
);
395 http_message_set_type(msg
, Z_LVAL_P(cpy
));
398 case HTTP_MSG_PROPHASH_HTTP_VERSION
:
399 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION
:
400 convert_to_double(cpy
);
401 msg
->http
.version
= Z_DVAL_P(cpy
);
404 case HTTP_MSG_PROPHASH_BODY
:
405 case HTTP_MSG_CHILD_PROPHASH_BODY
:
406 convert_to_string(cpy
);
407 phpstr_dtor(PHPSTR(msg
));
408 phpstr_from_string_ex(PHPSTR(msg
), Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
));
411 case HTTP_MSG_PROPHASH_HEADERS
:
412 case HTTP_MSG_CHILD_PROPHASH_HEADERS
:
413 convert_to_array(cpy
);
414 zend_hash_clean(&msg
->hdrs
);
415 zend_hash_copy(&msg
->hdrs
, Z_ARRVAL_P(cpy
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
418 case HTTP_MSG_PROPHASH_PARENT_MESSAGE
:
419 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE
:
420 if (Z_TYPE_P(value
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(value
), http_message_object_ce TSRMLS_CC
)) {
423 tmp
.value
.obj
= obj
->parent
;
426 Z_OBJ_ADDREF_P(value
);
427 obj
->parent
= value
->value
.obj
;
431 case HTTP_MSG_PROPHASH_REQUEST_METHOD
:
432 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD
:
433 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
434 convert_to_string(cpy
);
435 STR_SET(msg
->http
.info
.request
.method
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
439 case HTTP_MSG_PROPHASH_REQUEST_URL
:
440 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL
:
441 if (HTTP_MSG_TYPE(REQUEST
, msg
)) {
442 convert_to_string(cpy
);
443 STR_SET(msg
->http
.info
.request
.url
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
447 case HTTP_MSG_PROPHASH_RESPONSE_CODE
:
448 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE
:
449 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
450 convert_to_long(cpy
);
451 msg
->http
.info
.response
.code
= Z_LVAL_P(cpy
);
455 case HTTP_MSG_PROPHASH_RESPONSE_STATUS
:
456 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS
:
457 if (HTTP_MSG_TYPE(RESPONSE
, msg
)) {
458 convert_to_string(cpy
);
459 STR_SET(msg
->http
.info
.response
.status
, estrndup(Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
)));
465 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
472 static HashTable
*_http_message_object_get_props(zval
*object TSRMLS_DC
)
475 getObjectEx(http_message_object
, obj
, object
);
476 http_message
*msg
= obj
->message
;
477 HashTable
*props
= OBJ_PROP(obj
);
480 INIT_ZARR(array
, props
);
482 #define ASSOC_PROP(array, ptype, name, val) \
486 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
487 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
488 efree(m_prop_name); \
490 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
491 #define ASSOC_STRINGL(array, name, val, len) \
495 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
496 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, 1); \
497 efree(m_prop_name); \
500 ASSOC_PROP(array
, long, "type", msg
->type
);
501 ASSOC_PROP(array
, double, "httpVersion", msg
->http
.version
);
505 case HTTP_MSG_REQUEST
:
506 ASSOC_PROP(array
, long, "responseCode", 0);
507 ASSOC_STRINGL(array
, "responseStatus", "", 0);
508 ASSOC_STRING(array
, "requestMethod", msg
->http
.info
.request
.method
);
509 ASSOC_STRING(array
, "requestUrl", msg
->http
.info
.request
.url
);
512 case HTTP_MSG_RESPONSE
:
513 ASSOC_PROP(array
, long, "responseCode", msg
->http
.info
.response
.code
);
514 ASSOC_STRING(array
, "responseStatus", msg
->http
.info
.response
.status
);
515 ASSOC_STRINGL(array
, "requestMethod", "", 0);
516 ASSOC_STRINGL(array
, "requestUrl", "", 0);
521 ASSOC_PROP(array
, long, "responseCode", 0);
522 ASSOC_STRINGL(array
, "responseStatus", "", 0);
523 ASSOC_STRINGL(array
, "requestMethod", "", 0);
524 ASSOC_STRINGL(array
, "requestUrl", "", 0);
528 MAKE_STD_ZVAL(headers
);
530 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
531 ASSOC_PROP(array
, zval
, "headers", headers
);
532 ASSOC_STRINGL(array
, "body", PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
534 return OBJ_PROP(obj
);
537 /* ### USERLAND ### */
539 /* {{{ proto void HttpMessage::__construct([string message])
541 * Instantiate a new HttpMessage object.
543 * Accepts an optional string parameter containing a single or several
544 * consecutive HTTP messages. The constructed object will actually
545 * represent the *last* message of the passed string. If there were
546 * prior messages, those can be accessed by HttpMessage::getParentMessage().
548 * Throws HttpMalformedHeaderException.
550 PHP_METHOD(HttpMessage
, __construct
)
553 char *message
= NULL
;
555 getObject(http_message_object
, obj
);
558 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
559 http_message
*msg
= obj
->message
;
561 http_message_dtor(msg
);
562 if ((obj
->message
= http_message_parse_ex(msg
, message
, length
))) {
563 if (obj
->message
->parent
) {
564 obj
->parent
= http_message_object_new_ex(Z_OBJCE_P(getThis()), obj
->message
->parent
, NULL
);
567 obj
->message
= http_message_init(msg
);
571 obj
->message
= http_message_new();
577 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message[, string class_name = "HttpMessage"])
579 * Create an HttpMessage object from a string. Kind of a static constructor.
581 * Expects a string parameter containing a sinlge or several consecutive
582 * HTTP messages. Accepts an optionsl string parameter specifying the class to use.
584 * Returns an HttpMessage object on success or NULL on failure.
586 * Throws HttpMalformedHeadersException.
588 PHP_METHOD(HttpMessage
, fromString
)
590 char *string
= NULL
, *class_name
= NULL
;
591 int length
= 0, class_length
= 0;
592 http_message
*msg
= NULL
;
597 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|s", &string
, &length
, &class_name
, &class_length
)) {
598 if ((msg
= http_message_parse(string
, length
))) {
599 zend_class_entry
*ce
= http_message_object_ce
;
601 if (class_name
&& *class_name
) {
602 ce
= zend_fetch_class(class_name
, class_length
, ZEND_FETCH_CLASS_DEFAULT TSRMLS_CC
);
603 if (ce
&& !instanceof_function(ce
, http_request_object_ce TSRMLS_CC
)) {
604 http_error_ex(HE_WARNING
, HTTP_E_RUNTIME
, "Class %s does not extend HttpMessage", class_name
);
609 ZVAL_OBJVAL(return_value
, http_message_object_new_ex(ce
, msg
, NULL
));
617 /* {{{ proto string HttpMessage::getBody()
619 * Get the body of the parsed HttpMessage.
621 * Returns the message body as string.
623 PHP_METHOD(HttpMessage
, getBody
)
628 getObject(http_message_object
, obj
);
629 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
634 /* {{{ proto void HttpMessage::setBody(string body)
636 * Set the body of the HttpMessage.
637 * NOTE: Don't forget to update any headers accordingly.
639 * Expects a string parameter containing the new body of the message.
641 PHP_METHOD(HttpMessage
, setBody
)
645 getObject(http_message_object
, obj
);
647 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &body
, &len
)) {
648 phpstr_dtor(PHPSTR(obj
->message
));
649 phpstr_from_string_ex(PHPSTR(obj
->message
), body
, len
);
654 /* {{{ proto array HttpMessage::getHeaders()
656 * Get Message Headers.
658 * Returns an associative array containing the messages HTTP headers.
660 PHP_METHOD(HttpMessage
, getHeaders
)
666 getObject(http_message_object
, obj
);
668 INIT_ZARR(headers
, &obj
->message
->hdrs
);
669 array_init(return_value
);
670 array_copy(&headers
, return_value
);
675 /* {{{ proto void HttpMessage::setHeaders(array headers)
679 * Expects an associative array as parameter containing the new HTTP headers,
680 * which will replace *all* previous HTTP headers of the message.
682 PHP_METHOD(HttpMessage
, setHeaders
)
684 zval
*new_headers
, old_headers
;
685 getObject(http_message_object
, obj
);
687 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
691 zend_hash_clean(&obj
->message
->hdrs
);
692 INIT_ZARR(old_headers
, &obj
->message
->hdrs
);
693 array_copy(new_headers
, &old_headers
);
697 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
699 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
701 * Expects an associative array as parameter containing the additional HTTP headers
702 * to add to the messages existing headers. If the optional bool parameter is true,
703 * and a header with the same name of one to add exists already, this respective
704 * header will be converted to an array containing both header values, otherwise
705 * it will be overwritten with the new header value.
707 PHP_METHOD(HttpMessage
, addHeaders
)
709 zval old_headers
, *new_headers
;
710 zend_bool append
= 0;
711 getObject(http_message_object
, obj
);
713 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
717 INIT_ZARR(old_headers
, &obj
->message
->hdrs
);
719 array_append(new_headers
, &old_headers
);
721 array_merge(new_headers
, &old_headers
);
726 /* {{{ proto int HttpMessage::getType()
728 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
730 * Returns the HttpMessage::TYPE.
732 PHP_METHOD(HttpMessage
, getType
)
737 getObject(http_message_object
, obj
);
738 RETURN_LONG(obj
->message
->type
);
743 /* {{{ proto void HttpMessage::setType(int type)
745 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
747 * Exptects an int parameter, the HttpMessage::TYPE.
749 PHP_METHOD(HttpMessage
, setType
)
752 getObject(http_message_object
, obj
);
754 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
757 http_message_set_type(obj
->message
, type
);
761 /* {{{ proto int HttpMessage::getResponseCode()
763 * Get the Response Code of the Message.
765 * Returns the HTTP response code if the message is of type
766 * HttpMessage::TYPE_RESPONSE, else FALSE.
768 PHP_METHOD(HttpMessage
, getResponseCode
)
773 getObject(http_message_object
, obj
);
774 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
775 RETURN_LONG(obj
->message
->http
.info
.response
.code
);
780 /* {{{ proto bool HttpMessage::setResponseCode(int code)
782 * Set the response code of an HTTP Response Message.
784 * Expects an int parameter with the HTTP response code.
786 * Returns TRUE on success, or FALSE if the message is not of type
787 * HttpMessage::TYPE_RESPONSE or the response code is out of range (100-510).
789 PHP_METHOD(HttpMessage
, setResponseCode
)
792 getObject(http_message_object
, obj
);
794 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj
->message
, RETURN_FALSE
);
796 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
799 if (code
< 100 || code
> 510) {
800 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid response code (100-510): %ld", code
);
804 obj
->message
->http
.info
.response
.code
= code
;
809 /* {{{ proto string HttpMessage::getRequestMethod()
811 * Get the Request Method of the Message.
813 * Returns the request method name on success, or FALSE if the message is
814 * not of type HttpMessage::TYPE_REQUEST.
816 PHP_METHOD(HttpMessage
, getRequestMethod
)
821 getObject(http_message_object
, obj
);
822 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
823 RETURN_STRING(obj
->message
->http
.info
.request
.method
, 1);
828 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
830 * Set the Request Method of the HTTP Message.
832 * Expects a string parameter containing the request method name.
834 * Returns TRUE on success, or FALSE if the message is not of type
835 * HttpMessage::TYPE_REQUEST or an invalid request method was supplied.
837 PHP_METHOD(HttpMessage
, setRequestMethod
)
841 getObject(http_message_object
, obj
);
843 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
845 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
848 if (method_len
< 1) {
849 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
852 if (SUCCESS
!= http_check_method(method
)) {
853 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unkown request method: %s", method
);
857 STR_SET(obj
->message
->http
.info
.request
.method
, estrndup(method
, method_len
));
862 /* {{{ proto string HttpMessage::getRequestUrl()
864 * Get the Request URL of the Message.
866 * Returns the request url as string on success, or FALSE if the message
867 * is not of type HttpMessage::TYPE_REQUEST.
869 PHP_METHOD(HttpMessage
, getRequestUrl
)
874 getObject(http_message_object
, obj
);
875 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
876 RETURN_STRING(obj
->message
->http
.info
.request
.url
, 1);
881 /* {{{ proto bool HttpMessage::setRequestUrl(string url)
883 * Set the Request URL of the HTTP Message.
885 * Expects a string parameters containing the request url.
887 * Returns TRUE on success, or FALSE if the message is not of type
888 * HttpMessage::TYPE_REQUEST or supplied URL was empty.
890 PHP_METHOD(HttpMessage
, setRequestUrl
)
894 getObject(http_message_object
, obj
);
896 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
899 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj
->message
, RETURN_FALSE
);
901 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Cannot set HttpMessage::requestUrl to an empty string");
905 STR_SET(obj
->message
->http
.info
.request
.url
, estrndup(URI
, URIlen
));
910 /* {{{ proto string HttpMessage::getHttpVersion()
912 * Get the HTTP Protocol Version of the Message.
914 * Returns the HTTP protocol version as string.
916 PHP_METHOD(HttpMessage
, getHttpVersion
)
922 getObject(http_message_object
, obj
);
924 sprintf(ver
, "%1.1lf", obj
->message
->http
.version
);
925 RETURN_STRINGL(ver
, 3, 1);
930 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
932 * Set the HTTP Protocol version of the Message.
934 * Expects a string parameter containing the HTTP protocol version.
936 * Returns TRUE on success, or FALSE if supplied version is out of range (1.0/1.1).
938 PHP_METHOD(HttpMessage
, setHttpVersion
)
942 getObject(http_message_object
, obj
);
944 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
948 convert_to_double(zv
);
949 sprintf(v
, "%1.1lf", Z_DVAL_P(zv
));
950 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
951 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
955 obj
->message
->http
.version
= Z_DVAL_P(zv
);
960 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
962 * Get parent Message.
964 * Returns the parent HttpMessage on success, or NULL if there's none.
966 PHP_METHOD(HttpMessage
, getParentMessage
)
971 getObject(http_message_object
, obj
);
973 if (obj
->message
->parent
) {
974 RETVAL_OBJVAL(obj
->parent
);
982 /* {{{ proto bool HttpMessage::send()
984 * Send the Message according to its type as Response or Request.
985 * This provides limited functionality compared to HttpRequest and HttpResponse.
987 * Returns TRUE on success, or FALSE on failure.
989 PHP_METHOD(HttpMessage
, send
)
991 getObject(http_message_object
, obj
);
995 RETURN_SUCCESS(http_message_send(obj
->message
));
999 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
1001 * Get the string representation of the Message.
1003 * Accepts a bool parameter which specifies whether the returned string
1004 * should also contain any parent messages.
1006 * Returns the full message as string.
1008 PHP_METHOD(HttpMessage
, toString
)
1013 zend_bool include_parent
= 0;
1014 getObject(http_message_object
, obj
);
1016 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1020 if (include_parent
) {
1021 http_message_serialize(obj
->message
, &string
, &length
);
1023 http_message_tostring(obj
->message
, &string
, &length
);
1025 RETURN_STRINGL(string
, length
, 0);
1030 /* {{{ proto int HttpMessage::count()
1032 * Implements Countable.
1034 * Returns the number of parent messages + 1.
1036 PHP_METHOD(HttpMessage
, count
)
1041 getObject(http_message_object
, obj
);
1043 for (i
= 0, msg
= obj
->message
; msg
; msg
= msg
->parent
, ++i
);
1049 /* {{{ proto string HttpMessage::serialize()
1051 * Implements Serializable.
1053 * Returns the serialized representation of the HttpMessage.
1055 PHP_METHOD(HttpMessage
, serialize
)
1060 getObject(http_message_object
, obj
);
1062 http_message_serialize(obj
->message
, &string
, &length
);
1063 RETURN_STRINGL(string
, length
, 0);
1068 /* {{{ proto void HttpMessage::unserialize(string serialized)
1070 * Implements Serializable.
1072 * Re-constructs the HttpMessage based upon the serialized string.
1074 PHP_METHOD(HttpMessage
, unserialize
)
1078 getObject(http_message_object
, obj
);
1080 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &serialized
, &length
)) {
1081 http_message_dtor(obj
->message
);
1082 if (!http_message_parse_ex(obj
->message
, serialized
, (size_t) length
)) {
1083 http_error(HE_ERROR
, HTTP_E_RUNTIME
, "Could not unserialize HttpMessage");
1084 http_message_init(obj
->message
);
1090 /* {{{ proto HttpMessage HttpMessage::detach(void)
1092 * Returns a clone of an HttpMessage object detached from any parent messages.
1094 PHP_METHOD(HttpMessage
, detach
)
1098 getObject(http_message_object
, obj
);
1102 info
.type
= obj
->message
->type
;
1103 memcpy(&HTTP_INFO(&info
), &HTTP_INFO(obj
->message
), sizeof(struct http_info
));
1105 msg
= http_message_new();
1106 http_message_set_info(msg
, &info
);
1108 zend_hash_copy(&msg
->hdrs
, &obj
->message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1109 phpstr_append(&msg
->body
, PHPSTR_VAL(obj
->message
), PHPSTR_LEN(obj
->message
));
1111 RETURN_OBJVAL(http_message_object_new_ex(Z_OBJCE_P(getThis()), msg
, NULL
));
1115 /* {{{ proto void HttpMessage::prepend(HttpMessage message)
1117 * Prepends message(s) to the HTTP message.
1119 * Expects an HttpMessage object as parameter.
1121 PHP_METHOD(HttpMessage
, prepend
)
1126 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O|b", &prepend
, http_message_object_ce
, &top
)) {
1128 http_message
*save_parent_msg
;
1129 zend_object_value save_parent_obj
;
1130 getObject(http_message_object
, obj
);
1131 getObjectEx(http_message_object
, prepend_obj
, prepend
);
1137 save_parent_obj
= obj
->parent
;
1138 save_parent_msg
= obj
->message
->parent
;
1140 /* iterate to the most parent object */
1141 while (obj
->parent
.handle
) {
1142 m
.value
.obj
= obj
->parent
;
1143 obj
= zend_object_store_get_object(&m TSRMLS_CC
);
1148 obj
->parent
= prepend
->value
.obj
;
1149 obj
->message
->parent
= prepend_obj
->message
;
1152 zend_objects_store_add_ref(prepend TSRMLS_CC
);
1153 while (prepend_obj
->parent
.handle
) {
1154 m
.value
.obj
= prepend_obj
->parent
;
1155 zend_objects_store_add_ref(&m TSRMLS_CC
);
1156 prepend_obj
= zend_object_store_get_object(&m TSRMLS_CC
);
1160 prepend_obj
->parent
= save_parent_obj
;
1161 prepend_obj
->message
->parent
= save_parent_msg
;
1167 #endif /* ZEND_ENGINE_2 */
1174 * vim600: noet sw=4 ts=4 fdm=marker
1175 * vim<600: noet sw=4 ts=4