137535Sdes/*-
2262560Sdes * Copyright (c) 2000-2014 Dag-Erling Sm��rgrav
337535Sdes * All rights reserved.
437535Sdes *
537535Sdes * Redistribution and use in source and binary forms, with or without
637535Sdes * modification, are permitted provided that the following conditions
737535Sdes * are met:
837535Sdes * 1. Redistributions of source code must retain the above copyright
937535Sdes *    notice, this list of conditions and the following disclaimer
1037535Sdes *    in this position and unchanged.
1137535Sdes * 2. Redistributions in binary form must reproduce the above copyright
1237535Sdes *    notice, this list of conditions and the following disclaimer in the
1337535Sdes *    documentation and/or other materials provided with the distribution.
1437535Sdes * 3. The name of the author may not be used to endorse or promote products
1563012Sdes *    derived from this software without specific prior written permission.
1637535Sdes *
1737535Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1837535Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1937535Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2037535Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2137535Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2237535Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2337535Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2437535Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2537535Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2637535Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2737535Sdes */
2837535Sdes
2984203Sdillon#include <sys/cdefs.h>
3084203Sdillon__FBSDID("$FreeBSD$");
3184203Sdillon
3263236Sdes/*
3363236Sdes * The following copyright applies to the base64 code:
3463236Sdes *
3563236Sdes *-
3663236Sdes * Copyright 1997 Massachusetts Institute of Technology
3763236Sdes *
3863236Sdes * Permission to use, copy, modify, and distribute this software and
3963236Sdes * its documentation for any purpose and without fee is hereby
4063236Sdes * granted, provided that both the above copyright notice and this
4163236Sdes * permission notice appear in all copies, that both the above
4263236Sdes * copyright notice and this permission notice appear in all
4363236Sdes * supporting documentation, and that the name of M.I.T. not be used
4463236Sdes * in advertising or publicity pertaining to distribution of the
4563236Sdes * software without specific, written prior permission.  M.I.T. makes
4663236Sdes * no representations about the suitability of this software for any
4763236Sdes * purpose.  It is provided "as is" without express or implied
4863236Sdes * warranty.
4990267Sdes *
5063236Sdes * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
5163236Sdes * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
5263236Sdes * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
5363236Sdes * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
5463236Sdes * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
5563236Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
5663236Sdes * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
5763236Sdes * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
5863236Sdes * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
5963236Sdes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
6063236Sdes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6163236Sdes * SUCH DAMAGE.
6263236Sdes */
6363236Sdes
6437535Sdes#include <sys/param.h>
6560737Sume#include <sys/socket.h>
66186124Smurray#include <sys/time.h>
6737535Sdes
6863012Sdes#include <ctype.h>
6937535Sdes#include <err.h>
7063012Sdes#include <errno.h>
7160376Sdes#include <locale.h>
7260189Sdes#include <netdb.h>
7337608Sdes#include <stdarg.h>
7437535Sdes#include <stdio.h>
7537535Sdes#include <stdlib.h>
7637535Sdes#include <string.h>
7760376Sdes#include <time.h>
7837535Sdes#include <unistd.h>
79240496Sdes
80240496Sdes#ifdef WITH_SSL
81240496Sdes#include <openssl/md5.h>
82240496Sdes#define MD5Init(c) MD5_Init(c)
83240496Sdes#define MD5Update(c, data, len) MD5_Update(c, data, len)
84240496Sdes#define MD5Final(md, c) MD5_Final(md, c)
85240496Sdes#else
86202613Sdes#include <md5.h>
87240496Sdes#endif
8837535Sdes
89141958Skbyanc#include <netinet/in.h>
90141958Skbyanc#include <netinet/tcp.h>
91141958Skbyanc
9237535Sdes#include "fetch.h"
9340939Sdes#include "common.h"
9441862Sdes#include "httperr.h"
9537535Sdes
9663012Sdes/* Maximum number of redirects to follow */
97241839Seadler#define MAX_REDIRECT 20
9837535Sdes
9963012Sdes/* Symbolic names for reply codes we care about */
10063012Sdes#define HTTP_OK			200
10163012Sdes#define HTTP_PARTIAL		206
10263012Sdes#define HTTP_MOVED_PERM		301
10363012Sdes#define HTTP_MOVED_TEMP		302
10463012Sdes#define HTTP_SEE_OTHER		303
105186124Smurray#define HTTP_NOT_MODIFIED	304
106241841Seadler#define HTTP_USE_PROXY		305
107169386Sdes#define HTTP_TEMP_REDIRECT	307
108241840Seadler#define HTTP_PERM_REDIRECT	308
10963012Sdes#define HTTP_NEED_AUTH		401
11087317Sdes#define HTTP_NEED_PROXY_AUTH	407
111125696Sdes#define HTTP_BAD_RANGE		416
11263012Sdes#define HTTP_PROTOCOL_ERROR	999
11360196Sdes
11463012Sdes#define HTTP_REDIRECT(xyz) ((xyz) == HTTP_MOVED_PERM \
11590267Sdes			    || (xyz) == HTTP_MOVED_TEMP \
116169386Sdes			    || (xyz) == HTTP_TEMP_REDIRECT \
117241841Seadler			    || (xyz) == HTTP_USE_PROXY \
11890267Sdes			    || (xyz) == HTTP_SEE_OTHER)
11963012Sdes
12088771Sdes#define HTTP_ERROR(xyz) ((xyz) > 400 && (xyz) < 599)
12163012Sdes
12290267Sdes
12363012Sdes/*****************************************************************************
12463012Sdes * I/O functions for decoding chunked streams
12563012Sdes */
12663012Sdes
12797859Sdesstruct httpio
12837535Sdes{
12997858Sdes	conn_t		*conn;		/* connection */
13097866Sdes	int		 chunked;	/* chunked mode */
13197858Sdes	char		*buf;		/* chunk buffer */
13297866Sdes	size_t		 bufsize;	/* size of chunk buffer */
13397866Sdes	ssize_t		 buflen;	/* amount of data currently in buffer */
13497866Sdes	int		 bufpos;	/* current read offset in buffer */
13597858Sdes	int		 eof;		/* end-of-file flag */
13697858Sdes	int		 error;		/* error flag */
13797858Sdes	size_t		 chunksize;	/* remaining size of current chunk */
13863281Sdes#ifndef NDEBUG
13990267Sdes	size_t		 total;
14063012Sdes#endif
14137535Sdes};
14237535Sdes
14337608Sdes/*
14463012Sdes * Get next chunk header
14537608Sdes */
14637608Sdesstatic int
147174588Sdeshttp_new_chunk(struct httpio *io)
14837608Sdes{
14990267Sdes	char *p;
15090267Sdes
151174588Sdes	if (fetch_getln(io->conn) == -1)
15290267Sdes		return (-1);
15390267Sdes
154174761Sdes	if (io->conn->buflen < 2 || !isxdigit((unsigned char)*io->conn->buf))
15590267Sdes		return (-1);
15690267Sdes
157174761Sdes	for (p = io->conn->buf; *p && !isspace((unsigned char)*p); ++p) {
15890267Sdes		if (*p == ';')
15990267Sdes			break;
160174761Sdes		if (!isxdigit((unsigned char)*p))
16190267Sdes			return (-1);
162174761Sdes		if (isdigit((unsigned char)*p)) {
16397859Sdes			io->chunksize = io->chunksize * 16 +
16490267Sdes			    *p - '0';
16590267Sdes		} else {
16697859Sdes			io->chunksize = io->chunksize * 16 +
167176036Sdes			    10 + tolower((unsigned char)*p) - 'a';
16890267Sdes		}
16990267Sdes	}
17090267Sdes
17163281Sdes#ifndef NDEBUG
17290267Sdes	if (fetchDebug) {
17397859Sdes		io->total += io->chunksize;
17497859Sdes		if (io->chunksize == 0)
175106207Sdes			fprintf(stderr, "%s(): end of last chunk\n", __func__);
17690267Sdes		else
177106207Sdes			fprintf(stderr, "%s(): new chunk: %lu (%lu)\n",
178106207Sdes			    __func__, (unsigned long)io->chunksize,
179106207Sdes			    (unsigned long)io->total);
18090267Sdes	}
18163012Sdes#endif
18290267Sdes
18397859Sdes	return (io->chunksize);
18437608Sdes}
18537608Sdes
18637608Sdes/*
18797866Sdes * Grow the input buffer to at least len bytes
18897866Sdes */
18997866Sdesstatic inline int
190174588Sdeshttp_growbuf(struct httpio *io, size_t len)
19197866Sdes{
19297866Sdes	char *tmp;
19397866Sdes
19497866Sdes	if (io->bufsize >= len)
19597866Sdes		return (0);
19697866Sdes
19797866Sdes	if ((tmp = realloc(io->buf, len)) == NULL)
19897866Sdes		return (-1);
19997866Sdes	io->buf = tmp;
20097866Sdes	io->bufsize = len;
201106044Sdes	return (0);
20297866Sdes}
20397866Sdes
20497866Sdes/*
20537608Sdes * Fill the input buffer, do chunk decoding on the fly
20637608Sdes */
207262560Sdesstatic ssize_t
208174588Sdeshttp_fillbuf(struct httpio *io, size_t len)
20937535Sdes{
210230307Sdes	ssize_t nbytes;
211262560Sdes	char ch;
212230307Sdes
21397859Sdes	if (io->error)
21490267Sdes		return (-1);
21597859Sdes	if (io->eof)
21690267Sdes		return (0);
21790267Sdes
21897866Sdes	if (io->chunked == 0) {
219174588Sdes		if (http_growbuf(io, len) == -1)
22097866Sdes			return (-1);
221230307Sdes		if ((nbytes = fetch_read(io->conn, io->buf, len)) == -1) {
222230307Sdes			io->error = errno;
22397866Sdes			return (-1);
224106185Sdes		}
225230307Sdes		io->buflen = nbytes;
22697866Sdes		io->bufpos = 0;
22797866Sdes		return (io->buflen);
22897866Sdes	}
22997866Sdes
23097859Sdes	if (io->chunksize == 0) {
231174588Sdes		switch (http_new_chunk(io)) {
23290267Sdes		case -1:
233262560Sdes			io->error = EPROTO;
23490267Sdes			return (-1);
23590267Sdes		case 0:
23697859Sdes			io->eof = 1;
23790267Sdes			return (0);
23890267Sdes		}
23937535Sdes	}
24063012Sdes
24197866Sdes	if (len > io->chunksize)
24297866Sdes		len = io->chunksize;
243174588Sdes	if (http_growbuf(io, len) == -1)
24490267Sdes		return (-1);
245230307Sdes	if ((nbytes = fetch_read(io->conn, io->buf, len)) == -1) {
246230307Sdes		io->error = errno;
24797866Sdes		return (-1);
248106185Sdes	}
249230307Sdes	io->buflen = nbytes;
25097866Sdes	io->chunksize -= io->buflen;
25190267Sdes
25297859Sdes	if (io->chunksize == 0) {
253262560Sdes		if (fetch_read(io->conn, &ch, 1) != 1 || ch != '\r' ||
254262560Sdes		    fetch_read(io->conn, &ch, 1) != 1 || ch != '\n')
25590267Sdes			return (-1);
25690267Sdes	}
25790267Sdes
25897866Sdes	io->bufpos = 0;
25990267Sdes
26097866Sdes	return (io->buflen);
26137535Sdes}
26237535Sdes
26337608Sdes/*
26437608Sdes * Read function
26537608Sdes */
26637535Sdesstatic int
267174588Sdeshttp_readfn(void *v, char *buf, int len)
26837535Sdes{
26997859Sdes	struct httpio *io = (struct httpio *)v;
270262560Sdes	int rlen;
27163012Sdes
27297859Sdes	if (io->error)
27390267Sdes		return (-1);
27497859Sdes	if (io->eof)
27590267Sdes		return (0);
27663012Sdes
277262560Sdes	/* empty buffer */
278262560Sdes	if (!io->buf || io->bufpos == io->buflen) {
279262560Sdes		if ((rlen = http_fillbuf(io, len)) < 0) {
280262560Sdes			if ((errno = io->error) == EINTR)
281262560Sdes				io->error = 0;
282262560Sdes			return (-1);
283262560Sdes		} else if (rlen == 0) {
284262560Sdes			return (0);
285262560Sdes		}
28690267Sdes	}
28737535Sdes
288262560Sdes	rlen = io->buflen - io->bufpos;
289262560Sdes	if (len < rlen)
290262560Sdes		rlen = len;
291262560Sdes	memcpy(buf, io->buf + io->bufpos, rlen);
292262560Sdes	io->bufpos += rlen;
293262560Sdes	return (rlen);
29437535Sdes}
29537535Sdes
29637608Sdes/*
29737608Sdes * Write function
29837608Sdes */
29937535Sdesstatic int
300174588Sdeshttp_writefn(void *v, const char *buf, int len)
30137535Sdes{
30297859Sdes	struct httpio *io = (struct httpio *)v;
30390267Sdes
304174588Sdes	return (fetch_write(io->conn, buf, len));
30537535Sdes}
30637535Sdes
30737608Sdes/*
30837608Sdes * Close function
30937608Sdes */
31037535Sdesstatic int
311174588Sdeshttp_closefn(void *v)
31237535Sdes{
31397859Sdes	struct httpio *io = (struct httpio *)v;
31490267Sdes	int r;
31563012Sdes
316174588Sdes	r = fetch_close(io->conn);
31797859Sdes	if (io->buf)
31897859Sdes		free(io->buf);
31997859Sdes	free(io);
32090267Sdes	return (r);
32137535Sdes}
32237535Sdes
32337608Sdes/*
32463012Sdes * Wrap a file descriptor up
32537608Sdes */
32663012Sdesstatic FILE *
327174588Sdeshttp_funopen(conn_t *conn, int chunked)
32837535Sdes{
32997859Sdes	struct httpio *io;
33090267Sdes	FILE *f;
33163012Sdes
332109967Sdes	if ((io = calloc(1, sizeof(*io))) == NULL) {
333174588Sdes		fetch_syserr();
33490267Sdes		return (NULL);
33590267Sdes	}
33697859Sdes	io->conn = conn;
33797866Sdes	io->chunked = chunked;
338174588Sdes	f = funopen(io, http_readfn, http_writefn, NULL, http_closefn);
33990267Sdes	if (f == NULL) {
340174588Sdes		fetch_syserr();
34197859Sdes		free(io);
34290267Sdes		return (NULL);
34390267Sdes	}
34490267Sdes	return (f);
34563012Sdes}
34663012Sdes
34790267Sdes
34863012Sdes/*****************************************************************************
34963012Sdes * Helper functions for talking to the server and parsing its replies
35063012Sdes */
35163012Sdes
35263012Sdes/* Header types */
35363012Sdestypedef enum {
35490267Sdes	hdr_syserror = -2,
35590267Sdes	hdr_error = -1,
35690267Sdes	hdr_end = 0,
35790267Sdes	hdr_unknown = 1,
35890267Sdes	hdr_content_length,
35990267Sdes	hdr_content_range,
36090267Sdes	hdr_last_modified,
36190267Sdes	hdr_location,
36290267Sdes	hdr_transfer_encoding,
363202613Sdes	hdr_www_authenticate,
364202613Sdes	hdr_proxy_authenticate,
36585093Sdes} hdr_t;
36663012Sdes
36763012Sdes/* Names of interesting headers */
36863012Sdesstatic struct {
36990267Sdes	hdr_t		 num;
37090267Sdes	const char	*name;
37163012Sdes} hdr_names[] = {
37290267Sdes	{ hdr_content_length,		"Content-Length" },
37390267Sdes	{ hdr_content_range,		"Content-Range" },
37490267Sdes	{ hdr_last_modified,		"Last-Modified" },
37590267Sdes	{ hdr_location,			"Location" },
37690267Sdes	{ hdr_transfer_encoding,	"Transfer-Encoding" },
37790267Sdes	{ hdr_www_authenticate,		"WWW-Authenticate" },
378202613Sdes	{ hdr_proxy_authenticate,	"Proxy-Authenticate" },
37990267Sdes	{ hdr_unknown,			NULL },
38063012Sdes};
38163012Sdes
38263012Sdes/*
38363012Sdes * Send a formatted line; optionally echo to terminal
38463012Sdes */
38563012Sdesstatic int
386174588Sdeshttp_cmd(conn_t *conn, const char *fmt, ...)
38763012Sdes{
38890267Sdes	va_list ap;
38990267Sdes	size_t len;
39090267Sdes	char *msg;
39190267Sdes	int r;
39263012Sdes
39390267Sdes	va_start(ap, fmt);
39490267Sdes	len = vasprintf(&msg, fmt, ap);
39590267Sdes	va_end(ap);
39690267Sdes
39790267Sdes	if (msg == NULL) {
39890267Sdes		errno = ENOMEM;
399174588Sdes		fetch_syserr();
40090267Sdes		return (-1);
40190267Sdes	}
40290267Sdes
403174588Sdes	r = fetch_putln(conn, msg, len);
40490267Sdes	free(msg);
40590267Sdes
40690267Sdes	if (r == -1) {
407174588Sdes		fetch_syserr();
40890267Sdes		return (-1);
40990267Sdes	}
41090267Sdes
41190267Sdes	return (0);
41263012Sdes}
41363012Sdes
41463012Sdes/*
41563012Sdes * Get and parse status line
41663012Sdes */
41763012Sdesstatic int
418174588Sdeshttp_get_reply(conn_t *conn)
41963012Sdes{
42090267Sdes	char *p;
42190267Sdes
422174588Sdes	if (fetch_getln(conn) == -1)
42390267Sdes		return (-1);
42490267Sdes	/*
42590267Sdes	 * A valid status line looks like "HTTP/m.n xyz reason" where m
42690267Sdes	 * and n are the major and minor protocol version numbers and xyz
42790267Sdes	 * is the reply code.
42890267Sdes	 * Unfortunately, there are servers out there (NCSA 1.5.1, to name
42990267Sdes	 * just one) that do not send a version number, so we can't rely
43090267Sdes	 * on finding one, but if we do, insist on it being 1.0 or 1.1.
43190267Sdes	 * We don't care about the reason phrase.
43290267Sdes	 */
43397856Sdes	if (strncmp(conn->buf, "HTTP", 4) != 0)
43490267Sdes		return (HTTP_PROTOCOL_ERROR);
43597856Sdes	p = conn->buf + 4;
43690267Sdes	if (*p == '/') {
43790267Sdes		if (p[1] != '1' || p[2] != '.' || (p[3] != '0' && p[3] != '1'))
43890267Sdes			return (HTTP_PROTOCOL_ERROR);
43990267Sdes		p += 4;
44090267Sdes	}
441174761Sdes	if (*p != ' ' ||
442174761Sdes	    !isdigit((unsigned char)p[1]) ||
443174761Sdes	    !isdigit((unsigned char)p[2]) ||
444174761Sdes	    !isdigit((unsigned char)p[3]))
44590267Sdes		return (HTTP_PROTOCOL_ERROR);
44690267Sdes
44797856Sdes	conn->err = (p[1] - '0') * 100 + (p[2] - '0') * 10 + (p[3] - '0');
44897856Sdes	return (conn->err);
44937535Sdes}
45037535Sdes
45137608Sdes/*
45290267Sdes * Check a header; if the type matches the given string, return a pointer
45390267Sdes * to the beginning of the value.
45463012Sdes */
45575891Sarchiestatic const char *
456174588Sdeshttp_match(const char *str, const char *hdr)
45763012Sdes{
458176036Sdes	while (*str && *hdr &&
459176036Sdes	    tolower((unsigned char)*str++) == tolower((unsigned char)*hdr++))
46090267Sdes		/* nothing */;
46190267Sdes	if (*str || *hdr != ':')
46290267Sdes		return (NULL);
463174761Sdes	while (*hdr && isspace((unsigned char)*++hdr))
46490267Sdes		/* nothing */;
46590267Sdes	return (hdr);
46663012Sdes}
46763012Sdes
468202613Sdes
46963012Sdes/*
470202613Sdes * Get the next header and return the appropriate symbolic code.  We
471202613Sdes * need to read one line ahead for checking for a continuation line
472202613Sdes * belonging to the current header (continuation lines start with
473221821Sdes * white space).
474202613Sdes *
475202613Sdes * We get called with a fresh line already in the conn buffer, either
476202613Sdes * from the previous http_next_header() invocation, or, the first
477202613Sdes * time, from a fetch_getln() performed by our caller.
478202613Sdes *
479202613Sdes * This stops when we encounter an empty line (we dont read beyond the header
480202613Sdes * area).
481221821Sdes *
482202613Sdes * Note that the "headerbuf" is just a place to return the result. Its
483202613Sdes * contents are not used for the next call. This means that no cleanup
484202613Sdes * is needed when ie doing another connection, just call the cleanup when
485202613Sdes * fully done to deallocate memory.
48663012Sdes */
487202613Sdes
488202613Sdes/* Limit the max number of continuation lines to some reasonable value */
489202613Sdes#define HTTP_MAX_CONT_LINES 10
490202613Sdes
491202613Sdes/* Place into which to build a header from one or several lines */
492202613Sdestypedef struct {
493202613Sdes	char	*buf;		/* buffer */
494202613Sdes	size_t	 bufsize;	/* buffer size */
495202613Sdes	size_t	 buflen;	/* length of buffer contents */
496202613Sdes} http_headerbuf_t;
497202613Sdes
498202613Sdesstatic void
499202613Sdesinit_http_headerbuf(http_headerbuf_t *buf)
50063012Sdes{
501202613Sdes	buf->buf = NULL;
502202613Sdes	buf->bufsize = 0;
503202613Sdes	buf->buflen = 0;
504202613Sdes}
50590267Sdes
506221821Sdesstatic void
507202613Sdesclean_http_headerbuf(http_headerbuf_t *buf)
508202613Sdes{
509202613Sdes	if (buf->buf)
510202613Sdes		free(buf->buf);
511202613Sdes	init_http_headerbuf(buf);
512202613Sdes}
513202613Sdes
514202613Sdes/* Remove whitespace at the end of the buffer */
515221821Sdesstatic void
516202613Sdeshttp_conn_trimright(conn_t *conn)
517202613Sdes{
518221821Sdes	while (conn->buflen &&
519202613Sdes	       isspace((unsigned char)conn->buf[conn->buflen - 1]))
52097856Sdes		conn->buflen--;
52197856Sdes	conn->buf[conn->buflen] = '\0';
522202613Sdes}
523202613Sdes
524202613Sdesstatic hdr_t
525202613Sdeshttp_next_header(conn_t *conn, http_headerbuf_t *hbuf, const char **p)
526202613Sdes{
527221820Sdes	unsigned int i, len;
528202613Sdes
529221821Sdes	/*
530202613Sdes	 * Have to do the stripping here because of the first line. So
531221821Sdes	 * it's done twice for the subsequent lines. No big deal
532202613Sdes	 */
533202613Sdes	http_conn_trimright(conn);
53497856Sdes	if (conn->buflen == 0)
53597856Sdes		return (hdr_end);
536202613Sdes
537202613Sdes	/* Copy the line to the headerbuf */
538202613Sdes	if (hbuf->bufsize < conn->buflen + 1) {
539202613Sdes		if ((hbuf->buf = realloc(hbuf->buf, conn->buflen + 1)) == NULL)
540202613Sdes			return (hdr_syserror);
541202613Sdes		hbuf->bufsize = conn->buflen + 1;
542202613Sdes	}
543202613Sdes	strcpy(hbuf->buf, conn->buf);
544202613Sdes	hbuf->buflen = conn->buflen;
545202613Sdes
546221821Sdes	/*
547202613Sdes	 * Fetch possible continuation lines. Stop at 1st non-continuation
548221821Sdes	 * and leave it in the conn buffer
549221821Sdes	 */
550202613Sdes	for (i = 0; i < HTTP_MAX_CONT_LINES; i++) {
551202613Sdes		if (fetch_getln(conn) == -1)
552202613Sdes			return (hdr_syserror);
553202613Sdes
554221821Sdes		/*
555202613Sdes		 * Note: we carry on the idea from the previous version
556202613Sdes		 * that a pure whitespace line is equivalent to an empty
557202613Sdes		 * one (so it's not continuation and will be handled when
558221821Sdes		 * we are called next)
559202613Sdes		 */
560202613Sdes		http_conn_trimright(conn);
561202613Sdes		if (conn->buf[0] != ' ' && conn->buf[0] != "\t"[0])
562202613Sdes			break;
563202613Sdes
564202613Sdes		/* Got a continuation line. Concatenate to previous */
565202613Sdes		len = hbuf->buflen + conn->buflen;
566202613Sdes		if (hbuf->bufsize < len + 1) {
567202613Sdes			len *= 2;
568202613Sdes			if ((hbuf->buf = realloc(hbuf->buf, len + 1)) == NULL)
569202613Sdes				return (hdr_syserror);
570202613Sdes			hbuf->bufsize = len + 1;
571202613Sdes		}
572202613Sdes		strcpy(hbuf->buf + hbuf->buflen, conn->buf);
573202613Sdes		hbuf->buflen += conn->buflen;
574221821Sdes	}
575202613Sdes
57690267Sdes	/*
57790267Sdes	 * We could check for malformed headers but we don't really care.
57890267Sdes	 * A valid header starts with a token immediately followed by a
57990267Sdes	 * colon; a token is any sequence of non-control, non-whitespace
58090267Sdes	 * characters except "()<>@,;:\\\"{}".
58190267Sdes	 */
58290267Sdes	for (i = 0; hdr_names[i].num != hdr_unknown; i++)
583202613Sdes		if ((*p = http_match(hdr_names[i].name, hbuf->buf)) != NULL)
58490267Sdes			return (hdr_names[i].num);
585202613Sdes
58690267Sdes	return (hdr_unknown);
58763012Sdes}
58863012Sdes
589202613Sdes/**************************
590202613Sdes * [Proxy-]Authenticate header parsing
591202613Sdes */
592202613Sdes
593221821Sdes/*
594221821Sdes * Read doublequote-delimited string into output buffer obuf (allocated
595202613Sdes * by caller, whose responsibility it is to ensure that it's big enough)
596202613Sdes * cp points to the first char after the initial '"'
597221821Sdes * Handles \ quoting
598221821Sdes * Returns pointer to the first char after the terminating double quote, or
599202613Sdes * NULL for error.
600202613Sdes */
601202613Sdesstatic const char *
602202613Sdeshttp_parse_headerstring(const char *cp, char *obuf)
603202613Sdes{
604202613Sdes	for (;;) {
605202613Sdes		switch (*cp) {
606202613Sdes		case 0: /* Unterminated string */
607202613Sdes			*obuf = 0;
608202613Sdes			return (NULL);
609202613Sdes		case '"': /* Ending quote */
610202613Sdes			*obuf = 0;
611202613Sdes			return (++cp);
612202613Sdes		case '\\':
613202613Sdes			if (*++cp == 0) {
614202613Sdes				*obuf = 0;
615202613Sdes				return (NULL);
616202613Sdes			}
617202613Sdes			/* FALLTHROUGH */
618202613Sdes		default:
619202613Sdes			*obuf++ = *cp++;
620202613Sdes		}
621202613Sdes	}
622202613Sdes}
623202613Sdes
624202613Sdes/* Http auth challenge schemes */
625202613Sdestypedef enum {HTTPAS_UNKNOWN, HTTPAS_BASIC,HTTPAS_DIGEST} http_auth_schemes_t;
626202613Sdes
627202613Sdes/* Data holder for a Basic or Digest challenge. */
628202613Sdestypedef struct {
629202613Sdes	http_auth_schemes_t scheme;
630202613Sdes	char	*realm;
631202613Sdes	char	*qop;
632202613Sdes	char	*nonce;
633202613Sdes	char	*opaque;
634202613Sdes	char	*algo;
635202613Sdes	int	 stale;
636202613Sdes	int	 nc; /* Nonce count */
637202613Sdes} http_auth_challenge_t;
638202613Sdes
639221821Sdesstatic void
640202613Sdesinit_http_auth_challenge(http_auth_challenge_t *b)
641202613Sdes{
642202613Sdes	b->scheme = HTTPAS_UNKNOWN;
643202613Sdes	b->realm = b->qop = b->nonce = b->opaque = b->algo = NULL;
644202613Sdes	b->stale = b->nc = 0;
645202613Sdes}
646202613Sdes
647221821Sdesstatic void
648202613Sdesclean_http_auth_challenge(http_auth_challenge_t *b)
649202613Sdes{
650221821Sdes	if (b->realm)
651202613Sdes		free(b->realm);
652221821Sdes	if (b->qop)
653202613Sdes		free(b->qop);
654221821Sdes	if (b->nonce)
655202613Sdes		free(b->nonce);
656221821Sdes	if (b->opaque)
657202613Sdes		free(b->opaque);
658221821Sdes	if (b->algo)
659202613Sdes		free(b->algo);
660202613Sdes	init_http_auth_challenge(b);
661202613Sdes}
662202613Sdes
663202613Sdes/* Data holder for an array of challenges offered in an http response. */
664202613Sdes#define MAX_CHALLENGES 10
665202613Sdestypedef struct {
666202613Sdes	http_auth_challenge_t *challenges[MAX_CHALLENGES];
667202613Sdes	int	count; /* Number of parsed challenges in the array */
668202613Sdes	int	valid; /* We did parse an authenticate header */
669202613Sdes} http_auth_challenges_t;
670202613Sdes
671221821Sdesstatic void
672202613Sdesinit_http_auth_challenges(http_auth_challenges_t *cs)
673202613Sdes{
674202613Sdes	int i;
675202613Sdes	for (i = 0; i < MAX_CHALLENGES; i++)
676202613Sdes		cs->challenges[i] = NULL;
677202613Sdes	cs->count = cs->valid = 0;
678202613Sdes}
679202613Sdes
680221821Sdesstatic void
681202613Sdesclean_http_auth_challenges(http_auth_challenges_t *cs)
682202613Sdes{
683202613Sdes	int i;
684202613Sdes	/* We rely on non-zero pointers being allocated, not on the count */
685202613Sdes	for (i = 0; i < MAX_CHALLENGES; i++) {
686202613Sdes		if (cs->challenges[i] != NULL) {
687202613Sdes			clean_http_auth_challenge(cs->challenges[i]);
688202613Sdes			free(cs->challenges[i]);
689202613Sdes		}
690202613Sdes	}
691202613Sdes	init_http_auth_challenges(cs);
692202613Sdes}
693202613Sdes
694221821Sdes/*
695202613Sdes * Enumeration for lexical elements. Separators will be returned as their own
696202613Sdes * ascii value
697202613Sdes */
698202613Sdestypedef enum {HTTPHL_WORD=256, HTTPHL_STRING=257, HTTPHL_END=258,
699202613Sdes	      HTTPHL_ERROR = 259} http_header_lex_t;
700202613Sdes
701221821Sdes/*
702202613Sdes * Determine what kind of token comes next and return possible value
703202613Sdes * in buf, which is supposed to have been allocated big enough by
704221821Sdes * caller. Advance input pointer and return element type.
705202613Sdes */
706221821Sdesstatic int
707202613Sdeshttp_header_lex(const char **cpp, char *buf)
708202613Sdes{
709202613Sdes	size_t l;
710202613Sdes	/* Eat initial whitespace */
711202613Sdes	*cpp += strspn(*cpp, " \t");
712202613Sdes	if (**cpp == 0)
713202613Sdes		return (HTTPHL_END);
714202613Sdes
715202613Sdes	/* Separator ? */
716202613Sdes	if (**cpp == ',' || **cpp == '=')
717202613Sdes		return (*((*cpp)++));
718202613Sdes
719202613Sdes	/* String ? */
720202613Sdes	if (**cpp == '"') {
721202613Sdes		*cpp = http_parse_headerstring(++*cpp, buf);
722202613Sdes		if (*cpp == NULL)
723202613Sdes			return (HTTPHL_ERROR);
724202613Sdes		return (HTTPHL_STRING);
725202613Sdes	}
726202613Sdes
727202613Sdes	/* Read other token, until separator or whitespace */
728202613Sdes	l = strcspn(*cpp, " \t,=");
729202613Sdes	memcpy(buf, *cpp, l);
730202613Sdes	buf[l] = 0;
731202613Sdes	*cpp += l;
732202613Sdes	return (HTTPHL_WORD);
733202613Sdes}
734202613Sdes
735221821Sdes/*
736202613Sdes * Read challenges from http xxx-authenticate header and accumulate them
737202613Sdes * in the challenges list structure.
738202613Sdes *
739202613Sdes * Headers with multiple challenges are specified by rfc2617, but
740202613Sdes * servers (ie: squid) often send them in separate headers instead,
741202613Sdes * which in turn is forbidden by the http spec (multiple headers with
742202613Sdes * the same name are only allowed for pure comma-separated lists, see
743202613Sdes * rfc2616 sec 4.2).
744202613Sdes *
745202613Sdes * We support both approaches anyway
746202613Sdes */
747221821Sdesstatic int
748202613Sdeshttp_parse_authenticate(const char *cp, http_auth_challenges_t *cs)
749202613Sdes{
750202613Sdes	int ret = -1;
751202613Sdes	http_header_lex_t lex;
752202613Sdes	char *key = malloc(strlen(cp) + 1);
753202613Sdes	char *value = malloc(strlen(cp) + 1);
754202613Sdes	char *buf = malloc(strlen(cp) + 1);
755202613Sdes
756202613Sdes	if (key == NULL || value == NULL || buf == NULL) {
757202613Sdes		fetch_syserr();
758202613Sdes		goto out;
759202613Sdes	}
760202613Sdes
761202613Sdes	/* In any case we've seen the header and we set the valid bit */
762202613Sdes	cs->valid = 1;
763202613Sdes
764202613Sdes	/* Need word first */
765202613Sdes	lex = http_header_lex(&cp, key);
766202613Sdes	if (lex != HTTPHL_WORD)
767202613Sdes		goto out;
768202613Sdes
769202613Sdes	/* Loop on challenges */
770202613Sdes	for (; cs->count < MAX_CHALLENGES; cs->count++) {
771221821Sdes		cs->challenges[cs->count] =
772202613Sdes			malloc(sizeof(http_auth_challenge_t));
773202613Sdes		if (cs->challenges[cs->count] == NULL) {
774202613Sdes			fetch_syserr();
775202613Sdes			goto out;
776202613Sdes		}
777202613Sdes		init_http_auth_challenge(cs->challenges[cs->count]);
778202613Sdes		if (!strcasecmp(key, "basic")) {
779202613Sdes			cs->challenges[cs->count]->scheme = HTTPAS_BASIC;
780202613Sdes		} else if (!strcasecmp(key, "digest")) {
781202613Sdes			cs->challenges[cs->count]->scheme = HTTPAS_DIGEST;
782202613Sdes		} else {
783202613Sdes			cs->challenges[cs->count]->scheme = HTTPAS_UNKNOWN;
784221821Sdes			/*
785221821Sdes			 * Continue parsing as basic or digest may
786202613Sdes			 * follow, and the syntax is the same for
787202613Sdes			 * all. We'll just ignore this one when
788202613Sdes			 * looking at the list
789202613Sdes			 */
790202613Sdes		}
791221821Sdes
792202613Sdes		/* Loop on attributes */
793202613Sdes		for (;;) {
794202613Sdes			/* Key */
795202613Sdes			lex = http_header_lex(&cp, key);
796202613Sdes			if (lex != HTTPHL_WORD)
797202613Sdes				goto out;
798202613Sdes
799202613Sdes			/* Equal sign */
800202613Sdes			lex = http_header_lex(&cp, buf);
801202613Sdes			if (lex != '=')
802202613Sdes				goto out;
803202613Sdes
804202613Sdes			/* Value */
805202613Sdes			lex = http_header_lex(&cp, value);
806202613Sdes			if (lex != HTTPHL_WORD && lex != HTTPHL_STRING)
807202613Sdes				goto out;
808202613Sdes
809202613Sdes			if (!strcasecmp(key, "realm"))
810221821Sdes				cs->challenges[cs->count]->realm =
811202613Sdes					strdup(value);
812202613Sdes			else if (!strcasecmp(key, "qop"))
813221821Sdes				cs->challenges[cs->count]->qop =
814202613Sdes					strdup(value);
815202613Sdes			else if (!strcasecmp(key, "nonce"))
816221821Sdes				cs->challenges[cs->count]->nonce =
817202613Sdes					strdup(value);
818202613Sdes			else if (!strcasecmp(key, "opaque"))
819221821Sdes				cs->challenges[cs->count]->opaque =
820202613Sdes					strdup(value);
821202613Sdes			else if (!strcasecmp(key, "algorithm"))
822221821Sdes				cs->challenges[cs->count]->algo =
823202613Sdes					strdup(value);
824202613Sdes			else if (!strcasecmp(key, "stale"))
825221821Sdes				cs->challenges[cs->count]->stale =
826202613Sdes					strcasecmp(value, "no");
827202613Sdes			/* Else ignore unknown attributes */
828202613Sdes
829202613Sdes			/* Comma or Next challenge or End */
830202613Sdes			lex = http_header_lex(&cp, key);
831221821Sdes			/*
832221821Sdes			 * If we get a word here, this is the beginning of the
833221821Sdes			 * next challenge. Break the attributes loop
834221821Sdes			 */
835202613Sdes			if (lex == HTTPHL_WORD)
836202613Sdes				break;
837202613Sdes
838202613Sdes			if (lex == HTTPHL_END) {
839202613Sdes				/* End while looking for ',' is normal exit */
840202613Sdes				cs->count++;
841202613Sdes				ret = 0;
842202613Sdes				goto out;
843202613Sdes			}
844202613Sdes			/* Anything else is an error */
845202613Sdes			if (lex != ',')
846202613Sdes				goto out;
847202613Sdes
848202613Sdes		} /* End attributes loop */
849202613Sdes	} /* End challenge loop */
850202613Sdes
851221821Sdes	/*
852221821Sdes	 * Challenges max count exceeded. This really can't happen
853221821Sdes	 * with normal data, something's fishy -> error
854221821Sdes	 */
855202613Sdes
856202613Sdesout:
857202613Sdes	if (key)
858202613Sdes		free(key);
859202613Sdes	if (value)
860202613Sdes		free(value);
861202613Sdes	if (buf)
862202613Sdes		free(buf);
863202613Sdes	return (ret);
864202613Sdes}
865202613Sdes
866202613Sdes
86763012Sdes/*
86863012Sdes * Parse a last-modified header
86963012Sdes */
87063716Sdesstatic int
871174588Sdeshttp_parse_mtime(const char *p, time_t *mtime)
87263012Sdes{
87390267Sdes	char locale[64], *r;
87490267Sdes	struct tm tm;
87563012Sdes
876109967Sdes	strncpy(locale, setlocale(LC_TIME, NULL), sizeof(locale));
87790267Sdes	setlocale(LC_TIME, "C");
87890267Sdes	r = strptime(p, "%a, %d %b %Y %H:%M:%S GMT", &tm);
879263325Sbdrewery	/*
880263325Sbdrewery	 * Some proxies use UTC in response, but it should still be
881263325Sbdrewery	 * parsed. RFC2616 states GMT and UTC are exactly equal for HTTP.
882263325Sbdrewery	 */
883263325Sbdrewery	if (r == NULL)
884263325Sbdrewery		r = strptime(p, "%a, %d %b %Y %H:%M:%S UTC", &tm);
88590267Sdes	/* XXX should add support for date-2 and date-3 */
88690267Sdes	setlocale(LC_TIME, locale);
88790267Sdes	if (r == NULL)
88890267Sdes		return (-1);
88990267Sdes	DEBUG(fprintf(stderr, "last modified: [%04d-%02d-%02d "
89088769Sdes		  "%02d:%02d:%02d]\n",
89163012Sdes		  tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
89263012Sdes		  tm.tm_hour, tm.tm_min, tm.tm_sec));
89390267Sdes	*mtime = timegm(&tm);
89490267Sdes	return (0);
89563012Sdes}
89663012Sdes
89763012Sdes/*
89863012Sdes * Parse a content-length header
89963012Sdes */
90063716Sdesstatic int
901174588Sdeshttp_parse_length(const char *p, off_t *length)
90263012Sdes{
90390267Sdes	off_t len;
90490267Sdes
905174761Sdes	for (len = 0; *p && isdigit((unsigned char)*p); ++p)
90690267Sdes		len = len * 10 + (*p - '0');
90790267Sdes	if (*p)
90890267Sdes		return (-1);
90990267Sdes	DEBUG(fprintf(stderr, "content length: [%lld]\n",
91090267Sdes	    (long long)len));
91190267Sdes	*length = len;
91290267Sdes	return (0);
91363012Sdes}
91463012Sdes
91563012Sdes/*
91663012Sdes * Parse a content-range header
91763012Sdes */
91863716Sdesstatic int
919174588Sdeshttp_parse_range(const char *p, off_t *offset, off_t *length, off_t *size)
92063012Sdes{
92190267Sdes	off_t first, last, len;
92263716Sdes
92390267Sdes	if (strncasecmp(p, "bytes ", 6) != 0)
92490267Sdes		return (-1);
925125696Sdes	p += 6;
926125696Sdes	if (*p == '*') {
927125696Sdes		first = last = -1;
928125696Sdes		++p;
929125696Sdes	} else {
930174761Sdes		for (first = 0; *p && isdigit((unsigned char)*p); ++p)
931125696Sdes			first = first * 10 + *p - '0';
932125696Sdes		if (*p != '-')
933125696Sdes			return (-1);
934174761Sdes		for (last = 0, ++p; *p && isdigit((unsigned char)*p); ++p)
935125696Sdes			last = last * 10 + *p - '0';
936125696Sdes	}
93790267Sdes	if (first > last || *p != '/')
93890267Sdes		return (-1);
939174761Sdes	for (len = 0, ++p; *p && isdigit((unsigned char)*p); ++p)
94090267Sdes		len = len * 10 + *p - '0';
94190267Sdes	if (*p || len < last - first + 1)
94290267Sdes		return (-1);
943125696Sdes	if (first == -1) {
944125696Sdes		DEBUG(fprintf(stderr, "content range: [*/%lld]\n",
945125696Sdes		    (long long)len));
946125696Sdes		*length = 0;
947125696Sdes	} else {
948125696Sdes		DEBUG(fprintf(stderr, "content range: [%lld-%lld/%lld]\n",
949125696Sdes		    (long long)first, (long long)last, (long long)len));
950125696Sdes		*length = last - first + 1;
951125696Sdes	}
95290267Sdes	*offset = first;
95390267Sdes	*size = len;
95490267Sdes	return (0);
95563012Sdes}
95663012Sdes
95790267Sdes
95863012Sdes/*****************************************************************************
95963012Sdes * Helper functions for authorization
96063012Sdes */
96163012Sdes
96263012Sdes/*
96337608Sdes * Base64 encoding
96437608Sdes */
96562965Sdesstatic char *
966174588Sdeshttp_base64(const char *src)
96737608Sdes{
96890267Sdes	static const char base64[] =
96990267Sdes	    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
97090267Sdes	    "abcdefghijklmnopqrstuvwxyz"
97190267Sdes	    "0123456789+/";
97290267Sdes	char *str, *dst;
97390267Sdes	size_t l;
97490267Sdes	int t, r;
97562965Sdes
97690267Sdes	l = strlen(src);
977133280Sdes	if ((str = malloc(((l + 2) / 3) * 4 + 1)) == NULL)
97890267Sdes		return (NULL);
97990267Sdes	dst = str;
98090267Sdes	r = 0;
98137608Sdes
98290267Sdes	while (l >= 3) {
98390267Sdes		t = (src[0] << 16) | (src[1] << 8) | src[2];
98490267Sdes		dst[0] = base64[(t >> 18) & 0x3f];
98590267Sdes		dst[1] = base64[(t >> 12) & 0x3f];
98690267Sdes		dst[2] = base64[(t >> 6) & 0x3f];
98790267Sdes		dst[3] = base64[(t >> 0) & 0x3f];
98890267Sdes		src += 3; l -= 3;
98990267Sdes		dst += 4; r += 4;
99090267Sdes	}
99137608Sdes
99290267Sdes	switch (l) {
99390267Sdes	case 2:
99490267Sdes		t = (src[0] << 16) | (src[1] << 8);
99590267Sdes		dst[0] = base64[(t >> 18) & 0x3f];
99690267Sdes		dst[1] = base64[(t >> 12) & 0x3f];
99790267Sdes		dst[2] = base64[(t >> 6) & 0x3f];
99890267Sdes		dst[3] = '=';
99990267Sdes		dst += 4;
100090267Sdes		r += 4;
100190267Sdes		break;
100290267Sdes	case 1:
100390267Sdes		t = src[0] << 16;
100490267Sdes		dst[0] = base64[(t >> 18) & 0x3f];
100590267Sdes		dst[1] = base64[(t >> 12) & 0x3f];
100690267Sdes		dst[2] = dst[3] = '=';
100790267Sdes		dst += 4;
100890267Sdes		r += 4;
100990267Sdes		break;
101090267Sdes	case 0:
101190267Sdes		break;
101290267Sdes	}
101390267Sdes
101490267Sdes	*dst = 0;
101590267Sdes	return (str);
101637608Sdes}
101737608Sdes
1018202613Sdes
101937608Sdes/*
1020202613Sdes * Extract authorization parameters from environment value.
1021202613Sdes * The value is like scheme:realm:user:pass
1022202613Sdes */
1023202613Sdestypedef struct {
1024202613Sdes	char	*scheme;
1025202613Sdes	char	*realm;
1026202613Sdes	char	*user;
1027202613Sdes	char	*password;
1028202613Sdes} http_auth_params_t;
1029202613Sdes
1030202613Sdesstatic void
1031202613Sdesinit_http_auth_params(http_auth_params_t *s)
1032202613Sdes{
1033268900Sbapt	s->scheme = s->realm = s->user = s->password = NULL;
1034202613Sdes}
1035202613Sdes
1036221821Sdesstatic void
1037202613Sdesclean_http_auth_params(http_auth_params_t *s)
1038202613Sdes{
1039221821Sdes	if (s->scheme)
1040202613Sdes		free(s->scheme);
1041221821Sdes	if (s->realm)
1042202613Sdes		free(s->realm);
1043221821Sdes	if (s->user)
1044202613Sdes		free(s->user);
1045221821Sdes	if (s->password)
1046202613Sdes		free(s->password);
1047202613Sdes	init_http_auth_params(s);
1048202613Sdes}
1049202613Sdes
1050202613Sdesstatic int
1051202613Sdeshttp_authfromenv(const char *p, http_auth_params_t *parms)
1052202613Sdes{
1053202613Sdes	int ret = -1;
1054202613Sdes	char *v, *ve;
1055202613Sdes	char *str = strdup(p);
1056202613Sdes
1057202613Sdes	if (str == NULL) {
1058202613Sdes		fetch_syserr();
1059202613Sdes		return (-1);
1060202613Sdes	}
1061202613Sdes	v = str;
1062202613Sdes
1063202613Sdes	if ((ve = strchr(v, ':')) == NULL)
1064202613Sdes		goto out;
1065202613Sdes
1066202613Sdes	*ve = 0;
1067202613Sdes	if ((parms->scheme = strdup(v)) == NULL) {
1068202613Sdes		fetch_syserr();
1069202613Sdes		goto out;
1070202613Sdes	}
1071202613Sdes	v = ve + 1;
1072202613Sdes
1073202613Sdes	if ((ve = strchr(v, ':')) == NULL)
1074202613Sdes		goto out;
1075202613Sdes
1076202613Sdes	*ve = 0;
1077202613Sdes	if ((parms->realm = strdup(v)) == NULL) {
1078202613Sdes		fetch_syserr();
1079202613Sdes		goto out;
1080202613Sdes	}
1081202613Sdes	v = ve + 1;
1082202613Sdes
1083202613Sdes	if ((ve = strchr(v, ':')) == NULL)
1084202613Sdes		goto out;
1085202613Sdes
1086202613Sdes	*ve = 0;
1087202613Sdes	if ((parms->user = strdup(v)) == NULL) {
1088202613Sdes		fetch_syserr();
1089202613Sdes		goto out;
1090202613Sdes	}
1091202613Sdes	v = ve + 1;
1092202613Sdes
1093202613Sdes
1094202613Sdes	if ((parms->password = strdup(v)) == NULL) {
1095202613Sdes		fetch_syserr();
1096202613Sdes		goto out;
1097202613Sdes	}
1098202613Sdes	ret = 0;
1099202613Sdesout:
1100221821Sdes	if (ret == -1)
1101202613Sdes		clean_http_auth_params(parms);
1102202613Sdes	if (str)
1103202613Sdes		free(str);
1104202613Sdes	return (ret);
1105202613Sdes}
1106202613Sdes
1107202613Sdes
1108221821Sdes/*
1109202613Sdes * Digest response: the code to compute the digest is taken from the
1110221821Sdes * sample implementation in RFC2616
1111202613Sdes */
1112221822Sdes#define IN const
1113202613Sdes#define OUT
1114202613Sdes
1115202613Sdes#define HASHLEN 16
1116202613Sdestypedef char HASH[HASHLEN];
1117202613Sdes#define HASHHEXLEN 32
1118202613Sdestypedef char HASHHEX[HASHHEXLEN+1];
1119202613Sdes
1120202613Sdesstatic const char *hexchars = "0123456789abcdef";
1121221821Sdesstatic void
1122202613SdesCvtHex(IN HASH Bin, OUT HASHHEX Hex)
1123202613Sdes{
1124202613Sdes	unsigned short i;
1125202613Sdes	unsigned char j;
1126202613Sdes
1127202613Sdes	for (i = 0; i < HASHLEN; i++) {
1128202613Sdes		j = (Bin[i] >> 4) & 0xf;
1129202613Sdes		Hex[i*2] = hexchars[j];
1130202613Sdes		j = Bin[i] & 0xf;
1131202613Sdes		Hex[i*2+1] = hexchars[j];
1132268900Sbapt	}
1133202613Sdes	Hex[HASHHEXLEN] = '\0';
1134202613Sdes};
1135202613Sdes
1136202613Sdes/* calculate H(A1) as per spec */
1137221821Sdesstatic void
1138202613SdesDigestCalcHA1(
1139202613Sdes	IN char * pszAlg,
1140202613Sdes	IN char * pszUserName,
1141202613Sdes	IN char * pszRealm,
1142202613Sdes	IN char * pszPassword,
1143202613Sdes	IN char * pszNonce,
1144202613Sdes	IN char * pszCNonce,
1145202613Sdes	OUT HASHHEX SessionKey
1146202613Sdes	)
1147202613Sdes{
1148202613Sdes	MD5_CTX Md5Ctx;
1149202613Sdes	HASH HA1;
1150202613Sdes
1151202613Sdes	MD5Init(&Md5Ctx);
1152202613Sdes	MD5Update(&Md5Ctx, pszUserName, strlen(pszUserName));
1153202613Sdes	MD5Update(&Md5Ctx, ":", 1);
1154202613Sdes	MD5Update(&Md5Ctx, pszRealm, strlen(pszRealm));
1155202613Sdes	MD5Update(&Md5Ctx, ":", 1);
1156202613Sdes	MD5Update(&Md5Ctx, pszPassword, strlen(pszPassword));
1157202613Sdes	MD5Final(HA1, &Md5Ctx);
1158202613Sdes	if (strcasecmp(pszAlg, "md5-sess") == 0) {
1159202613Sdes
1160202613Sdes		MD5Init(&Md5Ctx);
1161202613Sdes		MD5Update(&Md5Ctx, HA1, HASHLEN);
1162202613Sdes		MD5Update(&Md5Ctx, ":", 1);
1163202613Sdes		MD5Update(&Md5Ctx, pszNonce, strlen(pszNonce));
1164202613Sdes		MD5Update(&Md5Ctx, ":", 1);
1165202613Sdes		MD5Update(&Md5Ctx, pszCNonce, strlen(pszCNonce));
1166202613Sdes		MD5Final(HA1, &Md5Ctx);
1167268900Sbapt	}
1168202613Sdes	CvtHex(HA1, SessionKey);
1169202613Sdes}
1170202613Sdes
1171202613Sdes/* calculate request-digest/response-digest as per HTTP Digest spec */
1172221821Sdesstatic void
1173202613SdesDigestCalcResponse(
1174202613Sdes	IN HASHHEX HA1,           /* H(A1) */
1175202613Sdes	IN char * pszNonce,       /* nonce from server */
1176202613Sdes	IN char * pszNonceCount,  /* 8 hex digits */
1177202613Sdes	IN char * pszCNonce,      /* client nonce */
1178202613Sdes	IN char * pszQop,         /* qop-value: "", "auth", "auth-int" */
1179202613Sdes	IN char * pszMethod,      /* method from the request */
1180202613Sdes	IN char * pszDigestUri,   /* requested URL */
1181202613Sdes	IN HASHHEX HEntity,       /* H(entity body) if qop="auth-int" */
1182202613Sdes	OUT HASHHEX Response      /* request-digest or response-digest */
1183202613Sdes	)
1184202613Sdes{
1185221821Sdes/*	DEBUG(fprintf(stderr,
1186202613Sdes		      "Calc: HA1[%s] Nonce[%s] qop[%s] method[%s] URI[%s]\n",
1187202613Sdes		      HA1, pszNonce, pszQop, pszMethod, pszDigestUri));*/
1188202613Sdes	MD5_CTX Md5Ctx;
1189202613Sdes	HASH HA2;
1190202613Sdes	HASH RespHash;
1191202613Sdes	HASHHEX HA2Hex;
1192202613Sdes
1193202613Sdes	// calculate H(A2)
1194202613Sdes	MD5Init(&Md5Ctx);
1195202613Sdes	MD5Update(&Md5Ctx, pszMethod, strlen(pszMethod));
1196202613Sdes	MD5Update(&Md5Ctx, ":", 1);
1197202613Sdes	MD5Update(&Md5Ctx, pszDigestUri, strlen(pszDigestUri));
1198202613Sdes	if (strcasecmp(pszQop, "auth-int") == 0) {
1199202613Sdes		MD5Update(&Md5Ctx, ":", 1);
1200202613Sdes		MD5Update(&Md5Ctx, HEntity, HASHHEXLEN);
1201268900Sbapt	}
1202202613Sdes	MD5Final(HA2, &Md5Ctx);
1203202613Sdes	CvtHex(HA2, HA2Hex);
1204202613Sdes
1205202613Sdes	// calculate response
1206202613Sdes	MD5Init(&Md5Ctx);
1207202613Sdes	MD5Update(&Md5Ctx, HA1, HASHHEXLEN);
1208202613Sdes	MD5Update(&Md5Ctx, ":", 1);
1209202613Sdes	MD5Update(&Md5Ctx, pszNonce, strlen(pszNonce));
1210202613Sdes	MD5Update(&Md5Ctx, ":", 1);
1211202613Sdes	if (*pszQop) {
1212202613Sdes		MD5Update(&Md5Ctx, pszNonceCount, strlen(pszNonceCount));
1213202613Sdes		MD5Update(&Md5Ctx, ":", 1);
1214202613Sdes		MD5Update(&Md5Ctx, pszCNonce, strlen(pszCNonce));
1215202613Sdes		MD5Update(&Md5Ctx, ":", 1);
1216202613Sdes		MD5Update(&Md5Ctx, pszQop, strlen(pszQop));
1217202613Sdes		MD5Update(&Md5Ctx, ":", 1);
1218268900Sbapt	}
1219202613Sdes	MD5Update(&Md5Ctx, HA2Hex, HASHHEXLEN);
1220202613Sdes	MD5Final(RespHash, &Md5Ctx);
1221202613Sdes	CvtHex(RespHash, Response);
1222202613Sdes}
1223202613Sdes
1224221821Sdes/*
1225221821Sdes * Generate/Send a Digest authorization header
1226202613Sdes * This looks like: [Proxy-]Authorization: credentials
1227202613Sdes *
1228202613Sdes *  credentials      = "Digest" digest-response
1229202613Sdes *  digest-response  = 1#( username | realm | nonce | digest-uri
1230202613Sdes *                      | response | [ algorithm ] | [cnonce] |
1231202613Sdes *                      [opaque] | [message-qop] |
1232202613Sdes *                          [nonce-count]  | [auth-param] )
1233202613Sdes *  username         = "username" "=" username-value
1234202613Sdes *  username-value   = quoted-string
1235202613Sdes *  digest-uri       = "uri" "=" digest-uri-value
1236202613Sdes *  digest-uri-value = request-uri   ; As specified by HTTP/1.1
1237202613Sdes *  message-qop      = "qop" "=" qop-value
1238202613Sdes *  cnonce           = "cnonce" "=" cnonce-value
1239202613Sdes *  cnonce-value     = nonce-value
1240202613Sdes *  nonce-count      = "nc" "=" nc-value
1241202613Sdes *  nc-value         = 8LHEX
1242202613Sdes *  response         = "response" "=" request-digest
1243202613Sdes *  request-digest = <"> 32LHEX <">
1244202613Sdes */
1245202613Sdesstatic int
1246202613Sdeshttp_digest_auth(conn_t *conn, const char *hdr, http_auth_challenge_t *c,
1247202613Sdes		 http_auth_params_t *parms, struct url *url)
1248202613Sdes{
1249202613Sdes	int r;
1250202613Sdes	char noncecount[10];
1251202613Sdes	char cnonce[40];
1252268900Sbapt	char *options = NULL;
1253202613Sdes
1254202613Sdes	if (!c->realm || !c->nonce) {
1255202613Sdes		DEBUG(fprintf(stderr, "realm/nonce not set in challenge\n"));
1256202613Sdes		return(-1);
1257202613Sdes	}
1258221821Sdes	if (!c->algo)
1259202613Sdes		c->algo = strdup("");
1260202613Sdes
1261221821Sdes	if (asprintf(&options, "%s%s%s%s",
1262202613Sdes		     *c->algo? ",algorithm=" : "", c->algo,
1263202613Sdes		     c->opaque? ",opaque=" : "", c->opaque?c->opaque:"")== -1)
1264202613Sdes		return (-1);
1265202613Sdes
1266202613Sdes	if (!c->qop) {
1267202613Sdes		c->qop = strdup("");
1268202613Sdes		*noncecount = 0;
1269202613Sdes		*cnonce = 0;
1270202613Sdes	} else {
1271202613Sdes		c->nc++;
1272202613Sdes		sprintf(noncecount, "%08x", c->nc);
1273202613Sdes		/* We don't try very hard with the cnonce ... */
1274202613Sdes		sprintf(cnonce, "%x%lx", getpid(), (unsigned long)time(0));
1275202613Sdes	}
1276202613Sdes
1277202613Sdes	HASHHEX HA1;
1278202613Sdes	DigestCalcHA1(c->algo, parms->user, c->realm,
1279202613Sdes		      parms->password, c->nonce, cnonce, HA1);
1280202613Sdes	DEBUG(fprintf(stderr, "HA1: [%s]\n", HA1));
1281202613Sdes	HASHHEX digest;
1282202613Sdes	DigestCalcResponse(HA1, c->nonce, noncecount, cnonce, c->qop,
1283202613Sdes			   "GET", url->doc, "", digest);
1284202613Sdes
1285202613Sdes	if (c->qop[0]) {
1286202613Sdes		r = http_cmd(conn, "%s: Digest username=\"%s\",realm=\"%s\","
1287202613Sdes			     "nonce=\"%s\",uri=\"%s\",response=\"%s\","
1288202613Sdes			     "qop=\"auth\", cnonce=\"%s\", nc=%s%s",
1289221821Sdes			     hdr, parms->user, c->realm,
1290202613Sdes			     c->nonce, url->doc, digest,
1291202613Sdes			     cnonce, noncecount, options);
1292202613Sdes	} else {
1293202613Sdes		r = http_cmd(conn, "%s: Digest username=\"%s\",realm=\"%s\","
1294202613Sdes			     "nonce=\"%s\",uri=\"%s\",response=\"%s\"%s",
1295221821Sdes			     hdr, parms->user, c->realm,
1296202613Sdes			     c->nonce, url->doc, digest, options);
1297202613Sdes	}
1298202613Sdes	if (options)
1299202613Sdes		free(options);
1300202613Sdes	return (r);
1301202613Sdes}
1302202613Sdes
1303202613Sdes/*
130437608Sdes * Encode username and password
130537608Sdes */
130662965Sdesstatic int
1307174588Sdeshttp_basic_auth(conn_t *conn, const char *hdr, const char *usr, const char *pwd)
130837608Sdes{
130990267Sdes	char *upw, *auth;
131090267Sdes	int r;
131137608Sdes
1312202613Sdes	DEBUG(fprintf(stderr, "basic: usr: [%s]\n", usr));
1313202613Sdes	DEBUG(fprintf(stderr, "basic: pwd: [%s]\n", pwd));
131490267Sdes	if (asprintf(&upw, "%s:%s", usr, pwd) == -1)
131590267Sdes		return (-1);
1316174588Sdes	auth = http_base64(upw);
131790267Sdes	free(upw);
131890267Sdes	if (auth == NULL)
131990267Sdes		return (-1);
1320174588Sdes	r = http_cmd(conn, "%s: Basic %s", hdr, auth);
132190267Sdes	free(auth);
132290267Sdes	return (r);
132362965Sdes}
132462965Sdes
132562965Sdes/*
1326221821Sdes * Chose the challenge to answer and call the appropriate routine to
1327202613Sdes * produce the header.
132862965Sdes */
132962965Sdesstatic int
1330202613Sdeshttp_authorize(conn_t *conn, const char *hdr, http_auth_challenges_t *cs,
1331202613Sdes	       http_auth_params_t *parms, struct url *url)
133262965Sdes{
1333202613Sdes	http_auth_challenge_t *basic = NULL;
1334202613Sdes	http_auth_challenge_t *digest = NULL;
1335202613Sdes	int i;
133662965Sdes
1337202613Sdes	/* If user or pass are null we're not happy */
1338202613Sdes	if (!parms->user || !parms->password) {
1339202613Sdes		DEBUG(fprintf(stderr, "NULL usr or pass\n"));
1340202613Sdes		return (-1);
134190267Sdes	}
1342202613Sdes
1343202613Sdes	/* Look for a Digest and a Basic challenge */
1344202613Sdes	for (i = 0; i < cs->count; i++) {
1345202613Sdes		if (cs->challenges[i]->scheme == HTTPAS_BASIC)
1346202613Sdes			basic = cs->challenges[i];
1347202613Sdes		if (cs->challenges[i]->scheme == HTTPAS_DIGEST)
1348202613Sdes			digest = cs->challenges[i];
1349202613Sdes	}
1350202613Sdes
1351202613Sdes	/* Error if "Digest" was specified and there is no Digest challenge */
1352221821Sdes	if (!digest && (parms->scheme &&
1353202613Sdes			!strcasecmp(parms->scheme, "digest"))) {
1354221821Sdes		DEBUG(fprintf(stderr,
1355202613Sdes			      "Digest auth in env, not supported by peer\n"));
1356202613Sdes		return (-1);
1357202613Sdes	}
1358221821Sdes	/*
1359221821Sdes	 * If "basic" was specified in the environment, or there is no Digest
1360202613Sdes	 * challenge, do the basic thing. Don't need a challenge for this,
1361221821Sdes	 * so no need to check basic!=NULL
1362202613Sdes	 */
1363202613Sdes	if (!digest || (parms->scheme && !strcasecmp(parms->scheme,"basic")))
1364202613Sdes		return (http_basic_auth(conn,hdr,parms->user,parms->password));
1365202613Sdes
1366202613Sdes	/* Else, prefer digest. We just checked that it's not NULL */
1367202613Sdes	return (http_digest_auth(conn, hdr, digest, parms, url));
136837608Sdes}
136937608Sdes
137063012Sdes/*****************************************************************************
137163012Sdes * Helper functions for connecting to a server or proxy
137263012Sdes */
137363012Sdes
137437608Sdes/*
137590267Sdes * Connect to the correct HTTP server or proxy.
137663012Sdes */
137797856Sdesstatic conn_t *
1378174588Sdeshttp_connect(struct url *URL, struct url *purl, const char *flags)
137963012Sdes{
1380249431Sdes	struct url *curl;
138197856Sdes	conn_t *conn;
138290267Sdes	int verbose;
1383141958Skbyanc	int af, val;
138490267Sdes
138563012Sdes#ifdef INET6
138690267Sdes	af = AF_UNSPEC;
138760737Sume#else
138890267Sdes	af = AF_INET;
138960737Sume#endif
139090267Sdes
139190267Sdes	verbose = CHECK_FLAG('v');
139290267Sdes	if (CHECK_FLAG('4'))
139390267Sdes		af = AF_INET;
139467043Sdes#ifdef INET6
139590267Sdes	else if (CHECK_FLAG('6'))
139690267Sdes		af = AF_INET6;
139767043Sdes#endif
139867043Sdes
1399249431Sdes	curl = (purl != NULL) ? purl : URL;
140090267Sdes
1401249431Sdes	if ((conn = fetch_connect(curl->host, curl->port, af, verbose)) == NULL)
1402174588Sdes		/* fetch_connect() has already set an error code */
140397856Sdes		return (NULL);
1404249431Sdes	if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0 && purl) {
1405249431Sdes		http_cmd(conn, "CONNECT %s:%d HTTP/1.1",
1406249431Sdes		    URL->host, URL->port);
1407254650Sdes		http_cmd(conn, "Host: %s:%d",
1408254650Sdes		    URL->host, URL->port);
1409249431Sdes		http_cmd(conn, "");
1410249431Sdes		if (http_get_reply(conn) != HTTP_OK) {
1411249431Sdes			fetch_close(conn);
1412249431Sdes			return (NULL);
1413249431Sdes		}
1414249431Sdes		http_get_reply(conn);
1415249431Sdes	}
141697868Sdes	if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0 &&
1417253680Sdes	    fetch_ssl(conn, URL, verbose) == -1) {
1418174588Sdes		fetch_close(conn);
141997891Sdes		/* grrr */
142097891Sdes		errno = EAUTH;
1421174588Sdes		fetch_syserr();
142297868Sdes		return (NULL);
142397868Sdes	}
1424141958Skbyanc
1425141958Skbyanc	val = 1;
1426141958Skbyanc	setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val, sizeof(val));
1427141958Skbyanc
142897856Sdes	return (conn);
142967043Sdes}
143067043Sdes
143167043Sdesstatic struct url *
1432174752Sdeshttp_get_proxy(struct url * url, const char *flags)
143367043Sdes{
143490267Sdes	struct url *purl;
143590267Sdes	char *p;
143690267Sdes
1437112797Sdes	if (flags != NULL && strchr(flags, 'd') != NULL)
1438112081Sdes		return (NULL);
1439174752Sdes	if (fetch_no_proxy_match(url->host))
1440174752Sdes		return (NULL);
144190267Sdes	if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
1442149414Sdes	    *p && (purl = fetchParseURL(p))) {
144390267Sdes		if (!*purl->scheme)
144490267Sdes			strcpy(purl->scheme, SCHEME_HTTP);
144590267Sdes		if (!purl->port)
1446174588Sdes			purl->port = fetch_default_proxy_port(purl->scheme);
144790267Sdes		if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
144890267Sdes			return (purl);
144990267Sdes		fetchFreeURL(purl);
145090267Sdes	}
145190267Sdes	return (NULL);
145260376Sdes}
145360376Sdes
145488771Sdesstatic void
1455174588Sdeshttp_print_html(FILE *out, FILE *in)
145688771Sdes{
145790267Sdes	size_t len;
145890267Sdes	char *line, *p, *q;
145990267Sdes	int comment, tag;
146088771Sdes
146190267Sdes	comment = tag = 0;
146290267Sdes	while ((line = fgetln(in, &len)) != NULL) {
1463174761Sdes		while (len && isspace((unsigned char)line[len - 1]))
146490267Sdes			--len;
146590267Sdes		for (p = q = line; q < line + len; ++q) {
146690267Sdes			if (comment && *q == '-') {
146790267Sdes				if (q + 2 < line + len &&
146890267Sdes				    strcmp(q, "-->") == 0) {
146990267Sdes					tag = comment = 0;
147090267Sdes					q += 2;
147190267Sdes				}
147290267Sdes			} else if (tag && !comment && *q == '>') {
147390267Sdes				p = q + 1;
147490267Sdes				tag = 0;
147590267Sdes			} else if (!tag && *q == '<') {
147690267Sdes				if (q > p)
147790267Sdes					fwrite(p, q - p, 1, out);
147890267Sdes				tag = 1;
147990267Sdes				if (q + 3 < line + len &&
148090267Sdes				    strcmp(q, "<!--") == 0) {
148190267Sdes					comment = 1;
148290267Sdes					q += 3;
148390267Sdes				}
148490267Sdes			}
148588771Sdes		}
148690267Sdes		if (!tag && q > p)
148790267Sdes			fwrite(p, q - p, 1, out);
148890267Sdes		fputc('\n', out);
148988771Sdes	}
149088771Sdes}
149188771Sdes
149290267Sdes
149363012Sdes/*****************************************************************************
149463012Sdes * Core
149560954Sdes */
149660954Sdes
1497268900SbaptFILE *
1498268900Sbapthttp_request(struct url *URL, const char *op, struct url_stat *us,
1499268900Sbapt	struct url *purl, const char *flags)
1500268900Sbapt{
1501268900Sbapt
1502268900Sbapt	return (http_request_body(URL, op, us, purl, flags, NULL, NULL));
1503268900Sbapt}
1504268900Sbapt
150560954Sdes/*
150663012Sdes * Send a request and process the reply
150797866Sdes *
150897866Sdes * XXX This function is way too long, the do..while loop should be split
150997866Sdes * XXX off into a separate function.
151060376Sdes */
151167043SdesFILE *
1512268900Sbapthttp_request_body(struct url *URL, const char *op, struct url_stat *us,
1513268900Sbapt	struct url *purl, const char *flags, const char *content_type,
1514268900Sbapt	const char *body)
151560376Sdes{
1516186124Smurray	char timebuf[80];
1517186124Smurray	char hbuf[MAXHOSTNAMELEN + 7], *host;
151897856Sdes	conn_t *conn;
151990267Sdes	struct url *url, *new;
1520202613Sdes	int chunked, direct, ims, noredirect, verbose;
1521143049Skbyanc	int e, i, n, val;
152290267Sdes	off_t offset, clength, length, size;
152390267Sdes	time_t mtime;
152490267Sdes	const char *p;
152590267Sdes	FILE *f;
152690267Sdes	hdr_t h;
1527186124Smurray	struct tm *timestruct;
1528202613Sdes	http_headerbuf_t headerbuf;
1529202613Sdes	http_auth_challenges_t server_challenges;
1530202613Sdes	http_auth_challenges_t proxy_challenges;
1531268900Sbapt	size_t body_len;
153263012Sdes
1533202613Sdes	/* The following calls don't allocate anything */
1534221821Sdes	init_http_headerbuf(&headerbuf);
1535202613Sdes	init_http_auth_challenges(&server_challenges);
1536202613Sdes	init_http_auth_challenges(&proxy_challenges);
1537202613Sdes
153890267Sdes	direct = CHECK_FLAG('d');
153990267Sdes	noredirect = CHECK_FLAG('A');
154090267Sdes	verbose = CHECK_FLAG('v');
1541186124Smurray	ims = CHECK_FLAG('i');
154260737Sume
154390267Sdes	if (direct && purl) {
154490267Sdes		fetchFreeURL(purl);
154590267Sdes		purl = NULL;
154690267Sdes	}
154763716Sdes
154890267Sdes	/* try the provided URL first */
154990267Sdes	url = URL;
155063012Sdes
1551241840Seadler	n = MAX_REDIRECT;
155290267Sdes	i = 0;
155363012Sdes
155498422Sdes	e = HTTP_PROTOCOL_ERROR;
155590267Sdes	do {
155690267Sdes		new = NULL;
155790267Sdes		chunked = 0;
155890267Sdes		offset = 0;
155990267Sdes		clength = -1;
156090267Sdes		length = -1;
156190267Sdes		size = -1;
156290267Sdes		mtime = 0;
156390267Sdes
156490267Sdes		/* check port */
156590267Sdes		if (!url->port)
1566174588Sdes			url->port = fetch_default_port(url->scheme);
156790267Sdes
156890267Sdes		/* were we redirected to an FTP URL? */
156990267Sdes		if (purl == NULL && strcmp(url->scheme, SCHEME_FTP) == 0) {
157090267Sdes			if (strcmp(op, "GET") == 0)
1571174588Sdes				return (ftp_request(url, "RETR", us, purl, flags));
157290267Sdes			else if (strcmp(op, "HEAD") == 0)
1573174588Sdes				return (ftp_request(url, "STAT", us, purl, flags));
157490267Sdes		}
157590267Sdes
157690267Sdes		/* connect to server or proxy */
1577174588Sdes		if ((conn = http_connect(url, purl, flags)) == NULL)
157890267Sdes			goto ouch;
157990267Sdes
158090267Sdes		host = url->host;
158160737Sume#ifdef INET6
158290267Sdes		if (strchr(url->host, ':')) {
158390267Sdes			snprintf(hbuf, sizeof(hbuf), "[%s]", url->host);
158490267Sdes			host = hbuf;
158590267Sdes		}
158660737Sume#endif
1587174588Sdes		if (url->port != fetch_default_port(url->scheme)) {
1588107372Sdes			if (host != hbuf) {
1589107372Sdes				strcpy(hbuf, host);
1590107372Sdes				host = hbuf;
1591107372Sdes			}
1592107372Sdes			snprintf(hbuf + strlen(hbuf),
1593107372Sdes			    sizeof(hbuf) - strlen(hbuf), ":%d", url->port);
1594107372Sdes		}
159537535Sdes
159690267Sdes		/* send request */
159790267Sdes		if (verbose)
1598174588Sdes			fetch_info("requesting %s://%s%s",
1599107372Sdes			    url->scheme, host, url->doc);
1600253514Sdes		if (purl && strcasecmp(URL->scheme, SCHEME_HTTPS) != 0) {
1601174588Sdes			http_cmd(conn, "%s %s://%s%s HTTP/1.1",
1602107372Sdes			    op, url->scheme, host, url->doc);
160390267Sdes		} else {
1604174588Sdes			http_cmd(conn, "%s %s HTTP/1.1",
160590267Sdes			    op, url->doc);
160690267Sdes		}
160737535Sdes
1608186124Smurray		if (ims && url->ims_time) {
1609186124Smurray			timestruct = gmtime((time_t *)&url->ims_time);
1610186124Smurray			(void)strftime(timebuf, 80, "%a, %d %b %Y %T GMT",
1611186124Smurray			    timestruct);
1612186124Smurray			if (verbose)
1613186124Smurray				fetch_info("If-Modified-Since: %s", timebuf);
1614186124Smurray			http_cmd(conn, "If-Modified-Since: %s", timebuf);
1615186124Smurray		}
161690267Sdes		/* virtual host */
1617174588Sdes		http_cmd(conn, "Host: %s", host);
161890267Sdes
1619221821Sdes		/*
1620221821Sdes		 * Proxy authorization: we only send auth after we received
1621221821Sdes		 * a 407 error. We do not first try basic anyway (changed
1622221821Sdes		 * when support was added for digest-auth)
1623221821Sdes		 */
1624202613Sdes		if (purl && proxy_challenges.valid) {
1625202613Sdes			http_auth_params_t aparams;
1626202613Sdes			init_http_auth_params(&aparams);
1627202613Sdes			if (*purl->user || *purl->pwd) {
1628221821Sdes				aparams.user = purl->user ?
1629202613Sdes					strdup(purl->user) : strdup("");
1630202613Sdes				aparams.password = purl->pwd?
1631202613Sdes					strdup(purl->pwd) : strdup("");
1632221821Sdes			} else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL &&
1633202613Sdes				   *p != '\0') {
1634202613Sdes				if (http_authfromenv(p, &aparams) < 0) {
1635202613Sdes					http_seterr(HTTP_NEED_PROXY_AUTH);
1636202613Sdes					goto ouch;
1637202613Sdes				}
1638202613Sdes			}
1639221821Sdes			http_authorize(conn, "Proxy-Authorization",
1640202613Sdes				       &proxy_challenges, &aparams, url);
1641202613Sdes			clean_http_auth_params(&aparams);
164290267Sdes		}
164390267Sdes
1644221821Sdes		/*
1645221821Sdes		 * Server authorization: we never send "a priori"
1646202613Sdes		 * Basic auth, which used to be done if user/pass were
1647202613Sdes		 * set in the url. This would be weird because we'd send the
1648221821Sdes		 * password in the clear even if Digest is finally to be
1649202613Sdes		 * used (it would have made more sense for the
1650221821Sdes		 * pre-digest version to do this when Basic was specified
1651221821Sdes		 * in the environment)
1652221821Sdes		 */
1653202613Sdes		if (server_challenges.valid) {
1654202613Sdes			http_auth_params_t aparams;
1655202613Sdes			init_http_auth_params(&aparams);
1656202613Sdes			if (*url->user || *url->pwd) {
1657221821Sdes				aparams.user = url->user ?
1658202613Sdes					strdup(url->user) : strdup("");
1659221821Sdes				aparams.password = url->pwd ?
1660202613Sdes					strdup(url->pwd) : strdup("");
1661221821Sdes			} else if ((p = getenv("HTTP_AUTH")) != NULL &&
1662202613Sdes				   *p != '\0') {
1663202613Sdes				if (http_authfromenv(p, &aparams) < 0) {
1664202613Sdes					http_seterr(HTTP_NEED_AUTH);
1665202613Sdes					goto ouch;
1666202613Sdes				}
1667221821Sdes			} else if (fetchAuthMethod &&
1668202613Sdes				   fetchAuthMethod(url) == 0) {
1669221821Sdes				aparams.user = url->user ?
1670202613Sdes					strdup(url->user) : strdup("");
1671221821Sdes				aparams.password = url->pwd ?
1672202613Sdes					strdup(url->pwd) : strdup("");
167390267Sdes			} else {
1674174588Sdes				http_seterr(HTTP_NEED_AUTH);
167590267Sdes				goto ouch;
167690267Sdes			}
1677221821Sdes			http_authorize(conn, "Authorization",
1678202613Sdes				       &server_challenges, &aparams, url);
1679202613Sdes			clean_http_auth_params(&aparams);
168090267Sdes		}
168190267Sdes
168290267Sdes		/* other headers */
1683253805Sdes		if ((p = getenv("HTTP_ACCEPT")) != NULL) {
1684253805Sdes			if (*p != '\0')
1685253805Sdes				http_cmd(conn, "Accept: %s", p);
1686253805Sdes		} else {
1687253805Sdes			http_cmd(conn, "Accept: */*");
1688253805Sdes		}
1689107372Sdes		if ((p = getenv("HTTP_REFERER")) != NULL && *p != '\0') {
1690107372Sdes			if (strcasecmp(p, "auto") == 0)
1691174588Sdes				http_cmd(conn, "Referer: %s://%s%s",
1692107372Sdes				    url->scheme, host, url->doc);
1693107372Sdes			else
1694174588Sdes				http_cmd(conn, "Referer: %s", p);
1695107372Sdes		}
1696270460Sdes		if ((p = getenv("HTTP_USER_AGENT")) != NULL) {
1697270460Sdes			/* no User-Agent if defined but empty */
1698270460Sdes			if  (*p != '\0')
1699270460Sdes				http_cmd(conn, "User-Agent: %s", p);
1700270460Sdes		} else {
1701270460Sdes			/* default User-Agent */
1702270460Sdes			http_cmd(conn, "User-Agent: %s " _LIBFETCH_VER,
1703270460Sdes			    getprogname());
1704270460Sdes		}
1705109693Sdes		if (url->offset > 0)
1706174588Sdes			http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset);
1707174588Sdes		http_cmd(conn, "Connection: close");
1708268900Sbapt
1709268900Sbapt		if (body) {
1710268900Sbapt			body_len = strlen(body);
1711268900Sbapt			http_cmd(conn, "Content-Length: %zu", body_len);
1712268900Sbapt			if (content_type != NULL)
1713268900Sbapt				http_cmd(conn, "Content-Type: %s", content_type);
1714268900Sbapt		}
1715268900Sbapt
1716174588Sdes		http_cmd(conn, "");
171790267Sdes
1718268900Sbapt		if (body)
1719268900Sbapt			fetch_write(conn, body, body_len);
1720268900Sbapt
1721143049Skbyanc		/*
1722143049Skbyanc		 * Force the queued request to be dispatched.  Normally, one
1723143049Skbyanc		 * would do this with shutdown(2) but squid proxies can be
1724143049Skbyanc		 * configured to disallow such half-closed connections.  To
1725143049Skbyanc		 * be compatible with such configurations, fiddle with socket
1726143049Skbyanc		 * options to force the pending data to be written.
1727143049Skbyanc		 */
1728143049Skbyanc		val = 0;
1729143049Skbyanc		setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val,
1730143049Skbyanc			   sizeof(val));
1731143049Skbyanc		val = 1;
1732143049Skbyanc		setsockopt(conn->sd, IPPROTO_TCP, TCP_NODELAY, &val,
1733143049Skbyanc			   sizeof(val));
1734143049Skbyanc
173590267Sdes		/* get reply */
1736174588Sdes		switch (http_get_reply(conn)) {
173790267Sdes		case HTTP_OK:
173890267Sdes		case HTTP_PARTIAL:
1739186124Smurray		case HTTP_NOT_MODIFIED:
174090267Sdes			/* fine */
174190267Sdes			break;
174290267Sdes		case HTTP_MOVED_PERM:
174390267Sdes		case HTTP_MOVED_TEMP:
174490267Sdes		case HTTP_SEE_OTHER:
1745241841Seadler		case HTTP_USE_PROXY:
174690267Sdes			/*
1747125695Sdes			 * Not so fine, but we still have to read the
1748125695Sdes			 * headers to get the new location.
174990267Sdes			 */
175090267Sdes			break;
175190267Sdes		case HTTP_NEED_AUTH:
1752202613Sdes			if (server_challenges.valid) {
175390267Sdes				/*
1754125695Sdes				 * We already sent out authorization code,
1755125695Sdes				 * so there's nothing more we can do.
175690267Sdes				 */
1757174588Sdes				http_seterr(conn->err);
175890267Sdes				goto ouch;
175990267Sdes			}
176090267Sdes			/* try again, but send the password this time */
176190267Sdes			if (verbose)
1762174588Sdes				fetch_info("server requires authorization");
176390267Sdes			break;
176490267Sdes		case HTTP_NEED_PROXY_AUTH:
1765202613Sdes			if (proxy_challenges.valid) {
1766202613Sdes				/*
1767202613Sdes				 * We already sent our proxy
1768202613Sdes				 * authorization code, so there's
1769202613Sdes				 * nothing more we can do. */
1770202613Sdes				http_seterr(conn->err);
1771202613Sdes				goto ouch;
1772202613Sdes			}
1773202613Sdes			/* try again, but send the password this time */
1774202613Sdes			if (verbose)
1775202613Sdes				fetch_info("proxy requires authorization");
1776202613Sdes			break;
1777125696Sdes		case HTTP_BAD_RANGE:
1778125696Sdes			/*
1779125696Sdes			 * This can happen if we ask for 0 bytes because
1780125696Sdes			 * we already have the whole file.  Consider this
1781125696Sdes			 * a success for now, and check sizes later.
1782125696Sdes			 */
1783125696Sdes			break;
178490267Sdes		case HTTP_PROTOCOL_ERROR:
178590267Sdes			/* fall through */
178690267Sdes		case -1:
1787174588Sdes			fetch_syserr();
178890267Sdes			goto ouch;
178990267Sdes		default:
1790174588Sdes			http_seterr(conn->err);
179190267Sdes			if (!verbose)
179290267Sdes				goto ouch;
179390267Sdes			/* fall through so we can get the full error message */
179490267Sdes		}
179590267Sdes
1796202613Sdes		/* get headers. http_next_header expects one line readahead */
1797202613Sdes		if (fetch_getln(conn) == -1) {
1798243149Sdes			fetch_syserr();
1799243149Sdes			goto ouch;
1800202613Sdes		}
180190267Sdes		do {
1802243149Sdes			switch ((h = http_next_header(conn, &headerbuf, &p))) {
180390267Sdes			case hdr_syserror:
1804174588Sdes				fetch_syserr();
180590267Sdes				goto ouch;
180690267Sdes			case hdr_error:
1807174588Sdes				http_seterr(HTTP_PROTOCOL_ERROR);
180890267Sdes				goto ouch;
180990267Sdes			case hdr_content_length:
1810174588Sdes				http_parse_length(p, &clength);
181190267Sdes				break;
181290267Sdes			case hdr_content_range:
1813174588Sdes				http_parse_range(p, &offset, &length, &size);
181490267Sdes				break;
181590267Sdes			case hdr_last_modified:
1816174588Sdes				http_parse_mtime(p, &mtime);
181790267Sdes				break;
181890267Sdes			case hdr_location:
181997856Sdes				if (!HTTP_REDIRECT(conn->err))
182090267Sdes					break;
1821241840Seadler				/*
1822241840Seadler				 * if the A flag is set, we don't follow
1823241840Seadler				 * temporary redirects.
1824241840Seadler				 */
1825241840Seadler				if (noredirect &&
1826241840Seadler				    conn->err != HTTP_MOVED_PERM &&
1827241841Seadler				    conn->err != HTTP_PERM_REDIRECT &&
1828241841Seadler				    conn->err != HTTP_USE_PROXY) {
1829241840Seadler					n = 1;
1830241840Seadler					break;
1831243149Sdes				}
183290267Sdes				if (new)
183390267Sdes					free(new);
183490267Sdes				if (verbose)
1835174588Sdes					fetch_info("%d redirect to %s", conn->err, p);
183690267Sdes				if (*p == '/')
183790267Sdes					/* absolute path */
183890267Sdes					new = fetchMakeURL(url->scheme, url->host, url->port, p,
183990267Sdes					    url->user, url->pwd);
184090267Sdes				else
184190267Sdes					new = fetchParseURL(p);
184290267Sdes				if (new == NULL) {
184390267Sdes					/* XXX should set an error code */
184490267Sdes					DEBUG(fprintf(stderr, "failed to parse new URL\n"));
184590267Sdes					goto ouch;
184690267Sdes				}
1847234838Sdes
1848234838Sdes				/* Only copy credentials if the host matches */
1849234838Sdes				if (!strcmp(new->host, url->host) && !*new->user && !*new->pwd) {
185090267Sdes					strcpy(new->user, url->user);
185190267Sdes					strcpy(new->pwd, url->pwd);
185290267Sdes				}
185390267Sdes				new->offset = url->offset;
185490267Sdes				new->length = url->length;
185590267Sdes				break;
185690267Sdes			case hdr_transfer_encoding:
185790267Sdes				/* XXX weak test*/
185890267Sdes				chunked = (strcasecmp(p, "chunked") == 0);
185990267Sdes				break;
186090267Sdes			case hdr_www_authenticate:
186197856Sdes				if (conn->err != HTTP_NEED_AUTH)
186290267Sdes					break;
1863210563Sdes				if (http_parse_authenticate(p, &server_challenges) == 0)
1864209632Sdes					++n;
186590267Sdes				break;
1866202613Sdes			case hdr_proxy_authenticate:
1867202613Sdes				if (conn->err != HTTP_NEED_PROXY_AUTH)
1868202613Sdes					break;
1869210563Sdes				if (http_parse_authenticate(p, &proxy_challenges) == 0)
1870209632Sdes					++n;
1871202613Sdes				break;
187290267Sdes			case hdr_end:
187390267Sdes				/* fall through */
187490267Sdes			case hdr_unknown:
187590267Sdes				/* ignore */
187690267Sdes				break;
187790267Sdes			}
187890267Sdes		} while (h > hdr_end);
187990267Sdes
188090267Sdes		/* we need to provide authentication */
1881221821Sdes		if (conn->err == HTTP_NEED_AUTH ||
1882202613Sdes		    conn->err == HTTP_NEED_PROXY_AUTH) {
188398422Sdes			e = conn->err;
1884221821Sdes			if ((conn->err == HTTP_NEED_AUTH &&
1885221821Sdes			     !server_challenges.valid) ||
1886221821Sdes			    (conn->err == HTTP_NEED_PROXY_AUTH &&
1887202613Sdes			     !proxy_challenges.valid)) {
1888202613Sdes				/* 401/7 but no www/proxy-authenticate ?? */
1889202613Sdes				DEBUG(fprintf(stderr, "401/7 and no auth header\n"));
1890202613Sdes				goto ouch;
1891202613Sdes			}
1892174588Sdes			fetch_close(conn);
189397856Sdes			conn = NULL;
189490267Sdes			continue;
189590267Sdes		}
189690267Sdes
1897125696Sdes		/* requested range not satisfiable */
1898125696Sdes		if (conn->err == HTTP_BAD_RANGE) {
1899125696Sdes			if (url->offset == size && url->length == 0) {
1900125696Sdes				/* asked for 0 bytes; fake it */
1901125696Sdes				offset = url->offset;
1902184222Sru				clength = -1;
1903125696Sdes				conn->err = HTTP_OK;
1904125696Sdes				break;
1905125696Sdes			} else {
1906174588Sdes				http_seterr(conn->err);
1907125696Sdes				goto ouch;
1908125696Sdes			}
1909125696Sdes		}
1910125696Sdes
1911104404Sru		/* we have a hit or an error */
1912186124Smurray		if (conn->err == HTTP_OK
1913186124Smurray		    || conn->err == HTTP_NOT_MODIFIED
1914186124Smurray		    || conn->err == HTTP_PARTIAL
1915186124Smurray		    || HTTP_ERROR(conn->err))
1916104404Sru			break;
1917104404Sru
191890267Sdes		/* all other cases: we got a redirect */
191998422Sdes		e = conn->err;
1920202613Sdes		clean_http_auth_challenges(&server_challenges);
1921174588Sdes		fetch_close(conn);
192297856Sdes		conn = NULL;
192390267Sdes		if (!new) {
192490267Sdes			DEBUG(fprintf(stderr, "redirect with no new location\n"));
192590267Sdes			break;
192690267Sdes		}
192790267Sdes		if (url != URL)
192890267Sdes			fetchFreeURL(url);
192990267Sdes		url = new;
193090267Sdes	} while (++i < n);
193190267Sdes
193290267Sdes	/* we failed, or ran out of retries */
193397856Sdes	if (conn == NULL) {
1934174588Sdes		http_seterr(e);
193563012Sdes		goto ouch;
193663012Sdes	}
193760376Sdes
193890267Sdes	DEBUG(fprintf(stderr, "offset %lld, length %lld,"
193990267Sdes		  " size %lld, clength %lld\n",
194090267Sdes		  (long long)offset, (long long)length,
194190267Sdes		  (long long)size, (long long)clength));
194260376Sdes
1943186124Smurray	if (conn->err == HTTP_NOT_MODIFIED) {
1944186124Smurray		http_seterr(HTTP_NOT_MODIFIED);
1945186124Smurray		return (NULL);
1946186124Smurray	}
1947186124Smurray
194890267Sdes	/* check for inconsistencies */
194990267Sdes	if (clength != -1 && length != -1 && clength != length) {
1950174588Sdes		http_seterr(HTTP_PROTOCOL_ERROR);
195163012Sdes		goto ouch;
195263012Sdes	}
195390267Sdes	if (clength == -1)
195490267Sdes		clength = length;
195590267Sdes	if (clength != -1)
195690267Sdes		length = offset + clength;
195790267Sdes	if (length != -1 && size != -1 && length != size) {
1958174588Sdes		http_seterr(HTTP_PROTOCOL_ERROR);
195963012Sdes		goto ouch;
196090267Sdes	}
196190267Sdes	if (size == -1)
196290267Sdes		size = length;
196360376Sdes
196490267Sdes	/* fill in stats */
196590267Sdes	if (us) {
196690267Sdes		us->size = size;
196790267Sdes		us->atime = us->mtime = mtime;
196890267Sdes	}
196963069Sdes
197090267Sdes	/* too far? */
1971109693Sdes	if (URL->offset > 0 && offset > URL->offset) {
1972174588Sdes		http_seterr(HTTP_PROTOCOL_ERROR);
197390267Sdes		goto ouch;
197477238Sdes	}
197560376Sdes
197690267Sdes	/* report back real offset and size */
197790267Sdes	URL->offset = offset;
197890267Sdes	URL->length = clength;
197937535Sdes
198090267Sdes	/* wrap it up in a FILE */
1981174588Sdes	if ((f = http_funopen(conn, chunked)) == NULL) {
1982174588Sdes		fetch_syserr();
198390267Sdes		goto ouch;
198490267Sdes	}
198563716Sdes
198690267Sdes	if (url != URL)
198790267Sdes		fetchFreeURL(url);
198890267Sdes	if (purl)
198990267Sdes		fetchFreeURL(purl);
199063567Sdes
199197856Sdes	if (HTTP_ERROR(conn->err)) {
1992174588Sdes		http_print_html(stderr, f);
199390267Sdes		fclose(f);
199490267Sdes		f = NULL;
199590267Sdes	}
1996202613Sdes	clean_http_headerbuf(&headerbuf);
1997202613Sdes	clean_http_auth_challenges(&server_challenges);
1998202613Sdes	clean_http_auth_challenges(&proxy_challenges);
199990267Sdes	return (f);
200088771Sdes
200190267Sdesouch:
200290267Sdes	if (url != URL)
200390267Sdes		fetchFreeURL(url);
200490267Sdes	if (purl)
200590267Sdes		fetchFreeURL(purl);
200697856Sdes	if (conn != NULL)
2007174588Sdes		fetch_close(conn);
2008202613Sdes	clean_http_headerbuf(&headerbuf);
2009202613Sdes	clean_http_auth_challenges(&server_challenges);
2010202613Sdes	clean_http_auth_challenges(&proxy_challenges);
201190267Sdes	return (NULL);
201263012Sdes}
201360189Sdes
201490267Sdes
201563012Sdes/*****************************************************************************
201663012Sdes * Entry points
201763012Sdes */
201863012Sdes
201963012Sdes/*
202063340Sdes * Retrieve and stat a file by HTTP
202163340Sdes */
202263340SdesFILE *
202375891SarchiefetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags)
202463340Sdes{
2025174752Sdes	return (http_request(URL, "GET", us, http_get_proxy(URL, flags), flags));
202663340Sdes}
202763340Sdes
202863340Sdes/*
202963012Sdes * Retrieve a file by HTTP
203063012Sdes */
203163012SdesFILE *
203275891SarchiefetchGetHTTP(struct url *URL, const char *flags)
203363012Sdes{
203490267Sdes	return (fetchXGetHTTP(URL, NULL, flags));
203537535Sdes}
203637535Sdes
203763340Sdes/*
203863340Sdes * Store a file by HTTP
203963340Sdes */
204037535SdesFILE *
204185093SdesfetchPutHTTP(struct url *URL __unused, const char *flags __unused)
204237535Sdes{
204390267Sdes	warnx("fetchPutHTTP(): not implemented");
204490267Sdes	return (NULL);
204537535Sdes}
204640975Sdes
204740975Sdes/*
204840975Sdes * Get an HTTP document's metadata
204940975Sdes */
205040975Sdesint
205175891SarchiefetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags)
205240975Sdes{
205390267Sdes	FILE *f;
205490267Sdes
2055174752Sdes	f = http_request(URL, "HEAD", us, http_get_proxy(URL, flags), flags);
2056112081Sdes	if (f == NULL)
205790267Sdes		return (-1);
205890267Sdes	fclose(f);
205990267Sdes	return (0);
206040975Sdes}
206141989Sdes
206241989Sdes/*
206341989Sdes * List a directory
206441989Sdes */
206541989Sdesstruct url_ent *
206685093SdesfetchListHTTP(struct url *url __unused, const char *flags __unused)
206741989Sdes{
206890267Sdes	warnx("fetchListHTTP(): not implemented");
206990267Sdes	return (NULL);
207041989Sdes}
2071268900Sbapt
2072268900SbaptFILE *
2073268900SbaptfetchReqHTTP(struct url *URL, const char *method, const char *flags,
2074268900Sbapt	const char *content_type, const char *body)
2075268900Sbapt{
2076268900Sbapt
2077268900Sbapt	return (http_request_body(URL, method, NULL, http_get_proxy(URL, flags),
2078268900Sbapt	    flags, content_type, body));
2079268900Sbapt}
2080