- save some time by reversing on the message level
authorMichael Wallner <mike@php.net>
Tue, 14 Feb 2006 18:08:06 +0000 (18:08 +0000)
committerMichael Wallner <mike@php.net>
Tue, 14 Feb 2006 18:08:06 +0000 (18:08 +0000)
http_message_api.c
http_request_object.c
php_http_message_api.h

index 0b5908ecab8d5ea4598241fe440ad62929457af5..399650ea591975a4fdf998ee686e11923113e359 100644 (file)
@@ -377,6 +377,57 @@ PHP_HTTP_API void _http_message_serialize(http_message *message, char **string,
        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) {
+               return NULL;
+       } else if (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;
+               }
+       }
+       return m1;
+}
+
 PHP_HTTP_API void _http_message_tostruct_recursive(http_message *msg, zval *obj TSRMLS_DC)
 {
        zval strct;
index 45ef05053cc6b07ccff0e56bb1e3d8a2cde3a0df..35c66200278a4eed9233cdf02224b4b2c736bdb1 100644 (file)
@@ -576,62 +576,20 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this
                         * the requests and the responses in separate chains
                         * for redirects
                         */
-                       http_message *response = http_message_dup(msg);
+                       http_message *response = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response));
                        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);
+                       if (response && request) {
+                               zval *hist, *history = GET_PROP(history);
+                               http_message *hist_msg = http_message_reverse(http_message_interconnect(response, request));
                                
-                               /*
-                                       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);
+                               MAKE_STD_ZVAL(hist);
+                               ZVAL_OBJVAL(hist, http_message_object_new_ex(http_message_object_ce, hist_msg, NULL), 0);
+                               if (Z_TYPE_P(history) == IS_OBJECT) {
+                                       http_message_object_prepend(hist, history);
                                }
-                               /* TODO: error? */
+                               SET_PROP(history, hist);
+                               zval_ptr_dtor(&hist);
                        } else {
                                http_message_free(&response);
                                http_message_free(&request);
index 8e9084135792c197b393228cbc7bbf9ebf0a5070..52149f01adb9545a1665ea7646073d3427ff2cfc 100644 (file)
@@ -79,6 +79,12 @@ PHP_HTTP_API void _http_message_tostring(http_message *msg, char **string, size_
 #define http_message_serialize(m, s, l) _http_message_serialize((m), (s), (l))
 PHP_HTTP_API void _http_message_serialize(http_message *message, char **string, size_t *length);
 
+#define http_message_reverse(m) _http_message_reverse(m)
+PHP_HTTP_API http_message *_http_message_reverse(http_message *msg);
+
+#define http_message_interconnect(m1, m2) _http_message_interconnect((m1), (m2))
+PHP_HTTP_API http_message *_http_message_interconnect(http_message *m1, http_message *m2);
+
 #define http_message_tostruct_recursive(m, s) _http_message_tostruct_recursive((m), (s) TSRMLS_CC)
 PHP_HTTP_API void _http_message_tostruct_recursive(http_message *msg, zval *strct TSRMLS_DC);