- fix cookie handling
authorMichael Wallner <mike@php.net>
Sun, 21 May 2006 18:41:07 +0000 (18:41 +0000)
committerMichael Wallner <mike@php.net>
Sun, 21 May 2006 18:41:07 +0000 (18:41 +0000)
- add HttpRequest::resetCookies()
- add test

http_request_api.c
http_request_object.c
php_http_request_object.h
tests/HttpRequest_010.phpt [new file with mode: 0644]

index 1addfd96d0b418cd0a8d33a0e3ccc4a85aea57e4..96a613fd66a1a1fb5e95e10d7d21c27bcac7da36 100644 (file)
@@ -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));
        }
index 7fa8ead74710fc894fb3d1cc983ee8a0ce537fad..b2d45fce920cd775e80069ce163e7a1985b7c059 100644 (file)
@@ -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.
index 7a7a2fbd69f793a2e6ecf89efe704bbfe081d8f2..e4c25189dea144e9ac94650e4b5059c4416a1f7f 100644 (file)
@@ -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 (file)
index 0000000..4192aee
--- /dev/null
@@ -0,0 +1,42 @@
+--TEST--
+HttpRequest cookie API
+--SKIPIF--
+<?php
+include 'skip.inc';
+checkmin(5);
+?>
+--FILE--
+<?php
+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->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