[svn] Restructure generation of HTTP requests. Allow headers specified with

--header to override generated headers.
This commit is contained in:
hniksic 2003-11-29 10:40:01 -08:00
parent 83b0e38940
commit 0ff9eb4727
12 changed files with 625 additions and 378 deletions

6
NEWS
View File

@ -14,6 +14,12 @@ use of IPv4 and IPv6 respectively.
** Talking to SSL servers over proxies now actually works. ** Talking to SSL servers over proxies now actually works.
** The `--header' option can now be used to override generated
headers. For example, `wget --header="Host: foo.bar"
http://127.0.0.1' tells Wget to connect to localhost, but to specify
"foo.bar" in the `Host' header. In previous versions such use of
`--header' lead to duplicate headers in HTTP requests.
** The progress bar is now updated regularly even when the data does ** The progress bar is now updated regularly even when the data does
not arrive. not arrive.

View File

@ -1,3 +1,21 @@
2003-11-29 Hrvoje Niksic <hniksic@xemacs.org>
* cookies.c (cookie_header): Only generate the contents of the
header, not the leading "Cookie: " or the trailing "\r\n".
* http.c (gethttp): When adding headers specified with `--header',
allow them to override the headers generated by Wget.
* init.c (cmd_spec_header): Made opt.user_headers a vector.
* http.c (request_new): New function. Returns a request structure
which can be modified in various ways, most notably by adding HTTP
headers to the request.
(request_set_header): New function for adding the header to the
request. If the header is already available, it gets replaced.
(request_send): Construct and send the request.
(gethttp): Use the request_* functions to generate the request.
2003-11-28 Hrvoje Niksic <hniksic@xemacs.org> 2003-11-28 Hrvoje Niksic <hniksic@xemacs.org>
* http.c (gethttp): Don't include the Proxy-Authorization header * http.c (gethttp): Don't include the Proxy-Authorization header

View File

@ -65,3 +65,16 @@ char *alloca ();
#define _BSD_SOURCE #define _BSD_SOURCE
#endif /* NAMESPACE_TWEAKS */ #endif /* NAMESPACE_TWEAKS */
/* Determine whether to use stdarg. Use it only if the compiler
supports ANSI C and stdarg.h is present. We check for both because
there are configurations where stdarg.h exists, but doesn't work.
This check cannot be in sysdep.h because we use it to choose which
system headers to include. */
#ifndef WGET_USE_STDARG
# ifdef __STDC__
# ifdef HAVE_STDARG_H
# define WGET_USE_STDARG
# endif
# endif
#endif /* not WGET_USE_STDARG */

View File

@ -1192,16 +1192,12 @@ cookie_header (struct cookie_jar *jar, const char *host,
} }
/* Allocate output buffer: /* Allocate output buffer:
"Cookie: " -- 8
name=value pairs -- result_size name=value pairs -- result_size
"; " separators -- (count - 1) * 2 "; " separators -- (count - 1) * 2
\r\n line ending -- 2
\0 terminator -- 1 */ \0 terminator -- 1 */
result_size = 8 + result_size + (count - 1) * 2 + 2 + 1; result_size = result_size + (count - 1) * 2 + 1;
result = xmalloc (result_size); result = xmalloc (result_size);
pos = 0; pos = 0;
strcpy (result, "Cookie: ");
pos += 8;
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
struct cookie *c = outgoing[i].cookie; struct cookie *c = outgoing[i].cookie;
@ -1219,8 +1215,6 @@ cookie_header (struct cookie_jar *jar, const char *host,
result[pos++] = ' '; result[pos++] = ' ';
} }
} }
result[pos++] = '\r';
result[pos++] = '\n';
result[pos++] = '\0'; result[pos++] = '\0';
assert (pos == result_size); assert (pos == result_size);
return result; return result;

File diff suppressed because it is too large Load Diff

View File

@ -145,7 +145,7 @@ static struct {
{ "forcehtml", &opt.force_html, cmd_boolean }, { "forcehtml", &opt.force_html, cmd_boolean },
{ "ftpproxy", &opt.ftp_proxy, cmd_string }, { "ftpproxy", &opt.ftp_proxy, cmd_string },
{ "glob", &opt.ftp_glob, cmd_boolean }, { "glob", &opt.ftp_glob, cmd_boolean },
{ "header", NULL, cmd_spec_header }, { "header", &opt.user_headers, cmd_spec_header },
{ "htmlextension", &opt.html_extension, cmd_boolean }, { "htmlextension", &opt.html_extension, cmd_boolean },
{ "htmlify", NULL, cmd_spec_htmlify }, { "htmlify", NULL, cmd_spec_htmlify },
{ "httpkeepalive", &opt.http_keep_alive, cmd_boolean }, { "httpkeepalive", &opt.http_keep_alive, cmd_boolean },
@ -1040,32 +1040,13 @@ cmd_spec_dirstruct (const char *com, const char *val, void *closure)
static int static int
cmd_spec_header (const char *com, const char *val, void *closure) cmd_spec_header (const char *com, const char *val, void *closure)
{ {
if (!*val) if (!check_user_specified_header (val))
{ {
/* Empty header means reset headers. */ fprintf (stderr, _("%s: %s: Invalid header `%s'.\n"),
xfree_null (opt.user_header); exec_name, com, val);
opt.user_header = NULL; return 0;
} }
else return cmd_vector (com, val, closure);
{
int i;
if (!check_user_specified_header (val))
{
fprintf (stderr, _("%s: %s: Invalid header `%s'.\n"),
exec_name, com, val);
return 0;
}
i = opt.user_header ? strlen (opt.user_header) : 0;
opt.user_header = (char *)xrealloc (opt.user_header, i + strlen (val)
+ 2 + 1);
strcpy (opt.user_header + i, val);
i += strlen (val);
opt.user_header[i++] = '\r';
opt.user_header[i++] = '\n';
opt.user_header[i] = '\0';
}
return 1;
} }
static int static int

