http.c revision 174752
137535Sdes/*-
2135546Sdes * Copyright (c) 2000-2004 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 174752 2007-12-18 11:03:07Z 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
79141958Skbyanc#include <netinet/in.h>
80141958Skbyanc#include <netinet/tcp.h>
81141958Skbyanc
8237535Sdes#include "fetch.h"
8340939Sdes#include "common.h"
8441862Sdes#include "httperr.h"
8537535Sdes
8663012Sdes/* Maximum number of redirects to follow */
8763012Sdes#define MAX_REDIRECT 5
8837535Sdes
8963012Sdes/* Symbolic names for reply codes we care about */
9063012Sdes#define HTTP_OK			200
9163012Sdes#define HTTP_PARTIAL		206
9263012Sdes#define HTTP_MOVED_PERM		301
9363012Sdes#define HTTP_MOVED_TEMP		302
9463012Sdes#define HTTP_SEE_OTHER		303
95169386Sdes#define HTTP_TEMP_REDIRECT	307
9663012Sdes#define HTTP_NEED_AUTH		401
9787317Sdes#define HTTP_NEED_PROXY_AUTH	407
98125696Sdes#define HTTP_BAD_RANGE		416
9963012Sdes#define HTTP_PROTOCOL_ERROR	999
10060196Sdes
10163012Sdes#define HTTP_REDIRECT(xyz) ((xyz) == HTTP_MOVED_PERM \
10290267Sdes			    || (xyz) == HTTP_MOVED_TEMP \
103169386Sdes			    || (xyz) == HTTP_TEMP_REDIRECT \
10490267Sdes			    || (xyz) == HTTP_SEE_OTHER)
10563012Sdes
10688771Sdes#define HTTP_ERROR(xyz) ((xyz) > 400 && (xyz) < 599)
10763012Sdes
10890267Sdes
10963012Sdes/*****************************************************************************
11063012Sdes * I/O functions for decoding chunked streams
11163012Sdes */
11263012Sdes
11397859Sdesstruct httpio
11437535Sdes{
11597858Sdes	conn_t		*conn;		/* connection */
11697866Sdes	int		 chunked;	/* chunked mode */
11797858Sdes	char		*buf;		/* chunk buffer */
11897866Sdes	size_t		 bufsize;	/* size of chunk buffer */
11997866Sdes	ssize_t		 buflen;	/* amount of data currently in buffer */
12097866Sdes	int		 bufpos;	/* current read offset in buffer */
12197858Sdes	int		 eof;		/* end-of-file flag */
12297858Sdes	int		 error;		/* error flag */
12397858Sdes	size_t		 chunksize;	/* remaining size of current chunk */
12463281Sdes#ifndef NDEBUG
12590267Sdes	size_t		 total;
12663012Sdes#endif
12737535Sdes};
12837535Sdes
12937608Sdes/*
13063012Sdes * Get next chunk header
13137608Sdes */
13237608Sdesstatic int
133174588Sdeshttp_new_chunk(struct httpio *io)
13437608Sdes{
13590267Sdes	char *p;
13690267Sdes
137174588Sdes	if (fetch_getln(io->conn) == -1)
13890267Sdes		return (-1);
13990267Sdes
140174751Sdes	if (io->conn->buflen < 2 || !isxdigit((int)*io->conn->buf))
14190267Sdes		return (-1);
14290267Sdes
143174751Sdes	for (p = io->conn->buf; *p && !isspace((int)*p); ++p) {
14490267Sdes		if (*p == ';')
14590267Sdes			break;
146174751Sdes		if (!isxdigit((int)*p))
14790267Sdes			return (-1);
148174751Sdes		if (isdigit((int)*p)) {
14997859Sdes			io->chunksize = io->chunksize * 16 +
15090267Sdes			    *p - '0';
15190267Sdes		} else {
15297859Sdes			io->chunksize = io->chunksize * 16 +
15390267Sdes			    10 + tolower(*p) - 'a';
15490267Sdes		}
15590267Sdes	}
15690267Sdes
15763281Sdes#ifndef NDEBUG
15890267Sdes	if (fetchDebug) {
15997859Sdes		io->total += io->chunksize;
16097859Sdes		if (io->chunksize == 0)
161106207Sdes			fprintf(stderr, "%s(): end of last chunk\n", __func__);
16290267Sdes		else
163106207Sdes			fprintf(stderr, "%s(): new chunk: %lu (%lu)\n",
164106207Sdes			    __func__, (unsigned long)io->chunksize,
165106207Sdes			    (unsigned long)io->total);
16690267Sdes	}
16763012Sdes#endif
16890267Sdes
16997859Sdes	return (io->chunksize);
17037608Sdes}
17137608Sdes
17237608Sdes/*
17397866Sdes * Grow the input buffer to at least len bytes
17497866Sdes */
17597866Sdesstatic inline int
176174588Sdeshttp_growbuf(struct httpio *io, size_t len)
17797866Sdes{
17897866Sdes	char *tmp;
17997866Sdes
18097866Sdes	if (io->bufsize >= len)
18197866Sdes		return (0);
18297866Sdes
18397866Sdes	if ((tmp = realloc(io->buf, len)) == NULL)
18497866Sdes		return (-1);
18597866Sdes	io->buf = tmp;
18697866Sdes	io->bufsize = len;
187106044Sdes	return (0);
18897866Sdes}
18997866Sdes
19097866Sdes/*
19137608Sdes * Fill the input buffer, do chunk decoding on the fly
19237608Sdes */
19363012Sdesstatic int
194174588Sdeshttp_fillbuf(struct httpio *io, size_t len)
19537535Sdes{
19697859Sdes	if (io->error)
19790267Sdes		return (-1);
19897859Sdes	if (io->eof)
19990267Sdes		return (0);
20090267Sdes
20197866Sdes	if (io->chunked == 0) {
202174588Sdes		if (http_growbuf(io, len) == -1)
20397866Sdes			return (-1);
204174588Sdes		if ((io->buflen = fetch_read(io->conn, io->buf, len)) == -1) {
205106185Sdes			io->error = 1;
20697866Sdes			return (-1);
207106185Sdes		}
20897866Sdes		io->bufpos = 0;
20997866Sdes		return (io->buflen);
21097866Sdes	}
21197866Sdes
21297859Sdes	if (io->chunksize == 0) {
213174588Sdes		switch (http_new_chunk(io)) {
21490267Sdes		case -1:
21597859Sdes			io->error = 1;
21690267Sdes			return (-1);
21790267Sdes		case 0:
21897859Sdes			io->eof = 1;
21990267Sdes			return (0);
22090267Sdes		}
22137535Sdes	}
22263012Sdes
22397866Sdes	if (len > io->chunksize)
22497866Sdes		len = io->chunksize;
225174588Sdes	if (http_growbuf(io, len) == -1)
22690267Sdes		return (-1);
227174588Sdes	if ((io->buflen = fetch_read(io->conn, io->buf, len)) == -1) {
228106185Sdes		io->error = 1;
22997866Sdes		return (-1);
230106185Sdes	}
23197866Sdes	io->chunksize -= io->buflen;
23290267Sdes
23397859Sdes	if (io->chunksize == 0) {
23497856Sdes		char endl[2];
23597856Sdes
236174588Sdes		if (fetch_read(io->conn, endl, 2) != 2 ||
23797856Sdes		    endl[0] != '\r' || endl[1] != '\n')
23890267Sdes			return (-1);
23990267Sdes	}
24090267Sdes
24197866Sdes	io->bufpos = 0;
24290267Sdes
24397866Sdes	return (io->buflen);
24437535Sdes}
24537535Sdes
24637608Sdes/*
24737608Sdes * Read function
24837608Sdes */
24937535Sdesstatic int
250174588Sdeshttp_readfn(void *v, char *buf, int len)
25137535Sdes{
25297859Sdes	struct httpio *io = (struct httpio *)v;
25390267Sdes	int l, pos;
25463012Sdes
25597859Sdes	if (io->error)
25690267Sdes		return (-1);
25797859Sdes	if (io->eof)
25890267Sdes		return (0);
25963012Sdes
26090267Sdes	for (pos = 0; len > 0; pos += l, len -= l) {
26190267Sdes		/* empty buffer */
26297866Sdes		if (!io->buf || io->bufpos == io->buflen)
263174588Sdes			if (http_fillbuf(io, len) < 1)
26490267Sdes				break;
26597866Sdes		l = io->buflen - io->bufpos;
26690267Sdes		if (len < l)
26790267Sdes			l = len;
26897866Sdes		bcopy(io->buf + io->bufpos, buf + pos, l);
26997866Sdes		io->bufpos += l;
27090267Sdes	}
27137535Sdes
27297859Sdes	if (!pos && io->error)
27390267Sdes		return (-1);
27490267Sdes	return (pos);
27537535Sdes}
27637535Sdes
27737608Sdes/*
27837608Sdes * Write function
27937608Sdes */
28037535Sdesstatic int
281174588Sdeshttp_writefn(void *v, const char *buf, int len)
28237535Sdes{
28397859Sdes	struct httpio *io = (struct httpio *)v;
28490267Sdes
285174588Sdes	return (fetch_write(io->conn, buf, len));
28637535Sdes}
28737535Sdes
28837608Sdes/*
28937608Sdes * Close function
29037608Sdes */
29137535Sdesstatic int
292174588Sdeshttp_closefn(void *v)
29337535Sdes{
29497859Sdes	struct httpio *io = (struct httpio *)v;
29590267Sdes	int r;
29663012Sdes
297174588Sdes	r = fetch_close(io->conn);
29897859Sdes	if (io->buf)
29997859Sdes		free(io->buf);
30097859Sdes	free(io);
30190267Sdes	return (r);
30237535Sdes}
30337535Sdes
30437608Sdes/*
30563012Sdes * Wrap a file descriptor up
30637608Sdes */
30763012Sdesstatic FILE *
308174588Sdeshttp_funopen(conn_t *conn, int chunked)
30937535Sdes{
31097859Sdes	struct httpio *io;
31190267Sdes	FILE *f;
31263012Sdes
313109967Sdes	if ((io = calloc(1, sizeof(*io))) == NULL) {
314174588Sdes		fetch_syserr();
31590267Sdes		return (NULL);
31690267Sdes	}
31797859Sdes	io->conn = conn;
31897866Sdes	io->chunked = chunked;
319174588Sdes	f = funopen(io, http_readfn, http_writefn, NULL, http_closefn);
32090267Sdes	if (f == NULL) {
321174588Sdes		fetch_syserr();
32297859Sdes		free(io);
32390267Sdes		return (NULL);
32490267Sdes	}
32590267Sdes	return (f);
32663012Sdes}
32763012Sdes
32890267Sdes
32963012Sdes/*****************************************************************************
33063012Sdes * Helper functions for talking to the server and parsing its replies
33163012Sdes */
33263012Sdes
33363012Sdes/* Header types */
33463012Sdestypedef enum {
33590267Sdes	hdr_syserror = -2,
33690267Sdes	hdr_error = -1,
33790267Sdes	hdr_end = 0,
33890267Sdes	hdr_unknown = 1,
33990267Sdes	hdr_content_length,
34090267Sdes	hdr_content_range,
34190267Sdes	hdr_last_modified,
34290267Sdes	hdr_location,
34390267Sdes	hdr_transfer_encoding,
34490267Sdes	hdr_www_authenticate
34585093Sdes} hdr_t;
34663012Sdes
34763012Sdes/* Names of interesting headers */
34863012Sdesstatic struct {
34990267Sdes	hdr_t		 num;
35090267Sdes	const char	*name;
35163012Sdes} hdr_names[] = {
35290267Sdes	{ hdr_content_length,		"Content-Length" },
35390267Sdes	{ hdr_content_range,		"Content-Range" },
35490267Sdes	{ hdr_last_modified,		"Last-Modified" },
35590267Sdes	{ hdr_location,			"Location" },
35690267Sdes	{ hdr_transfer_encoding,	"Transfer-Encoding" },
35790267Sdes	{ hdr_www_authenticate,		"WWW-Authenticate" },
35890267Sdes	{ hdr_unknown,			NULL },
35963012Sdes};
36063012Sdes
36163012Sdes/*
36263012Sdes * Send a formatted line; optionally echo to terminal
36363012Sdes */
36463012Sdesstatic int
365174588Sdeshttp_cmd(conn_t *conn, const char *fmt, ...)
36663012Sdes{
36790267Sdes	va_list ap;
36890267Sdes	size_t len;
36990267Sdes	char *msg;
37090267Sdes	int r;
37163012Sdes
37290267Sdes	va_start(ap, fmt);
37390267Sdes	len = vasprintf(&msg, fmt, ap);
37490267Sdes	va_end(ap);
37590267Sdes
37690267Sdes	if (msg == NULL) {
37790267Sdes		errno = ENOMEM;
378174588Sdes		fetch_syserr();
37990267Sdes		return (-1);
38090267Sdes	}
38190267Sdes
382174588Sdes	r = fetch_putln(conn, msg, len);
38390267Sdes	free(msg);
38490267Sdes
38590267Sdes	if (r == -1) {
386174588Sdes		fetch_syserr();
38790267Sdes		return (-1);
38890267Sdes	}
38990267Sdes
39090267Sdes	return (0);
39163012Sdes}
39263012Sdes
39363012Sdes/*
39463012Sdes * Get and parse status line
39563012Sdes */
39663012Sdesstatic int
397174588Sdeshttp_get_reply(conn_t *conn)
39863012Sdes{
39990267Sdes	char *p;
40090267Sdes
401174588Sdes	if (fetch_getln(conn) == -1)
40290267Sdes		return (-1);
40390267Sdes	/*
40490267Sdes	 * A valid status line looks like "HTTP/m.n xyz reason" where m
40590267Sdes	 * and n are the major and minor protocol version numbers and xyz
40690267Sdes	 * is the reply code.
40790267Sdes	 * Unfortunately, there are servers out there (NCSA 1.5.1, to name
40890267Sdes	 * just one) that do not send a version number, so we can't rely
40990267Sdes	 * on finding one, but if we do, insist on it being 1.0 or 1.1.
41090267Sdes	 * We don't care about the reason phrase.
41190267Sdes	 */
41297856Sdes	if (strncmp(conn->buf, "HTTP", 4) != 0)
41390267Sdes		return (HTTP_PROTOCOL_ERROR);
41497856Sdes	p = conn->buf + 4;
41590267Sdes	if (*p == '/') {
41690267Sdes		if (p[1] != '1' || p[2] != '.' || (p[3] != '0' && p[3] != '1'))
41790267Sdes			return (HTTP_PROTOCOL_ERROR);
41890267Sdes		p += 4;
41990267Sdes	}
420174751Sdes	if (*p != ' ' || !isdigit((int)p[1]) ||
421174751Sdes	    !isdigit((int)p[2]) || !isdigit((int)p[3]))
42290267Sdes		return (HTTP_PROTOCOL_ERROR);
42390267Sdes
42497856Sdes	conn->err = (p[1] - '0') * 100 + (p[2] - '0') * 10 + (p[3] - '0');
42597856Sdes	return (conn->err);
42637535Sdes}
42737535Sdes
42837608Sdes/*
42990267Sdes * Check a header; if the type matches the given string, return a pointer
43090267Sdes * to the beginning of the value.
43163012Sdes */
43275891Sarchiestatic const char *
433174588Sdeshttp_match(const char *str, const char *hdr)
43463012Sdes{
43590267Sdes	while (*str && *hdr && tolower(*str++) == tolower(*hdr++))
43690267Sdes		/* nothing */;
43790267Sdes	if (*str || *hdr != ':')
43890267Sdes		return (NULL);
439174751Sdes	while (*hdr && isspace((int)*++hdr))
44090267Sdes		/* nothing */;
44190267Sdes	return (hdr);
44263012Sdes}
44363012Sdes
44463012Sdes/*
44563012Sdes * Get the next header and return the appropriate symbolic code.
44663012Sdes */
44785093Sdesstatic hdr_t
448174588Sdeshttp_next_header(conn_t *conn, const char **p)
44963012Sdes{
45090267Sdes	int i;
45190267Sdes
452174588Sdes	if (fetch_getln(conn) == -1)
45390267Sdes		return (hdr_syserror);
454174751Sdes	while (conn->buflen && isspace((int)conn->buf[conn->buflen - 1]))
45597856Sdes		conn->buflen--;
45697856Sdes	conn->buf[conn->buflen] = '\0';
45797856Sdes	if (conn->buflen == 0)
45897856Sdes		return (hdr_end);
45990267Sdes	/*
46090267Sdes	 * We could check for malformed headers but we don't really care.
46190267Sdes	 * A valid header starts with a token immediately followed by a
46290267Sdes	 * colon; a token is any sequence of non-control, non-whitespace
46390267Sdes	 * characters except "()<>@,;:\\\"{}".
46490267Sdes	 */
46590267Sdes	for (i = 0; hdr_names[i].num != hdr_unknown; i++)
466174588Sdes		if ((*p = http_match(hdr_names[i].name, conn->buf)) != NULL)
46790267Sdes			return (hdr_names[i].num);
46890267Sdes	return (hdr_unknown);
46963012Sdes}
47063012Sdes
47163012Sdes/*
47263012Sdes * Parse a last-modified header
47363012Sdes */
47463716Sdesstatic int
475174588Sdeshttp_parse_mtime(const char *p, time_t *mtime)
47663012Sdes{
47790267Sdes	char locale[64], *r;
47890267Sdes	struct tm tm;
47963012Sdes
480109967Sdes	strncpy(locale, setlocale(LC_TIME, NULL), sizeof(locale));
48190267Sdes	setlocale(LC_TIME, "C");
48290267Sdes	r = strptime(p, "%a, %d %b %Y %H:%M:%S GMT", &tm);
48390267Sdes	/* XXX should add support for date-2 and date-3 */
48490267Sdes	setlocale(LC_TIME, locale);
48590267Sdes	if (r == NULL)
48690267Sdes		return (-1);
48790267Sdes	DEBUG(fprintf(stderr, "last modified: [%04d-%02d-%02d "
48888769Sdes		  "%02d:%02d:%02d]\n",
48963012Sdes		  tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
49063012Sdes		  tm.tm_hour, tm.tm_min, tm.tm_sec));
49190267Sdes	*mtime = timegm(&tm);
49290267Sdes	return (0);
49363012Sdes}
49463012Sdes
49563012Sdes/*
49663012Sdes * Parse a content-length header
49763012Sdes */
49863716Sdesstatic int
499174588Sdeshttp_parse_length(const char *p, off_t *length)
50063012Sdes{
50190267Sdes	off_t len;
50290267Sdes
503174751Sdes	for (len = 0; *p && isdigit((int)*p); ++p)
50490267Sdes		len = len * 10 + (*p - '0');
50590267Sdes	if (*p)
50690267Sdes		return (-1);
50790267Sdes	DEBUG(fprintf(stderr, "content length: [%lld]\n",
50890267Sdes	    (long long)len));
50990267Sdes	*length = len;
51090267Sdes	return (0);
51163012Sdes}
51263012Sdes
51363012Sdes/*
51463012Sdes * Parse a content-range header
51563012Sdes */
51663716Sdesstatic int
517174588Sdeshttp_parse_range(const char *p, off_t *offset, off_t *length, off_t *size)
51863012Sdes{
51990267Sdes	off_t first, last, len;
52063716Sdes
52190267Sdes	if (strncasecmp(p, "bytes ", 6) != 0)
52290267Sdes		return (-1);
523125696Sdes	p += 6;
524125696Sdes	if (*p == '*') {
525125696Sdes		first = last = -1;
526125696Sdes		++p;
527125696Sdes	} else {
528174751Sdes		for (first = 0; *p && isdigit((int)*p); ++p)
529125696Sdes			first = first * 10 + *p - '0';
530125696Sdes		if (*p != '-')
531125696Sdes			return (-1);
532174751Sdes		for (last = 0, ++p; *p && isdigit((int)*p); ++p)
533125696Sdes			last = last * 10 + *p - '0';
534125696Sdes	}
53590267Sdes	if (first > last || *p != '/')
53690267Sdes		return (-1);
537174751Sdes	for (len = 0, ++p; *p && isdigit((int)*p); ++p)
53890267Sdes		len = len * 10 + *p - '0';
53990267Sdes	if (*p || len < last - first + 1)
54090267Sdes		return (-1);
541125696Sdes	if (first == -1) {
542125696Sdes		DEBUG(fprintf(stderr, "content range: [*/%lld]\n",
543125696Sdes		    (long long)len));
544125696Sdes		*length = 0;
545125696Sdes	} else {
546125696Sdes		DEBUG(fprintf(stderr, "content range: [%lld-%lld/%lld]\n",
547125696Sdes		    (long long)first, (long long)last, (long long)len));
548125696Sdes		*length = last - first + 1;
549125696Sdes	}
55090267Sdes	*offset = first;
55190267Sdes	*size = len;
55290267Sdes	return (0);
55363012Sdes}
55463012Sdes
55590267Sdes
55663012Sdes/*****************************************************************************
55763012Sdes * Helper functions for authorization
55863012Sdes */
55963012Sdes
56063012Sdes/*
56137608Sdes * Base64 encoding
56237608Sdes */
56362965Sdesstatic char *
564174588Sdeshttp_base64(const char *src)
56537608Sdes{
56690267Sdes	static const char base64[] =
56790267Sdes	    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
56890267Sdes	    "abcdefghijklmnopqrstuvwxyz"
56990267Sdes	    "0123456789+/";
57090267Sdes	char *str, *dst;
57190267Sdes	size_t l;
57290267Sdes	int t, r;
57362965Sdes
57490267Sdes	l = strlen(src);
575133280Sdes	if ((str = malloc(((l + 2) / 3) * 4 + 1)) == NULL)
57690267Sdes		return (NULL);
57790267Sdes	dst = str;
57890267Sdes	r = 0;
57937608Sdes
58090267Sdes	while (l >= 3) {
58190267Sdes		t = (src[0] << 16) | (src[1] << 8) | src[2];
58290267Sdes		dst[0] = base64[(t >> 18) & 0x3f];
58390267Sdes		dst[1] = base64[(t >> 12) & 0x3f];
58490267Sdes		dst[2] = base64[(t >> 6) & 0x3f];
58590267Sdes		dst[3] = base64[(t >> 0) & 0x3f];
58690267Sdes		src += 3; l -= 3;
58790267Sdes		dst += 4; r += 4;
58890267Sdes	}
58937608Sdes
59090267Sdes	switch (l) {
59190267Sdes	case 2:
59290267Sdes		t = (src[0] << 16) | (src[1] << 8);
59390267Sdes		dst[0] = base64[(t >> 18) & 0x3f];
59490267Sdes		dst[1] = base64[(t >> 12) & 0x3f];
59590267Sdes		dst[2] = base64[(t >> 6) & 0x3f];
59690267Sdes		dst[3] = '=';
59790267Sdes		dst += 4;
59890267Sdes		r += 4;
59990267Sdes		break;
60090267Sdes	case 1:
60190267Sdes		t = src[0] << 16;
60290267Sdes		dst[0] = base64[(t >> 18) & 0x3f];
60390267Sdes		dst[1] = base64[(t >> 12) & 0x3f];
60490267Sdes		dst[2] = dst[3] = '=';
60590267Sdes		dst += 4;
60690267Sdes		r += 4;
60790267Sdes		break;
60890267Sdes	case 0:
60990267Sdes		break;
61090267Sdes	}
61190267Sdes
61290267Sdes	*dst = 0;
61390267Sdes	return (str);
61437608Sdes}
61537608Sdes
61637608Sdes/*
61737608Sdes * Encode username and password
61837608Sdes */
61962965Sdesstatic int
620174588Sdeshttp_basic_auth(conn_t *conn, const char *hdr, const char *usr, const char *pwd)
62137608Sdes{
62290267Sdes	char *upw, *auth;
62390267Sdes	int r;
62437608Sdes
62590267Sdes	DEBUG(fprintf(stderr, "usr: [%s]\n", usr));
62690267Sdes	DEBUG(fprintf(stderr, "pwd: [%s]\n", pwd));
62790267Sdes	if (asprintf(&upw, "%s:%s", usr, pwd) == -1)
62890267Sdes		return (-1);
629174588Sdes	auth = http_base64(upw);
63090267Sdes	free(upw);
63190267Sdes	if (auth == NULL)
63290267Sdes		return (-1);
633174588Sdes	r = http_cmd(conn, "%s: Basic %s", hdr, auth);
63490267Sdes	free(auth);
63590267Sdes	return (r);
63662965Sdes}
63762965Sdes
63862965Sdes/*
63962965Sdes * Send an authorization header
64062965Sdes */
64162965Sdesstatic int
642174588Sdeshttp_authorize(conn_t *conn, const char *hdr, const char *p)
64362965Sdes{
64490267Sdes	/* basic authorization */
64590267Sdes	if (strncasecmp(p, "basic:", 6) == 0) {
64690267Sdes		char *user, *pwd, *str;
64790267Sdes		int r;
64862965Sdes
64990267Sdes		/* skip realm */
65090267Sdes		for (p += 6; *p && *p != ':'; ++p)
65190267Sdes			/* nothing */ ;
65290267Sdes		if (!*p || strchr(++p, ':') == NULL)
65390267Sdes			return (-1);
65490267Sdes		if ((str = strdup(p)) == NULL)
65590267Sdes			return (-1); /* XXX */
65690267Sdes		user = str;
65790267Sdes		pwd = strchr(str, ':');
65890267Sdes		*pwd++ = '\0';
659174588Sdes		r = http_basic_auth(conn, hdr, user, pwd);
66090267Sdes		free(str);
66190267Sdes		return (r);
66290267Sdes	}
66390267Sdes	return (-1);
66437608Sdes}
66537608Sdes
66690267Sdes
66763012Sdes/*****************************************************************************
66863012Sdes * Helper functions for connecting to a server or proxy
66963012Sdes */
67063012Sdes
67137608Sdes/*
67290267Sdes * Connect to the correct HTTP server or proxy.
67363012Sdes */
67497856Sdesstatic conn_t *
675174588Sdeshttp_connect(struct url *URL, struct url *purl, const char *flags)
67663012Sdes{
67797856Sdes	conn_t *conn;
67890267Sdes	int verbose;
679141958Skbyanc	int af, val;
68090267Sdes
68163012Sdes#ifdef INET6
68290267Sdes	af = AF_UNSPEC;
68360737Sume#else
68490267Sdes	af = AF_INET;
68560737Sume#endif
68690267Sdes
68790267Sdes	verbose = CHECK_FLAG('v');
68890267Sdes	if (CHECK_FLAG('4'))
68990267Sdes		af = AF_INET;
69067043Sdes#ifdef INET6
69190267Sdes	else if (CHECK_FLAG('6'))
69290267Sdes		af = AF_INET6;
69367043Sdes#endif
69467043Sdes
69597868Sdes	if (purl && strcasecmp(URL->scheme, SCHEME_HTTPS) != 0) {
69690267Sdes		URL = purl;
69790267Sdes	} else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0) {
69890267Sdes		/* can't talk http to an ftp server */
69990267Sdes		/* XXX should set an error code */
70097856Sdes		return (NULL);
70190267Sdes	}
70290267Sdes
703174588Sdes	if ((conn = fetch_connect(URL->host, URL->port, af, verbose)) == NULL)
704174588Sdes		/* fetch_connect() has already set an error code */
70597856Sdes		return (NULL);
70697868Sdes	if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0 &&
707174588Sdes	    fetch_ssl(conn, verbose) == -1) {
708174588Sdes		fetch_close(conn);
70997891Sdes		/* grrr */
71097891Sdes		errno = EAUTH;
711174588Sdes		fetch_syserr();
71297868Sdes		return (NULL);
71397868Sdes	}
714141958Skbyanc
715141958Skbyanc	val = 1;
716141958Skbyanc	setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val, sizeof(val));
717141958Skbyanc
71897856Sdes	return (conn);
71967043Sdes}
72067043Sdes
72167043Sdesstatic struct url *
722174752Sdeshttp_get_proxy(struct url * url, const char *flags)
72367043Sdes{
72490267Sdes	struct url *purl;
72590267Sdes	char *p;
72690267Sdes
727112797Sdes	if (flags != NULL && strchr(flags, 'd') != NULL)
728112081Sdes		return (NULL);
729174752Sdes	if (fetch_no_proxy_match(url->host))
730174752Sdes		return (NULL);
73190267Sdes	if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
732149414Sdes	    *p && (purl = fetchParseURL(p))) {
73390267Sdes		if (!*purl->scheme)
73490267Sdes			strcpy(purl->scheme, SCHEME_HTTP);
73590267Sdes		if (!purl->port)
736174588Sdes			purl->port = fetch_default_proxy_port(purl->scheme);
73790267Sdes		if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
73890267Sdes			return (purl);
73990267Sdes		fetchFreeURL(purl);
74090267Sdes	}
74190267Sdes	return (NULL);
74260376Sdes}
74360376Sdes
74488771Sdesstatic void
745174588Sdeshttp_print_html(FILE *out, FILE *in)
74688771Sdes{
74790267Sdes	size_t len;
74890267Sdes	char *line, *p, *q;
74990267Sdes	int comment, tag;
75088771Sdes
75190267Sdes	comment = tag = 0;
75290267Sdes	while ((line = fgetln(in, &len)) != NULL) {
753174751Sdes		while (len && isspace((int)line[len - 1]))
75490267Sdes			--len;
75590267Sdes		for (p = q = line; q < line + len; ++q) {
75690267Sdes			if (comment && *q == '-') {
75790267Sdes				if (q + 2 < line + len &&
75890267Sdes				    strcmp(q, "-->") == 0) {
75990267Sdes					tag = comment = 0;
76090267Sdes					q += 2;
76190267Sdes				}
76290267Sdes			} else if (tag && !comment && *q == '>') {
76390267Sdes				p = q + 1;
76490267Sdes				tag = 0;
76590267Sdes			} else if (!tag && *q == '<') {
76690267Sdes				if (q > p)
76790267Sdes					fwrite(p, q - p, 1, out);
76890267Sdes				tag = 1;
76990267Sdes				if (q + 3 < line + len &&
77090267Sdes				    strcmp(q, "<!--") == 0) {
77190267Sdes					comment = 1;
77290267Sdes					q += 3;
77390267Sdes				}
77490267Sdes			}
77588771Sdes		}
77690267Sdes		if (!tag && q > p)
77790267Sdes			fwrite(p, q - p, 1, out);
77890267Sdes		fputc('\n', out);
77988771Sdes	}
78088771Sdes}
78188771Sdes
78290267Sdes
78363012Sdes/*****************************************************************************
78463012Sdes * Core
78560954Sdes */
78660954Sdes
78760954Sdes/*
78863012Sdes * Send a request and process the reply
78997866Sdes *
79097866Sdes * XXX This function is way too long, the do..while loop should be split
79197866Sdes * XXX off into a separate function.
79260376Sdes */
79367043SdesFILE *
794174588Sdeshttp_request(struct url *URL, const char *op, struct url_stat *us,
79590267Sdes    struct url *purl, const char *flags)
79660376Sdes{
79797856Sdes	conn_t *conn;
79890267Sdes	struct url *url, *new;
79990267Sdes	int chunked, direct, need_auth, noredirect, verbose;
800143049Skbyanc	int e, i, n, val;
80190267Sdes	off_t offset, clength, length, size;
80290267Sdes	time_t mtime;
80390267Sdes	const char *p;
80490267Sdes	FILE *f;
80590267Sdes	hdr_t h;
806107372Sdes	char hbuf[MAXHOSTNAMELEN + 7], *host;
80763012Sdes
80890267Sdes	direct = CHECK_FLAG('d');
80990267Sdes	noredirect = CHECK_FLAG('A');
81090267Sdes	verbose = CHECK_FLAG('v');
81160737Sume
81290267Sdes	if (direct && purl) {
81390267Sdes		fetchFreeURL(purl);
81490267Sdes		purl = NULL;
81590267Sdes	}
81663716Sdes
81790267Sdes	/* try the provided URL first */
81890267Sdes	url = URL;
81963012Sdes
82090267Sdes	/* if the A flag is set, we only get one try */
82190267Sdes	n = noredirect ? 1 : MAX_REDIRECT;
82290267Sdes	i = 0;
82363012Sdes
82498422Sdes	e = HTTP_PROTOCOL_ERROR;
82590267Sdes	need_auth = 0;
82690267Sdes	do {
82790267Sdes		new = NULL;
82890267Sdes		chunked = 0;
82990267Sdes		offset = 0;
83090267Sdes		clength = -1;
83190267Sdes		length = -1;
83290267Sdes		size = -1;
83390267Sdes		mtime = 0;
83490267Sdes
83590267Sdes		/* check port */
83690267Sdes		if (!url->port)
837174588Sdes			url->port = fetch_default_port(url->scheme);
83890267Sdes
83990267Sdes		/* were we redirected to an FTP URL? */
84090267Sdes		if (purl == NULL && strcmp(url->scheme, SCHEME_FTP) == 0) {
84190267Sdes			if (strcmp(op, "GET") == 0)
842174588Sdes				return (ftp_request(url, "RETR", us, purl, flags));
84390267Sdes			else if (strcmp(op, "HEAD") == 0)
844174588Sdes				return (ftp_request(url, "STAT", us, purl, flags));
84590267Sdes		}
84690267Sdes
84790267Sdes		/* connect to server or proxy */
848174588Sdes		if ((conn = http_connect(url, purl, flags)) == NULL)
84990267Sdes			goto ouch;
85090267Sdes
85190267Sdes		host = url->host;
85260737Sume#ifdef INET6
85390267Sdes		if (strchr(url->host, ':')) {
85490267Sdes			snprintf(hbuf, sizeof(hbuf), "[%s]", url->host);
85590267Sdes			host = hbuf;
85690267Sdes		}
85760737Sume#endif
858174588Sdes		if (url->port != fetch_default_port(url->scheme)) {
859107372Sdes			if (host != hbuf) {
860107372Sdes				strcpy(hbuf, host);
861107372Sdes				host = hbuf;
862107372Sdes			}
863107372Sdes			snprintf(hbuf + strlen(hbuf),
864107372Sdes			    sizeof(hbuf) - strlen(hbuf), ":%d", url->port);
865107372Sdes		}
86637535Sdes
86790267Sdes		/* send request */
86890267Sdes		if (verbose)
869174588Sdes			fetch_info("requesting %s://%s%s",
870107372Sdes			    url->scheme, host, url->doc);
87190267Sdes		if (purl) {
872174588Sdes			http_cmd(conn, "%s %s://%s%s HTTP/1.1",
873107372Sdes			    op, url->scheme, host, url->doc);
87490267Sdes		} else {
875174588Sdes			http_cmd(conn, "%s %s HTTP/1.1",
87690267Sdes			    op, url->doc);
87790267Sdes		}
87837535Sdes
87990267Sdes		/* virtual host */
880174588Sdes		http_cmd(conn, "Host: %s", host);
88190267Sdes
88290267Sdes		/* proxy authorization */
88390267Sdes		if (purl) {
88490267Sdes			if (*purl->user || *purl->pwd)
885174588Sdes				http_basic_auth(conn, "Proxy-Authorization",
88690267Sdes				    purl->user, purl->pwd);
88790267Sdes			else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL && *p != '\0')
888174588Sdes				http_authorize(conn, "Proxy-Authorization", p);
88990267Sdes		}
89090267Sdes
89190267Sdes		/* server authorization */
89290267Sdes		if (need_auth || *url->user || *url->pwd) {
89390267Sdes			if (*url->user || *url->pwd)
894174588Sdes				http_basic_auth(conn, "Authorization", url->user, url->pwd);
89590267Sdes			else if ((p = getenv("HTTP_AUTH")) != NULL && *p != '\0')
896174588Sdes				http_authorize(conn, "Authorization", p);
89790267Sdes			else if (fetchAuthMethod && fetchAuthMethod(url) == 0) {
898174588Sdes				http_basic_auth(conn, "Authorization", url->user, url->pwd);
89990267Sdes			} else {
900174588Sdes				http_seterr(HTTP_NEED_AUTH);
90190267Sdes				goto ouch;
90290267Sdes			}
90390267Sdes		}
90490267Sdes
90590267Sdes		/* other headers */
906107372Sdes		if ((p = getenv("HTTP_REFERER")) != NULL && *p != '\0') {
907107372Sdes			if (strcasecmp(p, "auto") == 0)
908174588Sdes				http_cmd(conn, "Referer: %s://%s%s",
909107372Sdes				    url->scheme, host, url->doc);
910107372Sdes			else
911174588Sdes				http_cmd(conn, "Referer: %s", p);
912107372Sdes		}
91390267Sdes		if ((p = getenv("HTTP_USER_AGENT")) != NULL && *p != '\0')
914174588Sdes			http_cmd(conn, "User-Agent: %s", p);
91590267Sdes		else
916174588Sdes			http_cmd(conn, "User-Agent: %s " _LIBFETCH_VER, getprogname());
917109693Sdes		if (url->offset > 0)
918174588Sdes			http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset);
919174588Sdes		http_cmd(conn, "Connection: close");
920174588Sdes		http_cmd(conn, "");
92190267Sdes
922143049Skbyanc		/*
923143049Skbyanc		 * Force the queued request to be dispatched.  Normally, one
924143049Skbyanc		 * would do this with shutdown(2) but squid proxies can be
925143049Skbyanc		 * configured to disallow such half-closed connections.  To
926143049Skbyanc		 * be compatible with such configurations, fiddle with socket
927143049Skbyanc		 * options to force the pending data to be written.
928143049Skbyanc		 */
929143049Skbyanc		val = 0;
930143049Skbyanc		setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val,
931143049Skbyanc			   sizeof(val));
932143049Skbyanc		val = 1;
933143049Skbyanc		setsockopt(conn->sd, IPPROTO_TCP, TCP_NODELAY, &val,
934143049Skbyanc			   sizeof(val));
935143049Skbyanc
93690267Sdes		/* get reply */
937174588Sdes		switch (http_get_reply(conn)) {
93890267Sdes		case HTTP_OK:
93990267Sdes		case HTTP_PARTIAL:
94090267Sdes			/* fine */
94190267Sdes			break;
94290267Sdes		case HTTP_MOVED_PERM:
94390267Sdes		case HTTP_MOVED_TEMP:
94490267Sdes		case HTTP_SEE_OTHER:
94590267Sdes			/*
946125695Sdes			 * Not so fine, but we still have to read the
947125695Sdes			 * headers to get the new location.
94890267Sdes			 */
94990267Sdes			break;
95090267Sdes		case HTTP_NEED_AUTH:
95190267Sdes			if (need_auth) {
95290267Sdes				/*
953125695Sdes				 * We already sent out authorization code,
954125695Sdes				 * so there's nothing more we can do.
95590267Sdes				 */
956174588Sdes				http_seterr(conn->err);
95790267Sdes				goto ouch;
95890267Sdes			}
95990267Sdes			/* try again, but send the password this time */
96090267Sdes			if (verbose)
961174588Sdes				fetch_info("server requires authorization");
96290267Sdes			break;
96390267Sdes		case HTTP_NEED_PROXY_AUTH:
96490267Sdes			/*
965125695Sdes			 * If we're talking to a proxy, we already sent
966125695Sdes			 * our proxy authorization code, so there's
967125695Sdes			 * nothing more we can do.
96890267Sdes			 */
969174588Sdes			http_seterr(conn->err);
97090267Sdes			goto ouch;
971125696Sdes		case HTTP_BAD_RANGE:
972125696Sdes			/*
973125696Sdes			 * This can happen if we ask for 0 bytes because
974125696Sdes			 * we already have the whole file.  Consider this
975125696Sdes			 * a success for now, and check sizes later.
976125696Sdes			 */
977125696Sdes			break;
97890267Sdes		case HTTP_PROTOCOL_ERROR:
97990267Sdes			/* fall through */
98090267Sdes		case -1:
981174588Sdes			fetch_syserr();
98290267Sdes			goto ouch;
98390267Sdes		default:
984174588Sdes			http_seterr(conn->err);
98590267Sdes			if (!verbose)
98690267Sdes				goto ouch;
98790267Sdes			/* fall through so we can get the full error message */
98890267Sdes		}
98990267Sdes
99090267Sdes		/* get headers */
99190267Sdes		do {
992174588Sdes			switch ((h = http_next_header(conn, &p))) {
99390267Sdes			case hdr_syserror:
994174588Sdes				fetch_syserr();
99590267Sdes				goto ouch;
99690267Sdes			case hdr_error:
997174588Sdes				http_seterr(HTTP_PROTOCOL_ERROR);
99890267Sdes				goto ouch;
99990267Sdes			case hdr_content_length:
1000174588Sdes				http_parse_length(p, &clength);
100190267Sdes				break;
100290267Sdes			case hdr_content_range:
1003174588Sdes				http_parse_range(p, &offset, &length, &size);
100490267Sdes				break;
100590267Sdes			case hdr_last_modified:
1006174588Sdes				http_parse_mtime(p, &mtime);
100790267Sdes				break;
100890267Sdes			case hdr_location:
100997856Sdes				if (!HTTP_REDIRECT(conn->err))
101090267Sdes					break;
101190267Sdes				if (new)
101290267Sdes					free(new);
101390267Sdes				if (verbose)
1014174588Sdes					fetch_info("%d redirect to %s", conn->err, p);
101590267Sdes				if (*p == '/')
101690267Sdes					/* absolute path */
101790267Sdes					new = fetchMakeURL(url->scheme, url->host, url->port, p,
101890267Sdes					    url->user, url->pwd);
101990267Sdes				else
102090267Sdes					new = fetchParseURL(p);
102190267Sdes				if (new == NULL) {
102290267Sdes					/* XXX should set an error code */
102390267Sdes					DEBUG(fprintf(stderr, "failed to parse new URL\n"));
102490267Sdes					goto ouch;
102590267Sdes				}
102690267Sdes				if (!*new->user && !*new->pwd) {
102790267Sdes					strcpy(new->user, url->user);
102890267Sdes					strcpy(new->pwd, url->pwd);
102990267Sdes				}
103090267Sdes				new->offset = url->offset;
103190267Sdes				new->length = url->length;
103290267Sdes				break;
103390267Sdes			case hdr_transfer_encoding:
103490267Sdes				/* XXX weak test*/
103590267Sdes				chunked = (strcasecmp(p, "chunked") == 0);
103690267Sdes				break;
103790267Sdes			case hdr_www_authenticate:
103897856Sdes				if (conn->err != HTTP_NEED_AUTH)
103990267Sdes					break;
104090267Sdes				/* if we were smarter, we'd check the method and realm */
104190267Sdes				break;
104290267Sdes			case hdr_end:
104390267Sdes				/* fall through */
104490267Sdes			case hdr_unknown:
104590267Sdes				/* ignore */
104690267Sdes				break;
104790267Sdes			}
104890267Sdes		} while (h > hdr_end);
104990267Sdes
105090267Sdes		/* we need to provide authentication */
105197856Sdes		if (conn->err == HTTP_NEED_AUTH) {
105298422Sdes			e = conn->err;
105390267Sdes			need_auth = 1;
1054174588Sdes			fetch_close(conn);
105597856Sdes			conn = NULL;
105690267Sdes			continue;
105790267Sdes		}
105890267Sdes
1059125696Sdes		/* requested range not satisfiable */
1060125696Sdes		if (conn->err == HTTP_BAD_RANGE) {
1061125696Sdes			if (url->offset == size && url->length == 0) {
1062125696Sdes				/* asked for 0 bytes; fake it */
1063125696Sdes				offset = url->offset;
1064125696Sdes				conn->err = HTTP_OK;
1065125696Sdes				break;
1066125696Sdes			} else {
1067174588Sdes				http_seterr(conn->err);
1068125696Sdes				goto ouch;
1069125696Sdes			}
1070125696Sdes		}
1071125696Sdes
1072104404Sru		/* we have a hit or an error */
1073104404Sru		if (conn->err == HTTP_OK || conn->err == HTTP_PARTIAL || HTTP_ERROR(conn->err))
1074104404Sru			break;
1075104404Sru
107690267Sdes		/* all other cases: we got a redirect */
107798422Sdes		e = conn->err;
107890267Sdes		need_auth = 0;
1079174588Sdes		fetch_close(conn);
108097856Sdes		conn = NULL;
108190267Sdes		if (!new) {
108290267Sdes			DEBUG(fprintf(stderr, "redirect with no new location\n"));
108390267Sdes			break;
108490267Sdes		}
108590267Sdes		if (url != URL)
108690267Sdes			fetchFreeURL(url);
108790267Sdes		url = new;
108890267Sdes	} while (++i < n);
108990267Sdes
109090267Sdes	/* we failed, or ran out of retries */
109197856Sdes	if (conn == NULL) {
1092174588Sdes		http_seterr(e);
109363012Sdes		goto ouch;
109463012Sdes	}
109560376Sdes
109690267Sdes	DEBUG(fprintf(stderr, "offset %lld, length %lld,"
109790267Sdes		  " size %lld, clength %lld\n",
109890267Sdes		  (long long)offset, (long long)length,
109990267Sdes		  (long long)size, (long long)clength));
110060376Sdes
110190267Sdes	/* check for inconsistencies */
110290267Sdes	if (clength != -1 && length != -1 && clength != length) {
1103174588Sdes		http_seterr(HTTP_PROTOCOL_ERROR);
110463012Sdes		goto ouch;
110563012Sdes	}
110690267Sdes	if (clength == -1)
110790267Sdes		clength = length;
110890267Sdes	if (clength != -1)
110990267Sdes		length = offset + clength;
111090267Sdes	if (length != -1 && size != -1 && length != size) {
1111174588Sdes		http_seterr(HTTP_PROTOCOL_ERROR);
111263012Sdes		goto ouch;
111390267Sdes	}
111490267Sdes	if (size == -1)
111590267Sdes		size = length;
111660376Sdes
111790267Sdes	/* fill in stats */
111890267Sdes	if (us) {
111990267Sdes		us->size = size;
112090267Sdes		us->atime = us->mtime = mtime;
112190267Sdes	}
112263069Sdes
112390267Sdes	/* too far? */
1124109693Sdes	if (URL->offset > 0 && offset > URL->offset) {
1125174588Sdes		http_seterr(HTTP_PROTOCOL_ERROR);
112690267Sdes		goto ouch;
112777238Sdes	}
112860376Sdes
112990267Sdes	/* report back real offset and size */
113090267Sdes	URL->offset = offset;
113190267Sdes	URL->length = clength;
113237535Sdes
113390267Sdes	/* wrap it up in a FILE */
1134174588Sdes	if ((f = http_funopen(conn, chunked)) == NULL) {
1135174588Sdes		fetch_syserr();
113690267Sdes		goto ouch;
113790267Sdes	}
113863716Sdes
113990267Sdes	if (url != URL)
114090267Sdes		fetchFreeURL(url);
114190267Sdes	if (purl)
114290267Sdes		fetchFreeURL(purl);
114363567Sdes
114497856Sdes	if (HTTP_ERROR(conn->err)) {
1145174588Sdes		http_print_html(stderr, f);
114690267Sdes		fclose(f);
114790267Sdes		f = NULL;
114890267Sdes	}
114963012Sdes
115090267Sdes	return (f);
115188771Sdes
115290267Sdesouch:
115390267Sdes	if (url != URL)
115490267Sdes		fetchFreeURL(url);
115590267Sdes	if (purl)
115690267Sdes		fetchFreeURL(purl);
115797856Sdes	if (conn != NULL)
1158174588Sdes		fetch_close(conn);
115990267Sdes	return (NULL);
116063012Sdes}
116160189Sdes
116290267Sdes
116363012Sdes/*****************************************************************************
116463012Sdes * Entry points
116563012Sdes */
116663012Sdes
116763012Sdes/*
116863340Sdes * Retrieve and stat a file by HTTP
116963340Sdes */
117063340SdesFILE *
117175891SarchiefetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags)
117263340Sdes{
1173174752Sdes	return (http_request(URL, "GET", us, http_get_proxy(URL, flags), flags));
117463340Sdes}
117563340Sdes
117663340Sdes/*
117763012Sdes * Retrieve a file by HTTP
117863012Sdes */
117963012SdesFILE *
118075891SarchiefetchGetHTTP(struct url *URL, const char *flags)
118163012Sdes{
118290267Sdes	return (fetchXGetHTTP(URL, NULL, flags));
118337535Sdes}
118437535Sdes
118563340Sdes/*
118663340Sdes * Store a file by HTTP
118763340Sdes */
118837535SdesFILE *
118985093SdesfetchPutHTTP(struct url *URL __unused, const char *flags __unused)
119037535Sdes{
119190267Sdes	warnx("fetchPutHTTP(): not implemented");
119290267Sdes	return (NULL);
119337535Sdes}
119440975Sdes
119540975Sdes/*
119640975Sdes * Get an HTTP document's metadata
119740975Sdes */
119840975Sdesint
119975891SarchiefetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags)
120040975Sdes{
120190267Sdes	FILE *f;
120290267Sdes
1203174752Sdes	f = http_request(URL, "HEAD", us, http_get_proxy(URL, flags), flags);
1204112081Sdes	if (f == NULL)
120590267Sdes		return (-1);
120690267Sdes	fclose(f);
120790267Sdes	return (0);
120840975Sdes}
120941989Sdes
121041989Sdes/*
121141989Sdes * List a directory
121241989Sdes */
121341989Sdesstruct url_ent *
121485093SdesfetchListHTTP(struct url *url __unused, const char *flags __unused)
121541989Sdes{
121690267Sdes	warnx("fetchListHTTP(): not implemented");
121790267Sdes	return (NULL);
121841989Sdes}
1219