+/* {{{ 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. */
+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 post, body, *array, *headers, *host = http_message_header(obj->message, "Host");
+ php_url hurl, *purl = php_url_parse(STR_PTR(obj->message->http.info.request.url));
+
+ MAKE_STD_ZVAL(array);
+ array_init(array);
+
+ memset(&hurl, 0, sizeof(php_url));
+ if (host) {
+ hurl.host = Z_STRVAL_P(host);
+ zval_ptr_dtor(&host);
+ }
+ http_build_url(HTTP_URL_REPLACE, purl, &hurl, NULL, &url, NULL);
+ php_url_free(purl);
+ add_assoc_string(array, "url", url, 0);
+
+ if ( obj->message->http.info.request.method &&
+ ((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);
+ array_copy(&obj->message->hdrs, Z_ARRVAL_P(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);
+
+ if (PHPSTR_VAL(obj->message) && PHPSTR_LEN(obj->message)) {
+ phpstr_fix(PHPSTR(obj->message));
+ INIT_PZVAL(&body);
+ ZVAL_STRINGL(&body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message), 0);
+ if (method != HTTP_POST) {
+ zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setbody", NULL, &body);
+ } else {
+ INIT_PZVAL(&post);
+ array_init(&post);
+
+ zval_copy_ctor(&body);
+ sapi_module.treat_data(PARSE_STRING, Z_STRVAL(body), &post TSRMLS_CC);
+ zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setpostfields", NULL, &post);
+ zval_dtor(&post);
+ }
+ }
+#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;
+ HashKey key = initHashKey(0);
+ 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_KEYVAL(pos1, &obj->message->hdrs, key, header) {
+ if (key.type == HASH_KEY_IS_STRING) {
+ zval *zkey;
+
+ MAKE_STD_ZVAL(zkey);
+ ZVAL_STRINGL(zkey, key.str, key.len - 1, 1);
+
+ 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;
+ }
+ zval_ptr_dtor(&zkey);
+ }
+ }
+
+ 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();
+}
+/* }}} */
+