From dd199f9961262ed43fa544240df15a9d6d7d0f08 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Mon, 22 May 2006 09:20:40 +0000 Subject: [PATCH] - adjust cookie handling to work better together with libcurl # blog post to follow --- docs/functions.html | 13 +++++++++--- http_functions.c | 3 +-- http_request_api.c | 38 ++++++++++++++++++++++++++--------- http_request_object.c | 41 ++++++++++++++++++++++++-------------- php_http_request_api.h | 6 ++++++ php_http_request_object.h | 3 +-- tests/HttpRequest_010.phpt | 4 +--- 7 files changed, 74 insertions(+), 34 deletions(-) diff --git a/docs/functions.html b/docs/functions.html index 2c1a469..d1725a1 100644 --- a/docs/functions.html +++ b/docs/functions.html @@ -327,9 +327,8 @@ array where the following keys will be recognized:

 - redirect:
  - cookies:          array, list of cookies as associative array
like array("cookie" => "value")
- encodecookies: bool, whether to urlencode the cookies (default: true)
- - resetcookies: bool, wheter to reset the cookies
- cookiestore: string, path to a file where cookies are/will be stored
- - cookiesession: bool, accept (true) or reset (false) sessioncookies
+ - cookiesession: bool, don't load session cookies from cookiestore if TRUE
- resume: int, byte offset to start the download from;
if the server supports ranges
- range: array, array of arrays, each containing two integers,
@@ -705,6 +704,12 @@ pairs to add.

array HttpRequest::getCookies()

Get previously set cookies.

Returns an associative array containing any previously set cookies.

+

bool HttpRequest::enableCookies()

+

Enable automatic sending of received cookies.
+Note that cuutomly set cookies will be sent anyway.

+

bool HttpRequest::resetCookies()

+

Reset all automatically received/sent cookies.
+Note that customly set cookies are not affected.

bool HttpRequest::setUrl(string url)

Set the request URL.

Expects a string as parameter specifying the request url.

