curl-and-python

Support curl_easy_reset

From: Nick Pilon <npilon_at_oreilly.com>
Date: Wed, 09 Jul 2008 09:53:01 -0700

I've attached a patch against CVS head that provides access to
curl_easy_reset for Python programs. This seems to be the only way to
clear many options, and is rather useful for re-using a CURL handle (to
take advantage of its persistent information) when making a series of
HTTP requests where some, but not all, use hard-to-clear options.

After calling curl_easy_reset, do_curl_reset does the same work to free
up callbacks, file handles, and variables allocated by setopt as
util_curl_close.

Please review and criticize.

? patchfile
Index: src/pycurl.c
===================================================================
RCS file: /cvsroot/pycurl/pycurl/src/pycurl.c,v
retrieving revision 1.144
diff -U3 -r1.144 pycurl.c
--- src/pycurl.c 12 Jun 2008 18:12:17 -0000 1.144
+++ src/pycurl.c 8 Jul 2008 23:49:32 -0000
@@ -1363,6 +1363,44 @@
 }
 
 
+/* ------------------------ reset ------------------------ */
+
+static PyObject*
+do_curl_reset(CurlObject *self)
+{
+ unsigned int i;
+
+ curl_easy_reset(self->handle);
+
+ /* Decref callbacks and file handles */
+ util_curl_xdecref(self, 4 | 8, self->handle);
+
+ /* Free all variables allocated by setopt */
+#undef SFREE
+#define SFREE(v) if ((v) != NULL) (curl_formfree(v), (v) = NULL)
+ SFREE(self->httppost);
+#undef SFREE
+#define SFREE(v) if ((v) != NULL) (curl_slist_free_all(v), (v) = NULL)
+ SFREE(self->httpheader);
+ SFREE(self->http200aliases);
+ SFREE(self->quote);
+ SFREE(self->postquote);
+ SFREE(self->prequote);
+#undef SFREE
+
+ /* Last, free the options. This must be done after the curl handle
+ * is closed since libcurl assumes that some options are valid when
+ * invoking curl_easy_cleanup(). */
+ for (i = 0; i < OPTIONS_SIZE; i++) {
+ if (self->options[i] != NULL) {
+ free(self->options[i]);
+ self->options[i] = NULL;
+ }
+ }
+
+ return Py_None;
+}
+
 /* --------------- unsetopt/setopt/getinfo --------------- */
 
 static PyObject *
@@ -2842,6 +2880,7 @@
 static char co_perform_doc [] = "perform() -> None. Perform a file transfer. Throws pycurl.error exception upon failure.\n";
 static char co_setopt_doc [] = "setopt(option, parameter) -> None. Set curl session option. Throws pycurl.error exception upon failure.\n";
 static char co_unsetopt_doc [] = "unsetopt(option) -> None. Reset curl session option to default value. Throws pycurl.error exception upon failure.\n";
+static char co_reset_doc [] = "reset() -> None. Reset all options set on curl handle to default values, but preserves live connections, session ID cache, DNS cache, cookies, and shares.\n";
 
 static char co_multi_fdset_doc [] = "fdset() -> Tuple. Returns a tuple of three lists that can be passed to the select.select() method .\n";
 static char co_multi_info_read_doc [] = "info_read([max_objects]) -> Tuple. Returns a tuple (number of queued handles, [curl objects]).\n";
@@ -2861,6 +2900,7 @@
     {"perform", (PyCFunction)do_curl_perform, METH_NOARGS, co_perform_doc},
     {"setopt", (PyCFunction)do_curl_setopt, METH_VARARGS, co_setopt_doc},
     {"unsetopt", (PyCFunction)do_curl_unsetopt, METH_VARARGS, co_unsetopt_doc},
+ {"reset", (PyCFunction)do_curl_reset, METH_NOARGS, co_reset_doc},
     {NULL, NULL, 0, NULL}
 };
 

_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-python
Received on 2008-07-09