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;
* 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);
#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);