http.c revision 63567
1/*-
2 * Copyright (c) 2000 Dag-Erling Co�dan Sm�rgrav
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 *      $FreeBSD: head/lib/libfetch/http.c 63567 2000-07-19 23:43:49Z des $
29 */
30
31/*
32 * The following copyright applies to the base64 code:
33 *
34 *-
35 * Copyright 1997 Massachusetts Institute of Technology
36 *
37 * Permission to use, copy, modify, and distribute this software and
38 * its documentation for any purpose and without fee is hereby
39 * granted, provided that both the above copyright notice and this
40 * permission notice appear in all copies, that both the above
41 * copyright notice and this permission notice appear in all
42 * supporting documentation, and that the name of M.I.T. not be used
43 * in advertising or publicity pertaining to distribution of the
44 * software without specific, written prior permission.  M.I.T. makes
45 * no representations about the suitability of this software for any
46 * purpose.  It is provided "as is" without express or implied
47 * warranty.
48 *
49 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
50 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
51 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
52 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
53 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
56 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
58 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
59 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 */
62
63#include <sys/param.h>
64#include <sys/socket.h>
65
66#include <ctype.h>
67#include <err.h>
68#include <errno.h>
69#include <locale.h>
70#include <netdb.h>
71#include <stdarg.h>
72#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
75#include <time.h>
76#include <unistd.h>
77
78#include "fetch.h"
79#include "common.h"
80#include "httperr.h"
81
82extern char *__progname; /* XXX not portable */
83
84/* Maximum number of redirects to follow */
85#define MAX_REDIRECT 5
86
87/* Symbolic names for reply codes we care about */
88#define HTTP_OK			200
89#define HTTP_PARTIAL		206
90#define HTTP_MOVED_PERM		301
91#define HTTP_MOVED_TEMP		302
92#define HTTP_SEE_OTHER		303
93#define HTTP_NEED_AUTH		401
94#define HTTP_NEED_PROXY_AUTH	403
95#define HTTP_PROTOCOL_ERROR	999
96
97#define HTTP_REDIRECT(xyz) ((xyz) == HTTP_MOVED_PERM \
98                            || (xyz) == HTTP_MOVED_TEMP \
99                            || (xyz) == HTTP_SEE_OTHER)
100
101
102
103/*****************************************************************************
104 * I/O functions for decoding chunked streams
105 */
106
107struct cookie
108{
109    int		 fd;
110    char	*buf;
111    size_t	 b_size;
112    size_t	 b_len;
113    int		 b_pos;
114    int		 eof;
115    int		 error;
116    long	 chunksize;
117#ifndef NDEBUG
118    long	 total;
119#endif
120};
121
122/*
123 * Get next chunk header
124 */
125static int
126_http_new_chunk(struct cookie *c)
127{
128    char *p;
129
130    if (_fetch_getln(c->fd, &c->buf, &c->b_size, &c->b_len) == -1)
131	return -1;
132
133    if (c->b_len < 2 || !ishexnumber(*c->buf))
134	return -1;
135
136    for (p = c->buf; !isspace(*p) && *p != ';' && p < c->buf + c->b_len; ++p)
137	if (!ishexnumber(*p))
138	    return -1;
139	else if (isdigit(*p))
140	    c->chunksize = c->chunksize * 16 + *p - '0';
141	else
142	    c->chunksize = c->chunksize * 16 + 10 + tolower(*p) - 'a';
143
144#ifndef NDEBUG
145    c->total += c->chunksize;
146    if (c->chunksize == 0)
147	fprintf(stderr, "\033[1m_http_fillbuf(): "
148		"end of last chunk\033[m\n");
149    else
150	fprintf(stderr, "\033[1m_http_fillbuf(): "
151		"new chunk: %ld (%ld)\033[m\n", c->chunksize, c->total);
152#endif
153
154    return c->chunksize;
155}
156
157/*
158 * Fill the input buffer, do chunk decoding on the fly
159 */
160static int
161_http_fillbuf(struct cookie *c)
162{
163    if (c->error)
164	return -1;
165    if (c->eof)
166	return 0;
167
168    if (c->chunksize == 0) {
169	switch (_http_new_chunk(c)) {
170	case -1:
171	    c->error = 1;
172	    return -1;
173	case 0:
174	    c->eof = 1;
175	    return 0;
176	}
177    }
178
179    if (c->b_size < c->chunksize) {
180	char *tmp;
181
182	if ((tmp = realloc(c->buf, c->chunksize)) == NULL)
183	    return -1;
184	c->buf = tmp;
185	c->b_size = c->chunksize;
186    }
187
188    if ((c->b_len = read(c->fd, c->buf, c->chunksize)) == -1)
189	return -1;
190    c->chunksize -= c->b_len;
191
192    if (c->chunksize == 0) {
193	char endl[2];
194	read(c->fd, endl, 2);
195    }
196
197    c->b_pos = 0;
198
199    return c->b_len;
200}
201
202/*
203 * Read function
204 */
205static int
206_http_readfn(void *v, char *buf, int len)
207{
208    struct cookie *c = (struct cookie *)v;
209    int l, pos;
210
211    if (c->error)
212	return -1;
213    if (c->eof)
214	return 0;
215
216    for (pos = 0; len > 0; pos += l, len -= l) {
217	/* empty buffer */
218	if (!c->buf || c->b_pos == c->b_len)
219	    if (_http_fillbuf(c) < 1)
220		break;
221	l = c->b_len - c->b_pos;
222	if (len < l)
223	    l = len;
224	bcopy(c->buf + c->b_pos, buf + pos, l);
225	c->b_pos += l;
226    }
227
228    if (!pos && c->error)
229	return -1;
230    return pos;
231}
232
233/*
234 * Write function
235 */
236static int
237_http_writefn(void *v, const char *buf, int len)
238{
239    struct cookie *c = (struct cookie *)v;
240
241    return write(c->fd, buf, len);
242}
243
244/*
245 * Close function
246 */
247static int
248_http_closefn(void *v)
249{
250    struct cookie *c = (struct cookie *)v;
251    int r;
252
253    r = close(c->fd);
254    if (c->buf)
255	free(c->buf);
256    free(c);
257    return r;
258}
259
260/*
261 * Wrap a file descriptor up
262 */
263static FILE *
264_http_funopen(int fd)
265{
266    struct cookie *c;
267    FILE *f;
268
269    if ((c = calloc(1, sizeof *c)) == NULL) {
270	_fetch_syserr();
271	return NULL;
272    }
273    c->fd = fd;
274    if (!(f = funopen(c, _http_readfn, _http_writefn, NULL, _http_closefn))) {
275	_fetch_syserr();
276	free(c);
277	return NULL;
278    }
279    return f;
280}
281
282
283/*****************************************************************************
284 * Helper functions for talking to the server and parsing its replies
285 */
286
287/* Header types */
288typedef enum {
289    hdr_syserror = -2,
290    hdr_error = -1,
291    hdr_end = 0,
292    hdr_unknown = 1,
293    hdr_content_length,
294    hdr_content_range,
295    hdr_last_modified,
296    hdr_location,
297    hdr_transfer_encoding
298} hdr;
299
300/* Names of interesting headers */
301static struct {
302    hdr		 num;
303    char	*name;
304} hdr_names[] = {
305    { hdr_content_length,	"Content-Length" },
306    { hdr_content_range,	"Content-Range" },
307    { hdr_last_modified,	"Last-Modified" },
308    { hdr_location,		"Location" },
309    { hdr_transfer_encoding,	"Transfer-Encoding" },
310    { hdr_unknown,		NULL },
311};
312
313static char	*reply_buf;
314static size_t	 reply_size;
315static size_t	 reply_length;
316
317/*
318 * Send a formatted line; optionally echo to terminal
319 */
320static int
321_http_cmd(int fd, char *fmt, ...)
322{
323    va_list ap;
324    size_t len;
325    char *msg;
326    int r;
327
328    va_start(ap, fmt);
329    len = vasprintf(&msg, fmt, ap);
330    va_end(ap);
331
332    if (msg == NULL) {
333	errno = ENOMEM;
334	_fetch_syserr();
335	return -1;
336    }
337
338    r = _fetch_putln(fd, msg, len);
339    free(msg);
340
341    if (r == -1) {
342	_fetch_syserr();
343	return -1;
344    }
345
346    return 0;
347}
348
349/*
350 * Get and parse status line
351 */
352static int
353_http_get_reply(int fd)
354{
355    if (_fetch_getln(fd, &reply_buf, &reply_size, &reply_length) == -1)
356	return -1;
357    /*
358     * A valid status line looks like "HTTP/m.n xyz reason" where m
359     * and n are the major and minor protocol version numbers and xyz
360     * is the reply code.
361     * We grok HTTP 1.0 and 1.1, so m must be 1 and n must be 0 or 1.
362     * We don't care about the reason phrase.
363     */
364    if (strncmp(reply_buf, "HTTP/1.", 7) != 0
365	|| (reply_buf[7] != '0' && reply_buf[7] != '1') || reply_buf[8] != ' '
366	|| !isdigit(reply_buf[9])
367	|| !isdigit(reply_buf[10])
368	|| !isdigit(reply_buf[11]))
369	return HTTP_PROTOCOL_ERROR;
370
371    return ((reply_buf[9] - '0') * 100
372	    + (reply_buf[10] - '0') * 10
373	    + (reply_buf[11] - '0'));
374}
375
376/*
377 * Check a header; if the type matches the given string, return a
378 * pointer to the beginning of the value.
379 */
380static char *
381_http_match(char *str, char *hdr)
382{
383    while (*str && *hdr && tolower(*str++) == tolower(*hdr++))
384	/* nothing */;
385    if (*str || *hdr != ':')
386	return NULL;
387    while (*hdr && isspace(*++hdr))
388	/* nothing */;
389    return hdr;
390}
391
392/*
393 * Get the next header and return the appropriate symbolic code.
394 */
395static hdr
396_http_next_header(int fd, char **p)
397{
398    int i;
399
400    if (_fetch_getln(fd, &reply_buf, &reply_size, &reply_length) == -1)
401	return hdr_syserror;
402    while (reply_length && isspace(reply_buf[reply_length-1]))
403	reply_length--;
404    reply_buf[reply_length] = 0;
405    if (reply_length == 0)
406	return hdr_end;
407    /*
408     * We could check for malformed headers but we don't really care.
409     * A valid header starts with a token immediately followed by a
410     * colon; a token is any sequence of non-control, non-whitespace
411     * characters except "()<>@,;:\\\"{}".
412     */
413    for (i = 0; hdr_names[i].num != hdr_unknown; i++)
414	if ((*p = _http_match(hdr_names[i].name, reply_buf)) != NULL)
415	    return hdr_names[i].num;
416    return hdr_unknown;
417}
418
419/*
420 * Parse a last-modified header
421 */
422static time_t
423_http_parse_mtime(char *p)
424{
425    char locale[64];
426    struct tm tm;
427
428    strncpy(locale, setlocale(LC_TIME, NULL), sizeof locale);
429    setlocale(LC_TIME, "C");
430    strptime(p, "%a, %d %b %Y %H:%M:%S GMT", &tm);
431    /* XXX should add support for date-2 and date-3 */
432    setlocale(LC_TIME, locale);
433    DEBUG(fprintf(stderr, "last modified: [\033[1m%04d-%02d-%02d "
434		  "%02d:%02d:%02d\033[m]\n",
435		  tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
436		  tm.tm_hour, tm.tm_min, tm.tm_sec));
437    return timegm(&tm);
438}
439
440/*
441 * Parse a content-length header
442 */
443static off_t
444_http_parse_length(char *p)
445{
446    off_t len;
447
448    for (len = 0; *p && isdigit(*p); ++p)
449	len = len * 10 + (*p - '0');
450    DEBUG(fprintf(stderr, "content length: [\033[1m%lld\033[m]\n", len));
451    return len;
452}
453
454/*
455 * Parse a content-range header
456 */
457static off_t
458_http_parse_range(char *p)
459{
460    off_t off;
461
462    if (strncasecmp(p, "bytes ", 6) != 0)
463	return -1;
464    for (p += 6, off = 0; *p && isdigit(*p); ++p)
465	off = off * 10 + *p - '0';
466    if (*p != '-')
467	return -1;
468    DEBUG(fprintf(stderr, "content range: [\033[1m%lld-\033[m]\n", off));
469    return off;
470}
471
472
473/*****************************************************************************
474 * Helper functions for authorization
475 */
476
477/*
478 * Base64 encoding
479 */
480static char *
481_http_base64(char *src)
482{
483    static const char base64[] =
484	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
485	"abcdefghijklmnopqrstuvwxyz"
486	"0123456789+/";
487    char *str, *dst;
488    size_t l;
489    int t, r;
490
491    l = strlen(src);
492    if ((str = malloc(((l + 2) / 3) * 4)) == NULL)
493	return NULL;
494    dst = str;
495    r = 0;
496
497    while (l >= 3) {
498	t = (src[0] << 16) | (src[1] << 8) | src[2];
499	dst[0] = base64[(t >> 18) & 0x3f];
500	dst[1] = base64[(t >> 12) & 0x3f];
501	dst[2] = base64[(t >> 6) & 0x3f];
502	dst[3] = base64[(t >> 0) & 0x3f];
503	src += 3; l -= 3;
504	dst += 4; r += 4;
505    }
506
507    switch (l) {
508    case 2:
509	t = (src[0] << 16) | (src[1] << 8);
510	dst[0] = base64[(t >> 18) & 0x3f];
511	dst[1] = base64[(t >> 12) & 0x3f];
512	dst[2] = base64[(t >> 6) & 0x3f];
513	dst[3] = '=';
514	dst += 4;
515	r += 4;
516	break;
517    case 1:
518	t = src[0] << 16;
519	dst[0] = base64[(t >> 18) & 0x3f];
520	dst[1] = base64[(t >> 12) & 0x3f];
521	dst[2] = dst[3] = '=';
522	dst += 4;
523	r += 4;
524	break;
525    case 0:
526	break;
527    }
528
529    *dst = 0;
530    return str;
531}
532
533/*
534 * Encode username and password
535 */
536static int
537_http_basic_auth(int fd, char *hdr, char *usr, char *pwd)
538{
539    char *upw, *auth;
540    int r;
541
542    if (asprintf(&upw, "%s:%s", usr, pwd) == -1)
543	return -1;
544    auth = _http_base64(upw);
545    free(upw);
546    if (auth == NULL)
547	return -1;
548    r = _http_cmd(fd, "%s: Basic %s", hdr, auth);
549    free(auth);
550    return r;
551}
552
553/*
554 * Send an authorization header
555 */
556static int
557_http_authorize(int fd, char *hdr, char *p)
558{
559    /* basic authorization */
560    if (strncasecmp(p, "basic:", 6) == 0) {
561	char *user, *pwd, *str;
562	int r;
563
564	/* skip realm */
565	for (p += 6; *p && *p != ':'; ++p)
566	    /* nothing */ ;
567	if (!*p || strchr(++p, ':') == NULL)
568	    return -1;
569	if ((str = strdup(p)) == NULL)
570	    return -1; /* XXX */
571	user = str;
572	pwd = strchr(str, ':');
573	*pwd++ = '\0';
574	r = _http_basic_auth(fd, hdr, user, pwd);
575	free(str);
576	return r;
577    }
578    return -1;
579}
580
581
582/*****************************************************************************
583 * Helper functions for connecting to a server or proxy
584 */
585
586/*
587 * Connect to the specified HTTP proxy server.
588 */
589static int
590_http_proxy_connect(char *proxy, int af, int verbose)
591{
592    char *hostname, *p;
593    int fd, port;
594
595    /* get hostname */
596    hostname = NULL;
597#ifdef INET6
598    /* host part can be an IPv6 address enclosed in square brackets */
599    if (*proxy == '[') {
600	if ((p = strchr(proxy, ']')) == NULL) {
601	    /* no terminating bracket */
602	    /* XXX should set an error code */
603	    goto ouch;
604	}
605	if (p[1] != '\0' && p[1] != ':') {
606	    /* garbage after address */
607	    /* XXX should set an error code */
608	    goto ouch;
609	}
610	if ((hostname = malloc(p - proxy)) == NULL) {
611	    errno = ENOMEM;
612	    _fetch_syserr();
613	    goto ouch;
614	}
615	strncpy(hostname, proxy + 1, p - proxy - 1);
616	hostname[p - proxy - 1] = '\0';
617	++p;
618    } else {
619#endif /* INET6 */
620	if ((p = strchr(proxy, ':')) == NULL)
621	    p = strchr(proxy, '\0');
622	if ((hostname = malloc(p - proxy + 1)) == NULL) {
623	    errno = ENOMEM;
624	    _fetch_syserr();
625	    goto ouch;
626	}
627	strncpy(hostname, proxy, p - proxy);
628	hostname[p - proxy] = '\0';
629#ifdef INET6
630    }
631#endif /* INET6 */
632    DEBUG(fprintf(stderr, "proxy name: [%s]\n", hostname));
633
634    /* get port number */
635    port = 0;
636    if (*p == ':') {
637	++p;
638	if (strspn(p, "0123456789") != strlen(p) || strlen(p) > 5) {
639	    /* port number is non-numeric or too long */
640	    /* XXX should set an error code */
641	    goto ouch;
642	}
643	port = atoi(p);
644	if (port < 1 || port > 65535) {
645	    /* port number is out of range */
646	    /* XXX should set an error code */
647	    goto ouch;
648	}
649    }
650
651    if (!port) {
652#if 0
653	/*
654	 * commented out, since there is currently no service name
655	 * for HTTP proxies
656	 */
657	struct servent *se;
658
659	if ((se = getservbyname("xxxx", "tcp")) != NULL)
660	    port = ntohs(se->s_port);
661	else
662#endif
663	    port = 3128;
664    }
665    DEBUG(fprintf(stderr, "proxy port: %d\n", port));
666
667    /* connect */
668    if ((fd = _fetch_connect(hostname, port, af, verbose)) == -1)
669	_fetch_syserr();
670    return fd;
671
672 ouch:
673    if (hostname)
674	free(hostname);
675    return -1;
676}
677
678/*
679 * Connect to the correct HTTP server or proxy.
680 */
681static int
682_http_connect(struct url *URL, int *proxy, char *flags)
683{
684    int direct, verbose;
685    int af, fd;
686    char *p;
687
688#ifdef INET6
689    af = AF_UNSPEC;
690#else
691    af = AF_INET;
692#endif
693
694    direct = (flags && strchr(flags, 'd'));
695    verbose = (flags && strchr(flags, 'v'));
696    if (flags && strchr(flags, '4'))
697	af = AF_INET;
698    else if (flags && strchr(flags, '6'))
699	af = AF_INET6;
700
701    /* check port */
702    if (!URL->port) {
703	struct servent *se;
704
705	/* Scheme can be ftp if we're using a proxy */
706	if (strcasecmp(URL->scheme, "ftp") == 0)
707	    if ((se = getservbyname("ftp", "tcp")) != NULL)
708		URL->port = ntohs(se->s_port);
709	    else
710		URL->port = 21;
711	else
712	    if ((se = getservbyname("http", "tcp")) != NULL)
713		URL->port = ntohs(se->s_port);
714	    else
715		URL->port = 80;
716    }
717
718    if (!direct && (p = getenv("HTTP_PROXY")) != NULL) {
719	/* attempt to connect to proxy server */
720	if ((fd = _http_proxy_connect(p, af, verbose)) == -1)
721	    return -1;
722	*proxy = 1;
723    } else {
724	/* if no proxy is configured, try direct */
725	if (strcasecmp(URL->scheme, "ftp") == 0) {
726	    /* can't talk http to an ftp server */
727	    /* XXX should set an error code */
728	    return -1;
729	}
730	if ((fd = _fetch_connect(URL->host, URL->port, af, verbose)) == -1)
731	    /* _fetch_connect() has already set an error code */
732	    return -1;
733	*proxy = 0;
734    }
735
736    return fd;
737}
738
739
740/*****************************************************************************
741 * Core
742 */
743
744/*
745 * Send a request and process the reply
746 */
747static FILE *
748_http_request(struct url *URL, char *op, struct url_stat *us, char *flags)
749{
750    struct url *url, *new;
751    int chunked, need_auth, noredirect, proxy, verbose;
752    int code, fd, i, n;
753    off_t offset;
754    char *p;
755    FILE *f;
756    hdr h;
757    char *host;
758#ifdef INET6
759    char hbuf[MAXHOSTNAMELEN + 1];
760#endif
761
762    noredirect = (flags && strchr(flags, 'A'));
763    verbose = (flags && strchr(flags, 'v'));
764
765    n = noredirect ? 1 : MAX_REDIRECT;
766
767    /* just to appease compiler warnings */
768    code = HTTP_PROTOCOL_ERROR;
769    chunked = 0;
770    offset = 0;
771    fd = -1;
772
773    for (url = URL, i = 0; i < n; ++i) {
774	new = NULL;
775	if (us) {
776	    us->size = -1;
777	    us->atime = us->mtime = 0;
778	}
779	chunked = 0;
780	need_auth = 0;
781	offset = 0;
782    retry:
783	/* connect to server or proxy */
784	if ((fd = _http_connect(url, &proxy, flags)) == -1)
785	    goto ouch;
786
787	host = url->host;
788#ifdef INET6
789	if (strchr(url->host, ':')) {
790	    snprintf(hbuf, sizeof(hbuf), "[%s]", url->host);
791	    host = hbuf;
792	}
793#endif
794
795	/* send request */
796	if (verbose)
797	    _fetch_info("requesting %s://%s:%d%s",
798			url->scheme, host, url->port, url->doc);
799	if (proxy) {
800	    _http_cmd(fd, "%s %s://%s:%d%s HTTP/1.1",
801		      op, url->scheme, host, url->port, url->doc);
802	} else {
803	    _http_cmd(fd, "%s %s HTTP/1.1",
804		      op, url->doc);
805	}
806
807	/* proxy authorization */
808	if (proxy && (p = getenv("HTTP_PROXY_AUTH")) != NULL)
809	    _http_authorize(fd, "Proxy-Authorization", p);
810
811	/* server authorization */
812	if (need_auth) {
813	    if (*url->user || *url->pwd)
814		_http_basic_auth(fd, "Authorization",
815				 url->user ? url->user : "",
816				 url->pwd ? url->pwd : "");
817	    else if ((p = getenv("HTTP_AUTH")) != NULL)
818		_http_authorize(fd, "Authorization", p);
819	    else {
820		_http_seterr(HTTP_NEED_AUTH);
821		goto ouch;
822	    }
823	}
824
825	/* other headers */
826	_http_cmd(fd, "Host: %s:%d", host, url->port);
827	_http_cmd(fd, "User-Agent: %s " _LIBFETCH_VER, __progname);
828	if (url->offset)
829	    _http_cmd(fd, "Range: bytes=%lld-", url->offset);
830	_http_cmd(fd, "Connection: close");
831	_http_cmd(fd, "");
832
833	/* get reply */
834	switch ((code = _http_get_reply(fd))) {
835	case HTTP_OK:
836	case HTTP_PARTIAL:
837	    /* fine */
838	    break;
839	case HTTP_MOVED_PERM:
840	case HTTP_MOVED_TEMP:
841	    /*
842	     * Not so fine, but we still have to read the headers to
843	     * get the new location.
844	     */
845	    break;
846	case HTTP_NEED_AUTH:
847	    if (need_auth) {
848		/*
849		 * We already sent out authorization code, so there's
850		 * nothing more we can do.
851		 */
852		_http_seterr(code);
853		goto ouch;
854	    }
855	    /* try again, but send the password this time */
856	    if (verbose)
857		_fetch_info("server requires authorization");
858	    need_auth = 1;
859	    close(fd);
860	    goto retry;
861	case HTTP_NEED_PROXY_AUTH:
862	    /*
863	     * If we're talking to a proxy, we already sent our proxy
864	     * authorization code, so there's nothing more we can do.
865	     */
866	    _http_seterr(code);
867	    goto ouch;
868	case HTTP_PROTOCOL_ERROR:
869	    /* fall through */
870	case -1:
871	    _fetch_syserr();
872	    goto ouch;
873	default:
874	    _http_seterr(code);
875	    goto ouch;
876	}
877
878	/* get headers */
879	do {
880	    switch ((h = _http_next_header(fd, &p))) {
881	    case hdr_syserror:
882		_fetch_syserr();
883		goto ouch;
884	    case hdr_error:
885		_http_seterr(HTTP_PROTOCOL_ERROR);
886		goto ouch;
887	    case hdr_content_length:
888		if (us)
889		    us->size = _http_parse_length(p);
890		break;
891	    case hdr_content_range:
892		offset = _http_parse_range(p);
893		break;
894	    case hdr_last_modified:
895		if (us)
896		    us->atime = us->mtime = _http_parse_mtime(p);
897		break;
898	    case hdr_location:
899		if (!HTTP_REDIRECT(code))
900		    break;
901		if (new)
902		    free(new);
903		if (verbose)
904		    _fetch_info("%d redirect to %s", code, p);
905		if (*p == '/')
906		    /* absolute path */
907		    new = fetchMakeURL(url->scheme, url->host, url->port, p,
908				       url->user, url->pwd);
909		else
910		    new = fetchParseURL(p);
911		if (new == NULL) {
912		    /* XXX should set an error code */
913		    DEBUG(fprintf(stderr, "failed to parse new URL\n"));
914		    goto ouch;
915		}
916		if (!*new->user && !*new->pwd) {
917		    strcpy(new->user, url->user);
918		    strcpy(new->pwd, url->pwd);
919		}
920		new->offset = url->offset;
921		new->length = url->length;
922		break;
923	    case hdr_transfer_encoding:
924		/* XXX weak test*/
925		chunked = (strcasecmp(p, "chunked") == 0);
926		break;
927	    case hdr_end:
928		/* fall through */
929	    case hdr_unknown:
930		/* ignore */
931		break;
932	    }
933	} while (h > hdr_end);
934
935	/* we either have a hit, or a redirect with no Location: header */
936	if (code == HTTP_OK || code == HTTP_PARTIAL || !new)
937	    break;
938
939	/* we have a redirect */
940	close(fd);
941	fd = -1;
942	if (url != URL)
943	    fetchFreeURL(url);
944	url = new;
945    }
946
947    /* no success */
948    if (fd == -1) {
949	_http_seterr(code);
950	goto ouch;
951    }
952
953    /* too far? */
954    if (offset > url->offset) {
955	_http_seterr(HTTP_PROTOCOL_ERROR);
956	goto ouch;
957    }
958
959    /* report back real offset */
960    URL->offset = offset;
961
962    /* wrap it up in a FILE */
963    if ((f = chunked ? _http_funopen(fd) : fdopen(fd, "r")) == NULL) {
964	_fetch_syserr();
965	goto ouch;
966    }
967
968    if (url != URL)
969	fetchFreeURL(url);
970
971    return f;
972
973 ouch:
974    if (url != URL)
975	fetchFreeURL(url);
976    if (fd != -1)
977	close(fd);
978    return NULL;
979}
980
981
982/*****************************************************************************
983 * Entry points
984 */
985
986/*
987 * Retrieve and stat a file by HTTP
988 */
989FILE *
990fetchXGetHTTP(struct url *URL, struct url_stat *us, char *flags)
991{
992    return _http_request(URL, "GET", us, flags);
993}
994
995/*
996 * Retrieve a file by HTTP
997 */
998FILE *
999fetchGetHTTP(struct url *URL, char *flags)
1000{
1001    return fetchXGetHTTP(URL, NULL, flags);
1002}
1003
1004/*
1005 * Store a file by HTTP
1006 */
1007FILE *
1008fetchPutHTTP(struct url *URL, char *flags)
1009{
1010    warnx("fetchPutHTTP(): not implemented");
1011    return NULL;
1012}
1013
1014/*
1015 * Get an HTTP document's metadata
1016 */
1017int
1018fetchStatHTTP(struct url *URL, struct url_stat *us, char *flags)
1019{
1020    FILE *f;
1021
1022    if ((f = _http_request(URL, "HEAD", us, flags)) == NULL)
1023	return -1;
1024    fclose(f);
1025    return 0;
1026}
1027
1028/*
1029 * List a directory
1030 */
1031struct url_ent *
1032fetchListHTTP(struct url *url, char *flags)
1033{
1034    warnx("fetchListHTTP(): not implemented");
1035    return NULL;
1036}
1037