Index: lib/transfer.c
===================================================================
RCS file: /cvsroot/curl/curl/lib/transfer.c,v
retrieving revision 1.369
diff -u -p -r1.369 transfer.c
--- lib/transfer.c	2 Oct 2007 10:21:36 -0000	1.369
+++ lib/transfer.c	12 Oct 2007 15:54:37 -0000
@@ -2016,6 +2016,26 @@ static void strcpy_url(char *output, con
 
 }
 
+/* returns 1 if the two urls have the same protocol
+ * (http and https are considered same) */
+static int same_proto(const char *url1, const char *url2)
+{
+  char *p1 = strchr(url1, ':');
+  char *p2 = strchr(url2, ':');
+  if (!p1 || !p2) {
+    /* shouldn't happen */
+    return 0;
+  }
+  if ((checkprefix("http:", url1) && checkprefix("https:", url2)) ||
+      (checkprefix("http:", url2) && checkprefix("https:", url1))) {
+    return 1;
+  }
+  if (p1 - url1 != p2 - url2) {
+    return 0;
+  }
+  return strnequal(url1, url2, p1 - url1);
+}
+
 /*
  * Curl_follow() handles the URL redirect magic. Pass in the 'newurl' string
  * as given by the remote server and set up the new URL to request.
@@ -2213,6 +2233,14 @@ CURLcode Curl_follow(struct SessionHandl
     }
 
   }
+  /* Both URLSs are absolute now, check if we're switching protocols */
+  if (!same_proto(data->change.url, newest)) {
+    if (data->reqdata.proto.generic) {
+      free(data->reqdata.proto.generic);
+      data->reqdata.proto.generic = NULL;
+    }
+  }
+
 
   if(data->change.url_alloc)
     free(data->change.url);

