+ 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 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", msg->http.info.response.status, 1);
+ break;
+
+ case HTTP_MSG_REQUEST:
+ add_assoc_string(&strct, "requestMethod", msg->http.info.request.method, 1);
+ add_assoc_string(&strct, "requestUri", msg->http.info.request.URI, 1);
+ 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);
+ zval_ptr_dtor(&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:
+ {
+ char *key;
+ ulong idx;
+ zval **val;
+
+ FOREACH_HASH_KEYVAL(&message->hdrs, key, idx, val) {
+ if (key) {
+ if (Z_TYPE_PP(val) == IS_ARRAY) {
+ zend_bool first = 1;
+ zval **data;
+
+ FOREACH_VAL(*val, data) {
+ http_send_header_ex(key, strlen(key), Z_STRVAL_PP(data), Z_STRLEN_PP(data), first, NULL);
+ first = 0;
+ }
+ } else {
+ http_send_header_ex(key, strlen(key), Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1, NULL);
+ }
+ key = NULL;
+ }
+ }
+ rs = SUCCESS == http_send_status(message->http.info.response.code) &&
+ SUCCESS == http_send_data(PHPSTR_VAL(message), PHPSTR_LEN(message)) ?
+ SUCCESS : FAILURE;