- adjust cookie handling to work better together with libcurl
authorMichael Wallner <mike@php.net>
Mon, 22 May 2006 09:20:40 +0000 (09:20 +0000)
committerMichael Wallner <mike@php.net>
Mon, 22 May 2006 09:20:40 +0000 (09:20 +0000)
# blog post to follow

docs/functions.html
http_functions.c
http_request_api.c
http_request_object.c
php_http_request_api.h
php_http_request_object.h
tests/HttpRequest_010.phpt

index 2c1a469bc0887b074c4d6005e4c6c22c7e253ee9..d1725a19ff9e28e7b9e8319fe45b14c32a701e9b 100644 (file)
@@ -327,9 +327,8 @@ array where the following keys will be recognized:</p><pre> - redirect:
  - cookies:          array, list of cookies as associative array<br />
                      like array("cookie" => "value")<br />
  - encodecookies:    bool, whether to urlencode the cookies (default: true)<br />
- - resetcookies:     bool, wheter to reset the cookies<br />
  - cookiestore:      string, path to a file where cookies are/will be stored<br />
- - cookiesession:    bool, accept (true) or reset (false) sessioncookies<br />
+ - cookiesession:    bool, don't load session cookies from cookiestore if TRUE<br />
  - resume:           int, byte offset to start the download from;<br />
                      if the server supports ranges<br />
  - range:            array, array of arrays, each containing two integers,<br />
@@ -705,6 +704,12 @@ pairs to add.</p>
 <h3 id="HttpRequest_getCookies">array HttpRequest::getCookies()</h3>
 <p>Get previously set cookies.</p>
 <p>Returns an associative array containing any previously set cookies.</p>
+<h3 id="HttpRequest_enableCookies">bool HttpRequest::enableCookies()</h3>
+<p>Enable automatic sending of received cookies.<br />
+Note that cuutomly set cookies will be sent anyway.</p>
+<h3 id="HttpRequest_resetCookies">bool HttpRequest::resetCookies()</h3>
+<p>Reset all automatically received/sent cookies.<br />
+Note that customly set cookies are not affected.</p>
 <h3 id="HttpRequest_setUrl">bool HttpRequest::setUrl(string url)</h3>
 <p>Set the request URL.</p>
 <p>Expects a string as parameter specifying the request url.</p>
@@ -1326,6 +1331,8 @@ http.cache_log is set.</p>
 <li><a href="#HttpRequest_setCookies">HttpRequest::setCookies()</a></li>
 <li><a href="#HttpRequest_addCookies">HttpRequest::addCookies()</a></li>
 <li><a href="#HttpRequest_getCookies">HttpRequest::getCookies()</a></li>
+<li><a href="#HttpRequest_enableCookies">HttpRequest::enableCookies()</a></li>
+<li><a href="#HttpRequest_resetCookies">HttpRequest::resetCookies()</a></li>
 <li><a href="#HttpRequest_setUrl">HttpRequest::setUrl()</a></li>
 <li><a href="#HttpRequest_getUrl">HttpRequest::getUrl()</a></li>
 <li><a href="#HttpRequest_setMethod">HttpRequest::setMethod()</a></li>
@@ -1423,7 +1430,7 @@ http.cache_log is set.</p>
 </li>
 </ul>
 </div>
-    <p><b>Generated at: Fri, 19 May 2006 16:55:19 +0200</b></p>
+    <p><b>Generated at: Mon, 22 May 2006 11:19:25 +0200</b></p>
 </body>
 </html>
 
index 0dfb68bcf9747e9554d7392a21a8814f0d839087..807b0a527cb94ae2f3bfeff483bf066b6eb08d7c 100644 (file)
@@ -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,
index 96a613fd66a1a1fb5e95e10d7d21c27bcac7da36..d6a370481dbe2da6cb0c90cbb9e276f7f8f50d6d 100644 (file)
@@ -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 */
index 60935420a651332df6c027089e3512c694b26459..45625f0f777ab4dbf1fa0be4d5e229ed5818e591 100644 (file)
@@ -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)
  *
index 5b4b752b311f9f2d6a1a7ba071b4b3f63b31f6ef..5666fecbdc09fa181a176799ca6d3200644deebe 100644 (file)
@@ -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);
 
index e4c25189dea144e9ac94650e4b5059c4416a1f7f..9ac41162717de00f1f45352dc910dfe8f3f5fd00 100644 (file)
@@ -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);
index 4192aeef39db162aca57a8e03ddce60b86062fd2..b8bcaa516975a03900d2b10441f944c51ced8907 100644 (file)
@@ -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]));