]> sjero.net Git - wget/blobdiff - src/http.c
[svn] Applied contributed patches (see ChangeLog for details.)
[wget] / src / http.c
index 9ea0670f4988a5b756e7911cbf0ffcf8b85be956..3403648a777d1aa0c1cc0e98b057b6b587550c89 100644 (file)
@@ -303,12 +303,14 @@ static time_t http_atotm PARAMS ((char *));
 static uerr_t
 gethttp (struct urlinfo *u, struct http_stat *hs, int *dt)
 {
-  char *request, *type, *command, *path;
+  char *request, *type, *command, *path, *qstring;
   char *user, *passwd;
   char *pragma_h, *referer, *useragent, *range, *wwwauth, *remhost;
   char *authenticate_h;
   char *proxyauth;
   char *all_headers;
+  char *host_port;
+  int host_port_len;
   int sock, hcount, num_written, all_length, remport, statcode;
   long contlen, contrange;
   struct urlinfo *ou;
@@ -382,6 +384,9 @@ gethttp (struct urlinfo *u, struct http_stat *hs, int *dt)
     path = u->proxy->url;
   else
     path = u->path;
+  
+  qstring = u->qstring;
+
   command = (*dt & HEAD_ONLY) ? "HEAD" : "GET";
   referer = NULL;
   if (ou->referer)
@@ -454,10 +459,20 @@ gethttp (struct urlinfo *u, struct http_stat *hs, int *dt)
     }
   remhost = ou->host;
   remport = ou->port;
+
+  if (remport == 80) {
+      host_port = NULL; host_port_len = 0;
+  }
+  else {
+      host_port = (char *)alloca (numdigit (remport) + 2);
+      host_port_len = sprintf (host_port, ":%d", remport);
+  }
+
   /* Allocate the memory for the request.  */
   request = (char *)alloca (strlen (command) + strlen (path)
+                           + (qstring ? strlen (qstring) : 0)
                            + strlen (useragent)
-                           + strlen (remhost) + numdigit (remport)
+                           + strlen (remhost) + host_port_len
                            + strlen (HTTP_ACCEPT)
                            + (referer ? strlen (referer) : 0)
                            + (wwwauth ? strlen (wwwauth) : 0)
@@ -468,18 +483,19 @@ gethttp (struct urlinfo *u, struct http_stat *hs, int *dt)
                            + 64);
   /* Construct the request.  */
   sprintf (request, "\
-%s %s HTTP/1.0\r\n\
+%s %s%s HTTP/1.0\r\n\
 User-Agent: %s\r\n\
-Host: %s:%d\r\n\
+Host: %s%s\r\n\
 Accept: %s\r\n\
 %s%s%s%s%s%s\r\n",
-         command, path, useragent, remhost, remport, HTTP_ACCEPT, 
-         referer ? referer : "", 
-         wwwauth ? wwwauth : "", 
-         proxyauth ? proxyauth : "", 
-         range ? range : "",
-         pragma_h, 
-         opt.user_header ? opt.user_header : "");
+          command, path, qstring ? qstring : "", useragent, remhost,
+          host_port ? host_port : "",
+          HTTP_ACCEPT, referer ? referer : "",
+          wwwauth ? wwwauth : "", 
+          proxyauth ? proxyauth : "", 
+          range ? range : "",
+          pragma_h, 
+          opt.user_header ? opt.user_header : "");
   DEBUGP (("---request begin---\n%s---request end---\n", request));
    /* Free the temporary memory.  */
   FREE_MAYBE (wwwauth);
@@ -840,6 +856,7 @@ http_loop (struct urlinfo *u, char **newloc, int *dt)
   static int first_retrieval = 1;
 
   int count;
+  int local_dot_orig_file_exists = FALSE;
   int use_ts, got_head = 0;    /* time-stamping info */
   char *tms, *suf, *locf, *tmrate;
   uerr_t err;
@@ -851,7 +868,7 @@ http_loop (struct urlinfo *u, char **newloc, int *dt)
   *newloc = NULL;
 
   /* Warn on (likely bogus) wildcard usage in HTTP.  Don't use
-     has_wildcards_p because it would also warn on `?', and we that
+     has_wildcards_p because it would also warn on `?', and we know that
      shows up in CGI paths a *lot*.  */
   if (strchr (u->url, '*'))
     logputs (LOG_VERBOSE, _("Warning: wildcards not supported in HTTP.\n"));
