]> sjero.net Git - wget/blobdiff - src/http.c
[svn] Remove K&R support.
[wget] / src / http.c
index d549012cb1e3c33d5471bd9fb58baeed348717f4..7b3f379dec0d323162a296c26741488c1081ebda 100644 (file)
@@ -31,30 +31,13 @@ so, delete this exception statement from your version.  */
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <sys/types.h>
-#ifdef HAVE_STRING_H
-# include <string.h>
-#else
-# include <strings.h>
-#endif
+#include <string.h>
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif
 #include <assert.h>
 #include <errno.h>
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
-#ifndef errno
-extern int errno;
-#endif
+#include <time.h>
 
 #include "wget.h"
 #include "utils.h"
@@ -368,7 +351,7 @@ request_send (const struct request *req, int fd)
 
   /* Send the request to the server. */
 
-  write_error = fd_write (fd, request_string, size - 1, -1.0);
+  write_error = fd_write (fd, request_string, size - 1, -1);
   if (write_error < 0)
     logprintf (LOG_VERBOSE, _("Failed writing HTTP request: %s.\n"),
               strerror (errno));
@@ -412,7 +395,7 @@ post_file (int sock, const char *file_name, wgint promised_size)
       if (length == 0)
        break;
       towrite = MIN (promised_size - written, length);
