+PHP_HTTP_API void _http_message_serialize(http_message *message, char **string, size_t *length)
+{
+ char *buf;
+ size_t len;
+ phpstr str;
+
+ phpstr_init(&str);
+
+ do {
+ http_message_tostring(message, &buf, &len);
+ phpstr_prepend(&str, buf, len);
+ efree(buf);
+ } while ((message = message->parent));
+
+ buf = phpstr_data(&str, string, length);
+ if (!string) {
+ efree(buf);
+ }
+
+ phpstr_dtor(&str);
+}
+
+PHP_HTTP_API http_message *_http_message_reverse(http_message *msg)
+{
+ int i, c;
+
+ http_message_count(c, msg);
+
+ if (c > 1) {
+ http_message *tmp = msg, **arr = ecalloc(c, sizeof(http_message *));
+
+ for (i = 0; i < c; ++i) {
+ arr[i] = tmp;
+ tmp = tmp->parent;
+ }
+ arr[0]->parent = NULL;
+ for (i = 0; i < c-1; ++i) {
+ arr[i+1]->parent = arr[i];
+ }
+
+ msg = arr[c-1];
+ efree(arr);
+ }
+
+ return msg;
+}
+
+PHP_HTTP_API http_message *_http_message_interconnect(http_message *m1, http_message *m2)
+{
+ if (m1 && m2) {
+ int i = 0, c1, c2;
+ http_message *t1 = m1, *t2 = m2, *p1, *p2;
+
+ http_message_count(c1, m1);
+ http_message_count(c2, m2);
+
+ while (i++ < (c1 - c2)) {
+ t1 = t1->parent;
+ }
+ while (i++ <= c1) {
+ p1 = t1->parent;
+ p2 = t2->parent;
+ t1->parent = t2;
+ t2->parent = p1;
+ t1 = p1;
+ t2 = p2;
+ }
+ } else if (!m1 && m2) {
+ m1 = m2;
+ }
+ return m1;
+}
+
+PHP_HTTP_API void _http_message_tostruct_recursive(http_message *msg, zval *obj TSRMLS_DC)
+{
+ zval strct;
+ zval *headers;
+
+ INIT_ZARR(strct, HASH_OF(obj));
+
+ add_assoc_long(&strct, "type", msg->type);
+ add_assoc_double(&strct, "httpVersion", msg->http.version);
+ switch (msg->type)
+ {
+ case HTTP_MSG_RESPONSE:
+ add_assoc_long(&strct, "responseCode", msg->http.info.response.code);
+ add_assoc_string(&strct, "responseStatus", STR_PTR(msg->http.info.response.status), 1);
+ break;
+
+ case HTTP_MSG_REQUEST:
+ add_assoc_string(&strct, "requestMethod", STR_PTR(msg->http.info.request.method), 1);
+ add_assoc_string(&strct, "requestUrl", STR_PTR(msg->http.info.request.url), 1);
+ break;
+
+ case HTTP_MSG_NONE:
+ /* avoid compiler warning */
+ break;
+ }
+
+ MAKE_STD_ZVAL(headers);
+ array_init(headers);
+ zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
+ add_assoc_zval(&strct, "headers", headers);
+
+ add_assoc_stringl(&strct, "body", PHPSTR_VAL(msg), PHPSTR_LEN(msg), 1);
+
+ if (msg->parent) {
+ zval *parent;
+
+ MAKE_STD_ZVAL(parent);
+ if (Z_TYPE_P(obj) == IS_ARRAY) {
+ array_init(parent);
+ } else {
+ object_init(parent);
+ }
+ add_assoc_zval(&strct, "parentMessage", parent);
+ http_message_tostruct_recursive(msg->parent, parent);
+ } else {
+ add_assoc_null(&strct, "parentMessage");
+ }
+}
+
+PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC)
+{
+ STATUS rs = FAILURE;
+
+ switch (message->type) {
+ case HTTP_MSG_RESPONSE:
+ {
+ HashKey key = initHashKey(0);
+ zval **val;
+ HashPosition pos;
+
+ FOREACH_HASH_KEYVAL(pos, &message->hdrs, key, val) {
+ if (key.type == HASH_KEY_IS_STRING) {
+ http_send_header_zval_ex(key.str, key.len-1, val, 1);
+ }
+ }
+ rs = SUCCESS == http_send_status(message->http.info.response.code) &&
+ SUCCESS == http_send_data(PHPSTR_VAL(message), PHPSTR_LEN(message)) ?
+ SUCCESS : FAILURE;
+ break;
+ }
+
+ case HTTP_MSG_REQUEST:
+ {
+#ifdef HTTP_HAVE_CURL
+ char *uri = NULL;
+ http_request request;
+ zval **zhost, options, headers;
+
+ INIT_PZVAL(&options);
+ INIT_PZVAL(&headers);
+ array_init(&options);
+ array_init(&headers);
+ zend_hash_copy(Z_ARRVAL(headers), &message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
+ add_assoc_zval(&options, "headers", &headers);
+
+ /* check host header */
+ if (SUCCESS == zend_hash_find(&message->hdrs, "Host", sizeof("Host"), (void *) &zhost)) {
+ char *colon = NULL;
+ php_url parts, *url = php_url_parse(message->http.info.request.url);
+
+ memset(&parts, 0, sizeof(php_url));
+
+ /* check for port */
+ if ((colon = strchr(Z_STRVAL_PP(zhost), ':'))) {
+ parts.port = atoi(colon + 1);
+ parts.host = estrndup(Z_STRVAL_PP(zhost), (Z_STRVAL_PP(zhost) - colon - 1));
+ } else {
+ parts.host = estrndup(Z_STRVAL_PP(zhost), Z_STRLEN_PP(zhost));
+ }
+
+ http_build_url(HTTP_URL_REPLACE, url, &parts, NULL, &uri, NULL);
+ php_url_free(url);
+ efree(parts.host);
+ } else {
+ uri = http_absolute_url(message->http.info.request.url);
+ }
+
+ if ((request.meth = http_request_method_exists(1, 0, message->http.info.request.method))) {
+ http_request_body body;
+
+ http_request_init_ex(&request, NULL, request.meth, uri);
+ request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_CSTRING, PHPSTR_VAL(message), PHPSTR_LEN(message), 0);
+ if (SUCCESS == (rs = http_request_prepare(&request, NULL))) {
+ http_request_exec(&request);
+ }
+ http_request_dtor(&request);
+ } else {
+ http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD,
+ "Cannot send HttpMessage. Request method %s not supported",
+ message->http.info.request.method);
+ }
+ efree(uri);
+#else
+ http_error(HE_WARNING, HTTP_E_RUNTIME, "HTTP requests not supported - ext/http was not linked against libcurl.");
+#endif
+ break;
+ }
+
+ case HTTP_MSG_NONE:
+ default:
+ http_error(HE_WARNING, HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type HTTP_MSG_REQUEST nor HTTP_MSG_RESPONSE");
+ break;
+ }
+
+ return rs;
+}
+
+PHP_HTTP_API http_message *_http_message_dup(http_message *orig TSRMLS_DC)
+{
+ http_message *temp, *copy = NULL;
+ http_info info;
+
+ if (orig) {
+ info.type = orig->type;
+ info.http = orig->http;
+
+ copy = temp = http_message_new();
+ http_message_set_info(temp, &info);
+ zend_hash_copy(&temp->hdrs, &orig->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
+ phpstr_append(&temp->body, orig->body.data, orig->body.used);
+
+ while (orig->parent) {
+ info.type = orig->parent->type;
+ info.http = orig->parent->http;
+
+ temp->parent = http_message_new();
+ http_message_set_info(temp->parent, &info);
+ zend_hash_copy(&temp->parent->hdrs, &orig->parent->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
+ phpstr_append(&temp->parent->body, orig->parent->body.data, orig->parent->body.used);
+
+ temp = temp->parent;
+ orig = orig->parent;
+ }
+ }
+
+ return copy;
+}
+