View File

@ -29,19 +29,6 @@ so, delete this exception statement from your version. */
#include <config.h> #include <config.h>
/* This allows the architecture-specific .h files to specify the use
of stdargs regardless of __STDC__. */
#ifndef WGET_USE_STDARG
/* Use stdarg only if the compiler supports ANSI C and stdarg.h is
present. We check for both because there are configurations where
stdarg.h exists, but doesn't work. */
# ifdef __STDC__
# ifdef HAVE_STDARG_H
# define WGET_USE_STDARG
# endif
# endif
#endif /* not WGET_USE_STDARG */
#include <stdio.h> #include <stdio.h>
#ifdef HAVE_STRING_H #ifdef HAVE_STRING_H
# include <string.h> # include <string.h>

View File

@ -33,13 +33,6 @@ so, delete this exception statement from your version. */
/* The log file to which Wget writes to after HUP. */ /* The log file to which Wget writes to after HUP. */
#define DEFAULT_LOGFILE "wget-log" #define DEFAULT_LOGFILE "wget-log"
/* Make gcc check for the format of logmsg() and debug_logmsg(). */
#ifdef __GNUC__
# define GCC_FORMAT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
#else /* not __GNUC__ */
# define GCC_FORMAT_ATTR(a, b)
#endif /* not __GNUC__ */
enum log_options { LOG_VERBOSE, LOG_NOTQUIET, LOG_NONVERBOSE, LOG_ALWAYS }; enum log_options { LOG_VERBOSE, LOG_NOTQUIET, LOG_NONVERBOSE, LOG_ALWAYS };
#ifdef HAVE_STDARG_H #ifdef HAVE_STDARG_H

View File

@ -96,7 +96,7 @@ struct options
char *http_user; /* HTTP user. */ char *http_user; /* HTTP user. */
char *http_passwd; /* HTTP password. */ char *http_passwd; /* HTTP password. */
char *user_header; /* User-defined header(s). */ char **user_headers; /* User-defined header(s). */
int http_keep_alive; /* whether we use keep-alive */ int http_keep_alive; /* whether we use keep-alive */
int use_proxy; /* Do we use proxy? */ int use_proxy; /* Do we use proxy? */

View File

@ -61,6 +61,11 @@ so, delete this exception statement from your version. */
#endif #endif
#include <fcntl.h> #include <fcntl.h>
#include <assert.h> #include <assert.h>
#ifdef WGET_USE_STDARG
# include <stdarg.h>
#else
# include <varargs.h>
#endif
/* For TIOCGWINSZ and friends: */ /* For TIOCGWINSZ and friends: */
#ifdef HAVE_SYS_IOCTL_H #ifdef HAVE_SYS_IOCTL_H
@ -176,6 +181,52 @@ sepstring (const char *s)
return res; return res;
} }
#ifdef WGET_USE_STDARG
# define VA_START(args, arg1) va_start (args, arg1)
#else
# define VA_START(args, ignored) va_start (args)
#endif
/* Like sprintf, but allocates a string of sufficient size with malloc
and returns it. GNU libc has a similar function named asprintf,
which requires the pointer to the string to be passed. */
char *
aprintf (const char *fmt, ...)
{
/* This function is implemented using vsnprintf, which we provide
for the systems that don't have it. Therefore, it should be 100%
portable. */
int size = 32;
char *str = xmalloc (size);
while (1)
{
int n;
va_list args;
/* See log_vprintf_internal for explanation why it's OK to rely
on the return value of vsnprintf. */
VA_START (args, fmt);
n = vsnprintf (str, size, fmt, args);
va_end (args);
/* If the printing worked, return the string. */
if (n > -1 && n < size)
return str;
/* Else try again with a larger buffer. */
if (n > -1) /* C99 */
size = n + 1; /* precisely what is needed */
else
size <<= 1; /* twice the old size */
str = xrealloc (str, size);
}
return NULL; /* unreached */
}
/* Return pointer to a static char[] buffer in which zero-terminated /* Return pointer to a static char[] buffer in which zero-terminated
string-representation of TM (in form hh:mm:ss) is printed. string-representation of TM (in form hh:mm:ss) is printed.

View File

@ -68,6 +68,13 @@ char **sepstring PARAMS ((const char *));
int frontcmp PARAMS ((const char *, const char *)); int frontcmp PARAMS ((const char *, const char *));
void fork_to_background PARAMS ((void)); void fork_to_background PARAMS ((void));
#ifdef HAVE_STDARG_H
char *aprintf PARAMS ((const char *, ...))
GCC_FORMAT_ATTR (1, 2);
#else /* not HAVE_STDARG_H */
char *aprintf ();
#endif /* not HAVE_STDARG_H */
void touch PARAMS ((const char *, time_t)); void touch PARAMS ((const char *, time_t));
int remove_link PARAMS ((const char *)); int remove_link PARAMS ((const char *));
int file_exists_p PARAMS ((const char *)); int file_exists_p PARAMS ((const char *));

View File

@ -101,6 +101,12 @@ so, delete this exception statement from your version. */
# define DEBUGP(x) DO_NOTHING # define DEBUGP(x) DO_NOTHING
#endif /* not ENABLE_DEBUG */ #endif /* not ENABLE_DEBUG */
#ifdef __GNUC__
# define GCC_FORMAT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
#else /* not __GNUC__ */
# define GCC_FORMAT_ATTR(a, b)
#endif /* not __GNUC__ */
/* Everything uses this, so include them here directly. */ /* Everything uses this, so include them here directly. */
#include "xmalloc.h" #include "xmalloc.h"