+/* {{{ 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);
+ }
+ }
+}
+/* }}} */
+