X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=http_request_object.c;h=35c66200278a4eed9233cdf02224b4b2c736bdb1;hb=79b54baf2c7fa5b37a4ed96a21d29c3574c1270b;hp=0c9a4cdd3f6b4cfc175e7438a3b8f17ecc41bb29;hpb=778163476c4500a8554c35c68dfc9c0cd33f82f8;p=m6w6%2Fext-http diff --git a/http_request_object.c b/http_request_object.c index 0c9a4cd..35c6620 100644 --- a/http_request_object.c +++ b/http_request_object.c @@ -551,72 +551,45 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this_ptr TSRMLS_DC) { + STATUS ret; + zval *info; http_message *msg; + /* always fetch info */ + MAKE_STD_ZVAL(info); + array_init(info); + http_request_info(obj->request, Z_ARRVAL_P(info)); + SET_PROP(responseInfo, info); + zval_ptr_dtor(&info); + + /* parse response message */ phpstr_fix(&obj->request->conv.request); phpstr_fix(&obj->request->conv.response); - msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response)); - - if (!msg) { - return FAILURE; - } else { + if ((msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response)))) { char *body; size_t body_len; - zval *headers, *message, *resp, *info; + zval *headers, *message, *resp; if (zval_is_true(GET_PROP(recordHistory))) { /* we need to act like a zipper, as we'll receive * 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; + if (response && request) { + zval *hist, *history = GET_PROP(history); + http_message *hist_msg = http_message_reverse(http_message_interconnect(response, request)); - 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 (num_req == num_resp) { - int i; - zval *hist, *history = GET_PROP(history); - http_message *res_tmp = response, *req_tmp = request, *req_par, *res_par; - - 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); - if (Z_TYPE_P(history) == IS_OBJECT) { - http_message_object_prepend(hist, history); - } - SET_PROP(history, hist); - 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); @@ -642,18 +615,53 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this SET_PROP(responseMessage, message); zval_ptr_dtor(&message); - MAKE_STD_ZVAL(info); - array_init(info); - http_request_info(obj->request, Z_ARRVAL_P(info)); - SET_PROP(responseInfo, info); - zval_ptr_dtor(&info); + ret = SUCCESS; + } else { + /* update properties with empty values*/ + zval *resp = GET_PROP(responseData), *znull; + + MAKE_STD_ZVAL(znull); + ZVAL_NULL(znull); + SET_PROP(responseMessage, znull); + zval_ptr_dtor(&znull); - if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onfinish", sizeof("onfinish"))) { - zend_call_method_with_0_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "onfinish", NULL); + if (Z_TYPE_P(resp) == IS_ARRAY) { + zend_hash_clean(Z_ARRVAL_P(resp)); } - return SUCCESS; + UPD_PROP(long, responseCode, 0); + UPD_PROP(string, responseStatus, ""); + + /* append request message to history */ + if (zval_is_true(GET_PROP(recordHistory))) { + http_message *request; + + if ((request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request)))) { + zval *hist, *history = GET_PROP(history); + + MAKE_STD_ZVAL(hist); + ZVAL_OBJVAL(hist, http_message_object_new_ex(http_message_object_ce, request, NULL), 0); + if (Z_TYPE_P(history) == IS_OBJECT) { + http_message_object_prepend(hist, history); + } + SET_PROP(history, hist); + zval_ptr_dtor(&hist); + } + } + + ret = FAILURE; } + + if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onfinish", sizeof("onfinish"))) { + zval *param; + + MAKE_STD_ZVAL(param); + ZVAL_BOOL(param, ret == SUCCESS); + zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "onfinish", NULL, param); + zval_ptr_dtor(¶m); + } + + return ret; } #define http_request_object_set_options_subr(key, ow) \ @@ -1571,7 +1579,6 @@ PHP_METHOD(HttpRequest, getResponseCookies) if (Z_TYPE_PP(header) == IS_ARRAY) { zval **single_header; - HashPosition pos; FOREACH_VAL(pos2, *header, single_header) { ZVAL_ADDREF(*single_header); @@ -1879,7 +1886,7 @@ PHP_METHOD(HttpRequest, clearHistory) MAKE_STD_ZVAL(hist); ZVAL_NULL(hist); SET_PROP(history, hist); - zval_ptr_dtor(hist); + zval_ptr_dtor(&hist); } } /* }}} */