]> sjero.net Git - wget/blobdiff - src/http.c
[svn] Allow Wget to be compiled with a K&R compiler.
[wget] / src / http.c
index 261b51e4cf980ad50c0e6fbc8dfc3241d8e86900..6dacacab060c68d1b03101e4e5c3dae5bd6a0e6b 100644 (file)
@@ -44,12 +44,6 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 # endif
 #endif
 
-#ifdef WINDOWS
-# include <winsock.h>
-#else
-# include <netdb.h>            /* for h_errno */
-#endif
-
 #include "wget.h"
 #include "utils.h"
 #include "url.h"
@@ -73,11 +67,6 @@ extern char *version_string;
 #ifndef errno
 extern int errno;
 #endif
-#ifndef h_errno
-# ifndef __CYGWIN__
-extern int h_errno;
-# endif
-#endif
 \f
 static int cookies_loaded_p;
 
@@ -324,13 +313,15 @@ invalidate_persistent (void)
 
    If a previous connection was persistent, it is closed. */
 
-static void
-register_persistent (const char *host, unsigned short port, int fd
 #ifdef HAVE_SSL
-                    , SSL *ssl
-#endif
-                    )
+static void
+register_persistent (const char *host, unsigned short port, int fd, SSL *ssl)
+{
+#else
+static void
+register_persistent (const char *host, unsigned short port, int fd)
 {
+#endif
   if (pc_active_p)
     {
       if (pc_last_fd == fd)
@@ -386,13 +377,15 @@ register_persistent (const char *host, unsigned short port, int fd
 /* Return non-zero if a persistent connection is available for
    connecting to HOST:PORT.  */
 
-static int
-persistent_available_p (const char *host, unsigned short port
 #ifdef HAVE_SSL
-                       , int ssl
-#endif
-                       )
+static int
+persistent_available_p (const char *host, unsigned short port, int ssl)
+{
+#else
+static int
+persistent_available_p (const char *host, unsigned short port)
 {
+#endif
   int success;
   struct address_list *this_host_ip;
 
@@ -570,6 +563,11 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
   /* Whether keep-alive should be inhibited. */
   int inhibit_keep_alive;
 
+  /* Whether we need to print the host header with braces around host,
+     e.g. "Host: [3ffe:8100:200:2::2]:1234" instead of the usual
+     "Host: symbolic-name:1234". */
+  int squares_around_host = 0;
+
 #ifdef HAVE_SSL
   /* initialize ssl_ctx on first run */
   if (!ssl_ctx)
@@ -822,6 +820,9 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
        "param=value", full_path will be "/foo/bar?param=value".  */
     full_path = url_full_path (u);
 
+  if (strchr (u->host, ':'))
+    squares_around_host = 1;
+
   /* Allocate the memory for the request.  */
   request = (char *)alloca (strlen (command)
                            + strlen (full_path)
@@ -843,11 +844,12 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
   sprintf (request, "\
 %s %s HTTP/1.0\r\n\
 User-Agent: %s\r\n\
-Host: %s%s\r\n\
+Host: %s%s%s%s\r\n\
 Accept: %s\r\n\
 %s%s%s%s%s%s%s%s\r\n",
           command, full_path,
-          useragent, u->host,
+          useragent,
+          squares_around_host ? "[" : "", u->host, squares_around_host ? "]" : "",
           port_maybe ? port_maybe : "",
           HTTP_ACCEPT,
           request_keep_alive ? request_keep_alive : "",
@@ -868,7 +870,7 @@ Accept: %s\r\n\
 
   /* Send the request to server.  */
 #ifdef HAVE_SSL
-  if (u->scheme == SCHEME_HTTPS)
+  if (conn->scheme == SCHEME_HTTPS)
     num_written = ssl_iwrite (ssl, request, strlen (request));
   else
 #endif /* HAVE_SSL */
@@ -891,7 +893,7 @@ Accept: %s\r\n\
   /* Before reading anything, initialize the rbuf.  */
   rbuf_initialize (&rbuf, sock);
 #ifdef HAVE_SSL
-  if (u->scheme == SCHEME_HTTPS)
+  if (conn->scheme == SCHEME_HTTPS)
     rbuf.ssl = ssl;
   else
     rbuf.ssl = NULL;
@@ -1226,6 +1228,8 @@ Accept: %s\r\n\
              /* In case the caller inspects. */
              hs->len = contlen;
              hs->res = 0;
+             /* Mark as successfully retrieved. */
+             *dt |= RETROKF;
              FREE_MAYBE (type);
              FREE_MAYBE (all_headers);
              CLOSE_INVALIDATE (sock);  /* would be CLOSE_FINISH, but there
@@ -1368,6 +1372,11 @@ Refusing to truncate existing file `%s'.\n\n"), *hs->local_file);
                          (contlen != -1 ? contlen : 0),
                          &rbuf, keep_alive, &hs->dltime);
 
+  if (hs->res >= 0)
+    CLOSE_FINISH (sock);
+  else
+    CLOSE_INVALIDATE (sock);
+
   {
     /* Close or flush the file.  We have to be careful to check for
        error here.  Checking the result of fwrite() is not enough --
@@ -1381,7 +1390,6 @@ Refusing to truncate existing file `%s'.\n\n"), *hs->local_file);
       hs->res = -2;
   }
   FREE_MAYBE (all_headers);
-  CLOSE_FINISH (sock);
   if (hs->res == -2)
     return FWRITEERR;
   return RETRFINISHED;
@@ -2175,7 +2183,7 @@ dump_hash (unsigned char *buf, const unsigned char *hash)
 
 /* Take the line apart to find the challenge, and compose a digest
    authorization header.  See RFC2069 section 2.1.2.  */
-char *
+static char *
 digest_authentication_encode (const char *au, const char *user,
                              const char *passwd, const char *method,
                              const char *path)