http.c revision 106044
137535Sdes/*-
263012Sdes * Copyright (c) 2000 Dag-Erling Co�dan 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: head/lib/libfetch/http.c 106044 2002-10-27 15:43:40Z des $");
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>
6637535Sdes
6763012Sdes#include <ctype.h>
6837535Sdes#include <err.h>
6963012Sdes#include <errno.h>
7060376Sdes#include <locale.h>
7160189Sdes#include <netdb.h>
7237608Sdes#include <stdarg.h>
7337535Sdes#include <stdio.h>
7437535Sdes#include <stdlib.h>
7537535Sdes#include <string.h>
7660376Sdes#include <time.h>
7737535Sdes#include <unistd.h>
7837535Sdes
7937535Sdes#include "fetch.h"
8040939Sdes#include "common.h"
8141862Sdes#include "httperr.h"
8237535Sdes
8363012Sdes/* Maximum number of redirects to follow */
8463012Sdes#define MAX_REDIRECT 5
8537535Sdes
8663012Sdes/* Symbolic names for reply codes we care about */
8763012Sdes#define HTTP_OK			200
8863012Sdes#define HTTP_PARTIAL		206
8963012Sdes#define HTTP_MOVED_PERM		301
9063012Sdes#define HTTP_MOVED_TEMP		302
9163012Sdes#define HTTP_SEE_OTHER		303
9263012Sdes#define HTTP_NEED_AUTH		401
9387317Sdes#define HTTP_NEED_PROXY_AUTH	407
9463012Sdes#define HTTP_PROTOCOL_ERROR	999
9560196Sdes
9663012Sdes#define HTTP_REDIRECT(xyz) ((xyz) == HTTP_MOVED_PERM \
9790267Sdes			    || (xyz) == HTTP_MOVED_TEMP \
9890267Sdes			    || (xyz) == HTTP_SEE_OTHER)
9963012Sdes
10088771Sdes#define HTTP_ERROR(xyz) ((xyz) > 400 && (xyz) < 599)
10163012Sdes
10290267Sdes
10363012Sdes/*****************************************************************************
10463012Sdes * I/O functions for decoding chunked streams
10563012Sdes */
10663012Sdes
10797859Sdesstruct httpio
10837535Sdes{
10997858Sdes	conn_t		*conn;		/* connection */
11097866Sdes	int		 chunked;	/* chunked mode */
11197858Sdes	char		*buf;		/* chunk buffer */
11297866Sdes	size_t		 bufsize;	/* size of chunk buffer */
11397866Sdes	ssize_t		 buflen;	/* amount of data currently in buffer */
11497866Sdes	int		 bufpos;	/* current read offset in buffer */
11597858Sdes	int		 eof;		/* end-of-file flag */
11697858Sdes	int		 error;		/* error flag */
11797858Sdes	size_t		 chunksize;	/* remaining size of current chunk */
11863281Sdes#ifndef NDEBUG
11990267Sdes	size_t		 total;
12063012Sdes#endif
12137535Sdes};
12237535Sdes
12337608Sdes/*
12463012Sdes * Get next chunk header
12537608Sdes */
12637608Sdesstatic int
12797859Sdes_http_new_chunk(struct httpio *io)
12837608Sdes{
12990267Sdes	char *p;
13090267Sdes
13197859Sdes	if (_fetch_getln(io->conn) == -1)
13290267Sdes		return (-1);
13390267Sdes
13497859Sdes	if (io->conn->buflen < 2 || !ishexnumber(*io->conn->buf))
13590267Sdes		return (-1);
13690267Sdes
13797859Sdes	for (p = io->conn->buf; *p && !isspace(*p); ++p) {
13890267Sdes		if (*p == ';')
13990267Sdes			break;
14090267Sdes		if (!ishexnumber(*p))
14190267Sdes			return (-1);
14290267Sdes		if (isdigit(*p)) {
14397859Sdes			io->chunksize = io->chunksize * 16 +
14490267Sdes			    *p - '0';
14590267Sdes		} else {
14697859Sdes			io->chunksize = io->chunksize * 16 +
14790267Sdes			    10 + tolower(*p) - 'a';
14890267Sdes		}
14990267Sdes	}
15090267Sdes
15163281Sdes#ifndef NDEBUG
15290267Sdes	if (fetchDebug) {
15397859Sdes		io->total += io->chunksize;
15497859Sdes		if (io->chunksize == 0)
15590267Sdes			fprintf(stderr, "_http_fillbuf(): "
15690267Sdes			    "end of last chunk\n");
15790267Sdes		else
15890267Sdes			fprintf(stderr, "_http_fillbuf(): "
15990267Sdes			    "new chunk: %lu (%lu)\n",
16097859Sdes			    (unsigned long)io->chunksize, (unsigned long)io->total);
16190267Sdes	}
16263012Sdes#endif
16390267Sdes
16497859Sdes	return (io->chunksize);
16537608Sdes}
16637608Sdes
16737608Sdes/*
16897866Sdes * Grow the input buffer to at least len bytes
16997866Sdes */
17097866Sdesstatic inline int
17197866Sdes_http_growbuf(struct httpio *io, size_t len)
17297866Sdes{
17397866Sdes	char *tmp;
17497866Sdes
17597866Sdes	if (io->bufsize >= len)
17697866Sdes		return (0);
17797866Sdes
17897866Sdes	if ((tmp = realloc(io->buf, len)) == NULL)
17997866Sdes		return (-1);
18097866Sdes	io->buf = tmp;
18197866Sdes	io->bufsize = len;
182106044Sdes	return (0);
18397866Sdes}
18497866Sdes
18597866Sdes/*
18637608Sdes * Fill the input buffer, do chunk decoding on the fly
18737608Sdes */
18863012Sdesstatic int
18997866Sdes_http_fillbuf(struct httpio *io, size_t len)
19037535Sdes{
19197859Sdes	if (io->error)
19290267Sdes		return (-1);
19397859Sdes	if (io->eof)
19490267Sdes		return (0);
19590267Sdes
19697866Sdes	if (io->chunked == 0) {
19797866Sdes		if (_http_growbuf(io, len) == -1)
19897866Sdes			return (-1);
19997866Sdes		if ((io->buflen = _fetch_read(io->conn, io->buf, len)) == -1)
20097866Sdes			return (-1);
20197866Sdes		io->bufpos = 0;
20297866Sdes		return (io->buflen);
20397866Sdes	}
20497866Sdes
20597859Sdes	if (io->chunksize == 0) {
20697859Sdes		switch (_http_new_chunk(io)) {
20790267Sdes		case -1:
20897859Sdes			io->error = 1;
20990267Sdes			return (-1);
21090267Sdes		case 0:
21197859Sdes			io->eof = 1;
21290267Sdes			return (0);
21390267Sdes		}
21437535Sdes	}
21563012Sdes
21697866Sdes	if (len > io->chunksize)
21797866Sdes		len = io->chunksize;
21897866Sdes	if (_http_growbuf(io, len) == -1)
21990267Sdes		return (-1);
22097866Sdes	if ((io->buflen = _fetch_read(io->conn, io->buf, len)) == -1)
22197866Sdes		return (-1);
22297866Sdes	io->chunksize -= io->buflen;
22390267Sdes
22497859Sdes	if (io->chunksize == 0) {
22597856Sdes		char endl[2];
22697856Sdes
22797866Sdes		if (_fetch_read(io->conn, endl, 2) != 2 ||
22897856Sdes		    endl[0] != '\r' || endl[1] != '\n')
22990267Sdes			return (-1);
23090267Sdes	}
23190267Sdes
23297866Sdes	io->bufpos = 0;
23390267Sdes
23497866Sdes	return (io->buflen);
23537535Sdes}
23637535Sdes
23737608Sdes/*
23837608Sdes * Read function
23937608Sdes */
24037535Sdesstatic int
24163012Sdes_http_readfn(void *v, char *buf, int len)
24237535Sdes{
24397859Sdes	struct httpio *io = (struct httpio *)v;
24490267Sdes	int l, pos;
24563012Sdes
24697859Sdes	if (io->error)
24790267Sdes		return (-1);
24897859Sdes	if (io->eof)
24990267Sdes		return (0);
25063012Sdes
25190267Sdes	for (pos = 0; len > 0; pos += l, len -= l) {
25290267Sdes		/* empty buffer */
25397866Sdes		if (!io->buf || io->bufpos == io->buflen)
25497866Sdes			if (_http_fillbuf(io, len) < 1)
25590267Sdes				break;
25697866Sdes		l = io->buflen - io->bufpos;
25790267Sdes		if (len < l)
25890267Sdes			l = len;
25997866Sdes		bcopy(io->buf + io->bufpos, buf + pos, l);
26097866Sdes		io->bufpos += l;
26190267Sdes	}
26237535Sdes
26397859Sdes	if (!pos && io->error)
26490267Sdes		return (-1);
26590267Sdes	return (pos);
26637535Sdes}
26737535Sdes
26837608Sdes/*
26937608Sdes * Write function
27037608Sdes */
27137535Sdesstatic int
27263012Sdes_http_writefn(void *v, const char *buf, int len)
27337535Sdes{
27497859Sdes	struct httpio *io = (struct httpio *)v;
27590267Sdes
27697866Sdes	return (_fetch_write(io->conn, buf, len));
27737535Sdes}
27837535Sdes
27937608Sdes/*
28037608Sdes * Close function
28137608Sdes */
28237535Sdesstatic int
28363012Sdes_http_closefn(void *v)
28437535Sdes{
28597859Sdes	struct httpio *io = (struct httpio *)v;
28690267Sdes	int r;
28763012Sdes
28897859Sdes	r = _fetch_close(io->conn);
28997859Sdes	if (io->buf)
29097859Sdes		free(io->buf);
29197859Sdes	free(io);
29290267Sdes	return (r);
29337535Sdes}
29437535Sdes
29537608Sdes/*
29663012Sdes * Wrap a file descriptor up
29737608Sdes */
29863012Sdesstatic FILE *
29997866Sdes_http_funopen(conn_t *conn, int chunked)
30037535Sdes{
30197859Sdes	struct httpio *io;
30290267Sdes	FILE *f;
30363012Sdes
30497859Sdes	if ((io = calloc(1, sizeof *io)) == NULL) {
30590267Sdes		_fetch_syserr();
30690267Sdes		return (NULL);
30790267Sdes	}
30897859Sdes	io->conn = conn;
30997866Sdes	io->chunked = chunked;
31097859Sdes	f = funopen(io, _http_readfn, _http_writefn, NULL, _http_closefn);
31190267Sdes	if (f == NULL) {
31290267Sdes		_fetch_syserr();
31397859Sdes		free(io);
31490267Sdes		return (NULL);
31590267Sdes	}
31690267Sdes	return (f);
31763012Sdes}
31863012Sdes
31990267Sdes
32063012Sdes/*****************************************************************************
32163012Sdes * Helper functions for talking to the server and parsing its replies
32263012Sdes */
32363012Sdes
32463012Sdes/* Header types */
32563012Sdestypedef enum {
32690267Sdes	hdr_syserror = -2,
32790267Sdes	hdr_error = -1,
32890267Sdes	hdr_end = 0,
32990267Sdes	hdr_unknown = 1,
33090267Sdes	hdr_content_length,
33190267Sdes	hdr_content_range,
33290267Sdes	hdr_last_modified,
33390267Sdes	hdr_location,
33490267Sdes	hdr_transfer_encoding,
33590267Sdes	hdr_www_authenticate
33685093Sdes} hdr_t;
33763012Sdes
33863012Sdes/* Names of interesting headers */
33963012Sdesstatic struct {
34090267Sdes	hdr_t		 num;
34190267Sdes	const char	*name;
34263012Sdes} hdr_names[] = {
34390267Sdes	{ hdr_content_length,		"Content-Length" },
34490267Sdes	{ hdr_content_range,		"Content-Range" },
34590267Sdes	{ hdr_last_modified,		"Last-Modified" },
34690267Sdes	{ hdr_location,			"Location" },
34790267Sdes	{ hdr_transfer_encoding,	"Transfer-Encoding" },
34890267Sdes	{ hdr_www_authenticate,		"WWW-Authenticate" },
34990267Sdes	{ hdr_unknown,			NULL },
35063012Sdes};
35163012Sdes
35263012Sdes/*
35363012Sdes * Send a formatted line; optionally echo to terminal
35463012Sdes */
35563012Sdesstatic int
35697856Sdes_http_cmd(conn_t *conn, const char *fmt, ...)
35763012Sdes{
35890267Sdes	va_list ap;
35990267Sdes	size_t len;
36090267Sdes	char *msg;
36190267Sdes	int r;
36263012Sdes
36390267Sdes	va_start(ap, fmt);
36490267Sdes	len = vasprintf(&msg, fmt, ap);
36590267Sdes	va_end(ap);
36690267Sdes
36790267Sdes	if (msg == NULL) {
36890267Sdes		errno = ENOMEM;
36990267Sdes		_fetch_syserr();
37090267Sdes		return (-1);
37190267Sdes	}
37290267Sdes
37397856Sdes	r = _fetch_putln(conn, msg, len);
37490267Sdes	free(msg);
37590267Sdes
37690267Sdes	if (r == -1) {
37790267Sdes		_fetch_syserr();
37890267Sdes		return (-1);
37990267Sdes	}
38090267Sdes
38190267Sdes	return (0);
38263012Sdes}
38363012Sdes
38463012Sdes/*
38563012Sdes * Get and parse status line
38663012Sdes */
38763012Sdesstatic int
38897856Sdes_http_get_reply(conn_t *conn)
38963012Sdes{
39090267Sdes	char *p;
39190267Sdes
39297856Sdes	if (_fetch_getln(conn) == -1)
39390267Sdes		return (-1);
39490267Sdes	/*
39590267Sdes	 * A valid status line looks like "HTTP/m.n xyz reason" where m
39690267Sdes	 * and n are the major and minor protocol version numbers and xyz
39790267Sdes	 * is the reply code.
39890267Sdes	 * Unfortunately, there are servers out there (NCSA 1.5.1, to name
39990267Sdes	 * just one) that do not send a version number, so we can't rely
40090267Sdes	 * on finding one, but if we do, insist on it being 1.0 or 1.1.
40190267Sdes	 * We don't care about the reason phrase.
40290267Sdes	 */
40397856Sdes	if (strncmp(conn->buf, "HTTP", 4) != 0)
40490267Sdes		return (HTTP_PROTOCOL_ERROR);
40597856Sdes	p = conn->buf + 4;
40690267Sdes	if (*p == '/') {
40790267Sdes		if (p[1] != '1' || p[2] != '.' || (p[3] != '0' && p[3] != '1'))
40890267Sdes			return (HTTP_PROTOCOL_ERROR);
40990267Sdes		p += 4;
41090267Sdes	}
41190267Sdes	if (*p != ' ' || !isdigit(p[1]) || !isdigit(p[2]) || !isdigit(p[3]))
41290267Sdes		return (HTTP_PROTOCOL_ERROR);
41390267Sdes
41497856Sdes	conn->err = (p[1] - '0') * 100 + (p[2] - '0') * 10 + (p[3] - '0');
41597856Sdes	return (conn->err);
41637535Sdes}
41737535Sdes
41837608Sdes/*
41990267Sdes * Check a header; if the type matches the given string, return a pointer
42090267Sdes * to the beginning of the value.
42163012Sdes */
42275891Sarchiestatic const char *
42375891Sarchie_http_match(const char *str, const char *hdr)
42463012Sdes{
42590267Sdes	while (*str && *hdr && tolower(*str++) == tolower(*hdr++))
42690267Sdes		/* nothing */;
42790267Sdes	if (*str || *hdr != ':')
42890267Sdes		return (NULL);
42990267Sdes	while (*hdr && isspace(*++hdr))
43090267Sdes		/* nothing */;
43190267Sdes	return (hdr);
43263012Sdes}
43363012Sdes
43463012Sdes/*
43563012Sdes * Get the next header and return the appropriate symbolic code.
43663012Sdes */
43785093Sdesstatic hdr_t
43897856Sdes_http_next_header(conn_t *conn, const char **p)
43963012Sdes{
44090267Sdes	int i;
44190267Sdes
44297856Sdes	if (_fetch_getln(conn) == -1)
44390267Sdes		return (hdr_syserror);
44497856Sdes	while (conn->buflen && isspace(conn->buf[conn->buflen - 1]))
44597856Sdes		conn->buflen--;
44697856Sdes	conn->buf[conn->buflen] = '\0';
44797856Sdes	if (conn->buflen == 0)
44897856Sdes		return (hdr_end);
44990267Sdes	/*
45090267Sdes	 * We could check for malformed headers but we don't really care.
45190267Sdes	 * A valid header starts with a token immediately followed by a
45290267Sdes	 * colon; a token is any sequence of non-control, non-whitespace
45390267Sdes	 * characters except "()<>@,;:\\\"{}".
45490267Sdes	 */
45590267Sdes	for (i = 0; hdr_names[i].num != hdr_unknown; i++)
45697856Sdes		if ((*p = _http_match(hdr_names[i].name, conn->buf)) != NULL)
45790267Sdes			return (hdr_names[i].num);
45890267Sdes	return (hdr_unknown);
45963012Sdes}
46063012Sdes
46163012Sdes/*
46263012Sdes * Parse a last-modified header
46363012Sdes */
46463716Sdesstatic int
46575891Sarchie_http_parse_mtime(const char *p, time_t *mtime)
46663012Sdes{
46790267Sdes	char locale[64], *r;
46890267Sdes	struct tm tm;
46963012Sdes
47090267Sdes	strncpy(locale, setlocale(LC_TIME, NULL), sizeof locale);
47190267Sdes	setlocale(LC_TIME, "C");
47290267Sdes	r = strptime(p, "%a, %d %b %Y %H:%M:%S GMT", &tm);
47390267Sdes	/* XXX should add support for date-2 and date-3 */
47490267Sdes	setlocale(LC_TIME, locale);
47590267Sdes	if (r == NULL)
47690267Sdes		return (-1);
47790267Sdes	DEBUG(fprintf(stderr, "last modified: [%04d-%02d-%02d "
47888769Sdes		  "%02d:%02d:%02d]\n",
47963012Sdes		  tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
48063012Sdes		  tm.tm_hour, tm.tm_min, tm.tm_sec));
48190267Sdes	*mtime = timegm(&tm);
48290267Sdes	return (0);
48363012Sdes}
48463012Sdes
48563012Sdes/*
48663012Sdes * Parse a content-length header
48763012Sdes */
48863716Sdesstatic int
48975891Sarchie_http_parse_length(const char *p, off_t *length)
49063012Sdes{
49190267Sdes	off_t len;
49290267Sdes
49390267Sdes	for (len = 0; *p && isdigit(*p); ++p)
49490267Sdes		len = len * 10 + (*p - '0');
49590267Sdes	if (*p)
49690267Sdes		return (-1);
49790267Sdes	DEBUG(fprintf(stderr, "content length: [%lld]\n",
49890267Sdes	    (long long)len));
49990267Sdes	*length = len;
50090267Sdes	return (0);
50163012Sdes}
50263012Sdes
50363012Sdes/*
50463012Sdes * Parse a content-range header
50563012Sdes */
50663716Sdesstatic int
50775891Sarchie_http_parse_range(const char *p, off_t *offset, off_t *length, off_t *size)
50863012Sdes{
50990267Sdes	off_t first, last, len;
51063716Sdes
51190267Sdes	if (strncasecmp(p, "bytes ", 6) != 0)
51290267Sdes		return (-1);
51390267Sdes	for (first = 0, p += 6; *p && isdigit(*p); ++p)
51490267Sdes		first = first * 10 + *p - '0';
51590267Sdes	if (*p != '-')
51690267Sdes		return (-1);
51790267Sdes	for (last = 0, ++p; *p && isdigit(*p); ++p)
51890267Sdes		last = last * 10 + *p - '0';
51990267Sdes	if (first > last || *p != '/')
52090267Sdes		return (-1);
52190267Sdes	for (len = 0, ++p; *p && isdigit(*p); ++p)
52290267Sdes		len = len * 10 + *p - '0';
52390267Sdes	if (*p || len < last - first + 1)
52490267Sdes		return (-1);
52590267Sdes	DEBUG(fprintf(stderr, "content range: [%lld-%lld/%lld]\n",
52690267Sdes	    (long long)first, (long long)last, (long long)len));
52790267Sdes	*offset = first;
52890267Sdes	*length = last - first + 1;
52990267Sdes	*size = len;
53090267Sdes	return (0);
53163012Sdes}
53263012Sdes
53390267Sdes
53463012Sdes/*****************************************************************************
53563012Sdes * Helper functions for authorization
53663012Sdes */
53763012Sdes
53863012Sdes/*
53937608Sdes * Base64 encoding
54037608Sdes */
54162965Sdesstatic char *
54290267Sdes_http_base64(const char *src)
54337608Sdes{
54490267Sdes	static const char base64[] =
54590267Sdes	    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
54690267Sdes	    "abcdefghijklmnopqrstuvwxyz"
54790267Sdes	    "0123456789+/";
54890267Sdes	char *str, *dst;
54990267Sdes	size_t l;
55090267Sdes	int t, r;
55162965Sdes
55290267Sdes	l = strlen(src);
55390267Sdes	if ((str = malloc(((l + 2) / 3) * 4)) == NULL)
55490267Sdes		return (NULL);
55590267Sdes	dst = str;
55690267Sdes	r = 0;
55737608Sdes
55890267Sdes	while (l >= 3) {
55990267Sdes		t = (src[0] << 16) | (src[1] << 8) | src[2];
56090267Sdes		dst[0] = base64[(t >> 18) & 0x3f];
56190267Sdes		dst[1] = base64[(t >> 12) & 0x3f];
56290267Sdes		dst[2] = base64[(t >> 6) & 0x3f];
56390267Sdes		dst[3] = base64[(t >> 0) & 0x3f];
56490267Sdes		src += 3; l -= 3;
56590267Sdes		dst += 4; r += 4;
56690267Sdes	}
56737608Sdes
56890267Sdes	switch (l) {
56990267Sdes	case 2:
57090267Sdes		t = (src[0] << 16) | (src[1] << 8);
57190267Sdes		dst[0] = base64[(t >> 18) & 0x3f];
57290267Sdes		dst[1] = base64[(t >> 12) & 0x3f];
57390267Sdes		dst[2] = base64[(t >> 6) & 0x3f];
57490267Sdes		dst[3] = '=';
57590267Sdes		dst += 4;
57690267Sdes		r += 4;
57790267Sdes		break;
57890267Sdes	case 1:
57990267Sdes		t = src[0] << 16;
58090267Sdes		dst[0] = base64[(t >> 18) & 0x3f];
58190267Sdes		dst[1] = base64[(t >> 12) & 0x3f];
58290267Sdes		dst[2] = dst[3] = '=';
58390267Sdes		dst += 4;
58490267Sdes		r += 4;
58590267Sdes		break;
58690267Sdes	case 0:
58790267Sdes		break;
58890267Sdes	}
58990267Sdes
59090267Sdes	*dst = 0;
59190267Sdes	return (str);
59237608Sdes}
59337608Sdes
59437608Sdes/*
59537608Sdes * Encode username and password
59637608Sdes */
59762965Sdesstatic int
59897856Sdes_http_basic_auth(conn_t *conn, const char *hdr, const char *usr, const char *pwd)
59937608Sdes{
60090267Sdes	char *upw, *auth;
60190267Sdes	int r;
60237608Sdes
60390267Sdes	DEBUG(fprintf(stderr, "usr: [%s]\n", usr));
60490267Sdes	DEBUG(fprintf(stderr, "pwd: [%s]\n", pwd));
60590267Sdes	if (asprintf(&upw, "%s:%s", usr, pwd) == -1)
60690267Sdes		return (-1);
60790267Sdes	auth = _http_base64(upw);
60890267Sdes	free(upw);
60990267Sdes	if (auth == NULL)
61090267Sdes		return (-1);
61197856Sdes	r = _http_cmd(conn, "%s: Basic %s", hdr, auth);
61290267Sdes	free(auth);
61390267Sdes	return (r);
61462965Sdes}
61562965Sdes
61662965Sdes/*
61762965Sdes * Send an authorization header
61862965Sdes */
61962965Sdesstatic int
62097856Sdes_http_authorize(conn_t *conn, const char *hdr, const char *p)
62162965Sdes{
62290267Sdes	/* basic authorization */
62390267Sdes	if (strncasecmp(p, "basic:", 6) == 0) {
62490267Sdes		char *user, *pwd, *str;
62590267Sdes		int r;
62662965Sdes
62790267Sdes		/* skip realm */
62890267Sdes		for (p += 6; *p && *p != ':'; ++p)
62990267Sdes			/* nothing */ ;
63090267Sdes		if (!*p || strchr(++p, ':') == NULL)
63190267Sdes			return (-1);
63290267Sdes		if ((str = strdup(p)) == NULL)
63390267Sdes			return (-1); /* XXX */
63490267Sdes		user = str;
63590267Sdes		pwd = strchr(str, ':');
63690267Sdes		*pwd++ = '\0';
63797856Sdes		r = _http_basic_auth(conn, hdr, user, pwd);
63890267Sdes		free(str);
63990267Sdes		return (r);
64090267Sdes	}
64190267Sdes	return (-1);
64237608Sdes}
64337608Sdes
64490267Sdes
64563012Sdes/*****************************************************************************
64663012Sdes * Helper functions for connecting to a server or proxy
64763012Sdes */
64863012Sdes
64937608Sdes/*
65090267Sdes * Connect to the correct HTTP server or proxy.
65163012Sdes */
65297856Sdesstatic conn_t *
65375891Sarchie_http_connect(struct url *URL, struct url *purl, const char *flags)
65463012Sdes{
65597856Sdes	conn_t *conn;
65690267Sdes	int verbose;
65797856Sdes	int af;
65890267Sdes
65963012Sdes#ifdef INET6
66090267Sdes	af = AF_UNSPEC;
66160737Sume#else
66290267Sdes	af = AF_INET;
66360737Sume#endif
66490267Sdes
66590267Sdes	verbose = CHECK_FLAG('v');
66690267Sdes	if (CHECK_FLAG('4'))
66790267Sdes		af = AF_INET;
66867043Sdes#ifdef INET6
66990267Sdes	else if (CHECK_FLAG('6'))
67090267Sdes		af = AF_INET6;
67167043Sdes#endif
67267043Sdes
67397868Sdes	if (purl && strcasecmp(URL->scheme, SCHEME_HTTPS) != 0) {
67490267Sdes		URL = purl;
67590267Sdes	} else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0) {
67690267Sdes		/* can't talk http to an ftp server */
67790267Sdes		/* XXX should set an error code */
67897856Sdes		return (NULL);
67990267Sdes	}
68090267Sdes
68197856Sdes	if ((conn = _fetch_connect(URL->host, URL->port, af, verbose)) == NULL)
68290267Sdes		/* _fetch_connect() has already set an error code */
68397856Sdes		return (NULL);
68497868Sdes	if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0 &&
68597868Sdes	    _fetch_ssl(conn, verbose) == -1) {
68697868Sdes		_fetch_close(conn);
68797891Sdes		/* grrr */
68897891Sdes		errno = EAUTH;
68997891Sdes		_fetch_syserr();
69097868Sdes		return (NULL);
69197868Sdes	}
69297856Sdes	return (conn);
69367043Sdes}
69467043Sdes
69567043Sdesstatic struct url *
69675891Sarchie_http_get_proxy(void)
69767043Sdes{
69890267Sdes	struct url *purl;
69990267Sdes	char *p;
70090267Sdes
70190267Sdes	if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
70290267Sdes	    (purl = fetchParseURL(p))) {
70390267Sdes		if (!*purl->scheme)
70490267Sdes			strcpy(purl->scheme, SCHEME_HTTP);
70590267Sdes		if (!purl->port)
70690267Sdes			purl->port = _fetch_default_proxy_port(purl->scheme);
70790267Sdes		if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
70890267Sdes			return (purl);
70990267Sdes		fetchFreeURL(purl);
71090267Sdes	}
71190267Sdes	return (NULL);
71260376Sdes}
71360376Sdes
71488771Sdesstatic void
71588771Sdes_http_print_html(FILE *out, FILE *in)
71688771Sdes{
71790267Sdes	size_t len;
71890267Sdes	char *line, *p, *q;
71990267Sdes	int comment, tag;
72088771Sdes
72190267Sdes	comment = tag = 0;
72290267Sdes	while ((line = fgetln(in, &len)) != NULL) {
72390267Sdes		while (len && isspace(line[len - 1]))
72490267Sdes			--len;
72590267Sdes		for (p = q = line; q < line + len; ++q) {
72690267Sdes			if (comment && *q == '-') {
72790267Sdes				if (q + 2 < line + len &&
72890267Sdes				    strcmp(q, "-->") == 0) {
72990267Sdes					tag = comment = 0;
73090267Sdes					q += 2;
73190267Sdes				}
73290267Sdes			} else if (tag && !comment && *q == '>') {
73390267Sdes				p = q + 1;
73490267Sdes				tag = 0;
73590267Sdes			} else if (!tag && *q == '<') {
73690267Sdes				if (q > p)
73790267Sdes					fwrite(p, q - p, 1, out);
73890267Sdes				tag = 1;
73990267Sdes				if (q + 3 < line + len &&
74090267Sdes				    strcmp(q, "<!--") == 0) {
74190267Sdes					comment = 1;
74290267Sdes					q += 3;
74390267Sdes				}
74490267Sdes			}
74588771Sdes		}
74690267Sdes		if (!tag && q > p)
74790267Sdes			fwrite(p, q - p, 1, out);
74890267Sdes		fputc('\n', out);
74988771Sdes	}
75088771Sdes}
75188771Sdes
75290267Sdes
75363012Sdes/*****************************************************************************
75463012Sdes * Core
75560954Sdes */
75660954Sdes
75760954Sdes/*
75863012Sdes * Send a request and process the reply
75997866Sdes *
76097866Sdes * XXX This function is way too long, the do..while loop should be split
76197866Sdes * XXX off into a separate function.
76260376Sdes */
76367043SdesFILE *
76475891Sarchie_http_request(struct url *URL, const char *op, struct url_stat *us,
76590267Sdes    struct url *purl, const char *flags)
76660376Sdes{
76797856Sdes	conn_t *conn;
76890267Sdes	struct url *url, *new;
76990267Sdes	int chunked, direct, need_auth, noredirect, verbose;
77098422Sdes	int e, i, n;
77190267Sdes	off_t offset, clength, length, size;
77290267Sdes	time_t mtime;
77390267Sdes	const char *p;
77490267Sdes	FILE *f;
77590267Sdes	hdr_t h;
77690267Sdes	char *host;
77760737Sume#ifdef INET6
77890267Sdes	char hbuf[MAXHOSTNAMELEN + 1];
77960737Sume#endif
78063012Sdes
78190267Sdes	direct = CHECK_FLAG('d');
78290267Sdes	noredirect = CHECK_FLAG('A');
78390267Sdes	verbose = CHECK_FLAG('v');
78460737Sume
78590267Sdes	if (direct && purl) {
78690267Sdes		fetchFreeURL(purl);
78790267Sdes		purl = NULL;
78890267Sdes	}
78963716Sdes
79090267Sdes	/* try the provided URL first */
79190267Sdes	url = URL;
79263012Sdes
79390267Sdes	/* if the A flag is set, we only get one try */
79490267Sdes	n = noredirect ? 1 : MAX_REDIRECT;
79590267Sdes	i = 0;
79663012Sdes
79798422Sdes	e = HTTP_PROTOCOL_ERROR;
79890267Sdes	need_auth = 0;
79990267Sdes	do {
80090267Sdes		new = NULL;
80190267Sdes		chunked = 0;
80290267Sdes		offset = 0;
80390267Sdes		clength = -1;
80490267Sdes		length = -1;
80590267Sdes		size = -1;
80690267Sdes		mtime = 0;
80790267Sdes
80890267Sdes		/* check port */
80990267Sdes		if (!url->port)
81090267Sdes			url->port = _fetch_default_port(url->scheme);
81190267Sdes
81290267Sdes		/* were we redirected to an FTP URL? */
81390267Sdes		if (purl == NULL && strcmp(url->scheme, SCHEME_FTP) == 0) {
81490267Sdes			if (strcmp(op, "GET") == 0)
81590267Sdes				return (_ftp_request(url, "RETR", us, purl, flags));
81690267Sdes			else if (strcmp(op, "HEAD") == 0)
81790267Sdes				return (_ftp_request(url, "STAT", us, purl, flags));
81890267Sdes		}
81990267Sdes
82090267Sdes		/* connect to server or proxy */
82197856Sdes		if ((conn = _http_connect(url, purl, flags)) == NULL)
82290267Sdes			goto ouch;
82390267Sdes
82490267Sdes		host = url->host;
82560737Sume#ifdef INET6
82690267Sdes		if (strchr(url->host, ':')) {
82790267Sdes			snprintf(hbuf, sizeof(hbuf), "[%s]", url->host);
82890267Sdes			host = hbuf;
82990267Sdes		}
83060737Sume#endif
83137535Sdes
83290267Sdes		/* send request */
83390267Sdes		if (verbose)
83490267Sdes			_fetch_info("requesting %s://%s:%d%s",
83590267Sdes			    url->scheme, host, url->port, url->doc);
83690267Sdes		if (purl) {
83797856Sdes			_http_cmd(conn, "%s %s://%s:%d%s HTTP/1.1",
83890267Sdes			    op, url->scheme, host, url->port, url->doc);
83990267Sdes		} else {
84097856Sdes			_http_cmd(conn, "%s %s HTTP/1.1",
84190267Sdes			    op, url->doc);
84290267Sdes		}
84337535Sdes
84490267Sdes		/* virtual host */
84590267Sdes		if (url->port == _fetch_default_port(url->scheme))
84697856Sdes			_http_cmd(conn, "Host: %s", host);
84790267Sdes		else
84897856Sdes			_http_cmd(conn, "Host: %s:%d", host, url->port);
84990267Sdes
85090267Sdes		/* proxy authorization */
85190267Sdes		if (purl) {
85290267Sdes			if (*purl->user || *purl->pwd)
85397856Sdes				_http_basic_auth(conn, "Proxy-Authorization",
85490267Sdes				    purl->user, purl->pwd);
85590267Sdes			else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL && *p != '\0')
85697856Sdes				_http_authorize(conn, "Proxy-Authorization", p);
85790267Sdes		}
85890267Sdes
85990267Sdes		/* server authorization */
86090267Sdes		if (need_auth || *url->user || *url->pwd) {
86190267Sdes			if (*url->user || *url->pwd)
86297856Sdes				_http_basic_auth(conn, "Authorization", url->user, url->pwd);
86390267Sdes			else if ((p = getenv("HTTP_AUTH")) != NULL && *p != '\0')
86497856Sdes				_http_authorize(conn, "Authorization", p);
86590267Sdes			else if (fetchAuthMethod && fetchAuthMethod(url) == 0) {
86697856Sdes				_http_basic_auth(conn, "Authorization", url->user, url->pwd);
86790267Sdes			} else {
86890267Sdes				_http_seterr(HTTP_NEED_AUTH);
86990267Sdes				goto ouch;
87090267Sdes			}
87190267Sdes		}
87290267Sdes
87390267Sdes		/* other headers */
87490267Sdes		if ((p = getenv("HTTP_USER_AGENT")) != NULL && *p != '\0')
87597856Sdes			_http_cmd(conn, "User-Agent: %s", p);
87690267Sdes		else
87797856Sdes			_http_cmd(conn, "User-Agent: %s " _LIBFETCH_VER, getprogname());
87890267Sdes		if (url->offset)
87997856Sdes			_http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset);
88097856Sdes		_http_cmd(conn, "Connection: close");
88197856Sdes		_http_cmd(conn, "");
88290267Sdes
88390267Sdes		/* get reply */
88497856Sdes		switch (_http_get_reply(conn)) {
88590267Sdes		case HTTP_OK:
88690267Sdes		case HTTP_PARTIAL:
88790267Sdes			/* fine */
88890267Sdes			break;
88990267Sdes		case HTTP_MOVED_PERM:
89090267Sdes		case HTTP_MOVED_TEMP:
89190267Sdes		case HTTP_SEE_OTHER:
89290267Sdes			/*
89390267Sdes			 * Not so fine, but we still have to read the headers to
89490267Sdes			 * get the new location.
89590267Sdes			 */
89690267Sdes			break;
89790267Sdes		case HTTP_NEED_AUTH:
89890267Sdes			if (need_auth) {
89990267Sdes				/*
90090267Sdes				 * We already sent out authorization code, so there's
90190267Sdes				 * nothing more we can do.
90290267Sdes				 */
90397856Sdes				_http_seterr(conn->err);
90490267Sdes				goto ouch;
90590267Sdes			}
90690267Sdes			/* try again, but send the password this time */
90790267Sdes			if (verbose)
90890267Sdes				_fetch_info("server requires authorization");
90990267Sdes			break;
91090267Sdes		case HTTP_NEED_PROXY_AUTH:
91190267Sdes			/*
91290267Sdes			 * If we're talking to a proxy, we already sent our proxy
91390267Sdes			 * authorization code, so there's nothing more we can do.
91490267Sdes			 */
91597856Sdes			_http_seterr(conn->err);
91690267Sdes			goto ouch;
91790267Sdes		case HTTP_PROTOCOL_ERROR:
91890267Sdes			/* fall through */
91990267Sdes		case -1:
92090267Sdes			_fetch_syserr();
92190267Sdes			goto ouch;
92290267Sdes		default:
92397856Sdes			_http_seterr(conn->err);
92490267Sdes			if (!verbose)
92590267Sdes				goto ouch;
92690267Sdes			/* fall through so we can get the full error message */
92790267Sdes		}
92890267Sdes
92990267Sdes		/* get headers */
93090267Sdes		do {
93197856Sdes			switch ((h = _http_next_header(conn, &p))) {
93290267Sdes			case hdr_syserror:
93390267Sdes				_fetch_syserr();
93490267Sdes				goto ouch;
93590267Sdes			case hdr_error:
93690267Sdes				_http_seterr(HTTP_PROTOCOL_ERROR);
93790267Sdes				goto ouch;
93890267Sdes			case hdr_content_length:
93990267Sdes				_http_parse_length(p, &clength);
94090267Sdes				break;
94190267Sdes			case hdr_content_range:
94290267Sdes				_http_parse_range(p, &offset, &length, &size);
94390267Sdes				break;
94490267Sdes			case hdr_last_modified:
94590267Sdes				_http_parse_mtime(p, &mtime);
94690267Sdes				break;
94790267Sdes			case hdr_location:
94897856Sdes				if (!HTTP_REDIRECT(conn->err))
94990267Sdes					break;
95090267Sdes				if (new)
95190267Sdes					free(new);
95290267Sdes				if (verbose)
95397856Sdes					_fetch_info("%d redirect to %s", conn->err, p);
95490267Sdes				if (*p == '/')
95590267Sdes					/* absolute path */
95690267Sdes					new = fetchMakeURL(url->scheme, url->host, url->port, p,
95790267Sdes					    url->user, url->pwd);
95890267Sdes				else
95990267Sdes					new = fetchParseURL(p);
96090267Sdes				if (new == NULL) {
96190267Sdes					/* XXX should set an error code */
96290267Sdes					DEBUG(fprintf(stderr, "failed to parse new URL\n"));
96390267Sdes					goto ouch;
96490267Sdes				}
96590267Sdes				if (!*new->user && !*new->pwd) {
96690267Sdes					strcpy(new->user, url->user);
96790267Sdes					strcpy(new->pwd, url->pwd);
96890267Sdes				}
96990267Sdes				new->offset = url->offset;
97090267Sdes				new->length = url->length;
97190267Sdes				break;
97290267Sdes			case hdr_transfer_encoding:
97390267Sdes				/* XXX weak test*/
97490267Sdes				chunked = (strcasecmp(p, "chunked") == 0);
97590267Sdes				break;
97690267Sdes			case hdr_www_authenticate:
97797856Sdes				if (conn->err != HTTP_NEED_AUTH)
97890267Sdes					break;
97990267Sdes				/* if we were smarter, we'd check the method and realm */
98090267Sdes				break;
98190267Sdes			case hdr_end:
98290267Sdes				/* fall through */
98390267Sdes			case hdr_unknown:
98490267Sdes				/* ignore */
98590267Sdes				break;
98690267Sdes			}
98790267Sdes		} while (h > hdr_end);
98890267Sdes
98990267Sdes		/* we need to provide authentication */
99097856Sdes		if (conn->err == HTTP_NEED_AUTH) {
99198422Sdes			e = conn->err;
99290267Sdes			need_auth = 1;
99397856Sdes			_fetch_close(conn);
99497856Sdes			conn = NULL;
99590267Sdes			continue;
99690267Sdes		}
99790267Sdes
998104404Sru		/* we have a hit or an error */
999104404Sru		if (conn->err == HTTP_OK || conn->err == HTTP_PARTIAL || HTTP_ERROR(conn->err))
1000104404Sru			break;
1001104404Sru
100290267Sdes		/* all other cases: we got a redirect */
100398422Sdes		e = conn->err;
100490267Sdes		need_auth = 0;
100597856Sdes		_fetch_close(conn);
100697856Sdes		conn = NULL;
100790267Sdes		if (!new) {
100890267Sdes			DEBUG(fprintf(stderr, "redirect with no new location\n"));
100990267Sdes			break;
101090267Sdes		}
101190267Sdes		if (url != URL)
101290267Sdes			fetchFreeURL(url);
101390267Sdes		url = new;
101490267Sdes	} while (++i < n);
101590267Sdes
101690267Sdes	/* we failed, or ran out of retries */
101797856Sdes	if (conn == NULL) {
101898422Sdes		_http_seterr(e);
101963012Sdes		goto ouch;
102063012Sdes	}
102160376Sdes
102290267Sdes	DEBUG(fprintf(stderr, "offset %lld, length %lld,"
102390267Sdes		  " size %lld, clength %lld\n",
102490267Sdes		  (long long)offset, (long long)length,
102590267Sdes		  (long long)size, (long long)clength));
102660376Sdes
102790267Sdes	/* check for inconsistencies */
102890267Sdes	if (clength != -1 && length != -1 && clength != length) {
102990267Sdes		_http_seterr(HTTP_PROTOCOL_ERROR);
103063012Sdes		goto ouch;
103163012Sdes	}
103290267Sdes	if (clength == -1)
103390267Sdes		clength = length;
103490267Sdes	if (clength != -1)
103590267Sdes		length = offset + clength;
103690267Sdes	if (length != -1 && size != -1 && length != size) {
103763012Sdes		_http_seterr(HTTP_PROTOCOL_ERROR);
103863012Sdes		goto ouch;
103990267Sdes	}
104090267Sdes	if (size == -1)
104190267Sdes		size = length;
104260376Sdes
104390267Sdes	/* fill in stats */
104490267Sdes	if (us) {
104590267Sdes		us->size = size;
104690267Sdes		us->atime = us->mtime = mtime;
104790267Sdes	}
104863069Sdes
104990267Sdes	/* too far? */
105090267Sdes	if (offset > URL->offset) {
105190267Sdes		_http_seterr(HTTP_PROTOCOL_ERROR);
105290267Sdes		goto ouch;
105377238Sdes	}
105460376Sdes
105590267Sdes	/* report back real offset and size */
105690267Sdes	URL->offset = offset;
105790267Sdes	URL->length = clength;
105837535Sdes
105990267Sdes	/* wrap it up in a FILE */
106097866Sdes	if ((f = _http_funopen(conn, chunked)) == NULL) {
106190267Sdes		_fetch_syserr();
106290267Sdes		goto ouch;
106390267Sdes	}
106463716Sdes
106590267Sdes	if (url != URL)
106690267Sdes		fetchFreeURL(url);
106790267Sdes	if (purl)
106890267Sdes		fetchFreeURL(purl);
106963567Sdes
107097856Sdes	if (HTTP_ERROR(conn->err)) {
107190267Sdes		_http_print_html(stderr, f);
107290267Sdes		fclose(f);
107390267Sdes		f = NULL;
107490267Sdes	}
107563012Sdes
107690267Sdes	return (f);
107788771Sdes
107890267Sdesouch:
107990267Sdes	if (url != URL)
108090267Sdes		fetchFreeURL(url);
108190267Sdes	if (purl)
108290267Sdes		fetchFreeURL(purl);
108397856Sdes	if (conn != NULL)
108497856Sdes		_fetch_close(conn);
108590267Sdes	return (NULL);
108663012Sdes}
108760189Sdes
108890267Sdes
108963012Sdes/*****************************************************************************
109063012Sdes * Entry points
109163012Sdes */
109263012Sdes
109363012Sdes/*
109463340Sdes * Retrieve and stat a file by HTTP
109563340Sdes */
109663340SdesFILE *
109775891SarchiefetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags)
109863340Sdes{
109990267Sdes	return (_http_request(URL, "GET", us, _http_get_proxy(), flags));
110063340Sdes}
110163340Sdes
110263340Sdes/*
110363012Sdes * Retrieve a file by HTTP
110463012Sdes */
110563012SdesFILE *
110675891SarchiefetchGetHTTP(struct url *URL, const char *flags)
110763012Sdes{
110890267Sdes	return (fetchXGetHTTP(URL, NULL, flags));
110937535Sdes}
111037535Sdes
111163340Sdes/*
111263340Sdes * Store a file by HTTP
111363340Sdes */
111437535SdesFILE *
111585093SdesfetchPutHTTP(struct url *URL __unused, const char *flags __unused)
111637535Sdes{
111790267Sdes	warnx("fetchPutHTTP(): not implemented");
111890267Sdes	return (NULL);
111937535Sdes}
112040975Sdes
112140975Sdes/*
112240975Sdes * Get an HTTP document's metadata
112340975Sdes */
112440975Sdesint
112575891SarchiefetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags)
112640975Sdes{
112790267Sdes	FILE *f;
112890267Sdes
112990267Sdes	if ((f = _http_request(URL, "HEAD", us, _http_get_proxy(), flags)) == NULL)
113090267Sdes		return (-1);
113190267Sdes	fclose(f);
113290267Sdes	return (0);
113340975Sdes}
113441989Sdes
113541989Sdes/*
113641989Sdes * List a directory
113741989Sdes */
113841989Sdesstruct url_ent *
113985093SdesfetchListHTTP(struct url *url __unused, const char *flags __unused)
114041989Sdes{
114190267Sdes	warnx("fetchListHTTP(): not implemented");
114290267Sdes	return (NULL);
114341989Sdes}
1144