xref: /freebsd/lib/libfetch/http.c (revision 282a3889ebf826db9839be296ff1dd903f6d6d6e)
1 /*-
2  * Copyright (c) 2000-2004 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 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 /*
33  * The following copyright applies to the base64 code:
34  *
35  *-
36  * Copyright 1997 Massachusetts Institute of Technology
37  *
38  * Permission to use, copy, modify, and distribute this software and
39  * its documentation for any purpose and without fee is hereby
40  * granted, provided that both the above copyright notice and this
41  * permission notice appear in all copies, that both the above
42  * copyright notice and this permission notice appear in all
43  * supporting documentation, and that the name of M.I.T. not be used
44  * in advertising or publicity pertaining to distribution of the
45  * software without specific, written prior permission.  M.I.T. makes
46  * no representations about the suitability of this software for any
47  * purpose.  It is provided "as is" without express or implied
48  * warranty.
49  *
50  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
51  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
52  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
53  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
54  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
57  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
58  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
59  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
60  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61  * SUCH DAMAGE.
62  */
63 
64 #include <sys/param.h>
65 #include <sys/socket.h>
66 
67 #include <ctype.h>
68 #include <err.h>
69 #include <errno.h>
70 #include <locale.h>
71 #include <netdb.h>
72 #include <stdarg.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <string.h>
76 #include <time.h>
77 #include <unistd.h>
78 
79 #include <netinet/in.h>
80 #include <netinet/tcp.h>
81 
82 #include "fetch.h"
83 #include "common.h"
84 #include "httperr.h"
85 
86 /* Maximum number of redirects to follow */
87 #define MAX_REDIRECT 5
88 
89 /* Symbolic names for reply codes we care about */
90 #define HTTP_OK			200
91 #define HTTP_PARTIAL		206
92 #define HTTP_MOVED_PERM		301
93 #define HTTP_MOVED_TEMP		302
94 #define HTTP_SEE_OTHER		303
95 #define HTTP_TEMP_REDIRECT	307
96 #define HTTP_NEED_AUTH		401
97 #define HTTP_NEED_PROXY_AUTH	407
98 #define HTTP_BAD_RANGE		416
99 #define HTTP_PROTOCOL_ERROR	999
100 
101 #define HTTP_REDIRECT(xyz) ((xyz) == HTTP_MOVED_PERM \
102 			    || (xyz) == HTTP_MOVED_TEMP \
103 			    || (xyz) == HTTP_TEMP_REDIRECT \
104 			    || (xyz) == HTTP_SEE_OTHER)
105 
106 #define HTTP_ERROR(xyz) ((xyz) > 400 && (xyz) < 599)
107 
108 
109 /*****************************************************************************
110  * I/O functions for decoding chunked streams
111  */
112 
113 struct httpio
114 {
115 	conn_t		*conn;		/* connection */
116 	int		 chunked;	/* chunked mode */
117 	char		*buf;		/* chunk buffer */
118 	size_t		 bufsize;	/* size of chunk buffer */
119 	ssize_t		 buflen;	/* amount of data currently in buffer */
120 	int		 bufpos;	/* current read offset in buffer */
121 	int		 eof;		/* end-of-file flag */
122 	int		 error;		/* error flag */
123 	size_t		 chunksize;	/* remaining size of current chunk */
124 #ifndef NDEBUG
125 	size_t		 total;
126 #endif
127 };
128 
129 /*
130  * Get next chunk header
131  */
132 static int
133 _http_new_chunk(struct httpio *io)
134 {
135 	char *p;
136 
137 	if (_fetch_getln(io->conn) == -1)
138 		return (-1);
139 
140 	if (io->conn->buflen < 2 || !ishexnumber(*io->conn->buf))
141 		return (-1);
142 
143 	for (p = io->conn->buf; *p && !isspace(*p); ++p) {
144 		if (*p == ';')
145 			break;
146 		if (!ishexnumber(*p))
147 			return (-1);
148 		if (isdigit(*p)) {
149 			io->chunksize = io->chunksize * 16 +
150 			    *p - '0';
151 		} else {
152 			io->chunksize = io->chunksize * 16 +
153 			    10 + tolower(*p) - 'a';
154 		}
155 	}
156 
157 #ifndef NDEBUG
158 	if (fetchDebug) {
159 		io->total += io->chunksize;
160 		if (io->chunksize == 0)
161 			fprintf(stderr, "%s(): end of last chunk\n", __func__);
162 		else
163 			fprintf(stderr, "%s(): new chunk: %lu (%lu)\n",
164 			    __func__, (unsigned long)io->chunksize,
165 			    (unsigned long)io->total);
166 	}
167 #endif
168 
169 	return (io->chunksize);
170 }
171 
172 /*
173  * Grow the input buffer to at least len bytes
174  */
175 static inline int
176 _http_growbuf(struct httpio *io, size_t len)
177 {
178 	char *tmp;
179 
180 	if (io->bufsize >= len)
181 		return (0);
182 
183 	if ((tmp = realloc(io->buf, len)) == NULL)
184 		return (-1);
185 	io->buf = tmp;
186 	io->bufsize = len;
187 	return (0);
188 }
189 
190 /*
191  * Fill the input buffer, do chunk decoding on the fly
192  */
193 static int
194 _http_fillbuf(struct httpio *io, size_t len)
195 {
196 	if (io->error)
197 		return (-1);
198 	if (io->eof)
199 		return (0);
200 
201 	if (io->chunked == 0) {
202 		if (_http_growbuf(io, len) == -1)
203 			return (-1);
204 		if ((io->buflen = _fetch_read(io->conn, io->buf, len)) == -1) {
205 			io->error = 1;
206 			return (-1);
207 		}
208 		io->bufpos = 0;
209 		return (io->buflen);
210 	}
211 
212 	if (io->chunksize == 0) {
213 		switch (_http_new_chunk(io)) {
214 		case -1:
215 			io->error = 1;
216 			return (-1);
217 		case 0:
218 			io->eof = 1;
219 			return (0);
220 		}
221 	}
222 
223 	if (len > io->chunksize)
224 		len = io->chunksize;
225 	if (_http_growbuf(io, len) == -1)
226 		return (-1);
227 	if ((io->buflen = _fetch_read(io->conn, io->buf, len)) == -1) {
228 		io->error = 1;
229 		return (-1);
230 	}
231 	io->chunksize -= io->buflen;
232 
233 	if (io->chunksize == 0) {
234 		char endl[2];
235 
236 		if (_fetch_read(io->conn, endl, 2) != 2 ||
237 		    endl[0] != '\r' || endl[1] != '\n')
238 			return (-1);
239 	}
240 
241 	io->bufpos = 0;
242 
243 	return (io->buflen);
244 }
245 
246 /*
247  * Read function
248  */
249 static int
250 _http_readfn(void *v, char *buf, int len)
251 {
252 	struct httpio *io = (struct httpio *)v;
253 	int l, pos;
254 
255 	if (io->error)
256 		return (-1);
257 	if (io->eof)
258 		return (0);
259 
260 	for (pos = 0; len > 0; pos += l, len -= l) {
261 		/* empty buffer */
262 		if (!io->buf || io->bufpos == io->buflen)
263 			if (_http_fillbuf(io, len) < 1)
264 				break;
265 		l = io->buflen - io->bufpos;
266 		if (len < l)
267 			l = len;
268 		bcopy(io->buf + io->bufpos, buf + pos, l);
269 		io->bufpos += l;
270 	}
271 
272 	if (!pos && io->error)
273 		return (-1);
274 	return (pos);
275 }
276 
277 /*
278  * Write function
279  */
280 static int
281 _http_writefn(void *v, const char *buf, int len)
282 {
283 	struct httpio *io = (struct httpio *)v;
284 
285 	return (_fetch_write(io->conn, buf, len));
286 }
287 
288 /*
289  * Close function
290  */
291 static int
292 _http_closefn(void *v)
293 {
294 	struct httpio *io = (struct httpio *)v;
295 	int r;
296 
297 	r = _fetch_close(io->conn);
298 	if (io->buf)
299 		free(io->buf);
300 	free(io);
301 	return (r);
302 }
303 
304 /*
305  * Wrap a file descriptor up
306  */
307 static FILE *
308 _http_funopen(conn_t *conn, int chunked)
309 {
310 	struct httpio *io;
311 	FILE *f;
312 
313 	if ((io = calloc(1, sizeof(*io))) == NULL) {
314 		_fetch_syserr();
315 		return (NULL);
316 	}
317 	io->conn = conn;
318 	io->chunked = chunked;
319 	f = funopen(io, _http_readfn, _http_writefn, NULL, _http_closefn);
320 	if (f == NULL) {
321 		_fetch_syserr();
322 		free(io);
323 		return (NULL);
324 	}
325 	return (f);
326 }
327 
328 
329 /*****************************************************************************
330  * Helper functions for talking to the server and parsing its replies
331  */
332 
333 /* Header types */
334 typedef enum {
335 	hdr_syserror = -2,
336 	hdr_error = -1,
337 	hdr_end = 0,
338 	hdr_unknown = 1,
339 	hdr_content_length,
340 	hdr_content_range,
341 	hdr_last_modified,
342 	hdr_location,
343 	hdr_transfer_encoding,
344 	hdr_www_authenticate
345 } hdr_t;
346 
347 /* Names of interesting headers */
348 static struct {
349 	hdr_t		 num;
350 	const char	*name;
351 } hdr_names[] = {
352 	{ hdr_content_length,		"Content-Length" },
353 	{ hdr_content_range,		"Content-Range" },
354 	{ hdr_last_modified,		"Last-Modified" },
355 	{ hdr_location,			"Location" },
356 	{ hdr_transfer_encoding,	"Transfer-Encoding" },
357 	{ hdr_www_authenticate,		"WWW-Authenticate" },
358 	{ hdr_unknown,			NULL },
359 };
360 
361 /*
362  * Send a formatted line; optionally echo to terminal
363  */
364 static int
365 _http_cmd(conn_t *conn, const char *fmt, ...)
366 {
367 	va_list ap;
368 	size_t len;
369 	char *msg;
370 	int r;
371 
372 	va_start(ap, fmt);
373 	len = vasprintf(&msg, fmt, ap);
374 	va_end(ap);
375 
376 	if (msg == NULL) {
377 		errno = ENOMEM;
378 		_fetch_syserr();
379 		return (-1);
380 	}
381 
382 	r = _fetch_putln(conn, msg, len);
383 	free(msg);
384 
385 	if (r == -1) {
386 		_fetch_syserr();
387 		return (-1);
388 	}
389 
390 	return (0);
391 }
392 
393 /*
394  * Get and parse status line
395  */
396 static int
397 _http_get_reply(conn_t *conn)
398 {
399 	char *p;
400 
401 	if (_fetch_getln(conn) == -1)
402 		return (-1);
403 	/*
404 	 * A valid status line looks like "HTTP/m.n xyz reason" where m
405 	 * and n are the major and minor protocol version numbers and xyz
406 	 * is the reply code.
407 	 * Unfortunately, there are servers out there (NCSA 1.5.1, to name
408 	 * just one) that do not send a version number, so we can't rely
409 	 * on finding one, but if we do, insist on it being 1.0 or 1.1.
410 	 * We don't care about the reason phrase.
411 	 */
412 	if (strncmp(conn->buf, "HTTP", 4) != 0)
413 		return (HTTP_PROTOCOL_ERROR);
414 	p = conn->buf + 4;
415 	if (*p == '/') {
416 		if (p[1] != '1' || p[2] != '.' || (p[3] != '0' && p[3] != '1'))
417 			return (HTTP_PROTOCOL_ERROR);
418 		p += 4;
419 	}
420 	if (*p != ' ' || !isdigit(p[1]) || !isdigit(p[2]) || !isdigit(p[3]))
421 		return (HTTP_PROTOCOL_ERROR);
422 
423 	conn->err = (p[1] - '0') * 100 + (p[2] - '0') * 10 + (p[3] - '0');
424 	return (conn->err);
425 }
426 
427 /*
428  * Check a header; if the type matches the given string, return a pointer
429  * to the beginning of the value.
430  */
431 static const char *
432 _http_match(const char *str, const char *hdr)
433 {
434 	while (*str && *hdr && tolower(*str++) == tolower(*hdr++))
435 		/* nothing */;
436 	if (*str || *hdr != ':')
437 		return (NULL);
438 	while (*hdr && isspace(*++hdr))
439 		/* nothing */;
440 	return (hdr);
441 }
442 
443 /*
444  * Get the next header and return the appropriate symbolic code.
445  */
446 static hdr_t
447 _http_next_header(conn_t *conn, const char **p)
448 {
449 	int i;
450 
451 	if (_fetch_getln(conn) == -1)
452 		return (hdr_syserror);
453 	while (conn->buflen && isspace(conn->buf[conn->buflen - 1]))
454 		conn->buflen--;
455 	conn->buf[conn->buflen] = '\0';
456 	if (conn->buflen == 0)
457 		return (hdr_end);
458 	/*
459 	 * We could check for malformed headers but we don't really care.
460 	 * A valid header starts with a token immediately followed by a
461 	 * colon; a token is any sequence of non-control, non-whitespace
462 	 * characters except "()<>@,;:\\\"{}".
463 	 */
464 	for (i = 0; hdr_names[i].num != hdr_unknown; i++)
465 		if ((*p = _http_match(hdr_names[i].name, conn->buf)) != NULL)
466 			return (hdr_names[i].num);
467 	return (hdr_unknown);
468 }
469 
470 /*
471  * Parse a last-modified header
472  */
473 static int
474 _http_parse_mtime(const char *p, time_t *mtime)
475 {
476 	char locale[64], *r;
477 	struct tm tm;
478 
479 	strncpy(locale, setlocale(LC_TIME, NULL), sizeof(locale));
480 	setlocale(LC_TIME, "C");
481 	r = strptime(p, "%a, %d %b %Y %H:%M:%S GMT", &tm);
482 	/* XXX should add support for date-2 and date-3 */
483 	setlocale(LC_TIME, locale);
484 	if (r == NULL)
485 		return (-1);
486 	DEBUG(fprintf(stderr, "last modified: [%04d-%02d-%02d "
487 		  "%02d:%02d:%02d]\n",
488 		  tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
489 		  tm.tm_hour, tm.tm_min, tm.tm_sec));
490 	*mtime = timegm(&tm);
491 	return (0);
492 }
493 
494 /*
495  * Parse a content-length header
496  */
497 static int
498 _http_parse_length(const char *p, off_t *length)
499 {
500 	off_t len;
501 
502 	for (len = 0; *p && isdigit(*p); ++p)
503 		len = len * 10 + (*p - '0');
504 	if (*p)
505 		return (-1);
506 	DEBUG(fprintf(stderr, "content length: [%lld]\n",
507 	    (long long)len));
508 	*length = len;
509 	return (0);
510 }
511 
512 /*
513  * Parse a content-range header
514  */
515 static int
516 _http_parse_range(const char *p, off_t *offset, off_t *length, off_t *size)
517 {
518 	off_t first, last, len;
519 
520 	if (strncasecmp(p, "bytes ", 6) != 0)
521 		return (-1);
522 	p += 6;
523 	if (*p == '*') {
524 		first = last = -1;
525 		++p;
526 	} else {
527 		for (first = 0; *p && isdigit(*p); ++p)
528 			first = first * 10 + *p - '0';
529 		if (*p != '-')
530 			return (-1);
531 		for (last = 0, ++p; *p && isdigit(*p); ++p)
532 			last = last * 10 + *p - '0';
533 	}
534 	if (first > last || *p != '/')
535 		return (-1);
536 	for (len = 0, ++p; *p && isdigit(*p); ++p)
537 		len = len * 10 + *p - '0';
538 	if (*p || len < last - first + 1)
539 		return (-1);
540 	if (first == -1) {
541 		DEBUG(fprintf(stderr, "content range: [*/%lld]\n",
542 		    (long long)len));
543 		*length = 0;
544 	} else {
545 		DEBUG(fprintf(stderr, "content range: [%lld-%lld/%lld]\n",
546 		    (long long)first, (long long)last, (long long)len));
547 		*length = last - first + 1;
548 	}
549 	*offset = first;
550 	*size = len;
551 	return (0);
552 }
553 
554 
555 /*****************************************************************************
556  * Helper functions for authorization
557  */
558 
559 /*
560  * Base64 encoding
561  */
562 static char *
563 _http_base64(const char *src)
564 {
565 	static const char base64[] =
566 	    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
567 	    "abcdefghijklmnopqrstuvwxyz"
568 	    "0123456789+/";
569 	char *str, *dst;
570 	size_t l;
571 	int t, r;
572 
573 	l = strlen(src);
574 	if ((str = malloc(((l + 2) / 3) * 4 + 1)) == NULL)
575 		return (NULL);
576 	dst = str;
577 	r = 0;
578 
579 	while (l >= 3) {
580 		t = (src[0] << 16) | (src[1] << 8) | src[2];
581 		dst[0] = base64[(t >> 18) & 0x3f];
582 		dst[1] = base64[(t >> 12) & 0x3f];
583 		dst[2] = base64[(t >> 6) & 0x3f];
584 		dst[3] = base64[(t >> 0) & 0x3f];
585 		src += 3; l -= 3;
586 		dst += 4; r += 4;
587 	}
588 
589 	switch (l) {
590 	case 2:
591 		t = (src[0] << 16) | (src[1] << 8);
592 		dst[0] = base64[(t >> 18) & 0x3f];
593 		dst[1] = base64[(t >> 12) & 0x3f];
594 		dst[2] = base64[(t >> 6) & 0x3f];
595 		dst[3] = '=';
596 		dst += 4;
597 		r += 4;
598 		break;
599 	case 1:
600 		t = src[0] << 16;
601 		dst[0] = base64[(t >> 18) & 0x3f];
602 		dst[1] = base64[(t >> 12) & 0x3f];
603 		dst[2] = dst[3] = '=';
604 		dst += 4;
605 		r += 4;
606 		break;
607 	case 0:
608 		break;
609 	}
610 
611 	*dst = 0;
612 	return (str);
613 }
614 
615 /*
616  * Encode username and password
617  */
618 static int
619 _http_basic_auth(conn_t *conn, const char *hdr, const char *usr, const char *pwd)
620 {
621 	char *upw, *auth;
622 	int r;
623 
624 	DEBUG(fprintf(stderr, "usr: [%s]\n", usr));
625 	DEBUG(fprintf(stderr, "pwd: [%s]\n", pwd));
626 	if (asprintf(&upw, "%s:%s", usr, pwd) == -1)
627 		return (-1);
628 	auth = _http_base64(upw);
629 	free(upw);
630 	if (auth == NULL)
631 		return (-1);
632 	r = _http_cmd(conn, "%s: Basic %s", hdr, auth);
633 	free(auth);
634 	return (r);
635 }
636 
637 /*
638  * Send an authorization header
639  */
640 static int
641 _http_authorize(conn_t *conn, const char *hdr, const char *p)
642 {
643 	/* basic authorization */
644 	if (strncasecmp(p, "basic:", 6) == 0) {
645 		char *user, *pwd, *str;
646 		int r;
647 
648 		/* skip realm */
649 		for (p += 6; *p && *p != ':'; ++p)
650 			/* nothing */ ;
651 		if (!*p || strchr(++p, ':') == NULL)
652 			return (-1);
653 		if ((str = strdup(p)) == NULL)
654 			return (-1); /* XXX */
655 		user = str;
656 		pwd = strchr(str, ':');
657 		*pwd++ = '\0';
658 		r = _http_basic_auth(conn, hdr, user, pwd);
659 		free(str);
660 		return (r);
661 	}
662 	return (-1);
663 }
664 
665 
666 /*****************************************************************************
667  * Helper functions for connecting to a server or proxy
668  */
669 
670 /*
671  * Connect to the correct HTTP server or proxy.
672  */
673 static conn_t *
674 _http_connect(struct url *URL, struct url *purl, const char *flags)
675 {
676 	conn_t *conn;
677 	int verbose;
678 	int af, val;
679 
680 #ifdef INET6
681 	af = AF_UNSPEC;
682 #else
683 	af = AF_INET;
684 #endif
685 
686 	verbose = CHECK_FLAG('v');
687 	if (CHECK_FLAG('4'))
688 		af = AF_INET;
689 #ifdef INET6
690 	else if (CHECK_FLAG('6'))
691 		af = AF_INET6;
692 #endif
693 
694 	if (purl && strcasecmp(URL->scheme, SCHEME_HTTPS) != 0) {
695 		URL = purl;
696 	} else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0) {
697 		/* can't talk http to an ftp server */
698 		/* XXX should set an error code */
699 		return (NULL);
700 	}
701 
702 	if ((conn = _fetch_connect(URL->host, URL->port, af, verbose)) == NULL)
703 		/* _fetch_connect() has already set an error code */
704 		return (NULL);
705 	if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0 &&
706 	    _fetch_ssl(conn, verbose) == -1) {
707 		_fetch_close(conn);
708 		/* grrr */
709 		errno = EAUTH;
710 		_fetch_syserr();
711 		return (NULL);
712 	}
713 
714 	val = 1;
715 	setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val, sizeof(val));
716 
717 	return (conn);
718 }
719 
720 static struct url *
721 _http_get_proxy(const char *flags)
722 {
723 	struct url *purl;
724 	char *p;
725 
726 	if (flags != NULL && strchr(flags, 'd') != NULL)
727 		return (NULL);
728 	if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
729 	    *p && (purl = fetchParseURL(p))) {
730 		if (!*purl->scheme)
731 			strcpy(purl->scheme, SCHEME_HTTP);
732 		if (!purl->port)
733 			purl->port = _fetch_default_proxy_port(purl->scheme);
734 		if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
735 			return (purl);
736 		fetchFreeURL(purl);
737 	}
738 	return (NULL);
739 }
740 
741 static void
742 _http_print_html(FILE *out, FILE *in)
743 {
744 	size_t len;
745 	char *line, *p, *q;
746 	int comment, tag;
747 
748 	comment = tag = 0;
749 	while ((line = fgetln(in, &len)) != NULL) {
750 		while (len && isspace(line[len - 1]))
751 			--len;
752 		for (p = q = line; q < line + len; ++q) {
753 			if (comment && *q == '-') {
754 				if (q + 2 < line + len &&
755 				    strcmp(q, "-->") == 0) {
756 					tag = comment = 0;
757 					q += 2;
758 				}
759 			} else if (tag && !comment && *q == '>') {
760 				p = q + 1;
761 				tag = 0;
762 			} else if (!tag && *q == '<') {
763 				if (q > p)
764 					fwrite(p, q - p, 1, out);
765 				tag = 1;
766 				if (q + 3 < line + len &&
767 				    strcmp(q, "<!--") == 0) {
768 					comment = 1;
769 					q += 3;
770 				}
771 			}
772 		}
773 		if (!tag && q > p)
774 			fwrite(p, q - p, 1, out);
775 		fputc('\n', out);
776 	}
777 }
778 
779 
780 /*****************************************************************************
781  * Core
782  */
783 
784 /*
785  * Send a request and process the reply
786  *
787  * XXX This function is way too long, the do..while loop should be split
788  * XXX off into a separate function.
789  */
790 FILE *
791 _http_request(struct url *URL, const char *op, struct url_stat *us,
792     struct url *purl, const char *flags)
793 {
794 	conn_t *conn;
795 	struct url *url, *new;
796 	int chunked, direct, need_auth, noredirect, verbose;
797 	int e, i, n, val;
798 	off_t offset, clength, length, size;
799 	time_t mtime;
800 	const char *p;
801 	FILE *f;
802 	hdr_t h;
803 	char hbuf[MAXHOSTNAMELEN + 7], *host;
804 
805 	direct = CHECK_FLAG('d');
806 	noredirect = CHECK_FLAG('A');
807 	verbose = CHECK_FLAG('v');
808 
809 	if (direct && purl) {
810 		fetchFreeURL(purl);
811 		purl = NULL;
812 	}
813 
814 	/* try the provided URL first */
815 	url = URL;
816 
817 	/* if the A flag is set, we only get one try */
818 	n = noredirect ? 1 : MAX_REDIRECT;
819 	i = 0;
820 
821 	e = HTTP_PROTOCOL_ERROR;
822 	need_auth = 0;
823 	do {
824 		new = NULL;
825 		chunked = 0;
826 		offset = 0;
827 		clength = -1;
828 		length = -1;
829 		size = -1;
830 		mtime = 0;
831 
832 		/* check port */
833 		if (!url->port)
834 			url->port = _fetch_default_port(url->scheme);
835 
836 		/* were we redirected to an FTP URL? */
837 		if (purl == NULL && strcmp(url->scheme, SCHEME_FTP) == 0) {
838 			if (strcmp(op, "GET") == 0)
839 				return (_ftp_request(url, "RETR", us, purl, flags));
840 			else if (strcmp(op, "HEAD") == 0)
841 				return (_ftp_request(url, "STAT", us, purl, flags));
842 		}
843 
844 		/* connect to server or proxy */
845 		if ((conn = _http_connect(url, purl, flags)) == NULL)
846 			goto ouch;
847 
848 		host = url->host;
849 #ifdef INET6
850 		if (strchr(url->host, ':')) {
851 			snprintf(hbuf, sizeof(hbuf), "[%s]", url->host);
852 			host = hbuf;
853 		}
854 #endif
855 		if (url->port != _fetch_default_port(url->scheme)) {
856 			if (host != hbuf) {
857 				strcpy(hbuf, host);
858 				host = hbuf;
859 			}
860 			snprintf(hbuf + strlen(hbuf),
861 			    sizeof(hbuf) - strlen(hbuf), ":%d", url->port);
862 		}
863 
864 		/* send request */
865 		if (verbose)
866 			_fetch_info("requesting %s://%s%s",
867 			    url->scheme, host, url->doc);
868 		if (purl) {
869 			_http_cmd(conn, "%s %s://%s%s HTTP/1.1",
870 			    op, url->scheme, host, url->doc);
871 		} else {
872 			_http_cmd(conn, "%s %s HTTP/1.1",
873 			    op, url->doc);
874 		}
875 
876 		/* virtual host */
877 		_http_cmd(conn, "Host: %s", host);
878 
879 		/* proxy authorization */
880 		if (purl) {
881 			if (*purl->user || *purl->pwd)
882 				_http_basic_auth(conn, "Proxy-Authorization",
883 				    purl->user, purl->pwd);
884 			else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL && *p != '\0')
885 				_http_authorize(conn, "Proxy-Authorization", p);
886 		}
887 
888 		/* server authorization */
889 		if (need_auth || *url->user || *url->pwd) {
890 			if (*url->user || *url->pwd)
891 				_http_basic_auth(conn, "Authorization", url->user, url->pwd);
892 			else if ((p = getenv("HTTP_AUTH")) != NULL && *p != '\0')
893 				_http_authorize(conn, "Authorization", p);
894 			else if (fetchAuthMethod && fetchAuthMethod(url) == 0) {
895 				_http_basic_auth(conn, "Authorization", url->user, url->pwd);
896 			} else {
897 				_http_seterr(HTTP_NEED_AUTH);
898 				goto ouch;
899 			}
900 		}
901 
902 		/* other headers */
903 		if ((p = getenv("HTTP_REFERER")) != NULL && *p != '\0') {
904 			if (strcasecmp(p, "auto") == 0)
905 				_http_cmd(conn, "Referer: %s://%s%s",
906 				    url->scheme, host, url->doc);
907 			else
908 				_http_cmd(conn, "Referer: %s", p);
909 		}
910 		if ((p = getenv("HTTP_USER_AGENT")) != NULL && *p != '\0')
911 			_http_cmd(conn, "User-Agent: %s", p);
912 		else
913 			_http_cmd(conn, "User-Agent: %s " _LIBFETCH_VER, getprogname());
914 		if (url->offset > 0)
915 			_http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset);
916 		_http_cmd(conn, "Connection: close");
917 		_http_cmd(conn, "");
918 
919 		/*
920 		 * Force the queued request to be dispatched.  Normally, one
921 		 * would do this with shutdown(2) but squid proxies can be
922 		 * configured to disallow such half-closed connections.  To
923 		 * be compatible with such configurations, fiddle with socket
924 		 * options to force the pending data to be written.
925 		 */
926 		val = 0;
927 		setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val,
928 			   sizeof(val));
929 		val = 1;
930 		setsockopt(conn->sd, IPPROTO_TCP, TCP_NODELAY, &val,
931 			   sizeof(val));
932 
933 		/* get reply */
934 		switch (_http_get_reply(conn)) {
935 		case HTTP_OK:
936 		case HTTP_PARTIAL:
937 			/* fine */
938 			break;
939 		case HTTP_MOVED_PERM:
940 		case HTTP_MOVED_TEMP:
941 		case HTTP_SEE_OTHER:
942 			/*
943 			 * Not so fine, but we still have to read the
944 			 * headers to get the new location.
945 			 */
946 			break;
947 		case HTTP_NEED_AUTH:
948 			if (need_auth) {
949 				/*
950 				 * We already sent out authorization code,
951 				 * so there's nothing more we can do.
952 				 */
953 				_http_seterr(conn->err);
954 				goto ouch;
955 			}
956 			/* try again, but send the password this time */
957 			if (verbose)
958 				_fetch_info("server requires authorization");
959 			break;
960 		case HTTP_NEED_PROXY_AUTH:
961 			/*
962 			 * If we're talking to a proxy, we already sent
963 			 * our proxy authorization code, so there's
964 			 * nothing more we can do.
965 			 */
966 			_http_seterr(conn->err);
967 			goto ouch;
968 		case HTTP_BAD_RANGE:
969 			/*
970 			 * This can happen if we ask for 0 bytes because
971 			 * we already have the whole file.  Consider this
972 			 * a success for now, and check sizes later.
973 			 */
974 			break;
975 		case HTTP_PROTOCOL_ERROR:
976 			/* fall through */
977 		case -1:
978 			_fetch_syserr();
979 			goto ouch;
980 		default:
981 			_http_seterr(conn->err);
982 			if (!verbose)
983 				goto ouch;
984 			/* fall through so we can get the full error message */
985 		}
986 
987 		/* get headers */
988 		do {
989 			switch ((h = _http_next_header(conn, &p))) {
990 			case hdr_syserror:
991 				_fetch_syserr();
992 				goto ouch;
993 			case hdr_error:
994 				_http_seterr(HTTP_PROTOCOL_ERROR);
995 				goto ouch;
996 			case hdr_content_length:
997 				_http_parse_length(p, &clength);
998 				break;
999 			case hdr_content_range:
1000 				_http_parse_range(p, &offset, &length, &size);
1001 				break;
1002 			case hdr_last_modified:
1003 				_http_parse_mtime(p, &mtime);
1004 				break;
1005 			case hdr_location:
1006 				if (!HTTP_REDIRECT(conn->err))
1007 					break;
1008 				if (new)
1009 					free(new);
1010 				if (verbose)
1011 					_fetch_info("%d redirect to %s", conn->err, p);
1012 				if (*p == '/')
1013 					/* absolute path */
1014 					new = fetchMakeURL(url->scheme, url->host, url->port, p,
1015 					    url->user, url->pwd);
1016 				else
1017 					new = fetchParseURL(p);
1018 				if (new == NULL) {
1019 					/* XXX should set an error code */
1020 					DEBUG(fprintf(stderr, "failed to parse new URL\n"));
1021 					goto ouch;
1022 				}
1023 				if (!*new->user && !*new->pwd) {
1024 					strcpy(new->user, url->user);
1025 					strcpy(new->pwd, url->pwd);
1026 				}
1027 				new->offset = url->offset;
1028 				new->length = url->length;
1029 				break;
1030 			case hdr_transfer_encoding:
1031 				/* XXX weak test*/
1032 				chunked = (strcasecmp(p, "chunked") == 0);
1033 				break;
1034 			case hdr_www_authenticate:
1035 				if (conn->err != HTTP_NEED_AUTH)
1036 					break;
1037 				/* if we were smarter, we'd check the method and realm */
1038 				break;
1039 			case hdr_end:
1040 				/* fall through */
1041 			case hdr_unknown:
1042 				/* ignore */
1043 				break;
1044 			}
1045 		} while (h > hdr_end);
1046 
1047 		/* we need to provide authentication */
1048 		if (conn->err == HTTP_NEED_AUTH) {
1049 			e = conn->err;
1050 			need_auth = 1;
1051 			_fetch_close(conn);
1052 			conn = NULL;
1053 			continue;
1054 		}
1055 
1056 		/* requested range not satisfiable */
1057 		if (conn->err == HTTP_BAD_RANGE) {
1058 			if (url->offset == size && url->length == 0) {
1059 				/* asked for 0 bytes; fake it */
1060 				offset = url->offset;
1061 				conn->err = HTTP_OK;
1062 				break;
1063 			} else {
1064 				_http_seterr(conn->err);
1065 				goto ouch;
1066 			}
1067 		}
1068 
1069 		/* we have a hit or an error */
1070 		if (conn->err == HTTP_OK || conn->err == HTTP_PARTIAL || HTTP_ERROR(conn->err))
1071 			break;
1072 
1073 		/* all other cases: we got a redirect */
1074 		e = conn->err;
1075 		need_auth = 0;
1076 		_fetch_close(conn);
1077 		conn = NULL;
1078 		if (!new) {
1079 			DEBUG(fprintf(stderr, "redirect with no new location\n"));
1080 			break;
1081 		}
1082 		if (url != URL)
1083 			fetchFreeURL(url);
1084 		url = new;
1085 	} while (++i < n);
1086 
1087 	/* we failed, or ran out of retries */
1088 	if (conn == NULL) {
1089 		_http_seterr(e);
1090 		goto ouch;
1091 	}
1092 
1093 	DEBUG(fprintf(stderr, "offset %lld, length %lld,"
1094 		  " size %lld, clength %lld\n",
1095 		  (long long)offset, (long long)length,
1096 		  (long long)size, (long long)clength));
1097 
1098 	/* check for inconsistencies */
1099 	if (clength != -1 && length != -1 && clength != length) {
1100 		_http_seterr(HTTP_PROTOCOL_ERROR);
1101 		goto ouch;
1102 	}
1103 	if (clength == -1)
1104 		clength = length;
1105 	if (clength != -1)
1106 		length = offset + clength;
1107 	if (length != -1 && size != -1 && length != size) {
1108 		_http_seterr(HTTP_PROTOCOL_ERROR);
1109 		goto ouch;
1110 	}
1111 	if (size == -1)
1112 		size = length;
1113 
1114 	/* fill in stats */
1115 	if (us) {
1116 		us->size = size;
1117 		us->atime = us->mtime = mtime;
1118 	}
1119 
1120 	/* too far? */
1121 	if (URL->offset > 0 && offset > URL->offset) {
1122 		_http_seterr(HTTP_PROTOCOL_ERROR);
1123 		goto ouch;
1124 	}
1125 
1126 	/* report back real offset and size */
1127 	URL->offset = offset;
1128 	URL->length = clength;
1129 
1130 	/* wrap it up in a FILE */
1131 	if ((f = _http_funopen(conn, chunked)) == NULL) {
1132 		_fetch_syserr();
1133 		goto ouch;
1134 	}
1135 
1136 	if (url != URL)
1137 		fetchFreeURL(url);
1138 	if (purl)
1139 		fetchFreeURL(purl);
1140 
1141 	if (HTTP_ERROR(conn->err)) {
1142 		_http_print_html(stderr, f);
1143 		fclose(f);
1144 		f = NULL;
1145 	}
1146 
1147 	return (f);
1148 
1149 ouch:
1150 	if (url != URL)
1151 		fetchFreeURL(url);
1152 	if (purl)
1153 		fetchFreeURL(purl);
1154 	if (conn != NULL)
1155 		_fetch_close(conn);
1156 	return (NULL);
1157 }
1158 
1159 
1160 /*****************************************************************************
1161  * Entry points
1162  */
1163 
1164 /*
1165  * Retrieve and stat a file by HTTP
1166  */
1167 FILE *
1168 fetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags)
1169 {
1170 	return (_http_request(URL, "GET", us, _http_get_proxy(flags), flags));
1171 }
1172 
1173 /*
1174  * Retrieve a file by HTTP
1175  */
1176 FILE *
1177 fetchGetHTTP(struct url *URL, const char *flags)
1178 {
1179 	return (fetchXGetHTTP(URL, NULL, flags));
1180 }
1181 
1182 /*
1183  * Store a file by HTTP
1184  */
1185 FILE *
1186 fetchPutHTTP(struct url *URL __unused, const char *flags __unused)
1187 {
1188 	warnx("fetchPutHTTP(): not implemented");
1189 	return (NULL);
1190 }
1191 
1192 /*
1193  * Get an HTTP document's metadata
1194  */
1195 int
1196 fetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags)
1197 {
1198 	FILE *f;
1199 
1200 	f = _http_request(URL, "HEAD", us, _http_get_proxy(flags), flags);
1201 	if (f == NULL)
1202 		return (-1);
1203 	fclose(f);
1204 	return (0);
1205 }
1206 
1207 /*
1208  * List a directory
1209  */
1210 struct url_ent *
1211 fetchListHTTP(struct url *url __unused, const char *flags __unused)
1212 {
1213 	warnx("fetchListHTTP(): not implemented");
1214 	return (NULL);
1215 }
1216