2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Wget.
6 GNU Wget 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 GNU Wget 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 Wget; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 In addition, as a special exception, the Free Software Foundation
21 gives permission to link the code of its release of Wget with the
22 OpenSSL project's "OpenSSL" library (or with modified versions of it
23 that use the same license as the "OpenSSL" library), and distribute
24 the linked executables. You must obey the GNU General Public License
25 in all respects for all of the code used other than "OpenSSL". If you
26 modify this file, you may extend this exception to your version of the
27 file, but you are not obligated to do so. If you do not wish to do
28 so, delete this exception statement from your version. */
34 #include <sys/types.h>
45 #if TIME_WITH_SYS_TIME
46 # include <sys/time.h>
50 # include <sys/time.h>
67 # include "gen_sslfunc.h"
75 extern char *version_string;
76 extern LARGE_INT total_downloaded_bytes;
78 extern FILE *output_stream;
79 extern int output_stream_regular;
82 # define MIN(x, y) ((x) > (y) ? (y) : (x))
86 static int cookies_loaded_p;
87 static struct cookie_jar *wget_cookie_jar;
89 #define TEXTHTML_S "text/html"
90 #define TEXTXHTML_S "application/xhtml+xml"
92 /* Some status code validation macros: */
93 #define H_20X(x) (((x) >= 200) && ((x) < 300))
94 #define H_PARTIAL(x) ((x) == HTTP_STATUS_PARTIAL_CONTENTS)
95 #define H_REDIRECTED(x) ((x) == HTTP_STATUS_MOVED_PERMANENTLY \
96 || (x) == HTTP_STATUS_MOVED_TEMPORARILY \
97 || (x) == HTTP_STATUS_SEE_OTHER \
98 || (x) == HTTP_STATUS_TEMPORARY_REDIRECT)
100 /* HTTP/1.0 status codes from RFC1945, provided for reference. */
101 /* Successful 2xx. */
102 #define HTTP_STATUS_OK 200
103 #define HTTP_STATUS_CREATED 201
104 #define HTTP_STATUS_ACCEPTED 202
105 #define HTTP_STATUS_NO_CONTENT 204
106 #define HTTP_STATUS_PARTIAL_CONTENTS 206
108 /* Redirection 3xx. */
109 #define HTTP_STATUS_MULTIPLE_CHOICES 300
110 #define HTTP_STATUS_MOVED_PERMANENTLY 301
111 #define HTTP_STATUS_MOVED_TEMPORARILY 302
112 #define HTTP_STATUS_SEE_OTHER 303 /* from HTTP/1.1 */
113 #define HTTP_STATUS_NOT_MODIFIED 304
114 #define HTTP_STATUS_TEMPORARY_REDIRECT 307 /* from HTTP/1.1 */
116 /* Client error 4xx. */
117 #define HTTP_STATUS_BAD_REQUEST 400
118 #define HTTP_STATUS_UNAUTHORIZED 401
119 #define HTTP_STATUS_FORBIDDEN 403
120 #define HTTP_STATUS_NOT_FOUND 404
121 #define HTTP_STATUS_RANGE_NOT_SATISFIABLE 416
123 /* Server errors 5xx. */
124 #define HTTP_STATUS_INTERNAL 500
125 #define HTTP_STATUS_NOT_IMPLEMENTED 501
126 #define HTTP_STATUS_BAD_GATEWAY 502
127 #define HTTP_STATUS_UNAVAILABLE 503
130 rel_none, rel_name, rel_value, rel_both
137 struct request_header {
139 enum rp release_policy;
141 int hcount, hcapacity;
144 /* Create a new, empty request. At least request_set_method must be
145 called before the request can be used. */
147 static struct request *
150 struct request *req = xnew0 (struct request);
152 req->headers = xnew_array (struct request_header, req->hcapacity);
156 /* Set the request's method and its arguments. METH should be a
157 literal string (or it should outlive the request) because it will
158 not be freed. ARG will be freed by request_free. */
161 request_set_method (struct request *req, const char *meth, char *arg)
167 /* Return the method string passed with the last call to
168 request_set_method. */
171 request_method (const struct request *req)
176 /* Free one header according to the release policy specified with
177 request_set_header. */
180 release_header (struct request_header *hdr)
182 switch (hdr->release_policy)
199 /* Set the request named NAME to VALUE. Specifically, this means that
200 a "NAME: VALUE\r\n" header line will be used in the request. If a
201 header with the same name previously existed in the request, its
202 value will be replaced by this one.
204 RELEASE_POLICY determines whether NAME and VALUE should be released
205 (freed) with request_free. Allowed values are:
207 - rel_none - don't free NAME or VALUE
208 - rel_name - free NAME when done
209 - rel_value - free VALUE when done
210 - rel_both - free both NAME and VALUE when done
212 Setting release policy is useful when arguments come from different
213 sources. For example:
215 // Don't free literal strings!
216 request_set_header (req, "Pragma", "no-cache", rel_none);
218 // Don't free a global variable, we'll need it later.
219 request_set_header (req, "Referer", opt.referer, rel_none);
221 // Value freshly allocated, free it when done.
222 request_set_header (req, "Range",
223 aprintf ("bytes=%s-", number_to_static_string (hs->restval)),
228 request_set_header (struct request *req, char *name, char *value,
229 enum rp release_policy)
231 struct request_header *hdr;
235 for (i = 0; i < req->hcount; i++)
237 hdr = &req->headers[i];
238 if (0 == strcasecmp (name, hdr->name))
240 /* Replace existing header. */
241 release_header (hdr);
244 hdr->release_policy = release_policy;
249 /* Install new header. */
251 if (req->hcount >= req->hcount)
253 req->hcapacity <<= 1;
254 req->headers = xrealloc (req->headers,
255 req->hcapacity * sizeof (struct request_header));
257 hdr = &req->headers[req->hcount++];
260 hdr->release_policy = release_policy;
263 /* Like request_set_header, but sets the whole header line, as
264 provided by the user using the `--header' option. For example,
265 request_set_user_header (req, "Foo: bar") works just like
266 request_set_header (req, "Foo", "bar"). */
269 request_set_user_header (struct request *req, const char *header)
272 const char *p = strchr (header, ':');
275 BOUNDED_TO_ALLOCA (header, p, name);
279 request_set_header (req, xstrdup (name), (char *) p, rel_name);
282 #define APPEND(p, str) do { \
283 int A_len = strlen (str); \
284 memcpy (p, str, A_len); \
288 /* Construct the request and write it to FD using fd_write. */
291 request_send (const struct request *req, int fd)
293 char *request_string, *p;
294 int i, size, write_error;
296 /* Count the request size. */
299 /* METHOD " " ARG " " "HTTP/1.0" "\r\n" */
300 size += strlen (req->method) + 1 + strlen (req->arg) + 1 + 8 + 2;
302 for (i = 0; i < req->hcount; i++)
304 struct request_header *hdr = &req->headers[i];
305 /* NAME ": " VALUE "\r\n" */
306 size += strlen (hdr->name) + 2 + strlen (hdr->value) + 2;
312 p = request_string = alloca_array (char, size);
314 /* Generate the request. */
316 APPEND (p, req->method); *p++ = ' ';
317 APPEND (p, req->arg); *p++ = ' ';
318 memcpy (p, "HTTP/1.0\r\n", 10); p += 10;
320 for (i = 0; i < req->hcount; i++)
322 struct request_header *hdr = &req->headers[i];
323 APPEND (p, hdr->name);
324 *p++ = ':', *p++ = ' ';
325 APPEND (p, hdr->value);
326 *p++ = '\r', *p++ = '\n';
329 *p++ = '\r', *p++ = '\n', *p++ = '\0';
330 assert (p - request_string == size);
334 DEBUGP (("\n---request begin---\n%s---request end---\n", request_string));
336 /* Send the request to the server. */
338 write_error = fd_write (fd, request_string, size - 1, -1);
340 logprintf (LOG_VERBOSE, _("Failed writing HTTP request: %s.\n"),
345 /* Release the resources used by REQ. */
348 request_free (struct request *req)
351 xfree_null (req->arg);
352 for (i = 0; i < req->hcount; i++)
353 release_header (&req->headers[i]);
354 xfree_null (req->headers);
358 /* Send the contents of FILE_NAME to SOCK/SSL. Make sure that exactly
359 PROMISED_SIZE bytes are sent over the wire -- if the file is
360 longer, read only that much; if the file is shorter, report an error. */
363 post_file (int sock, const char *file_name, wgint promised_size)
365 static char chunk[8192];
370 DEBUGP (("[writing POST file %s ... ", file_name));
372 fp = fopen (file_name, "rb");
375 while (!feof (fp) && written < promised_size)
378 int length = fread (chunk, 1, sizeof (chunk), fp);
381 towrite = MIN (promised_size - written, length);
382 write_error = fd_write (sock, chunk, towrite, -1);
392 /* If we've written less than was promised, report a (probably
393 nonsensical) error rather than break the promise. */
394 if (written < promised_size)
400 assert (written == promised_size);
401 DEBUGP (("done]\n"));
406 response_head_terminator (const char *hunk, int oldlen, int peeklen)
408 const char *start, *end;
410 /* If at first peek, verify whether HUNK starts with "HTTP". If
411 not, this is a HTTP/0.9 request and we must bail out without
413 if (oldlen == 0 && 0 != memcmp (hunk, "HTTP", MIN (peeklen, 4)))
419 start = hunk + oldlen - 4;
420 end = hunk + oldlen + peeklen;
422 for (; start < end - 1; start++)
429 if (start[1] == '\n')
435 /* The maximum size of a single HTTP response we care to read. This
436 is not meant to impose an arbitrary limit, but to protect the user
437 from Wget slurping up available memory upon encountering malicious
438 or buggy server output. Define it to 0 to remove the limit. */
440 #define HTTP_RESPONSE_MAX_SIZE 65536
442 /* Read the HTTP request head from FD and return it. The error
443 conditions are the same as with fd_read_hunk.
445 To support HTTP/0.9 responses, this function tries to make sure
446 that the data begins with "HTTP". If this is not the case, no data
447 is read and an empty request is returned, so that the remaining
448 data can be treated as body. */
451 read_http_response_head (int fd)
453 return fd_read_hunk (fd, response_head_terminator, 512,
454 HTTP_RESPONSE_MAX_SIZE);
458 /* The response data. */
461 /* The array of pointers that indicate where each header starts.
462 For example, given this HTTP response:
469 The headers are located like this:
471 "HTTP/1.0 200 Ok\r\nDescription: some\r\n text\r\nEtag: x\r\n\r\n"
473 headers[0] headers[1] headers[2] headers[3]
475 I.e. headers[0] points to the beginning of the request,
476 headers[1] points to the end of the first header and the
477 beginning of the second one, etc. */
479 const char **headers;
482 /* Create a new response object from the text of the HTTP response,
483 available in HEAD. That text is automatically split into
484 constituent header lines for fast retrieval using
487 static struct response *
488 resp_new (const char *head)
493 struct response *resp = xnew0 (struct response);
498 /* Empty head means that we're dealing with a headerless
499 (HTTP/0.9) response. In that case, don't set HEADERS at
504 /* Split HEAD into header lines, so that resp_header_* functions
505 don't need to do this over and over again. */
511 DO_REALLOC (resp->headers, size, count + 1, const char *);
512 resp->headers[count++] = hdr;
514 /* Break upon encountering an empty line. */
515 if (!hdr[0] || (hdr[0] == '\r' && hdr[1] == '\n') || hdr[0] == '\n')
518 /* Find the end of HDR, including continuations. */
521 const char *end = strchr (hdr, '\n');
527 while (*hdr == ' ' || *hdr == '\t');
529 DO_REALLOC (resp->headers, size, count + 1, const char *);
530 resp->headers[count] = NULL;
535 /* Locate the header named NAME in the request data, starting with
536 position START. This allows the code to loop through the request
537 data, filtering for all requests of a given name. Returns the
538 found position, or -1 for failure. The code that uses this
539 function typically looks like this:
541 for (pos = 0; (pos = resp_header_locate (...)) != -1; pos++)
542 ... do something with header ...
544 If you only care about one header, use resp_header_get instead of
548 resp_header_locate (const struct response *resp, const char *name, int start,
549 const char **begptr, const char **endptr)
552 const char **headers = resp->headers;
555 if (!headers || !headers[1])
558 name_len = strlen (name);
564 for (; headers[i + 1]; i++)
566 const char *b = headers[i];
567 const char *e = headers[i + 1];
569 && b[name_len] == ':'
570 && 0 == strncasecmp (b, name, name_len))
573 while (b < e && ISSPACE (*b))
575 while (b < e && ISSPACE (e[-1]))
585 /* Find and retrieve the header named NAME in the request data. If
586 found, set *BEGPTR to its starting, and *ENDPTR to its ending
587 position, and return 1. Otherwise return 0.
589 This function is used as a building block for resp_header_copy
590 and resp_header_strdup. */
593 resp_header_get (const struct response *resp, const char *name,
594 const char **begptr, const char **endptr)
596 int pos = resp_header_locate (resp, name, 0, begptr, endptr);
600 /* Copy the response header named NAME to buffer BUF, no longer than
601 BUFSIZE (BUFSIZE includes the terminating 0). If the header
602 exists, 1 is returned, otherwise 0. If there should be no limit on
603 the size of the header, use resp_header_strdup instead.
605 If BUFSIZE is 0, no data is copied, but the boolean indication of
606 whether the header is present is still returned. */
609 resp_header_copy (const struct response *resp, const char *name,
610 char *buf, int bufsize)
613 if (!resp_header_get (resp, name, &b, &e))
617 int len = MIN (e - b, bufsize - 1);
618 memcpy (buf, b, len);
624 /* Return the value of header named NAME in RESP, allocated with
625 malloc. If such a header does not exist in RESP, return NULL. */
628 resp_header_strdup (const struct response *resp, const char *name)
631 if (!resp_header_get (resp, name, &b, &e))
633 return strdupdelim (b, e);
636 /* Parse the HTTP status line, which is of format:
638 HTTP-Version SP Status-Code SP Reason-Phrase
640 The function returns the status-code, or -1 if the status line
641 appears malformed. The pointer to "reason-phrase" message is
642 returned in *MESSAGE. */
645 resp_status (const struct response *resp, char **message)
652 /* For a HTTP/0.9 response, assume status 200. */
654 *message = xstrdup (_("No headers, assuming HTTP/0.9"));
658 p = resp->headers[0];
659 end = resp->headers[1];
665 if (end - p < 4 || 0 != strncmp (p, "HTTP", 4))
669 /* Match the HTTP version. This is optional because Gnutella
670 servers have been reported to not specify HTTP version. */
671 if (p < end && *p == '/')
674 while (p < end && ISDIGIT (*p))
676 if (p < end && *p == '.')
678 while (p < end && ISDIGIT (*p))
682 while (p < end && ISSPACE (*p))
684 if (end - p < 3 || !ISDIGIT (p[0]) || !ISDIGIT (p[1]) || !ISDIGIT (p[2]))
687 status = 100 * (p[0] - '0') + 10 * (p[1] - '0') + (p[2] - '0');
692 while (p < end && ISSPACE (*p))
694 while (p < end && ISSPACE (end[-1]))
696 *message = strdupdelim (p, end);
702 /* Release the resources used by RESP. */
705 resp_free (struct response *resp)
707 xfree_null (resp->headers);
711 /* Print [b, e) to the log, omitting the trailing CRLF. */
714 print_server_response_1 (const char *prefix, const char *b, const char *e)
717 if (b < e && e[-1] == '\n')
719 if (b < e && e[-1] == '\r')
721 BOUNDED_TO_ALLOCA (b, e, ln);
722 logprintf (LOG_VERBOSE, "%s%s\n", prefix, escnonprint (ln));
725 /* Print the server response, line by line, omitting the trailing CR
726 characters, prefixed with PREFIX. */
729 print_server_response (const struct response *resp, const char *prefix)
734 for (i = 0; resp->headers[i + 1]; i++)
735 print_server_response_1 (prefix, resp->headers[i], resp->headers[i + 1]);
738 /* Parse the `Content-Range' header and extract the information it
739 contains. Returns 1 if successful, -1 otherwise. */
741 parse_content_range (const char *hdr, wgint *first_byte_ptr,
742 wgint *last_byte_ptr, wgint *entity_length_ptr)
746 /* Ancient versions of Netscape proxy server, presumably predating
747 rfc2068, sent out `Content-Range' without the "bytes"
749 if (!strncasecmp (hdr, "bytes", 5))
752 /* "JavaWebServer/1.1.1" sends "bytes: x-y/z", contrary to the
756 while (ISSPACE (*hdr))
763 for (num = 0; ISDIGIT (*hdr); hdr++)
764 num = 10 * num + (*hdr - '0');
765 if (*hdr != '-' || !ISDIGIT (*(hdr + 1)))
767 *first_byte_ptr = num;
769 for (num = 0; ISDIGIT (*hdr); hdr++)
770 num = 10 * num + (*hdr - '0');
771 if (*hdr != '/' || !ISDIGIT (*(hdr + 1)))
773 *last_byte_ptr = num;
775 for (num = 0; ISDIGIT (*hdr); hdr++)
776 num = 10 * num + (*hdr - '0');
777 *entity_length_ptr = num;
781 /* Read the body of the request, but don't store it anywhere and don't
782 display a progress gauge. This is useful for reading the error
783 responses whose bodies don't need to be displayed or logged, but
784 which need to be read anyway. */
787 skip_short_body (int fd, wgint contlen)
789 /* Skipping the body doesn't make sense if the content length is
790 unknown because, in that case, persistent connections cannot be
791 used. (#### This is not the case with HTTP/1.1 where they can
792 still be used with the magic of the "chunked" transfer!) */
795 DEBUGP (("Skipping %s bytes of body data... ", number_to_static_string (contlen)));
800 int ret = fd_read (fd, dlbuf, MIN (contlen, sizeof (dlbuf)), -1);
805 DEBUGP (("done.\n"));
808 /* Persistent connections. Currently, we cache the most recently used
809 connection as persistent, provided that the HTTP server agrees to
810 make it such. The persistence data is stored in the variables
811 below. Ideally, it should be possible to cache an arbitrary fixed
812 number of these connections. */
814 /* Whether a persistent connection is active. */
815 static int pconn_active;
818 /* The socket of the connection. */
821 /* Host and port of the currently active persistent connection. */
825 /* Whether a ssl handshake has occoured on this connection. */
829 /* Mark the persistent connection as invalid and free the resources it
830 uses. This is used by the CLOSE_* macros after they forcefully
831 close a registered persistent connection. */
834 invalidate_persistent (void)
836 DEBUGP (("Disabling further reuse of socket %d.\n", pconn.socket));
838 fd_close (pconn.socket);
843 /* Register FD, which should be a TCP/IP connection to HOST:PORT, as
844 persistent. This will enable someone to use the same connection
845 later. In the context of HTTP, this must be called only AFTER the
846 response has been received and the server has promised that the
847 connection will remain alive.
849 If a previous connection was persistent, it is closed. */
852 register_persistent (const char *host, int port, int fd, int ssl)
856 if (pconn.socket == fd)
858 /* The connection FD is already registered. */
863 /* The old persistent connection is still active; close it
864 first. This situation arises whenever a persistent
865 connection exists, but we then connect to a different
866 host, and try to register a persistent connection to that
868 invalidate_persistent ();
874 pconn.host = xstrdup (host);
878 DEBUGP (("Registered socket %d for persistent reuse.\n", fd));
881 /* Return non-zero if a persistent connection is available for
882 connecting to HOST:PORT. */
885 persistent_available_p (const char *host, int port, int ssl,
886 int *host_lookup_failed)
888 /* First, check whether a persistent connection is active at all. */
892 /* If we want SSL and the last connection wasn't or vice versa,
893 don't use it. Checking for host and port is not enough because
894 HTTP and HTTPS can apparently coexist on the same port. */
895 if (ssl != pconn.ssl)
898 /* If we're not connecting to the same port, we're not interested. */
899 if (port != pconn.port)
902 /* If the host is the same, we're in business. If not, there is
903 still hope -- read below. */
904 if (0 != strcasecmp (host, pconn.host))
906 /* If pconn.socket is already talking to HOST, we needn't
907 reconnect. This happens often when both sites are virtual
908 hosts distinguished only by name and served by the same
909 network interface, and hence the same web server (possibly
910 set up by the ISP and serving many different web sites).
911 This admittedly non-standard optimization does not contradict
912 HTTP and works well with popular server software. */
916 struct address_list *al;
919 /* Don't try to talk to two different SSL sites over the same
920 secure connection! (Besides, it's not clear if name-based
921 virtual hosting is even possible with SSL.) */
924 /* If pconn.socket's peer is one of the IP addresses HOST
925 resolves to, pconn.socket is for all intents and purposes
926 already talking to HOST. */
928 if (!socket_ip_address (pconn.socket, &ip, ENDPOINT_PEER))
930 /* Can't get the peer's address -- something must be very
931 wrong with the connection. */
932 invalidate_persistent ();
935 al = lookup_host (host, 0);
938 *host_lookup_failed = 1;
942 found = address_list_contains (al, &ip);
943 address_list_release (al);
948 /* The persistent connection's peer address was found among the
949 addresses HOST resolved to; therefore, pconn.sock is in fact
950 already talking to HOST -- no need to reconnect. */
953 /* Finally, check whether the connection is still open. This is
954 important because most server implement a liberal (short) timeout
955 on persistent connections. Wget can of course always reconnect
956 if the connection doesn't work out, but it's nicer to know in
957 advance. This test is a logical followup of the first test, but
958 is "expensive" and therefore placed at the end of the list. */
960 if (!test_socket_open (pconn.socket))
962 /* Oops, the socket is no longer open. Now that we know that,
963 let's invalidate the persistent connection before returning
965 invalidate_persistent ();
972 /* The idea behind these two CLOSE macros is to distinguish between
973 two cases: one when the job we've been doing is finished, and we
974 want to close the connection and leave, and two when something is
975 seriously wrong and we're closing the connection as part of
978 In case of keep_alive, CLOSE_FINISH should leave the connection
979 open, while CLOSE_INVALIDATE should still close it.
981 Note that the semantics of the flag `keep_alive' is "this
982 connection *will* be reused (the server has promised not to close
983 the connection once we're done)", while the semantics of
984 `pc_active_p && (fd) == pc_last_fd' is "we're *now* using an
985 active, registered connection". */
987 #define CLOSE_FINISH(fd) do { \
990 if (pconn_active && (fd) == pconn.socket) \
991 invalidate_persistent (); \
1000 #define CLOSE_INVALIDATE(fd) do { \
1001 if (pconn_active && (fd) == pconn.socket) \
1002 invalidate_persistent (); \
1010 wgint len; /* received length */
1011 wgint contlen; /* expected length */
1012 wgint restval; /* the restart value */
1013 int res; /* the result of last read */
1014 char *newloc; /* new location (redirection) */
1015 char *remote_time; /* remote time-stamp string */
1016 char *error; /* textual HTTP error */
1017 int statcode; /* status code */
1018 wgint rd_size; /* amount of data read from socket */
1019 double dltime; /* time it took to download the data */
1020 const char *referer; /* value of the referer header. */
1021 char **local_file; /* local file. */
1025 free_hstat (struct http_stat *hs)
1027 xfree_null (hs->newloc);
1028 xfree_null (hs->remote_time);
1029 xfree_null (hs->error);
1031 /* Guard against being called twice. */
1033 hs->remote_time = NULL;
1037 static char *create_authorization_line PARAMS ((const char *, const char *,
1038 const char *, const char *,
1040 static char *basic_authentication_encode PARAMS ((const char *, const char *));
1041 static int known_authentication_scheme_p PARAMS ((const char *));
1043 time_t http_atotm PARAMS ((const char *));
1045 #define BEGINS_WITH(line, string_constant) \
1046 (!strncasecmp (line, string_constant, sizeof (string_constant) - 1) \
1047 && (ISSPACE (line[sizeof (string_constant) - 1]) \
1048 || !line[sizeof (string_constant) - 1]))
1050 /* Retrieve a document through HTTP protocol. It recognizes status
1051 code, and correctly handles redirections. It closes the network
1052 socket. If it receives an error from the functions below it, it
1053 will print it if there is enough information to do so (almost
1054 always), returning the error to the caller (i.e. http_loop).
1056 Various HTTP parameters are stored to hs.
1058 If PROXY is non-NULL, the connection will be made to the proxy
1059 server, and u->url will be requested. */
1061 gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
1063 struct request *req;
1066 char *user, *passwd;
1070 wgint contlen, contrange;
1077 /* Whether authorization has been already tried. */
1078 int auth_tried_already;
1080 /* Whether our connection to the remote host is through SSL. */
1084 struct response *resp;
1088 /* Whether this connection will be kept alive after the HTTP request
1092 /* Whether keep-alive should be inhibited. */
1093 int inhibit_keep_alive = !opt.http_keep_alive || opt.ignore_length;
1095 /* Headers sent when using POST. */
1096 wgint post_data_size = 0;
1098 int host_lookup_failed = 0;
1101 if (u->scheme == SCHEME_HTTPS)
1103 /* Initialize the SSL context. After this has once been done,
1104 it becomes a no-op. */
1105 switch (ssl_init ())
1107 case SSLERRCTXCREATE:
1109 logprintf (LOG_NOTQUIET, _("Failed to set up an SSL context\n"));
1110 return SSLERRCTXCREATE;
1111 case SSLERRCERTFILE:
1112 /* try without certfile */
1113 logprintf (LOG_NOTQUIET,
1114 _("Failed to load certificates from %s\n"),
1116 logprintf (LOG_NOTQUIET,
1117 _("Trying without the specified certificate\n"));
1120 logprintf (LOG_NOTQUIET,
1121 _("Failed to get certificate key from %s\n"),
1123 logprintf (LOG_NOTQUIET,
1124 _("Trying without the specified certificate\n"));
1130 #endif /* HAVE_SSL */
1132 if (!(*dt & HEAD_ONLY))
1133 /* If we're doing a GET on the URL, as opposed to just a HEAD, we need to
1134 know the local filename so we can save to it. */
1135 assert (*hs->local_file != NULL);
1137 auth_tried_already = 0;
1139 /* Initialize certain elements of struct http_stat. */
1144 hs->remote_time = NULL;
1149 /* Prepare the request to send. */
1151 req = request_new ();
1153 const char *meth = "GET";
1154 if (*dt & HEAD_ONLY)
1156 else if (opt.post_file_name || opt.post_data)
1158 /* Use the full path, i.e. one that includes the leading slash and
1159 the query string. E.g. if u->path is "foo/bar" and u->query is
1160 "param=value", full_path will be "/foo/bar?param=value". */
1161 request_set_method (req, meth,
1162 proxy ? xstrdup (u->url) : url_full_path (u));
1165 request_set_header (req, "Referer", (char *) hs->referer, rel_none);
1166 if (*dt & SEND_NOCACHE)
1167 request_set_header (req, "Pragma", "no-cache", rel_none);
1169 request_set_header (req, "Range",
1170 aprintf ("bytes=%s-",
1171 number_to_static_string (hs->restval)),
1174 request_set_header (req, "User-Agent", opt.useragent, rel_none);
1176 request_set_header (req, "User-Agent",
1177 aprintf ("Wget/%s", version_string), rel_value);
1178 request_set_header (req, "Accept", "*/*", rel_none);
1180 /* Find the username and password for authentication. */
1183 search_netrc (u->host, (const char **)&user, (const char **)&passwd, 0);
1184 user = user ? user : opt.http_user;
1185 passwd = passwd ? passwd : opt.http_passwd;
1189 /* We have the username and the password, but haven't tried
1190 any authorization yet. Let's see if the "Basic" method
1191 works. If not, we'll come back here and construct a
1192 proper authorization method with the right challenges.
1194 If we didn't employ this kind of logic, every URL that
1195 requires authorization would have to be processed twice,
1196 which is very suboptimal and generates a bunch of false
1197 "unauthorized" errors in the server log.
1199 #### But this logic also has a serious problem when used
1200 with stronger authentications: we *first* transmit the
1201 username and the password in clear text, and *then* attempt a
1202 stronger authentication scheme. That cannot be right! We
1203 are only fortunate that almost everyone still uses the
1204 `Basic' scheme anyway.
1206 There should be an option to prevent this from happening, for
1207 those who use strong authentication schemes and value their
1209 request_set_header (req, "Authorization",
1210 basic_authentication_encode (user, passwd),
1217 char *proxy_user, *proxy_passwd;
1218 /* For normal username and password, URL components override
1219 command-line/wgetrc parameters. With proxy
1220 authentication, it's the reverse, because proxy URLs are
1221 normally the "permanent" ones, so command-line args
1222 should take precedence. */
1223 if (opt.proxy_user && opt.proxy_passwd)
1225 proxy_user = opt.proxy_user;
1226 proxy_passwd = opt.proxy_passwd;
1230 proxy_user = proxy->user;
1231 proxy_passwd = proxy->passwd;
1233 /* #### This does not appear right. Can't the proxy request,
1234 say, `Digest' authentication? */
1235 if (proxy_user && proxy_passwd)
1236 proxyauth = basic_authentication_encode (proxy_user, proxy_passwd);
1238 /* If we're using a proxy, we will be connecting to the proxy
1242 /* Proxy authorization over SSL is handled below. */
1244 if (u->scheme != SCHEME_HTTPS)
1246 request_set_header (req, "Proxy-Authorization", proxyauth, rel_value);
1250 /* Whether we need to print the host header with braces around
1251 host, e.g. "Host: [3ffe:8100:200:2::2]:1234" instead of the
1252 usual "Host: symbolic-name:1234". */
1253 int squares = strchr (u->host, ':') != NULL;
1254 if (u->port == scheme_default_port (u->scheme))
1255 request_set_header (req, "Host",
1256 aprintf (squares ? "[%s]" : "%s", u->host),
1259 request_set_header (req, "Host",
1260 aprintf (squares ? "[%s]:%d" : "%s:%d",
1265 if (!inhibit_keep_alive)
1266 request_set_header (req, "Connection", "Keep-Alive", rel_none);
1269 request_set_header (req, "Cookie",
1270 cookie_header (wget_cookie_jar,
1271 u->host, u->port, u->path,
1273 u->scheme == SCHEME_HTTPS
1280 if (opt.post_data || opt.post_file_name)
1282 request_set_header (req, "Content-Type",
1283 "application/x-www-form-urlencoded", rel_none);
1285 post_data_size = strlen (opt.post_data);
1288 post_data_size = file_size (opt.post_file_name);
1289 if (post_data_size == -1)
1291 logprintf (LOG_NOTQUIET, "POST data file missing: %s\n",
1292 opt.post_file_name);
1296 request_set_header (req, "Content-Length",
1297 xstrdup (number_to_static_string (post_data_size)),
1301 /* Add the user headers. */
1302 if (opt.user_headers)
1305 for (i = 0; opt.user_headers[i]; i++)
1306 request_set_user_header (req, opt.user_headers[i]);
1310 /* We need to come back here when the initial attempt to retrieve
1311 without authorization header fails. (Expected to happen at least
1312 for the Digest authorization scheme.) */
1316 /* Establish the connection. */
1318 if (!inhibit_keep_alive)
1320 /* Look for a persistent connection to target host, unless a
1321 proxy is used. The exception is when SSL is in use, in which
1322 case the proxy is nothing but a passthrough to the target
1323 host, registered as a connection to the latter. */
1324 struct url *relevant = conn;
1326 if (u->scheme == SCHEME_HTTPS)
1330 if (persistent_available_p (relevant->host, relevant->port,
1332 relevant->scheme == SCHEME_HTTPS,
1336 &host_lookup_failed))
1338 sock = pconn.socket;
1339 using_ssl = pconn.ssl;
1340 logprintf (LOG_VERBOSE, _("Reusing existing connection to %s:%d.\n"),
1341 escnonprint (pconn.host), pconn.port);
1342 DEBUGP (("Reusing fd %d.\n", sock));
1348 /* In its current implementation, persistent_available_p will
1349 look up conn->host in some cases. If that lookup failed, we
1350 don't need to bother with connect_to_host. */
1351 if (host_lookup_failed)
1357 sock = connect_to_host (conn->host, conn->port);
1366 return (retryable_socket_connect_error (errno)
1367 ? CONERROR : CONIMPOSSIBLE);
1371 if (proxy && u->scheme == SCHEME_HTTPS)
1373 /* When requesting SSL URLs through proxies, use the
1374 CONNECT method to request passthrough. */
1375 struct request *connreq = request_new ();
1376 request_set_method (connreq, "CONNECT",
1377 aprintf ("%s:%d", u->host, u->port));
1380 request_set_header (connreq, "Proxy-Authorization",
1381 proxyauth, rel_value);
1382 /* Now that PROXYAUTH is part of the CONNECT request,
1383 zero it out so we don't send proxy authorization with
1384 the regular request below. */
1388 write_error = request_send (connreq, sock);
1389 request_free (connreq);
1390 if (write_error < 0)
1392 logprintf (LOG_VERBOSE, _("Failed writing to proxy: %s.\n"),
1394 CLOSE_INVALIDATE (sock);
1398 head = read_http_response_head (sock);
1401 logprintf (LOG_VERBOSE, _("Failed reading proxy response: %s\n"),
1403 CLOSE_INVALIDATE (sock);
1412 DEBUGP (("proxy responded with: [%s]\n", head));
1414 resp = resp_new (head);
1415 statcode = resp_status (resp, &message);
1418 if (statcode != 200)
1421 logprintf (LOG_NOTQUIET, _("Proxy tunneling failed: %s"),
1422 message ? escnonprint (message) : "?");
1423 xfree_null (message);
1426 xfree_null (message);
1428 /* SOCK is now *really* connected to u->host, so update CONN
1429 to reflect this. That way register_persistent will
1430 register SOCK as being connected to u->host:u->port. */
1434 if (conn->scheme == SCHEME_HTTPS)
1436 if (!ssl_connect (sock))
1443 #endif /* HAVE_SSL */
1446 /* Send the request to server. */
1447 write_error = request_send (req, sock);
1449 if (write_error >= 0)
1453 DEBUGP (("[POST data: %s]\n", opt.post_data));
1454 write_error = fd_write (sock, opt.post_data, post_data_size, -1);
1456 else if (opt.post_file_name && post_data_size != 0)
1457 write_error = post_file (sock, opt.post_file_name, post_data_size);
1460 if (write_error < 0)
1462 logprintf (LOG_VERBOSE, _("Failed writing HTTP request: %s.\n"),
1464 CLOSE_INVALIDATE (sock);
1468 logprintf (LOG_VERBOSE, _("%s request sent, awaiting response... "),
1469 proxy ? "Proxy" : "HTTP");
1474 head = read_http_response_head (sock);
1479 logputs (LOG_NOTQUIET, _("No data received.\n"));
1480 CLOSE_INVALIDATE (sock);
1486 logprintf (LOG_NOTQUIET, _("Read error (%s) in headers.\n"),
1488 CLOSE_INVALIDATE (sock);
1493 DEBUGP (("\n---response begin---\n%s---response end---\n", head));
1495 resp = resp_new (head);
1497 /* Check for status line. */
1499 statcode = resp_status (resp, &message);
1500 if (!opt.server_response)
1501 logprintf (LOG_VERBOSE, "%2d %s\n", statcode,
1502 message ? escnonprint (message) : "");
1505 logprintf (LOG_VERBOSE, "\n");
1506 print_server_response (resp, " ");
1509 if (!opt.ignore_length
1510 && resp_header_copy (resp, "Content-Length", hdrval, sizeof (hdrval)))
1514 parsed = str_to_wgint (hdrval, NULL, 10);
1515 if (parsed == WGINT_MAX && errno == ERANGE)
1517 #### If Content-Length is out of range, it most likely
1518 means that the file is larger than 2G and that we're
1519 compiled without LFS. In that case we should probably
1520 refuse to even attempt to download the file. */
1526 /* Check for keep-alive related responses. */
1527 if (!inhibit_keep_alive && contlen != -1)
1529 if (resp_header_copy (resp, "Keep-Alive", NULL, 0))
1531 else if (resp_header_copy (resp, "Connection", hdrval, sizeof (hdrval)))
1533 if (0 == strcasecmp (hdrval, "Keep-Alive"))
1538 /* The server has promised that it will not close the connection
1539 when we're done. This means that we can register it. */
1540 register_persistent (conn->host, conn->port, sock, using_ssl);
1542 if (statcode == HTTP_STATUS_UNAUTHORIZED)
1544 /* Authorization is required. */
1545 skip_short_body (sock, contlen);
1546 CLOSE_FINISH (sock);
1547 if (auth_tried_already || !(user && passwd))
1549 /* If we have tried it already, then there is not point
1551 logputs (LOG_NOTQUIET, _("Authorization failed.\n"));
1555 char *www_authenticate = resp_header_strdup (resp,
1556 "WWW-Authenticate");
1557 /* If the authentication scheme is unknown or if it's the
1558 "Basic" authentication (which we try by default), there's
1559 no sense in retrying. */
1560 if (!www_authenticate
1561 || !known_authentication_scheme_p (www_authenticate)
1562 || BEGINS_WITH (www_authenticate, "Basic"))
1564 xfree_null (www_authenticate);
1565 logputs (LOG_NOTQUIET, _("Unknown authentication scheme.\n"));
1570 auth_tried_already = 1;
1571 pth = url_full_path (u);
1572 request_set_header (req, "Authorization",
1573 create_authorization_line (www_authenticate,
1575 request_method (req),
1579 xfree (www_authenticate);
1580 goto retry_with_auth;
1588 hs->statcode = statcode;
1590 hs->error = xstrdup (_("Malformed status line"));
1592 hs->error = xstrdup (_("(no description)"));
1594 hs->error = xstrdup (message);
1597 type = resp_header_strdup (resp, "Content-Type");
1600 char *tmp = strchr (type, ';');
1603 while (tmp > type && ISSPACE (tmp[-1]))
1608 hs->newloc = resp_header_strdup (resp, "Location");
1609 hs->remote_time = resp_header_strdup (resp, "Last-Modified");
1611 /* Handle (possibly multiple instances of) the Set-Cookie header. */
1614 const char *scbeg, *scend;
1615 /* The jar should have been created by now. */
1616 assert (wget_cookie_jar != NULL);
1618 (scpos = resp_header_locate (resp, "Set-Cookie", scpos,
1619 &scbeg, &scend)) != -1;
1622 char *set_cookie = strdupdelim (scbeg, scend);
1623 cookie_handle_set_cookie (wget_cookie_jar, u->host, u->port, u->path,
1629 if (resp_header_copy (resp, "Content-Range", hdrval, sizeof (hdrval)))
1631 wgint first_byte_pos, last_byte_pos, entity_length;
1632 if (parse_content_range (hdrval, &first_byte_pos, &last_byte_pos,
1634 contrange = first_byte_pos;
1639 /* 20x responses are counted among successful by default. */
1640 if (H_20X (statcode))
1643 /* Return if redirected. */
1644 if (H_REDIRECTED (statcode) || statcode == HTTP_STATUS_MULTIPLE_CHOICES)
1646 /* RFC2068 says that in case of the 300 (multiple choices)
1647 response, the server can output a preferred URL through
1648 `Location' header; otherwise, the request should be treated
1649 like GET. So, if the location is set, it will be a
1650 redirection; otherwise, just proceed normally. */
1651 if (statcode == HTTP_STATUS_MULTIPLE_CHOICES && !hs->newloc)
1655 logprintf (LOG_VERBOSE,
1656 _("Location: %s%s\n"),
1657 hs->newloc ? escnonprint_uri (hs->newloc) : _("unspecified"),
1658 hs->newloc ? _(" [following]") : "");
1660 skip_short_body (sock, contlen);
1661 CLOSE_FINISH (sock);
1667 /* If content-type is not given, assume text/html. This is because
1668 of the multitude of broken CGI's that "forget" to generate the
1671 0 == strncasecmp (type, TEXTHTML_S, strlen (TEXTHTML_S)) ||
1672 0 == strncasecmp (type, TEXTXHTML_S, strlen (TEXTXHTML_S)))
1677 if (opt.html_extension && (*dt & TEXTHTML))
1678 /* -E / --html-extension / html_extension = on was specified, and this is a
1679 text/html file. If some case-insensitive variation on ".htm[l]" isn't
1680 already the file's suffix, tack on ".html". */
1682 char* last_period_in_local_filename = strrchr(*hs->local_file, '.');
1684 if (last_period_in_local_filename == NULL
1685 || !(0 == strcasecmp (last_period_in_local_filename, ".htm")
1686 || 0 == strcasecmp (last_period_in_local_filename, ".html")))
1688 size_t local_filename_len = strlen(*hs->local_file);
1690 *hs->local_file = xrealloc(*hs->local_file,
1691 local_filename_len + sizeof(".html"));
1692 strcpy(*hs->local_file + local_filename_len, ".html");
1694 *dt |= ADDED_HTML_EXTENSION;
1698 if (statcode == HTTP_STATUS_RANGE_NOT_SATISFIABLE)
1700 /* If `-c' is in use and the file has been fully downloaded (or
1701 the remote file has shrunk), Wget effectively requests bytes
1702 after the end of file and the server response with 416. */
1703 logputs (LOG_VERBOSE, _("\
1704 \n The file is already fully retrieved; nothing to do.\n\n"));
1705 /* In case the caller inspects. */
1708 /* Mark as successfully retrieved. */
1711 CLOSE_INVALIDATE (sock); /* would be CLOSE_FINISH, but there
1712 might be more bytes in the body. */
1713 return RETRUNNEEDED;
1715 if ((contrange != 0 && contrange != hs->restval)
1716 || (H_PARTIAL (statcode) && !contrange))
1718 /* The Range request was somehow misunderstood by the server.
1721 CLOSE_INVALIDATE (sock);
1724 hs->contlen = contlen + contrange;
1730 /* No need to print this output if the body won't be
1731 downloaded at all, or if the original server response is
1733 logputs (LOG_VERBOSE, _("Length: "));
1736 logputs (LOG_VERBOSE, legible (contlen + contrange));
1738 logprintf (LOG_VERBOSE, _(" (%s to go)"), legible (contlen));
1741 logputs (LOG_VERBOSE,
1742 opt.ignore_length ? _("ignored") : _("unspecified"));
1744 logprintf (LOG_VERBOSE, " [%s]\n", escnonprint (type));
1746 logputs (LOG_VERBOSE, "\n");
1750 type = NULL; /* We don't need it any more. */
1752 /* Return if we have no intention of further downloading. */
1753 if (!(*dt & RETROKF) || (*dt & HEAD_ONLY))
1755 /* In case the caller cares to look... */
1759 /* Pre-1.10 Wget used CLOSE_INVALIDATE here. Now we trust the
1760 servers not to send body in response to a HEAD request. If
1761 you encounter such a server (more likely a broken CGI), use
1762 `--no-http-keep-alive'. */
1763 CLOSE_FINISH (sock);
1764 return RETRFINISHED;
1767 /* Open the local file. */
1770 mkalldirs (*hs->local_file);
1772 rotate_backups (*hs->local_file);
1774 fp = fopen (*hs->local_file, "ab");
1775 else if (opt.noclobber || opt.always_rest || opt.timestamping || opt.dirstruct
1776 || opt.output_document)
1777 fp = fopen (*hs->local_file, "wb");
1780 fp = fopen_excl (*hs->local_file, 0);
1781 if (!fp && errno == EEXIST)
1783 /* We cannot just invent a new name and use it (which is
1784 what functions like unique_create typically do)
1785 because we told the user we'd use this name.
1786 Instead, return and retry the download. */
1787 logprintf (LOG_NOTQUIET,
1788 _("%s has sprung into existence.\n"),
1790 CLOSE_INVALIDATE (sock);
1791 return FOPEN_EXCL_ERR;
1796 logprintf (LOG_NOTQUIET, "%s: %s\n", *hs->local_file, strerror (errno));
1797 CLOSE_INVALIDATE (sock);
1804 /* #### This confuses the timestamping code that checks for file
1805 size. Maybe we should save some additional information? */
1806 if (opt.save_headers)
1807 fwrite (head, 1, strlen (head), fp);
1809 /* Download the request body. */
1812 flags |= rb_read_exactly;
1813 if (hs->restval > 0 && contrange == 0)
1814 /* If the server ignored our range request, instruct fd_read_body
1815 to skip the first RESTVAL bytes of body. */
1816 flags |= rb_skip_startpos;
1817 hs->len = hs->restval;
1819 hs->res = fd_read_body (sock, fp, contlen != -1 ? contlen : 0,
1820 hs->restval, &hs->rd_size, &hs->len, &hs->dltime,
1824 CLOSE_FINISH (sock);
1826 CLOSE_INVALIDATE (sock);
1829 /* Close or flush the file. We have to be careful to check for
1830 error here. Checking the result of fwrite() is not enough --
1831 errors could go unnoticed! */
1834 flush_res = fclose (fp);
1836 flush_res = fflush (fp);
1837 if (flush_res == EOF)
1842 return RETRFINISHED;
1845 /* The genuine HTTP loop! This is the part where the retrieval is
1846 retried, and retried, and retried, and... */
1848 http_loop (struct url *u, char **newloc, char **local_file, const char *referer,
1849 int *dt, struct url *proxy)
1852 int use_ts, got_head = 0; /* time-stamping info */
1853 char *filename_plus_orig_suffix;
1854 char *local_filename = NULL;
1855 char *tms, *locf, *tmrate;
1857 time_t tml = -1, tmr = -1; /* local and remote time-stamps */
1858 wgint local_size = 0; /* the size of the local file */
1859 size_t filename_len;
1860 struct http_stat hstat; /* HTTP status */
1864 /* This used to be done in main(), but it's a better idea to do it
1865 here so that we don't go through the hoops if we're just using
1869 if (!wget_cookie_jar)
1870 wget_cookie_jar = cookie_jar_new ();
1871 if (opt.cookies_input && !cookies_loaded_p)
1873 cookie_jar_load (wget_cookie_jar, opt.cookies_input);
1874 cookies_loaded_p = 1;
1880 /* Warn on (likely bogus) wildcard usage in HTTP. Don't use
1881 has_wildcards_p because it would also warn on `?', and we know that
1882 shows up in CGI paths a *lot*. */
1883 if (strchr (u->url, '*'))
1884 logputs (LOG_VERBOSE, _("Warning: wildcards not supported in HTTP.\n"));
1888 /* Determine the local filename. */
1889 if (local_file && *local_file)
1890 hstat.local_file = local_file;
1891 else if (local_file && !opt.output_document)
1893 *local_file = url_file_name (u);
1894 hstat.local_file = local_file;
1898 dummy = url_file_name (u);
1899 hstat.local_file = &dummy;
1900 /* be honest about where we will save the file */
1901 if (local_file && opt.output_document)
1902 *local_file = HYPHENP (opt.output_document) ? NULL : xstrdup (opt.output_document);
1905 if (!opt.output_document)
1906 locf = *hstat.local_file;
1908 locf = opt.output_document;
1910 hstat.referer = referer;
1912 filename_len = strlen (*hstat.local_file);
1913 filename_plus_orig_suffix = alloca (filename_len + sizeof (".orig"));
1915 if (opt.noclobber && file_exists_p (*hstat.local_file))
1917 /* If opt.noclobber is turned on and file already exists, do not
1918 retrieve the file */
1919 logprintf (LOG_VERBOSE, _("\
1920 File `%s' already there, will not retrieve.\n"), *hstat.local_file);
1921 /* If the file is there, we suppose it's retrieved OK. */
1924 /* #### Bogusness alert. */
1925 /* If its suffix is "html" or "htm" or similar, assume text/html. */
1926 if (has_html_suffix_p (*hstat.local_file))
1934 if (opt.timestamping)
1936 int local_dot_orig_file_exists = 0;
1938 if (opt.backup_converted)
1939 /* If -K is specified, we'll act on the assumption that it was specified
1940 last time these files were downloaded as well, and instead of just
1941 comparing local file X against server file X, we'll compare local
1942 file X.orig (if extant, else X) against server file X. If -K
1943 _wasn't_ specified last time, or the server contains files called
1944 *.orig, -N will be back to not operating correctly with -k. */
1946 /* Would a single s[n]printf() call be faster? --dan
1948 Definitely not. sprintf() is horribly slow. It's a
1949 different question whether the difference between the two
1950 affects a program. Usually I'd say "no", but at one
1951 point I profiled Wget, and found that a measurable and
1952 non-negligible amount of time was lost calling sprintf()
1953 in url.c. Replacing sprintf with inline calls to
1954 strcpy() and number_to_string() made a difference.
1956 memcpy (filename_plus_orig_suffix, *hstat.local_file, filename_len);
1957 memcpy (filename_plus_orig_suffix + filename_len,
1958 ".orig", sizeof (".orig"));
1960 /* Try to stat() the .orig file. */
1961 if (stat (filename_plus_orig_suffix, &st) == 0)
1963 local_dot_orig_file_exists = 1;
1964 local_filename = filename_plus_orig_suffix;
1968 if (!local_dot_orig_file_exists)
1969 /* Couldn't stat() <file>.orig, so try to stat() <file>. */
1970 if (stat (*hstat.local_file, &st) == 0)
1971 local_filename = *hstat.local_file;
1973 if (local_filename != NULL)
1974 /* There was a local file, so we'll check later to see if the version
1975 the server has is the same version we already have, allowing us to
1981 /* Modification time granularity is 2 seconds for Windows, so
1982 increase local time by 1 second for later comparison. */
1985 local_size = st.st_size;
1989 /* Reset the counter. */
1995 /* Increment the pass counter. */
1997 sleep_between_retrievals (count);
1998 /* Get the current time string. */
1999 tms = time_str (NULL);
2000 /* Print fetch message, if opt.verbose. */
2003 char *hurl = url_string (u, 1);
2007 sprintf (tmp, _("(try:%2d)"), count);
2008 logprintf (LOG_VERBOSE, "--%s-- %s\n %s => `%s'\n",
2009 tms, hurl, tmp, locf);
2011 ws_changetitle (hurl);
2016 /* Default document type is empty. However, if spider mode is
2017 on or time-stamping is employed, HEAD_ONLY commands is
2018 encoded within *dt. */
2019 if (opt.spider || (use_ts && !got_head))
2024 /* Decide whether or not to restart. */
2027 hstat.restval = hstat.len; /* continue where we left off */
2028 else if (opt.always_rest
2029 && stat (locf, &st) == 0
2030 && S_ISREG (st.st_mode))
2031 hstat.restval = st.st_size;
2033 /* Decide whether to send the no-cache directive. We send it in
2035 a) we're using a proxy, and we're past our first retrieval.
2036 Some proxies are notorious for caching incomplete data, so
2037 we require a fresh get.
2038 b) caching is explicitly inhibited. */
2039 if ((proxy && count > 1) /* a */
2040 || !opt.allow_cache /* b */
2042 *dt |= SEND_NOCACHE;
2044 *dt &= ~SEND_NOCACHE;
2046 /* Try fetching the document, or at least its head. */
2047 err = gethttp (u, &hstat, dt, proxy);
2049 /* It's unfortunate that wget determines the local filename before finding
2050 out the Content-Type of the file. Barring a major restructuring of the
2051 code, we need to re-set locf here, since gethttp() may have xrealloc()d
2052 *hstat.local_file to tack on ".html". */
2053 if (!opt.output_document)
2054 locf = *hstat.local_file;
2057 tms = time_str (NULL);
2058 /* Get the new location (with or without the redirection). */
2060 *newloc = xstrdup (hstat.newloc);
2063 case HERR: case HEOF: case CONSOCKERR: case CONCLOSED:
2064 case CONERROR: case READERR: case WRITEFAILED:
2065 case RANGEERR: case FOPEN_EXCL_ERR:
2066 /* Non-fatal errors continue executing the loop, which will
2067 bring them to "while" statement at the end, to judge
2068 whether the number of tries was exceeded. */
2069 free_hstat (&hstat);
2070 printwhat (count, opt.ntry);
2071 if (err == FOPEN_EXCL_ERR)
2073 /* Re-determine the file name. */
2074 if (local_file && *local_file)
2076 xfree (*local_file);
2077 *local_file = url_file_name (u);
2078 hstat.local_file = local_file;
2083 dummy = url_file_name (u);
2084 hstat.local_file = &dummy;
2086 /* be honest about where we will save the file */
2087 if (local_file && opt.output_document)
2088 *local_file = HYPHENP (opt.output_document) ? NULL : xstrdup (opt.output_document);
2089 if (!opt.output_document)
2090 locf = *hstat.local_file;
2092 locf = opt.output_document;
2096 case HOSTERR: case CONIMPOSSIBLE: case PROXERR: case AUTHFAILED:
2097 case SSLERRCTXCREATE: case CONTNOTSUPPORTED:
2098 /* Fatal errors just return from the function. */
2099 free_hstat (&hstat);
2103 case FWRITEERR: case FOPENERR:
2104 /* Another fatal error. */
2105 logputs (LOG_VERBOSE, "\n");
2106 logprintf (LOG_NOTQUIET, _("Cannot write to `%s' (%s).\n"),
2107 *hstat.local_file, strerror (errno));
2108 free_hstat (&hstat);
2113 /* Another fatal error. */
2114 logputs (LOG_VERBOSE, "\n");
2115 logprintf (LOG_NOTQUIET, _("Unable to establish SSL connection.\n"));
2116 free_hstat (&hstat);
2121 /* Return the new location to the caller. */
2124 logprintf (LOG_NOTQUIET,
2125 _("ERROR: Redirection (%d) without location.\n"),
2127 free_hstat (&hstat);
2131 free_hstat (&hstat);
2136 /* The file was already fully retrieved. */
2137 free_hstat (&hstat);
2142 /* Deal with you later. */
2145 /* All possibilities should have been exhausted. */
2148 if (!(*dt & RETROKF))
2152 /* #### Ugly ugly ugly! */
2153 char *hurl = url_string (u, 1);
2154 logprintf (LOG_NONVERBOSE, "%s:\n", hurl);
2157 logprintf (LOG_NOTQUIET, _("%s ERROR %d: %s.\n"),
2158 tms, hstat.statcode, escnonprint (hstat.error));
2159 logputs (LOG_VERBOSE, "\n");
2160 free_hstat (&hstat);
2165 /* Did we get the time-stamp? */
2168 if (opt.timestamping && !hstat.remote_time)
2170 logputs (LOG_NOTQUIET, _("\
2171 Last-modified header missing -- time-stamps turned off.\n"));
2173 else if (hstat.remote_time)
2175 /* Convert the date-string into struct tm. */
2176 tmr = http_atotm (hstat.remote_time);
2177 if (tmr == (time_t) (-1))
2178 logputs (LOG_VERBOSE, _("\
2179 Last-modified header invalid -- time-stamp ignored.\n"));
2183 /* The time-stamping section. */
2188 use_ts = 0; /* no more time-stamping */
2189 count = 0; /* the retrieve count for HEAD is
2191 if (hstat.remote_time && tmr != (time_t) (-1))
2193 /* Now time-stamping can be used validly. Time-stamping
2194 means that if the sizes of the local and remote file
2195 match, and local file is newer than the remote file,
2196 it will not be retrieved. Otherwise, the normal
2197 download procedure is resumed. */
2199 (hstat.contlen == -1 || local_size == hstat.contlen))
2201 logprintf (LOG_VERBOSE, _("\
2202 Server file no newer than local file `%s' -- not retrieving.\n\n"),
2204 free_hstat (&hstat);
2208 else if (tml >= tmr)
2209 logprintf (LOG_VERBOSE, _("\
2210 The sizes do not match (local %s) -- retrieving.\n"),
2211 number_to_static_string (local_size));
2213 logputs (LOG_VERBOSE,
2214 _("Remote file is newer, retrieving.\n"));
2216 free_hstat (&hstat);
2219 if ((tmr != (time_t) (-1))
2221 && ((hstat.len == hstat.contlen) ||
2222 ((hstat.res == 0) &&
2223 ((hstat.contlen == -1) ||
2224 (hstat.len >= hstat.contlen && !opt.kill_longer)))))
2226 /* #### This code repeats in http.c and ftp.c. Move it to a
2228 const char *fl = NULL;
2229 if (opt.output_document)
2231 if (output_stream_regular)
2232 fl = opt.output_document;
2235 fl = *hstat.local_file;
2239 /* End of time-stamping section. */
2243 logprintf (LOG_NOTQUIET, "%d %s\n\n", hstat.statcode,
2244 escnonprint (hstat.error));
2249 tmrate = retr_rate (hstat.rd_size, hstat.dltime, 0);
2251 if (hstat.len == hstat.contlen)
2255 logprintf (LOG_VERBOSE,
2256 _("%s (%s) - `%s' saved [%s/%s]\n\n"),
2258 number_to_static_string (hstat.len),
2259 number_to_static_string (hstat.contlen));
2260 logprintf (LOG_NONVERBOSE,
2261 "%s URL:%s [%s/%s] -> \"%s\" [%d]\n",
2263 number_to_static_string (hstat.len),
2264 number_to_static_string (hstat.contlen),
2268 total_downloaded_bytes += hstat.len;
2270 /* Remember that we downloaded the file for later ".orig" code. */
2271 if (*dt & ADDED_HTML_EXTENSION)
2272 downloaded_file(FILE_DOWNLOADED_AND_HTML_EXTENSION_ADDED, locf);
2274 downloaded_file(FILE_DOWNLOADED_NORMALLY, locf);
2276 free_hstat (&hstat);
2280 else if (hstat.res == 0) /* No read error */
2282 if (hstat.contlen == -1) /* We don't know how much we were supposed
2283 to get, so assume we succeeded. */
2287 logprintf (LOG_VERBOSE,
2288 _("%s (%s) - `%s' saved [%s]\n\n"),
2290 number_to_static_string (hstat.len));
2291 logprintf (LOG_NONVERBOSE,
2292 "%s URL:%s [%s] -> \"%s\" [%d]\n",
2293 tms, u->url, number_to_static_string (hstat.len),
2297 total_downloaded_bytes += hstat.len;
2299 /* Remember that we downloaded the file for later ".orig" code. */
2300 if (*dt & ADDED_HTML_EXTENSION)
2301 downloaded_file(FILE_DOWNLOADED_AND_HTML_EXTENSION_ADDED, locf);
2303 downloaded_file(FILE_DOWNLOADED_NORMALLY, locf);
2305 free_hstat (&hstat);
2309 else if (hstat.len < hstat.contlen) /* meaning we lost the
2310 connection too soon */
2312 logprintf (LOG_VERBOSE,
2313 _("%s (%s) - Connection closed at byte %s. "),
2314 tms, tmrate, number_to_static_string (hstat.len));
2315 printwhat (count, opt.ntry);
2316 free_hstat (&hstat);
2319 else if (!opt.kill_longer) /* meaning we got more than expected */
2321 logprintf (LOG_VERBOSE,
2322 _("%s (%s) - `%s' saved [%s/%s])\n\n"),
2324 number_to_static_string (hstat.len),
2325 number_to_static_string (hstat.contlen));
2326 logprintf (LOG_NONVERBOSE,
2327 "%s URL:%s [%s/%s] -> \"%s\" [%d]\n",
2329 number_to_static_string (hstat.len),
2330 number_to_static_string (hstat.contlen),
2333 total_downloaded_bytes += hstat.len;
2335 /* Remember that we downloaded the file for later ".orig" code. */
2336 if (*dt & ADDED_HTML_EXTENSION)
2337 downloaded_file(FILE_DOWNLOADED_AND_HTML_EXTENSION_ADDED, locf);
2339 downloaded_file(FILE_DOWNLOADED_NORMALLY, locf);
2341 free_hstat (&hstat);
2345 else /* the same, but not accepted */
2347 logprintf (LOG_VERBOSE,
2348 _("%s (%s) - Connection closed at byte %s/%s. "),
2350 number_to_static_string (hstat.len),
2351 number_to_static_string (hstat.contlen));
2352 printwhat (count, opt.ntry);
2353 free_hstat (&hstat);
2357 else /* now hstat.res can only be -1 */
2359 if (hstat.contlen == -1)
2361 logprintf (LOG_VERBOSE,
2362 _("%s (%s) - Read error at byte %s (%s)."),
2363 tms, tmrate, number_to_static_string (hstat.len),
2365 printwhat (count, opt.ntry);
2366 free_hstat (&hstat);
2369 else /* hstat.res == -1 and contlen is given */
2371 logprintf (LOG_VERBOSE,
2372 _("%s (%s) - Read error at byte %s/%s (%s). "),
2374 number_to_static_string (hstat.len),
2375 number_to_static_string (hstat.contlen),
2377 printwhat (count, opt.ntry);
2378 free_hstat (&hstat);
2385 while (!opt.ntry || (count < opt.ntry));
2389 /* Converts struct tm to time_t, assuming the data in tm is UTC rather
2390 than local timezone.
2392 mktime is similar but assumes struct tm, also known as the
2393 "broken-down" form of time, is in local time zone. mktime_from_utc
2394 uses mktime to make the conversion understanding that an offset
2395 will be introduced by the local time assumption.
2397 mktime_from_utc then measures the introduced offset by applying
2398 gmtime to the initial result and applying mktime to the resulting
2399 "broken-down" form. The difference between the two mktime results
2400 is the measured offset which is then subtracted from the initial
2401 mktime result to yield a calendar time which is the value returned.
2403 tm_isdst in struct tm is set to 0 to force mktime to introduce a
2404 consistent offset (the non DST offset) since tm and tm+o might be
2405 on opposite sides of a DST change.
2407 Some implementations of mktime return -1 for the nonexistent
2408 localtime hour at the beginning of DST. In this event, use
2409 mktime(tm - 1hr) + 3600.
2413 gmtime(t+o) --> tm+o
2414 mktime(tm+o) --> t+2o
2415 t+o - (t+2o - t+o) = t
2417 Note that glibc contains a function of the same purpose named
2418 `timegm' (reverse of gmtime). But obviously, it is not universally
2419 available, and unfortunately it is not straightforwardly
2420 extractable for use here. Perhaps configure should detect timegm
2421 and use it where available.
2423 Contributed by Roger Beeman <beeman@cisco.com>, with the help of
2424 Mark Baushke <mdb@cisco.com> and the rest of the Gurus at CISCO.
2425 Further improved by Roger with assistance from Edward J. Sabol
2426 based on input by Jamie Zawinski. */
2429 mktime_from_utc (struct tm *t)
2440 return -1; /* can't deal with output from strptime */
2451 return -1; /* can't deal with output from gmtime */
2454 return (tl - (tb - tl));
2457 /* Check whether the result of strptime() indicates success.
2458 strptime() returns the pointer to how far it got to in the string.
2459 The processing has been successful if the string is at `GMT' or
2460 `+X', or at the end of the string.
2462 In extended regexp parlance, the function returns 1 if P matches
2463 "^ *(GMT|[+-][0-9]|$)", 0 otherwise. P being NULL (which strptime
2464 can return) is considered a failure and 0 is returned. */
2466 check_end (const char *p)
2470 while (ISSPACE (*p))
2473 || (p[0] == 'G' && p[1] == 'M' && p[2] == 'T')
2474 || ((p[0] == '+' || p[0] == '-') && ISDIGIT (p[1])))
2480 /* Convert the textual specification of time in TIME_STRING to the
2481 number of seconds since the Epoch.
2483 TIME_STRING can be in any of the three formats RFC2068 allows the
2484 HTTP servers to emit -- RFC1123-date, RFC850-date or asctime-date.
2485 Timezones are ignored, and should be GMT.
2487 Return the computed time_t representation, or -1 if the conversion
2490 This function uses strptime with various string formats for parsing
2491 TIME_STRING. This results in a parser that is not as lenient in
2492 interpreting TIME_STRING as I would like it to be. Being based on
2493 strptime, it always allows shortened months, one-digit days, etc.,
2494 but due to the multitude of formats in which time can be
2495 represented, an ideal HTTP time parser would be even more
2496 forgiving. It should completely ignore things like week days and
2497 concentrate only on the various forms of representing years,
2498 months, days, hours, minutes, and seconds. For example, it would
2499 be nice if it accepted ISO 8601 out of the box.
2501 I've investigated free and PD code for this purpose, but none was
2502 usable. getdate was big and unwieldy, and had potential copyright
2503 issues, or so I was informed. Dr. Marcus Hennecke's atotm(),
2504 distributed with phttpd, is excellent, but we cannot use it because
2505 it is not assigned to the FSF. So I stuck it with strptime. */
2508 http_atotm (const char *time_string)
2510 /* NOTE: Solaris strptime man page claims that %n and %t match white
2511 space, but that's not universally available. Instead, we simply
2512 use ` ' to mean "skip all WS", which works under all strptime
2513 implementations I've tested. */
2515 static const char *time_formats[] = {
2516 "%a, %d %b %Y %T", /* RFC1123: Thu, 29 Jan 1998 22:12:57 */
2517 "%A, %d-%b-%y %T", /* RFC850: Thursday, 29-Jan-98 22:12:57 */
2518 "%a, %d-%b-%Y %T", /* pseudo-RFC850: Thu, 29-Jan-1998 22:12:57
2519 (google.com uses this for their cookies.) */
2520 "%a %b %d %T %Y" /* asctime: Thu Jan 29 22:12:57 1998 */
2526 /* According to Roger Beeman, we need to initialize tm_isdst, since
2527 strptime won't do it. */
2530 /* Note that under foreign locales Solaris strptime() fails to
2531 recognize English dates, which renders this function useless. We
2532 solve this by being careful not to affect LC_TIME when
2533 initializing locale.
2535 Another solution would be to temporarily set locale to C, invoke
2536 strptime(), and restore it back. This is slow and dirty,
2537 however, and locale support other than LC_MESSAGES can mess other
2538 things, so I rather chose to stick with just setting LC_MESSAGES.
2540 GNU strptime does not have this problem because it recognizes
2541 both international and local dates. */
2543 for (i = 0; i < countof (time_formats); i++)
2544 if (check_end (strptime (time_string, time_formats[i], &t)))
2545 return mktime_from_utc (&t);
2547 /* All formats have failed. */
2551 /* Authorization support: We support two authorization schemes:
2553 * `Basic' scheme, consisting of base64-ing USER:PASSWORD string;
2555 * `Digest' scheme, added by Junio Hamano <junio@twinsun.com>,
2556 consisting of answering to the server's challenge with the proper
2559 /* How many bytes it will take to store LEN bytes in base64. */
2560 #define BASE64_LENGTH(len) (4 * (((len) + 2) / 3))
2562 /* Encode the string S of length LENGTH to base64 format and place it
2563 to STORE. STORE will be 0-terminated, and must point to a writable
2564 buffer of at least 1+BASE64_LENGTH(length) bytes. */
2566 base64_encode (const char *s, char *store, int length)
2568 /* Conversion table. */
2569 static char tbl[64] = {
2570 'A','B','C','D','E','F','G','H',
2571 'I','J','K','L','M','N','O','P',
2572 'Q','R','S','T','U','V','W','X',
2573 'Y','Z','a','b','c','d','e','f',
2574 'g','h','i','j','k','l','m','n',
2575 'o','p','q','r','s','t','u','v',
2576 'w','x','y','z','0','1','2','3',
2577 '4','5','6','7','8','9','+','/'
2580 unsigned char *p = (unsigned char *)store;
2582 /* Transform the 3x8 bits to 4x6 bits, as required by base64. */
2583 for (i = 0; i < length; i += 3)
2585 *p++ = tbl[s[0] >> 2];
2586 *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)];
2587 *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)];
2588 *p++ = tbl[s[2] & 0x3f];
2591 /* Pad the result if necessary... */
2592 if (i == length + 1)
2594 else if (i == length + 2)
2595 *(p - 1) = *(p - 2) = '=';
2596 /* ...and zero-terminate it. */
2600 /* Create the authentication header contents for the `Basic' scheme.
2601 This is done by encoding the string `USER:PASS' in base64 and
2602 prepending `HEADER: Basic ' to it. */
2604 basic_authentication_encode (const char *user, const char *passwd)
2607 int len1 = strlen (user) + 1 + strlen (passwd);
2608 int len2 = BASE64_LENGTH (len1);
2610 t1 = (char *)alloca (len1 + 1);
2611 sprintf (t1, "%s:%s", user, passwd);
2613 t2 = (char *)alloca (len2 + 1);
2614 base64_encode (t1, t2, len1);
2616 return concat_strings ("Basic ", t2, (char *) 0);
2619 #define SKIP_WS(x) do { \
2620 while (ISSPACE (*(x))) \
2625 /* Parse HTTP `WWW-Authenticate:' header. AU points to the beginning
2626 of a field in such a header. If the field is the one specified by
2627 ATTR_NAME ("realm", "opaque", and "nonce" are used by the current
2628 digest authorization code), extract its value in the (char*)
2629 variable pointed by RET. Returns negative on a malformed header,
2630 or number of bytes that have been parsed by this call. */
2632 extract_header_attr (const char *au, const char *attr_name, char **ret)
2635 const char *cp = au;
2637 if (strncmp (cp, attr_name, strlen (attr_name)) == 0)
2639 cp += strlen (attr_name);
2652 for (ep = cp; *ep && *ep != '\"'; ep++)
2657 *ret = strdupdelim (cp, ep);
2664 /* Dump the hexadecimal representation of HASH to BUF. HASH should be
2665 an array of 16 bytes containing the hash keys, and BUF should be a
2666 buffer of 33 writable characters (32 for hex digits plus one for
2667 zero termination). */
2669 dump_hash (unsigned char *buf, const unsigned char *hash)
2673 for (i = 0; i < MD5_HASHLEN; i++, hash++)
2675 *buf++ = XNUM_TO_digit (*hash >> 4);
2676 *buf++ = XNUM_TO_digit (*hash & 0xf);
2681 /* Take the line apart to find the challenge, and compose a digest
2682 authorization header. See RFC2069 section 2.1.2. */
2684 digest_authentication_encode (const char *au, const char *user,
2685 const char *passwd, const char *method,
2688 static char *realm, *opaque, *nonce;
2693 { "realm", &realm },
2694 { "opaque", &opaque },
2699 realm = opaque = nonce = NULL;
2701 au += 6; /* skip over `Digest' */
2707 for (i = 0; i < countof (options); i++)
2709 int skip = extract_header_attr (au, options[i].name,
2710 options[i].variable);
2714 xfree_null (opaque);
2724 if (i == countof (options))
2726 while (*au && *au != '=')
2734 while (*au && *au != '\"')
2741 while (*au && *au != ',')
2746 if (!realm || !nonce || !user || !passwd || !path || !method)
2749 xfree_null (opaque);
2754 /* Calculate the digest value. */
2756 ALLOCA_MD5_CONTEXT (ctx);
2757 unsigned char hash[MD5_HASHLEN];
2758 unsigned char a1buf[MD5_HASHLEN * 2 + 1], a2buf[MD5_HASHLEN * 2 + 1];
2759 unsigned char response_digest[MD5_HASHLEN * 2 + 1];
2761 /* A1BUF = H(user ":" realm ":" password) */
2763 gen_md5_update ((unsigned char *)user, strlen (user), ctx);
2764 gen_md5_update ((unsigned char *)":", 1, ctx);
2765 gen_md5_update ((unsigned char *)realm, strlen (realm), ctx);
2766 gen_md5_update ((unsigned char *)":", 1, ctx);
2767 gen_md5_update ((unsigned char *)passwd, strlen (passwd), ctx);
2768 gen_md5_finish (ctx, hash);
2769 dump_hash (a1buf, hash);
2771 /* A2BUF = H(method ":" path) */
2773 gen_md5_update ((unsigned char *)method, strlen (method), ctx);
2774 gen_md5_update ((unsigned char *)":", 1, ctx);
2775 gen_md5_update ((unsigned char *)path, strlen (path), ctx);
2776 gen_md5_finish (ctx, hash);
2777 dump_hash (a2buf, hash);
2779 /* RESPONSE_DIGEST = H(A1BUF ":" nonce ":" A2BUF) */
2781 gen_md5_update (a1buf, MD5_HASHLEN * 2, ctx);
2782 gen_md5_update ((unsigned char *)":", 1, ctx);
2783 gen_md5_update ((unsigned char *)nonce, strlen (nonce), ctx);
2784 gen_md5_update ((unsigned char *)":", 1, ctx);
2785 gen_md5_update (a2buf, MD5_HASHLEN * 2, ctx);
2786 gen_md5_finish (ctx, hash);
2787 dump_hash (response_digest, hash);
2789 res = (char*) xmalloc (strlen (user)
2794 + 2 * MD5_HASHLEN /*strlen (response_digest)*/
2795 + (opaque ? strlen (opaque) : 0)
2797 sprintf (res, "Digest \
2798 username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
2799 user, realm, nonce, path, response_digest);
2802 char *p = res + strlen (res);
2803 strcat (p, ", opaque=\"");
2810 #endif /* USE_DIGEST */
2813 #define BEGINS_WITH(line, string_constant) \
2814 (!strncasecmp (line, string_constant, sizeof (string_constant) - 1) \
2815 && (ISSPACE (line[sizeof (string_constant) - 1]) \
2816 || !line[sizeof (string_constant) - 1]))
2819 known_authentication_scheme_p (const char *au)
2821 return BEGINS_WITH (au, "Basic")
2822 || BEGINS_WITH (au, "Digest")
2823 || BEGINS_WITH (au, "NTLM");
2828 /* Create the HTTP authorization request header. When the
2829 `WWW-Authenticate' response header is seen, according to the
2830 authorization scheme specified in that header (`Basic' and `Digest'
2831 are supported by the current implementation), produce an
2832 appropriate HTTP authorization request header. */
2834 create_authorization_line (const char *au, const char *user,
2835 const char *passwd, const char *method,
2838 if (0 == strncasecmp (au, "Basic", 5))
2839 return basic_authentication_encode (user, passwd);
2841 if (0 == strncasecmp (au, "Digest", 6))
2842 return digest_authentication_encode (au, user, passwd, method, path);
2843 #endif /* USE_DIGEST */
2850 if (wget_cookie_jar)
2851 cookie_jar_save (wget_cookie_jar, opt.cookies_output);
2857 xfree_null (pconn.host);
2858 if (wget_cookie_jar)
2859 cookie_jar_delete (wget_cookie_jar);