php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #54022
Patch curl_reset revision 2011-04-05 00:48 UTC by mtdowling at gmail dot com

Patch curl_reset for cURL related Bug #54022

Patch version 2011-04-05 00:48 UTC

Return to Bug #54022 | Download this patch
Patch Revisions:

Developer: mtdowling@gmail.com

Index: ext/curl/interface.c
===================================================================
--- ext/curl/interface.c	(revision 309954)
+++ ext/curl/interface.c	(working copy)
@@ -249,6 +249,10 @@
 	ZEND_ARG_INFO(0, ch)
 ZEND_END_ARG_INFO()
 
+ZEND_BEGIN_ARG_INFO(arginfo_curl_reset, 0)
+	ZEND_ARG_INFO(0, ch)
+ZEND_END_ARG_INFO()
+
 ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_init, 0)
 ZEND_END_ARG_INFO()
 
@@ -299,6 +303,7 @@
 	PHP_FE(curl_error,               arginfo_curl_error)
 	PHP_FE(curl_errno,               arginfo_curl_errno)
 	PHP_FE(curl_close,               arginfo_curl_close)
+	PHP_FE(curl_reset,               arginfo_curl_reset)
 	PHP_FE(curl_multi_init,          arginfo_curl_multi_init)
 	PHP_FE(curl_multi_add_handle,    arginfo_curl_multi_add_handle)
 	PHP_FE(curl_multi_remove_handle, arginfo_curl_multi_remove_handle)
@@ -1432,6 +1437,36 @@
 /* }}} */
 #endif
 
+/* {{{ _php_curl_set_default_options()
+   Set default options for a handle */
+static void _php_set_default_options(php_curl *ch)
+{
+	char *cainfo;
+
+	curl_easy_setopt(ch->cp, CURLOPT_NOPROGRESS,        1);
+	curl_easy_setopt(ch->cp, CURLOPT_VERBOSE,           0);
+	curl_easy_setopt(ch->cp, CURLOPT_ERRORBUFFER,       ch->err.str);
+	curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION,     curl_write);
+	curl_easy_setopt(ch->cp, CURLOPT_FILE,              (void *) ch);
+	curl_easy_setopt(ch->cp, CURLOPT_READFUNCTION,      curl_read);
+	curl_easy_setopt(ch->cp, CURLOPT_INFILE,            (void *) ch);
+	curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION,    curl_write_header);
+	curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER,       (void *) ch);
+	curl_easy_setopt(ch->cp, CURLOPT_DNS_USE_GLOBAL_CACHE, 1);
+	curl_easy_setopt(ch->cp, CURLOPT_DNS_CACHE_TIMEOUT, 120);
+	curl_easy_setopt(ch->cp, CURLOPT_MAXREDIRS, 20); /* prevent infinite redirects */
+
+	cainfo = INI_STR("curl.cainfo");
+	if (cainfo && strlen(cainfo) > 0) {
+		curl_easy_setopt(ch->cp, CURLOPT_CAINFO, cainfo);
+	}
+
+#if defined(ZTS)
+	curl_easy_setopt(ch->cp, CURLOPT_NOSIGNAL, 1);
+#endif
+}
+/* }}} */
+
 /* {{{ proto resource curl_init([string url])
    Initialize a cURL session */
 PHP_FUNCTION(curl_init)
@@ -1441,7 +1476,6 @@
 	zval		*clone;
 	char		*url = NULL;
 	int		url_len = 0;
