2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
4 This file is part of Wget.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include <sys/types.h>
36 #if TIME_WITH_SYS_TIME
37 # include <sys/time.h>
41 # include <sys/time.h>
65 extern char *version_string;
75 #define TEXTHTML_S "text/html"
76 #define HTTP_ACCEPT "*/*"
78 /* Some status code validation macros: */
79 #define H_20X(x) (((x) >= 200) && ((x) < 300))
80 #define H_PARTIAL(x) ((x) == HTTP_STATUS_PARTIAL_CONTENTS)
81 #define H_REDIRECTED(x) (((x) == HTTP_STATUS_MOVED_PERMANENTLY) \
82 || ((x) == HTTP_STATUS_MOVED_TEMPORARILY))
84 /* HTTP/1.0 status codes from RFC1945, provided for reference. */
86 #define HTTP_STATUS_OK 200
87 #define HTTP_STATUS_CREATED 201
88 #define HTTP_STATUS_ACCEPTED 202
89 #define HTTP_STATUS_NO_CONTENT 204
90 #define HTTP_STATUS_PARTIAL_CONTENTS 206
92 /* Redirection 3xx. */
93 #define HTTP_STATUS_MULTIPLE_CHOICES 300
94 #define HTTP_STATUS_MOVED_PERMANENTLY 301
95 #define HTTP_STATUS_MOVED_TEMPORARILY 302
96 #define HTTP_STATUS_NOT_MODIFIED 304
98 /* Client error 4xx. */
99 #define HTTP_STATUS_BAD_REQUEST 400
100 #define HTTP_STATUS_UNAUTHORIZED 401
101 #define HTTP_STATUS_FORBIDDEN 403
102 #define HTTP_STATUS_NOT_FOUND 404
104 /* Server errors 5xx. */
105 #define HTTP_STATUS_INTERNAL 500
106 #define HTTP_STATUS_NOT_IMPLEMENTED 501
107 #define HTTP_STATUS_BAD_GATEWAY 502
108 #define HTTP_STATUS_UNAVAILABLE 503
111 /* Parse the HTTP status line, which is of format:
113 HTTP-Version SP Status-Code SP Reason-Phrase
115 The function returns the status-code, or -1 if the status line is
116 malformed. The pointer to reason-phrase is returned in RP. */
118 parse_http_status_line (const char *line, const char **reason_phrase_ptr)
120 /* (the variables must not be named `major' and `minor', because
121 that breaks compilation with SunOS4 cc.) */
122 int mjr, mnr, statcode;
125 *reason_phrase_ptr = NULL;
127 /* The standard format of HTTP-Version is: `HTTP/X.Y', where X is
128 major version, and Y is minor version. */
129 if (strncmp (line, "HTTP/", 5) != 0)
133 /* Calculate major HTTP version. */
135 for (mjr = 0; ISDIGIT (*line); line++)
136 mjr = 10 * mjr + (*line - '0');
137 if (*line != '.' || p == line)
141 /* Calculate minor HTTP version. */
143 for (mnr = 0; ISDIGIT (*line); line++)
144 mnr = 10 * mnr + (*line - '0');
145 if (*line != ' ' || p == line)
147 /* Wget will accept only 1.0 and higher HTTP-versions. The value of
148 minor version can be safely ignored. */
153 /* Calculate status code. */
154 if (!(ISDIGIT (*line) && ISDIGIT (line[1]) && ISDIGIT (line[2])))
156 statcode = 100 * (*line - '0') + 10 * (line[1] - '0') + (line[2] - '0');
158 /* Set up the reason phrase pointer. */
160 /* RFC2068 requires SPC here, but we allow the string to finish
161 here, in case no reason-phrase is present. */
165 *reason_phrase_ptr = line;
170 *reason_phrase_ptr = line + 1;
175 /* Functions to be used as arguments to header_process(): */
177 struct http_process_range_closure {
183 /* Parse the `Content-Range' header and extract the information it
184 contains. Returns 1 if successful, -1 otherwise. */
186 http_process_range (const char *hdr, void *arg)
188 struct http_process_range_closure *closure
189 = (struct http_process_range_closure *)arg;
192 /* Certain versions of Nutscape proxy server send out
193 `Content-Length' without "bytes" specifier, which is a breach of
194 RFC2068 (as well as the HTTP/1.1 draft which was current at the
195 time). But hell, I must support it... */
196 if (!strncasecmp (hdr, "bytes", 5))
199 hdr += skip_lws (hdr);
205 for (num = 0; ISDIGIT (*hdr); hdr++)
206 num = 10 * num + (*hdr - '0');
207 if (*hdr != '-' || !ISDIGIT (*(hdr + 1)))
209 closure->first_byte_pos = num;
211 for (num = 0; ISDIGIT (*hdr); hdr++)
212 num = 10 * num + (*hdr - '0');
213 if (*hdr != '/' || !ISDIGIT (*(hdr + 1)))
215 closure->last_byte_pos = num;
217 for (num = 0; ISDIGIT (*hdr); hdr++)
218 num = 10 * num + (*hdr - '0');
219 closure->entity_length = num;
223 /* Place 1 to ARG if the HDR contains the word "none", 0 otherwise.
224 Used for `Accept-Ranges'. */
226 http_process_none (const char *hdr, void *arg)
228 int *where = (int *)arg;
230 if (strstr (hdr, "none"))
237 /* Place the malloc-ed copy of HDR hdr, to the first `;' to ARG. */
239 http_process_type (const char *hdr, void *arg)
241 char **result = (char **)arg;
244 *result = xstrdup (hdr);
245 p = strrchr (hdr, ';');
249 *result = (char *)xmalloc (len + 1);
250 memcpy (*result, hdr, len);
251 (*result)[len] = '\0';
254 *result = xstrdup (hdr);
261 long len; /* received length */
262 long contlen; /* expected length */
263 long restval; /* the restart value */
264 int res; /* the result of last read */
265 char *newloc; /* new location (redirection) */
266 char *remote_time; /* remote time-stamp string */
267 char *error; /* textual HTTP error */
268 int statcode; /* status code */
269 long dltime; /* time of the download */
272 /* Free the elements of hstat X. */
273 #define FREEHSTAT(x) do \
275 FREE_MAYBE ((x).newloc); \
276 FREE_MAYBE ((x).remote_time); \
277 FREE_MAYBE ((x).error); \
278 (x).newloc = (x).remote_time = (x).error = NULL; \
281 static char *create_authorization_line PARAMS ((const char *, const char *,
282 const char *, const char *,
284 static char *basic_authentication_encode PARAMS ((const char *, const char *,
286 static int known_authentication_scheme_p PARAMS ((const char *));
288 static time_t http_atotm PARAMS ((char *));
290 /* Retrieve a document through HTTP protocol. It recognizes status
291 code, and correctly handles redirections. It closes the network
292 socket. If it receives an error from the functions below it, it
293 will print it if there is enough information to do so (almost
294 always), returning the error to the caller (i.e. http_loop).
296 Various HTTP parameters are stored to hs. Although it parses the
297 response code correctly, it is not used in a sane way. The caller
300 If u->proxy is non-NULL, the URL u will be taken as a proxy URL,
301 and u->proxy->url will be given to the proxy server (bad naming,
304 gethttp (struct urlinfo *u, struct http_stat *hs, int *dt)
306 char *request, *type, *command, *path;
308 char *pragma_h, *referer, *useragent, *range, *wwwauth, *remhost;
309 char *authenticate_h;
312 int sock, hcount, num_written, all_length, remport, statcode;
313 long contlen, contrange;
317 int auth_tried_already;
320 /* Let the others worry about local filename... */
321 if (!(*dt & HEAD_ONLY))
322 assert (u->local != NULL);
325 auth_tried_already = 0;
328 /* We need to come back here when the initial attempt to retrieve
329 without authorization header fails. */
331 /* Initialize certain elements of struct hstat. */
336 hs->remote_time = NULL;
339 /* Which structure to use to retrieve the original URL data. */
345 /* First: establish the connection. */
346 logprintf (LOG_VERBOSE, _("Connecting to %s:%hu... "), u->host, u->port);
347 err = make_connection (&sock, u->host, u->port);
351 logputs (LOG_VERBOSE, "\n");
352 logprintf (LOG_NOTQUIET, "%s: %s.\n", u->host, herrmsg (h_errno));
356 logputs (LOG_VERBOSE, "\n");
357 logprintf (LOG_NOTQUIET, "socket: %s\n", strerror (errno));
361 logputs (LOG_VERBOSE, "\n");
362 logprintf (LOG_NOTQUIET,
363 _("Connection to %s:%hu refused.\n"), u->host, u->port);
367 logputs (LOG_VERBOSE, "\n");
368 logprintf (LOG_NOTQUIET, "connect: %s\n", strerror (errno));
373 /* Everything is fine! */
374 logputs (LOG_VERBOSE, _("connected!\n"));
382 path = u->proxy->url;
385 command = (*dt & HEAD_ONLY) ? "HEAD" : "GET";
389 referer = (char *)alloca (9 + strlen (ou->referer) + 3);
390 sprintf (referer, "Referer: %s\r\n", ou->referer);
392 if (*dt & SEND_NOCACHE)
393 pragma_h = "Pragma: no-cache\r\n";
398 range = (char *)alloca (13 + numdigit (hs->restval) + 4);
399 /* #### Gag me! Some servers (e.g. WebSitePro) have been known
400 to misinterpret the following `Range' format, and return the
401 document as multipart/x-byte-ranges MIME type!
403 #### TODO: Interpret MIME types, recognize bullshits similar
404 the one described above, and deal with them! */
405 sprintf (range, "Range: bytes=%ld-\r\n", hs->restval);
410 STRDUP_ALLOCA (useragent, opt.useragent);
413 useragent = (char *)alloca (10 + strlen (version_string));
414 sprintf (useragent, "Wget/%s", version_string);
416 /* Construct the authentication, if userid is present. */
419 search_netrc (u->host, (const char **)&user, (const char **)&passwd, 0);
420 user = user ? user : opt.http_user;
421 passwd = passwd ? passwd : opt.http_passwd;
424 if (authenticate_h && user && passwd)
426 wwwauth = create_authorization_line (authenticate_h, user, passwd,
433 char *proxy_user, *proxy_passwd;
434 /* For normal username and password, URL components override
435 command-line/wgetrc parameters. With proxy authentication,
436 it's the reverse, because proxy URLs are normally the
437 "permanent" ones, so command-line args should take
439 if (opt.proxy_user && opt.proxy_passwd)
441 proxy_user = opt.proxy_user;
442 proxy_passwd = opt.proxy_passwd;
446 proxy_user = u->user;
447 proxy_passwd = u->passwd;
449 /* #### This is junky. Can't the proxy request, say, `Digest'
451 if (proxy_user && proxy_passwd)
452 proxyauth = basic_authentication_encode (proxy_user, proxy_passwd,
453 "Proxy-Authorization");
457 /* Allocate the memory for the request. */
458 request = (char *)alloca (strlen (command) + strlen (path)
460 + strlen (remhost) + numdigit (remport)
461 + strlen (HTTP_ACCEPT)
462 + (referer ? strlen (referer) : 0)
463 + (wwwauth ? strlen (wwwauth) : 0)
464 + (proxyauth ? strlen (proxyauth) : 0)
465 + (range ? strlen (range) : 0)
467 + (opt.user_header ? strlen (opt.user_header) : 0)
469 /* Construct the request. */
476 command, path, useragent, remhost, remport, HTTP_ACCEPT,
477 referer ? referer : "",
478 wwwauth ? wwwauth : "",
479 proxyauth ? proxyauth : "",
482 opt.user_header ? opt.user_header : "");
483 DEBUGP (("---request begin---\n%s---request end---\n", request));
484 /* Free the temporary memory. */
485 FREE_MAYBE (wwwauth);
486 FREE_MAYBE (proxyauth);
488 /* Send the request to server. */
489 num_written = iwrite (sock, request, strlen (request));
492 logputs (LOG_VERBOSE, _("Failed writing HTTP request.\n"));
497 logprintf (LOG_VERBOSE, _("%s request sent, awaiting response... "),
498 u->proxy ? "Proxy" : "HTTP");
499 contlen = contrange = -1;
504 /* Before reading anything, initialize the rbuf. */
505 rbuf_initialize (&rbuf, sock);
509 /* Header-fetching loop. */
517 /* Get the header. */
518 status = header_get (&rbuf, &hdr,
519 /* Disallow continuations for status line. */
520 (hcount == 1 ? HG_NO_CONTINUATIONS : HG_NONE));
522 /* Check for errors. */
523 if (status == HG_EOF && *hdr)
525 /* This used to be an unconditional error, but that was
526 somewhat controversial, because of a large number of
527 broken CGI's that happily "forget" to send the second EOL
528 before closing the connection of a HEAD request.
530 So, the deal is to check whether the header is empty
531 (*hdr is zero if it is); if yes, it means that the
532 previous header was fully retrieved, and that -- most
533 probably -- the request is complete. "...be liberal in
534 what you accept." Oh boy. */
535 logputs (LOG_VERBOSE, "\n");
536 logputs (LOG_NOTQUIET, _("End of file while parsing headers.\n"));
539 FREE_MAYBE (hs->newloc);
540 FREE_MAYBE (all_headers);
544 else if (status == HG_ERROR)
546 logputs (LOG_VERBOSE, "\n");
547 logprintf (LOG_NOTQUIET, _("Read error (%s) in headers.\n"),
551 FREE_MAYBE (hs->newloc);
552 FREE_MAYBE (all_headers);
557 /* If the headers are to be saved to a file later, save them to
559 if (opt.save_headers)
561 int lh = strlen (hdr);
562 all_headers = (char *)xrealloc (all_headers, all_length + lh + 2);
563 memcpy (all_headers + all_length, hdr, lh);
565 all_headers[all_length++] = '\n';
566 all_headers[all_length] = '\0';
569 /* Print the header if requested. */
570 if (opt.server_response && hcount != 1)
571 logprintf (LOG_VERBOSE, "\n%d %s", hcount, hdr);
573 /* Check for status line. */
577 /* Parse the first line of server response. */
578 statcode = parse_http_status_line (hdr, &error);
579 hs->statcode = statcode;
580 /* Store the descriptive response. */
581 if (statcode == -1) /* malformed response */
583 /* A common reason for "malformed response" error is the
584 case when no data was actually received. Handle this
587 hs->error = xstrdup (_("No data received"));
589 hs->error = xstrdup (_("Malformed status line"));
594 hs->error = xstrdup (_("(no description)"));
596 hs->error = xstrdup (error);
603 logprintf (LOG_VERBOSE, "%d %s", statcode, error);
608 /* Exit on empty header. */
615 /* Try getting content-length. */
616 if (contlen == -1 && !opt.ignore_length)
617 if (header_process (hdr, "Content-Length", header_extract_number,
620 /* Try getting content-type. */
622 if (header_process (hdr, "Content-Type", http_process_type, &type))
624 /* Try getting location. */
626 if (header_process (hdr, "Location", header_strdup, &hs->newloc))
628 /* Try getting last-modified. */
629 if (!hs->remote_time)
630 if (header_process (hdr, "Last-Modified", header_strdup,
633 /* Try getting www-authentication. */
635 if (header_process (hdr, "WWW-Authenticate", header_strdup,
638 /* Check for accept-ranges header. If it contains the word
639 `none', disable the ranges. */
640 if (*dt & ACCEPTRANGES)
643 if (header_process (hdr, "Accept-Ranges", http_process_none, &nonep))
646 *dt &= ~ACCEPTRANGES;
650 /* Try getting content-range. */
653 struct http_process_range_closure closure;
654 if (header_process (hdr, "Content-Range", http_process_range, &closure))
656 contrange = closure.first_byte_pos;
664 logputs (LOG_VERBOSE, "\n");
666 if ((statcode == HTTP_STATUS_UNAUTHORIZED)
669 /* Authorization is required. */
674 if (auth_tried_already)
676 /* If we have tried it already, then there is not point
678 logputs (LOG_NOTQUIET, _("Authorization failed.\n"));
679 free (authenticate_h);
682 else if (!known_authentication_scheme_p (authenticate_h))
684 free (authenticate_h);
685 logputs (LOG_NOTQUIET, _("Unknown authentication scheme.\n"));
690 auth_tried_already = 1;
694 /* We do not need this anymore. */
697 free (authenticate_h);
698 authenticate_h = NULL;
701 /* 20x responses are counted among successful by default. */
702 if (H_20X (statcode))
705 if (type && !strncasecmp (type, TEXTHTML_S, strlen (TEXTHTML_S)))
708 /* We don't assume text/html by default. */
713 else if (contrange != hs->restval ||
714 (H_PARTIAL (statcode) && contrange == -1))
716 /* This means the whole request was somehow misunderstood by the
719 FREE_MAYBE (hs->newloc);
720 FREE_MAYBE (all_headers);
728 contlen += contrange;
730 contrange = -1; /* If conent-length was not sent,
731 content-range will be ignored. */
733 hs->contlen = contlen;
735 /* Return if redirected. */
736 if (H_REDIRECTED (statcode) || statcode == HTTP_STATUS_MULTIPLE_CHOICES)
738 /* RFC2068 says that in case of the 300 (multiple choices)
739 response, the server can output a preferred URL through
740 `Location' header; otherwise, the request should be treated
741 like GET. So, if the location is set, it will be a
742 redirection; otherwise, just proceed normally. */
743 if (statcode == HTTP_STATUS_MULTIPLE_CHOICES && !hs->newloc)
747 logprintf (LOG_VERBOSE,
748 _("Location: %s%s\n"),
749 hs->newloc ? hs->newloc : _("unspecified"),
750 hs->newloc ? _(" [following]") : "");
753 FREE_MAYBE (all_headers);
759 if ((*dt & RETROKF) && !opt.server_response)
761 /* No need to print this output if the body won't be
762 downloaded at all, or if the original server response is
764 logputs (LOG_VERBOSE, _("Length: "));
767 logputs (LOG_VERBOSE, legible (contlen));
769 logprintf (LOG_VERBOSE, _(" (%s to go)"),
770 legible (contlen - contrange));
773 logputs (LOG_VERBOSE,
774 opt.ignore_length ? _("ignored") : _("unspecified"));
776 logprintf (LOG_VERBOSE, " [%s]\n", type);
778 logputs (LOG_VERBOSE, "\n");
782 type = NULL; /* We don't need it any more. */
784 /* Return if we have no intention of further downloading. */
785 if (!(*dt & RETROKF) || (*dt & HEAD_ONLY))
787 /* In case someone cares to look... */
791 FREE_MAYBE (all_headers);
796 /* Open the local file. */
799 mkalldirs (u->local);
801 rotate_backups (u->local);
802 fp = fopen (u->local, hs->restval ? "ab" : "wb");
805 logprintf (LOG_NOTQUIET, "%s: %s\n", u->local, strerror (errno));
807 FREE_MAYBE (all_headers);
814 /* #### This confuses the code that checks for file size. There
815 should be some overhead information. */
816 if (opt.save_headers)
817 fwrite (all_headers, 1, all_length, fp);
819 /* Get the contents of the document. */
820 hs->res = get_contents (sock, fp, &hs->len, hs->restval,
821 (contlen != -1 ? contlen : 0),
823 hs->dltime = elapsed_time ();
828 FREE_MAYBE (all_headers);
835 /* The genuine HTTP loop! This is the part where the retrieval is
836 retried, and retried, and retried, and... */
838 http_loop (struct urlinfo *u, char **newloc, int *dt)
840 static int first_retrieval = 1;
843 int use_ts, got_head = 0; /* time-stamping info */
844 char *tms, *suf, *locf, *tmrate;
846 time_t tml = -1, tmr = -1; /* local and remote time-stamps */
847 long local_size = 0; /* the size of the local file */
848 struct http_stat hstat; /* HTTP status */
853 /* Warn on (likely bogus) wildcard usage in HTTP. Don't use
854 has_wildcards_p because it would also warn on `?', and we that
855 shows up in CGI paths a *lot*. */
856 if (strchr (u->url, '*'))
857 logputs (LOG_VERBOSE, _("Warning: wildcards not supported in HTTP.\n"));
859 /* Determine the local filename. */
861 u->local = url_filename (u->proxy ? u->proxy : u);
863 if (!opt.output_document)
866 locf = opt.output_document;
868 if (opt.noclobber && file_exists_p (u->local))
870 /* If opt.noclobber is turned on and file already exists, do not
872 logprintf (LOG_VERBOSE, _("\
873 File `%s' already there, will not retrieve.\n"), u->local);
874 /* If the file is there, we suppose it's retrieved OK. */
877 /* #### Bogusness alert. */
878 /* If its suffix is "html" or (yuck!) "htm", we suppose it's
879 text/html, a harmless lie. */
880 if (((suf = suffix (u->local)) != NULL)
881 && (!strcmp (suf, "html") || !strcmp (suf, "htm")))
884 /* Another harmless lie: */
889 if (opt.timestamping)
891 if (stat (u->local, &st) == 0)
895 local_size = st.st_size;
899 /* Reset the counter. */
901 *dt = 0 | ACCEPTRANGES;
905 /* Increment the pass counter. */
907 /* Wait before the retrieval (unless this is the very first
909 if (!first_retrieval && opt.wait)
913 /* Get the current time string. */
914 tms = time_str (NULL);
915 /* Print fetch message, if opt.verbose. */
918 char *hurl = str_url (u->proxy ? u->proxy : u, 1);
922 sprintf (tmp, _("(try:%2d)"), count);
923 logprintf (LOG_VERBOSE, "--%s-- %s\n %s => `%s'\n",
924 tms, hurl, tmp, locf);
926 ws_changetitle (hurl, 1);
931 /* Default document type is empty. However, if spider mode is
932 on or time-stamping is employed, HEAD_ONLY commands is
933 encoded within *dt. */
934 if (opt.spider || (use_ts && !got_head))
938 /* Assume no restarting. */
940 /* Decide whether or not to restart. */
941 if (((count > 1 && (*dt & ACCEPTRANGES)) || opt.always_rest)
942 && file_exists_p (u->local))
943 if (stat (u->local, &st) == 0)
944 hstat.restval = st.st_size;
945 /* Decide whether to send the no-cache directive. */
946 if (u->proxy && (count > 1 || (opt.proxy_cache == 0)))
949 *dt &= ~SEND_NOCACHE;
951 /* Try fetching the document, or at least its head. :-) */
952 err = gethttp (u, &hstat, dt);
954 tms = time_str (NULL);
955 /* Get the new location (with or without the redirection). */
957 *newloc = xstrdup (hstat.newloc);
960 case HERR: case HEOF: case CONSOCKERR: case CONCLOSED:
961 case CONERROR: case READERR: case WRITEFAILED:
963 /* Non-fatal errors continue executing the loop, which will
964 bring them to "while" statement at the end, to judge
965 whether the number of tries was exceeded. */
967 printwhat (count, opt.ntry);
970 case HOSTERR: case CONREFUSED: case PROXERR: case AUTHFAILED:
971 /* Fatal errors just return from the function. */
975 case FWRITEERR: case FOPENERR:
976 /* Another fatal error. */
977 logputs (LOG_VERBOSE, "\n");
978 logprintf (LOG_NOTQUIET, _("Cannot write to `%s' (%s).\n"),
979 u->local, strerror (errno));
984 /* Return the new location to the caller. */
987 logprintf (LOG_NOTQUIET,
988 _("ERROR: Redirection (%d) without location.\n"),
996 /* Deal with you later. */
999 /* All possibilities should have been exhausted. */
1002 if (!(*dt & RETROKF))
1006 /* #### Ugly ugly ugly! */
1007 char *hurl = str_url (u->proxy ? u->proxy : u, 1);
1008 logprintf (LOG_NONVERBOSE, "%s:\n", hurl);
1011 logprintf (LOG_NOTQUIET, _("%s ERROR %d: %s.\n"),
1012 tms, hstat.statcode, hstat.error);
1013 logputs (LOG_VERBOSE, "\n");
1018 /* Did we get the time-stamp? */
1021 if (opt.timestamping && !hstat.remote_time)
1023 logputs (LOG_NOTQUIET, _("\
1024 Last-modified header missing -- time-stamps turned off.\n"));
1026 else if (hstat.remote_time)
1028 /* Convert the date-string into struct tm. */
1029 tmr = http_atotm (hstat.remote_time);
1030 if (tmr == (time_t) (-1))
1031 logputs (LOG_VERBOSE, _("\
1032 Last-modified header invalid -- time-stamp ignored.\n"));
1036 /* The time-stamping section. */
1041 use_ts = 0; /* no more time-stamping */
1042 count = 0; /* the retrieve count for HEAD is
1044 if (hstat.remote_time && tmr != (time_t) (-1))
1046 /* Now time-stamping can be used validly. Time-stamping
1047 means that if the sizes of the local and remote file
1048 match, and local file is newer than the remote file,
1049 it will not be retrieved. Otherwise, the normal
1050 download procedure is resumed. */
1052 (hstat.contlen == -1 || local_size == hstat.contlen))
1054 logprintf (LOG_VERBOSE, _("\
1055 Local file `%s' is more recent, not retrieving.\n\n"), u->local);
1059 else if (tml >= tmr)
1060 logprintf (LOG_VERBOSE, _("\
1061 The sizes do not match (local %ld), retrieving.\n"), local_size);
1063 logputs (LOG_VERBOSE,
1064 _("Remote file is newer, retrieving.\n"));
1070 && (tmr != (time_t) (-1))
1072 && ((hstat.len == hstat.contlen) ||
1073 ((hstat.res == 0) &&
1074 ((hstat.contlen == -1) ||
1075 (hstat.len >= hstat.contlen && !opt.kill_longer)))))
1077 touch (u->local, tmr);
1079 /* End of time-stamping section. */
1083 logprintf (LOG_NOTQUIET, "%d %s\n\n", hstat.statcode, hstat.error);
1087 /* It is now safe to free the remainder of hstat, since the
1088 strings within it will no longer be used. */
1091 tmrate = rate (hstat.len - hstat.restval, hstat.dltime);
1093 if (hstat.len == hstat.contlen)
1097 logprintf (LOG_VERBOSE,
1098 _("%s (%s) - `%s' saved [%ld/%ld]\n\n"),
1099 tms, tmrate, locf, hstat.len, hstat.contlen);
1100 logprintf (LOG_NONVERBOSE,
1101 "%s URL:%s [%ld/%ld] -> \"%s\" [%d]\n",
1102 tms, u->url, hstat.len, hstat.contlen, locf, count);
1105 opt.downloaded += hstat.len;
1108 else if (hstat.res == 0) /* No read error */
1110 if (hstat.contlen == -1) /* We don't know how much we were
1111 supposed to get, so... */
1115 logprintf (LOG_VERBOSE,
1116 _("%s (%s) - `%s' saved [%ld]\n\n"),
1117 tms, tmrate, locf, hstat.len);
1118 logprintf (LOG_NONVERBOSE,
1119 "%s URL:%s [%ld] -> \"%s\" [%d]\n",
1120 tms, u->url, hstat.len, locf, count);
1123 opt.downloaded += hstat.len;
1126 else if (hstat.len < hstat.contlen) /* meaning we lost the
1127 connection too soon */
1129 logprintf (LOG_VERBOSE,
1130 _("%s (%s) - Connection closed at byte %ld. "),
1131 tms, tmrate, hstat.len);
1132 printwhat (count, opt.ntry);
1135 else if (!opt.kill_longer) /* meaning we got more than expected */
1137 logprintf (LOG_VERBOSE,
1138 _("%s (%s) - `%s' saved [%ld/%ld])\n\n"),
1139 tms, tmrate, locf, hstat.len, hstat.contlen);
1140 logprintf (LOG_NONVERBOSE,
1141 "%s URL:%s [%ld/%ld] -> \"%s\" [%d]\n",
1142 tms, u->url, hstat.len, hstat.contlen, locf, count);
1144 opt.downloaded += hstat.len;
1147 else /* the same, but not accepted */
1149 logprintf (LOG_VERBOSE,
1150 _("%s (%s) - Connection closed at byte %ld/%ld. "),
1151 tms, tmrate, hstat.len, hstat.contlen);
1152 printwhat (count, opt.ntry);
1156 else /* now hstat.res can only be -1 */
1158 if (hstat.contlen == -1)
1160 logprintf (LOG_VERBOSE,
1161 _("%s (%s) - Read error at byte %ld (%s)."),
1162 tms, tmrate, hstat.len, strerror (errno));
1163 printwhat (count, opt.ntry);
1166 else /* hstat.res == -1 and contlen is given */
1168 logprintf (LOG_VERBOSE,
1169 _("%s (%s) - Read error at byte %ld/%ld (%s). "),
1170 tms, tmrate, hstat.len, hstat.contlen,
1172 printwhat (count, opt.ntry);
1179 while (!opt.ntry || (count < opt.ntry));
1183 /* Converts struct tm to time_t, assuming the data in tm is UTC rather
1184 than local timezone (mktime assumes the latter).
1186 Contributed by Roger Beeman <beeman@cisco.com>, with the help of
1187 Mark Baushke <mdb@cisco.com> and the rest of the Gurus at CISCO. */
1189 mktime_from_utc (struct tm *t)
1196 tb = mktime (gmtime (&tl));
1197 return (tl <= tb ? (tl + (tl - tb)) : (tl - (tb - tl)));
1200 /* Check whether the result of strptime() indicates success.
1201 strptime() returns the pointer to how far it got to in the string.
1202 The processing has been successful if the string is at `GMT' or
1203 `+X', or at the end of the string.
1205 In extended regexp parlance, the function returns 1 if P matches
1206 "^ *(GMT|[+-][0-9]|$)", 0 otherwise. P being NULL (a valid result of
1207 strptime()) is considered a failure and 0 is returned. */
1213 while (ISSPACE (*p))
1216 || (p[0] == 'G' && p[1] == 'M' && p[2] == 'T')
1217 || ((p[0] == '+' || p[1] == '-') && ISDIGIT (p[1])))
1223 /* Convert TIME_STRING time to time_t. TIME_STRING can be in any of
1224 the three formats RFC2068 allows the HTTP servers to emit --
1225 RFC1123-date, RFC850-date or asctime-date. Timezones are ignored,
1228 We use strptime() to recognize various dates, which makes it a
1229 little bit slacker than the RFC1123/RFC850/asctime (e.g. it always
1230 allows shortened dates and months, one-digit days, etc.). It also
1231 allows more than one space anywhere where the specs require one SP.
1232 The routine should probably be even more forgiving (as recommended
1233 by RFC2068), but I do not have the time to write one.
1235 Return the computed time_t representation, or -1 if all the
1238 Needless to say, what we *really* need here is something like
1239 Marcus Hennecke's atotm(), which is forgiving, fast, to-the-point,
1240 and does not use strptime(). atotm() is to be found in the sources
1241 of `phttpd', a little-known HTTP server written by Peter Erikson. */
1243 http_atotm (char *time_string)
1247 /* Roger Beeman says: "This function dynamically allocates struct tm
1248 t, but does no initialization. The only field that actually
1249 needs initialization is tm_isdst, since the others will be set by
1250 strptime. Since strptime does not set tm_isdst, it will return
1251 the data structure with whatever data was in tm_isdst to begin
1252 with. For those of us in timezones where DST can occur, there
1253 can be a one hour shift depending on the previous contents of the
1254 data area where the data structure is allocated." */
1257 /* Note that under foreign locales Solaris strptime() fails to
1258 recognize English dates, which renders this function useless. I
1259 assume that other non-GNU strptime's are plagued by the same
1260 disease. We solve this by setting only LC_MESSAGES in
1261 i18n_initialize(), instead of LC_ALL.
1263 Another solution could be to temporarily set locale to C, invoke
1264 strptime(), and restore it back. This is slow and dirty,
1265 however, and locale support other than LC_MESSAGES can mess other
1266 things, so I rather chose to stick with just setting LC_MESSAGES.
1268 Also note that none of this is necessary under GNU strptime(),
1269 because it recognizes both international and local dates. */
1271 /* NOTE: We don't use `%n' for white space, as OSF's strptime uses
1272 it to eat all white space up to (and including) a newline, and
1273 the function fails if there is no newline (!).
1275 Let's hope all strptime() implementations use ` ' to skip *all*
1276 whitespace instead of just one (it works that way on all the
1277 systems I've tested it on). */
1279 /* RFC1123: Thu, 29 Jan 1998 22:12:57 */
1280 if (check_end (strptime (time_string, "%a, %d %b %Y %T", &t)))
1281 return mktime_from_utc (&t);
1282 /* RFC850: Thu, 29-Jan-98 22:12:57 */
1283 if (check_end (strptime (time_string, "%a, %d-%b-%y %T", &t)))
1284 return mktime_from_utc (&t);
1285 /* asctime: Thu Jan 29 22:12:57 1998 */
1286 if (check_end (strptime (time_string, "%a %b %d %T %Y", &t)))
1287 return mktime_from_utc (&t);
1292 /* Authorization support: We support two authorization schemes:
1294 * `Basic' scheme, consisting of base64-ing USER:PASSWORD string;
1296 * `Digest' scheme, added by Junio Hamano <junio@twinsun.com>,
1297 consisting of answering to the server's challenge with the proper
1300 /* How many bytes it will take to store LEN bytes in base64. */
1301 #define BASE64_LENGTH(len) (4 * (((len) + 2) / 3))
1303 /* Encode the string S of length LENGTH to base64 format and place it
1304 to STORE. STORE will be 0-terminated, and must point to a writable
1305 buffer of at least 1+BASE64_LENGTH(length) bytes. */
1307 base64_encode (const char *s, char *store, int length)
1309 /* Conversion table. */
1310 static char tbl[64] = {
1311 'A','B','C','D','E','F','G','H',
1312 'I','J','K','L','M','N','O','P',
1313 'Q','R','S','T','U','V','W','X',
1314 'Y','Z','a','b','c','d','e','f',
1315 'g','h','i','j','k','l','m','n',
1316 'o','p','q','r','s','t','u','v',
1317 'w','x','y','z','0','1','2','3',
1318 '4','5','6','7','8','9','+','/'
1321 unsigned char *p = (unsigned char *)store;
1323 /* Transform the 3x8 bits to 4x6 bits, as required by base64. */
1324 for (i = 0; i < length; i += 3)
1326 *p++ = tbl[s[0] >> 2];
1327 *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)];
1328 *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)];
1329 *p++ = tbl[s[2] & 0x3f];
1332 /* Pad the result if necessary... */
1333 if (i == length + 1)
1335 else if (i == length + 2)
1336 *(p - 1) = *(p - 2) = '=';
1337 /* ...and zero-terminate it. */
1341 /* Create the authentication header contents for the `Basic' scheme.
1342 This is done by encoding the string `USER:PASS' in base64 and
1343 prepending `HEADER: Basic ' to it. */
1345 basic_authentication_encode (const char *user, const char *passwd,
1348 char *t1, *t2, *res;
1349 int len1 = strlen (user) + 1 + strlen (passwd);
1350 int len2 = BASE64_LENGTH (len1);
1352 t1 = (char *)alloca (len1 + 1);
1353 sprintf (t1, "%s:%s", user, passwd);
1354 t2 = (char *)alloca (1 + len2);
1355 base64_encode (t1, t2, len1);
1356 res = (char *)malloc (len2 + 11 + strlen (header));
1357 sprintf (res, "%s: Basic %s\r\n", header, t2);
1363 /* Parse HTTP `WWW-Authenticate:' header. AU points to the beginning
1364 of a field in such a header. If the field is the one specified by
1365 ATTR_NAME ("realm", "opaque", and "nonce" are used by the current
1366 digest authorization code), extract its value in the (char*)
1367 variable pointed by RET. Returns negative on a malformed header,
1368 or number of bytes that have been parsed by this call. */
1370 extract_header_attr (const char *au, const char *attr_name, char **ret)
1372 const char *cp, *ep;
1376 if (strncmp (cp, attr_name, strlen (attr_name)) == 0)
1378 cp += strlen (attr_name);
1381 cp += skip_lws (cp);
1386 cp += skip_lws (cp);
1391 for (ep = cp; *ep && *ep != '\"'; ep++)
1396 *ret = strdupdelim (cp, ep);
1403 /* Response value needs to be in lowercase, so we cannot use HEXD2ASC
1404 from url.h. See RFC 2069 2.1.2 for the syntax of response-digest. */
1405 #define HEXD2asc(x) (((x) < 10) ? ((x) + '0') : ((x) - 10 + 'a'))
1407 /* Dump the hexadecimal representation of HASH to BUF. HASH should be
1408 an array of 16 bytes containing the hash keys, and BUF should be a
1409 buffer of 33 writable characters (32 for hex digits plus one for
1410 zero termination). */
1412 dump_hash (unsigned char *buf, const unsigned char *hash)
1416 for (i = 0; i < MD5_HASHLEN; i++, hash++)
1418 *buf++ = HEXD2asc (*hash >> 4);
1419 *buf++ = HEXD2asc (*hash & 0xf);
1424 /* Take the line apart to find the challenge, and compose a digest
1425 authorization header. See RFC2069 section 2.1.2. */
1427 digest_authentication_encode (const char *au, const char *user,
1428 const char *passwd, const char *method,
1431 static char *realm, *opaque, *nonce;
1436 { "realm", &realm },
1437 { "opaque", &opaque },
1442 realm = opaque = nonce = NULL;
1444 au += 6; /* skip over `Digest' */
1449 au += skip_lws (au);
1450 for (i = 0; i < ARRAY_SIZE (options); i++)
1452 int skip = extract_header_attr (au, options[i].name,
1453 options[i].variable);
1457 FREE_MAYBE (opaque);
1467 if (i == ARRAY_SIZE (options))
1469 while (*au && *au != '=')
1473 au += skip_lws (au);
1477 while (*au && *au != '\"')
1484 while (*au && *au != ',')
1489 if (!realm || !nonce || !user || !passwd || !path || !method)
1492 FREE_MAYBE (opaque);
1497 /* Calculate the digest value. */
1500 unsigned char hash[MD5_HASHLEN];
1501 unsigned char a1buf[MD5_HASHLEN * 2 + 1], a2buf[MD5_HASHLEN * 2 + 1];
1502 unsigned char response_digest[MD5_HASHLEN * 2 + 1];
1504 /* A1BUF = H(user ":" realm ":" password) */
1505 md5_init_ctx (&ctx);
1506 md5_process_bytes (user, strlen (user), &ctx);
1507 md5_process_bytes (":", 1, &ctx);
1508 md5_process_bytes (realm, strlen (realm), &ctx);
1509 md5_process_bytes (":", 1, &ctx);
1510 md5_process_bytes (passwd, strlen (passwd), &ctx);
1511 md5_finish_ctx (&ctx, hash);
1512 dump_hash (a1buf, hash);
1514 /* A2BUF = H(method ":" path) */
1515 md5_init_ctx (&ctx);
1516 md5_process_bytes (method, strlen (method), &ctx);
1517 md5_process_bytes (":", 1, &ctx);
1518 md5_process_bytes (path, strlen (path), &ctx);
1519 md5_finish_ctx (&ctx, hash);
1520 dump_hash (a2buf, hash);
1522 /* RESPONSE_DIGEST = H(A1BUF ":" nonce ":" A2BUF) */
1523 md5_init_ctx (&ctx);
1524 md5_process_bytes (a1buf, MD5_HASHLEN * 2, &ctx);
1525 md5_process_bytes (":", 1, &ctx);
1526 md5_process_bytes (nonce, strlen (nonce), &ctx);
1527 md5_process_bytes (":", 1, &ctx);
1528 md5_process_bytes (a2buf, MD5_HASHLEN * 2, &ctx);
1529 md5_finish_ctx (&ctx, hash);
1530 dump_hash (response_digest, hash);
1532 res = (char*) xmalloc (strlen (user)
1537 + 2 * MD5_HASHLEN /*strlen (response_digest)*/
1538 + (opaque ? strlen (opaque) : 0)
1540 sprintf (res, "Authorization: Digest \
1541 username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
1542 user, realm, nonce, path, response_digest);
1545 char *p = res + strlen (res);
1546 strcat (p, ", opaque=\"");
1550 strcat (res, "\r\n");
1554 #endif /* USE_DIGEST */
1557 #define HACK_O_MATIC(line, string_constant) \
1558 (!strncasecmp (line, string_constant, sizeof (string_constant) - 1) \
1559 && (ISSPACE (line[sizeof (string_constant) - 1]) \
1560 || !line[sizeof (string_constant) - 1]))
1563 known_authentication_scheme_p (const char *au)
1565 return HACK_O_MATIC (au, "Basic") || HACK_O_MATIC (au, "Digest");
1570 /* Create the HTTP authorization request header. When the
1571 `WWW-Authenticate' response header is seen, according to the
1572 authorization scheme specified in that header (`Basic' and `Digest'
1573 are supported by the current implementation), produce an
1574 appropriate HTTP authorization request header. */
1576 create_authorization_line (const char *au, const char *user,
1577 const char *passwd, const char *method,
1580 char *wwwauth = NULL;
1582 if (!strncasecmp (au, "Basic", 5))
1583 wwwauth = basic_authentication_encode (user, passwd, "Authorization");
1585 else if (!strncasecmp (au, "Digest", 6))
1586 wwwauth = digest_authentication_encode (au, user, passwd, method, path);
1587 #endif /* USE_DIGEST */