- http_message *response = msg, *request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request));
- http_message *free_msg = request;
-
- do {
- char *message;
- size_t msglen;
-
- http_message_tostring(response, &message, &msglen);
- phpstr_append(&obj->history, message, msglen);
- efree(message);
-
- http_message_tostring(request, &message, &msglen);
- phpstr_append(&obj->history, message, msglen);
- efree(message);
-
- } while ((response = response->parent) && (request = request->parent));
-
- http_message_free(&free_msg);
- phpstr_fix(&obj->history);
+ http_message *response = http_message_dup(msg);
+ http_message *request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request));
+
+ if (request && response) {
+ int num_req, num_resp;
+
+ http_message_count(num_req, request);
+ http_message_count(num_resp, response);
+
+ /*
+ stuck request messages in between response messages
+
+ response request
+ v v
+ response request
+ v v
+ response request
+ ==================
+ response > request
+ ,---'
+ response > request
+ ,---'
+ response > request
+
+ if there are more responses than requests (PUT etc),
+ begin add $diff response's parent message
+ */
+ if (num_req <= num_resp) {
+ int i;
+ zval *prep, *hist, *history = GET_PROP(history);
+ http_message *res_tmp = response, *req_tmp = request, *req_par, *res_par;
+
+ for (i = 0; i < (num_resp - num_req); ++i) {
+ res_tmp = res_tmp->parent;
+ }
+ for (i = 0; i < num_req; ++i) {
+ res_par = res_tmp->parent;
+ req_par = req_tmp->parent;
+ res_tmp->parent = req_tmp;
+ req_tmp->parent = res_par;
+ res_tmp = res_par;
+ req_tmp = req_par;
+ }
+
+ MAKE_STD_ZVAL(hist);
+ ZVAL_OBJVAL(hist, http_message_object_new_ex(http_message_object_ce, response, NULL), 0);
+ MAKE_STD_ZVAL(prep);
+ http_message_object_reverse(hist, prep);
+ if (Z_TYPE_P(history) == IS_OBJECT) {
+ http_message_object_prepend(prep, history);
+ }
+ SET_PROP(history, prep);
+ zval_ptr_dtor(&prep);
+ zval_ptr_dtor(&hist);
+ }
+ /* TODO: error? */
+ } else {
+ http_message_free(&response);
+ http_message_free(&request);
+ }