-	char *cainfo;
 
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &url, &url_len) == FAILURE) {
 		return;
@@ -1468,30 +1502,8 @@
 	MAKE_STD_ZVAL(clone);
 	ch->clone = clone;
 
+        _php_set_default_options(ch);
 
-
-	curl_easy_setopt(ch->cp, CURLOPT_NOPROGRESS,        1);
-	curl_easy_setopt(ch->cp, CURLOPT_VERBOSE,           0);
-	curl_easy_setopt(ch->cp, CURLOPT_ERRORBUFFER,       ch->err.str);
-	curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION,     curl_write);
-	curl_easy_setopt(ch->cp, CURLOPT_FILE,              (void *) ch);
-	curl_easy_setopt(ch->cp, CURLOPT_READFUNCTION,      curl_read);
-	curl_easy_setopt(ch->cp, CURLOPT_INFILE,            (void *) ch);
-	curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION,    curl_write_header);
-	curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER,       (void *) ch);
-	curl_easy_setopt(ch->cp, CURLOPT_DNS_USE_GLOBAL_CACHE, 1);
-	curl_easy_setopt(ch->cp, CURLOPT_DNS_CACHE_TIMEOUT, 120);
-	curl_easy_setopt(ch->cp, CURLOPT_MAXREDIRS, 20); /* prevent infinite redirects */
-
-	cainfo = INI_STR("curl.cainfo");
-	if (cainfo && strlen(cainfo) > 0) {
-		curl_easy_setopt(ch->cp, CURLOPT_CAINFO, cainfo);
-	}
-
-#if defined(ZTS)
-	curl_easy_setopt(ch->cp, CURLOPT_NOSIGNAL, 1);
-#endif
-
 	if (url) {
 		if (!php_curl_option_url(ch, url, url_len)) {
 			_php_curl_close_ex(ch TSRMLS_CC);
@@ -2559,6 +2571,30 @@
 }
 /* }}} */
 
+/* {{{ proto void curl_reset(resource ch)
+   Reset a cURL session */
+PHP_FUNCTION(curl_reset)
+{
+	zval        *zid;
+	php_curl    *ch;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
+		return;
+	}
+
+	ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
+
+	if (ch->in_callback) {
+		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to reset cURL handle from a callback");
+		return;
+	}
+
+	curl_easy_reset(ch->cp);
+
+	_php_set_default_options(ch);
+}
+/* }}} */
+
 #endif /* HAVE_CURL */
 
 /*
Index: ext/curl/tests/curl_reset_basic.phpt
===================================================================
--- ext/curl/tests/curl_reset_basic.phpt	(revision 0)
+++ ext/curl/tests/curl_reset_basic.phpt	(revision 0)
@@ -0,0 +1,51 @@
+--TEST--
+curl_reset_basic
+--CREDITS--
+Michael Dowling <mtdowling @ gmail.com>
+--SKIPIF--
+<?php
+if (!extension_loaded("curl")) exit("skip curl extension not loaded");
+if (false === getenv('PHP_CURL_HTTP_REMOTE_SERVER'))  exit("skip PHP_CURL_HTTP_REMOTE_SERVER env variable is not defined");
+?>
+--FILE--
+<?php
+/* Prototype  : bool curl_reset(resource ch)
+ * Description: Reset a cURL session
+ * Source code: ext/curl/interface.c
+ * Alias to functions:
+ */
+
+  $host = getenv('PHP_CURL_HTTP_REMOTE_SERVER');
+
+  // start testing
+  echo "*** Testing curl_reset() ***\n";
+
+  $url = "{$host}/get.php?test=get";
+  $ch = curl_init();
+
+  curl_setopt($ch, CURLOPT_URL, $url); // set the url we want to use
+  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+  curl_setopt($ch, CURLINFO_HEADER_OUT, true);
+  curl_setopt($ch, CURLOPT_ENCODING, ''); // Enable all available encodings
+  curl_exec($ch);
+  $info = curl_getinfo($ch);
+  var_dump(strpos($info['request_header'], 'Accept-Encoding') !== false);
+
+  curl_reset($ch);
+
+  // Send the request again, this time without the Accept-Encoding header
+  curl_setopt($ch, CURLOPT_URL, $url);
+  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+  curl_setopt($ch, CURLINFO_HEADER_OUT, true);
+  curl_exec($ch);
+  $info = curl_getinfo($ch);
+  var_dump(strpos($info['request_header'], 'Accept-Encoding') !== false);
+
+  curl_close($ch);
+?>
+===DONE===
+--EXPECTF--
+*** Testing curl_reset() ***
+bool(true)
+bool(false)
+===DONE===
\ No newline at end of file
Index: ext/curl/php_curl.h
===================================================================
--- ext/curl/php_curl.h	(revision 309954)
+++ ext/curl/php_curl.h	(working copy)
@@ -69,6 +69,7 @@
 PHP_FUNCTION(curl_error);
 PHP_FUNCTION(curl_errno);
 PHP_FUNCTION(curl_close);
+PHP_FUNCTION(curl_reset);
 PHP_FUNCTION(curl_multi_init);
 PHP_FUNCTION(curl_multi_add_handle);
 PHP_FUNCTION(curl_multi_remove_handle);
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 22 00:01:30 2024 UTC