+/* ### USERLAND ### */
+
+/* {{{ proto void HttpMessage::__construct([string message])
+ *
+ * Instantiate a new HttpMessage object.
+ *
+ * Accepts an optional string parameter containing a single or several
+ * consecutive HTTP messages. The constructed object will actually
+ * represent the *last* message of the passed string. If there were
+ * prior messages, those can be accessed by HttpMessage::getParentMessage().
+ *
+ * Throws HttpMalformedHeaderException.
+ */
+PHP_METHOD(HttpMessage, __construct)
+{
+ int length = 0;
+ char *message = NULL;
+
+ getObject(http_message_object, obj);
+
+ SET_EH_THROW_HTTP();
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &message, &length) && message && length) {
+ http_message *msg = obj->message;
+
+ http_message_dtor(msg);
+ if ((obj->message = http_message_parse_ex(msg, message, length))) {
+ if (obj->message->parent) {
+ obj->parent = http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, NULL);
+ }
+ } else {
+ obj->message = http_message_init(msg);
+ }
+ }
+ if (!obj->message) {
+ obj->message = http_message_new();
+ }
+ SET_EH_NORMAL();
+}
+/* }}} */
+
+/* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message[, string class_name = "HttpMessage"])
+ *
+ * Create an HttpMessage object from a string. Kind of a static constructor.
+ *
+ * Expects a string parameter containing a single or several consecutive
+ * HTTP messages. Accepts an optional string parameter specifying the class to use.
+ *
+ * Returns an HttpMessage object on success or NULL on failure.
+ *
+ * Throws HttpMalformedHeadersException.
+ */
+PHP_METHOD(HttpMessage, fromString)
+{
+ char *string = NULL, *class_name = NULL;
+ int length = 0, class_length = 0;
+ http_message *msg = NULL;
+
+ RETVAL_NULL();
+
+ SET_EH_THROW_HTTP();
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &string, &length, &class_name, &class_length)) {
+ if ((msg = http_message_parse(string, length))) {
+ zend_class_entry *ce = http_message_object_ce;
+
+ if (class_name && *class_name) {
+ ce = zend_fetch_class(class_name, class_length, ZEND_FETCH_CLASS_DEFAULT TSRMLS_CC);
+ if (ce && !instanceof_function(ce, http_message_object_ce TSRMLS_CC)) {
+ http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Class %s does not extend HttpMessage", class_name);
+ ce = NULL;
+ }
+ }
+ if (ce) {
+ RETVAL_OBJVAL(http_message_object_new_ex(ce, msg, NULL), 0);
+ }
+ }
+ }
+ SET_EH_NORMAL();
+}
+/* }}} */
+
+/* {{{ proto string HttpMessage::getBody()
+ *
+ * Get the body of the parsed HttpMessage.
+ *
+ * Returns the message body as string.
+ */
+PHP_METHOD(HttpMessage, getBody)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ getObject(http_message_object, obj);
+ RETURN_PHPSTR(&obj->message->body, PHPSTR_FREE_NOT, 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto void HttpMessage::setBody(string body)
+ *
+ * Set the body of the HttpMessage.
+ * NOTE: Don't forget to update any headers accordingly.
+ *
+ * Expects a string parameter containing the new body of the message.
+ */
+PHP_METHOD(HttpMessage, setBody)
+{
+ char *body;
+ int len;
+ getObject(http_message_object, obj);
+
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &body, &len)) {
+ phpstr_dtor(PHPSTR(obj->message));
+ phpstr_from_string_ex(PHPSTR(obj->message), body, len);
+ }
+}
+/* }}} */
+
+/* {{{ proto array HttpMessage::getHeaders()
+ *
+ * Get Message Headers.
+ *
+ * Returns an associative array containing the messages HTTP headers.
+ */
+PHP_METHOD(HttpMessage, getHeaders)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ zval headers;
+ getObject(http_message_object, obj);
+
+ INIT_ZARR(headers, &obj->message->hdrs);
+ array_init(return_value);
+ array_copy(&headers, return_value);
+ }
+}
+/* }}} */
+
+/* {{{ proto void HttpMessage::setHeaders(array headers)
+ *
+ * Sets new headers.
+ *
+ * Expects an associative array as parameter containing the new HTTP headers,
+ * which will replace *all* previous HTTP headers of the message.
+ */
+PHP_METHOD(HttpMessage, setHeaders)
+{
+ zval *new_headers, old_headers;
+ getObject(http_message_object, obj);
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
+ return;
+ }
+
+ zend_hash_clean(&obj->message->hdrs);
+ INIT_ZARR(old_headers, &obj->message->hdrs);
+ array_copy(new_headers, &old_headers);
+}
+/* }}} */
+
+/* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
+ *
+ * Add headers. If append is true, headers with the same name will be separated, else overwritten.
+ *
+ * Expects an associative array as parameter containing the additional HTTP headers
+ * to add to the messages existing headers. If the optional bool parameter is true,
+ * and a header with the same name of one to add exists already, this respective
+ * header will be converted to an array containing both header values, otherwise
+ * it will be overwritten with the new header value.
+ */
+PHP_METHOD(HttpMessage, addHeaders)
+{
+ zval old_headers, *new_headers;
+ zend_bool append = 0;
+ getObject(http_message_object, obj);
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &new_headers, &append)) {
+ return;
+ }
+
+ INIT_ZARR(old_headers, &obj->message->hdrs);
+ if (append) {
+ array_append(new_headers, &old_headers);
+ } else {
+ array_merge(new_headers, &old_headers);
+ }
+}
+/* }}} */
+
+/* {{{ proto int HttpMessage::getType()
+ *
+ * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
+ *
+ * Returns the HttpMessage::TYPE.
+ */
+PHP_METHOD(HttpMessage, getType)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ getObject(http_message_object, obj);
+ RETURN_LONG(obj->message->type);
+ }
+}
+/* }}} */
+
+/* {{{ proto void HttpMessage::setType(int type)
+ *
+ * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
+ *
+ * Expects an int parameter, the HttpMessage::TYPE.
+ */
+PHP_METHOD(HttpMessage, setType)
+{
+ long type;
+ getObject(http_message_object, obj);
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type)) {
+ return;
+ }
+ http_message_set_type(obj->message, type);
+}
+/* }}} */
+
+/* {{{ proto int HttpMessage::getResponseCode()
+ *
+ * Get the Response Code of the Message.
+ *
+ * Returns the HTTP response code if the message is of type
+ * HttpMessage::TYPE_RESPONSE, else FALSE.
+ */
+PHP_METHOD(HttpMessage, getResponseCode)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ getObject(http_message_object, obj);
+ HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
+ RETURN_LONG(obj->message->http.info.response.code);
+ }
+}
+/* }}} */
+
+/* {{{ proto bool HttpMessage::setResponseCode(int code)
+ *
+ * Set the response code of an HTTP Response Message.
+ *
+ * Expects an int parameter with the HTTP response code.
+ *
+ * Returns TRUE on success, or FALSE if the message is not of type
+ * HttpMessage::TYPE_RESPONSE or the response code is out of range (100-510).
+ */
+PHP_METHOD(HttpMessage, setResponseCode)
+{
+ long code;
+ getObject(http_message_object, obj);
+
+ HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
+ RETURN_FALSE;
+ }
+ if (code < 100 || code > 510) {
+ http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid response code (100-510): %ld", code);
+ RETURN_FALSE;
+ }
+
+ obj->message->http.info.response.code = code;
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto string HttpMessage::getResponseStatus()
+ *
+ * Get the Response Status of the message (i.e. the string following the response code).
+ *
+ * Returns the HTTP response status string if the message is of type
+ * HttpMessage::TYPE_RESPONSE, else FALSE.
+ */
+PHP_METHOD(HttpMessage, getResponseStatus)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ getObject(http_message_object, obj);
+ HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
+ RETURN_STRING(obj->message->http.info.response.status, 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto bool HttpMessage::setResponseStatus(string status)
+ *
+ * Set the Response Status of the HTTP message (i.e. the string following the response code).
+ *
+ * Expects a string parameter containing the response status text.
+ *
+ * Returns TRUE on success or FALSE if the message is not of type
+ * HttpMessage::TYPE_RESPONSE.
+ */
+PHP_METHOD(HttpMessage, setResponseStatus)
+{
+ char *status;
+ int status_len;
+ getObject(http_message_object, obj);
+
+ HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
+
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &status, &status_len)) {
+ RETURN_FALSE;
+ }
+ STR_SET(obj->message->http.info.response.status, estrdup(status));
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto string HttpMessage::getRequestMethod()
+ *
+ * Get the Request Method of the Message.
+ *
+ * Returns the request method name on success, or FALSE if the message is
+ * not of type HttpMessage::TYPE_REQUEST.
+ */
+PHP_METHOD(HttpMessage, getRequestMethod)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ getObject(http_message_object, obj);
+ HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
+ RETURN_STRING(obj->message->http.info.request.method, 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto bool HttpMessage::setRequestMethod(string method)
+ *
+ * Set the Request Method of the HTTP Message.
+ *
+ * Expects a string parameter containing the request method name.
+ *
+ * Returns TRUE on success, or FALSE if the message is not of type
+ * HttpMessage::TYPE_REQUEST or an invalid request method was supplied.
+ */
+PHP_METHOD(HttpMessage, setRequestMethod)
+{
+ char *method;
+ int method_len;
+ getObject(http_message_object, obj);
+
+ HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
+ RETURN_FALSE;
+ }
+ if (method_len < 1) {
+ http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestMethod to an empty string");
+ RETURN_FALSE;
+ }
+ if (!http_request_method_exists(1, 0, method)) {
+ http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unknown request method: %s", method);
+ RETURN_FALSE;
+ }
+
+ STR_SET(obj->message->http.info.request.method, estrndup(method, method_len));
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto string HttpMessage::getRequestUrl()
+ *
+ * Get the Request URL of the Message.
+ *
+ * Returns the request url as string on success, or FALSE if the message
+ * is not of type HttpMessage::TYPE_REQUEST.
+ */
+PHP_METHOD(HttpMessage, getRequestUrl)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ getObject(http_message_object, obj);
+ HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
+ RETURN_STRING(obj->message->http.info.request.url, 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto bool HttpMessage::setRequestUrl(string url)
+ *
+ * Set the Request URL of the HTTP Message.
+ *
+ * Expects a string parameters containing the request url.
+ *
+ * Returns TRUE on success, or FALSE if the message is not of type
+ * HttpMessage::TYPE_REQUEST or supplied URL was empty.
+ */
+PHP_METHOD(HttpMessage, setRequestUrl)
+{
+ char *URI;
+ int URIlen;
+ getObject(http_message_object, obj);
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URI, &URIlen)) {
+ RETURN_FALSE;
+ }
+ HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
+ if (URIlen < 1) {
+ http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestUrl to an empty string");
+ RETURN_FALSE;
+ }
+
+ STR_SET(obj->message->http.info.request.url, estrndup(URI, URIlen));
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto string HttpMessage::getHttpVersion()
+ *
+ * Get the HTTP Protocol Version of the Message.
+ *
+ * Returns the HTTP protocol version as string.
+ */
+PHP_METHOD(HttpMessage, getHttpVersion)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ char ver[4] = {0};
+ getObject(http_message_object, obj);
+
+ sprintf(ver, "%1.1lf", obj->message->http.version);
+ RETURN_STRINGL(ver, 3, 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto bool HttpMessage::setHttpVersion(string version)
+ *
+ * Set the HTTP Protocol version of the Message.
+ *
+ * Expects a string parameter containing the HTTP protocol version.
+ *
+ * Returns TRUE on success, or FALSE if supplied version is out of range (1.0/1.1).
+ */
+PHP_METHOD(HttpMessage, setHttpVersion)
+{
+ char v[4];
+ zval *zv;
+ getObject(http_message_object, obj);
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &zv)) {
+ return;
+ }
+
+ convert_to_double(zv);
+ sprintf(v, "%1.1lf", Z_DVAL_P(zv));
+ if (strcmp(v, "1.0") && strcmp(v, "1.1")) {
+ http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid HTTP protocol version (1.0 or 1.1): %s", v);
+ RETURN_FALSE;
+ }
+
+ obj->message->http.version = Z_DVAL_P(zv);
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto HttpMessage HttpMessage::getParentMessage()
+ *
+ * Get parent Message.
+ *
+ * Returns the parent HttpMessage on success, or NULL if there's none.
+ *
+ * Throws HttpRuntimeException.
+ */
+PHP_METHOD(HttpMessage, getParentMessage)
+{
+ SET_EH_THROW_HTTP();
+ NO_ARGS {
+ getObject(http_message_object, obj);
+
+ if (obj->message->parent) {
+ RETVAL_OBJVAL(obj->parent, 1);
+ } else {
+ http_error(HE_WARNING, HTTP_E_RUNTIME, "HttpMessage does not have a parent message");
+ }
+ }
+ SET_EH_NORMAL();
+}
+/* }}} */
+
+/* {{{ proto bool HttpMessage::send()
+ *
+ * Send the Message according to its type as Response or Request.
+ * This provides limited functionality compared to HttpRequest and HttpResponse.
+ *
+ * Returns TRUE on success, or FALSE on failure.
+ */
+PHP_METHOD(HttpMessage, send)
+{
+ getObject(http_message_object, obj);
+
+ NO_ARGS;
+
+ RETURN_SUCCESS(http_message_send(obj->message));
+}
+/* }}} */
+
+/* {{{ proto string HttpMessage::toString([bool include_parent = false])
+ *
+ * Get the string representation of the Message.
+ *
+ * Accepts a bool parameter which specifies whether the returned string
+ * should also contain any parent messages.
+ *
+ * Returns the full message as string.
+ */
+PHP_METHOD(HttpMessage, toString)
+{
+ if (return_value_used) {
+ char *string;
+ size_t length;
+ zend_bool include_parent = 0;
+ getObject(http_message_object, obj);
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &include_parent)) {
+ RETURN_FALSE;
+ }
+
+ if (include_parent) {
+ http_message_serialize(obj->message, &string, &length);
+ } else {
+ http_message_tostring(obj->message, &string, &length);
+ }
+ RETURN_STRINGL(string, length, 0);
+ }
+}
+/* }}} */
+
+/* {{{ proto HttpRequest|HttpResponse HttpMessage::toMessageTypeObject(void)
+ *
+ * Creates an object regarding to the type of the message.
+ *
+ * Returns either an HttpRequest or HttpResponse object on success, or NULL on failure.
+ *
+ * Throws HttpRuntimeException, HttpMessageTypeException, HttpHeaderException.
+ */
+PHP_METHOD(HttpMessage, toMessageTypeObject)
+{
+ SET_EH_THROW_HTTP();
+
+ NO_ARGS;
+
+ if (return_value_used) {
+ getObject(http_message_object, obj);
+
+ switch (obj->message->type) {
+ case HTTP_MSG_REQUEST:
+ {
+#ifdef HTTP_HAVE_CURL
+ int method;
+ char *url;
+ zval tmp, body, *array, *headers, *host = http_message_header(obj->message, "Host");
+ php_url hurl, *purl = php_url_parse(obj->message->http.info.request.url);
+
+ MAKE_STD_ZVAL(array);
+ array_init(array);
+
+ memset(&hurl, 0, sizeof(php_url));
+ hurl.host = host ? Z_STRVAL_P(host) : NULL;
+ http_build_url(HTTP_URL_REPLACE, purl, &hurl, NULL, &url, NULL);
+ php_url_free(purl);
+ add_assoc_string(array, "url", url, 0);
+
+ if ( (method = http_request_method_exists(1, 0, obj->message->http.info.request.method)) ||
+ (method = http_request_method_register(obj->message->http.info.request.method, strlen(obj->message->http.info.request.method)))) {
+ add_assoc_long(array, "method", method);
+ }
+
+ if (10 == (int) (obj->message->http.version * 10)) {
+ add_assoc_long(array, "protocol", CURL_HTTP_VERSION_1_0);
+ }
+
+ MAKE_STD_ZVAL(headers);
+ array_init(headers);
+ INIT_ZARR(tmp, &obj->message->hdrs);
+ array_copy(&tmp, headers);
+ add_assoc_zval(array, "headers", headers);
+
+ object_init_ex(return_value, http_request_object_ce);
+ zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setoptions", NULL, array);
+ zval_ptr_dtor(&array);
+
+ INIT_PZVAL(&body);
+ ZVAL_STRINGL(&body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message), 0);
+ zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setrawpostdata", NULL, &body);
+#else
+ http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot transform HttpMessage to HttpRequest (missing curl support)");
+#endif
+ break;
+ }
+
+ case HTTP_MSG_RESPONSE:
+ {
+#ifndef WONKY
+ HashPosition pos1, pos2;
+ ulong idx;
+ uint key_len;
+ char *key = NULL;
+ zval **header, **h, *body;
+
+ if (obj->message->http.info.response.code) {
+ http_send_status(obj->message->http.info.response.code);
+ }
+
+ object_init_ex(return_value, http_response_object_ce);
+
+ FOREACH_HASH_KEYLENVAL(pos1, &obj->message->hdrs, key, key_len, idx, header) {
+ if (key) {
+ zval zkey;
+
+ INIT_PZVAL(&zkey);
+ ZVAL_STRINGL(&zkey, key, key_len, 0);
+
+ switch (Z_TYPE_PP(header)) {
+ case IS_ARRAY:
+ case IS_OBJECT:
+ FOREACH_HASH_VAL(pos2, HASH_OF(*header), h) {
+ ZVAL_ADDREF(*h);
+ zend_call_method_with_2_params(&return_value, http_response_object_ce, NULL, "setheader", NULL, &zkey, *h);
+ zval_ptr_dtor(h);
+ }
+ break;
+
+ default:
+ ZVAL_ADDREF(*header);
+ zend_call_method_with_2_params(&return_value, http_response_object_ce, NULL, "setheader", NULL, &zkey, *header);
+ zval_ptr_dtor(header);
+ break;
+ }
+ key = NULL;
+ }
+ }
+
+ MAKE_STD_ZVAL(body);
+ ZVAL_STRINGL(body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message), 1);
+ zend_call_method_with_1_params(&return_value, http_response_object_ce, NULL, "setdata", NULL, body);
+ zval_ptr_dtor(&body);
+#else
+ http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot transform HttpMessage to HttpResponse (need PHP 5.1+)");
+#endif
+ break;
+ }
+
+ default:
+ http_error(HE_WARNING, HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type HttpMessage::TYPE_REQUEST nor HttpMessage::TYPE_RESPONSE");
+ break;
+ }
+ }
+ SET_EH_NORMAL();
+}
+/* }}} */
+
+/* {{{ proto int HttpMessage::count()
+ *
+ * Implements Countable.
+ *
+ * Returns the number of parent messages + 1.
+ */
+PHP_METHOD(HttpMessage, count)
+{
+ NO_ARGS {
+ long i;
+ getObject(http_message_object, obj);
+
+ http_message_count(i, obj->message);
+ RETURN_LONG(i);
+ }
+}
+/* }}} */
+
+/* {{{ proto string HttpMessage::serialize()
+ *
+ * Implements Serializable.
+ *
+ * Returns the serialized representation of the HttpMessage.
+ */
+PHP_METHOD(HttpMessage, serialize)
+{
+ NO_ARGS {
+ char *string;
+ size_t length;
+ getObject(http_message_object, obj);
+
+ http_message_serialize(obj->message, &string, &length);
+ RETURN_STRINGL(string, length, 0);
+ }
+}
+/* }}} */
+
+/* {{{ proto void HttpMessage::unserialize(string serialized)
+ *
+ * Implements Serializable.
+ *
+ * Re-constructs the HttpMessage based upon the serialized string.
+ */
+PHP_METHOD(HttpMessage, unserialize)
+{
+ int length;
+ char *serialized;
+ getObject(http_message_object, obj);
+
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &serialized, &length)) {
+ http_message_dtor(obj->message);
+ if (!http_message_parse_ex(obj->message, serialized, (size_t) length)) {
+ http_error(HE_ERROR, HTTP_E_RUNTIME, "Could not unserialize HttpMessage");
+ http_message_init(obj->message);
+ }
+ }
+}
+/* }}} */
+
+/* {{{ proto HttpMessage HttpMessage::detach(void)
+ *
+ * Returns a clone of an HttpMessage object detached from any parent messages.
+ */
+PHP_METHOD(HttpMessage, detach)
+{
+ http_info info;
+ http_message *msg;
+ getObject(http_message_object, obj);
+
+ NO_ARGS;
+
+ info.type = obj->message->type;
+ memcpy(&HTTP_INFO(&info), &HTTP_INFO(obj->message), sizeof(struct http_info));
+
+ msg = http_message_new();
+ http_message_set_info(msg, &info);
+
+ zend_hash_copy(&msg->hdrs, &obj->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
+ phpstr_append(&msg->body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message));
+
+ RETVAL_OBJVAL(http_message_object_new_ex(Z_OBJCE_P(getThis()), msg, NULL), 0);
+}
+/* }}} */
+
+/* {{{ proto void HttpMessage::prepend(HttpMessage message[, bool top = true])
+ *
+ * Prepends message(s) to the HTTP message.
+ *
+ * Expects an HttpMessage object as parameter.
+ *
+ * Throws HttpInvalidParamException if the message is located within the same message chain.
+ */
+PHP_METHOD(HttpMessage, prepend)
+{
+ zval *prepend;
+ zend_bool top = 1;
+
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|b", &prepend, http_message_object_ce, &top)) {
+ http_message *msg[2];
+ getObject(http_message_object, obj);
+ getObjectEx(http_message_object, prepend_obj, prepend);
+
+ /* safety check */
+ for (msg[0] = obj->message; msg[0]; msg[0] = msg[0]->parent) {
+ for (msg[1] = prepend_obj->message; msg[1]; msg[1] = msg[1]->parent) {
+ if (msg[0] == msg[1]) {
+ http_error(HE_THROW, HTTP_E_INVALID_PARAM, "Cannot prepend a message located within the same message chain");
+ return;
+ }
+ }
+ }
+
+ http_message_object_prepend_ex(getThis(), prepend, top);
+ }
+}
+/* }}} */
+
+/* {{{ proto HttpMessage HttpMessage::reverse()
+ *
+ * Reorders the message chain in reverse order.
+ *
+ * Returns the most parent HttpMessage object.
+ */
+PHP_METHOD(HttpMessage, reverse)
+{
+ NO_ARGS {
+ http_message_object_reverse(getThis(), return_value);
+ }
+}
+/* }}} */
+
+/* {{{ proto void HttpMessage::rewind(void)
+ *
+ * Implements Iterator.
+ */
+PHP_METHOD(HttpMessage, rewind)
+{
+ NO_ARGS {
+ getObject(http_message_object, obj);
+
+ if (obj->iterator) {
+ zval_ptr_dtor(&obj->iterator);
+ }
+ ZVAL_ADDREF(getThis());
+ obj->iterator = getThis();
+ }
+}
+/* }}} */
+
+/* {{{ proto bool HttpMessage::valid(void)
+ *
+ * Implements Iterator.
+ */
+PHP_METHOD(HttpMessage, valid)
+{
+ NO_ARGS {
+ getObject(http_message_object, obj);
+
+ RETURN_BOOL(obj->iterator != NULL);
+ }
+}
+/* }}} */
+
+/* {{{ proto void HttpMessage::next(void)
+ *
+ * Implements Iterator.
+ */
+PHP_METHOD(HttpMessage, next)
+{
+ NO_ARGS {
+ getObject(http_message_object, obj);
+ getObjectEx(http_message_object, itr, obj->iterator);
+
+ if (itr && itr->parent.handle) {
+ zval *old = obj->iterator;
+ MAKE_STD_ZVAL(obj->iterator);
+ ZVAL_OBJVAL(obj->iterator, itr->parent, 1);
+ zval_ptr_dtor(&old);
+ } else {
+ zval_ptr_dtor(&obj->iterator);
+ obj->iterator = NULL;
+ }
+ }
+}
+/* }}} */
+
+/* {{{ proto int HttpMessage::key(void)
+ *
+ * Implements Iterator.
+ */
+PHP_METHOD(HttpMessage, key)
+{
+ NO_ARGS {
+ getObject(http_message_object, obj);
+
+ RETURN_LONG(obj->iterator ? obj->iterator->value.obj.handle:0);
+ }
+}
+/* }}} */
+
+/* {{{ proto HttpMessage HttpMessage::current(void)
+ *
+ * Implements Iterator.
+ */
+PHP_METHOD(HttpMessage, current)
+{
+ NO_ARGS {
+ getObject(http_message_object, obj);
+
+ if (obj->iterator) {
+ RETURN_ZVAL(obj->iterator, 1, 0);
+ }
+ }
+}
+/* }}} */
+