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: Tue, 12 Aug 2008 12:49:03 +0200

Daniel Stenberg napsal(a):
> On Tue, 12 Aug 2008, Martin Drasar wrote:
>
>> But before I send the final diff (hopefully!) - I have noticed that
>> renaming to POSTREDIR causes curl not to build. It seems that it is
>> using CURL_NO_OLDIES.
>
> Ah yes it is. (The reason for this is mostly because so many people use
> and read curl source code as a sample implementation of how to use
> libcurl so I don't want old names to linger in there - for educational
> reasons!)
>
>> Should I fix curl so that it is using POSTREDIR instead of POST?
>
> Yes please!
>

Done.
Here's the complete patch with src/main.c fixed.

-- 
   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_redirect
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 12 Aug 2008 10:42:59 -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),
@@ -1159,6 +1160,11 @@
                           the obsolete stuff removed! */
 
 /* Backwards compatibility with older names */
+/* These are scheduled to disappear by 2011 */
+
+/* This one was added in version 7.18.2 */
+#define CURLOPT_POST301 CURLOPT_POSTREDIR
+
 /* These are scheduled to disappear by 2009 */
 
 /* The following were added in 7.17.0 */
@@ -1223,6 +1229,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 12 Aug 2008 10:43:00 -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 12 Aug 2008 10:43:00 -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 12 Aug 2008 10:43:00 -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 */
Index: src/main.c
===================================================================
RCS file: /cvsroot/curl/curl/src/main.c,v
retrieving revision 1.468
diff -u -r1.468 main.c
--- src/main.c 1 Aug 2008 18:41:14 -0000 1.468
+++ src/main.c 12 Aug 2008 10:43:00 -0000
@@ -508,6 +508,7 @@
   char *libcurl; /* output libcurl code to this file name */
   bool raw;
   bool post301;
+ bool post302;
   bool nokeepalive; /* for keepalive needs */
   long alivetime;
 
@@ -732,6 +733,7 @@
     " -o/--output <file> Write output to <file> instead of stdout",
     " --pass <pass> Pass phrase for the private key (SSL/SSH)",
     " --post301 Do not switch to GET after following a 301 redirect (H)",
+ " --post302 Do not switch to GET after following a 302 redirect (H)",
     " -#/--progress-bar Display transfer progress as a progress bar",
     " -x/--proxy <host[:port]> Use HTTP proxy on given port",
     " --proxy-anyauth Pick \"any\" proxy authentication method (H)",
@@ -1639,6 +1641,7 @@
     {"$1", "keepalive", FALSE}, /* listed as --no-keepalive in the help */
     {"$2", "socks5-hostname", TRUE},
     {"$3", "keepalive-time", TRUE},
+ {"$4", "post302", FALSE},
 
     {"0", "http1.0", FALSE},
     {"1", "tlsv1", FALSE},
@@ -2147,6 +2150,9 @@
         if(str2num(&config->alivetime, nextarg))
           return PARAM_BAD_NUMERIC;
         break;
+ case '4': /* --post302 */
+ config->post302 = toggle;
+ break;
       }
       break;
     case '#': /* --progress-bar */
@@ -4757,12 +4763,15 @@
         }
 
         /* curl 7.17.1 */
- my_setopt(curl, CURLOPT_POST301, config->post301);
         if (!config->nokeepalive) {
           my_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockoptcallback);
           my_setopt(curl, CURLOPT_SOCKOPTDATA, config);
         }
 
+ /* curl 7.18.2 */
+ my_setopt(curl, CURLOPT_POSTREDIR, config->post301 |
+ (config->post302 ? CURL_REDIR_POST_302 : FALSE));
+
         retry_numretries = config->req_retry;
 
         retrystart = cutil_tvnow();
Received on 2008-08-12