-      write_error = fd_write (sock, chunk, towrite, -1.0);
+      write_error = fd_write (sock, chunk, towrite, -1);
       if (write_error < 0)
        {
          fclose (fp);
@@ -840,7 +823,7 @@ skip_short_body (int fd, wgint contlen)
 
   while (contlen > 0)
     {
-      int ret = fd_read (fd, dlbuf, MIN (contlen, SKIP_SIZE), -1.0);
+      int ret = fd_read (fd, dlbuf, MIN (contlen, SKIP_SIZE), -1);
       if (ret <= 0)
        {
          /* Don't normally report the error since this is an
@@ -1100,13 +1083,13 @@ free_hstat (struct http_stat *hs)
   hs->error = NULL;
 }
 
-static char *create_authorization_line PARAMS ((const char *, const char *,
-                                               const char *, const char *,
-                                               const char *, int *));
-static char *basic_authentication_encode PARAMS ((const char *, const char *));
-static int known_authentication_scheme_p PARAMS ((const char *, const char *));
+static char *create_authorization_line (const char *, const char *,
+                                       const char *, const char *,
+                                       const char *, int *);
+static char *basic_authentication_encode (const char *, const char *);
+static int known_authentication_scheme_p (const char *, const char *);
 
-time_t http_atotm PARAMS ((const char *));
+time_t http_atotm (const char *);
 
 #define BEGINS_WITH(line, string_constant)                             \
   (!strncasecmp (line, string_constant, sizeof (string_constant) - 1)  \
@@ -1121,6 +1104,12 @@ time_t http_atotm PARAMS ((const char *));
     request_set_header (req, "User-Agent", opt.useragent, rel_none);   \
 } while (0)
 
+/* The flags that allow clobbering the file (opening with "wb").
+   Defined here to avoid repetition later.  #### This will require
+   rework.  */
+#define ALLOW_CLOBBER (opt.noclobber || opt.always_rest || opt.timestamping \
+                      || opt.dirstruct || opt.output_document)
+
 /* Retrieve a document through HTTP protocol.  It recognizes status
    code, and correctly handles redirections.  It closes the network
    socket.  If it receives an error from the functions below it, it
@@ -1158,6 +1147,10 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
   /* 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 +1190,7 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
     }
 #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 +1211,7 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
   {
     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";
@@ -1361,8 +1354,8 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
          post_data_size = file_size (opt.post_file_name);
          if (post_data_size == -1)
            {
-             logprintf (LOG_NOTQUIET, "POST data file missing: %s\n",
-                        opt.post_file_name);
+             logprintf (LOG_NOTQUIET, _("POST data file missing: %s (%s)\n"),
+                        opt.post_file_name, strerror (errno));
              post_data_size = 0;
            }
        }
@@ -1534,7 +1527,7 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
       if (opt.post_data)
        {
          DEBUGP (("[POST data: %s]\n", opt.post_data));
-         write_error = fd_write (sock, opt.post_data, post_data_size, -1.0);
+         write_error = fd_write (sock, opt.post_data, post_data_size, -1);
        }
       else if (opt.post_file_name && post_data_size != 0)
        write_error = post_file (sock, opt.post_file_name, post_data_size);
@@ -1625,7 +1618,7 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
   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);
@@ -1743,7 +1736,6 @@ 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))
@@ -1765,7 +1757,7 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
                     _("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);
@@ -1789,18 +1781,28 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
        text/html file.  If some case-insensitive variation on ".htm[l]" isn't
        already the file's suffix, tack on ".html". */
     {
-      char*  last_period_in_local_filename = strrchr(*hs->local_file, '.');
+      char *last_period_in_local_filename = strrchr (*hs->local_file, '.');
 
       if (last_period_in_local_filename == NULL
          || !(0 == strcasecmp (last_period_in_local_filename, ".htm")
               || 0 == strcasecmp (last_period_in_local_filename, ".html")))
        {
-         size_t  local_filename_len = strlen(*hs->local_file);
-         
-         *hs->local_file = xrealloc(*hs->local_file,
-                                    local_filename_len + sizeof(".html"));
+         int local_filename_len = strlen (*hs->local_file);
+         /* Resize the local file, allowing for ".html" preceded by
+            optional ".NUMBER".  */
+         *hs->local_file = xrealloc (*hs->local_file,
+                                     local_filename_len + 24 + sizeof (".html"));
          strcpy(*hs->local_file + local_filename_len, ".html");
-
+         /* If clobbering is not allowed and the file, as named,
+            exists, tack on ".NUMBER.html" instead. */
+         if (!ALLOW_CLOBBER)
+           {
+             int ext_num = 1;
+             do
+               sprintf (*hs->local_file + local_filename_len,
+                        ".%d.html", ext_num++);
+             while (file_exists_p (*hs->local_file));
+           }
          *dt |= ADDED_HTML_EXTENSION;
        }
     }
@@ -1871,7 +1873,7 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
   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;
@@ -1893,8 +1895,7 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
        rotate_backups (*hs->local_file);
       if (hs->restval)
        fp = fopen (*hs->local_file, "ab");
-      else if (opt.noclobber || opt.always_rest || opt.timestamping || opt.dirstruct
-              || opt.output_document)
+      else if (ALLOW_CLOBBER)
        fp = fopen (*hs->local_file, "wb");
       else
        {
@@ -1927,6 +1928,9 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
   if (opt.save_headers)
     fwrite (head, 1, strlen (head), fp);
 
+  /* Now we no longer need to store the response header. */
+  xfree (head);
+
   /* Download the request body.  */
   flags = 0;
   if (keep_alive)
@@ -2036,7 +2040,7 @@ http_loop (struct url *u, char **newloc, char **local_file, const char *referer,
       /* If opt.noclobber is turned on and file already exists, do not
         retrieve the file */
       logprintf (LOG_VERBOSE, _("\
-File `%s' already there, will not retrieve.\n"), *hstat.local_file);
+File `%s' already there; not retrieving.\n\n"), *hstat.local_file);
       /* If the file is there, we suppose it's retrieved OK.  */
       *dt |= RETROKF;
 
@@ -2436,7 +2440,7 @@ The sizes do not match (local %s) -- retrieving.\n"),
          else if (!opt.kill_longer) /* meaning we got more than expected */
            {
              logprintf (LOG_VERBOSE,
-                        _("%s (%s) - `%s' saved [%s/%s])\n\n"),
+                        _("%s (%s) - `%s' saved [%s/%s]\n\n"),
                         tms, tmrate, locf,
                         number_to_static_string (hstat.len),
                         number_to_static_string (hstat.contlen));
@@ -2596,8 +2600,9 @@ check_end (const char *p)
 /* Convert the textual specification of time in TIME_STRING to the
    number of seconds since the Epoch.
 
-   TIME_STRING can be in any of the three formats RFC2068 allows the
-   HTTP servers to emit -- RFC1123-date, RFC850-date or asctime-date.
+   TIME_STRING can be in any of the three formats RFC2616 allows the
+   HTTP servers to emit -- RFC1123-date, RFC850-date or asctime-date,
+   as well as the time format used in the Set-Cookie header.
    Timezones are ignored, and should be GMT.
 
    Return the computed time_t representation, or -1 if the conversion
@@ -2631,10 +2636,10 @@ http_atotm (const char *time_string)
   static const char *time_formats[] = {
     "%a, %d %b %Y %T",         /* rfc1123: Thu, 29 Jan 1998 22:12:57 */
     "%A, %d-%b-%y %T",         /* rfc850:  Thursday, 29-Jan-98 22:12:57 */
-    "%a, %d-%b-%Y %T",         /* rfc850+: Thu, 29-Jan-1998 22:12:57
-                                  (post-y2k-rfc850; apparently google
-                                  uses this for their cookies.) */
-    "%a %b %d %T %Y"           /* asctime: Thu Jan 29 22:12:57 1998 */
+    "%a %b %d %T %Y",          /* asctime: Thu Jan 29 22:12:57 1998 */
+    "%a, %d-%b-%Y %T"          /* cookies: Thu, 29-Jan-1998 22:12:57
+                                  (used in Set-Cookie, defined in the
+                                  Netscape cookie specification.) */
   };
   int i;