]> sjero.net Git - wget/blobdiff - src/http.c
[svn] Added NTLM support.
[wget] / src / http.c
index 646a031e4e7c9ad4d164de321dd1597dd6ccc415..c509b4aea1fcace5e612339dc917d6f870ddf993 100644 (file)
@@ -65,9 +65,12 @@ extern int errno;
 #include "netrc.h"
 #ifdef HAVE_SSL
 # include "gen_sslfunc.h"
-#endif /* HAVE_SSL */
+#endif
+#ifdef ENABLE_NTLM
+# include "http-ntlm.h"
+#endif
 #include "cookies.h"
-#ifdef USE_DIGEST
+#ifdef ENABLE_DIGEST
 # include "gen-md5.h"
 #endif
 #include "convert.h"
@@ -84,7 +87,7 @@ extern int output_stream_regular;
 
 \f
 static int cookies_loaded_p;
-struct cookie_jar *wget_cookie_jar;
+static struct cookie_jar *wget_cookie_jar;
 
 #define TEXTHTML_S "text/html"
 #define TEXTXHTML_S "application/xhtml+xml"
@@ -432,6 +435,13 @@ response_head_terminator (const char *hunk, int oldlen, int peeklen)
   return NULL;
 }
 
+/* The maximum size of a single HTTP response we care to read.  This
+   is not meant to impose an arbitrary limit, but to protect the user
+   from Wget slurping up available memory upon encountering malicious
+   or buggy server output.  Define it to 0 to remove the limit.  */
+
+#define HTTP_RESPONSE_MAX_SIZE 65536
+
 /* Read the HTTP request head from FD and return it.  The error
    conditions are the same as with fd_read_hunk.
 
@@ -443,7 +453,8 @@ response_head_terminator (const char *hunk, int oldlen, int peeklen)
 static char *
 read_http_response_head (int fd)
 {
-  return fd_read_hunk (fd, response_head_terminator, 512);
+  return fd_read_hunk (fd, response_head_terminator, 512,
+                      HTTP_RESPONSE_MAX_SIZE);
 }
 
 struct response {
@@ -816,6 +827,11 @@ static struct {
 
   /* Whether a ssl handshake has occoured on this connection.  */
   int ssl;
+
+#ifdef ENABLE_NTLM
+  /* NTLM data of the current connection.  */
+  struct ntlmdata ntlm;
+#endif
 } pconn;
 
 /* Mark the persistent connection as invalid and free the resources it
@@ -1081,8 +1097,15 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
      is done. */
   int keep_alive;
 
-  /* Whether keep-alive should be inhibited. */
-  int inhibit_keep_alive = !opt.http_keep_alive || opt.ignore_length;
+  /* Whether keep-alive should be inhibited.
+
+     RFC 2068 requests that 1.0 clients not send keep-alive requests
+     to proxies.  This is because many 1.0 proxies do not interpret
+     the Connection header and transfer it to the remote server,
+     causing it to not close the connection and leave both the proxy
+     and the client hanging.  */
+  int inhibit_keep_alive =
+    !opt.http_keep_alive || opt.ignore_length /*|| proxy != NULL*/;
 
   /* Headers sent when using POST. */
   wgint post_data_size = 0;
@@ -1129,7 +1152,7 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
   auth_tried_already = 0;
 
   /* Initialize certain elements of struct http_stat.  */
-  hs->len = 0L;
+  hs->len = 0;
   hs->contlen = -1;
   hs->res = -1;
   hs->newloc = NULL;
@@ -1341,14 +1364,23 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
         look up conn->host in some cases.  If that lookup failed, we
         don't need to bother with connect_to_host.  */
       if (host_lookup_failed)
-       return HOSTERR;
+       {
+         request_free (req);
+         return HOSTERR;
+       }
 
       sock = connect_to_host (conn->host, conn->port);
       if (sock == E_HOST)
-       return HOSTERR;
+       {
+         request_free (req);
+         return HOSTERR;
+       }
       else if (sock < 0)
-       return (retryable_socket_connect_error (errno)
-               ? CONERROR : CONIMPOSSIBLE);
+       {
+         request_free (req);
+         return (retryable_socket_connect_error (errno)
+                 ? CONERROR : CONIMPOSSIBLE);
+       }
 
 #ifdef HAVE_SSL
       if (proxy && u->scheme == SCHEME_HTTPS)
@@ -1397,6 +1429,7 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
          resp = resp_new (head);
          statcode = resp_status (resp, &message);
          resp_free (resp);
+         xfree (head);
          if (statcode != 200)
            {
            failed_tunnel:
@@ -1574,6 +1607,7 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
     hs->error = xstrdup (_("(no description)"));
   else
     hs->error = xstrdup (message);
+  xfree (message);
 
   type = resp_header_strdup (resp, "Content-Type");
   if (type)
@@ -1615,6 +1649,7 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
        contrange = first_byte_pos;
     }
   resp_free (resp);
+  xfree (head);
 
   /* 20x responses are counted among successful by default.  */
   if (H_20X (statcode))