@@ -1326,6 +1331,8 @@ http.cache_log is set.

  • HttpRequest::setCookies()
  • HttpRequest::addCookies()
  • HttpRequest::getCookies()
  • +
  • HttpRequest::enableCookies()
  • +
  • HttpRequest::resetCookies()
  • HttpRequest::setUrl()
  • HttpRequest::getUrl()
  • HttpRequest::setMethod()
  • @@ -1423,7 +1430,7 @@ http.cache_log is set.

    -

    Generated at: Fri, 19 May 2006 16:55:19 +0200

    +

    Generated at: Mon, 22 May 2006 11:19:25 +0200

    diff --git a/http_functions.c b/http_functions.c index 0dfb68b..807b0a5 100644 --- a/http_functions.c +++ b/http_functions.c @@ -1248,9 +1248,8 @@ PHP_FUNCTION(http_match_request_header) * - cookies: array, list of cookies as associative array * like array("cookie" => "value") * - encodecookies: bool, whether to urlencode the cookies (default: true) - * - resetcookies: bool, wheter to reset the cookies * - cookiestore: string, path to a file where cookies are/will be stored - * - cookiesession: bool, accept (true) or reset (false) sessioncookies + * - cookiesession: bool, don't load session cookies from cookiestore if TRUE * - resume: int, byte offset to start the download from; * if the server supports ranges * - range: array, array of arrays, each containing two integers, diff --git a/http_request_api.c b/http_request_api.c index 96a613f..d6a3704 100644 --- a/http_request_api.c +++ b/http_request_api.c @@ -420,6 +420,32 @@ PHP_HTTP_API void _http_request_reset(http_request *request) } /* }}} */ +/* {{{ STATUS http_request_enable_cookies(http_request *) */ +PHP_HTTP_API STATUS _http_request_enable_cookies(http_request *request) +{ + TSRMLS_FETCH_FROM_CTX(request->tsrm_ls); + if (CURLE_OK == curl_easy_setopt(request->ch, CURLOPT_COOKIEFILE, "")) { + return SUCCESS; + } + http_error(HE_WARNING, HTTP_E_REQUEST, "Could not enable cookies for this session"); + return FAILURE; +} +/* }}} */ + +/* {{{ STATUS http_request_reset_cookies(http_request *) */ +PHP_HTTP_API STATUS _http_request_reset_cookies(http_request *request) +{ + TSRMLS_FETCH_FROM_CTX(request->tsrm_ls); +#if HTTP_CURL_VERSION(7,14,1) + if (CURLE_OK == curl_easy_setopt(request->ch, CURLOPT_COOKIELIST, "ALL")) { + return SUCCESS; + } +#endif + http_error(HE_WARNING, HTTP_E_REQUEST, "Could not reset cookies"); + return FAILURE; +} +/* }}} */ + /* {{{ void http_request_defaults(http_request *) */ PHP_HTTP_API void _http_request_defaults(http_request *request) { @@ -755,15 +781,9 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti } } - /* session cookies */ - if ((zoption = http_request_option(request, options, "cookiesession", IS_BOOL))) { - if (Z_BVAL_P(zoption)) { - /* accept cookies for this session */ - HTTP_CURL_OPT(CURLOPT_COOKIEFILE, ""); - } else { - /* don't load session cookies from cookiestore */ - HTTP_CURL_OPT(CURLOPT_COOKIESESSION, 1); - } + /* don't load session cookies from cookiestore */ + if ((zoption = http_request_option(request, options, "cookiesession", IS_BOOL)) && Z_BVAL_P(zoption)) { + HTTP_CURL_OPT(CURLOPT_COOKIESESSION, 1); } /* cookiestore, read initial cookies from that file and store cookies back into that file */ diff --git a/http_request_object.c b/http_request_object.c index 6093542..45625f0 100644 --- a/http_request_object.c +++ b/http_request_object.c @@ -72,6 +72,7 @@ HTTP_BEGIN_ARGS(addCookies, 1) HTTP_ARG_VAL(cookies, 0) HTTP_END_ARGS; +HTTP_EMPTY_ARGS(enableCookies); #if HTTP_CURL_VERSION(7,14,1) HTTP_EMPTY_ARGS(resetCookies); #endif @@ -254,6 +255,8 @@ 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) + + HTTP_REQUEST_ME(enableCookies, ZEND_ACC_PUBLIC) #if HTTP_CURL_VERSION(7,14,1) HTTP_REQUEST_ME(resetCookies, ZEND_ACC_PUBLIC) #endif @@ -472,15 +475,6 @@ void _http_request_object_free(zend_object *object TSRMLS_DC) efree(o); } -#if HTTP_CURL_VERSION(7,14,1) -#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"); -} -#endif - #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) { @@ -886,7 +880,8 @@ PHP_METHOD(HttpRequest, setOptions) zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setmethod", NULL, *opt); #if HTTP_CURL_VERSION(7,14,1) } else if (!strcmp(key, "resetcookies")) { - http_request_object_resetcookies(getThis()); + getObject(http_request_object, obj); + http_request_reset_cookies(obj->request); #endif } else { ZVAL_ADDREF(*opt); @@ -1050,18 +1045,34 @@ PHP_METHOD(HttpRequest, getCookies) } /* }}} */ -#if HTTP_CURL_VERSION(7,14,1) +/* {{{ proto bool HttpRequest::enableCookies() + * + * Enable automatic sending of received cookies. + * Note that cuutomly set cookies will be sent anyway. + */ +PHP_METHOD(HttpRequest, enableCookies) +{ + NO_ARGS { + getObject(http_request_object, obj); + RETURN_SUCCESS(http_request_enable_cookies(obj->request)); + } + +} +/* }}} */ + /* {{{ proto bool HttpRequest::resetCookies() * - * Reset all cookies. Note that customly set cookies are not affected. + * Reset all automatically received/sent cookies. + * Note that customly set cookies are not affected. */ PHP_METHOD(HttpRequest, resetCookies) { - NO_ARGS; - RETURN_SUCCESS(http_request_object_resetcookies(getThis())); + NO_ARGS { + getObject(http_request_object, obj); + RETURN_SUCCESS(http_request_reset_cookies(obj->request)); + } } /* }}} */ -#endif /* {{{ proto bool HttpRequest::setUrl(string url) * diff --git a/php_http_request_api.h b/php_http_request_api.h index 5b4b752..5666fec 100644 --- a/php_http_request_api.h +++ b/php_http_request_api.h @@ -71,6 +71,12 @@ PHP_HTTP_API void _http_request_free(http_request **request); #define http_request_reset(r) _http_request_reset(r) PHP_HTTP_API void _http_request_reset(http_request *r); +#define http_request_enable_cookies(r) _http_request_enable_cookies(r) +PHP_HTTP_API STATUS _http_request_enable_cookies(http_request *request); + +#define http_request_reset_cookies(r) _http_request_reset_cookies(r) +PHP_HTTP_API STATUS _http_request_reset_cookies(http_request *request); + #define http_request_defaults(r) _http_request_defaults(r) PHP_HTTP_API void _http_request_defaults(http_request *request); diff --git a/php_http_request_object.h b/php_http_request_object.h index e4c2518..9ac4116 100644 --- a/php_http_request_object.h +++ b/php_http_request_object.h @@ -56,9 +56,8 @@ 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, enableCookies); 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 index 4192aee..b8bcaa5 100644 --- a/tests/HttpRequest_010.phpt +++ b/tests/HttpRequest_010.phpt @@ -10,14 +10,12 @@ checkmin(5); echo "-TEST\n"; $r = new HttpRequest("http://dev.iworks.at/.cookie.php"); -$r->recordHistory = true; $r->send(); $c[0] = $r->getResponseInfo("cookies"); var_dump(empty($c[0])); -$r->setOptions(array("cookiesession" => true)); - +$r->enableCookies(); $r->send(); $c[1] = $r->getResponseInfo("cookies"); var_dump(empty($c[1])); -- 2.30.2