http.c revision 97868
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 97868 2002-06-05 12:46:36Z 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;
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);
68697868Sdes		return (NULL);
68797868Sdes	}
68897856Sdes	return (conn);
68967043Sdes}
69067043Sdes
69167043Sdesstatic struct url *
69275891Sarchie_http_get_proxy(void)
69367043Sdes{
69490267Sdes	struct url *purl;
69590267Sdes	char *p;
69690267Sdes
69790267Sdes	if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
69890267Sdes	    (purl = fetchParseURL(p))) {
69990267Sdes		if (!*purl->scheme)
70090267Sdes			strcpy(purl->scheme, SCHEME_HTTP);
70190267Sdes		if (!purl->port)
70290267Sdes			purl->port = _fetch_default_proxy_port(purl->scheme);
70390267Sdes		if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
70490267Sdes			return (purl);
70590267Sdes		fetchFreeURL(purl);
70690267Sdes	}
70790267Sdes	return (NULL);
70860376Sdes}
70960376Sdes
71088771Sdesstatic void
71188771Sdes_http_print_html(FILE *out, FILE *in)
71288771Sdes{
71390267Sdes	size_t len;
71490267Sdes	char *line, *p, *q;
71590267Sdes	int comment, tag;
71688771Sdes
71790267Sdes	comment = tag = 0;
71890267Sdes	while ((line = fgetln(in, &len)) != NULL) {
71990267Sdes		while (len && isspace(line[len - 1]))
72090267Sdes			--len;
72190267Sdes		for (p = q = line; q < line + len; ++q) {
72290267Sdes			if (comment && *q == '-') {
72390267Sdes				if (q + 2 < line + len &&
72490267Sdes				    strcmp(q, "-->") == 0) {
72590267Sdes					tag = comment = 0;
72690267Sdes					q += 2;
72790267Sdes				}
72890267Sdes			} else if (tag && !comment && *q == '>') {
72990267Sdes				p = q + 1;
73090267Sdes				tag = 0;
73190267Sdes			} else if (!tag && *q == '<') {
73290267Sdes				if (q > p)
73390267Sdes					fwrite(p, q - p, 1, out);
73490267Sdes				tag = 1;
73590267Sdes				if (q + 3 < line + len &&
73690267Sdes				    strcmp(q, "<!--") == 0) {
73790267Sdes					comment = 1;
73890267Sdes					q += 3;
73990267Sdes				}
74090267Sdes			}
74188771Sdes		}
74290267Sdes		if (!tag && q > p)
74390267Sdes			fwrite(p, q - p, 1, out);
74490267Sdes		fputc('\n', out);
74588771Sdes	}
74688771Sdes}
74788771Sdes
74890267Sdes
74963012Sdes/*****************************************************************************
75063012Sdes * Core
75160954Sdes */
75260954Sdes
75360954Sdes/*
75463012Sdes * Send a request and process the reply
75597866Sdes *
75697866Sdes * XXX This function is way too long, the do..while loop should be split
75797866Sdes * XXX off into a separate function.
75860376Sdes */
75967043SdesFILE *
76075891Sarchie_http_request(struct url *URL, const char *op, struct url_stat *us,
76190267Sdes    struct url *purl, const char *flags)
76260376Sdes{
76397856Sdes	conn_t *conn;
76490267Sdes	struct url *url, *new;
76590267Sdes	int chunked, direct, need_auth, noredirect, verbose;
76697856Sdes	int i, n;
76790267Sdes	off_t offset, clength, length, size;
76890267Sdes	time_t mtime;
76990267Sdes	const char *p;
77090267Sdes	FILE *f;
77190267Sdes	hdr_t h;
77290267Sdes	char *host;
77360737Sume#ifdef INET6
77490267Sdes	char hbuf[MAXHOSTNAMELEN + 1];
77560737Sume#endif
77663012Sdes
77790267Sdes	direct = CHECK_FLAG('d');
77890267Sdes	noredirect = CHECK_FLAG('A');
77990267Sdes	verbose = CHECK_FLAG('v');
78060737Sume
78190267Sdes	if (direct && purl) {
78290267Sdes		fetchFreeURL(purl);
78390267Sdes		purl = NULL;
78490267Sdes	}
78563716Sdes
78690267Sdes	/* try the provided URL first */
78790267Sdes	url = URL;
78863012Sdes
78990267Sdes	/* if the A flag is set, we only get one try */
79090267Sdes	n = noredirect ? 1 : MAX_REDIRECT;
79190267Sdes	i = 0;
79263012Sdes
79390267Sdes	need_auth = 0;
79490267Sdes	do {
79590267Sdes		new = NULL;
79690267Sdes		chunked = 0;
79790267Sdes		offset = 0;
79890267Sdes		clength = -1;
79990267Sdes		length = -1;
80090267Sdes		size = -1;
80190267Sdes		mtime = 0;
80290267Sdes
80390267Sdes		/* check port */
80490267Sdes		if (!url->port)
80590267Sdes			url->port = _fetch_default_port(url->scheme);
80690267Sdes
80790267Sdes		/* were we redirected to an FTP URL? */
80890267Sdes		if (purl == NULL && strcmp(url->scheme, SCHEME_FTP) == 0) {
80990267Sdes			if (strcmp(op, "GET") == 0)
81090267Sdes				return (_ftp_request(url, "RETR", us, purl, flags));
81190267Sdes			else if (strcmp(op, "HEAD") == 0)
81290267Sdes				return (_ftp_request(url, "STAT", us, purl, flags));
81390267Sdes		}
81490267Sdes
81590267Sdes		/* connect to server or proxy */
81697856Sdes		if ((conn = _http_connect(url, purl, flags)) == NULL)
81790267Sdes			goto ouch;
81890267Sdes
81990267Sdes		host = url->host;
82060737Sume#ifdef INET6
82190267Sdes		if (strchr(url->host, ':')) {
82290267Sdes			snprintf(hbuf, sizeof(hbuf), "[%s]", url->host);
82390267Sdes			host = hbuf;
82490267Sdes		}
82560737Sume#endif
82637535Sdes
82790267Sdes		/* send request */
82890267Sdes		if (verbose)
82990267Sdes			_fetch_info("requesting %s://%s:%d%s",
83090267Sdes			    url->scheme, host, url->port, url->doc);
83190267Sdes		if (purl) {
83297856Sdes			_http_cmd(conn, "%s %s://%s:%d%s HTTP/1.1",
83390267Sdes			    op, url->scheme, host, url->port, url->doc);
83490267Sdes		} else {
83597856Sdes			_http_cmd(conn, "%s %s HTTP/1.1",
83690267Sdes			    op, url->doc);
83790267Sdes		}
83837535Sdes
83990267Sdes		/* virtual host */
84090267Sdes		if (url->port == _fetch_default_port(url->scheme))
84197856Sdes			_http_cmd(conn, "Host: %s", host);
84290267Sdes		else
84397856Sdes			_http_cmd(conn, "Host: %s:%d", host, url->port);
84490267Sdes
84590267Sdes		/* proxy authorization */
84690267Sdes		if (purl) {
84790267Sdes			if (*purl->user || *purl->pwd)
84897856Sdes				_http_basic_auth(conn, "Proxy-Authorization",
84990267Sdes				    purl->user, purl->pwd);
85090267Sdes			else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL && *p != '\0')
85197856Sdes				_http_authorize(conn, "Proxy-Authorization", p);
85290267Sdes		}
85390267Sdes
85490267Sdes		/* server authorization */
85590267Sdes		if (need_auth || *url->user || *url->pwd) {
85690267Sdes			if (*url->user || *url->pwd)
85797856Sdes				_http_basic_auth(conn, "Authorization", url->user, url->pwd);
85890267Sdes			else if ((p = getenv("HTTP_AUTH")) != NULL && *p != '\0')
85997856Sdes				_http_authorize(conn, "Authorization", p);
86090267Sdes			else if (fetchAuthMethod && fetchAuthMethod(url) == 0) {
86197856Sdes				_http_basic_auth(conn, "Authorization", url->user, url->pwd);
86290267Sdes			} else {
86390267Sdes				_http_seterr(HTTP_NEED_AUTH);
86490267Sdes				goto ouch;
86590267Sdes			}
86690267Sdes		}
86790267Sdes
86890267Sdes		/* other headers */
86990267Sdes		if ((p = getenv("HTTP_USER_AGENT")) != NULL && *p != '\0')
87097856Sdes			_http_cmd(conn, "User-Agent: %s", p);
87190267Sdes		else
87297856Sdes			_http_cmd(conn, "User-Agent: %s " _LIBFETCH_VER, getprogname());
87390267Sdes		if (url->offset)
87497856Sdes			_http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset);
87597856Sdes		_http_cmd(conn, "Connection: close");
87697856Sdes		_http_cmd(conn, "");
87790267Sdes
87890267Sdes		/* get reply */
87997856Sdes		switch (_http_get_reply(conn)) {
88090267Sdes		case HTTP_OK:
88190267Sdes		case HTTP_PARTIAL:
88290267Sdes			/* fine */
88390267Sdes			break;
88490267Sdes		case HTTP_MOVED_PERM:
88590267Sdes		case HTTP_MOVED_TEMP:
88690267Sdes		case HTTP_SEE_OTHER:
88790267Sdes			/*
88890267Sdes			 * Not so fine, but we still have to read the headers to
88990267Sdes			 * get the new location.
89090267Sdes			 */
89190267Sdes			break;
89290267Sdes		case HTTP_NEED_AUTH:
89390267Sdes			if (need_auth) {
89490267Sdes				/*
89590267Sdes				 * We already sent out authorization code, so there's
89690267Sdes				 * nothing more we can do.
89790267Sdes				 */
89897856Sdes				_http_seterr(conn->err);
89990267Sdes				goto ouch;
90090267Sdes			}
90190267Sdes			/* try again, but send the password this time */
90290267Sdes			if (verbose)
90390267Sdes				_fetch_info("server requires authorization");
90490267Sdes			break;
90590267Sdes		case HTTP_NEED_PROXY_AUTH:
90690267Sdes			/*
90790267Sdes			 * If we're talking to a proxy, we already sent our proxy
90890267Sdes			 * authorization code, so there's nothing more we can do.
90990267Sdes			 */
91097856Sdes			_http_seterr(conn->err);
91190267Sdes			goto ouch;
91290267Sdes		case HTTP_PROTOCOL_ERROR:
91390267Sdes			/* fall through */
91490267Sdes		case -1:
91590267Sdes			_fetch_syserr();
91690267Sdes			goto ouch;
91790267Sdes		default:
91897856Sdes			_http_seterr(conn->err);
91990267Sdes			if (!verbose)
92090267Sdes				goto ouch;
92190267Sdes			/* fall through so we can get the full error message */
92290267Sdes		}
92390267Sdes
92490267Sdes		/* get headers */
92590267Sdes		do {
92697856Sdes			switch ((h = _http_next_header(conn, &p))) {
92790267Sdes			case hdr_syserror:
92890267Sdes				_fetch_syserr();
92990267Sdes				goto ouch;
93090267Sdes			case hdr_error:
93190267Sdes				_http_seterr(HTTP_PROTOCOL_ERROR);
93290267Sdes				goto ouch;
93390267Sdes			case hdr_content_length:
93490267Sdes				_http_parse_length(p, &clength);
93590267Sdes				break;
93690267Sdes			case hdr_content_range:
93790267Sdes				_http_parse_range(p, &offset, &length, &size);
93890267Sdes				break;
93990267Sdes			case hdr_last_modified:
94090267Sdes				_http_parse_mtime(p, &mtime);
94190267Sdes				break;
94290267Sdes			case hdr_location:
94397856Sdes				if (!HTTP_REDIRECT(conn->err))
94490267Sdes					break;
94590267Sdes				if (new)
94690267Sdes					free(new);
94790267Sdes				if (verbose)
94897856Sdes					_fetch_info("%d redirect to %s", conn->err, p);
94990267Sdes				if (*p == '/')
95090267Sdes					/* absolute path */
95190267Sdes					new = fetchMakeURL(url->scheme, url->host, url->port, p,
95290267Sdes					    url->user, url->pwd);
95390267Sdes				else
95490267Sdes					new = fetchParseURL(p);
95590267Sdes				if (new == NULL) {
95690267Sdes					/* XXX should set an error code */
95790267Sdes					DEBUG(fprintf(stderr, "failed to parse new URL\n"));
95890267Sdes					goto ouch;
95990267Sdes				}
96090267Sdes				if (!*new->user && !*new->pwd) {
96190267Sdes					strcpy(new->user, url->user);
96290267Sdes					strcpy(new->pwd, url->pwd);
96390267Sdes				}
96490267Sdes				new->offset = url->offset;
96590267Sdes				new->length = url->length;
96690267Sdes				break;
96790267Sdes			case hdr_transfer_encoding:
96890267Sdes				/* XXX weak test*/
96990267Sdes				chunked = (strcasecmp(p, "chunked") == 0);
97090267Sdes				break;
97190267Sdes			case hdr_www_authenticate:
97297856Sdes				if (conn->err != HTTP_NEED_AUTH)
97390267Sdes					break;
97490267Sdes				/* if we were smarter, we'd check the method and realm */
97590267Sdes				break;
97690267Sdes			case hdr_end:
97790267Sdes				/* fall through */
97890267Sdes			case hdr_unknown:
97990267Sdes				/* ignore */
98090267Sdes				break;
98190267Sdes			}
98290267Sdes		} while (h > hdr_end);
98390267Sdes
98490267Sdes		/* we have a hit or an error */
98597856Sdes		if (conn->err == HTTP_OK || conn->err == HTTP_PARTIAL || HTTP_ERROR(conn->err))
98690267Sdes			break;
98790267Sdes
98890267Sdes		/* we need to provide authentication */
98997856Sdes		if (conn->err == HTTP_NEED_AUTH) {
99090267Sdes			need_auth = 1;
99197856Sdes			_fetch_close(conn);
99297856Sdes			conn = NULL;
99390267Sdes			continue;
99490267Sdes		}
99590267Sdes
99690267Sdes		/* all other cases: we got a redirect */
99790267Sdes		need_auth = 0;
99897856Sdes		_fetch_close(conn);
99997856Sdes		conn = NULL;
100090267Sdes		if (!new) {
100190267Sdes			DEBUG(fprintf(stderr, "redirect with no new location\n"));
100290267Sdes			break;
100390267Sdes		}
100490267Sdes		if (url != URL)
100590267Sdes			fetchFreeURL(url);
100690267Sdes		url = new;
100790267Sdes	} while (++i < n);
100890267Sdes
100990267Sdes	/* we failed, or ran out of retries */
101097856Sdes	if (conn == NULL) {
101197856Sdes		_http_seterr(conn->err);
101263012Sdes		goto ouch;
101363012Sdes	}
101460376Sdes
101590267Sdes	DEBUG(fprintf(stderr, "offset %lld, length %lld,"
101690267Sdes		  " size %lld, clength %lld\n",
101790267Sdes		  (long long)offset, (long long)length,
101890267Sdes		  (long long)size, (long long)clength));
101960376Sdes
102090267Sdes	/* check for inconsistencies */
102190267Sdes	if (clength != -1 && length != -1 && clength != length) {
102290267Sdes		_http_seterr(HTTP_PROTOCOL_ERROR);
102363012Sdes		goto ouch;
102463012Sdes	}
102590267Sdes	if (clength == -1)
102690267Sdes		clength = length;
102790267Sdes	if (clength != -1)
102890267Sdes		length = offset + clength;
102990267Sdes	if (length != -1 && size != -1 && length != size) {
103063012Sdes		_http_seterr(HTTP_PROTOCOL_ERROR);
103163012Sdes		goto ouch;
103290267Sdes	}
103390267Sdes	if (size == -1)
103490267Sdes		size = length;
103560376Sdes
103690267Sdes	/* fill in stats */
103790267Sdes	if (us) {
103890267Sdes		us->size = size;
103990267Sdes		us->atime = us->mtime = mtime;
104090267Sdes	}
104163069Sdes
104290267Sdes	/* too far? */
104390267Sdes	if (offset > URL->offset) {
104490267Sdes		_http_seterr(HTTP_PROTOCOL_ERROR);
104590267Sdes		goto ouch;
104677238Sdes	}
104760376Sdes
104890267Sdes	/* report back real offset and size */
104990267Sdes	URL->offset = offset;
105090267Sdes	URL->length = clength;
105137535Sdes
105290267Sdes	/* wrap it up in a FILE */
105397866Sdes	if ((f = _http_funopen(conn, chunked)) == NULL) {
105490267Sdes		_fetch_syserr();
105590267Sdes		goto ouch;
105690267Sdes	}
105763716Sdes
105890267Sdes	if (url != URL)
105990267Sdes		fetchFreeURL(url);
106090267Sdes	if (purl)
106190267Sdes		fetchFreeURL(purl);
106263567Sdes
106397856Sdes	if (HTTP_ERROR(conn->err)) {
106490267Sdes		_http_print_html(stderr, f);
106590267Sdes		fclose(f);
106690267Sdes		f = NULL;
106790267Sdes	}
106863012Sdes
106990267Sdes	return (f);
107088771Sdes
107190267Sdesouch:
107290267Sdes	if (url != URL)
107390267Sdes		fetchFreeURL(url);
107490267Sdes	if (purl)
107590267Sdes		fetchFreeURL(purl);
107697856Sdes	if (conn != NULL)
107797856Sdes		_fetch_close(conn);
107890267Sdes	return (NULL);
107963012Sdes}
108060189Sdes
108190267Sdes
108263012Sdes/*****************************************************************************
108363012Sdes * Entry points
108463012Sdes */
108563012Sdes
108663012Sdes/*
108763340Sdes * Retrieve and stat a file by HTTP
108863340Sdes */
108963340SdesFILE *
109075891SarchiefetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags)
109163340Sdes{
109290267Sdes	return (_http_request(URL, "GET", us, _http_get_proxy(), flags));
109363340Sdes}
109463340Sdes
109563340Sdes/*
109663012Sdes * Retrieve a file by HTTP
109763012Sdes */
109863012SdesFILE *
109975891SarchiefetchGetHTTP(struct url *URL, const char *flags)
110063012Sdes{
110190267Sdes	return (fetchXGetHTTP(URL, NULL, flags));
110237535Sdes}
110337535Sdes
110463340Sdes/*
110563340Sdes * Store a file by HTTP
110663340Sdes */
110737535SdesFILE *
110885093SdesfetchPutHTTP(struct url *URL __unused, const char *flags __unused)
110937535Sdes{
111090267Sdes	warnx("fetchPutHTTP(): not implemented");
111190267Sdes	return (NULL);
111237535Sdes}
111340975Sdes
111440975Sdes/*
111540975Sdes * Get an HTTP document's metadata
111640975Sdes */
111740975Sdesint
111875891SarchiefetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags)
111940975Sdes{
112090267Sdes	FILE *f;
112190267Sdes
112290267Sdes	if ((f = _http_request(URL, "HEAD", us, _http_get_proxy(), flags)) == NULL)
112390267Sdes		return (-1);
112490267Sdes	fclose(f);
112590267Sdes	return (0);
112640975Sdes}
112741989Sdes
112841989Sdes/*
112941989Sdes * List a directory
113041989Sdes */
113141989Sdesstruct url_ent *
113285093SdesfetchListHTTP(struct url *url __unused, const char *flags __unused)
113341989Sdes{
113490267Sdes	warnx("fetchListHTTP(): not implemented");
113590267Sdes	return (NULL);
113641989Sdes}
1137