+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 STATUS _http_message_send(http_message *message TSRMLS_DC)
+{
+ STATUS rs = FAILURE;
+
+ switch (message->type)
+ {
+ case HTTP_MSG_RESPONSE:
+ {
+ char *key;
+ ulong idx;
+ zval **val;
+
+ FOREACH_HASH_KEYVAL(&message->hdrs, key, idx, val) {
+ if (key) {
+ char *header;
+ spprintf(&header, 0, "%s: %s", key, Z_STRVAL_PP(val));
+ http_send_header(header);
+ efree(header);
+ key = NULL;
+ }
+ }
+ rs = SUCCESS == http_send_status(message->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;
+ zval **zhost, options, 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, *host = NULL;
+ size_t host_len = 0;
+ int port = 0;
+
+ /* check for port */
+ if (colon = strchr(Z_STRVAL_PP(zhost), ':')) {
+ port = atoi(colon + 1);
+ host = estrndup(Z_STRVAL_PP(zhost), host_len = (Z_STRVAL_PP(zhost) - colon - 1));
+ } else {
+ host = estrndup(Z_STRVAL_PP(zhost), host_len = Z_STRLEN_PP(zhost));
+ }
+ uri = http_absolute_uri_ex(
+ message->info.request.URI, strlen(message->info.request.URI),
+ NULL, 0, host, host_len, port);
+ efree(host);
+ } else {
+ uri = http_absolute_uri(message->info.request.URI);
+ }
+
+ if (!strcasecmp("POST", message->info.request.method)) {
+ http_request_body body = {HTTP_REQUEST_BODY_CSTRING, PHPSTR_VAL(message), PHPSTR_LEN(message)};
+ rs = http_post(uri, &body, Z_ARRVAL(options), NULL, NULL);
+ } else
+ if (!strcasecmp("GET", message->info.request.method)) {
+ rs = http_get(uri, Z_ARRVAL(options), NULL, NULL);
+ } else
+ if (!strcasecmp("HEAD", message->info.request.method)) {
+ rs = http_head(uri, Z_ARRVAL(options), NULL, NULL);
+ } else {
+ http_error_ex(E_WARNING, HTTP_E_MSG,
+ "Cannot send HttpMessage. Request method %s not supported",
+ message->info.request.method);
+ }
+
+ efree(uri);
+#else
+ http_error(E_WARNING, HTTP_E_MSG, "HTTP requests not supported - ext/http was not linked against libcurl.");
+#endif
+ }
+ break;
+
+ case HTTP_MSG_NONE:
+ default:
+ http_error(E_WARNING, HTTP_E_MSG, "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 *msg TSRMLS_DC)
+{
+ /*
+ * TODO: unroll
+ */
+ http_message *new;
+ char *serialized_data;
+ size_t serialized_length;
+
+ http_message_serialize(msg, &serialized_data, &serialized_length);
+ new = http_message_parse(serialized_data, serialized_length);
+ efree(serialized_data);
+ return new;
+}
+