1999-12-02 02:42:23 -05:00
|
|
|
|
/* HTTP support.
|
2002-04-14 00:22:47 -04:00
|
|
|
|
Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001, 2002
|
2001-05-27 15:35:15 -04:00
|
|
|
|
Free Software Foundation, Inc.
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2001-05-27 15:35:15 -04:00
|
|
|
|
This file is part of GNU Wget.
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2001-05-27 15:35:15 -04:00
|
|
|
|
GNU Wget is free software; you can redistribute it and/or modify
|
1999-12-02 02:42:23 -05:00
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
2003-10-14 19:32:15 -04:00
|
|
|
|
(at your option) any later version.
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2001-05-27 15:35:15 -04:00
|
|
|
|
GNU Wget is distributed in the hope that it will be useful,
|
1999-12-02 02:42:23 -05:00
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2001-05-27 15:35:15 -04:00
|
|
|
|
along with Wget; if not, write to the Free Software
|
2002-05-17 22:16:36 -04:00
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
In addition, as a special exception, the Free Software Foundation
|
|
|
|
|
gives permission to link the code of its release of Wget with the
|
|
|
|
|
OpenSSL project's "OpenSSL" library (or with modified versions of it
|
|
|
|
|
that use the same license as the "OpenSSL" library), and distribute
|
|
|
|
|
the linked executables. You must obey the GNU General Public License
|
|
|
|
|
in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
|
modify this file, you may extend this exception to your version of the
|
|
|
|
|
file, but you are not obligated to do so. If you do not wish to do
|
|
|
|
|
so, delete this exception statement from your version. */
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
|
# include <string.h>
|
|
|
|
|
#else
|
|
|
|
|
# include <strings.h>
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
|
# include <unistd.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#if TIME_WITH_SYS_TIME
|
|
|
|
|
# include <sys/time.h>
|
|
|
|
|
# include <time.h>
|
|
|
|
|
#else
|
|
|
|
|
# if HAVE_SYS_TIME_H
|
|
|
|
|
# include <sys/time.h>
|
|
|
|
|
# else
|
|
|
|
|
# include <time.h>
|
|
|
|
|
# endif
|
|
|
|
|
#endif
|
2003-10-11 09:57:11 -04:00
|
|
|
|
#ifndef errno
|
|
|
|
|
extern int errno;
|
|
|
|
|
#endif
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
#include "wget.h"
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
#include "url.h"
|
|
|
|
|
#include "host.h"
|
|
|
|
|
#include "retr.h"
|
|
|
|
|
#include "connect.h"
|
|
|
|
|
#include "netrc.h"
|
2000-12-06 12:18:14 -05:00
|
|
|
|
#ifdef HAVE_SSL
|
|
|
|
|
# include "gen_sslfunc.h"
|
|
|
|
|
#endif /* HAVE_SSL */
|
2001-04-08 18:25:24 -04:00
|
|
|
|
#include "cookies.h"
|
2001-11-29 13:17:54 -05:00
|
|
|
|
#ifdef USE_DIGEST
|
|
|
|
|
# include "gen-md5.h"
|
|
|
|
|
#endif
|
2003-09-21 18:47:14 -04:00
|
|
|
|
#include "convert.h"
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
extern char *version_string;
|
2003-10-11 09:57:11 -04:00
|
|
|
|
extern LARGE_INT total_downloaded_bytes;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
#ifndef MIN
|
|
|
|
|
# define MIN(x, y) ((x) > (y) ? (y) : (x))
|
|
|
|
|
#endif
|
|
|
|
|
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2001-04-10 12:04:18 -04:00
|
|
|
|
static int cookies_loaded_p;
|
2002-04-20 16:46:38 -04:00
|
|
|
|
struct cookie_jar *wget_cookie_jar;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
#define TEXTHTML_S "text/html"
|
2003-09-21 08:02:57 -04:00
|
|
|
|
#define TEXTXHTML_S "application/xhtml+xml"
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
/* Some status code validation macros: */
|
|
|
|
|
#define H_20X(x) (((x) >= 200) && ((x) < 300))
|
|
|
|
|
#define H_PARTIAL(x) ((x) == HTTP_STATUS_PARTIAL_CONTENTS)
|
2003-10-14 19:32:15 -04:00
|
|
|
|
#define H_REDIRECTED(x) ((x) == HTTP_STATUS_MOVED_PERMANENTLY \
|
|
|
|
|
|| (x) == HTTP_STATUS_MOVED_TEMPORARILY \
|
|
|
|
|
|| (x) == HTTP_STATUS_TEMPORARY_REDIRECT)
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
/* HTTP/1.0 status codes from RFC1945, provided for reference. */
|
|
|
|
|
/* Successful 2xx. */
|
|
|
|
|
#define HTTP_STATUS_OK 200
|
|
|
|
|
#define HTTP_STATUS_CREATED 201
|
|
|
|
|
#define HTTP_STATUS_ACCEPTED 202
|
|
|
|
|
#define HTTP_STATUS_NO_CONTENT 204
|
|
|
|
|
#define HTTP_STATUS_PARTIAL_CONTENTS 206
|
|
|
|
|
|
|
|
|
|
/* Redirection 3xx. */
|
|
|
|
|
#define HTTP_STATUS_MULTIPLE_CHOICES 300
|
|
|
|
|
#define HTTP_STATUS_MOVED_PERMANENTLY 301
|
|
|
|
|
#define HTTP_STATUS_MOVED_TEMPORARILY 302
|
|
|
|
|
#define HTTP_STATUS_NOT_MODIFIED 304
|
2003-10-14 19:32:15 -04:00
|
|
|
|
#define HTTP_STATUS_TEMPORARY_REDIRECT 307
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
/* Client error 4xx. */
|
|
|
|
|
#define HTTP_STATUS_BAD_REQUEST 400
|
|
|
|
|
#define HTTP_STATUS_UNAUTHORIZED 401
|
|
|
|
|
#define HTTP_STATUS_FORBIDDEN 403
|
|
|
|
|
#define HTTP_STATUS_NOT_FOUND 404
|
|
|
|
|
|
|
|
|
|
/* Server errors 5xx. */
|
|
|
|
|
#define HTTP_STATUS_INTERNAL 500
|
|
|
|
|
#define HTTP_STATUS_NOT_IMPLEMENTED 501
|
|
|
|
|
#define HTTP_STATUS_BAD_GATEWAY 502
|
|
|
|
|
#define HTTP_STATUS_UNAVAILABLE 503
|
2003-11-29 13:40:01 -05:00
|
|
|
|
|
|
|
|
|
enum rp {
|
|
|
|
|
rel_none, rel_name, rel_value, rel_both
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct request {
|
|
|
|
|
const char *method;
|
|
|
|
|
char *arg;
|
|
|
|
|
|
|
|
|
|
struct request_header {
|
|
|
|
|
char *name, *value;
|
|
|
|
|
enum rp release_policy;
|
|
|
|
|
} *headers;
|
|
|
|
|
int hcount, hcapacity;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Create a new, empty request. At least request_set_method must be
|
|
|
|
|
called before the request can be used. */
|
|
|
|
|
|
|
|
|
|
static struct request *
|
|
|
|
|
request_new ()
|
|
|
|
|
{
|
|
|
|
|
struct request *req = xnew0 (struct request);
|
|
|
|
|
req->hcapacity = 8;
|
|
|
|
|
req->headers = xnew_array (struct request_header, req->hcapacity);
|
|
|
|
|
return req;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Set the request's method and its arguments. METH should be a
|
|
|
|
|
literal string (or it should outlive the request) because it will
|
|
|
|
|
not be freed. ARG will be freed by request_free. */
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
request_set_method (struct request *req, const char *meth, char *arg)
|
|
|
|
|
{
|
|
|
|
|
req->method = meth;
|
|
|
|
|
req->arg = arg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Return the method string passed with the last call to
|
|
|
|
|
request_set_method. */
|
|
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
|
request_method (const struct request *req)
|
|
|
|
|
{
|
|
|
|
|
return req->method;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Free one header according to the release policy specified with
|
|
|
|
|
request_set_header. */
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
release_header (struct request_header *hdr)
|
|
|
|
|
{
|
|
|
|
|
switch (hdr->release_policy)
|
|
|
|
|
{
|
|
|
|
|
case rel_none:
|
|
|
|
|
break;
|
|
|
|
|
case rel_name:
|
|
|
|
|
xfree (hdr->name);
|
|
|
|
|
break;
|
|
|
|
|
case rel_value:
|
|
|
|
|
xfree (hdr->value);
|
|
|
|
|
break;
|
|
|
|
|
case rel_both:
|
|
|
|
|
xfree (hdr->name);
|
|
|
|
|
xfree (hdr->value);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Set the request named NAME to VALUE. Specifically, this means that
|
|
|
|
|
a "NAME: VALUE\r\n" header line will be used in the request. If a
|
|
|
|
|
header with the same name previously existed in the request, its
|
|
|
|
|
value will be replaced by this one.
|
|
|
|
|
|
|
|
|
|
RELEASE_POLICY determines whether NAME and VALUE should be released
|
|
|
|
|
(freed) with request_free. Allowed values are:
|
|
|
|
|
|
|
|
|
|
- rel_none - don't free NAME or VALUE
|
|
|
|
|
- rel_name - free NAME when done
|
|
|
|
|
- rel_value - free VALUE when done
|
|
|
|
|
- rel_both - free both NAME and VALUE when done
|
|
|
|
|
|
|
|
|
|
Setting release policy is useful when arguments come from different
|
|
|
|
|
sources. For example:
|
|
|
|
|
|
|
|
|
|
// Don't free literal strings!
|
|
|
|
|
request_set_header (req, "Pragma", "no-cache", rel_none);
|
|
|
|
|
|
|
|
|
|
// Don't free a global variable, we'll need it later.
|
|
|
|
|
request_set_header (req, "Referer", opt.referer, rel_none);
|
|
|
|
|
|
|
|
|
|
// Value freshly allocated, free it when done.
|
|
|
|
|
request_set_header (req, "Range", aprintf ("bytes=%ld-", hs->restval),
|
|
|
|
|
rel_value);
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
request_set_header (struct request *req, char *name, char *value,
|
|
|
|
|
enum rp release_policy)
|
|
|
|
|
{
|
|
|
|
|
struct request_header *hdr;
|
|
|
|
|
int i;
|
|
|
|
|
if (!value)
|
|
|
|
|
return;
|
|
|
|
|
for (i = 0; i < req->hcount; i++)
|
|
|
|
|
{
|
|
|
|
|
hdr = &req->headers[i];
|
|
|
|
|
if (0 == strcasecmp (name, hdr->name))
|
|
|
|
|
{
|
|
|
|
|
/* Replace existing header. */
|
|
|
|
|
release_header (hdr);
|
|
|
|
|
hdr->name = name;
|
|
|
|
|
hdr->value = value;
|
|
|
|
|
hdr->release_policy = release_policy;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Install new header. */
|
|
|
|
|
|
|
|
|
|
if (req->hcount >= req->hcount)
|
|
|
|
|
{
|
|
|
|
|
req->hcapacity <<= 1;
|
|
|
|
|
req->headers = xrealloc (req->headers,
|
|
|
|
|
req->hcapacity * sizeof (struct request_header));
|
|
|
|
|
}
|
|
|
|
|
hdr = &req->headers[req->hcount++];
|
|
|
|
|
hdr->name = name;
|
|
|
|
|
hdr->value = value;
|
|
|
|
|
hdr->release_policy = release_policy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Like request_set_header, but sets the whole header line, as
|
|
|
|
|
provided by the user using the `--header' option. For example,
|
|
|
|
|
request_set_user_header (req, "Foo: bar") works just like
|
|
|
|
|
request_set_header (req, "Foo", "bar"). */
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
request_set_user_header (struct request *req, const char *header)
|
|
|
|
|
{
|
|
|
|
|
char *name;
|
|
|
|
|
const char *p = strchr (header, ':');
|
|
|
|
|
if (!p)
|
|
|
|
|
return;
|
|
|
|
|
BOUNDED_TO_ALLOCA (header, p, name);
|
|
|
|
|
++p;
|
|
|
|
|
while (ISSPACE (*p))
|
|
|
|
|
++p;
|
|
|
|
|
request_set_header (req, xstrdup (name), (char *) p, rel_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define APPEND(p, str) do { \
|
|
|
|
|
int A_len = strlen (str); \
|
|
|
|
|
memcpy (p, str, A_len); \
|
|
|
|
|
p += A_len; \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
/* Construct the request and write it to FD using fd_write. */
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
request_send (const struct request *req, int fd)
|
|
|
|
|
{
|
|
|
|
|
char *request_string, *p;
|
|
|
|
|
int i, size, write_error;
|
|
|
|
|
|
|
|
|
|
/* Count the request size. */
|
|
|
|
|
size = 0;
|
|
|
|
|
|
|
|
|
|
/* METHOD " " ARG " " "HTTP/1.0" "\r\n" */
|
|
|
|
|
size += strlen (req->method) + 1 + strlen (req->arg) + 1 + 8 + 2;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < req->hcount; i++)
|
|
|
|
|
{
|
|
|
|
|
struct request_header *hdr = &req->headers[i];
|
|
|
|
|
/* NAME ": " VALUE "\r\n" */
|
|
|
|
|
size += strlen (hdr->name) + 2 + strlen (hdr->value) + 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* "\r\n\0" */
|
|
|
|
|
size += 3;
|
|
|
|
|
|
|
|
|
|
p = request_string = alloca_array (char, size);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2003-11-29 13:40:01 -05:00
|
|
|
|
/* Generate the request. */
|
|
|
|
|
|
|
|
|
|
APPEND (p, req->method); *p++ = ' ';
|
|
|
|
|
APPEND (p, req->arg); *p++ = ' ';
|
|
|
|
|
memcpy (p, "HTTP/1.0\r\n", 10); p += 10;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < req->hcount; i++)
|
|
|
|
|
{
|
|
|
|
|
struct request_header *hdr = &req->headers[i];
|
|
|
|
|
APPEND (p, hdr->name);
|
|
|
|
|
*p++ = ':', *p++ = ' ';
|
|
|
|
|
APPEND (p, hdr->value);
|
|
|
|
|
*p++ = '\r', *p++ = '\n';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*p++ = '\r', *p++ = '\n', *p++ = '\0';
|
|
|
|
|
assert (p - request_string == size);
|
|
|
|
|
|
|
|
|
|
#undef APPEND
|
|
|
|
|
|
|
|
|
|
DEBUGP (("\n---request begin---\n%s---request end---\n", request_string));
|
|
|
|
|
|
|
|
|
|
/* Send the request to the server. */
|
|
|
|
|
|
|
|
|
|
write_error = fd_write (fd, request_string, size - 1, -1);
|
|
|
|
|
if (write_error < 0)
|
|
|
|
|
logprintf (LOG_VERBOSE, _("Failed writing HTTP request: %s.\n"),
|
|
|
|
|
strerror (errno));
|
|
|
|
|
return write_error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Release the resources used by REQ. */
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
request_free (struct request *req)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
xfree_null (req->arg);
|
|
|
|
|
for (i = 0; i < req->hcount; i++)
|
|
|
|
|
release_header (&req->headers[i]);
|
|
|
|
|
xfree_null (req->headers);
|
|
|
|
|
xfree (req);
|
|
|
|
|
}
|
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
static const char *
|
|
|
|
|
head_terminator (const char *hunk, int oldlen, int peeklen)
|
|
|
|
|
{
|
|
|
|
|
const char *start, *end;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
/* If at first peek, verify whether HUNK starts with "HTTP". If
|
|
|
|
|
not, this is a HTTP/0.9 request and we must bail out without
|
|
|
|
|
reading anything. */
|
|
|
|
|
if (oldlen == 0 && 0 != memcmp (hunk, "HTTP", MIN (peeklen, 4)))
|
|
|
|
|
return hunk;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
if (oldlen < 4)
|
|
|
|
|
start = hunk;
|
|
|
|
|
else
|
|
|
|
|
start = hunk + oldlen - 4;
|
|
|
|
|
end = hunk + oldlen + peeklen;
|
|
|
|
|
|
|
|
|
|
for (; start < end - 1; start++)
|
|
|
|
|
if (*start == '\n')
|
|
|
|
|
{
|
|
|
|
|
if (start < end - 2
|
|
|
|
|
&& start[1] == '\r'
|
|
|
|
|
&& start[2] == '\n')
|
|
|
|
|
return start + 3;
|
|
|
|
|
if (start[1] == '\n')
|
|
|
|
|
return start + 2;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Read the HTTP request head from FD and return it. The error
|
|
|
|
|
conditions are the same as with fd_read_hunk.
|
|
|
|
|
|
|
|
|
|
To support HTTP/0.9 responses, this function tries to make sure
|
|
|
|
|
that the data begins with "HTTP". If this is not the case, no data
|
|
|
|
|
is read and an empty request is returned, so that the remaining
|
|
|
|
|
data can be treated as body. */
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
fd_read_http_head (int fd)
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
2003-11-27 18:29:36 -05:00
|
|
|
|
return fd_read_hunk (fd, head_terminator, 512);
|
|
|
|
|
}
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
struct response {
|
|
|
|
|
/* The response data. */
|
|
|
|
|
const char *data;
|
|
|
|
|
|
|
|
|
|
/* The array of pointers that indicate where each header starts.
|
2003-11-29 13:40:01 -05:00
|
|
|
|
For example, given this HTTP response:
|
|
|
|
|
|
|
|
|
|
HTTP/1.0 200 Ok
|
|
|
|
|
Description: some
|
|
|
|
|
text
|
|
|
|
|
Etag: x
|
|
|
|
|
|
|
|
|
|
The headers are located like this:
|
|
|
|
|
|
|
|
|
|
"HTTP/1.0 200 Ok\r\nDescription: some\r\n text\r\nEtag: x\r\n\r\n"
|
|
|
|
|
^ ^ ^ ^
|
|
|
|
|
headers[0] headers[1] headers[2] headers[3]
|
|
|
|
|
|
|
|
|
|
I.e. headers[0] points to the beginning of the request,
|
|
|
|
|
headers[1] points to the end of the first header and the
|
|
|
|
|
beginning of the second one, etc. */
|
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
const char **headers;
|
|
|
|
|
};
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2003-11-29 13:40:01 -05:00
|
|
|
|
/* Create a new response object from the text of the HTTP response,
|
|
|
|
|
available in HEAD. That text is automatically split into
|
|
|
|
|
constituent header lines for fast retrieval using
|
|
|
|
|
response_header_*. */
|
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
static struct response *
|
|
|
|
|
response_new (const char *head)
|
|
|
|
|
{
|
|
|
|
|
const char *hdr;
|
|
|
|
|
int count, size;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
struct response *resp = xnew0 (struct response);
|
|
|
|
|
resp->data = head;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
if (*head == '\0')
|
|
|
|
|
{
|
|
|
|
|
/* Empty head means that we're dealing with a headerless
|
|
|
|
|
(HTTP/0.9) response. In that case, don't set HEADERS at
|
|
|
|
|
all. */
|
|
|
|
|
return resp;
|
|
|
|
|
}
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
/* Split HEAD into header lines, so that response_header_* functions
|
|
|
|
|
don't need to do this over and over again. */
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
size = count = 0;
|
|
|
|
|
hdr = head;
|
|
|
|
|
while (1)
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
2003-11-27 18:29:36 -05:00
|
|
|
|
DO_REALLOC (resp->headers, size, count + 1, const char *);
|
|
|
|
|
resp->headers[count++] = hdr;
|
|
|
|
|
|
|
|
|
|
/* Break upon encountering an empty line. */
|
|
|
|
|
if (!hdr[0] || (hdr[0] == '\r' && hdr[1] == '\n') || hdr[0] == '\n')
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* Find the end of HDR, including continuations. */
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
const char *end = strchr (hdr, '\n');
|
|
|
|
|
if (end)
|
|
|
|
|
hdr = end + 1;
|
|
|
|
|
else
|
|
|
|
|
hdr += strlen (hdr);
|
|
|
|
|
}
|
|
|
|
|
while (*hdr == ' ' || *hdr == '\t');
|
1999-12-02 02:42:23 -05:00
|
|
|
|
}
|
2003-11-27 18:29:36 -05:00
|
|
|
|
DO_REALLOC (resp->headers, size, count + 1, const char *);
|
|
|
|
|
resp->headers[count++] = NULL;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
return resp;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
}
|
2002-04-14 00:22:47 -04:00
|
|
|
|
|
2003-11-29 13:40:01 -05:00
|
|
|
|
/* Locate the header named NAME in the request data. If found, set
|
|
|
|
|
*BEGPTR to its starting, and *ENDPTR to its ending position, and
|
|
|
|
|
return 1. Otherwise return 0.
|
|
|
|
|
|
|
|
|
|
This function is used as a building block for response_header_copy
|
|
|
|
|
and response_header_strdup. */
|
|
|
|
|
|
2002-04-14 00:22:47 -04:00
|
|
|
|
static int
|
2003-11-27 18:29:36 -05:00
|
|
|
|
response_header_bounds (const struct response *resp, const char *name,
|
|
|
|
|
const char **begptr, const char **endptr)
|
2002-04-14 00:22:47 -04:00
|
|
|
|
{
|
2003-11-27 18:29:36 -05:00
|
|
|
|
int i;
|
|
|
|
|
const char **headers = resp->headers;
|
|
|
|
|
int name_len;
|
2002-04-14 00:22:47 -04:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
if (!headers || !headers[1])
|
|
|
|
|
return 0;
|
2002-04-14 00:22:47 -04:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
name_len = strlen (name);
|
|
|
|
|
|
|
|
|
|
for (i = 1; headers[i + 1]; i++)
|
2002-04-14 00:22:47 -04:00
|
|
|
|
{
|
2003-11-27 18:29:36 -05:00
|
|
|
|
const char *b = headers[i];
|
|
|
|
|
const char *e = headers[i + 1];
|
|
|
|
|
if (e - b > name_len
|
|
|
|
|
&& b[name_len] == ':'
|
|
|
|
|
&& 0 == strncasecmp (b, name, name_len))
|
2002-04-14 00:22:47 -04:00
|
|
|
|
{
|
2003-11-27 18:29:36 -05:00
|
|
|
|
b += name_len + 1;
|
|
|
|
|
while (b < e && ISSPACE (*b))
|
|
|
|
|
++b;
|
|
|
|
|
while (b < e && ISSPACE (e[-1]))
|
|
|
|
|
--e;
|
|
|
|
|
*begptr = b;
|
|
|
|
|
*endptr = e;
|
|
|
|
|
return 1;
|
2002-04-14 00:22:47 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2003-11-27 18:29:36 -05:00
|
|
|
|
|
2003-11-29 13:40:01 -05:00
|
|
|
|
/* Copy the response header named NAME to buffer BUF, no longer than
|
|
|
|
|
BUFSIZE (BUFSIZE includes the terminating 0). If the header
|
|
|
|
|
exists, 1 is returned, otherwise 0. If there should be no limit on
|
|
|
|
|
the size of the header, use response_header_strdup instead.
|
|
|
|
|
|
|
|
|
|
If BUFSIZE is 0, no data is copied, but the boolean indication of
|
|
|
|
|
whether the header is present is still returned. */
|
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
static int
|
|
|
|
|
response_header_copy (const struct response *resp, const char *name,
|
|
|
|
|
char *buf, int bufsize)
|
2003-11-20 20:48:11 -05:00
|
|
|
|
{
|
2003-11-27 18:29:36 -05:00
|
|
|
|
const char *b, *e;
|
|
|
|
|
if (!response_header_bounds (resp, name, &b, &e))
|
|
|
|
|
return 0;
|
|
|
|
|
if (bufsize)
|
2003-11-20 20:48:11 -05:00
|
|
|
|
{
|
2003-11-27 18:29:36 -05:00
|
|
|
|
int len = MIN (e - b, bufsize);
|
|
|
|
|
strncpy (buf, b, len);
|
|
|
|
|
buf[len] = '\0';
|
2003-11-20 20:48:11 -05:00
|
|
|
|
}
|
2003-11-27 18:29:36 -05:00
|
|
|
|
return 1;
|
2003-11-20 20:48:11 -05:00
|
|
|
|
}
|
|
|
|
|
|
2003-11-29 13:40:01 -05:00
|
|
|
|
/* Return the value of header named NAME in RESP, allocated with
|
|
|
|
|
malloc. If such a header does not exist in RESP, return NULL. */
|
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
static char *
|
|
|
|
|
response_header_strdup (const struct response *resp, const char *name)
|
2003-11-21 03:48:45 -05:00
|
|
|
|
{
|
2003-11-27 18:29:36 -05:00
|
|
|
|
const char *b, *e;
|
|
|
|
|
if (!response_header_bounds (resp, name, &b, &e))
|
|
|
|
|
return NULL;
|
|
|
|
|
return strdupdelim (b, e);
|
2003-11-21 03:48:45 -05:00
|
|
|
|
}
|
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
/* Parse the HTTP status line, which is of format:
|
2003-11-21 03:48:45 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
HTTP-Version SP Status-Code SP Reason-Phrase
|
2003-11-21 03:48:45 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
The function returns the status-code, or -1 if the status line
|
|
|
|
|
appears malformed. The pointer to "reason-phrase" message is
|
|
|
|
|
returned in *MESSAGE. */
|
2003-11-21 03:48:45 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
static int
|
|
|
|
|
response_status (const struct response *resp, char **message)
|
2003-11-21 03:48:45 -05:00
|
|
|
|
{
|
2003-11-27 18:29:36 -05:00
|
|
|
|
int status;
|
|
|
|
|
const char *p, *end;
|
2003-11-21 03:48:45 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
if (!resp->headers)
|
|
|
|
|
{
|
2003-11-29 17:19:46 -05:00
|
|
|
|
/* For a HTTP/0.9 response, assume status 200. */
|
2003-11-27 18:29:36 -05:00
|
|
|
|
if (message)
|
2003-11-29 13:40:01 -05:00
|
|
|
|
*message = xstrdup (_("No headers, assuming HTTP/0.9"));
|
2003-11-27 18:29:36 -05:00
|
|
|
|
return 200;
|
|
|
|
|
}
|
2003-11-21 03:48:45 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
p = resp->headers[0];
|
|
|
|
|
end = resp->headers[1];
|
2003-11-21 03:48:45 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
if (!end)
|
|
|
|
|
return -1;
|
2003-11-21 03:48:45 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
/* "HTTP" */
|
|
|
|
|
if (end - p < 4 || 0 != strncmp (p, "HTTP", 4))
|
|
|
|
|
return -1;
|
|
|
|
|
p += 4;
|
2003-11-21 03:48:45 -05:00
|
|
|
|
|
2003-11-29 17:19:46 -05:00
|
|
|
|
/* Match the HTTP version. This is optional because Gnutella
|
|
|
|
|
servers have been reported to not specify HTTP version. */
|
2003-11-27 18:29:36 -05:00
|
|
|
|
if (p < end && *p == '/')
|
|
|
|
|
{
|
|
|
|
|
++p;
|
|
|
|
|
while (p < end && ISDIGIT (*p))
|
|
|
|
|
++p;
|
|
|
|
|
if (p < end && *p == '.')
|
|
|
|
|
++p;
|
|
|
|
|
while (p < end && ISDIGIT (*p))
|
|
|
|
|
++p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (p < end && ISSPACE (*p))
|
|
|
|
|
++p;
|
|
|
|
|
if (end - p < 3 || !ISDIGIT (p[0]) || !ISDIGIT (p[1]) || !ISDIGIT (p[2]))
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
status = 100 * (p[0] - '0') + 10 * (p[1] - '0') + (p[2] - '0');
|
|
|
|
|
p += 3;
|
|
|
|
|
|
|
|
|
|
if (message)
|
|
|
|
|
{
|
|
|
|
|
while (p < end && ISSPACE (*p))
|
|
|
|
|
++p;
|
|
|
|
|
while (p < end && ISSPACE (end[-1]))
|
|
|
|
|
--end;
|
|
|
|
|
*message = strdupdelim (p, end);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return status;
|
2003-11-21 03:48:45 -05:00
|
|
|
|
}
|
|
|
|
|
|
2003-11-29 13:40:01 -05:00
|
|
|
|
/* Release the resources used by RESP. */
|
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
static void
|
|
|
|
|
response_free (struct response *resp)
|
2003-11-21 03:48:45 -05:00
|
|
|
|
{
|
2003-11-27 18:29:36 -05:00
|
|
|
|
xfree_null (resp->headers);
|
|
|
|
|
xfree (resp);
|
2003-11-21 03:48:45 -05:00
|
|
|
|
}
|
|
|
|
|
|
2003-11-29 13:40:01 -05:00
|
|
|
|
/* Print [b, e) to the log, omitting the trailing CRLF. */
|
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
static void
|
2003-11-29 13:40:01 -05:00
|
|
|
|
print_server_response_1 (const char *prefix, const char *b, const char *e)
|
2003-11-21 03:48:45 -05:00
|
|
|
|
{
|
2003-11-27 18:29:36 -05:00
|
|
|
|
char *ln;
|
|
|
|
|
if (b < e && e[-1] == '\n')
|
|
|
|
|
--e;
|
|
|
|
|
if (b < e && e[-1] == '\r')
|
|
|
|
|
--e;
|
|
|
|
|
BOUNDED_TO_ALLOCA (b, e, ln);
|
2003-11-29 13:40:01 -05:00
|
|
|
|
logprintf (LOG_VERBOSE, "%s%s\n", prefix, ln);
|
2003-11-21 03:48:45 -05:00
|
|
|
|
}
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2003-11-29 13:40:01 -05:00
|
|
|
|
/* Print the server response, line by line, omitting the trailing CR
|
|
|
|
|
characters, prefixed with PREFIX. */
|
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
static void
|
2003-11-29 13:40:01 -05:00
|
|
|
|
print_server_response (const struct response *resp, const char *prefix)
|
2003-11-27 18:29:36 -05:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
if (!resp->headers)
|
|
|
|
|
return;
|
|
|
|
|
for (i = 0; resp->headers[i + 1]; i++)
|
2003-11-29 13:40:01 -05:00
|
|
|
|
print_server_response_1 (prefix, resp->headers[i], resp->headers[i + 1]);
|
2003-11-27 18:29:36 -05:00
|
|
|
|
}
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
/* Parse the `Content-Range' header and extract the information it
|
|
|
|
|
contains. Returns 1 if successful, -1 otherwise. */
|
|
|
|
|
static int
|
2003-11-27 18:29:36 -05:00
|
|
|
|
parse_content_range (const char *hdr, long *first_byte_ptr,
|
|
|
|
|
long *last_byte_ptr, long *entity_length_ptr)
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
|
|
|
|
long num;
|
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
/* Ancient versions of Netscape proxy server, presumably predating
|
|
|
|
|
rfc2068, sent out `Content-Range' without the "bytes"
|
|
|
|
|
specifier. */
|
1999-12-02 02:42:23 -05:00
|
|
|
|
if (!strncasecmp (hdr, "bytes", 5))
|
|
|
|
|
{
|
|
|
|
|
hdr += 5;
|
2001-11-29 09:03:33 -05:00
|
|
|
|
/* "JavaWebServer/1.1.1" sends "bytes: x-y/z", contrary to the
|
|
|
|
|
HTTP spec. */
|
|
|
|
|
if (*hdr == ':')
|
|
|
|
|
++hdr;
|
2003-11-27 18:29:36 -05:00
|
|
|
|
while (ISSPACE (*hdr))
|
|
|
|
|
++hdr;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
if (!*hdr)
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (!ISDIGIT (*hdr))
|
|
|
|
|
return 0;
|
|
|
|
|
for (num = 0; ISDIGIT (*hdr); hdr++)
|
|
|
|
|
num = 10 * num + (*hdr - '0');
|
|
|
|
|
if (*hdr != '-' || !ISDIGIT (*(hdr + 1)))
|
|
|
|
|
return 0;
|
2003-11-27 18:29:36 -05:00
|
|
|
|
*first_byte_ptr = num;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
++hdr;
|
|
|
|
|
for (num = 0; ISDIGIT (*hdr); hdr++)
|
|
|
|
|
num = 10 * num + (*hdr - '0');
|
|
|
|
|
if (*hdr != '/' || !ISDIGIT (*(hdr + 1)))
|
|
|
|
|
return 0;
|
2003-11-27 18:29:36 -05:00
|
|
|
|
*last_byte_ptr = num;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
++hdr;
|
|
|
|
|
for (num = 0; ISDIGIT (*hdr); hdr++)
|
|
|
|
|
num = 10 * num + (*hdr - '0');
|
2003-11-27 18:29:36 -05:00
|
|
|
|
*entity_length_ptr = num;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
2003-11-27 18:29:36 -05:00
|
|
|
|
|
|
|
|
|
/* Send the contents of FILE_NAME to SOCK/SSL. Make sure that exactly
|
|
|
|
|
PROMISED_SIZE bytes are sent over the wire -- if the file is
|
|
|
|
|
longer, read only that much; if the file is shorter, report an error. */
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2000-11-19 15:50:10 -05:00
|
|
|
|
static int
|
2003-11-27 18:29:36 -05:00
|
|
|
|
post_file (int sock, const char *file_name, long promised_size)
|
2000-11-19 15:50:10 -05:00
|
|
|
|
{
|
2003-11-27 18:29:36 -05:00
|
|
|
|
static char chunk[8192];
|
|
|
|
|
long written = 0;
|
|
|
|
|
int write_error;
|
|
|
|
|
FILE *fp;
|
2002-04-20 16:46:38 -04:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
DEBUGP (("[writing POST file %s ... ", file_name));
|
2002-04-20 16:46:38 -04:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
fp = fopen (file_name, "rb");
|
|
|
|
|
if (!fp)
|
|
|
|
|
return -1;
|
|
|
|
|
while (!feof (fp) && written < promised_size)
|
|
|
|
|
{
|
|
|
|
|
int towrite;
|
|
|
|
|
int length = fread (chunk, 1, sizeof (chunk), fp);
|
|
|
|
|
if (length == 0)
|
|
|
|
|
break;
|
|
|
|
|
towrite = MIN (promised_size - written, length);
|
|
|
|
|
write_error = fd_write (sock, chunk, towrite, -1);
|
|
|
|
|
if (write_error < 0)
|
|
|
|
|
{
|
|
|
|
|
fclose (fp);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
written += towrite;
|
|
|
|
|
}
|
|
|
|
|
fclose (fp);
|
2002-04-20 16:46:38 -04:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
/* If we've written less than was promised, report a (probably
|
|
|
|
|
nonsensical) error rather than break the promise. */
|
|
|
|
|
if (written < promised_size)
|
|
|
|
|
{
|
|
|
|
|
errno = EINVAL;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2002-04-20 16:46:38 -04:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
assert (written == promised_size);
|
|
|
|
|
DEBUGP (("done]\n"));
|
|
|
|
|
return 0;
|
2002-04-20 16:46:38 -04:00
|
|
|
|
}
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
2000-11-20 05:20:39 -05:00
|
|
|
|
/* Persistent connections. Currently, we cache the most recently used
|
|
|
|
|
connection as persistent, provided that the HTTP server agrees to
|
|
|
|
|
make it such. The persistence data is stored in the variables
|
2003-11-07 21:57:51 -05:00
|
|
|
|
below. Ideally, it should be possible to cache an arbitrary fixed
|
|
|
|
|
number of these connections. */
|
2000-11-19 18:42:13 -05:00
|
|
|
|
|
2000-11-20 05:20:39 -05:00
|
|
|
|
/* Whether a persistent connection is active. */
|
2003-11-07 21:57:51 -05:00
|
|
|
|
static int pconn_active;
|
2003-11-05 20:12:03 -05:00
|
|
|
|
|
2003-11-07 21:57:51 -05:00
|
|
|
|
static struct {
|
|
|
|
|
/* The socket of the connection. */
|
|
|
|
|
int socket;
|
2000-11-19 18:42:13 -05:00
|
|
|
|
|
2003-11-07 21:57:51 -05:00
|
|
|
|
/* Host and port of the currently active persistent connection. */
|
|
|
|
|
char *host;
|
|
|
|
|
int port;
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
2003-11-07 21:57:51 -05:00
|
|
|
|
/* Whether a ssl handshake has occoured on this connection. */
|
|
|
|
|
int ssl;
|
|
|
|
|
} pconn;
|
2000-12-05 18:09:41 -05:00
|
|
|
|
|
2003-11-07 21:57:51 -05:00
|
|
|
|
/* Mark the persistent connection as invalid and free the resources it
|
|
|
|
|
uses. This is used by the CLOSE_* macros after they forcefully
|
|
|
|
|
close a registered persistent connection. */
|
2000-11-19 18:42:13 -05:00
|
|
|
|
|
2000-11-19 15:50:10 -05:00
|
|
|
|
static void
|
2000-11-19 18:42:13 -05:00
|
|
|
|
invalidate_persistent (void)
|
2000-11-19 15:50:10 -05:00
|
|
|
|
{
|
2003-11-07 21:57:51 -05:00
|
|
|
|
DEBUGP (("Disabling further reuse of socket %d.\n", pconn.socket));
|
|
|
|
|
pconn_active = 0;
|
2003-11-20 10:19:59 -05:00
|
|
|
|
fd_close (pconn.socket);
|
2003-11-07 21:57:51 -05:00
|
|
|
|
xfree (pconn.host);
|
|
|
|
|
xzero (pconn);
|
2000-11-19 15:50:10 -05:00
|
|
|
|
}
|
|
|
|
|
|
2000-11-19 18:42:13 -05:00
|
|
|
|
/* Register FD, which should be a TCP/IP connection to HOST:PORT, as
|
|
|
|
|
persistent. This will enable someone to use the same connection
|
|
|
|
|
later. In the context of HTTP, this must be called only AFTER the
|
|
|
|
|
response has been received and the server has promised that the
|
|
|
|
|
connection will remain alive.
|
|
|
|
|
|
|
|
|
|
If a previous connection was persistent, it is closed. */
|
|
|
|
|
|
2002-04-11 13:49:32 -04:00
|
|
|
|
static void
|
2003-11-07 21:57:51 -05:00
|
|
|
|
register_persistent (const char *host, int port, int fd, int ssl)
|
2000-11-19 15:50:10 -05:00
|
|
|
|
{
|
2003-11-07 21:57:51 -05:00
|
|
|
|
if (pconn_active)
|
2000-11-19 18:42:13 -05:00
|
|
|
|
{
|
2003-11-07 21:57:51 -05:00
|
|
|
|
if (pconn.socket == fd)
|
2000-11-19 18:42:13 -05:00
|
|
|
|
{
|
2003-11-07 21:57:51 -05:00
|
|
|
|
/* The connection FD is already registered. */
|
2000-11-19 18:42:13 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2003-11-07 21:57:51 -05:00
|
|
|
|
/* The old persistent connection is still active; close it
|
|
|
|
|
first. This situation arises whenever a persistent
|
|
|
|
|
connection exists, but we then connect to a different
|
|
|
|
|
host, and try to register a persistent connection to that
|
|
|
|
|
one. */
|
2000-11-19 18:42:13 -05:00
|
|
|
|
invalidate_persistent ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-11-07 21:57:51 -05:00
|
|
|
|
pconn_active = 1;
|
|
|
|
|
pconn.socket = fd;
|
|
|
|
|
pconn.host = xstrdup (host);
|
|
|
|
|
pconn.port = port;
|
|
|
|
|
pconn.ssl = ssl;
|
2001-11-26 00:36:33 -05:00
|
|
|
|
|
2003-11-07 21:57:51 -05:00
|
|
|
|
DEBUGP (("Registered socket %d for persistent reuse.\n", fd));
|
2000-11-19 15:50:10 -05:00
|
|
|
|
}
|
|
|
|
|
|
2000-11-19 18:42:13 -05:00
|
|
|
|
/* Return non-zero if a persistent connection is available for
|
|
|
|
|
connecting to HOST:PORT. */
|
|
|
|
|
|
2002-04-11 13:49:32 -04:00
|
|
|
|
static int
|
2003-11-07 21:57:51 -05:00
|
|
|
|
persistent_available_p (const char *host, int port, int ssl,
|
|
|
|
|
int *host_lookup_failed)
|
2000-11-19 15:50:10 -05:00
|
|
|
|
{
|
2000-11-20 05:20:39 -05:00
|
|
|
|
/* First, check whether a persistent connection is active at all. */
|
2003-11-07 21:57:51 -05:00
|
|
|
|
if (!pconn_active)
|
2000-11-19 15:50:10 -05:00
|
|
|
|
return 0;
|
2001-11-26 00:36:33 -05:00
|
|
|
|
|
2003-11-07 21:57:51 -05:00
|
|
|
|
/* If we want SSL and the last connection wasn't or vice versa,
|
|
|
|
|
don't use it. Checking for host and port is not enough because
|
|
|
|
|
HTTP and HTTPS can apparently coexist on the same port. */
|
|
|
|
|
if (ssl != pconn.ssl)
|
2000-12-05 18:35:56 -05:00
|
|
|
|
return 0;
|
2001-11-26 00:36:33 -05:00
|
|
|
|
|
2003-11-07 21:57:51 -05:00
|
|
|
|
/* If we're not connecting to the same port, we're not interested. */
|
|
|
|
|
if (port != pconn.port)
|
2000-11-19 15:50:10 -05:00
|
|
|
|
return 0;
|
2001-11-26 00:36:33 -05:00
|
|
|
|
|
2003-11-07 21:57:51 -05:00
|
|
|
|
/* If the host is the same, we're in business. If not, there is
|
|
|
|
|
still hope -- read below. */
|
|
|
|
|
if (0 != strcasecmp (host, pconn.host))
|
|
|
|
|
{
|
2003-11-13 17:48:03 -05:00
|
|
|
|
/* If pconn.socket is already talking to HOST, we needn't
|
|
|
|
|
reconnect. This happens often when both sites are virtual
|
|
|
|
|
hosts distinguished only by name and served by the same
|
|
|
|
|
network interface, and hence the same web server (possibly
|
|
|
|
|
set up by the ISP and serving many different web sites).
|
|
|
|
|
This admittedly non-standard optimization does not contradict
|
|
|
|
|
HTTP and works well with popular server software. */
|
|
|
|
|
|
2003-11-07 21:57:51 -05:00
|
|
|
|
int found;
|
|
|
|
|
ip_address ip;
|
|
|
|
|
struct address_list *al;
|
|
|
|
|
|
2003-11-13 17:48:03 -05:00
|
|
|
|
if (ssl)
|
|
|
|
|
/* Don't try to talk to two different SSL sites over the same
|
|
|
|
|
secure connection! (Besides, it's not clear if name-based
|
|
|
|
|
virtual hosting is even possible with SSL.) */
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
/* If pconn.socket's peer is one of the IP addresses HOST
|
|
|
|
|
resolves to, pconn.socket is for all intents and purposes
|
|
|
|
|
already talking to HOST. */
|
|
|
|
|
|
2003-11-14 21:03:23 -05:00
|
|
|
|
if (!socket_ip_address (pconn.socket, &ip, ENDPOINT_PEER))
|
2003-11-07 21:57:51 -05:00
|
|
|
|
{
|
2003-11-13 17:48:03 -05:00
|
|
|
|
/* Can't get the peer's address -- something must be very
|
|
|
|
|
wrong with the connection. */
|
2003-11-07 21:57:51 -05:00
|
|
|
|
invalidate_persistent ();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
al = lookup_host (host, 0);
|
|
|
|
|
if (!al)
|
|
|
|
|
{
|
|
|
|
|
*host_lookup_failed = 1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2003-11-18 17:18:44 -05:00
|
|
|
|
found = address_list_contains (al, &ip);
|
2003-11-07 21:57:51 -05:00
|
|
|
|
address_list_release (al);
|
|
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2003-11-13 17:48:03 -05:00
|
|
|
|
/* The persistent connection's peer address was found among the
|
|
|
|
|
addresses HOST resolved to; therefore, pconn.sock is in fact
|
|
|
|
|
already talking to HOST -- no need to reconnect. */
|
2003-11-07 21:57:51 -05:00
|
|
|
|
}
|
2001-11-26 00:36:33 -05:00
|
|
|
|
|
2003-11-07 21:57:51 -05:00
|
|
|
|
/* Finally, check whether the connection is still open. This is
|
2000-11-20 05:20:39 -05:00
|
|
|
|
important because most server implement a liberal (short) timeout
|
|
|
|
|
on persistent connections. Wget can of course always reconnect
|
|
|
|
|
if the connection doesn't work out, but it's nicer to know in
|
|
|
|
|
advance. This test is a logical followup of the first test, but
|
|
|
|
|
is "expensive" and therefore placed at the end of the list. */
|
2003-11-07 21:57:51 -05:00
|
|
|
|
|
|
|
|
|
if (!test_socket_open (pconn.socket))
|
2000-11-19 15:50:10 -05:00
|
|
|
|
{
|
2000-11-20 05:20:39 -05:00
|
|
|
|
/* Oops, the socket is no longer open. Now that we know that,
|
|
|
|
|
let's invalidate the persistent connection before returning
|
|
|
|
|
0. */
|
2000-11-19 15:50:10 -05:00
|
|
|
|
invalidate_persistent ();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2003-11-07 21:57:51 -05:00
|
|
|
|
|
2000-11-19 15:50:10 -05:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* The idea behind these two CLOSE macros is to distinguish between
|
|
|
|
|
two cases: one when the job we've been doing is finished, and we
|
|
|
|
|
want to close the connection and leave, and two when something is
|
|
|
|
|
seriously wrong and we're closing the connection as part of
|
|
|
|
|
cleanup.
|
|
|
|
|
|
|
|
|
|
In case of keep_alive, CLOSE_FINISH should leave the connection
|
|
|
|
|
open, while CLOSE_INVALIDATE should still close it.
|
|
|
|
|
|
2000-11-19 18:42:13 -05:00
|
|
|
|
Note that the semantics of the flag `keep_alive' is "this
|
|
|
|
|
connection *will* be reused (the server has promised not to close
|
|
|
|
|
the connection once we're done)", while the semantics of
|
|
|
|
|
`pc_active_p && (fd) == pc_last_fd' is "we're *now* using an
|
|
|
|
|
active, registered connection". */
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
|
|
|
|
#define CLOSE_FINISH(fd) do { \
|
|
|
|
|
if (!keep_alive) \
|
|
|
|
|
{ \
|
2003-11-07 21:57:51 -05:00
|
|
|
|
if (pconn_active && (fd) == pconn.socket) \
|
2000-11-19 15:50:10 -05:00
|
|
|
|
invalidate_persistent (); \
|
2003-11-07 21:57:51 -05:00
|
|
|
|
else \
|
2003-11-20 10:19:59 -05:00
|
|
|
|
fd_close (fd); \
|
2000-11-19 15:50:10 -05:00
|
|
|
|
} \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
#define CLOSE_INVALIDATE(fd) do { \
|
2003-11-07 21:57:51 -05:00
|
|
|
|
if (pconn_active && (fd) == pconn.socket) \
|
2000-11-19 15:50:10 -05:00
|
|
|
|
invalidate_persistent (); \
|
2003-11-07 21:57:51 -05:00
|
|
|
|
else \
|
2003-11-20 10:19:59 -05:00
|
|
|
|
fd_close (fd); \
|
2003-11-29 13:40:01 -05:00
|
|
|
|
fd = -1; \
|
2000-11-19 15:50:10 -05:00
|
|
|
|
} while (0)
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
struct http_stat
|
|
|
|
|
{
|
|
|
|
|
long len; /* received length */
|
|
|
|
|
long contlen; /* expected length */
|
|
|
|
|
long restval; /* the restart value */
|
|
|
|
|
int res; /* the result of last read */
|
|
|
|
|
char *newloc; /* new location (redirection) */
|
|
|
|
|
char *remote_time; /* remote time-stamp string */
|
|
|
|
|
char *error; /* textual HTTP error */
|
|
|
|
|
int statcode; /* status code */
|
2003-09-15 17:14:15 -04:00
|
|
|
|
double dltime; /* time of the download in msecs */
|
2001-04-01 16:04:21 -04:00
|
|
|
|
int no_truncate; /* whether truncating the file is
|
|
|
|
|
forbidden. */
|
2001-11-21 19:24:28 -05:00
|
|
|
|
const char *referer; /* value of the referer header. */
|
|
|
|
|
char **local_file; /* local file. */
|
1999-12-02 02:42:23 -05:00
|
|
|
|
};
|
|
|
|
|
|
2001-11-21 19:24:28 -05:00
|
|
|
|
static void
|
|
|
|
|
free_hstat (struct http_stat *hs)
|
|
|
|
|
{
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (hs->newloc);
|
|
|
|
|
xfree_null (hs->remote_time);
|
|
|
|
|
xfree_null (hs->error);
|
2001-11-21 19:24:28 -05:00
|
|
|
|
|
|
|
|
|
/* Guard against being called twice. */
|
|
|
|
|
hs->newloc = NULL;
|
|
|
|
|
hs->remote_time = NULL;
|
|
|
|
|
hs->error = NULL;
|
|
|
|
|
}
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
static char *create_authorization_line PARAMS ((const char *, const char *,
|
|
|
|
|
const char *, const char *,
|
|
|
|
|
const char *));
|
2003-11-29 13:40:01 -05:00
|
|
|
|
static char *basic_authentication_encode PARAMS ((const char *, const char *));
|
1999-12-02 02:42:23 -05:00
|
|
|
|
static int known_authentication_scheme_p PARAMS ((const char *));
|
|
|
|
|
|
2002-04-20 16:46:38 -04:00
|
|
|
|
time_t http_atotm PARAMS ((const char *));
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2000-11-19 21:10:54 -05:00
|
|
|
|
#define BEGINS_WITH(line, string_constant) \
|
|
|
|
|
(!strncasecmp (line, string_constant, sizeof (string_constant) - 1) \
|
|
|
|
|
&& (ISSPACE (line[sizeof (string_constant) - 1]) \
|
|
|
|
|
|| !line[sizeof (string_constant) - 1]))
|
|
|
|
|
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* Retrieve a document through HTTP protocol. It recognizes status
|
|
|
|
|
code, and correctly handles redirections. It closes the network
|
|
|
|
|
socket. If it receives an error from the functions below it, it
|
|
|
|
|
will print it if there is enough information to do so (almost
|
|
|
|
|
always), returning the error to the caller (i.e. http_loop).
|
|
|
|
|
|
2003-11-18 17:18:44 -05:00
|
|
|
|
Various HTTP parameters are stored to hs.
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2001-11-21 19:24:28 -05:00
|
|
|
|
If PROXY is non-NULL, the connection will be made to the proxy
|
|
|
|
|
server, and u->url will be requested. */
|
1999-12-02 02:42:23 -05:00
|
|
|
|
static uerr_t
|
2001-11-21 19:24:28 -05:00
|
|
|
|
gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
2003-11-29 13:40:01 -05:00
|
|
|
|
struct request *req;
|
|
|
|
|
|
|
|
|
|
char *type;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
char *user, *passwd;
|
|
|
|
|
char *proxyauth;
|
2003-11-29 13:40:01 -05:00
|
|
|
|
int statcode;
|
2002-04-14 00:22:47 -04:00
|
|
|
|
int write_error;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
long contlen, contrange;
|
2001-11-21 19:24:28 -05:00
|
|
|
|
struct url *conn;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
FILE *fp;
|
2003-11-29 13:40:01 -05:00
|
|
|
|
|
|
|
|
|
int sock = -1;
|
|
|
|
|
|
|
|
|
|
/* Whether authorization has been already tried. */
|
|
|
|
|
int auth_tried_already = 0;
|
|
|
|
|
|
|
|
|
|
/* Whether our connection to the remote host is through SSL. */
|
2003-11-05 20:12:03 -05:00
|
|
|
|
int using_ssl = 0;
|
2000-11-19 18:42:13 -05:00
|
|
|
|
|
2003-11-20 20:48:11 -05:00
|
|
|
|
char *head;
|
2003-11-27 18:29:36 -05:00
|
|
|
|
struct response *resp;
|
|
|
|
|
char hdrval[256];
|
|
|
|
|
char *message;
|
2003-11-20 20:48:11 -05:00
|
|
|
|
|
2000-11-19 18:42:13 -05:00
|
|
|
|
/* Whether this connection will be kept alive after the HTTP request
|
|
|
|
|
is done. */
|
|
|
|
|
int keep_alive;
|
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
/* Flag that detects having received a keep-alive response. */
|
|
|
|
|
int keep_alive_confirmed;
|
2000-11-19 18:42:13 -05:00
|
|
|
|
|
|
|
|
|
/* Whether keep-alive should be inhibited. */
|
2003-11-29 13:40:01 -05:00
|
|
|
|
int inhibit_keep_alive = !opt.http_keep_alive;
|
2002-01-26 18:19:10 -05:00
|
|
|
|
|
2002-04-14 00:22:47 -04:00
|
|
|
|
/* Headers sent when using POST. */
|
2002-04-14 01:19:27 -04:00
|
|
|
|
long post_data_size = 0;
|
2002-04-14 00:22:47 -04:00
|
|
|
|
|
2003-11-29 13:40:01 -05:00
|
|
|
|
int host_lookup_failed = 0;
|
2003-11-07 21:57:51 -05:00
|
|
|
|
|
2000-12-05 18:09:41 -05:00
|
|
|
|
#ifdef HAVE_SSL
|
2003-11-29 17:24:55 -05:00
|
|
|
|
if (u->scheme == SCHEME_HTTPS)
|
2001-02-10 17:33:31 -05:00
|
|
|
|
{
|
2003-11-29 17:24:55 -05:00
|
|
|
|
/* Initialize the SSL context. After this has once been done,
|
|
|
|
|
it becomes a no-op. */
|
|
|
|
|
switch (ssl_init ())
|
|
|
|
|
{
|
|
|
|
|
case SSLERRCTXCREATE:
|
|
|
|
|
/* this is fatal */
|
|
|
|
|
logprintf (LOG_NOTQUIET, _("Failed to set up an SSL context\n"));
|
|
|
|
|
return SSLERRCTXCREATE;
|
|
|
|
|
case SSLERRCERTFILE:
|
|
|
|
|
/* try without certfile */
|
|
|
|
|
logprintf (LOG_NOTQUIET,
|
|
|
|
|
_("Failed to load certificates from %s\n"),
|
|
|
|
|
opt.sslcertfile);
|
|
|
|
|
logprintf (LOG_NOTQUIET,
|
|
|
|
|
_("Trying without the specified certificate\n"));
|
|
|
|
|
break;
|
|
|
|
|
case SSLERRCERTKEY:
|
|
|
|
|
logprintf (LOG_NOTQUIET,
|
|
|
|
|
_("Failed to get certificate key from %s\n"),
|
|
|
|
|
opt.sslcertkey);
|
|
|
|
|
logprintf (LOG_NOTQUIET,
|
|
|
|
|
_("Trying without the specified certificate\n"));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2001-02-10 17:33:31 -05:00
|
|
|
|
}
|
2000-12-05 18:09:41 -05:00
|
|
|
|
#endif /* HAVE_SSL */
|
|
|
|
|
|
1999-12-02 02:42:23 -05:00
|
|
|
|
if (!(*dt & HEAD_ONLY))
|
2000-10-20 01:55:46 -04:00
|
|
|
|
/* If we're doing a GET on the URL, as opposed to just a HEAD, we need to
|
|
|
|
|
know the local filename so we can save to it. */
|
2001-11-21 19:24:28 -05:00
|
|
|
|
assert (*hs->local_file != NULL);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
auth_tried_already = 0;
|
|
|
|
|
|
2000-10-20 01:55:46 -04:00
|
|
|
|
/* Initialize certain elements of struct http_stat. */
|
1999-12-02 02:42:23 -05:00
|
|
|
|
hs->len = 0L;
|
|
|
|
|
hs->contlen = -1;
|
|
|
|
|
hs->res = -1;
|
|
|
|
|
hs->newloc = NULL;
|
|
|
|
|
hs->remote_time = NULL;
|
|
|
|
|
hs->error = NULL;
|
|
|
|
|
|
2003-11-27 21:24:53 -05:00
|
|
|
|
conn = u;
|
|
|
|
|
|
|
|
|
|
proxyauth = NULL;
|
|
|
|
|
if (proxy)
|
|
|
|
|
{
|
|
|
|
|
char *proxy_user, *proxy_passwd;
|
|
|
|
|
/* For normal username and password, URL components override
|
|
|
|
|
command-line/wgetrc parameters. With proxy
|
|
|
|
|
authentication, it's the reverse, because proxy URLs are
|
|
|
|
|
normally the "permanent" ones, so command-line args
|
|
|
|
|
should take precedence. */
|
|
|
|
|
if (opt.proxy_user && opt.proxy_passwd)
|
|
|
|
|
{
|
|
|
|
|
proxy_user = opt.proxy_user;
|
|
|
|
|
proxy_passwd = opt.proxy_passwd;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
proxy_user = proxy->user;
|
|
|
|
|
proxy_passwd = proxy->passwd;
|
|
|
|
|
}
|
|
|
|
|
/* #### This does not appear right. Can't the proxy request,
|
|
|
|
|
say, `Digest' authentication? */
|
|
|
|
|
if (proxy_user && proxy_passwd)
|
2003-11-29 13:40:01 -05:00
|
|
|
|
proxyauth = basic_authentication_encode (proxy_user, proxy_passwd);
|
2003-11-27 21:24:53 -05:00
|
|
|
|
|
|
|
|
|
/* If we're using a proxy, we will be connecting to the proxy
|
|
|
|
|
server. */
|
|
|
|
|
conn = proxy;
|
|
|
|
|
}
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2003-11-29 13:40:01 -05:00
|
|
|
|
/* Prepare the request to send. */
|
|
|
|
|
|
|
|
|
|
req = request_new ();
|
|
|
|
|
{
|
|
|
|
|
const char *meth = "GET";
|
|
|
|
|
if (*dt & HEAD_ONLY)
|
|
|
|
|
meth = "HEAD";
|
|
|
|
|
else if (opt.post_file_name || opt.post_data)
|
|
|
|
|
meth = "POST";
|
|
|
|
|
/* Use the full path, i.e. one that includes the leading slash and
|
|
|
|
|
the query string. E.g. if u->path is "foo/bar" and u->query is
|
|
|
|
|
"param=value", full_path will be "/foo/bar?param=value". */
|
|
|
|
|
request_set_method (req, meth,
|
|
|
|
|
proxy ? xstrdup (u->url) : url_full_path (u));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request_set_header (req, "Referer", (char *) hs->referer, rel_none);
|
|
|
|
|
if (*dt & SEND_NOCACHE)
|
|
|
|
|
request_set_header (req, "Pragma", "no-cache", rel_none);
|
|
|
|
|
if (hs->restval)
|
|
|
|
|
request_set_header (req, "Range",
|
|
|
|
|
aprintf ("bytes=%ld-", hs->restval), rel_value);
|
|
|
|
|
if (opt.useragent)
|
|
|
|
|
request_set_header (req, "User-Agent", opt.useragent, rel_none);
|
|
|
|
|
else
|
|
|
|
|
request_set_header (req, "User-Agent",
|
|
|
|
|
aprintf ("Wget/%s", version_string), rel_value);
|
|
|
|
|
request_set_header (req, "Accept", "*/*", rel_none);
|
|
|
|
|
|
|
|
|
|
/* Find the username and password for authentication. */
|
|
|
|
|
user = u->user;
|
|
|
|
|
passwd = u->passwd;
|
|
|
|
|
search_netrc (u->host, (const char **)&user, (const char **)&passwd, 0);
|
|
|
|
|
user = user ? user : opt.http_user;
|
|
|
|
|
passwd = passwd ? passwd : opt.http_passwd;
|
|
|
|
|
|
|
|
|
|
if (user && passwd)
|
|
|
|
|
{
|
|
|
|
|
/* We have the username and the password, but haven't tried
|
|
|
|
|
any authorization yet. Let's see if the "Basic" method
|
|
|
|
|
works. If not, we'll come back here and construct a
|
|
|
|
|
proper authorization method with the right challenges.
|
|
|
|
|
|
|
|
|
|
If we didn't employ this kind of logic, every URL that
|
|
|
|
|
requires authorization would have to be processed twice,
|
|
|
|
|
which is very suboptimal and generates a bunch of false
|
|
|
|
|
"unauthorized" errors in the server log.
|
|
|
|
|
|
|
|
|
|
#### But this logic also has a serious problem when used
|
|
|
|
|
with stronger authentications: we *first* transmit the
|
|
|
|
|
username and the password in clear text, and *then* attempt a
|
|
|
|
|
stronger authentication scheme. That cannot be right! We
|
|
|
|
|
are only fortunate that almost everyone still uses the
|
|
|
|
|
`Basic' scheme anyway.
|
|
|
|
|
|
|
|
|
|
There should be an option to prevent this from happening, for
|
|
|
|
|
those who use strong authentication schemes and value their
|
|
|
|
|
passwords. */
|
|
|
|
|
request_set_header (req, "Authorization",
|
|
|
|
|
basic_authentication_encode (user, passwd),
|
|
|
|
|
rel_value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
/* Whether we need to print the host header with braces around
|
|
|
|
|
host, e.g. "Host: [3ffe:8100:200:2::2]:1234" instead of the
|
|
|
|
|
usual "Host: symbolic-name:1234". */
|
|
|
|
|
int squares = strchr (u->host, ':') != NULL;
|
|
|
|
|
if (u->port == scheme_default_port (u->scheme))
|
|
|
|
|
request_set_header (req, "Host",
|
|
|
|
|
aprintf (squares ? "[%s]" : "%s", u->host),
|
|
|
|
|
rel_value);
|
|
|
|
|
else
|
|
|
|
|
request_set_header (req, "Host",
|
|
|
|
|
aprintf (squares ? "[%s]:%d" : "%s:%d",
|
|
|
|
|
u->host, u->port),
|
|
|
|
|
rel_value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!inhibit_keep_alive)
|
|
|
|
|
request_set_header (req, "Connection", "Keep-Alive", rel_none);
|
|
|
|
|
|
|
|
|
|
if (opt.cookies)
|
|
|
|
|
request_set_header (req, "Cookie",
|
|
|
|
|
cookie_header (wget_cookie_jar,
|
|
|
|
|
u->host, u->port, u->path,
|
|
|
|
|
#ifdef HAVE_SSL
|
|
|
|
|
u->scheme == SCHEME_HTTPS
|
|
|
|
|
#else
|
|
|
|
|
0
|
|
|
|
|
#endif
|
|
|
|
|
),
|
|
|
|
|
rel_value);
|
|
|
|
|
|
|
|
|
|
if (opt.post_data || opt.post_file_name)
|
|
|
|
|
{
|
|
|
|
|
request_set_header (req, "Content-Type",
|
|
|
|
|
"application/x-www-form-urlencoded", rel_none);
|
|
|
|
|
if (opt.post_data)
|
|
|
|
|
post_data_size = strlen (opt.post_data);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
post_data_size = file_size (opt.post_file_name);
|
|
|
|
|
if (post_data_size == -1)
|
|
|
|
|
{
|
|
|
|
|
logprintf (LOG_NOTQUIET, "POST data file missing: %s\n",
|
|
|
|
|
opt.post_file_name);
|
|
|
|
|
post_data_size = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
request_set_header (req, "Content-Length",
|
|
|
|
|
aprintf ("Content-Length: %ld", post_data_size),
|
|
|
|
|
rel_value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Add the user headers. */
|
|
|
|
|
if (opt.user_headers)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; opt.user_headers[i]; i++)
|
|
|
|
|
request_set_user_header (req, opt.user_headers[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
retry_with_auth:
|
|
|
|
|
/* We need to come back here when the initial attempt to retrieve
|
|
|
|
|
without authorization header fails. (Expected to happen at least
|
|
|
|
|
for the Digest authorization scheme.) */
|
|
|
|
|
|
|
|
|
|
keep_alive = 0;
|
|
|
|
|
keep_alive_confirmed = 0;
|
2003-11-07 21:57:51 -05:00
|
|
|
|
|
2003-11-29 13:40:01 -05:00
|
|
|
|
/* Establish the connection. */
|
2003-11-27 21:24:53 -05:00
|
|
|
|
|
|
|
|
|
if (!inhibit_keep_alive)
|
|
|
|
|
{
|
|
|
|
|
/* Look for a persistent connection to target host, unless a
|
|
|
|
|
proxy is used. The exception is when SSL is in use, in which
|
|
|
|
|
case the proxy is nothing but a passthrough to the target
|
|
|
|
|
host, registered as a connection to the latter. */
|
|
|
|
|
struct url *relevant = conn;
|
2003-11-05 20:12:03 -05:00
|
|
|
|
#ifdef HAVE_SSL
|
2003-11-27 21:24:53 -05:00
|
|
|
|
if (u->scheme == SCHEME_HTTPS)
|
|
|
|
|
relevant = u;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (persistent_available_p (relevant->host, relevant->port,
|
|
|
|
|
#ifdef HAVE_SSL
|
|
|
|
|
relevant->scheme == SCHEME_HTTPS,
|
2000-12-05 18:09:41 -05:00
|
|
|
|
#else
|
2003-11-27 21:24:53 -05:00
|
|
|
|
0,
|
2003-11-05 20:12:03 -05:00
|
|
|
|
#endif
|
2003-11-27 21:24:53 -05:00
|
|
|
|
&host_lookup_failed))
|
|
|
|
|
{
|
|
|
|
|
sock = pconn.socket;
|
|
|
|
|
using_ssl = pconn.ssl;
|
|
|
|
|
logprintf (LOG_VERBOSE, _("Reusing existing connection to %s:%d.\n"),
|
|
|
|
|
pconn.host, pconn.port);
|
|
|
|
|
DEBUGP (("Reusing fd %d.\n", sock));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sock < 0)
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
2003-11-07 21:57:51 -05:00
|
|
|
|
/* In its current implementation, persistent_available_p will
|
|
|
|
|
look up conn->host in some cases. If that lookup failed, we
|
|
|
|
|
don't need to bother with connect_to_host. */
|
|
|
|
|
if (host_lookup_failed)
|
|
|
|
|
return HOSTERR;
|
|
|
|
|
|
2003-10-30 19:18:08 -05:00
|
|
|
|
sock = connect_to_host (conn->host, conn->port);
|
|
|
|
|
if (sock == E_HOST)
|
2001-11-26 00:36:33 -05:00
|
|
|
|
return HOSTERR;
|
2003-10-30 19:18:08 -05:00
|
|
|
|
else if (sock < 0)
|
2003-11-13 17:27:38 -05:00
|
|
|
|
return (retryable_socket_connect_error (errno)
|
|
|
|
|
? CONERROR : CONIMPOSSIBLE);
|
2001-11-26 00:36:33 -05:00
|
|
|
|
|
2000-12-05 18:09:41 -05:00
|
|
|
|
#ifdef HAVE_SSL
|
2003-11-27 21:24:53 -05:00
|
|
|
|
if (proxy && u->scheme == SCHEME_HTTPS)
|
|
|
|
|
{
|
|
|
|
|
/* When requesting SSL URLs through proxies, use the
|
|
|
|
|
CONNECT method to request passthrough. */
|
2003-11-29 13:40:01 -05:00
|
|
|
|
struct request *connreq = request_new ();
|
|
|
|
|
request_set_method (connreq, "CONNECT",
|
|
|
|
|
aprintf ("%s:%d", u->host, u->port));
|
|
|
|
|
if (proxyauth)
|
|
|
|
|
{
|
|
|
|
|
request_set_header (connreq, "Proxy-Authorization",
|
|
|
|
|
proxyauth, rel_value);
|
|
|
|
|
/* Now that PROXYAUTH is part of the CONNECT request,
|
|
|
|
|
zero it out so we don't send proxy authorization with
|
|
|
|
|
the regular request below. */
|
|
|
|
|
proxyauth = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
write_error = request_send (connreq, sock);
|
|
|
|
|
request_free (connreq);
|
2003-11-27 21:24:53 -05:00
|
|
|
|
if (write_error < 0)
|
|
|
|
|
{
|
|
|
|
|
logprintf (LOG_VERBOSE, _("Failed writing to proxy: %s.\n"),
|
|
|
|
|
strerror (errno));
|
|
|
|
|
CLOSE_INVALIDATE (sock);
|
|
|
|
|
return WRITEFAILED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
head = fd_read_http_head (sock);
|
|
|
|
|
if (!head)
|
|
|
|
|
{
|
|
|
|
|
logprintf (LOG_VERBOSE, _("Failed reading proxy response: %s\n"),
|
|
|
|
|
strerror (errno));
|
|
|
|
|
CLOSE_INVALIDATE (sock);
|
|
|
|
|
return HERR;
|
|
|
|
|
}
|
|
|
|
|
message = NULL;
|
|
|
|
|
if (!*head)
|
|
|
|
|
{
|
|
|
|
|
xfree (head);
|
|
|
|
|
goto failed_tunnel;
|
|
|
|
|
}
|
|
|
|
|
DEBUGP (("proxy responded with: [%s]\n", head));
|
|
|
|
|
|
|
|
|
|
resp = response_new (head);
|
|
|
|
|
statcode = response_status (resp, &message);
|
|
|
|
|
response_free (resp);
|
|
|
|
|
if (statcode != 200)
|
|
|
|
|
{
|
|
|
|
|
failed_tunnel:
|
|
|
|
|
logprintf (LOG_NOTQUIET, _("Proxy tunneling failed: %s"),
|
|
|
|
|
message ? message : "?");
|
|
|
|
|
xfree_null (message);
|
|
|
|
|
return CONSSLERR;
|
|
|
|
|
}
|
|
|
|
|
xfree (message);
|
|
|
|
|
|
|
|
|
|
/* SOCK is now *really* connected to u->host, so update CONN
|
|
|
|
|
to reflect this. That way register_persistent will
|
|
|
|
|
register SOCK as being connected to u->host:u->port. */
|
|
|
|
|
conn = u;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (conn->scheme == SCHEME_HTTPS)
|
|
|
|
|
{
|
|
|
|
|
if (!ssl_connect (sock))
|
|
|
|
|
{
|
|
|
|
|
fd_close (sock);
|
|
|
|
|
return CONSSLERR;
|
|
|
|
|
}
|
|
|
|
|
using_ssl = 1;
|
|
|
|
|
}
|
2000-12-05 18:09:41 -05:00
|
|
|
|
#endif /* HAVE_SSL */
|
2000-11-19 15:50:10 -05:00
|
|
|
|
}
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
/* Send the request to server. */
|
2003-11-29 13:40:01 -05:00
|
|
|
|
write_error = request_send (req, sock);
|
2002-04-14 00:22:47 -04:00
|
|
|
|
|
|
|
|
|
if (write_error >= 0)
|
|
|
|
|
{
|
|
|
|
|
if (opt.post_data)
|
|
|
|
|
{
|
|
|
|
|
DEBUGP (("[POST data: %s]\n", opt.post_data));
|
2003-11-20 10:19:59 -05:00
|
|
|
|
write_error = fd_write (sock, opt.post_data, post_data_size, -1);
|
2002-04-14 00:22:47 -04:00
|
|
|
|
}
|
2003-10-04 18:26:58 -04:00
|
|
|
|
else if (opt.post_file_name && post_data_size != 0)
|
2003-11-05 20:12:03 -05:00
|
|
|
|
write_error = post_file (sock, opt.post_file_name, post_data_size);
|
2002-04-14 00:22:47 -04:00
|
|
|
|
}
|
|
|
|
|
DEBUGP (("---request end---\n"));
|
2000-12-05 18:35:56 -05:00
|
|
|
|
|
2002-04-14 00:22:47 -04:00
|
|
|
|
if (write_error < 0)
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
2000-11-19 15:50:10 -05:00
|
|
|
|
logprintf (LOG_VERBOSE, _("Failed writing HTTP request: %s.\n"),
|
|
|
|
|
strerror (errno));
|
2000-12-05 18:35:56 -05:00
|
|
|
|
CLOSE_INVALIDATE (sock);
|
2003-11-29 13:40:01 -05:00
|
|
|
|
request_free (req);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return WRITEFAILED;
|
|
|
|
|
}
|
|
|
|
|
logprintf (LOG_VERBOSE, _("%s request sent, awaiting response... "),
|
2001-11-21 19:24:28 -05:00
|
|
|
|
proxy ? "Proxy" : "HTTP");
|
1999-12-02 02:42:23 -05:00
|
|
|
|
contlen = contrange = -1;
|
|
|
|
|
type = NULL;
|
|
|
|
|
statcode = -1;
|
|
|
|
|
*dt &= ~RETROKF;
|
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
head = fd_read_http_head (sock);
|
2003-11-20 20:48:11 -05:00
|
|
|
|
if (!head)
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
2003-11-20 20:48:11 -05:00
|
|
|
|
if (errno == 0)
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
2003-11-20 20:48:11 -05:00
|
|
|
|
logputs (LOG_NOTQUIET, _("No data received.\n"));
|
2000-12-05 18:35:56 -05:00
|
|
|
|
CLOSE_INVALIDATE (sock);
|
2003-11-29 13:40:01 -05:00
|
|
|
|
request_free (req);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return HEOF;
|
|
|
|
|
}
|
2003-11-20 20:48:11 -05:00
|
|
|
|
else
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
|
|
|
|
logprintf (LOG_NOTQUIET, _("Read error (%s) in headers.\n"),
|
|
|
|
|
strerror (errno));
|
2000-12-05 18:35:56 -05:00
|
|
|
|
CLOSE_INVALIDATE (sock);
|
2003-11-29 13:40:01 -05:00
|
|
|
|
request_free (req);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return HERR;
|
|
|
|
|
}
|
2003-11-20 20:48:11 -05:00
|
|
|
|
}
|
2003-11-27 21:24:53 -05:00
|
|
|
|
DEBUGP (("\n---response begin---\n%s---response end---\n", head));
|
2003-11-20 20:48:11 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
resp = response_new (head);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
/* Check for status line. */
|
|
|
|
|
message = NULL;
|
|
|
|
|
statcode = response_status (resp, &message);
|
|
|
|
|
if (!opt.server_response)
|
|
|
|
|
logprintf (LOG_VERBOSE, "%2d %s\n", statcode, message ? message : "");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logprintf (LOG_VERBOSE, "\n");
|
2003-11-29 13:40:01 -05:00
|
|
|
|
print_server_response (resp, " ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (statcode == HTTP_STATUS_UNAUTHORIZED)
|
|
|
|
|
{
|
|
|
|
|
/* Authorization is required. */
|
|
|
|
|
CLOSE_INVALIDATE (sock); /* would be CLOSE_FINISH, but there
|
|
|
|
|
might be more bytes in the body. */
|
|
|
|
|
if (auth_tried_already || !(user && passwd))
|
|
|
|
|
{
|
|
|
|
|
/* If we have tried it already, then there is not point
|
|
|
|
|
retrying it. */
|
|
|
|
|
logputs (LOG_NOTQUIET, _("Authorization failed.\n"));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
char *www_authenticate = response_header_strdup (resp,
|
|
|
|
|
"WWW-Authenticate");
|
|
|
|
|
/* If the authentication scheme is unknown or if it's the
|
|
|
|
|
"Basic" authentication (which we try by default), there's
|
|
|
|
|
no sense in retrying. */
|
|
|
|
|
if (!www_authenticate
|
|
|
|
|
|| !known_authentication_scheme_p (www_authenticate)
|
|
|
|
|
|| BEGINS_WITH (www_authenticate, "Basic"))
|
|
|
|
|
{
|
|
|
|
|
xfree_null (www_authenticate);
|
|
|
|
|
logputs (LOG_NOTQUIET, _("Unknown authentication scheme.\n"));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
char *pth;
|
|
|
|
|
auth_tried_already = 1;
|
|
|
|
|
pth = url_full_path (u);
|
|
|
|
|
request_set_header (req, "Authorization",
|
|
|
|
|
create_authorization_line (www_authenticate,
|
|
|
|
|
user, passwd,
|
|
|
|
|
request_method (req),
|
|
|
|
|
pth),
|
|
|
|
|
rel_value);
|
|
|
|
|
xfree (pth);
|
|
|
|
|
xfree (www_authenticate);
|
|
|
|
|
goto retry_with_auth;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
request_free (req);
|
|
|
|
|
return AUTHFAILED;
|
2003-11-27 18:29:36 -05:00
|
|
|
|
}
|
2003-11-29 13:40:01 -05:00
|
|
|
|
request_free (req);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
hs->statcode = statcode;
|
|
|
|
|
if (statcode == -1)
|
|
|
|
|
hs->error = xstrdup (_("Malformed status line"));
|
|
|
|
|
else if (!*message)
|
|
|
|
|
hs->error = xstrdup (_("(no description)"));
|
|
|
|
|
else
|
|
|
|
|
hs->error = xstrdup (message);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
if (response_header_copy (resp, "Content-Length", hdrval, sizeof (hdrval)))
|
|
|
|
|
contlen = strtol (hdrval, NULL, 10);
|
|
|
|
|
type = response_header_strdup (resp, "Content-Type");
|
|
|
|
|
if (type)
|
|
|
|
|
{
|
|
|
|
|
char *tmp = strchr (type, ';');
|
|
|
|
|
if (tmp)
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
2003-11-27 18:29:36 -05:00
|
|
|
|
while (tmp > type && ISSPACE (tmp[-1]))
|
|
|
|
|
--tmp;
|
|
|
|
|
*tmp = '\0';
|
1999-12-02 02:42:23 -05:00
|
|
|
|
}
|
2003-11-27 18:29:36 -05:00
|
|
|
|
}
|
|
|
|
|
hs->newloc = response_header_strdup (resp, "Location");
|
|
|
|
|
hs->remote_time = response_header_strdup (resp, "Last-Modified");
|
2003-11-29 13:40:01 -05:00
|
|
|
|
{
|
|
|
|
|
char *set_cookie = response_header_strdup (resp, "Set-Cookie");
|
|
|
|
|
if (set_cookie)
|
|
|
|
|
{
|
|
|
|
|
/* The jar should have been created by now. */
|
|
|
|
|
assert (wget_cookie_jar != NULL);
|
|
|
|
|
cookie_handle_set_cookie (wget_cookie_jar, u->host, u->port, u->path,
|
|
|
|
|
set_cookie);
|
|
|
|
|
xfree (set_cookie);
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-11-27 18:29:36 -05:00
|
|
|
|
if (response_header_copy (resp, "Content-Range", hdrval, sizeof (hdrval)))
|
|
|
|
|
{
|
|
|
|
|
long first_byte_pos, last_byte_pos, entity_length;
|
|
|
|
|
if (parse_content_range (hdrval, &first_byte_pos, &last_byte_pos,
|
|
|
|
|
&entity_length))
|
|
|
|
|
contrange = first_byte_pos;
|
|
|
|
|
}
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
/* Check for keep-alive related responses. */
|
|
|
|
|
if (!inhibit_keep_alive && contlen != -1)
|
|
|
|
|
{
|
|
|
|
|
if (response_header_copy (resp, "Keep-Alive", NULL, 0))
|
|
|
|
|
keep_alive = 1;
|
|
|
|
|
else if (response_header_copy (resp, "Connection", hdrval,
|
|
|
|
|
sizeof (hdrval)))
|
2000-11-19 15:50:10 -05:00
|
|
|
|
{
|
2003-11-27 18:29:36 -05:00
|
|
|
|
if (0 == strcasecmp (hdrval, "Keep-Alive"))
|
|
|
|
|
keep_alive = 1;
|
2000-11-19 15:50:10 -05:00
|
|
|
|
}
|
1999-12-02 02:42:23 -05:00
|
|
|
|
}
|
2003-11-27 18:29:36 -05:00
|
|
|
|
response_free (resp);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2000-11-19 18:42:13 -05:00
|
|
|
|
if (keep_alive)
|
|
|
|
|
/* The server has promised that it will not close the connection
|
|
|
|
|
when we're done. This means that we can register it. */
|
2003-11-05 20:12:03 -05:00
|
|
|
|
register_persistent (conn->host, conn->port, sock, using_ssl);
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* 20x responses are counted among successful by default. */
|
|
|
|
|
if (H_20X (statcode))
|
|
|
|
|
*dt |= RETROKF;
|
|
|
|
|
|
2001-04-28 12:22:20 -04:00
|
|
|
|
/* Return if redirected. */
|
|
|
|
|
if (H_REDIRECTED (statcode) || statcode == HTTP_STATUS_MULTIPLE_CHOICES)
|
|
|
|
|
{
|
|
|
|
|
/* RFC2068 says that in case of the 300 (multiple choices)
|
|
|
|
|
response, the server can output a preferred URL through
|
|
|
|
|
`Location' header; otherwise, the request should be treated
|
|
|
|
|
like GET. So, if the location is set, it will be a
|
|
|
|
|
redirection; otherwise, just proceed normally. */
|
|
|
|
|
if (statcode == HTTP_STATUS_MULTIPLE_CHOICES && !hs->newloc)
|
|
|
|
|
*dt |= RETROKF;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logprintf (LOG_VERBOSE,
|
|
|
|
|
_("Location: %s%s\n"),
|
|
|
|
|
hs->newloc ? hs->newloc : _("unspecified"),
|
|
|
|
|
hs->newloc ? _(" [following]") : "");
|
|
|
|
|
CLOSE_INVALIDATE (sock); /* would be CLOSE_FINISH, but there
|
|
|
|
|
might be more bytes in the body. */
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (type);
|
2001-04-28 12:22:20 -04:00
|
|
|
|
return NEWLOCATION;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-15 21:13:51 -04:00
|
|
|
|
/* If content-type is not given, assume text/html. This is because
|
|
|
|
|
of the multitude of broken CGI's that "forget" to generate the
|
|
|
|
|
content-type. */
|
2003-09-21 08:02:57 -04:00
|
|
|
|
if (!type ||
|
|
|
|
|
0 == strncasecmp (type, TEXTHTML_S, strlen (TEXTHTML_S)) ||
|
|
|
|
|
0 == strncasecmp (type, TEXTXHTML_S, strlen (TEXTXHTML_S)))
|
1999-12-02 02:42:23 -05:00
|
|
|
|
*dt |= TEXTHTML;
|
|
|
|
|
else
|
|
|
|
|
*dt &= ~TEXTHTML;
|
|
|
|
|
|
2000-10-20 01:55:46 -04:00
|
|
|
|
if (opt.html_extension && (*dt & TEXTHTML))
|
|
|
|
|
/* -E / --html-extension / html_extension = on was specified, and this is a
|
|
|
|
|
text/html file. If some case-insensitive variation on ".htm[l]" isn't
|
|
|
|
|
already the file's suffix, tack on ".html". */
|
|
|
|
|
{
|
2001-11-21 19:24:28 -05:00
|
|
|
|
char* last_period_in_local_filename = strrchr(*hs->local_file, '.');
|
2000-10-20 01:55:46 -04:00
|
|
|
|
|
2003-10-31 09:31:56 -05:00
|
|
|
|
if (last_period_in_local_filename == NULL
|
|
|
|
|
|| !(0 == strcasecmp (last_period_in_local_filename, ".htm")
|
|
|
|
|
|| 0 == strcasecmp (last_period_in_local_filename, ".html")))
|
2000-10-20 01:55:46 -04:00
|
|
|
|
{
|
2001-11-21 19:24:28 -05:00
|
|
|
|
size_t local_filename_len = strlen(*hs->local_file);
|
2000-10-20 01:55:46 -04:00
|
|
|
|
|
2001-11-21 19:24:28 -05:00
|
|
|
|
*hs->local_file = xrealloc(*hs->local_file,
|
|
|
|
|
local_filename_len + sizeof(".html"));
|
|
|
|
|
strcpy(*hs->local_file + local_filename_len, ".html");
|
2000-10-20 01:55:46 -04:00
|
|
|
|
|
|
|
|
|
*dt |= ADDED_HTML_EXTENSION;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1999-12-02 02:42:23 -05:00
|
|
|
|
if (contrange == -1)
|
2001-04-01 16:04:21 -04:00
|
|
|
|
{
|
|
|
|
|
/* We did not get a content-range header. This means that the
|
|
|
|
|
server did not honor our `Range' request. Normally, this
|
|
|
|
|
means we should reset hs->restval and continue normally. */
|
|
|
|
|
|
|
|
|
|
/* However, if `-c' is used, we need to be a bit more careful:
|
|
|
|
|
|
|
|
|
|
1. If `-c' is specified and the file already existed when
|
|
|
|
|
Wget was started, it would be a bad idea for us to start
|
|
|
|
|
downloading it from scratch, effectively truncating it. I
|
|
|
|
|
believe this cannot happen unless `-c' was specified.
|
|
|
|
|
|
|
|
|
|
2. If `-c' is used on a file that is already fully
|
|
|
|
|
downloaded, we're requesting bytes after the end of file,
|
|
|
|
|
which can result in server not honoring `Range'. If this is
|
|
|
|
|
the case, `Content-Length' will be equal to the length of the
|
|
|
|
|
file. */
|
|
|
|
|
if (opt.always_rest)
|
|
|
|
|
{
|
|
|
|
|
/* Check for condition #2. */
|
2001-05-11 08:37:37 -04:00
|
|
|
|
if (hs->restval > 0 /* restart was requested. */
|
|
|
|
|
&& contlen != -1 /* we got content-length. */
|
|
|
|
|
&& hs->restval >= contlen /* file fully downloaded
|
|
|
|
|
or has shrunk. */
|
|
|
|
|
)
|
2001-04-01 16:04:21 -04:00
|
|
|
|
{
|
|
|
|
|
logputs (LOG_VERBOSE, _("\
|
|
|
|
|
\n The file is already fully retrieved; nothing to do.\n\n"));
|
|
|
|
|
/* In case the caller inspects. */
|
|
|
|
|
hs->len = contlen;
|
|
|
|
|
hs->res = 0;
|
2002-02-19 00:18:43 -05:00
|
|
|
|
/* Mark as successfully retrieved. */
|
|
|
|
|
*dt |= RETROKF;
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (type);
|
2001-04-01 16:04:21 -04:00
|
|
|
|
CLOSE_INVALIDATE (sock); /* would be CLOSE_FINISH, but there
|
|
|
|
|
might be more bytes in the body. */
|
2001-04-28 12:22:20 -04:00
|
|
|
|
return RETRUNNEEDED;
|
2001-04-01 16:04:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check for condition #1. */
|
|
|
|
|
if (hs->no_truncate)
|
|
|
|
|
{
|
|
|
|
|
logprintf (LOG_NOTQUIET,
|
|
|
|
|
_("\
|
2001-04-05 23:06:00 -04:00
|
|
|
|
\n\
|
2001-05-27 15:35:15 -04:00
|
|
|
|
Continued download failed on this file, which conflicts with `-c'.\n\
|
2001-11-21 19:24:28 -05:00
|
|
|
|
Refusing to truncate existing file `%s'.\n\n"), *hs->local_file);
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (type);
|
2001-04-28 12:22:20 -04:00
|
|
|
|
CLOSE_INVALIDATE (sock);
|
2001-04-01 16:04:21 -04:00
|
|
|
|
return CONTNOTSUPPORTED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Fallthrough */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hs->restval = 0;
|
|
|
|
|
}
|
1999-12-02 02:42:23 -05:00
|
|
|
|
else if (contrange != hs->restval ||
|
|
|
|
|
(H_PARTIAL (statcode) && contrange == -1))
|
|
|
|
|
{
|
|
|
|
|
/* This means the whole request was somehow misunderstood by the
|
|
|
|
|
server. Bail out. */
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (type);
|
2000-12-05 18:35:56 -05:00
|
|
|
|
CLOSE_INVALIDATE (sock);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return RANGEERR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hs->restval)
|
|
|
|
|
{
|
|
|
|
|
if (contlen != -1)
|
|
|
|
|
contlen += contrange;
|
|
|
|
|
else
|
|
|
|
|
contrange = -1; /* If conent-length was not sent,
|
|
|
|
|
content-range will be ignored. */
|
|
|
|
|
}
|
|
|
|
|
hs->contlen = contlen;
|
|
|
|
|
|
|
|
|
|
if (opt.verbose)
|
|
|
|
|
{
|
2003-11-29 13:40:01 -05:00
|
|
|
|
if (*dt & RETROKF)
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
|
|
|
|
/* No need to print this output if the body won't be
|
|
|
|
|
downloaded at all, or if the original server response is
|
|
|
|
|
printed. */
|
|
|
|
|
logputs (LOG_VERBOSE, _("Length: "));
|
|
|
|
|
if (contlen != -1)
|
|
|
|
|
{
|
|
|
|
|
logputs (LOG_VERBOSE, legible (contlen));
|
|
|
|
|
if (contrange != -1)
|
|
|
|
|
logprintf (LOG_VERBOSE, _(" (%s to go)"),
|
|
|
|
|
legible (contlen - contrange));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
logputs (LOG_VERBOSE,
|
|
|
|
|
opt.ignore_length ? _("ignored") : _("unspecified"));
|
|
|
|
|
if (type)
|
|
|
|
|
logprintf (LOG_VERBOSE, " [%s]\n", type);
|
|
|
|
|
else
|
|
|
|
|
logputs (LOG_VERBOSE, "\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (type);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
type = NULL; /* We don't need it any more. */
|
|
|
|
|
|
|
|
|
|
/* Return if we have no intention of further downloading. */
|
|
|
|
|
if (!(*dt & RETROKF) || (*dt & HEAD_ONLY))
|
|
|
|
|
{
|
2001-04-01 16:04:21 -04:00
|
|
|
|
/* In case the caller cares to look... */
|
1999-12-02 02:42:23 -05:00
|
|
|
|
hs->len = 0L;
|
|
|
|
|
hs->res = 0;
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (type);
|
2001-03-30 21:20:16 -05:00
|
|
|
|
CLOSE_INVALIDATE (sock); /* would be CLOSE_FINISH, but there
|
|
|
|
|
might be more bytes in the body. */
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return RETRFINISHED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Open the local file. */
|
|
|
|
|
if (!opt.dfp)
|
|
|
|
|
{
|
2001-11-21 19:24:28 -05:00
|
|
|
|
mkalldirs (*hs->local_file);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
if (opt.backups)
|
2001-11-21 19:24:28 -05:00
|
|
|
|
rotate_backups (*hs->local_file);
|
|
|
|
|
fp = fopen (*hs->local_file, hs->restval ? "ab" : "wb");
|
1999-12-02 02:42:23 -05:00
|
|
|
|
if (!fp)
|
|
|
|
|
{
|
2001-11-21 19:24:28 -05:00
|
|
|
|
logprintf (LOG_NOTQUIET, "%s: %s\n", *hs->local_file, strerror (errno));
|
2001-03-30 21:20:16 -05:00
|
|
|
|
CLOSE_INVALIDATE (sock); /* would be CLOSE_FINISH, but there
|
|
|
|
|
might be more bytes in the body. */
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return FOPENERR;
|
|
|
|
|
}
|
|
|
|
|
}
|
2000-10-31 14:25:32 -05:00
|
|
|
|
else /* opt.dfp */
|
|
|
|
|
{
|
2001-04-01 19:50:04 -04:00
|
|
|
|
extern int global_download_count;
|
2000-10-31 14:25:32 -05:00
|
|
|
|
fp = opt.dfp;
|
2001-04-01 19:50:04 -04:00
|
|
|
|
/* To ensure that repeated "from scratch" downloads work for -O
|
|
|
|
|
files, we rewind the file pointer, unless restval is
|
|
|
|
|
non-zero. (This works only when -O is used on regular files,
|
|
|
|
|
but it's still a valuable feature.)
|
|
|
|
|
|
|
|
|
|
However, this loses when more than one URL is specified on
|
|
|
|
|
the command line the second rewinds eradicates the contents
|
|
|
|
|
of the first download. Thus we disable the above trick for
|
|
|
|
|
all the downloads except the very first one.
|
|
|
|
|
|
|
|
|
|
#### A possible solution to this would be to remember the
|
|
|
|
|
file position in the output document and to seek to that
|
2002-05-14 08:23:51 -04:00
|
|
|
|
position, instead of rewinding.
|
|
|
|
|
|
|
|
|
|
We don't truncate stdout, since that breaks
|
|
|
|
|
"wget -O - [...] >> foo".
|
|
|
|
|
*/
|
|
|
|
|
if (!hs->restval && global_download_count == 0 && opt.dfp != stdout)
|
2000-10-31 14:25:32 -05:00
|
|
|
|
{
|
|
|
|
|
/* This will silently fail for streams that don't correspond
|
|
|
|
|
to regular files, but that's OK. */
|
|
|
|
|
rewind (fp);
|
2001-04-03 08:24:49 -04:00
|
|
|
|
/* ftruncate is needed because opt.dfp is opened in append
|
|
|
|
|
mode if opt.always_rest is set. */
|
|
|
|
|
ftruncate (fileno (fp), 0);
|
2000-10-31 14:25:32 -05:00
|
|
|
|
clearerr (fp);
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
/* #### This confuses the code that checks for file size. There
|
|
|
|
|
should be some overhead information. */
|
|
|
|
|
if (opt.save_headers)
|
2003-11-20 20:48:11 -05:00
|
|
|
|
fwrite (head, 1, strlen (head), fp);
|
2001-11-26 07:49:10 -05:00
|
|
|
|
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* Get the contents of the document. */
|
2003-11-20 20:48:11 -05:00
|
|
|
|
hs->res = fd_read_body (sock, fp, &hs->len, hs->restval,
|
1999-12-02 02:42:23 -05:00
|
|
|
|
(contlen != -1 ? contlen : 0),
|
2003-11-20 20:48:11 -05:00
|
|
|
|
keep_alive, &hs->dltime);
|
2001-11-26 07:49:10 -05:00
|
|
|
|
|
2002-01-13 20:39:16 -05:00
|
|
|
|
if (hs->res >= 0)
|
|
|
|
|
CLOSE_FINISH (sock);
|
|
|
|
|
else
|
|
|
|
|
CLOSE_INVALIDATE (sock);
|
|
|
|
|
|
2000-05-19 03:37:22 -04:00
|
|
|
|
{
|
|
|
|
|
/* Close or flush the file. We have to be careful to check for
|
|
|
|
|
error here. Checking the result of fwrite() is not enough --
|
|
|
|
|
errors could go unnoticed! */
|
|
|
|
|
int flush_res;
|
|
|
|
|
if (!opt.dfp)
|
|
|
|
|
flush_res = fclose (fp);
|
|
|
|
|
else
|
|
|
|
|
flush_res = fflush (fp);
|
|
|
|
|
if (flush_res == EOF)
|
|
|
|
|
hs->res = -2;
|
|
|
|
|
}
|
1999-12-02 02:42:23 -05:00
|
|
|
|
if (hs->res == -2)
|
|
|
|
|
return FWRITEERR;
|
|
|
|
|
return RETRFINISHED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* The genuine HTTP loop! This is the part where the retrieval is
|
|
|
|
|
retried, and retried, and retried, and... */
|
|
|
|
|
uerr_t
|
2001-11-21 19:24:28 -05:00
|
|
|
|
http_loop (struct url *u, char **newloc, char **local_file, const char *referer,
|
|
|
|
|
int *dt, struct url *proxy)
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
|
|
|
|
int count;
|
|
|
|
|
int use_ts, got_head = 0; /* time-stamping info */
|
2000-10-20 01:55:46 -04:00
|
|
|
|
char *filename_plus_orig_suffix;
|
|
|
|
|
char *local_filename = NULL;
|
2002-04-12 14:53:39 -04:00
|
|
|
|
char *tms, *locf, *tmrate;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
uerr_t err;
|
|
|
|
|
time_t tml = -1, tmr = -1; /* local and remote time-stamps */
|
|
|
|
|
long local_size = 0; /* the size of the local file */
|
2000-10-20 01:55:46 -04:00
|
|
|
|
size_t filename_len;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
struct http_stat hstat; /* HTTP status */
|
|
|
|
|
struct stat st;
|
2001-11-21 19:24:28 -05:00
|
|
|
|
char *dummy = NULL;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2001-04-10 12:04:18 -04:00
|
|
|
|
/* This used to be done in main(), but it's a better idea to do it
|
|
|
|
|
here so that we don't go through the hoops if we're just using
|
|
|
|
|
FTP or whatever. */
|
2002-04-20 16:46:38 -04:00
|
|
|
|
if (opt.cookies)
|
2001-04-24 22:29:54 -04:00
|
|
|
|
{
|
2002-04-20 16:46:38 -04:00
|
|
|
|
if (!wget_cookie_jar)
|
|
|
|
|
wget_cookie_jar = cookie_jar_new ();
|
|
|
|
|
if (opt.cookies_input && !cookies_loaded_p)
|
|
|
|
|
{
|
|
|
|
|
cookie_jar_load (wget_cookie_jar, opt.cookies_input);
|
|
|
|
|
cookies_loaded_p = 1;
|
|
|
|
|
}
|
2001-04-24 22:29:54 -04:00
|
|
|
|
}
|
2001-04-10 12:04:18 -04:00
|
|
|
|
|
1999-12-02 02:42:23 -05:00
|
|
|
|
*newloc = NULL;
|
|
|
|
|
|
|
|
|
|
/* Warn on (likely bogus) wildcard usage in HTTP. Don't use
|
2000-03-02 01:33:48 -05:00
|
|
|
|
has_wildcards_p because it would also warn on `?', and we know that
|
1999-12-02 02:42:23 -05:00
|
|
|
|
shows up in CGI paths a *lot*. */
|
|
|
|
|
if (strchr (u->url, '*'))
|
|
|
|
|
logputs (LOG_VERBOSE, _("Warning: wildcards not supported in HTTP.\n"));
|
|
|
|
|
|
|
|
|
|
/* Determine the local filename. */
|
2001-11-21 19:24:28 -05:00
|
|
|
|
if (local_file && *local_file)
|
|
|
|
|
hstat.local_file = local_file;
|
|
|
|
|
else if (local_file)
|
|
|
|
|
{
|
2003-09-14 18:04:13 -04:00
|
|
|
|
*local_file = url_file_name (u);
|
2001-11-21 19:24:28 -05:00
|
|
|
|
hstat.local_file = local_file;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2003-09-14 18:04:13 -04:00
|
|
|
|
dummy = url_file_name (u);
|
2001-11-21 19:24:28 -05:00
|
|
|
|
hstat.local_file = &dummy;
|
|
|
|
|
}
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
if (!opt.output_document)
|
2001-11-21 19:24:28 -05:00
|
|
|
|
locf = *hstat.local_file;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
else
|
|
|
|
|
locf = opt.output_document;
|
|
|
|
|
|
2001-11-21 19:24:28 -05:00
|
|
|
|
hstat.referer = referer;
|
|
|
|
|
|
|
|
|
|
filename_len = strlen (*hstat.local_file);
|
2001-04-28 12:36:46 -04:00
|
|
|
|
filename_plus_orig_suffix = alloca (filename_len + sizeof (".orig"));
|
2000-10-20 01:55:46 -04:00
|
|
|
|
|
2001-11-21 19:24:28 -05:00
|
|
|
|
if (opt.noclobber && file_exists_p (*hstat.local_file))
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
|
|
|
|
/* If opt.noclobber is turned on and file already exists, do not
|
|
|
|
|
retrieve the file */
|
|
|
|
|
logprintf (LOG_VERBOSE, _("\
|
2001-11-21 19:24:28 -05:00
|
|
|
|
File `%s' already there, will not retrieve.\n"), *hstat.local_file);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* If the file is there, we suppose it's retrieved OK. */
|
|
|
|
|
*dt |= RETROKF;
|
|
|
|
|
|
|
|
|
|
/* #### Bogusness alert. */
|
2002-04-12 14:53:39 -04:00
|
|
|
|
/* If its suffix is "html" or "htm" or similar, assume text/html. */
|
|
|
|
|
if (has_html_suffix_p (*hstat.local_file))
|
1999-12-02 02:42:23 -05:00
|
|
|
|
*dt |= TEXTHTML;
|
2001-11-21 19:24:28 -05:00
|
|
|
|
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (dummy);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return RETROK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
use_ts = 0;
|
|
|
|
|
if (opt.timestamping)
|
|
|
|
|
{
|
2003-11-02 14:56:37 -05:00
|
|
|
|
int local_dot_orig_file_exists = 0;
|
2000-03-02 01:33:48 -05:00
|
|
|
|
|
|
|
|
|
if (opt.backup_converted)
|
|
|
|
|
/* If -K is specified, we'll act on the assumption that it was specified
|
|
|
|
|
last time these files were downloaded as well, and instead of just
|
|
|
|
|
comparing local file X against server file X, we'll compare local
|
|
|
|
|
file X.orig (if extant, else X) against server file X. If -K
|
|
|
|
|
_wasn't_ specified last time, or the server contains files called
|
|
|
|
|
*.orig, -N will be back to not operating correctly with -k. */
|
|
|
|
|
{
|
2000-11-06 16:24:57 -05:00
|
|
|
|
/* Would a single s[n]printf() call be faster? --dan
|
|
|
|
|
|
2001-04-01 16:04:21 -04:00
|
|
|
|
Definitely not. sprintf() is horribly slow. It's a
|
|
|
|
|
different question whether the difference between the two
|
|
|
|
|
affects a program. Usually I'd say "no", but at one
|
|
|
|
|
point I profiled Wget, and found that a measurable and
|
2000-11-06 16:24:57 -05:00
|
|
|
|
non-negligible amount of time was lost calling sprintf()
|
|
|
|
|
in url.c. Replacing sprintf with inline calls to
|
|
|
|
|
strcpy() and long_to_string() made a difference.
|
|
|
|
|
--hniksic */
|
2001-11-21 19:24:28 -05:00
|
|
|
|
memcpy (filename_plus_orig_suffix, *hstat.local_file, filename_len);
|
2001-04-28 12:36:46 -04:00
|
|
|
|
memcpy (filename_plus_orig_suffix + filename_len,
|
|
|
|
|
".orig", sizeof (".orig"));
|
2000-03-02 01:33:48 -05:00
|
|
|
|
|
|
|
|
|
/* Try to stat() the .orig file. */
|
2001-04-28 12:36:46 -04:00
|
|
|
|
if (stat (filename_plus_orig_suffix, &st) == 0)
|
2000-03-02 01:33:48 -05:00
|
|
|
|
{
|
2003-11-02 14:56:37 -05:00
|
|
|
|
local_dot_orig_file_exists = 1;
|
2000-10-20 01:55:46 -04:00
|
|
|
|
local_filename = filename_plus_orig_suffix;
|
2000-03-02 01:33:48 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!local_dot_orig_file_exists)
|
|
|
|
|
/* Couldn't stat() <file>.orig, so try to stat() <file>. */
|
2001-11-21 19:24:28 -05:00
|
|
|
|
if (stat (*hstat.local_file, &st) == 0)
|
|
|
|
|
local_filename = *hstat.local_file;
|
2000-03-02 01:33:48 -05:00
|
|
|
|
|
2000-10-20 01:55:46 -04:00
|
|
|
|
if (local_filename != NULL)
|
2000-03-02 01:33:48 -05:00
|
|
|
|
/* There was a local file, so we'll check later to see if the version
|
|
|
|
|
the server has is the same version we already have, allowing us to
|
|
|
|
|
skip a download. */
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
|
|
|
|
use_ts = 1;
|
|
|
|
|
tml = st.st_mtime;
|
2002-04-12 15:21:02 -04:00
|
|
|
|
#ifdef WINDOWS
|
|
|
|
|
/* Modification time granularity is 2 seconds for Windows, so
|
|
|
|
|
increase local time by 1 second for later comparison. */
|
|
|
|
|
tml++;
|
|
|
|
|
#endif
|
1999-12-02 02:42:23 -05:00
|
|
|
|
local_size = st.st_size;
|
|
|
|
|
got_head = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Reset the counter. */
|
|
|
|
|
count = 0;
|
|
|
|
|
*dt = 0 | ACCEPTRANGES;
|
|
|
|
|
/* THE loop */
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
/* Increment the pass counter. */
|
|
|
|
|
++count;
|
2000-12-05 19:24:40 -05:00
|
|
|
|
sleep_between_retrievals (count);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* Get the current time string. */
|
|
|
|
|
tms = time_str (NULL);
|
|
|
|
|
/* Print fetch message, if opt.verbose. */
|
|
|
|
|
if (opt.verbose)
|
|
|
|
|
{
|
2001-11-21 19:24:28 -05:00
|
|
|
|
char *hurl = url_string (u, 1);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
char tmp[15];
|
|
|
|
|
strcpy (tmp, " ");
|
|
|
|
|
if (count > 1)
|
|
|
|
|
sprintf (tmp, _("(try:%2d)"), count);
|
|
|
|
|
logprintf (LOG_VERBOSE, "--%s-- %s\n %s => `%s'\n",
|
|
|
|
|
tms, hurl, tmp, locf);
|
|
|
|
|
#ifdef WINDOWS
|
|
|
|
|
ws_changetitle (hurl, 1);
|
|
|
|
|
#endif
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (hurl);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Default document type is empty. However, if spider mode is
|
|
|
|
|
on or time-stamping is employed, HEAD_ONLY commands is
|
|
|
|
|
encoded within *dt. */
|
|
|
|
|
if (opt.spider || (use_ts && !got_head))
|
|
|
|
|
*dt |= HEAD_ONLY;
|
|
|
|
|
else
|
|
|
|
|
*dt &= ~HEAD_ONLY;
|
|
|
|
|
/* Assume no restarting. */
|
|
|
|
|
hstat.restval = 0L;
|
|
|
|
|
/* Decide whether or not to restart. */
|
|
|
|
|
if (((count > 1 && (*dt & ACCEPTRANGES)) || opt.always_rest)
|
2001-05-07 15:08:29 -04:00
|
|
|
|
/* #### this calls access() and then stat(); could be optimized. */
|
2001-04-03 08:24:49 -04:00
|
|
|
|
&& file_exists_p (locf))
|
|
|
|
|
if (stat (locf, &st) == 0 && S_ISREG (st.st_mode))
|
1999-12-02 02:42:23 -05:00
|
|
|
|
hstat.restval = st.st_size;
|
2001-04-27 01:26:44 -04:00
|
|
|
|
|
2001-04-28 12:22:20 -04:00
|
|
|
|
/* In `-c' is used and the file is existing and non-empty,
|
|
|
|
|
refuse to truncate it if the server doesn't support continued
|
|
|
|
|
downloads. */
|
2001-05-08 07:47:05 -04:00
|
|
|
|
hstat.no_truncate = 0;
|
2001-04-28 12:22:20 -04:00
|
|
|
|
if (opt.always_rest && hstat.restval)
|
2001-05-07 15:08:29 -04:00
|
|
|
|
hstat.no_truncate = 1;
|
2001-04-28 12:22:20 -04:00
|
|
|
|
|
2001-04-27 01:26:44 -04:00
|
|
|
|
/* Decide whether to send the no-cache directive. We send it in
|
|
|
|
|
two cases:
|
|
|
|
|
a) we're using a proxy, and we're past our first retrieval.
|
|
|
|
|
Some proxies are notorious for caching incomplete data, so
|
|
|
|
|
we require a fresh get.
|
|
|
|
|
b) caching is explicitly inhibited. */
|
2001-11-21 19:24:28 -05:00
|
|
|
|
if ((proxy && count > 1) /* a */
|
|
|
|
|
|| !opt.allow_cache /* b */
|
2001-04-27 01:26:44 -04:00
|
|
|
|
)
|
1999-12-02 02:42:23 -05:00
|
|
|
|
*dt |= SEND_NOCACHE;
|
|
|
|
|
else
|
|
|
|
|
*dt &= ~SEND_NOCACHE;
|
|
|
|
|
|
2001-11-21 19:24:28 -05:00
|
|
|
|
/* Try fetching the document, or at least its head. */
|
|
|
|
|
err = gethttp (u, &hstat, dt, proxy);
|
2000-10-20 01:55:46 -04:00
|
|
|
|
|
|
|
|
|
/* It's unfortunate that wget determines the local filename before finding
|
|
|
|
|
out the Content-Type of the file. Barring a major restructuring of the
|
|
|
|
|
code, we need to re-set locf here, since gethttp() may have xrealloc()d
|
2001-11-21 19:24:28 -05:00
|
|
|
|
*hstat.local_file to tack on ".html". */
|
2000-10-20 01:55:46 -04:00
|
|
|
|
if (!opt.output_document)
|
2001-11-21 19:24:28 -05:00
|
|
|
|
locf = *hstat.local_file;
|
2000-10-20 01:55:46 -04:00
|
|
|
|
else
|
|
|
|
|
locf = opt.output_document;
|
|
|
|
|
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* Time? */
|
|
|
|
|
tms = time_str (NULL);
|
|
|
|
|
/* Get the new location (with or without the redirection). */
|
|
|
|
|
if (hstat.newloc)
|
|
|
|
|
*newloc = xstrdup (hstat.newloc);
|
|
|
|
|
switch (err)
|
|
|
|
|
{
|
|
|
|
|
case HERR: case HEOF: case CONSOCKERR: case CONCLOSED:
|
|
|
|
|
case CONERROR: case READERR: case WRITEFAILED:
|
|
|
|
|
case RANGEERR:
|
|
|
|
|
/* Non-fatal errors continue executing the loop, which will
|
|
|
|
|
bring them to "while" statement at the end, to judge
|
|
|
|
|
whether the number of tries was exceeded. */
|
2001-11-21 19:24:28 -05:00
|
|
|
|
free_hstat (&hstat);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
printwhat (count, opt.ntry);
|
|
|
|
|
continue;
|
|
|
|
|
break;
|
2003-10-29 18:13:25 -05:00
|
|
|
|
case HOSTERR: case CONIMPOSSIBLE: case PROXERR: case AUTHFAILED:
|
2001-04-01 16:04:21 -04:00
|
|
|
|
case SSLERRCTXCREATE: case CONTNOTSUPPORTED:
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* Fatal errors just return from the function. */
|
2001-11-21 19:24:28 -05:00
|
|
|
|
free_hstat (&hstat);
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (dummy);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return err;
|
|
|
|
|
break;
|
|
|
|
|
case FWRITEERR: case FOPENERR:
|
|
|
|
|
/* Another fatal error. */
|
|
|
|
|
logputs (LOG_VERBOSE, "\n");
|
|
|
|
|
logprintf (LOG_NOTQUIET, _("Cannot write to `%s' (%s).\n"),
|
2001-11-21 19:24:28 -05:00
|
|
|
|
*hstat.local_file, strerror (errno));
|
|
|
|
|
free_hstat (&hstat);
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (dummy);
|
2000-12-05 18:09:41 -05:00
|
|
|
|
return err;
|
|
|
|
|
break;
|
2001-04-01 16:04:21 -04:00
|
|
|
|
case CONSSLERR:
|
2000-12-05 18:09:41 -05:00
|
|
|
|
/* Another fatal error. */
|
|
|
|
|
logputs (LOG_VERBOSE, "\n");
|
|
|
|
|
logprintf (LOG_NOTQUIET, _("Unable to establish SSL connection.\n"));
|
2001-11-21 19:24:28 -05:00
|
|
|
|
free_hstat (&hstat);
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (dummy);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return err;
|
|
|
|
|
break;
|
|
|
|
|
case NEWLOCATION:
|
|
|
|
|
/* Return the new location to the caller. */
|
|
|
|
|
if (!hstat.newloc)
|
|
|
|
|
{
|
|
|
|
|
logprintf (LOG_NOTQUIET,
|
|
|
|
|
_("ERROR: Redirection (%d) without location.\n"),
|
|
|
|
|
hstat.statcode);
|
2001-11-21 19:24:28 -05:00
|
|
|
|
free_hstat (&hstat);
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (dummy);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return WRONGCODE;
|
|
|
|
|
}
|
2001-11-21 19:24:28 -05:00
|
|
|
|
free_hstat (&hstat);
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (dummy);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return NEWLOCATION;
|
|
|
|
|
break;
|
2001-04-28 12:22:20 -04:00
|
|
|
|
case RETRUNNEEDED:
|
|
|
|
|
/* The file was already fully retrieved. */
|
2001-11-21 19:24:28 -05:00
|
|
|
|
free_hstat (&hstat);
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (dummy);
|
2001-04-28 12:22:20 -04:00
|
|
|
|
return RETROK;
|
|
|
|
|
break;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
case RETRFINISHED:
|
|
|
|
|
/* Deal with you later. */
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
/* All possibilities should have been exhausted. */
|
|
|
|
|
abort ();
|
|
|
|
|
}
|
|
|
|
|
if (!(*dt & RETROKF))
|
|
|
|
|
{
|
|
|
|
|
if (!opt.verbose)
|
|
|
|
|
{
|
|
|
|
|
/* #### Ugly ugly ugly! */
|
2001-11-21 19:24:28 -05:00
|
|
|
|
char *hurl = url_string (u, 1);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
logprintf (LOG_NONVERBOSE, "%s:\n", hurl);
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (hurl);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
logprintf (LOG_NOTQUIET, _("%s ERROR %d: %s.\n"),
|
|
|
|
|
tms, hstat.statcode, hstat.error);
|
|
|
|
|
logputs (LOG_VERBOSE, "\n");
|
2001-11-21 19:24:28 -05:00
|
|
|
|
free_hstat (&hstat);
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (dummy);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return WRONGCODE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Did we get the time-stamp? */
|
|
|
|
|
if (!got_head)
|
|
|
|
|
{
|
|
|
|
|
if (opt.timestamping && !hstat.remote_time)
|
|
|
|
|
{
|
|
|
|
|
logputs (LOG_NOTQUIET, _("\
|
|
|
|
|
Last-modified header missing -- time-stamps turned off.\n"));
|
|
|
|
|
}
|
|
|
|
|
else if (hstat.remote_time)
|
|
|
|
|
{
|
|
|
|
|
/* Convert the date-string into struct tm. */
|
|
|
|
|
tmr = http_atotm (hstat.remote_time);
|
|
|
|
|
if (tmr == (time_t) (-1))
|
|
|
|
|
logputs (LOG_VERBOSE, _("\
|
|
|
|
|
Last-modified header invalid -- time-stamp ignored.\n"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* The time-stamping section. */
|
|
|
|
|
if (use_ts)
|
|
|
|
|
{
|
|
|
|
|
got_head = 1;
|
|
|
|
|
*dt &= ~HEAD_ONLY;
|
|
|
|
|
use_ts = 0; /* no more time-stamping */
|
|
|
|
|
count = 0; /* the retrieve count for HEAD is
|
|
|
|
|
reset */
|
|
|
|
|
if (hstat.remote_time && tmr != (time_t) (-1))
|
|
|
|
|
{
|
|
|
|
|
/* Now time-stamping can be used validly. Time-stamping
|
|
|
|
|
means that if the sizes of the local and remote file
|
|
|
|
|
match, and local file is newer than the remote file,
|
|
|
|
|
it will not be retrieved. Otherwise, the normal
|
|
|
|
|
download procedure is resumed. */
|
|
|
|
|
if (tml >= tmr &&
|
|
|
|
|
(hstat.contlen == -1 || local_size == hstat.contlen))
|
|
|
|
|
{
|
2000-10-20 01:55:46 -04:00
|
|
|
|
logprintf (LOG_VERBOSE, _("\
|
|
|
|
|
Server file no newer than local file `%s' -- not retrieving.\n\n"),
|
|
|
|
|
local_filename);
|
2001-11-21 19:24:28 -05:00
|
|
|
|
free_hstat (&hstat);
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (dummy);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return RETROK;
|
|
|
|
|
}
|
|
|
|
|
else if (tml >= tmr)
|
|
|
|
|
logprintf (LOG_VERBOSE, _("\
|
2000-03-02 01:33:48 -05:00
|
|
|
|
The sizes do not match (local %ld) -- retrieving.\n"), local_size);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
else
|
|
|
|
|
logputs (LOG_VERBOSE,
|
|
|
|
|
_("Remote file is newer, retrieving.\n"));
|
|
|
|
|
}
|
2001-11-21 19:24:28 -05:00
|
|
|
|
free_hstat (&hstat);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2000-12-10 19:31:14 -05:00
|
|
|
|
if ((tmr != (time_t) (-1))
|
1999-12-02 02:42:23 -05:00
|
|
|
|
&& !opt.spider
|
|
|
|
|
&& ((hstat.len == hstat.contlen) ||
|
|
|
|
|
((hstat.res == 0) &&
|
|
|
|
|
((hstat.contlen == -1) ||
|
|
|
|
|
(hstat.len >= hstat.contlen && !opt.kill_longer)))))
|
|
|
|
|
{
|
2000-12-10 19:47:44 -05:00
|
|
|
|
/* #### This code repeats in http.c and ftp.c. Move it to a
|
|
|
|
|
function! */
|
|
|
|
|
const char *fl = NULL;
|
|
|
|
|
if (opt.output_document)
|
|
|
|
|
{
|
|
|
|
|
if (opt.od_known_regular)
|
|
|
|
|
fl = opt.output_document;
|
|
|
|
|
}
|
|
|
|
|
else
|
2001-11-21 19:24:28 -05:00
|
|
|
|
fl = *hstat.local_file;
|
2000-12-10 19:47:44 -05:00
|
|
|
|
if (fl)
|
|
|
|
|
touch (fl, tmr);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
/* End of time-stamping section. */
|
|
|
|
|
|
|
|
|
|
if (opt.spider)
|
|
|
|
|
{
|
|
|
|
|
logprintf (LOG_NOTQUIET, "%d %s\n\n", hstat.statcode, hstat.error);
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (dummy);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return RETROK;
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-24 23:46:26 -05:00
|
|
|
|
tmrate = retr_rate (hstat.len - hstat.restval, hstat.dltime, 0);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
if (hstat.len == hstat.contlen)
|
|
|
|
|
{
|
|
|
|
|
if (*dt & RETROKF)
|
|
|
|
|
{
|
|
|
|
|
logprintf (LOG_VERBOSE,
|
|
|
|
|
_("%s (%s) - `%s' saved [%ld/%ld]\n\n"),
|
|
|
|
|
tms, tmrate, locf, hstat.len, hstat.contlen);
|
|
|
|
|
logprintf (LOG_NONVERBOSE,
|
|
|
|
|
"%s URL:%s [%ld/%ld] -> \"%s\" [%d]\n",
|
|
|
|
|
tms, u->url, hstat.len, hstat.contlen, locf, count);
|
|
|
|
|
}
|
|
|
|
|
++opt.numurls;
|
2003-10-11 09:57:11 -04:00
|
|
|
|
total_downloaded_bytes += hstat.len;
|
2000-10-20 01:55:46 -04:00
|
|
|
|
|
|
|
|
|
/* Remember that we downloaded the file for later ".orig" code. */
|
|
|
|
|
if (*dt & ADDED_HTML_EXTENSION)
|
|
|
|
|
downloaded_file(FILE_DOWNLOADED_AND_HTML_EXTENSION_ADDED, locf);
|
|
|
|
|
else
|
|
|
|
|
downloaded_file(FILE_DOWNLOADED_NORMALLY, locf);
|
|
|
|
|
|
2001-11-21 19:24:28 -05:00
|
|
|
|
free_hstat (&hstat);
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (dummy);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return RETROK;
|
|
|
|
|
}
|
|
|
|
|
else if (hstat.res == 0) /* No read error */
|
|
|
|
|
{
|
2000-03-02 01:33:48 -05:00
|
|
|
|
if (hstat.contlen == -1) /* We don't know how much we were supposed
|
|
|
|
|
to get, so assume we succeeded. */
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
|
|
|
|
if (*dt & RETROKF)
|
|
|
|
|
{
|
|
|
|
|
logprintf (LOG_VERBOSE,
|
|
|
|
|
_("%s (%s) - `%s' saved [%ld]\n\n"),
|
|
|
|
|
tms, tmrate, locf, hstat.len);
|
|
|
|
|
logprintf (LOG_NONVERBOSE,
|
|
|
|
|
"%s URL:%s [%ld] -> \"%s\" [%d]\n",
|
|
|
|
|
tms, u->url, hstat.len, locf, count);
|
|
|
|
|
}
|
|
|
|
|
++opt.numurls;
|
2003-10-11 09:57:11 -04:00
|
|
|
|
total_downloaded_bytes += hstat.len;
|
2000-10-20 01:55:46 -04:00
|
|
|
|
|
|
|
|
|
/* Remember that we downloaded the file for later ".orig" code. */
|
|
|
|
|
if (*dt & ADDED_HTML_EXTENSION)
|
|
|
|
|
downloaded_file(FILE_DOWNLOADED_AND_HTML_EXTENSION_ADDED, locf);
|
|
|
|
|
else
|
|
|
|
|
downloaded_file(FILE_DOWNLOADED_NORMALLY, locf);
|
|
|
|
|
|
2001-11-21 19:24:28 -05:00
|
|
|
|
free_hstat (&hstat);
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (dummy);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return RETROK;
|
|
|
|
|
}
|
|
|
|
|
else if (hstat.len < hstat.contlen) /* meaning we lost the
|
|
|
|
|
connection too soon */
|
|
|
|
|
{
|
|
|
|
|
logprintf (LOG_VERBOSE,
|
|
|
|
|
_("%s (%s) - Connection closed at byte %ld. "),
|
|
|
|
|
tms, tmrate, hstat.len);
|
|
|
|
|
printwhat (count, opt.ntry);
|
2001-11-21 19:24:28 -05:00
|
|
|
|
free_hstat (&hstat);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else if (!opt.kill_longer) /* meaning we got more than expected */
|
|
|
|
|
{
|
|
|
|
|
logprintf (LOG_VERBOSE,
|
|
|
|
|
_("%s (%s) - `%s' saved [%ld/%ld])\n\n"),
|
|
|
|
|
tms, tmrate, locf, hstat.len, hstat.contlen);
|
|
|
|
|
logprintf (LOG_NONVERBOSE,
|
|
|
|
|
"%s URL:%s [%ld/%ld] -> \"%s\" [%d]\n",
|
|
|
|
|
tms, u->url, hstat.len, hstat.contlen, locf, count);
|
|
|
|
|
++opt.numurls;
|
2003-10-11 09:57:11 -04:00
|
|
|
|
total_downloaded_bytes += hstat.len;
|
2000-10-20 01:55:46 -04:00
|
|
|
|
|
|
|
|
|
/* Remember that we downloaded the file for later ".orig" code. */
|
|
|
|
|
if (*dt & ADDED_HTML_EXTENSION)
|
|
|
|
|
downloaded_file(FILE_DOWNLOADED_AND_HTML_EXTENSION_ADDED, locf);
|
|
|
|
|
else
|
|
|
|
|
downloaded_file(FILE_DOWNLOADED_NORMALLY, locf);
|
|
|
|
|
|
2001-11-21 19:24:28 -05:00
|
|
|
|
free_hstat (&hstat);
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (dummy);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return RETROK;
|
|
|
|
|
}
|
|
|
|
|
else /* the same, but not accepted */
|
|
|
|
|
{
|
|
|
|
|
logprintf (LOG_VERBOSE,
|
|
|
|
|
_("%s (%s) - Connection closed at byte %ld/%ld. "),
|
|
|
|
|
tms, tmrate, hstat.len, hstat.contlen);
|
|
|
|
|
printwhat (count, opt.ntry);
|
2001-11-21 19:24:28 -05:00
|
|
|
|
free_hstat (&hstat);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else /* now hstat.res can only be -1 */
|
|
|
|
|
{
|
|
|
|
|
if (hstat.contlen == -1)
|
|
|
|
|
{
|
|
|
|
|
logprintf (LOG_VERBOSE,
|
|
|
|
|
_("%s (%s) - Read error at byte %ld (%s)."),
|
|
|
|
|
tms, tmrate, hstat.len, strerror (errno));
|
|
|
|
|
printwhat (count, opt.ntry);
|
2001-11-21 19:24:28 -05:00
|
|
|
|
free_hstat (&hstat);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else /* hstat.res == -1 and contlen is given */
|
|
|
|
|
{
|
|
|
|
|
logprintf (LOG_VERBOSE,
|
|
|
|
|
_("%s (%s) - Read error at byte %ld/%ld (%s). "),
|
|
|
|
|
tms, tmrate, hstat.len, hstat.contlen,
|
|
|
|
|
strerror (errno));
|
|
|
|
|
printwhat (count, opt.ntry);
|
2001-11-21 19:24:28 -05:00
|
|
|
|
free_hstat (&hstat);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* not reached */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
while (!opt.ntry || (count < opt.ntry));
|
|
|
|
|
return TRYLIMEXC;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Converts struct tm to time_t, assuming the data in tm is UTC rather
|
2001-04-27 01:11:38 -04:00
|
|
|
|
than local timezone.
|
|
|
|
|
|
|
|
|
|
mktime is similar but assumes struct tm, also known as the
|
|
|
|
|
"broken-down" form of time, is in local time zone. mktime_from_utc
|
|
|
|
|
uses mktime to make the conversion understanding that an offset
|
|
|
|
|
will be introduced by the local time assumption.
|
|
|
|
|
|
|
|
|
|
mktime_from_utc then measures the introduced offset by applying
|
|
|
|
|
gmtime to the initial result and applying mktime to the resulting
|
|
|
|
|
"broken-down" form. The difference between the two mktime results
|
|
|
|
|
is the measured offset which is then subtracted from the initial
|
|
|
|
|
mktime result to yield a calendar time which is the value returned.
|
|
|
|
|
|
|
|
|
|
tm_isdst in struct tm is set to 0 to force mktime to introduce a
|
|
|
|
|
consistent offset (the non DST offset) since tm and tm+o might be
|
|
|
|
|
on opposite sides of a DST change.
|
|
|
|
|
|
|
|
|
|
Some implementations of mktime return -1 for the nonexistent
|
|
|
|
|
localtime hour at the beginning of DST. In this event, use
|
|
|
|
|
mktime(tm - 1hr) + 3600.
|
|
|
|
|
|
|
|
|
|
Schematically
|
|
|
|
|
mktime(tm) --> t+o
|
|
|
|
|
gmtime(t+o) --> tm+o
|
|
|
|
|
mktime(tm+o) --> t+2o
|
|
|
|
|
t+o - (t+2o - t+o) = t
|
|
|
|
|
|
|
|
|
|
Note that glibc contains a function of the same purpose named
|
|
|
|
|
`timegm' (reverse of gmtime). But obviously, it is not universally
|
|
|
|
|
available, and unfortunately it is not straightforwardly
|
|
|
|
|
extractable for use here. Perhaps configure should detect timegm
|
|
|
|
|
and use it where available.
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
Contributed by Roger Beeman <beeman@cisco.com>, with the help of
|
2001-04-24 20:50:22 -04:00
|
|
|
|
Mark Baushke <mdb@cisco.com> and the rest of the Gurus at CISCO.
|
|
|
|
|
Further improved by Roger with assistance from Edward J. Sabol
|
|
|
|
|
based on input by Jamie Zawinski. */
|
|
|
|
|
|
1999-12-02 02:42:23 -05:00
|
|
|
|
static time_t
|
|
|
|
|
mktime_from_utc (struct tm *t)
|
|
|
|
|
{
|
|
|
|
|
time_t tl, tb;
|
2001-04-24 20:50:22 -04:00
|
|
|
|
struct tm *tg;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
tl = mktime (t);
|
|
|
|
|
if (tl == -1)
|
2001-04-24 20:50:22 -04:00
|
|
|
|
{
|
|
|
|
|
t->tm_hour--;
|
|
|
|
|
tl = mktime (t);
|
|
|
|
|
if (tl == -1)
|
|
|
|
|
return -1; /* can't deal with output from strptime */
|
|
|
|
|
tl += 3600;
|
|
|
|
|
}
|
|
|
|
|
tg = gmtime (&tl);
|
|
|
|
|
tg->tm_isdst = 0;
|
|
|
|
|
tb = mktime (tg);
|
|
|
|
|
if (tb == -1)
|
|
|
|
|
{
|
|
|
|
|
tg->tm_hour--;
|
|
|
|
|
tb = mktime (tg);
|
|
|
|
|
if (tb == -1)
|
|
|
|
|
return -1; /* can't deal with output from gmtime */
|
|
|
|
|
tb += 3600;
|
|
|
|
|
}
|
|
|
|
|
return (tl - (tb - tl));
|
1999-12-02 02:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check whether the result of strptime() indicates success.
|
|
|
|
|
strptime() returns the pointer to how far it got to in the string.
|
|
|
|
|
The processing has been successful if the string is at `GMT' or
|
|
|
|
|
`+X', or at the end of the string.
|
|
|
|
|
|
|
|
|
|
In extended regexp parlance, the function returns 1 if P matches
|
2001-04-27 01:11:38 -04:00
|
|
|
|
"^ *(GMT|[+-][0-9]|$)", 0 otherwise. P being NULL (which strptime
|
|
|
|
|
can return) is considered a failure and 0 is returned. */
|
1999-12-02 02:42:23 -05:00
|
|
|
|
static int
|
2000-11-20 05:25:28 -05:00
|
|
|
|
check_end (const char *p)
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
|
|
|
|
if (!p)
|
|
|
|
|
return 0;
|
|
|
|
|
while (ISSPACE (*p))
|
|
|
|
|
++p;
|
|
|
|
|
if (!*p
|
|
|
|
|
|| (p[0] == 'G' && p[1] == 'M' && p[2] == 'T')
|
2000-12-17 14:31:30 -05:00
|
|
|
|
|| ((p[0] == '+' || p[0] == '-') && ISDIGIT (p[1])))
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return 1;
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-04-27 01:11:38 -04:00
|
|
|
|
/* Convert the textual specification of time in TIME_STRING to the
|
|
|
|
|
number of seconds since the Epoch.
|
|
|
|
|
|
|
|
|
|
TIME_STRING can be in any of the three formats RFC2068 allows the
|
|
|
|
|
HTTP servers to emit -- RFC1123-date, RFC850-date or asctime-date.
|
|
|
|
|
Timezones are ignored, and should be GMT.
|
|
|
|
|
|
|
|
|
|
Return the computed time_t representation, or -1 if the conversion
|
|
|
|
|
fails.
|
|
|
|
|
|
|
|
|
|
This function uses strptime with various string formats for parsing
|
|
|
|
|
TIME_STRING. This results in a parser that is not as lenient in
|
|
|
|
|
interpreting TIME_STRING as I would like it to be. Being based on
|
|
|
|
|
strptime, it always allows shortened months, one-digit days, etc.,
|
|
|
|
|
but due to the multitude of formats in which time can be
|
|
|
|
|
represented, an ideal HTTP time parser would be even more
|
|
|
|
|
forgiving. It should completely ignore things like week days and
|
|
|
|
|
concentrate only on the various forms of representing years,
|
|
|
|
|
months, days, hours, minutes, and seconds. For example, it would
|
|
|
|
|
be nice if it accepted ISO 8601 out of the box.
|
|
|
|
|
|
|
|
|
|
I've investigated free and PD code for this purpose, but none was
|
|
|
|
|
usable. getdate was big and unwieldy, and had potential copyright
|
|
|
|
|
issues, or so I was informed. Dr. Marcus Hennecke's atotm(),
|
|
|
|
|
distributed with phttpd, is excellent, but we cannot use it because
|
|
|
|
|
it is not assigned to the FSF. So I stuck it with strptime. */
|
|
|
|
|
|
2001-04-08 18:25:24 -04:00
|
|
|
|
time_t
|
2002-04-20 16:46:38 -04:00
|
|
|
|
http_atotm (const char *time_string)
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
2001-04-27 01:11:38 -04:00
|
|
|
|
/* NOTE: Solaris strptime man page claims that %n and %t match white
|
|
|
|
|
space, but that's not universally available. Instead, we simply
|
|
|
|
|
use ` ' to mean "skip all WS", which works under all strptime
|
|
|
|
|
implementations I've tested. */
|
|
|
|
|
|
|
|
|
|
static const char *time_formats[] = {
|
|
|
|
|
"%a, %d %b %Y %T", /* RFC1123: Thu, 29 Jan 1998 22:12:57 */
|
|
|
|
|
"%A, %d-%b-%y %T", /* RFC850: Thursday, 29-Jan-98 22:12:57 */
|
|
|
|
|
"%a, %d-%b-%Y %T", /* pseudo-RFC850: Thu, 29-Jan-1998 22:12:57
|
|
|
|
|
(google.com uses this for their cookies.) */
|
|
|
|
|
"%a %b %d %T %Y" /* asctime: Thu Jan 29 22:12:57 1998 */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int i;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
struct tm t;
|
|
|
|
|
|
2001-04-24 20:50:22 -04:00
|
|
|
|
/* According to Roger Beeman, we need to initialize tm_isdst, since
|
|
|
|
|
strptime won't do it. */
|
|
|
|
|
t.tm_isdst = 0;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
/* Note that under foreign locales Solaris strptime() fails to
|
2001-04-27 01:11:38 -04:00
|
|
|
|
recognize English dates, which renders this function useless. We
|
|
|
|
|
solve this by being careful not to affect LC_TIME when
|
|
|
|
|
initializing locale.
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2001-04-27 01:11:38 -04:00
|
|
|
|
Another solution would be to temporarily set locale to C, invoke
|
1999-12-02 02:42:23 -05:00
|
|
|
|
strptime(), and restore it back. This is slow and dirty,
|
|
|
|
|
however, and locale support other than LC_MESSAGES can mess other
|
|
|
|
|
things, so I rather chose to stick with just setting LC_MESSAGES.
|
|
|
|
|
|
2001-04-27 01:11:38 -04:00
|
|
|
|
GNU strptime does not have this problem because it recognizes
|
|
|
|
|
both international and local dates. */
|
|
|
|
|
|
2003-09-19 10:08:37 -04:00
|
|
|
|
for (i = 0; i < countof (time_formats); i++)
|
2001-04-27 01:11:38 -04:00
|
|
|
|
if (check_end (strptime (time_string, time_formats[i], &t)))
|
|
|
|
|
return mktime_from_utc (&t);
|
|
|
|
|
|
|
|
|
|
/* All formats have failed. */
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Authorization support: We support two authorization schemes:
|
|
|
|
|
|
|
|
|
|
* `Basic' scheme, consisting of base64-ing USER:PASSWORD string;
|
|
|
|
|
|
|
|
|
|
* `Digest' scheme, added by Junio Hamano <junio@twinsun.com>,
|
|
|
|
|
consisting of answering to the server's challenge with the proper
|
|
|
|
|
MD5 digests. */
|
|
|
|
|
|
|
|
|
|
/* How many bytes it will take to store LEN bytes in base64. */
|
|
|
|
|
#define BASE64_LENGTH(len) (4 * (((len) + 2) / 3))
|
|
|
|
|
|
|
|
|
|
/* Encode the string S of length LENGTH to base64 format and place it
|
|
|
|
|
to STORE. STORE will be 0-terminated, and must point to a writable
|
|
|
|
|
buffer of at least 1+BASE64_LENGTH(length) bytes. */
|
|
|
|
|
static void
|
|
|
|
|
base64_encode (const char *s, char *store, int length)
|
|
|
|
|
{
|
|
|
|
|
/* Conversion table. */
|
|
|
|
|
static char tbl[64] = {
|
|
|
|
|
'A','B','C','D','E','F','G','H',
|
|
|
|
|
'I','J','K','L','M','N','O','P',
|
|
|
|
|
'Q','R','S','T','U','V','W','X',
|
|
|
|
|
'Y','Z','a','b','c','d','e','f',
|
|
|
|
|
'g','h','i','j','k','l','m','n',
|
|
|
|
|
'o','p','q','r','s','t','u','v',
|
|
|
|
|
'w','x','y','z','0','1','2','3',
|
|
|
|
|
'4','5','6','7','8','9','+','/'
|
|
|
|
|
};
|
|
|
|
|
int i;
|
|
|
|
|
unsigned char *p = (unsigned char *)store;
|
|
|
|
|
|
|
|
|
|
/* Transform the 3x8 bits to 4x6 bits, as required by base64. */
|
|
|
|
|
for (i = 0; i < length; i += 3)
|
|
|
|
|
{
|
|
|
|
|
*p++ = tbl[s[0] >> 2];
|
|
|
|
|
*p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)];
|
|
|
|
|
*p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)];
|
|
|
|
|
*p++ = tbl[s[2] & 0x3f];
|
|
|
|
|
s += 3;
|
|
|
|
|
}
|
|
|
|
|
/* Pad the result if necessary... */
|
|
|
|
|
if (i == length + 1)
|
|
|
|
|
*(p - 1) = '=';
|
|
|
|
|
else if (i == length + 2)
|
|
|
|
|
*(p - 1) = *(p - 2) = '=';
|
|
|
|
|
/* ...and zero-terminate it. */
|
|
|
|
|
*p = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Create the authentication header contents for the `Basic' scheme.
|
|
|
|
|
This is done by encoding the string `USER:PASS' in base64 and
|
|
|
|
|
prepending `HEADER: Basic ' to it. */
|
|
|
|
|
static char *
|
2003-11-29 13:40:01 -05:00
|
|
|
|
basic_authentication_encode (const char *user, const char *passwd)
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
|
|
|
|
char *t1, *t2, *res;
|
|
|
|
|
int len1 = strlen (user) + 1 + strlen (passwd);
|
|
|
|
|
int len2 = BASE64_LENGTH (len1);
|
|
|
|
|
|
|
|
|
|
t1 = (char *)alloca (len1 + 1);
|
|
|
|
|
sprintf (t1, "%s:%s", user, passwd);
|
2003-11-29 13:40:01 -05:00
|
|
|
|
|
|
|
|
|
t2 = (char *)alloca (len2 + 1);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
base64_encode (t1, t2, len1);
|
2003-11-29 13:40:01 -05:00
|
|
|
|
|
|
|
|
|
res = (char *)xmalloc (6 + len2 + 1);
|
|
|
|
|
sprintf (res, "Basic %s", t2);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
#define SKIP_WS(x) do { \
|
|
|
|
|
while (ISSPACE (*(x))) \
|
|
|
|
|
++(x); \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
1999-12-02 02:42:23 -05:00
|
|
|
|
#ifdef USE_DIGEST
|
|
|
|
|
/* Parse HTTP `WWW-Authenticate:' header. AU points to the beginning
|
|
|
|
|
of a field in such a header. If the field is the one specified by
|
|
|
|
|
ATTR_NAME ("realm", "opaque", and "nonce" are used by the current
|
|
|
|
|
digest authorization code), extract its value in the (char*)
|
|
|
|
|
variable pointed by RET. Returns negative on a malformed header,
|
|
|
|
|
or number of bytes that have been parsed by this call. */
|
|
|
|
|
static int
|
|
|
|
|
extract_header_attr (const char *au, const char *attr_name, char **ret)
|
|
|
|
|
{
|
|
|
|
|
const char *cp, *ep;
|
|
|
|
|
|
|
|
|
|
ep = cp = au;
|
|
|
|
|
|
|
|
|
|
if (strncmp (cp, attr_name, strlen (attr_name)) == 0)
|
|
|
|
|
{
|
|
|
|
|
cp += strlen (attr_name);
|
|
|
|
|
if (!*cp)
|
|
|
|
|
return -1;
|
2003-11-27 18:29:36 -05:00
|
|
|
|
SKIP_WS (cp);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
if (*cp != '=')
|
|
|
|
|
return -1;
|
|
|
|
|
if (!*++cp)
|
|
|
|
|
return -1;
|
2003-11-27 18:29:36 -05:00
|
|
|
|
SKIP_WS (cp);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
if (*cp != '\"')
|
|
|
|
|
return -1;
|
|
|
|
|
if (!*++cp)
|
|
|
|
|
return -1;
|
|
|
|
|
for (ep = cp; *ep && *ep != '\"'; ep++)
|
|
|
|
|
;
|
|
|
|
|
if (!*ep)
|
|
|
|
|
return -1;
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (*ret);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
*ret = strdupdelim (cp, ep);
|
|
|
|
|
return ep - au + 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Dump the hexadecimal representation of HASH to BUF. HASH should be
|
|
|
|
|
an array of 16 bytes containing the hash keys, and BUF should be a
|
|
|
|
|
buffer of 33 writable characters (32 for hex digits plus one for
|
|
|
|
|
zero termination). */
|
|
|
|
|
static void
|
|
|
|
|
dump_hash (unsigned char *buf, const unsigned char *hash)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < MD5_HASHLEN; i++, hash++)
|
|
|
|
|
{
|
2003-09-19 10:08:37 -04:00
|
|
|
|
*buf++ = XNUM_TO_digit (*hash >> 4);
|
|
|
|
|
*buf++ = XNUM_TO_digit (*hash & 0xf);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
*buf = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Take the line apart to find the challenge, and compose a digest
|
|
|
|
|
authorization header. See RFC2069 section 2.1.2. */
|
2001-12-08 20:24:41 -05:00
|
|
|
|
static char *
|
1999-12-02 02:42:23 -05:00
|
|
|
|
digest_authentication_encode (const char *au, const char *user,
|
|
|
|
|
const char *passwd, const char *method,
|
|
|
|
|
const char *path)
|
|
|
|
|
{
|
|
|
|
|
static char *realm, *opaque, *nonce;
|
|
|
|
|
static struct {
|
|
|
|
|
const char *name;
|
|
|
|
|
char **variable;
|
|
|
|
|
} options[] = {
|
|
|
|
|
{ "realm", &realm },
|
|
|
|
|
{ "opaque", &opaque },
|
|
|
|
|
{ "nonce", &nonce }
|
|
|
|
|
};
|
|
|
|
|
char *res;
|
|
|
|
|
|
|
|
|
|
realm = opaque = nonce = NULL;
|
|
|
|
|
|
|
|
|
|
au += 6; /* skip over `Digest' */
|
|
|
|
|
while (*au)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
2003-11-27 18:29:36 -05:00
|
|
|
|
SKIP_WS (au);
|
2003-09-19 10:08:37 -04:00
|
|
|
|
for (i = 0; i < countof (options); i++)
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
|
|
|
|
int skip = extract_header_attr (au, options[i].name,
|
|
|
|
|
options[i].variable);
|
|
|
|
|
if (skip < 0)
|
|
|
|
|
{
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (realm);
|
|
|
|
|
xfree_null (opaque);
|
|
|
|
|
xfree_null (nonce);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
else if (skip)
|
|
|
|
|
{
|
|
|
|
|
au += skip;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-09-19 10:08:37 -04:00
|
|
|
|
if (i == countof (options))
|
1999-12-02 02:42:23 -05:00
|
|
|
|
{
|
|
|
|
|
while (*au && *au != '=')
|
|
|
|
|
au++;
|
|
|
|
|
if (*au && *++au)
|
|
|
|
|
{
|
2003-11-27 18:29:36 -05:00
|
|
|
|
SKIP_WS (au);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
if (*au == '\"')
|
|
|
|
|
{
|
|
|
|
|
au++;
|
|
|
|
|
while (*au && *au != '\"')
|
|
|
|
|
au++;
|
|
|
|
|
if (*au)
|
|
|
|
|
au++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (*au && *au != ',')
|
|
|
|
|
au++;
|
|
|
|
|
if (*au)
|
|
|
|
|
au++;
|
|
|
|
|
}
|
|
|
|
|
if (!realm || !nonce || !user || !passwd || !path || !method)
|
|
|
|
|
{
|
2003-11-02 14:56:37 -05:00
|
|
|
|
xfree_null (realm);
|
|
|
|
|
xfree_null (opaque);
|
|
|
|
|
xfree_null (nonce);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Calculate the digest value. */
|
|
|
|
|
{
|
2001-11-29 13:17:54 -05:00
|
|
|
|
ALLOCA_MD5_CONTEXT (ctx);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
unsigned char hash[MD5_HASHLEN];
|
|
|
|
|
unsigned char a1buf[MD5_HASHLEN * 2 + 1], a2buf[MD5_HASHLEN * 2 + 1];
|
|
|
|
|
unsigned char response_digest[MD5_HASHLEN * 2 + 1];
|
|
|
|
|
|
|
|
|
|
/* A1BUF = H(user ":" realm ":" password) */
|
2001-11-29 13:17:54 -05:00
|
|
|
|
gen_md5_init (ctx);
|
2001-11-29 13:48:43 -05:00
|
|
|
|
gen_md5_update ((unsigned char *)user, strlen (user), ctx);
|
|
|
|
|
gen_md5_update ((unsigned char *)":", 1, ctx);
|
|
|
|
|
gen_md5_update ((unsigned char *)realm, strlen (realm), ctx);
|
|
|
|
|
gen_md5_update ((unsigned char *)":", 1, ctx);
|
|
|
|
|
gen_md5_update ((unsigned char *)passwd, strlen (passwd), ctx);
|
2001-11-29 13:17:54 -05:00
|
|
|
|
gen_md5_finish (ctx, hash);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
dump_hash (a1buf, hash);
|
|
|
|
|
|
|
|
|
|
/* A2BUF = H(method ":" path) */
|
2001-11-29 13:17:54 -05:00
|
|
|
|
gen_md5_init (ctx);
|
2001-11-29 13:48:43 -05:00
|
|
|
|
gen_md5_update ((unsigned char *)method, strlen (method), ctx);
|
|
|
|
|
gen_md5_update ((unsigned char *)":", 1, ctx);
|
|
|
|
|
gen_md5_update ((unsigned char *)path, strlen (path), ctx);
|
2001-11-29 13:17:54 -05:00
|
|
|
|
gen_md5_finish (ctx, hash);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
dump_hash (a2buf, hash);
|
|
|
|
|
|
|
|
|
|
/* RESPONSE_DIGEST = H(A1BUF ":" nonce ":" A2BUF) */
|
2001-11-29 13:17:54 -05:00
|
|
|
|
gen_md5_init (ctx);
|
|
|
|
|
gen_md5_update (a1buf, MD5_HASHLEN * 2, ctx);
|
2001-11-29 13:48:43 -05:00
|
|
|
|
gen_md5_update ((unsigned char *)":", 1, ctx);
|
|
|
|
|
gen_md5_update ((unsigned char *)nonce, strlen (nonce), ctx);
|
|
|
|
|
gen_md5_update ((unsigned char *)":", 1, ctx);
|
2001-11-29 13:17:54 -05:00
|
|
|
|
gen_md5_update (a2buf, MD5_HASHLEN * 2, ctx);
|
|
|
|
|
gen_md5_finish (ctx, hash);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
dump_hash (response_digest, hash);
|
|
|
|
|
|
|
|
|
|
res = (char*) xmalloc (strlen (user)
|
|
|
|
|
+ strlen (user)
|
|
|
|
|
+ strlen (realm)
|
|
|
|
|
+ strlen (nonce)
|
|
|
|
|
+ strlen (path)
|
|
|
|
|
+ 2 * MD5_HASHLEN /*strlen (response_digest)*/
|
|
|
|
|
+ (opaque ? strlen (opaque) : 0)
|
|
|
|
|
+ 128);
|
2003-11-29 13:40:01 -05:00
|
|
|
|
sprintf (res, "Digest \
|
1999-12-02 02:42:23 -05:00
|
|
|
|
username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
|
|
|
|
|
user, realm, nonce, path, response_digest);
|
|
|
|
|
if (opaque)
|
|
|
|
|
{
|
|
|
|
|
char *p = res + strlen (res);
|
|
|
|
|
strcat (p, ", opaque=\"");
|
|
|
|
|
strcat (p, opaque);
|
|
|
|
|
strcat (p, "\"");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
#endif /* USE_DIGEST */
|
|
|
|
|
|
|
|
|
|
|
2000-11-19 21:10:54 -05:00
|
|
|
|
#define BEGINS_WITH(line, string_constant) \
|
1999-12-02 02:42:23 -05:00
|
|
|
|
(!strncasecmp (line, string_constant, sizeof (string_constant) - 1) \
|
|
|
|
|
&& (ISSPACE (line[sizeof (string_constant) - 1]) \
|
|
|
|
|
|| !line[sizeof (string_constant) - 1]))
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
known_authentication_scheme_p (const char *au)
|
|
|
|
|
{
|
2000-11-19 21:10:54 -05:00
|
|
|
|
return BEGINS_WITH (au, "Basic")
|
|
|
|
|
|| BEGINS_WITH (au, "Digest")
|
|
|
|
|
|| BEGINS_WITH (au, "NTLM");
|
1999-12-02 02:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
|
2000-11-19 21:10:54 -05:00
|
|
|
|
#undef BEGINS_WITH
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
/* Create the HTTP authorization request header. When the
|
|
|
|
|
`WWW-Authenticate' response header is seen, according to the
|
|
|
|
|
authorization scheme specified in that header (`Basic' and `Digest'
|
|
|
|
|
are supported by the current implementation), produce an
|
|
|
|
|
appropriate HTTP authorization request header. */
|
|
|
|
|
static char *
|
|
|
|
|
create_authorization_line (const char *au, const char *user,
|
|
|
|
|
const char *passwd, const char *method,
|
|
|
|
|
const char *path)
|
|
|
|
|
{
|
2003-11-29 13:40:01 -05:00
|
|
|
|
if (0 == strncasecmp (au, "Basic", 5))
|
|
|
|
|
return basic_authentication_encode (user, passwd);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
#ifdef USE_DIGEST
|
2003-11-29 13:40:01 -05:00
|
|
|
|
if (0 == strncasecmp (au, "Digest", 6))
|
|
|
|
|
return digest_authentication_encode (au, user, passwd, method, path);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
#endif /* USE_DIGEST */
|
2003-11-29 13:40:01 -05:00
|
|
|
|
return NULL;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
}
|
2001-11-26 00:36:33 -05:00
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
http_cleanup (void)
|
|
|
|
|
{
|
|
|
|
|
}
|