Don't attempt to read (skip) short body in response to HEAD

Hrvoje Niksic <[email protected]> Mon, 30 May 2005 16:27:49 +0200
Newsgroups gmane.comp.web.wget.patches
Message-ID <[email protected]>
I noticed that `wget --spider www.hotmail.com' hangs indefinitely due
to Wget's attempt to read and skip the body of the redirection --
which never comes because HEAD is used.  This patch fixes the problem.

2005-05-30  Hrvoje Niksic  <[email protected]>

	* http.c (gethttp): Don't attempt to "skip short body" if we're
	issuing a HEAD request (in which the response head is not followed
	by a body).

Index: src/http.c
===================================================================
RCS file: /pack/anoncvs/wget/src/http.c,v
retrieving revision 1.189
diff -u -r1.189 http.c
--- src/http.c	2005/05/24 14:16:27	1.189
+++ src/http.c	2005/05/30 14:25:34
@@ -1158,6 +1158,10 @@
   /* Whether our connection to the remote host is through SSL.  */
   int using_ssl = 0;
 
+  /* Whether a HEAD request will be issued (as opposed to GET or
+     POST). */
+  int head_only = *dt & HEAD_ONLY;
+
   char *head;
   struct response *resp;
   char hdrval[256];
@@ -1197,7 +1201,7 @@
     }
 #endif /* HAVE_SSL */
 
-  if (!(*dt & HEAD_ONLY))
+  if (!head_only)
     /* If we're doing a GET on the URL, as opposed to just a HEAD, we need to
        know the local filename so we can save to it. */
     assert (*hs->local_file != NULL);
@@ -1218,7 +1222,7 @@
   {
     char *meth_arg;
     const char *meth = "GET";
-    if (*dt & HEAD_ONLY)
+    if (head_only)
       meth = "HEAD";
     else if (opt.post_file_name || opt.post_data)
       meth = "POST";
@@ -1625,7 +1629,7 @@
   if (statcode == HTTP_STATUS_UNAUTHORIZED)
     {
       /* Authorization is required.  */
-      if (keep_alive && skip_short_body (sock, contlen))
+      if (keep_alive && !head_only && skip_short_body (sock, contlen))
 	CLOSE_FINISH (sock);
       else
 	CLOSE_INVALIDATE (sock);
@@ -1765,7 +1769,7 @@
 		     _("Location: %s%s\n"),
 		     hs->newloc ? escnonprint_uri (hs->newloc) : _("unspecified"),
 		     hs->newloc ? _(" [following]") : "");
-	  if (keep_alive && skip_short_body (sock, contlen))
+	  if (keep_alive && !head_only && skip_short_body (sock, contlen))
 	    CLOSE_FINISH (sock);
 	  else
 	    CLOSE_INVALIDATE (sock);
@@ -1871,7 +1875,7 @@
   type = NULL;			/* We don't need it any more.  */
 
   /* Return if we have no intention of further downloading.  */
-  if (!(*dt & RETROKF) || (*dt & HEAD_ONLY))
+  if (!(*dt & RETROKF) || head_only)
     {
       /* In case the caller cares to look...  */
       hs->len = 0;