X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_request_object.c;h=761e9ca4fda6bc9c280118dfaf2e508ef848d0b0;hp=9bcac0271098ec7e72b388c2970e1cb2cc1e312c;hb=2190567a9216095798e0268f9d61eb5ec920790e;hpb=8051cb5f80e8ca2d938808397fd2d9298b9a0a5d diff --git a/http_request_object.c b/http_request_object.c index 9bcac02..761e9ca 100644 --- a/http_request_object.c +++ b/http_request_object.c @@ -144,6 +144,7 @@ HTTP_END_ARGS; HTTP_EMPTY_ARGS(getResponseMessage, 1); HTTP_EMPTY_ARGS(getRequestMessage, 1); +HTTP_EMPTY_ARGS(getHistory, 1); HTTP_EMPTY_ARGS(send, 0); HTTP_BEGIN_ARGS(get, 0, 1) @@ -267,6 +268,7 @@ zend_function_entry http_request_object_fe[] = { HTTP_REQUEST_ME(getResponseInfo, ZEND_ACC_PUBLIC) HTTP_REQUEST_ME(getResponseMessage, ZEND_ACC_PUBLIC) HTTP_REQUEST_ME(getRequestMessage, ZEND_ACC_PUBLIC) + HTTP_REQUEST_ME(getHistory, ZEND_ACC_PUBLIC) HTTP_REQUEST_ALIAS(get, http_get) HTTP_REQUEST_ALIAS(head, http_head) @@ -340,6 +342,7 @@ zend_object_value _http_request_object_new(zend_class_entry *ce TSRMLS_DC) o->ch = curl_easy_init(); o->pool = NULL; + phpstr_init(&o->history); phpstr_init(&o->request); phpstr_init_ex(&o->response, HTTP_CURLBUF_SIZE, 0); @@ -373,6 +376,7 @@ static inline void _http_request_object_declare_default_properties(TSRMLS_D) DCL_PROP(PROTECTED, string, putFile, ""); DCL_PROP_N(PRIVATE, dbg_user_cb); + DCL_PROP(PUBLIC, bool, recordHistory, 1); } void _http_request_object_free(zend_object *object TSRMLS_DC) @@ -386,11 +390,14 @@ void _http_request_object_free(zend_object *object TSRMLS_DC) if (o->ch) { /* avoid nasty segfaults with already cleaned up callbacks */ curl_easy_setopt(o->ch, CURLOPT_NOPROGRESS, 1); + curl_easy_setopt(o->ch, CURLOPT_PROGRESSFUNCTION, NULL); curl_easy_setopt(o->ch, CURLOPT_VERBOSE, 0); + curl_easy_setopt(o->ch, CURLOPT_DEBUGFUNCTION, NULL); curl_easy_cleanup(o->ch); } phpstr_dtor(&o->response); phpstr_dtor(&o->request); + phpstr_dtor(&o->history); efree(o); } @@ -432,10 +439,12 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ zval *dbg_cb; MAKE_STD_ZVAL(dbg_cb); array_init(dbg_cb); + zval_add_ref(&getThis()); add_next_index_zval(dbg_cb, getThis()); add_next_index_stringl(dbg_cb, "debugWrapper", lenof("debugWrapper"), 1); add_assoc_zval(opts, "ondebug", dbg_cb); } + /* */ switch (Z_LVAL_P(meth)) { @@ -506,7 +515,39 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this if (msg = http_message_parse(PHPSTR_VAL(&obj->response), PHPSTR_LEN(&obj->response))) { char *body; size_t body_len; - zval *headers, *message, *resp = GET_PROP(obj, responseData), *info = GET_PROP(obj, responseInfo); + zval *headers, *message, + *resp = GET_PROP(obj, responseData), + *info = GET_PROP(obj, responseInfo), + *hist = GET_PROP(obj, recordHistory); + + if (Z_TYPE_P(hist) != IS_BOOL) { + convert_to_boolean_ex(&hist); + } + if (Z_LVAL_P(hist)) { + /* we need to act like a zipper, as we'll receive + * the requests and the responses in separate chains + * for redirects + */ + http_message *response = msg, *request = http_message_parse(PHPSTR_VAL(&obj->request), PHPSTR_LEN(&obj->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); + } UPD_PROP(obj, long, responseCode, msg->info.response.code); @@ -602,27 +643,40 @@ PHP_METHOD(HttpRequest, setOptions) old_opts = GET_PROP(obj, options); - /* headers and cookies need extra attention -- thus cannot use array_merge() directly */ + /* some options need extra attention -- thus cannot use array_merge() directly */ FOREACH_KEYVAL(opts, key, idx, opt) { if (key) { if (!strcmp(key, "headers")) { zval **headers; if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "headers", sizeof("headers"), (void **) &headers)) { array_merge(*opt, *headers); + continue; } } else if (!strcmp(key, "cookies")) { zval **cookies; if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "cookies", sizeof("cookies"), (void **) &cookies)) { array_merge(*opt, *cookies); + continue; } - } else { - if (!strcmp(key, "ondebug")) { - SET_PROP(obj, dbg_user_cb, *opt); + } else if ((!strcasecmp(key, "url")) || (!strcasecmp(key, "uri"))) { + if (Z_TYPE_PP(opt) != IS_STRING) { + convert_to_string_ex(opt); } - zval_add_ref(opt); - add_assoc_zval(old_opts, key, *opt); + UPD_PROP(obj, string, url, Z_STRVAL_PP(opt)); + continue; + } else if (!strcmp(key, "method")) { + if (Z_TYPE_PP(opt) != IS_LONG) { + convert_to_long_ex(opt); + } + UPD_PROP(obj, long, method, Z_LVAL_PP(opt)); + continue; + } else if (!strcmp(key, "ondebug")) { + SET_PROP(obj, dbg_user_cb, *opt); } + zval_add_ref(opt); + add_assoc_zval(old_opts, key, *opt); + /* reset */ key = NULL; } @@ -663,6 +717,7 @@ PHP_METHOD(HttpRequest, unsetOptions) FREE_PARR(obj, options); INIT_PARR(obj, options); + zend_update_property_null(http_request_object_ce, getThis(), "dbg_user_cb", lenof("dbg_user_cb") TSRMLS_CC); } /* }}} */ @@ -1563,6 +1618,23 @@ PHP_METHOD(HttpRequest, getRequestMessage) } /* }}} */ +PHP_METHOD(HttpRequest, getHistory) +{ + NO_ARGS; + + IF_RETVAL_USED { + zval *history; + http_message *msg; + getObject(http_request_object, obj); + + SET_EH_THROW_HTTP(); + if (msg = http_message_parse(PHPSTR_VAL(&obj->history), PHPSTR_LEN(&obj->history))) { + RETVAL_OBJVAL(http_message_object_from_msg(msg)); + } + SET_EH_NORMAL(); + } +} + /* {{{ proto bool HttpRequest::send() * * Send the HTTP request. @@ -1632,12 +1704,14 @@ PHP_METHOD(HttpRequest, send) */ PHP_METHOD(HttpRequest, debugWrapper) { + static int curl_ignores_body = 0; getObject(http_request_object, obj); zval *type, *message, *dbg_user_cb = GET_PROP(obj, dbg_user_cb); if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &type, &message)) { RETURN_NULL(); } + if (Z_TYPE_P(type) != IS_LONG) { convert_to_long_ex(&type); } @@ -1645,9 +1719,25 @@ PHP_METHOD(HttpRequest, debugWrapper) convert_to_string_ex(&message); } - /* fetch outgoing request message */ - if (Z_LVAL_P(type) == CURLINFO_HEADER_OUT || Z_LVAL_P(type) == CURLINFO_DATA_OUT) { - phpstr_append(&obj->request, Z_STRVAL_P(message), Z_STRLEN_P(message)); + switch (Z_LVAL_P(type)) + { + case CURLINFO_DATA_IN: + /* fetch ignored body */ + if (curl_ignores_body && Z_LVAL_P(type) == CURLINFO_DATA_IN) { + phpstr_append(&obj->response, Z_STRVAL_P(message), Z_STRLEN_P(message)); + } + break; + + case CURLINFO_TEXT: + /* check if following incoming data would be ignored */ + curl_ignores_body = !strcmp(Z_STRVAL_P(message), "Ignoring the response-body\n"); + break; + + case CURLINFO_HEADER_OUT: + case CURLINFO_DATA_OUT: + /* fetch outgoing request message */ + phpstr_append(&obj->request, Z_STRVAL_P(message), Z_STRLEN_P(message)); + break; } /* call user debug callback */