+static php_http_message_t *php_http_curlm_responseparser(php_http_client_curl_handler_t *h TSRMLS_DC)
+{
+ php_http_message_t *response;
+ php_http_header_parser_t parser;
+ zval *zh;
+
+ response = php_http_message_init(NULL, 0, h->response.body TSRMLS_CC);
+ php_http_header_parser_init(&parser TSRMLS_CC);
+ php_http_header_parser_parse(&parser, &h->response.headers, PHP_HTTP_HEADER_PARSER_CLEANUP, &response->hdrs, (php_http_info_callback_t) php_http_message_info_callback, (void *) &response);
+ php_http_header_parser_dtor(&parser);
+
+ /* move body to right message */
+ if (response->body != h->response.body) {
+ php_http_message_t *ptr = response;
+
+ while (ptr->parent) {
+ ptr = ptr->parent;
+ }
+ response->body = ptr->body;
+ ptr->body = NULL;
+ }
+ php_http_message_body_addref(h->response.body);
+
+ /* let's update the response headers */
+ if ((zh = php_http_message_header(response, ZEND_STRL("Content-Length")))) {
+ Z_TRY_ADDREF_P(zh);
+ zend_hash_str_update(&response->hdrs, "X-Original-Content-Length", lenof("X-Original-Content-Length"), zh);
+ }
+ if ((zh = php_http_message_header(response, ZEND_STRL("Transfer-Encoding")))) {
+ Z_TRY_ADDREF_P(zh);
+ zend_hash_str_update(&response->hdrs, "X-Original-Transfer-Encoding", lenof("X-Original-Transfer-Encoding"), zh);
+ zend_hash_str_del(&response->hdrs, "Transfer-Encoding", lenof("Transfer-Encoding"));
+ }
+ if ((zh = php_http_message_header(response, ZEND_STRL("Content-Range")))) {
+ Z_TRY_ADDREF_P(zh);
+ zend_hash_str_update(&response->hdrs, "X-Original-Content-Range", lenof("X-Original-Content-Range"), zh);
+ zend_hash_str_del(&response->hdrs, "Content-Range", lenof("Content-Range"));
+ }
+ if ((zh = php_http_message_header(response, ZEND_STRL("Content-Encoding")))) {
+ Z_TRY_ADDREF_P(zh);
+ zend_hash_str_update(&response->hdrs, "X-Original-Content-Encoding", lenof("X-Original-Content-Encoding"), zh);
+ zend_hash_str_del(&response->hdrs, "Content-Encoding", lenof("Content-Encoding"));
+ }
+ php_http_message_update_headers(response);
+
+ return response;
+}
+