@@ -1713,9 +1748,20 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
          logputs (LOG_VERBOSE, _("Length: "));
          if (contlen != -1)
            {
-             logputs (LOG_VERBOSE, legible (contlen + contrange));
+             logputs (LOG_VERBOSE, with_thousand_seps (contlen + contrange));
+             if (contlen + contrange >= 1024)
+               logprintf (LOG_VERBOSE, " (%s)",
+                          human_readable (contlen + contrange));
              if (contrange)
-               logprintf (LOG_VERBOSE, _(" (%s to go)"), legible (contlen));
+               {
+                 if (contlen >= 1024)
+                   logprintf (LOG_VERBOSE, _(", %s (%s) remaining"),
+                              with_thousand_seps (contlen),
+                              human_readable (contlen));
+                 else
+                   logprintf (LOG_VERBOSE, _(", %s remaining"),
+                              with_thousand_seps (contlen));
+               }
            }
          else
            logputs (LOG_VERBOSE,
@@ -1733,7 +1779,7 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
   if (!(*dt & RETROKF) || (*dt & HEAD_ONLY))
     {
       /* In case the caller cares to look...  */
-      hs->len = 0L;
+      hs->len = 0;
       hs->res = 0;
       xfree_null (type);
       /* Pre-1.10 Wget used CLOSE_INVALIDATE here.  Now we trust the
@@ -2536,47 +2582,6 @@ http_atotm (const char *time_string)
    consisting of answering to the server's challenge with the proper
    MD5 digests.  */
 
-/* How many bytes it will take to store LEN bytes in base64.  */
-#define BASE64_LENGTH(len) (4 * (((len) + 2) / 3))
-
-/* Encode the string S of length LENGTH to base64 format and place it
-   to STORE.  STORE will be 0-terminated, and must point to a writable
-   buffer of at least 1+BASE64_LENGTH(length) bytes.  */
-static void
-base64_encode (const char *s, char *store, int length)
-{
-  /* Conversion table.  */
-  static char tbl[64] = {
-    'A','B','C','D','E','F','G','H',
-    'I','J','K','L','M','N','O','P',
-    'Q','R','S','T','U','V','W','X',
-    'Y','Z','a','b','c','d','e','f',
-    'g','h','i','j','k','l','m','n',
-    'o','p','q','r','s','t','u','v',
-    'w','x','y','z','0','1','2','3',
-    '4','5','6','7','8','9','+','/'
-  };
-  int i;
-  unsigned char *p = (unsigned char *)store;
-
-  /* Transform the 3x8 bits to 4x6 bits, as required by base64.  */
-  for (i = 0; i < length; i += 3)
-    {
-      *p++ = tbl[s[0] >> 2];
-      *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)];
-      *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)];
-      *p++ = tbl[s[2] & 0x3f];
-      s += 3;
-    }
-  /* Pad the result if necessary...  */
-  if (i == length + 1)
-    *(p - 1) = '=';
-  else if (i == length + 2)
-    *(p - 1) = *(p - 2) = '=';
-  /* ...and zero-terminate it.  */
-  *p = '\0';
-}
-
 /* Create the authentication header contents for the `Basic' scheme.
    This is done by encoding the string `USER:PASS' in base64 and
    prepending `HEADER: Basic ' to it.  */
@@ -2601,7 +2606,7 @@ basic_authentication_encode (const char *user, const char *passwd)
     ++(x);                                     \
 } while (0)
 
-#ifdef USE_DIGEST
+#ifdef ENABLE_DIGEST
 /* Parse HTTP `WWW-Authenticate:' header.  AU points to the beginning
    of a field in such a header.  If the field is the one specified by
    ATTR_NAME ("realm", "opaque", and "nonce" are used by the current
@@ -2787,7 +2792,7 @@ username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
   }
   return res;
 }
-#endif /* USE_DIGEST */
+#endif /* ENABLE_DIGEST */
 
 
 #define BEGINS_WITH(line, string_constant)                             \
@@ -2799,8 +2804,13 @@ static int
 known_authentication_scheme_p (const char *au)
 {
   return BEGINS_WITH (au, "Basic")
+#ifdef ENABLE_DIGEST
     || BEGINS_WITH (au, "Digest")
-    || BEGINS_WITH (au, "NTLM");
+#endif
+#ifdef ENABLE_NTLM
+    || BEGINS_WITH (au, "NTLM")
+#endif
+    ;
 }
 
 #undef BEGINS_WITH
@@ -2817,14 +2827,34 @@ create_authorization_line (const char *au, const char *user,
 {
   if (0 == strncasecmp (au, "Basic", 5))
     return basic_authentication_encode (user, passwd);
-#ifdef USE_DIGEST
+#ifdef ENABLE_DIGEST
   if (0 == strncasecmp (au, "Digest", 6))
     return digest_authentication_encode (au, user, passwd, method, path);
-#endif /* USE_DIGEST */
+#endif
+#ifdef ENABLE_NTLM
+  if (0 == strncasecmp (au, "NTLM", 4))
+    {
+      int ok = ntlm_input (&pconn.ntlm, au);
+      if (!ok)
+       return NULL;
+      /* #### we shouldn't ignore the OK that ntlm_output returns. */
+      return ntlm_output (&pconn.ntlm, user, passwd, &ok);
+    }
+#endif
   return NULL;
 }
 \f
+void
+save_cookies (void)
+{
+  if (wget_cookie_jar)
+    cookie_jar_save (wget_cookie_jar, opt.cookies_output);
+}
+
 void
 http_cleanup (void)
 {
+  xfree_null (pconn.host);
+  if (wget_cookie_jar)
+    cookie_jar_delete (wget_cookie_jar);
 }