From: Michael Wallner Date: Sun, 21 May 2006 18:41:07 +0000 (+0000) Subject: - fix cookie handling X-Git-Tag: RELEASE_1_0_0~21 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=80842e67750ad9280770a7cfc956613207d46c1a;p=m6w6%2Fext-http - fix cookie handling - add HttpRequest::resetCookies() - add test --- diff --git a/http_request_api.c b/http_request_api.c index 1addfd9..96a613f 100644 --- a/http_request_api.c +++ b/http_request_api.c @@ -450,8 +450,6 @@ PHP_HTTP_API void _http_request_defaults(http_request *request) #if HTTP_CURL_VERSION(7,14,1) HTTP_CURL_OPT(CURLOPT_COOKIELIST, NULL); #endif - HTTP_CURL_OPT(CURLOPT_COOKIEFILE, NULL); - HTTP_CURL_OPT(CURLOPT_COOKIEJAR, NULL); HTTP_CURL_OPT(CURLOPT_RANGE, NULL); HTTP_CURL_OPT(CURLOPT_RESUME_FROM, 0); HTTP_CURL_OPT(CURLOPT_MAXFILESIZE, 0); @@ -757,27 +755,22 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti } } -#if HTTP_CURL_VERSION(7,14,1) - /* reset cookies */ - if ((zoption = http_request_option(request, options, "resetcookies", IS_BOOL)) && Z_LVAL_P(zoption)) { - HTTP_CURL_OPT(CURLOPT_COOKIELIST, "ALL"); - } -#endif - /* session cookies */ if ((zoption = http_request_option(request, options, "cookiesession", IS_BOOL))) { - if (Z_LVAL_P(zoption)) { + if (Z_BVAL_P(zoption)) { /* accept cookies for this session */ HTTP_CURL_OPT(CURLOPT_COOKIEFILE, ""); } else { - /* reset session cookies */ + /* don't load session cookies from cookiestore */ HTTP_CURL_OPT(CURLOPT_COOKIESESSION, 1); } } /* cookiestore, read initial cookies from that file and store cookies back into that file */ - if ((zoption = http_request_option(request, options, "cookiestore", IS_STRING)) && Z_STRLEN_P(zoption)) { - HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(zoption), return FAILURE); + if ((zoption = http_request_option(request, options, "cookiestore", IS_STRING))) { + if (Z_STRLEN_P(zoption)) { + HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(zoption), return FAILURE); + } HTTP_CURL_OPT(CURLOPT_COOKIEFILE, Z_STRVAL_P(zoption)); HTTP_CURL_OPT(CURLOPT_COOKIEJAR, Z_STRVAL_P(zoption)); } diff --git a/http_request_object.c b/http_request_object.c index 7fa8ead..b2d45fc 100644 --- a/http_request_object.c +++ b/http_request_object.c @@ -72,6 +72,10 @@ HTTP_BEGIN_ARGS(addCookies, 1) HTTP_ARG_VAL(cookies, 0) HTTP_END_ARGS; +#if HTTP_CURL_VERSION(7,14,1) +HTTP_EMPTY_ARGS(resetCookies); +#endif + HTTP_EMPTY_ARGS(getUrl); HTTP_BEGIN_ARGS(setUrl, 1) HTTP_ARG_VAL(url, 0) @@ -250,6 +254,9 @@ zend_function_entry http_request_object_fe[] = { HTTP_REQUEST_ME(addCookies, ZEND_ACC_PUBLIC) HTTP_REQUEST_ME(getCookies, ZEND_ACC_PUBLIC) HTTP_REQUEST_ME(setCookies, ZEND_ACC_PUBLIC) +#if HTTP_CURL_VERSION(7,14,1) + HTTP_REQUEST_ME(resetCookies, ZEND_ACC_PUBLIC) +#endif HTTP_REQUEST_ME(setMethod, ZEND_ACC_PUBLIC) HTTP_REQUEST_ME(getMethod, ZEND_ACC_PUBLIC) @@ -465,6 +472,13 @@ void _http_request_object_free(zend_object *object TSRMLS_DC) efree(o); } +#define http_request_object_resetcookies(o) _http_request_object_resetcookies((o) TSRMLS_CC) +static inline STATUS _http_request_object_resetcookies(zval *this_ptr TSRMLS_DC) +{ + getObject(http_request_object, obj); + return curl_easy_setopt(obj->request->ch, CURLOPT_COOKIELIST, "ALL"); +} + #define http_request_object_check_request_content_type(t) _http_request_object_check_request_content_type((t) TSRMLS_CC) static inline void _http_request_object_check_request_content_type(zval *this_ptr TSRMLS_DC) { @@ -868,6 +882,8 @@ PHP_METHOD(HttpRequest, setOptions) zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "seturl", NULL, *opt); } else if (!strcmp(key, "method")) { zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setmethod", NULL, *opt); + } else if (!strcmp(key, "resetcookies")) { + http_request_object_resetcookies(getThis()); } else { ZVAL_ADDREF(*opt); add_assoc_zval(add_opts, key, *opt); @@ -1030,6 +1046,19 @@ PHP_METHOD(HttpRequest, getCookies) } /* }}} */ +#if HTTP_CURL_VERSION(7,14,1) +/* {{{ proto bool HttpRequest::resetCookies() + * + * Reset all cookies. Note that customly set cookies are not affected. + */ +PHP_METHOD(HttpRequest, resetCookies) +{ + NO_ARGS; + RETURN_SUCCESS(http_request_object_resetcookies(getThis())); +} +/* }}} */ +#endif + /* {{{ proto bool HttpRequest::setUrl(string url) * * Set the request URL. diff --git a/php_http_request_object.h b/php_http_request_object.h index 7a7a2fb..e4c2518 100644 --- a/php_http_request_object.h +++ b/php_http_request_object.h @@ -56,6 +56,9 @@ PHP_METHOD(HttpRequest, setHeaders); PHP_METHOD(HttpRequest, addCookies); PHP_METHOD(HttpRequest, getCookies); PHP_METHOD(HttpRequest, setCookies); +#if HTTP_CURL_VERSION(7,14,1) +PHP_METHOD(HttpRequest, resetCookies); +#endif PHP_METHOD(HttpRequest, setMethod); PHP_METHOD(HttpRequest, getMethod); PHP_METHOD(HttpRequest, setUrl); diff --git a/tests/HttpRequest_010.phpt b/tests/HttpRequest_010.phpt new file mode 100644 index 0000000..4192aee --- /dev/null +++ b/tests/HttpRequest_010.phpt @@ -0,0 +1,42 @@ +--TEST-- +HttpRequest cookie API +--SKIPIF-- + +--FILE-- +recordHistory = true; + +$r->send(); +$c[0] = $r->getResponseInfo("cookies"); +var_dump(empty($c[0])); + +$r->setOptions(array("cookiesession" => true)); + +$r->send(); +$c[1] = $r->getResponseInfo("cookies"); +var_dump(empty($c[1])); + +$r->resetCookies(); + +$r->send(); +$c[2] = $r->getResponseInfo("cookies"); +var_dump($c[1] === $c[2]); + +$r->send(); +$c[3] = $r->getResponseInfo("cookies"); +var_dump($c[2] === $c[3]); + +echo "Done\n"; +--EXPECTF-- +%sTEST +bool(true) +bool(false) +bool(false) +bool(true) +Done