From: Michael Wallner Date: Tue, 14 Feb 2006 18:08:06 +0000 (+0000) Subject: - save some time by reversing on the message level X-Git-Tag: RELEASE_0_23_0~3 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=79b54baf2c7fa5b37a4ed96a21d29c3574c1270b;p=m6w6%2Fext-http - save some time by reversing on the message level --- diff --git a/http_message_api.c b/http_message_api.c index 0b5908e..399650e 100644 --- a/http_message_api.c +++ b/http_message_api.c @@ -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; diff --git a/http_request_object.c b/http_request_object.c index 45ef050..35c6620 100644 --- a/http_request_object.c +++ b/http_request_object.c @@ -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); diff --git a/php_http_message_api.h b/php_http_message_api.h index 8e90841..52149f0 100644 --- a/php_http_message_api.h +++ b/php_http_message_api.h @@ -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);