cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Patch for 301/302 redirect after post

From: Martin Drasar <drasar_at_optimsys.cz>
Date: Mon, 11 Aug 2008 14:10:11 +0200

Daniel Stenberg napsal(a):
> On Mon, 11 Aug 2008, Martin Drasar wrote:
>
>> Patch is attached, I hope that this time it's ok ;-)
>
> Please use diff -u!
>

omg! my bad... here's the better version

-- 
   Martin Drasar, Developer / Analyst
   OptimSys, s.r.o.
   drasar_at_optimsys.cz
   Tel: +420 541 143 065
   Fax: +420 541 143 066
   http://www.optimsys.cz

? patch
Index: include/curl/curl.h
===================================================================
RCS file: /cvsroot/curl/curl/include/curl/curl.h,v
retrieving revision 1.361
diff -u -r1.361 curl.h
--- include/curl/curl.h 7 Aug 2008 00:29:08 -0000 1.361
+++ include/curl/curl.h 11 Aug 2008 12:06:10 -0000
@@ -1120,8 +1120,9 @@
   CINIT(NEW_FILE_PERMS, LONG, 159),
   CINIT(NEW_DIRECTORY_PERMS, LONG, 160),
 
- /* Obey RFC 2616/10.3.2 and keep POSTs as POSTs after a 301 */
- CINIT(POST301, LONG, 161),
+ /* Set the behaviour of POST when redirecting. Values must be set to one
+ of CURL_REDIR* enums below */
+ CINIT(POSTREDIR, LONG, 161),
 
   /* used by scp/sftp to verify the host's public key */
   CINIT(SSH_HOST_PUBLIC_KEY_MD5, OBJECTPOINT, 162),
@@ -1161,6 +1162,10 @@
 /* Backwards compatibility with older names */
 /* These are scheduled to disappear by 2009 */
 
+/* This one was added in version 7.18.2 */
+#define CURLOPT_POST301 CURLOPT_POSTREDIR
+
+
 /* The following were added in 7.17.0 */
 #define CURLOPT_SSLKEYPASSWD CURLOPT_KEYPASSWD
 #define CURLOPT_FTPAPPEND CURLOPT_APPEND
@@ -1223,6 +1228,16 @@
   CURL_SSLVERSION_LAST /* never use, keep last */
 };
 
+/* enums to use with CURLOPT_POSTREDIR.
+ CURL_REDIR_POST_301 and CURL_REDIR_POST_302 can be bitwise ORed so that
+ CURL_REDIR_POST_301 | CURL_REDIR_POST_302 == CURL_REDIR_POST_ALL */
+enum {
+ CURL_REDIR_GET_ALL,
+ CURL_REDIR_POST_301,
+ CURL_REDIR_POST_302,
+ CURL_REDIR_POST_ALL
+};
+
 
 typedef enum {
   CURL_TIMECOND_NONE,
Index: lib/transfer.c
===================================================================
RCS file: /cvsroot/curl/curl/lib/transfer.c,v
retrieving revision 1.399
diff -u -r1.399 transfer.c
--- lib/transfer.c 4 Aug 2008 22:00:22 -0000 1.399
+++ lib/transfer.c 11 Aug 2008 12:06:10 -0000
@@ -2192,7 +2192,8 @@
      * libcurl gets the page that most user agents would get, libcurl has to
      * force GET.
      *
- * This behaviour can be overriden with CURLOPT_POST301.
+ * This behaviour can be overriden with CURLOPT_POST301 or
+ * CURLOPT_POSTREDIR
      */
     if( (data->set.httpreq == HTTPREQ_POST
          || data->set.httpreq == HTTPREQ_POST_FORM)
@@ -2219,7 +2220,18 @@
     status. When interoperability with such clients is a concern, the
     302 status code may be used instead, since most user agents react
     to a 302 response as described here for 303.
+
+ This behaviour can be overriden with CURLOPT_POSTREDIR
     */
+ if( (data->set.httpreq == HTTPREQ_POST
+ || data->set.httpreq == HTTPREQ_POST_FORM)
+ && !data->set.post302) {
+ infof(data,
+ "Violate RFC 2616/10.3.3 and switch from POST to GET\n");
+ data->set.httpreq = HTTPREQ_GET;
+ }
+ break;
+
   case 303: /* See Other */
     /* Disable both types of POSTs, since doing a second POST when
      * following isn't what anyone would want! */
Index: lib/url.c
===================================================================
RCS file: /cvsroot/curl/curl/lib/url.c,v
retrieving revision 1.726
diff -u -r1.726 url.c
--- lib/url.c 1 Aug 2008 02:09:08 -0000 1.726
+++ lib/url.c 11 Aug 2008 12:06:10 -0000
@@ -1021,11 +1021,19 @@
     data->set.maxredirs = va_arg(param, long);
     break;
 
- case CURLOPT_POST301:
+ case CURLOPT_POSTREDIR:
     /*
- * Obey RFC 2616/10.3.2 and resubmit a POST as a POST after a 301.
- */
- data->set.post301 = (bool)(0 != va_arg(param, long));
+ * Set the behaviour of POST when redirecting
+ * CURL_REDIR_GET_ALL - POST is changed to GET after 301 and 302
+ * CURL_REDIR_POST_301 - POST is kept as POST after 301
+ * CURL_REDIR_POST_302 - POST is kept as POST after 302
+ * CURL_REDIR_POST_ALL - POST is kept as POST after 301 and 302
+ * other - POST is kept as POST after 301 and 302
+ */
+ data->set.post301 = (CURL_REDIR_GET_ALL != va_arg(param, long)) &&
+ (CURL_REDIR_POST_302 != va_arg(param, long));
+ data->set.post302 = (CURL_REDIR_GET_ALL != va_arg(param, long)) &&
+ (CURL_REDIR_POST_301 != va_arg(param, long));
     break;
 
   case CURLOPT_POST:
Index: lib/urldata.h
===================================================================
RCS file: /cvsroot/curl/curl/lib/urldata.h,v
retrieving revision 1.384
diff -u -r1.384 urldata.h
--- lib/urldata.h 30 Jul 2008 21:55:27 -0000 1.384
+++ lib/urldata.h 11 Aug 2008 12:06:10 -0000
@@ -1348,6 +1348,7 @@
                         for infinity */
   bool post301; /* Obey RFC 2616/10.3.2 and keep POSTs as POSTs after a
                         301 */
+ bool post302; /* keep POSTs as POSTs after a 302 */
   bool free_referer; /* set TRUE if 'referer' points to a string we
                         allocated */
   void *postfields; /* if POST, set the fields' values here */
Received on 2008-08-11