@@ -888,7 +905,43 @@ File `%s' already there, will not retrieve.\n"), u->local);
   use_ts = 0;
   if (opt.timestamping)
     {
-      if (stat (u->local, &st) == 0)
+      boolean  local_file_exists = FALSE;
+
+      if (opt.backup_converted)
+       /* If -K is specified, we'll act on the assumption that it was specified
+          last time these files were downloaded as well, and instead of just
+          comparing local file X against server file X, we'll compare local
+          file X.orig (if extant, else X) against server file X.  If -K
+          _wasn't_ specified last time, or the server contains files called
+          *.orig, -N will be back to not operating correctly with -k. */
+       {
+         size_t filename_len = strlen(u->local);
+         char*  filename_plus_orig_suffix = malloc(filename_len +
+                                                   sizeof(".orig"));
+
+         /* Would a single s[n]printf() call be faster? */
+         strcpy(filename_plus_orig_suffix, u->local);
+         strcpy(filename_plus_orig_suffix + filename_len, ".orig");
+
+         /* Try to stat() the .orig file. */
+         if (stat(filename_plus_orig_suffix, &st) == 0)
+           {
+             local_file_exists = TRUE;
+             local_dot_orig_file_exists = TRUE;
+           }
+
+         free(filename_plus_orig_suffix);
+       }      
+
+      if (!local_dot_orig_file_exists)
+       /* Couldn't stat() <file>.orig, so try to stat() <file>. */
+       if (stat (u->local, &st) == 0)
+         local_file_exists = TRUE;
+
+      if (local_file_exists)
+       /* There was a local file, so we'll check later to see if the version
+          the server has is the same version we already have, allowing us to
+          skip a download. */
        {
          use_ts = 1;
          tml = st.st_mtime;
@@ -905,9 +958,16 @@ File `%s' already there, will not retrieve.\n"), u->local);
       /* Increment the pass counter.  */
       ++count;
       /* Wait before the retrieval (unless this is the very first
-        retrieval).  */
-      if (!first_retrieval && opt.wait)
-       sleep (opt.wait);
+        retrieval).
+        Check if we are retrying or not, wait accordingly - HEH */
+      if (!first_retrieval && (opt.wait || (count && opt.waitretry)))
+       if (count)
+         if (count<opt.waitretry)
+           sleep(count);
+         else
+           sleep(opt.waitretry);
+       else
+         sleep (opt.wait);
       if (first_retrieval)
        first_retrieval = 0;
       /* Get the current time string.  */
@@ -1051,14 +1111,26 @@ Last-modified header invalid -- time-stamp ignored.\n"));
              if (tml >= tmr &&
                  (hstat.contlen == -1 || local_size == hstat.contlen))
                {
-                 logprintf (LOG_VERBOSE, _("\
-Local file `%s' is more recent, not retrieving.\n\n"), u->local);
+                 if (local_dot_orig_file_exists)
+                   /* We can't collapse this down into just one logprintf()
+                      call with a variable set to u->local or the .orig
+                      filename because we have to malloc() space for the
+                      latter, and because there are multiple returns above (a
+                      coding style no-no by many measures, for reasons such as
+                      this) we'd have to remember to free() the string at each
+                      one to avoid a memory leak. */
+                   logprintf (LOG_VERBOSE, _("\
+Server file no newer than local file `%s.orig' -- not retrieving.\n\n"),
+                              u->local);
+                 else
+                   logprintf (LOG_VERBOSE, _("\
+Server file no newer than local file `%s' -- not retrieving.\n\n"), u->local);
                  FREEHSTAT (hstat);
                  return RETROK;
                }
              else if (tml >= tmr)
                logprintf (LOG_VERBOSE, _("\
-The sizes do not match (local %ld), retrieving.\n"), local_size);
+The sizes do not match (local %ld) -- retrieving.\n"), local_size);
              else
                logputs (LOG_VERBOSE,
                         _("Remote file is newer, retrieving.\n"));
@@ -1103,12 +1175,13 @@ The sizes do not match (local %ld), retrieving.\n"), local_size);
            }
          ++opt.numurls;
          opt.downloaded += hstat.len;
+         downloaded_file(ADD_FILE, locf);
          return RETROK;
        }
       else if (hstat.res == 0) /* No read error */
        {
-         if (hstat.contlen == -1)  /* We don't know how much we were
-                                      supposed to get, so...  */
+         if (hstat.contlen == -1)  /* We don't know how much we were supposed
+                                      to get, so assume we succeeded. */ 
            {
              if (*dt & RETROKF)
                {
@@ -1121,6 +1194,7 @@ The sizes do not match (local %ld), retrieving.\n"), local_size);
                }
              ++opt.numurls;
              opt.downloaded += hstat.len;
+             downloaded_file(ADD_FILE, locf);
              return RETROK;
            }
          else if (hstat.len < hstat.contlen) /* meaning we lost the
@@ -1142,6 +1216,7 @@ The sizes do not match (local %ld), retrieving.\n"), local_size);
                         tms, u->url, hstat.len, hstat.contlen, locf, count);
              ++opt.numurls;
              opt.downloaded += hstat.len;
+             downloaded_file(ADD_FILE, locf);
              return RETROK;
            }
          else                  /* the same, but not accepted */