http.c revision 104404
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 104404 2002-10-03 10:42:19Z ru $");
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;
18297866Sdes}
18397866Sdes
18497866Sdes/*
18537608Sdes * Fill the input buffer, do chunk decoding on the fly
18637608Sdes */
18763012Sdesstatic int
18897866Sdes_http_fillbuf(struct httpio *io, size_t len)
18937535Sdes{
19097859Sdes	if (io->error)
19190267Sdes		return (-1);
19297859Sdes	if (io->eof)
19390267Sdes		return (0);
19490267Sdes
19597866Sdes	if (io->chunked == 0) {
19697866Sdes		if (_http_growbuf(io, len) == -1)
19797866Sdes			return (-1);
19897866Sdes		if ((io->buflen = _fetch_read(io->conn, io->buf, len)) == -1)
19997866Sdes			return (-1);
20097866Sdes		io->bufpos = 0;
20197866Sdes		return (io->buflen);
20297866Sdes	}
20397866Sdes
20497859Sdes	if (io->chunksize == 0) {
20597859Sdes		switch (_http_new_chunk(io)) {
20690267Sdes		case -1:
20797859Sdes			io->error = 1;
20890267Sdes			return (-1);
20990267Sdes		case 0:
21097859Sdes			io->eof = 1;
21190267Sdes			return (0);
21290267Sdes		}
21337535Sdes	}
21463012Sdes
21597866Sdes	if (len > io->chunksize)
21697866Sdes		len = io->chunksize;
21797866Sdes	if (_http_growbuf(io, len) == -1)
21890267Sdes		return (-1);
21997866Sdes	if ((io->buflen = _fetch_read(io->conn, io->buf, len)) == -1)
22097866Sdes		return (-1);
22197866Sdes	io->chunksize -= io->buflen;
22290267Sdes
22397859Sdes	if (io->chunksize == 0) {
22497856Sdes		char endl[2];
22597856Sdes
22697866Sdes		if (_fetch_read(io->conn, endl, 2) != 2 ||
22797856Sdes		    endl[0] != '\r' || endl[1] != '\n')
22890267Sdes			return (-1);
22990267Sdes	}
23090267Sdes
23197866Sdes	io->bufpos = 0;
23290267Sdes
23397866Sdes	return (io->buflen);
23437535Sdes}
23537535Sdes
23637608Sdes/*
23737608Sdes * Read function
23837608Sdes */
23937535Sdesstatic int
24063012Sdes_http_readfn(void *v, char *buf, int len)
24137535Sdes{
24297859Sdes	struct httpio *io = (struct httpio *)v;
24390267Sdes	int l, pos;
24463012Sdes
24597859Sdes	if (io->error)
24690267Sdes		return (-1);
24797859Sdes	if (io->eof)
24890267Sdes		return (0);
24963012Sdes
25090267Sdes	for (pos = 0; len > 0; pos += l, len -= l) {
25190267Sdes		/* empty buffer */
25297866Sdes		if (!io->buf || io->bufpos == io->buflen)
25397866Sdes			if (_http_fillbuf(io, len) < 1)
25490267Sdes				break;
25597866Sdes		l = io->buflen - io->bufpos;
25690267Sdes		if (len < l)
25790267Sdes			l = len;
25897866Sdes		bcopy(io->buf + io->bufpos, buf + pos, l);
25997866Sdes		io->bufpos += l;
26090267Sdes	}
26137535Sdes
26297859Sdes	if (!pos && io->error)
26390267Sdes		return (-1);
26490267Sdes	return (pos);
26537535Sdes}
26637535Sdes
26737608Sdes/*
26837608Sdes * Write function
26937608Sdes */
27037535Sdesstatic int
27163012Sdes_http_writefn(void *v, const char *buf, int len)
27237535Sdes{
27397859Sdes	struct httpio *io = (struct httpio *)v;
27490267Sdes
27597866Sdes	return (_fetch_write(io->conn, buf, len));
27637535Sdes}
27737535Sdes
27837608Sdes/*
27937608Sdes * Close function
28037608Sdes */
28137535Sdesstatic int
28263012Sdes_http_closefn(void *v)
28337535Sdes{
28497859Sdes	struct httpio *io = (struct httpio *)v;
28590267Sdes	int r;
28663012Sdes
28797859Sdes	r = _fetch_close(io->conn);
28897859Sdes	if (io->buf)
28997859Sdes		free(io->buf);
29097859Sdes	free(io);
29190267Sdes	return (r);
29237535Sdes}
29337535Sdes
29437608Sdes/*
29563012Sdes * Wrap a file descriptor up
29637608Sdes */
29763012Sdesstatic FILE *
29897866Sdes_http_funopen(conn_t *conn, int chunked)
29937535Sdes{
30097859Sdes	struct httpio *io;
30190267Sdes	FILE *f;
30263012Sdes
30397859Sdes	if ((io = calloc(1, sizeof *io)) == NULL) {
30490267Sdes		_fetch_syserr();
30590267Sdes		return (NULL);
30690267Sdes	}
30797859Sdes	io->conn = conn;
30897866Sdes	io->chunked = chunked;
30997859Sdes	f = funopen(io, _http_readfn, _http_writefn, NULL, _http_closefn);
31090267Sdes	if (f == NULL) {
31190267Sdes		_fetch_syserr();
31297859Sdes		free(io);
31390267Sdes		return (NULL);
31490267Sdes	}
31590267Sdes	return (f);
31663012Sdes}
31763012Sdes
31890267Sdes
31963012Sdes/*****************************************************************************
32063012Sdes * Helper functions for talking to the server and parsing its replies
32163012Sdes */
32263012Sdes
32363012Sdes/* Header types */
32463012Sdestypedef enum {
32590267Sdes	hdr_syserror = -2,
32690267Sdes	hdr_error = -1,
32790267Sdes	hdr_end = 0,
32890267Sdes	hdr_unknown = 1,
32990267Sdes	hdr_content_length,
33090267Sdes	hdr_content_range,
33190267Sdes	hdr_last_modified,
33290267Sdes	hdr_location,
33390267Sdes	hdr_transfer_encoding,
33490267Sdes	hdr_www_authenticate
33585093Sdes} hdr_t;
33663012Sdes
33763012Sdes/* Names of interesting headers */
33863012Sdesstatic struct {
33990267Sdes	hdr_t		 num;
34090267Sdes	const char	*name;
34163012Sdes} hdr_names[] = {
34290267Sdes	{ hdr_content_length,		"Content-Length" },
34390267Sdes	{ hdr_content_range,		"Content-Range" },
34490267Sdes	{ hdr_last_modified,		"Last-Modified" },
34590267Sdes	{ hdr_location,			"Location" },
34690267Sdes	{ hdr_transfer_encoding,	"Transfer-Encoding" },
34790267Sdes	{ hdr_www_authenticate,		"WWW-Authenticate" },
34890267Sdes	{ hdr_unknown,			NULL },
34963012Sdes};
35063012Sdes
35163012Sdes/*
35263012Sdes * Send a formatted line; optionally echo to terminal
35363012Sdes */
35463012Sdesstatic int
35597856Sdes_http_cmd(conn_t *conn, const char *fmt, ...)
35663012Sdes{
35790267Sdes	va_list ap;
35890267Sdes	size_t len;
35990267Sdes	char *msg;
36090267Sdes	int r;
36163012Sdes
36290267Sdes	va_start(ap, fmt);
36390267Sdes	len = vasprintf(&msg, fmt, ap);
36490267Sdes	va_end(ap);
36590267Sdes
36690267Sdes	if (msg == NULL) {
36790267Sdes		errno = ENOMEM;
36890267Sdes		_fetch_syserr();
36990267Sdes		return (-1);
37090267Sdes	}
37190267Sdes
37297856Sdes	r = _fetch_putln(conn, msg, len);
37390267Sdes	free(msg);
37490267Sdes
37590267Sdes	if (r == -1) {
37690267Sdes		_fetch_syserr();
37790267Sdes		return (-1);
37890267Sdes	}
37990267Sdes
38090267Sdes	return (0);
38163012Sdes}
38263012Sdes
38363012Sdes/*
38463012Sdes * Get and parse status line
38563012Sdes */
38663012Sdesstatic int
38797856Sdes_http_get_reply(conn_t *conn)
38863012Sdes{
38990267Sdes	char *p;
39090267Sdes
39197856Sdes	if (_fetch_getln(conn) == -1)
39290267Sdes		return (-1);
39390267Sdes	/*
39490267Sdes	 * A valid status line looks like "HTTP/m.n xyz reason" where m
39590267Sdes	 * and n are the major and minor protocol version numbers and xyz
39690267Sdes	 * is the reply code.
39790267Sdes	 * Unfortunately, there are servers out there (NCSA 1.5.1, to name
39890267Sdes	 * just one) that do not send a version number, so we can't rely
39990267Sdes	 * on finding one, but if we do, insist on it being 1.0 or 1.1.
40090267Sdes	 * We don't care about the reason phrase.
40190267Sdes	 */
40297856Sdes	if (strncmp(conn->buf, "HTTP", 4) != 0)
40390267Sdes		return (HTTP_PROTOCOL_ERROR);
40497856Sdes	p = conn->buf + 4;
40590267Sdes	if (*p == '/') {
40690267Sdes		if (p[1] != '1' || p[2] != '.' || (p[3] != '0' && p[3] != '1'))
40790267Sdes			return (HTTP_PROTOCOL_ERROR);
40890267Sdes		p += 4;
40990267Sdes	}
41090267Sdes	if (*p != ' ' || !isdigit(p[1]) || !isdigit(p[2]) || !isdigit(p[3]))
41190267Sdes		return (HTTP_PROTOCOL_ERROR);
41290267Sdes
41397856Sdes	conn->err = (p[1] - '0') * 100 + (p[2] - '0') * 10 + (p[3] - '0');
41497856Sdes	return (conn->err);
41537535Sdes}
41637535Sdes
41737608Sdes/*
41890267Sdes * Check a header; if the type matches the given string, return a pointer
41990267Sdes * to the beginning of the value.
42063012Sdes */
42175891Sarchiestatic const char *
42275891Sarchie_http_match(const char *str, const char *hdr)
42363012Sdes{
42490267Sdes	while (*str && *hdr && tolower(*str++) == tolower(*hdr++))
42590267Sdes		/* nothing */;
42690267Sdes	if (*str || *hdr != ':')
42790267Sdes		return (NULL);
42890267Sdes	while (*hdr && isspace(*++hdr))
42990267Sdes		/* nothing */;
43090267Sdes	return (hdr);
43163012Sdes}
43263012Sdes
43363012Sdes/*
43463012Sdes * Get the next header and return the appropriate symbolic code.
43563012Sdes */
43685093Sdesstatic hdr_t
43797856Sdes_http_next_header(conn_t *conn, const char **p)
43863012Sdes{
43990267Sdes	int i;
44090267Sdes
44197856Sdes	if (_fetch_getln(conn) == -1)
44290267Sdes		return (hdr_syserror);
44397856Sdes	while (conn->buflen && isspace(conn->buf[conn->buflen - 1]))
44497856Sdes		conn->buflen--;
44597856Sdes	conn->buf[conn->buflen] = '\0';
44697856Sdes	if (conn->buflen == 0)
44797856Sdes		return (hdr_end);
44890267Sdes	/*
44990267Sdes	 * We could check for malformed headers but we don't really care.
45090267Sdes	 * A valid header starts with a token immediately followed by a
45190267Sdes	 * colon; a token is any sequence of non-control, non-whitespace
45290267Sdes	 * characters except "()<>@,;:\\\"{}".
45390267Sdes	 */
45490267Sdes	for (i = 0; hdr_names[i].num != hdr_unknown; i++)
45597856Sdes		if ((*p = _http_match(hdr_names[i].name, conn->buf)) != NULL)
45690267Sdes			return (hdr_names[i].num);
45790267Sdes	return (hdr_unknown);
45863012Sdes}
45963012Sdes
46063012Sdes/*
46163012Sdes * Parse a last-modified header
46263012Sdes */
46363716Sdesstatic int
46475891Sarchie_http_parse_mtime(const char *p, time_t *mtime)
46563012Sdes{
46690267Sdes	char locale[64], *r;
46790267Sdes	struct tm tm;
46863012Sdes
46990267Sdes	strncpy(locale, setlocale(LC_TIME, NULL), sizeof locale);
47090267Sdes	setlocale(LC_TIME, "C");
47190267Sdes	r = strptime(p, "%a, %d %b %Y %H:%M:%S GMT", &tm);
47290267Sdes	/* XXX should add support for date-2 and date-3 */
47390267Sdes	setlocale(LC_TIME, locale);
47490267Sdes	if (r == NULL)
47590267Sdes		return (-1);
47690267Sdes	DEBUG(fprintf(stderr, "last modified: [%04d-%02d-%02d "
47788769Sdes		  "%02d:%02d:%02d]\n",
47863012Sdes		  tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
47963012Sdes		  tm.tm_hour, tm.tm_min, tm.tm_sec));
48090267Sdes	*mtime = timegm(&tm);
48190267Sdes	return (0);
48263012Sdes}
48363012Sdes
48463012Sdes/*
48563012Sdes * Parse a content-length header
48663012Sdes */
48763716Sdesstatic int
48875891Sarchie_http_parse_length(const char *p, off_t *length)
48963012Sdes{
49090267Sdes	off_t len;
49190267Sdes
49290267Sdes	for (len = 0; *p && isdigit(*p); ++p)
49390267Sdes		len = len * 10 + (*p - '0');
49490267Sdes	if (*p)
49590267Sdes		return (-1);
49690267Sdes	DEBUG(fprintf(stderr, "content length: [%lld]\n",
49790267Sdes	    (long long)len));
49890267Sdes	*length = len;
49990267Sdes	return (0);
50063012Sdes}
50163012Sdes
50263012Sdes/*
50363012Sdes * Parse a content-range header
50463012Sdes */
50563716Sdesstatic int
50675891Sarchie_http_parse_range(const char *p, off_t *offset, off_t *length, off_t *size)
50763012Sdes{
50890267Sdes	off_t first, last, len;
50963716Sdes
51090267Sdes	if (strncasecmp(p, "bytes ", 6) != 0)
51190267Sdes		return (-1);
51290267Sdes	for (first = 0, p += 6; *p && isdigit(*p); ++p)
51390267Sdes		first = first * 10 + *p - '0';
51490267Sdes	if (*p != '-')
51590267Sdes		return (-1);
51690267Sdes	for (last = 0, ++p; *p && isdigit(*p); ++p)
51790267Sdes		last = last * 10 + *p - '0';
51890267Sdes	if (first > last || *p != '/')
51990267Sdes		return (-1);
52090267Sdes	for (len = 0, ++p; *p && isdigit(*p); ++p)
52190267Sdes		len = len * 10 + *p - '0';
52290267Sdes	if (*p || len < last - first + 1)
52390267Sdes		return (-1);
52490267Sdes	DEBUG(fprintf(stderr, "content range: [%lld-%lld/%lld]\n",
52590267Sdes	    (long long)first, (long long)last, (long long)len));
52690267Sdes	*offset = first;
52790267Sdes	*length = last - first + 1;
52890267Sdes	*size = len;
52990267Sdes	return (0);
53063012Sdes}
53163012Sdes
53290267Sdes
53363012Sdes/*****************************************************************************
53463012Sdes * Helper functions for authorization
53563012Sdes */
53663012Sdes
53763012Sdes/*
53837608Sdes * Base64 encoding
53937608Sdes */
54062965Sdesstatic char *
54190267Sdes_http_base64(const char *src)
54237608Sdes{
54390267Sdes	static const char base64[] =
54490267Sdes	    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
54590267Sdes	    "abcdefghijklmnopqrstuvwxyz"
54690267Sdes	    "0123456789+/";
54790267Sdes	char *str, *dst;
54890267Sdes	size_t l;
54990267Sdes	int t, r;
55062965Sdes
55190267Sdes	l = strlen(src);
55290267Sdes	if ((str = malloc(((l + 2) / 3) * 4)) == NULL)
55390267Sdes		return (NULL);
55490267Sdes	dst = str;
55590267Sdes	r = 0;
55637608Sdes
55790267Sdes	while (l >= 3) {
55890267Sdes		t = (src[0] << 16) | (src[1] << 8) | src[2];
55990267Sdes		dst[0] = base64[(t >> 18) & 0x3f];
56090267Sdes		dst[1] = base64[(t >> 12) & 0x3f];
56190267Sdes		dst[2] = base64[(t >> 6) & 0x3f];
56290267Sdes		dst[3] = base64[(t >> 0) & 0x3f];
56390267Sdes		src += 3; l -= 3;
56490267Sdes		dst += 4; r += 4;
56590267Sdes	}
56637608Sdes
56790267Sdes	switch (l) {
56890267Sdes	case 2:
56990267Sdes		t = (src[0] << 16) | (src[1] << 8);
57090267Sdes		dst[0] = base64[(t >> 18) & 0x3f];
57190267Sdes		dst[1] = base64[(t >> 12) & 0x3f];
57290267Sdes		dst[2] = base64[(t >> 6) & 0x3f];
57390267Sdes		dst[3] = '=';
57490267Sdes		dst += 4;
57590267Sdes		r += 4;
57690267Sdes		break;
57790267Sdes	case 1:
57890267Sdes		t = src[0] << 16;
57990267Sdes		dst[0] = base64[(t >> 18) & 0x3f];
58090267Sdes		dst[1] = base64[(t >> 12) & 0x3f];
58190267Sdes		dst[2] = dst[3] = '=';
58290267Sdes		dst += 4;
58390267Sdes		r += 4;
58490267Sdes		break;
58590267Sdes	case 0:
58690267Sdes		break;
58790267Sdes	}
58890267Sdes
58990267Sdes	*dst = 0;
59090267Sdes	return (str);
59137608Sdes}
59237608Sdes
59337608Sdes/*
59437608Sdes * Encode username and password
59537608Sdes */
59662965Sdesstatic int
59797856Sdes_http_basic_auth(conn_t *conn, const char *hdr, const char *usr, const char *pwd)
59837608Sdes{
59990267Sdes	char *upw, *auth;
60090267Sdes	int r;
60137608Sdes
60290267Sdes	DEBUG(fprintf(stderr, "usr: [%s]\n", usr));
60390267Sdes	DEBUG(fprintf(stderr, "pwd: [%s]\n", pwd));
60490267Sdes	if (asprintf(&upw, "%s:%s", usr, pwd) == -1)
60590267Sdes		return (-1);
60690267Sdes	auth = _http_base64(upw);
60790267Sdes	free(upw);
60890267Sdes	if (auth == NULL)
60990267Sdes		return (-1);
61097856Sdes	r = _http_cmd(conn, "%s: Basic %s", hdr, auth);
61190267Sdes	free(auth);
61290267Sdes	return (r);
61362965Sdes}
61462965Sdes
61562965Sdes/*
61662965Sdes * Send an authorization header
61762965Sdes */
61862965Sdesstatic int
61997856Sdes_http_authorize(conn_t *conn, const char *hdr, const char *p)
62062965Sdes{
62190267Sdes	/* basic authorization */
62290267Sdes	if (strncasecmp(p, "basic:", 6) == 0) {
62390267Sdes		char *user, *pwd, *str;
62490267Sdes		int r;
62562965Sdes
62690267Sdes		/* skip realm */
62790267Sdes		for (p += 6; *p && *p != ':'; ++p)
62890267Sdes			/* nothing */ ;
62990267Sdes		if (!*p || strchr(++p, ':') == NULL)
63090267Sdes			return (-1);
63190267Sdes		if ((str = strdup(p)) == NULL)
63290267Sdes			return (-1); /* XXX */
63390267Sdes		user = str;
63490267Sdes		pwd = strchr(str, ':');
63590267Sdes		*pwd++ = '\0';
63697856Sdes		r = _http_basic_auth(conn, hdr, user, pwd);
63790267Sdes		free(str);
63890267Sdes		return (r);
63990267Sdes	}
64090267Sdes	return (-1);
64137608Sdes}
64237608Sdes
64390267Sdes
64463012Sdes/*****************************************************************************
64563012Sdes * Helper functions for connecting to a server or proxy
64663012Sdes */
64763012Sdes
64837608Sdes/*
64990267Sdes * Connect to the correct HTTP server or proxy.
65063012Sdes */
65197856Sdesstatic conn_t *
65275891Sarchie_http_connect(struct url *URL, struct url *purl, const char *flags)
65363012Sdes{
65497856Sdes	conn_t *conn;
65590267Sdes	int verbose;
65697856Sdes	int af;
65790267Sdes
65863012Sdes#ifdef INET6
65990267Sdes	af = AF_UNSPEC;
66060737Sume#else
66190267Sdes	af = AF_INET;
66260737Sume#endif
66390267Sdes
66490267Sdes	verbose = CHECK_FLAG('v');
66590267Sdes	if (CHECK_FLAG('4'))
66690267Sdes		af = AF_INET;
66767043Sdes#ifdef INET6
66890267Sdes	else if (CHECK_FLAG('6'))
66990267Sdes		af = AF_INET6;
67067043Sdes#endif
67167043Sdes
67297868Sdes	if (purl && strcasecmp(URL->scheme, SCHEME_HTTPS) != 0) {
67390267Sdes		URL = purl;
67490267Sdes	} else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0) {
67590267Sdes		/* can't talk http to an ftp server */
67690267Sdes		/* XXX should set an error code */
67797856Sdes		return (NULL);
67890267Sdes	}
67990267Sdes
68097856Sdes	if ((conn = _fetch_connect(URL->host, URL->port, af, verbose)) == NULL)
68190267Sdes		/* _fetch_connect() has already set an error code */
68297856Sdes		return (NULL);
68397868Sdes	if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0 &&
68497868Sdes	    _fetch_ssl(conn, verbose) == -1) {
68597868Sdes		_fetch_close(conn);
68697891Sdes		/* grrr */
68797891Sdes		errno = EAUTH;
68897891Sdes		_fetch_syserr();
68997868Sdes		return (NULL);
69097868Sdes	}
69197856Sdes	return (conn);
69267043Sdes}
69367043Sdes
69467043Sdesstatic struct url *
69575891Sarchie_http_get_proxy(void)
69667043Sdes{
69790267Sdes	struct url *purl;
69890267Sdes	char *p;
69990267Sdes
70090267Sdes	if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
70190267Sdes	    (purl = fetchParseURL(p))) {
70290267Sdes		if (!*purl->scheme)
70390267Sdes			strcpy(purl->scheme, SCHEME_HTTP);
70490267Sdes		if (!purl->port)
70590267Sdes			purl->port = _fetch_default_proxy_port(purl->scheme);
70690267Sdes		if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
70790267Sdes			return (purl);
70890267Sdes		fetchFreeURL(purl);
70990267Sdes	}
71090267Sdes	return (NULL);
71160376Sdes}
71260376Sdes
71388771Sdesstatic void
71488771Sdes_http_print_html(FILE *out, FILE *in)
71588771Sdes{
71690267Sdes	size_t len;
71790267Sdes	char *line, *p, *q;
71890267Sdes	int comment, tag;
71988771Sdes
72090267Sdes	comment = tag = 0;
72190267Sdes	while ((line = fgetln(in, &len)) != NULL) {
72290267Sdes		while (len && isspace(line[len - 1]))
72390267Sdes			--len;
72490267Sdes		for (p = q = line; q < line + len; ++q) {
72590267Sdes			if (comment && *q == '-') {
72690267Sdes				if (q + 2 < line + len &&
72790267Sdes				    strcmp(q, "-->") == 0) {
72890267Sdes					tag = comment = 0;
72990267Sdes					q += 2;
73090267Sdes				}
73190267Sdes			} else if (tag && !comment && *q == '>') {
73290267Sdes				p = q + 1;
73390267Sdes				tag = 0;
73490267Sdes			} else if (!tag && *q == '<') {
73590267Sdes				if (q > p)
73690267Sdes					fwrite(p, q - p, 1, out);
73790267Sdes				tag = 1;
73890267Sdes				if (q + 3 < line + len &&
73990267Sdes				    strcmp(q, "<!--") == 0) {
74090267Sdes					comment = 1;
74190267Sdes					q += 3;
74290267Sdes				}
74390267Sdes			}
74488771Sdes		}
74590267Sdes		if (!tag && q > p)
74690267Sdes			fwrite(p, q - p, 1, out);
74790267Sdes		fputc('\n', out);
74888771Sdes	}
74988771Sdes}
75088771Sdes
75190267Sdes
75263012Sdes/*****************************************************************************
75363012Sdes * Core
75460954Sdes */
75560954Sdes
75660954Sdes/*
75763012Sdes * Send a request and process the reply
75897866Sdes *
75997866Sdes * XXX This function is way too long, the do..while loop should be split
76097866Sdes * XXX off into a separate function.
76160376Sdes */
76267043SdesFILE *
76375891Sarchie_http_request(struct url *URL, const char *op, struct url_stat *us,
76490267Sdes    struct url *purl, const char *flags)
76560376Sdes{
76697856Sdes	conn_t *conn;
76790267Sdes	struct url *url, *new;
76890267Sdes	int chunked, direct, need_auth, noredirect, verbose;
76998422Sdes	int e, i, n;
77090267Sdes	off_t offset, clength, length, size;
77190267Sdes	time_t mtime;
77290267Sdes	const char *p;
77390267Sdes	FILE *f;
77490267Sdes	hdr_t h;
77590267Sdes	char *host;
77660737Sume#ifdef INET6
77790267Sdes	char hbuf[MAXHOSTNAMELEN + 1];
77860737Sume#endif
77963012Sdes
78090267Sdes	direct = CHECK_FLAG('d');
78190267Sdes	noredirect = CHECK_FLAG('A');
78290267Sdes	verbose = CHECK_FLAG('v');
78360737Sume
78490267Sdes	if (direct && purl) {
78590267Sdes		fetchFreeURL(purl);
78690267Sdes		purl = NULL;
78790267Sdes	}
78863716Sdes
78990267Sdes	/* try the provided URL first */
79090267Sdes	url = URL;
79163012Sdes
79290267Sdes	/* if the A flag is set, we only get one try */
79390267Sdes	n = noredirect ? 1 : MAX_REDIRECT;
79490267Sdes	i = 0;
79563012Sdes
79698422Sdes	e = HTTP_PROTOCOL_ERROR;
79790267Sdes	need_auth = 0;
79890267Sdes	do {
79990267Sdes		new = NULL;
80090267Sdes		chunked = 0;
80190267Sdes		offset = 0;
80290267Sdes		clength = -1;
80390267Sdes		length = -1;
80490267Sdes		size = -1;
80590267Sdes		mtime = 0;
80690267Sdes
80790267Sdes		/* check port */
80890267Sdes		if (!url->port)
80990267Sdes			url->port = _fetch_default_port(url->scheme);
81090267Sdes
81190267Sdes		/* were we redirected to an FTP URL? */
81290267Sdes		if (purl == NULL && strcmp(url->scheme, SCHEME_FTP) == 0) {
81390267Sdes			if (strcmp(op, "GET") == 0)
81490267Sdes				return (_ftp_request(url, "RETR", us, purl, flags));
81590267Sdes			else if (strcmp(op, "HEAD") == 0)
81690267Sdes				return (_ftp_request(url, "STAT", us, purl, flags));
81790267Sdes		}
81890267Sdes
81990267Sdes		/* connect to server or proxy */
82097856Sdes		if ((conn = _http_connect(url, purl, flags)) == NULL)
82190267Sdes			goto ouch;
82290267Sdes
82390267Sdes		host = url->host;
82460737Sume#ifdef INET6
82590267Sdes		if (strchr(url->host, ':')) {
82690267Sdes			snprintf(hbuf, sizeof(hbuf), "[%s]", url->host);
82790267Sdes			host = hbuf;
82890267Sdes		}
82960737Sume#endif
83037535Sdes
83190267Sdes		/* send request */
83290267Sdes		if (verbose)
83390267Sdes			_fetch_info("requesting %s://%s:%d%s",
83490267Sdes			    url->scheme, host, url->port, url->doc);
83590267Sdes		if (purl) {
83697856Sdes			_http_cmd(conn, "%s %s://%s:%d%s HTTP/1.1",
83790267Sdes			    op, url->scheme, host, url->port, url->doc);
83890267Sdes		} else {
83997856Sdes			_http_cmd(conn, "%s %s HTTP/1.1",
84090267Sdes			    op, url->doc);
84190267Sdes		}
84237535Sdes
84390267Sdes		/* virtual host */
84490267Sdes		if (url->port == _fetch_default_port(url->scheme))
84597856Sdes			_http_cmd(conn, "Host: %s", host);
84690267Sdes		else
84797856Sdes			_http_cmd(conn, "Host: %s:%d", host, url->port);
84890267Sdes
84990267Sdes		/* proxy authorization */
85090267Sdes		if (purl) {
85190267Sdes			if (*purl->user || *purl->pwd)
85297856Sdes				_http_basic_auth(conn, "Proxy-Authorization",
85390267Sdes				    purl->user, purl->pwd);
85490267Sdes			else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL && *p != '\0')
85597856Sdes				_http_authorize(conn, "Proxy-Authorization", p);
85690267Sdes		}
85790267Sdes
85890267Sdes		/* server authorization */
85990267Sdes		if (need_auth || *url->user || *url->pwd) {
86090267Sdes			if (*url->user || *url->pwd)
86197856Sdes				_http_basic_auth(conn, "Authorization", url->user, url->pwd);
86290267Sdes			else if ((p = getenv("HTTP_AUTH")) != NULL && *p != '\0')
86397856Sdes				_http_authorize(conn, "Authorization", p);
86490267Sdes			else if (fetchAuthMethod && fetchAuthMethod(url) == 0) {
86597856Sdes				_http_basic_auth(conn, "Authorization", url->user, url->pwd);
86690267Sdes			} else {
86790267Sdes				_http_seterr(HTTP_NEED_AUTH);
86890267Sdes				goto ouch;
86990267Sdes			}
87090267Sdes		}
87190267Sdes
87290267Sdes		/* other headers */
87390267Sdes		if ((p = getenv("HTTP_USER_AGENT")) != NULL && *p != '\0')
87497856Sdes			_http_cmd(conn, "User-Agent: %s", p);
87590267Sdes		else
87697856Sdes			_http_cmd(conn, "User-Agent: %s " _LIBFETCH_VER, getprogname());
87790267Sdes		if (url->offset)
87897856Sdes			_http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset);
87997856Sdes		_http_cmd(conn, "Connection: close");
88097856Sdes		_http_cmd(conn, "");
88190267Sdes
88290267Sdes		/* get reply */
88397856Sdes		switch (_http_get_reply(conn)) {
88490267Sdes		case HTTP_OK:
88590267Sdes		case HTTP_PARTIAL:
88690267Sdes			/* fine */
88790267Sdes			break;
88890267Sdes		case HTTP_MOVED_PERM:
88990267Sdes		case HTTP_MOVED_TEMP:
89090267Sdes		case HTTP_SEE_OTHER:
89190267Sdes			/*
89290267Sdes			 * Not so fine, but we still have to read the headers to
89390267Sdes			 * get the new location.
89490267Sdes			 */
89590267Sdes			break;
89690267Sdes		case HTTP_NEED_AUTH:
89790267Sdes			if (need_auth) {
89890267Sdes				/*
89990267Sdes				 * We already sent out authorization code, so there's
90090267Sdes				 * nothing more we can do.
90190267Sdes				 */
90297856Sdes				_http_seterr(conn->err);
90390267Sdes				goto ouch;
90490267Sdes			}
90590267Sdes			/* try again, but send the password this time */
90690267Sdes			if (verbose)
90790267Sdes				_fetch_info("server requires authorization");
90890267Sdes			break;
90990267Sdes		case HTTP_NEED_PROXY_AUTH:
91090267Sdes			/*
91190267Sdes			 * If we're talking to a proxy, we already sent our proxy
91290267Sdes			 * authorization code, so there's nothing more we can do.
91390267Sdes			 */
91497856Sdes			_http_seterr(conn->err);
91590267Sdes			goto ouch;
91690267Sdes		case HTTP_PROTOCOL_ERROR:
91790267Sdes			/* fall through */
91890267Sdes		case -1:
91990267Sdes			_fetch_syserr();
92090267Sdes			goto ouch;
92190267Sdes		default:
92297856Sdes			_http_seterr(conn->err);
92390267Sdes			if (!verbose)
92490267Sdes				goto ouch;
92590267Sdes			/* fall through so we can get the full error message */
92690267Sdes		}
92790267Sdes
92890267Sdes		/* get headers */
92990267Sdes		do {
93097856Sdes			switch ((h = _http_next_header(conn, &p))) {
93190267Sdes			case hdr_syserror:
93290267Sdes				_fetch_syserr();
93390267Sdes				goto ouch;
93490267Sdes			case hdr_error:
93590267Sdes				_http_seterr(HTTP_PROTOCOL_ERROR);
93690267Sdes				goto ouch;
93790267Sdes			case hdr_content_length:
93890267Sdes				_http_parse_length(p, &clength);
93990267Sdes				break;
94090267Sdes			case hdr_content_range:
94190267Sdes				_http_parse_range(p, &offset, &length, &size);
94290267Sdes				break;
94390267Sdes			case hdr_last_modified:
94490267Sdes				_http_parse_mtime(p, &mtime);
94590267Sdes				break;
94690267Sdes			case hdr_location:
94797856Sdes				if (!HTTP_REDIRECT(conn->err))
94890267Sdes					break;
94990267Sdes				if (new)
95090267Sdes					free(new);
95190267Sdes				if (verbose)
95297856Sdes					_fetch_info("%d redirect to %s", conn->err, p);
95390267Sdes				if (*p == '/')
95490267Sdes					/* absolute path */
95590267Sdes					new = fetchMakeURL(url->scheme, url->host, url->port, p,
95690267Sdes					    url->user, url->pwd);
95790267Sdes				else
95890267Sdes					new = fetchParseURL(p);
95990267Sdes				if (new == NULL) {
96090267Sdes					/* XXX should set an error code */
96190267Sdes					DEBUG(fprintf(stderr, "failed to parse new URL\n"));
96290267Sdes					goto ouch;
96390267Sdes				}
96490267Sdes				if (!*new->user && !*new->pwd) {
96590267Sdes					strcpy(new->user, url->user);
96690267Sdes					strcpy(new->pwd, url->pwd);
96790267Sdes				}
96890267Sdes				new->offset = url->offset;
96990267Sdes				new->length = url->length;
97090267Sdes				break;
97190267Sdes			case hdr_transfer_encoding:
97290267Sdes				/* XXX weak test*/
97390267Sdes				chunked = (strcasecmp(p, "chunked") == 0);
97490267Sdes				break;
97590267Sdes			case hdr_www_authenticate:
97697856Sdes				if (conn->err != HTTP_NEED_AUTH)
97790267Sdes					break;
97890267Sdes				/* if we were smarter, we'd check the method and realm */
97990267Sdes				break;
98090267Sdes			case hdr_end:
98190267Sdes				/* fall through */
98290267Sdes			case hdr_unknown:
98390267Sdes				/* ignore */
98490267Sdes				break;
98590267Sdes			}
98690267Sdes		} while (h > hdr_end);
98790267Sdes
98890267Sdes		/* we need to provide authentication */
98997856Sdes		if (conn->err == HTTP_NEED_AUTH) {
99098422Sdes			e = conn->err;
99190267Sdes			need_auth = 1;
99297856Sdes			_fetch_close(conn);
99397856Sdes			conn = NULL;
99490267Sdes			continue;
99590267Sdes		}
99690267Sdes
997104404Sru		/* we have a hit or an error */
998104404Sru		if (conn->err == HTTP_OK || conn->err == HTTP_PARTIAL || HTTP_ERROR(conn->err))
999104404Sru			break;
1000104404Sru
100190267Sdes		/* all other cases: we got a redirect */
100298422Sdes		e = conn->err;
100390267Sdes		need_auth = 0;
100497856Sdes		_fetch_close(conn);
100597856Sdes		conn = NULL;
100690267Sdes		if (!new) {
100790267Sdes			DEBUG(fprintf(stderr, "redirect with no new location\n"));
100890267Sdes			break;
100990267Sdes		}
101090267Sdes		if (url != URL)
101190267Sdes			fetchFreeURL(url);
101290267Sdes		url = new;
101390267Sdes	} while (++i < n);
101490267Sdes
101590267Sdes	/* we failed, or ran out of retries */
101697856Sdes	if (conn == NULL) {
101798422Sdes		_http_seterr(e);
101863012Sdes		goto ouch;
101963012Sdes	}
102060376Sdes
102190267Sdes	DEBUG(fprintf(stderr, "offset %lld, length %lld,"
102290267Sdes		  " size %lld, clength %lld\n",
102390267Sdes		  (long long)offset, (long long)length,
102490267Sdes		  (long long)size, (long long)clength));
102560376Sdes
102690267Sdes	/* check for inconsistencies */
102790267Sdes	if (clength != -1 && length != -1 && clength != length) {
102890267Sdes		_http_seterr(HTTP_PROTOCOL_ERROR);
102963012Sdes		goto ouch;
103063012Sdes	}
103190267Sdes	if (clength == -1)
103290267Sdes		clength = length;
103390267Sdes	if (clength != -1)
103490267Sdes		length = offset + clength;
103590267Sdes	if (length != -1 && size != -1 && length != size) {
103663012Sdes		_http_seterr(HTTP_PROTOCOL_ERROR);
103763012Sdes		goto ouch;
103890267Sdes	}
103990267Sdes	if (size == -1)
104090267Sdes		size = length;
104160376Sdes
104290267Sdes	/* fill in stats */
104390267Sdes	if (us) {
104490267Sdes		us->size = size;
104590267Sdes		us->atime = us->mtime = mtime;
104690267Sdes	}
104763069Sdes
104890267Sdes	/* too far? */
104990267Sdes	if (offset > URL->offset) {
105090267Sdes		_http_seterr(HTTP_PROTOCOL_ERROR);
105190267Sdes		goto ouch;
105277238Sdes	}
105360376Sdes
105490267Sdes	/* report back real offset and size */
105590267Sdes	URL->offset = offset;
105690267Sdes	URL->length = clength;
105737535Sdes
105890267Sdes	/* wrap it up in a FILE */
105997866Sdes	if ((f = _http_funopen(conn, chunked)) == NULL) {
106090267Sdes		_fetch_syserr();
106190267Sdes		goto ouch;
106290267Sdes	}
106363716Sdes
106490267Sdes	if (url != URL)
106590267Sdes		fetchFreeURL(url);
106690267Sdes	if (purl)
106790267Sdes		fetchFreeURL(purl);
106863567Sdes
106997856Sdes	if (HTTP_ERROR(conn->err)) {
107090267Sdes		_http_print_html(stderr, f);
107190267Sdes		fclose(f);
107290267Sdes		f = NULL;
107390267Sdes	}
107463012Sdes
107590267Sdes	return (f);
107688771Sdes
107790267Sdesouch:
107890267Sdes	if (url != URL)
107990267Sdes		fetchFreeURL(url);
108090267Sdes	if (purl)
108190267Sdes		fetchFreeURL(purl);
108297856Sdes	if (conn != NULL)
108397856Sdes		_fetch_close(conn);
108490267Sdes	return (NULL);
108563012Sdes}
108660189Sdes
108790267Sdes
108863012Sdes/*****************************************************************************
108963012Sdes * Entry points
109063012Sdes */
109163012Sdes
109263012Sdes/*
109363340Sdes * Retrieve and stat a file by HTTP
109463340Sdes */
109563340SdesFILE *
109675891SarchiefetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags)
109763340Sdes{
109890267Sdes	return (_http_request(URL, "GET", us, _http_get_proxy(), flags));
109963340Sdes}
110063340Sdes
110163340Sdes/*
110263012Sdes * Retrieve a file by HTTP
110363012Sdes */
110463012SdesFILE *
110575891SarchiefetchGetHTTP(struct url *URL, const char *flags)
110663012Sdes{
110790267Sdes	return (fetchXGetHTTP(URL, NULL, flags));
110837535Sdes}
110937535Sdes
111063340Sdes/*
111163340Sdes * Store a file by HTTP
111263340Sdes */
111337535SdesFILE *
111485093SdesfetchPutHTTP(struct url *URL __unused, const char *flags __unused)
111537535Sdes{
111690267Sdes	warnx("fetchPutHTTP(): not implemented");
111790267Sdes	return (NULL);
111837535Sdes}
111940975Sdes
112040975Sdes/*
112140975Sdes * Get an HTTP document's metadata
112240975Sdes */
112340975Sdesint
112475891SarchiefetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags)
112540975Sdes{
112690267Sdes	FILE *f;
112790267Sdes
112890267Sdes	if ((f = _http_request(URL, "HEAD", us, _http_get_proxy(), flags)) == NULL)
112990267Sdes		return (-1);
113090267Sdes	fclose(f);
113190267Sdes	return (0);
113240975Sdes}
113341989Sdes
113441989Sdes/*
113541989Sdes * List a directory
113641989Sdes */
113741989Sdesstruct url_ent *
113885093SdesfetchListHTTP(struct url *url __unused, const char *flags __unused)
113941989Sdes{
114090267Sdes	warnx("fetchListHTTP(): not implemented");
114190267Sdes	return (NULL);
114241989Sdes}
1143