Home | History | Annotate | Line # | Download | only in libfetch
http.c revision 1.3
      1  1.3     joerg /*	$NetBSD: http.c,v 1.3 2014/01/07 02:13:00 joerg Exp $	*/
      2  1.1     joerg /*-
      3  1.1     joerg  * Copyright (c) 2000-2004 Dag-Erling Codan Smrgrav
      4  1.1     joerg  * Copyright (c) 2003 Thomas Klausner <wiz (at) NetBSD.org>
      5  1.2  christos  * Copyright (c) 2008, 2009 Joerg Sonnenberger <joerg (at) NetBSD.org>
      6  1.1     joerg  * All rights reserved.
      7  1.1     joerg  *
      8  1.1     joerg  * Redistribution and use in source and binary forms, with or without
      9  1.1     joerg  * modification, are permitted provided that the following conditions
     10  1.1     joerg  * are met:
     11  1.1     joerg  * 1. Redistributions of source code must retain the above copyright
     12  1.1     joerg  *    notice, this list of conditions and the following disclaimer
     13  1.1     joerg  *    in this position and unchanged.
     14  1.1     joerg  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1     joerg  *    notice, this list of conditions and the following disclaimer in the
     16  1.1     joerg  *    documentation and/or other materials provided with the distribution.
     17  1.1     joerg  * 3. The name of the author may not be used to endorse or promote products
     18  1.1     joerg  *    derived from this software without specific prior written permission.
     19  1.1     joerg  *
     20  1.1     joerg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  1.1     joerg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  1.1     joerg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.1     joerg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  1.1     joerg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  1.1     joerg  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  1.1     joerg  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  1.1     joerg  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  1.1     joerg  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  1.1     joerg  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.1     joerg  *
     31  1.1     joerg  * $FreeBSD: http.c,v 1.83 2008/02/06 11:39:55 des Exp $
     32  1.1     joerg  */
     33  1.1     joerg 
     34  1.1     joerg /*
     35  1.1     joerg  * The following copyright applies to the base64 code:
     36  1.1     joerg  *
     37  1.1     joerg  *-
     38  1.1     joerg  * Copyright 1997 Massachusetts Institute of Technology
     39  1.1     joerg  *
     40  1.1     joerg  * Permission to use, copy, modify, and distribute this software and
     41  1.1     joerg  * its documentation for any purpose and without fee is hereby
     42  1.1     joerg  * granted, provided that both the above copyright notice and this
     43  1.1     joerg  * permission notice appear in all copies, that both the above
     44  1.1     joerg  * copyright notice and this permission notice appear in all
     45  1.1     joerg  * supporting documentation, and that the name of M.I.T. not be used
     46  1.1     joerg  * in advertising or publicity pertaining to distribution of the
     47  1.1     joerg  * software without specific, written prior permission.  M.I.T. makes
     48  1.1     joerg  * no representations about the suitability of this software for any
     49  1.1     joerg  * purpose.  It is provided "as is" without express or implied
     50  1.1     joerg  * warranty.
     51  1.1     joerg  *
     52  1.1     joerg  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
     53  1.1     joerg  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
     54  1.1     joerg  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     55  1.1     joerg  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
     56  1.1     joerg  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     57  1.1     joerg  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     58  1.1     joerg  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     59  1.1     joerg  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     60  1.1     joerg  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     61  1.1     joerg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     62  1.1     joerg  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  1.1     joerg  * SUCH DAMAGE.
     64  1.1     joerg  */
     65  1.1     joerg 
     66  1.2  christos #if defined(__linux__) || defined(__MINT__)
     67  1.2  christos /* Keep this down to Linux or MiNT, it can create surprises elsewhere. */
     68  1.1     joerg #define _GNU_SOURCE
     69  1.1     joerg #endif
     70  1.1     joerg 
     71  1.2  christos /* Needed for gmtime_r on Interix */
     72  1.2  christos #define _REENTRANT
     73  1.2  christos 
     74  1.1     joerg #if HAVE_CONFIG_H
     75  1.1     joerg #include "config.h"
     76  1.1     joerg #endif
     77  1.1     joerg #ifndef NETBSD
     78  1.1     joerg #include <nbcompat.h>
     79  1.1     joerg #endif
     80  1.1     joerg 
     81  1.1     joerg #include <sys/types.h>
     82  1.1     joerg #include <sys/socket.h>
     83  1.1     joerg 
     84  1.1     joerg #include <ctype.h>
     85  1.1     joerg #include <errno.h>
     86  1.1     joerg #include <locale.h>
     87  1.1     joerg #include <stdarg.h>
     88  1.1     joerg #ifndef NETBSD
     89  1.1     joerg #include <nbcompat/stdio.h>
     90  1.1     joerg #else
     91  1.1     joerg #include <stdio.h>
     92  1.1     joerg #endif
     93  1.1     joerg #include <stdlib.h>
     94  1.1     joerg #include <string.h>
     95  1.1     joerg #include <time.h>
     96  1.1     joerg #include <unistd.h>
     97  1.1     joerg 
     98  1.1     joerg #include <netinet/in.h>
     99  1.1     joerg #include <netinet/tcp.h>
    100  1.1     joerg 
    101  1.2  christos #ifndef NETBSD
    102  1.2  christos #include <nbcompat/netdb.h>
    103  1.2  christos #else
    104  1.2  christos #include <netdb.h>
    105  1.2  christos #endif
    106  1.2  christos 
    107  1.2  christos #include <arpa/inet.h>
    108  1.2  christos 
    109  1.1     joerg #include "fetch.h"
    110  1.1     joerg #include "common.h"
    111  1.1     joerg #include "httperr.h"
    112  1.1     joerg 
    113  1.1     joerg /* Maximum number of redirects to follow */
    114  1.1     joerg #define MAX_REDIRECT 5
    115  1.1     joerg 
    116  1.1     joerg /* Symbolic names for reply codes we care about */
    117  1.1     joerg #define HTTP_OK			200
    118  1.1     joerg #define HTTP_PARTIAL		206
    119  1.1     joerg #define HTTP_MOVED_PERM		301
    120  1.1     joerg #define HTTP_MOVED_TEMP		302
    121  1.1     joerg #define HTTP_SEE_OTHER		303
    122  1.2  christos #define HTTP_NOT_MODIFIED	304
    123  1.1     joerg #define HTTP_TEMP_REDIRECT	307
    124  1.1     joerg #define HTTP_NEED_AUTH		401
    125  1.1     joerg #define HTTP_NEED_PROXY_AUTH	407
    126  1.1     joerg #define HTTP_BAD_RANGE		416
    127  1.1     joerg #define HTTP_PROTOCOL_ERROR	999
    128  1.1     joerg 
    129  1.1     joerg #define HTTP_REDIRECT(xyz) ((xyz) == HTTP_MOVED_PERM \
    130  1.1     joerg 			    || (xyz) == HTTP_MOVED_TEMP \
    131  1.1     joerg 			    || (xyz) == HTTP_TEMP_REDIRECT \
    132  1.1     joerg 			    || (xyz) == HTTP_SEE_OTHER)
    133  1.1     joerg 
    134  1.1     joerg #define HTTP_ERROR(xyz) ((xyz) > 400 && (xyz) < 599)
    135  1.1     joerg 
    136  1.1     joerg 
    137  1.1     joerg /*****************************************************************************
    138  1.1     joerg  * I/O functions for decoding chunked streams
    139  1.1     joerg  */
    140  1.1     joerg 
    141  1.1     joerg struct httpio
    142  1.1     joerg {
    143  1.1     joerg 	conn_t		*conn;		/* connection */
    144  1.1     joerg 	int		 chunked;	/* chunked mode */
    145  1.2  christos 	int		 keep_alive;	/* keep-alive mode */
    146  1.1     joerg 	char		*buf;		/* chunk buffer */
    147  1.1     joerg 	size_t		 bufsize;	/* size of chunk buffer */
    148  1.1     joerg 	ssize_t		 buflen;	/* amount of data currently in buffer */
    149  1.2  christos 	size_t		 bufpos;	/* current read offset in buffer */
    150  1.1     joerg 	int		 eof;		/* end-of-file flag */
    151  1.1     joerg 	int		 error;		/* error flag */
    152  1.1     joerg 	size_t		 chunksize;	/* remaining size of current chunk */
    153  1.2  christos 	off_t		 contentlength;	/* remaining size of the content */
    154  1.1     joerg };
    155  1.1     joerg 
    156  1.1     joerg /*
    157  1.1     joerg  * Get next chunk header
    158  1.1     joerg  */
    159  1.2  christos static ssize_t
    160  1.1     joerg http_new_chunk(struct httpio *io)
    161  1.1     joerg {
    162  1.1     joerg 	char *p;
    163  1.1     joerg 
    164  1.1     joerg 	if (fetch_getln(io->conn) == -1)
    165  1.1     joerg 		return (-1);
    166  1.1     joerg 
    167  1.1     joerg 	if (io->conn->buflen < 2 || !isxdigit((unsigned char)*io->conn->buf))
    168  1.1     joerg 		return (-1);
    169  1.1     joerg 
    170  1.1     joerg 	for (p = io->conn->buf; *p && !isspace((unsigned char)*p); ++p) {
    171  1.1     joerg 		if (*p == ';')
    172  1.1     joerg 			break;
    173  1.1     joerg 		if (!isxdigit((unsigned char)*p))
    174  1.1     joerg 			return (-1);
    175  1.1     joerg 		if (isdigit((unsigned char)*p)) {
    176  1.1     joerg 			io->chunksize = io->chunksize * 16 +
    177  1.1     joerg 			    *p - '0';
    178  1.1     joerg 		} else {
    179  1.1     joerg 			io->chunksize = io->chunksize * 16 +
    180  1.1     joerg 			    10 + tolower((unsigned char)*p) - 'a';
    181  1.1     joerg 		}
    182  1.1     joerg 	}
    183  1.1     joerg 
    184  1.1     joerg 	return (io->chunksize);
    185  1.1     joerg }
    186  1.1     joerg 
    187  1.1     joerg /*
    188  1.1     joerg  * Grow the input buffer to at least len bytes
    189  1.1     joerg  */
    190  1.1     joerg static int
    191  1.1     joerg http_growbuf(struct httpio *io, size_t len)
    192  1.1     joerg {
    193  1.1     joerg 	char *tmp;
    194  1.1     joerg 
    195  1.1     joerg 	if (io->bufsize >= len)
    196  1.1     joerg 		return (0);
    197  1.1     joerg 
    198  1.1     joerg 	if ((tmp = realloc(io->buf, len)) == NULL)
    199  1.1     joerg 		return (-1);
    200  1.1     joerg 	io->buf = tmp;
    201  1.1     joerg 	io->bufsize = len;
    202  1.1     joerg 	return (0);
    203  1.1     joerg }
    204  1.1     joerg 
    205  1.1     joerg /*
    206  1.1     joerg  * Fill the input buffer, do chunk decoding on the fly
    207  1.1     joerg  */
    208  1.2  christos static ssize_t
    209  1.1     joerg http_fillbuf(struct httpio *io, size_t len)
    210  1.1     joerg {
    211  1.1     joerg 	if (io->error)
    212  1.1     joerg 		return (-1);
    213  1.1     joerg 	if (io->eof)
    214  1.1     joerg 		return (0);
    215  1.1     joerg 
    216  1.2  christos 	if (io->contentlength >= 0 && (off_t)len > io->contentlength)
    217  1.2  christos 		len = io->contentlength;
    218  1.2  christos 
    219  1.1     joerg 	if (io->chunked == 0) {
    220  1.1     joerg 		if (http_growbuf(io, len) == -1)
    221  1.1     joerg 			return (-1);
    222  1.1     joerg 		if ((io->buflen = fetch_read(io->conn, io->buf, len)) == -1) {
    223  1.1     joerg 			io->error = 1;
    224  1.1     joerg 			return (-1);
    225  1.1     joerg 		}
    226  1.2  christos 		if (io->contentlength)
    227  1.2  christos 			io->contentlength -= io->buflen;
    228  1.1     joerg 		io->bufpos = 0;
    229  1.1     joerg 		return (io->buflen);
    230  1.1     joerg 	}
    231  1.1     joerg 
    232  1.1     joerg 	if (io->chunksize == 0) {
    233  1.1     joerg 		switch (http_new_chunk(io)) {
    234  1.1     joerg 		case -1:
    235  1.1     joerg 			io->error = 1;
    236  1.1     joerg 			return (-1);
    237  1.1     joerg 		case 0:
    238  1.1     joerg 			io->eof = 1;
    239  1.2  christos 			if (fetch_getln(io->conn) == -1)
    240  1.2  christos 				return (-1);
    241  1.1     joerg 			return (0);
    242  1.1     joerg 		}
    243  1.1     joerg 	}
    244  1.1     joerg 
    245  1.1     joerg 	if (len > io->chunksize)
    246  1.1     joerg 		len = io->chunksize;
    247  1.1     joerg 	if (http_growbuf(io, len) == -1)
    248  1.1     joerg 		return (-1);
    249  1.1     joerg 	if ((io->buflen = fetch_read(io->conn, io->buf, len)) == -1) {
    250  1.1     joerg 		io->error = 1;
    251  1.1     joerg 		return (-1);
    252  1.1     joerg 	}
    253  1.1     joerg 	io->chunksize -= io->buflen;
    254  1.2  christos 	if (io->contentlength >= 0)
    255  1.2  christos 		io->contentlength -= io->buflen;
    256  1.1     joerg 
    257  1.1     joerg 	if (io->chunksize == 0) {
    258  1.1     joerg 		char endl[2];
    259  1.1     joerg 		ssize_t len2;
    260  1.1     joerg 
    261  1.1     joerg 		len2 = fetch_read(io->conn, endl, 2);
    262  1.1     joerg 		if (len2 == 1 && fetch_read(io->conn, endl + 1, 1) != 1)
    263  1.1     joerg 			return (-1);
    264  1.1     joerg 		if (len2 == -1 || endl[0] != '\r' || endl[1] != '\n')
    265  1.1     joerg 			return (-1);
    266  1.1     joerg 	}
    267  1.1     joerg 
    268  1.1     joerg 	io->bufpos = 0;
    269  1.1     joerg 
    270  1.1     joerg 	return (io->buflen);
    271  1.1     joerg }
    272  1.1     joerg 
    273  1.1     joerg /*
    274  1.1     joerg  * Read function
    275  1.1     joerg  */
    276  1.1     joerg static ssize_t
    277  1.1     joerg http_readfn(void *v, void *buf, size_t len)
    278  1.1     joerg {
    279  1.1     joerg 	struct httpio *io = (struct httpio *)v;
    280  1.1     joerg 	size_t l, pos;
    281  1.1     joerg 
    282  1.1     joerg 	if (io->error)
    283  1.1     joerg 		return (-1);
    284  1.1     joerg 	if (io->eof)
    285  1.1     joerg 		return (0);
    286  1.1     joerg 
    287  1.1     joerg 	for (pos = 0; len > 0; pos += l, len -= l) {
    288  1.1     joerg 		/* empty buffer */
    289  1.2  christos 		if (!io->buf || (ssize_t)io->bufpos == io->buflen)
    290  1.1     joerg 			if (http_fillbuf(io, len) < 1)
    291  1.1     joerg 				break;
    292  1.1     joerg 		l = io->buflen - io->bufpos;
    293  1.1     joerg 		if (len < l)
    294  1.1     joerg 			l = len;
    295  1.1     joerg 		memcpy((char *)buf + pos, io->buf + io->bufpos, l);
    296  1.1     joerg 		io->bufpos += l;
    297  1.1     joerg 	}
    298  1.1     joerg 
    299  1.1     joerg 	if (!pos && io->error)
    300  1.1     joerg 		return (-1);
    301  1.1     joerg 	return (pos);
    302  1.1     joerg }
    303  1.1     joerg 
    304  1.1     joerg /*
    305  1.1     joerg  * Write function
    306  1.1     joerg  */
    307  1.1     joerg static ssize_t
    308  1.1     joerg http_writefn(void *v, const void *buf, size_t len)
    309  1.1     joerg {
    310  1.1     joerg 	struct httpio *io = (struct httpio *)v;
    311  1.1     joerg 
    312  1.1     joerg 	return (fetch_write(io->conn, buf, len));
    313  1.1     joerg }
    314  1.1     joerg 
    315  1.1     joerg /*
    316  1.1     joerg  * Close function
    317  1.1     joerg  */
    318  1.1     joerg static void
    319  1.1     joerg http_closefn(void *v)
    320  1.1     joerg {
    321  1.1     joerg 	struct httpio *io = (struct httpio *)v;
    322  1.1     joerg 
    323  1.2  christos 	if (io->keep_alive) {
    324  1.2  christos 		int val;
    325  1.2  christos 
    326  1.2  christos 		val = 0;
    327  1.2  christos 		setsockopt(io->conn->sd, IPPROTO_TCP, TCP_NODELAY, &val,
    328  1.2  christos 			   (socklen_t)sizeof(val));
    329  1.2  christos 			  fetch_cache_put(io->conn, fetch_close);
    330  1.2  christos #ifdef TCP_NOPUSH
    331  1.2  christos 		val = 1;
    332  1.2  christos 		setsockopt(io->conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val,
    333  1.2  christos 		    sizeof(val));
    334  1.2  christos #endif
    335  1.2  christos 	} else {
    336  1.2  christos 		fetch_close(io->conn);
    337  1.2  christos 	}
    338  1.2  christos 
    339  1.2  christos 	free(io->buf);
    340  1.1     joerg 	free(io);
    341  1.1     joerg }
    342  1.1     joerg 
    343  1.1     joerg /*
    344  1.1     joerg  * Wrap a file descriptor up
    345  1.1     joerg  */
    346  1.1     joerg static fetchIO *
    347  1.2  christos http_funopen(conn_t *conn, int chunked, int keep_alive, off_t clength)
    348  1.1     joerg {
    349  1.1     joerg 	struct httpio *io;
    350  1.1     joerg 	fetchIO *f;
    351  1.1     joerg 
    352  1.1     joerg 	if ((io = calloc(1, sizeof(*io))) == NULL) {
    353  1.1     joerg 		fetch_syserr();
    354  1.1     joerg 		return (NULL);
    355  1.1     joerg 	}
    356  1.1     joerg 	io->conn = conn;
    357  1.1     joerg 	io->chunked = chunked;
    358  1.2  christos 	io->contentlength = clength;
    359  1.2  christos 	io->keep_alive = keep_alive;
    360  1.1     joerg 	f = fetchIO_unopen(io, http_readfn, http_writefn, http_closefn);
    361  1.1     joerg 	if (f == NULL) {
    362  1.1     joerg 		fetch_syserr();
    363  1.1     joerg 		free(io);
    364  1.1     joerg 		return (NULL);
    365  1.1     joerg 	}
    366  1.1     joerg 	return (f);
    367  1.1     joerg }
    368  1.1     joerg 
    369  1.1     joerg 
    370  1.1     joerg /*****************************************************************************
    371  1.1     joerg  * Helper functions for talking to the server and parsing its replies
    372  1.1     joerg  */
    373  1.1     joerg 
    374  1.1     joerg /* Header types */
    375  1.1     joerg typedef enum {
    376  1.1     joerg 	hdr_syserror = -2,
    377  1.1     joerg 	hdr_error = -1,
    378  1.1     joerg 	hdr_end = 0,
    379  1.1     joerg 	hdr_unknown = 1,
    380  1.2  christos 	hdr_connection,
    381  1.1     joerg 	hdr_content_length,
    382  1.1     joerg 	hdr_content_range,
    383  1.1     joerg 	hdr_last_modified,
    384  1.1     joerg 	hdr_location,
    385  1.1     joerg 	hdr_transfer_encoding,
    386  1.1     joerg 	hdr_www_authenticate
    387  1.1     joerg } hdr_t;
    388  1.1     joerg 
    389  1.1     joerg /* Names of interesting headers */
    390  1.1     joerg static struct {
    391  1.1     joerg 	hdr_t		 num;
    392  1.1     joerg 	const char	*name;
    393  1.1     joerg } hdr_names[] = {
    394  1.2  christos 	{ hdr_connection,		"Connection" },
    395  1.1     joerg 	{ hdr_content_length,		"Content-Length" },
    396  1.1     joerg 	{ hdr_content_range,		"Content-Range" },
    397  1.1     joerg 	{ hdr_last_modified,		"Last-Modified" },
    398  1.1     joerg 	{ hdr_location,			"Location" },
    399  1.1     joerg 	{ hdr_transfer_encoding,	"Transfer-Encoding" },
    400  1.1     joerg 	{ hdr_www_authenticate,		"WWW-Authenticate" },
    401  1.1     joerg 	{ hdr_unknown,			NULL },
    402  1.1     joerg };
    403  1.1     joerg 
    404  1.1     joerg /*
    405  1.1     joerg  * Send a formatted line; optionally echo to terminal
    406  1.1     joerg  */
    407  1.3     joerg __printflike(2, 3)
    408  1.1     joerg static int
    409  1.1     joerg http_cmd(conn_t *conn, const char *fmt, ...)
    410  1.1     joerg {
    411  1.1     joerg 	va_list ap;
    412  1.1     joerg 	size_t len;
    413  1.1     joerg 	char *msg;
    414  1.2  christos 	ssize_t r;
    415  1.1     joerg 
    416  1.1     joerg 	va_start(ap, fmt);
    417  1.1     joerg 	len = vasprintf(&msg, fmt, ap);
    418  1.1     joerg 	va_end(ap);
    419  1.1     joerg 
    420  1.1     joerg 	if (msg == NULL) {
    421  1.1     joerg 		errno = ENOMEM;
    422  1.1     joerg 		fetch_syserr();
    423  1.1     joerg 		return (-1);
    424  1.1     joerg 	}
    425  1.1     joerg 
    426  1.2  christos 	r = fetch_write(conn, msg, len);
    427  1.1     joerg 	free(msg);
    428  1.1     joerg 
    429  1.1     joerg 	if (r == -1) {
    430  1.1     joerg 		fetch_syserr();
    431  1.1     joerg 		return (-1);
    432  1.1     joerg 	}
    433  1.1     joerg 
    434  1.1     joerg 	return (0);
    435  1.1     joerg }
    436  1.1     joerg 
    437  1.1     joerg /*
    438  1.1     joerg  * Get and parse status line
    439  1.1     joerg  */
    440  1.1     joerg static int
    441  1.1     joerg http_get_reply(conn_t *conn)
    442  1.1     joerg {
    443  1.1     joerg 	char *p;
    444  1.1     joerg 
    445  1.1     joerg 	if (fetch_getln(conn) == -1)
    446  1.1     joerg 		return (-1);
    447  1.1     joerg 	/*
    448  1.1     joerg 	 * A valid status line looks like "HTTP/m.n xyz reason" where m
    449  1.1     joerg 	 * and n are the major and minor protocol version numbers and xyz
    450  1.1     joerg 	 * is the reply code.
    451  1.1     joerg 	 * Unfortunately, there are servers out there (NCSA 1.5.1, to name
    452  1.1     joerg 	 * just one) that do not send a version number, so we can't rely
    453  1.1     joerg 	 * on finding one, but if we do, insist on it being 1.0 or 1.1.
    454  1.1     joerg 	 * We don't care about the reason phrase.
    455  1.1     joerg 	 */
    456  1.1     joerg 	if (strncmp(conn->buf, "HTTP", 4) != 0)
    457  1.1     joerg 		return (HTTP_PROTOCOL_ERROR);
    458  1.1     joerg 	p = conn->buf + 4;
    459  1.1     joerg 	if (*p == '/') {
    460  1.1     joerg 		if (p[1] != '1' || p[2] != '.' || (p[3] != '0' && p[3] != '1'))
    461  1.1     joerg 			return (HTTP_PROTOCOL_ERROR);
    462  1.1     joerg 		p += 4;
    463  1.1     joerg 	}
    464  1.1     joerg 	if (*p != ' ' ||
    465  1.1     joerg 	    !isdigit((unsigned char)p[1]) ||
    466  1.1     joerg 	    !isdigit((unsigned char)p[2]) ||
    467  1.1     joerg 	    !isdigit((unsigned char)p[3]))
    468  1.1     joerg 		return (HTTP_PROTOCOL_ERROR);
    469  1.1     joerg 
    470  1.1     joerg 	conn->err = (p[1] - '0') * 100 + (p[2] - '0') * 10 + (p[3] - '0');
    471  1.1     joerg 	return (conn->err);
    472  1.1     joerg }
    473  1.1     joerg 
    474  1.1     joerg /*
    475  1.1     joerg  * Check a header; if the type matches the given string, return a pointer
    476  1.1     joerg  * to the beginning of the value.
    477  1.1     joerg  */
    478  1.1     joerg static const char *
    479  1.1     joerg http_match(const char *str, const char *hdr)
    480  1.1     joerg {
    481  1.1     joerg 	while (*str && *hdr &&
    482  1.1     joerg 	    tolower((unsigned char)*str++) == tolower((unsigned char)*hdr++))
    483  1.1     joerg 		/* nothing */;
    484  1.1     joerg 	if (*str || *hdr != ':')
    485  1.1     joerg 		return (NULL);
    486  1.1     joerg 	while (*hdr && isspace((unsigned char)*++hdr))
    487  1.1     joerg 		/* nothing */;
    488  1.1     joerg 	return (hdr);
    489  1.1     joerg }
    490  1.1     joerg 
    491  1.1     joerg /*
    492  1.1     joerg  * Get the next header and return the appropriate symbolic code.
    493  1.1     joerg  */
    494  1.1     joerg static hdr_t
    495  1.1     joerg http_next_header(conn_t *conn, const char **p)
    496  1.1     joerg {
    497  1.1     joerg 	int i;
    498  1.1     joerg 
    499  1.1     joerg 	if (fetch_getln(conn) == -1)
    500  1.1     joerg 		return (hdr_syserror);
    501  1.1     joerg 	while (conn->buflen && isspace((unsigned char)conn->buf[conn->buflen - 1]))
    502  1.1     joerg 		conn->buflen--;
    503  1.1     joerg 	conn->buf[conn->buflen] = '\0';
    504  1.1     joerg 	if (conn->buflen == 0)
    505  1.1     joerg 		return (hdr_end);
    506  1.1     joerg 	/*
    507  1.1     joerg 	 * We could check for malformed headers but we don't really care.
    508  1.1     joerg 	 * A valid header starts with a token immediately followed by a
    509  1.1     joerg 	 * colon; a token is any sequence of non-control, non-whitespace
    510  1.1     joerg 	 * characters except "()<>@,;:\\\"{}".
    511  1.1     joerg 	 */
    512  1.1     joerg 	for (i = 0; hdr_names[i].num != hdr_unknown; i++)
    513  1.1     joerg 		if ((*p = http_match(hdr_names[i].name, conn->buf)) != NULL)
    514  1.1     joerg 			return (hdr_names[i].num);
    515  1.1     joerg 	return (hdr_unknown);
    516  1.1     joerg }
    517  1.1     joerg 
    518  1.1     joerg /*
    519  1.1     joerg  * Parse a last-modified header
    520  1.1     joerg  */
    521  1.1     joerg static int
    522  1.1     joerg http_parse_mtime(const char *p, time_t *mtime)
    523  1.1     joerg {
    524  1.1     joerg 	char locale[64], *r;
    525  1.1     joerg 	struct tm tm;
    526  1.1     joerg 
    527  1.1     joerg 	strncpy(locale, setlocale(LC_TIME, NULL), sizeof(locale));
    528  1.1     joerg 	setlocale(LC_TIME, "C");
    529  1.1     joerg 	r = strptime(p, "%a, %d %b %Y %H:%M:%S GMT", &tm);
    530  1.1     joerg 	/* XXX should add support for date-2 and date-3 */
    531  1.1     joerg 	setlocale(LC_TIME, locale);
    532  1.1     joerg 	if (r == NULL)
    533  1.1     joerg 		return (-1);
    534  1.1     joerg 	*mtime = timegm(&tm);
    535  1.1     joerg 	return (0);
    536  1.1     joerg }
    537  1.1     joerg 
    538  1.1     joerg /*
    539  1.1     joerg  * Parse a content-length header
    540  1.1     joerg  */
    541  1.1     joerg static int
    542  1.1     joerg http_parse_length(const char *p, off_t *length)
    543  1.1     joerg {
    544  1.1     joerg 	off_t len;
    545  1.1     joerg 
    546  1.1     joerg 	for (len = 0; *p && isdigit((unsigned char)*p); ++p)
    547  1.1     joerg 		len = len * 10 + (*p - '0');
    548  1.1     joerg 	if (*p)
    549  1.1     joerg 		return (-1);
    550  1.1     joerg 	*length = len;
    551  1.1     joerg 	return (0);
    552  1.1     joerg }
    553  1.1     joerg 
    554  1.1     joerg /*
    555  1.1     joerg  * Parse a content-range header
    556  1.1     joerg  */
    557  1.1     joerg static int
    558  1.1     joerg http_parse_range(const char *p, off_t *offset, off_t *length, off_t *size)
    559  1.1     joerg {
    560  1.1     joerg 	off_t first, last, len;
    561  1.1     joerg 
    562  1.1     joerg 	if (strncasecmp(p, "bytes ", 6) != 0)
    563  1.1     joerg 		return (-1);
    564  1.1     joerg 	p += 6;
    565  1.1     joerg 	if (*p == '*') {
    566  1.1     joerg 		first = last = -1;
    567  1.1     joerg 		++p;
    568  1.1     joerg 	} else {
    569  1.1     joerg 		for (first = 0; *p && isdigit((unsigned char)*p); ++p)
    570  1.1     joerg 			first = first * 10 + *p - '0';
    571  1.1     joerg 		if (*p != '-')
    572  1.1     joerg 			return (-1);
    573  1.1     joerg 		for (last = 0, ++p; *p && isdigit((unsigned char)*p); ++p)
    574  1.1     joerg 			last = last * 10 + *p - '0';
    575  1.1     joerg 	}
    576  1.1     joerg 	if (first > last || *p != '/')
    577  1.1     joerg 		return (-1);
    578  1.1     joerg 	for (len = 0, ++p; *p && isdigit((unsigned char)*p); ++p)
    579  1.1     joerg 		len = len * 10 + *p - '0';
    580  1.1     joerg 	if (*p || len < last - first + 1)
    581  1.1     joerg 		return (-1);
    582  1.1     joerg 	if (first == -1)
    583  1.1     joerg 		*length = 0;
    584  1.1     joerg 	else
    585  1.1     joerg 		*length = last - first + 1;
    586  1.1     joerg 	*offset = first;
    587  1.1     joerg 	*size = len;
    588  1.1     joerg 	return (0);
    589  1.1     joerg }
    590  1.1     joerg 
    591  1.1     joerg 
    592  1.1     joerg /*****************************************************************************
    593  1.1     joerg  * Helper functions for authorization
    594  1.1     joerg  */
    595  1.1     joerg 
    596  1.1     joerg /*
    597  1.1     joerg  * Base64 encoding
    598  1.1     joerg  */
    599  1.1     joerg static char *
    600  1.1     joerg http_base64(const char *src)
    601  1.1     joerg {
    602  1.1     joerg 	static const char base64[] =
    603  1.1     joerg 	    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    604  1.1     joerg 	    "abcdefghijklmnopqrstuvwxyz"
    605  1.1     joerg 	    "0123456789+/";
    606  1.1     joerg 	char *str, *dst;
    607  1.1     joerg 	size_t l;
    608  1.2  christos 	unsigned int t, r;
    609  1.1     joerg 
    610  1.1     joerg 	l = strlen(src);
    611  1.1     joerg 	if ((str = malloc(((l + 2) / 3) * 4 + 1)) == NULL)
    612  1.1     joerg 		return (NULL);
    613  1.1     joerg 	dst = str;
    614  1.1     joerg 	r = 0;
    615  1.1     joerg 
    616  1.1     joerg 	while (l >= 3) {
    617  1.1     joerg 		t = (src[0] << 16) | (src[1] << 8) | src[2];
    618  1.1     joerg 		dst[0] = base64[(t >> 18) & 0x3f];
    619  1.1     joerg 		dst[1] = base64[(t >> 12) & 0x3f];
    620  1.1     joerg 		dst[2] = base64[(t >> 6) & 0x3f];
    621  1.1     joerg 		dst[3] = base64[(t >> 0) & 0x3f];
    622  1.1     joerg 		src += 3; l -= 3;
    623  1.1     joerg 		dst += 4; r += 4;
    624  1.1     joerg 	}
    625  1.1     joerg 
    626  1.1     joerg 	switch (l) {
    627  1.1     joerg 	case 2:
    628  1.1     joerg 		t = (src[0] << 16) | (src[1] << 8);
    629  1.1     joerg 		dst[0] = base64[(t >> 18) & 0x3f];
    630  1.1     joerg 		dst[1] = base64[(t >> 12) & 0x3f];
    631  1.1     joerg 		dst[2] = base64[(t >> 6) & 0x3f];
    632  1.1     joerg 		dst[3] = '=';
    633  1.1     joerg 		dst += 4;
    634  1.1     joerg 		r += 4;
    635  1.1     joerg 		break;
    636  1.1     joerg 	case 1:
    637  1.1     joerg 		t = src[0] << 16;
    638  1.1     joerg 		dst[0] = base64[(t >> 18) & 0x3f];
    639  1.1     joerg 		dst[1] = base64[(t >> 12) & 0x3f];
    640  1.1     joerg 		dst[2] = dst[3] = '=';
    641  1.1     joerg 		dst += 4;
    642  1.1     joerg 		r += 4;
    643  1.1     joerg 		break;
    644  1.1     joerg 	case 0:
    645  1.1     joerg 		break;
    646  1.1     joerg 	}
    647  1.1     joerg 
    648  1.1     joerg 	*dst = 0;
    649  1.1     joerg 	return (str);
    650  1.1     joerg }
    651  1.1     joerg 
    652  1.1     joerg /*
    653  1.1     joerg  * Encode username and password
    654  1.1     joerg  */
    655  1.1     joerg static int
    656  1.1     joerg http_basic_auth(conn_t *conn, const char *hdr, const char *usr, const char *pwd)
    657  1.1     joerg {
    658  1.1     joerg 	char *upw, *auth;
    659  1.1     joerg 	int r;
    660  1.1     joerg 
    661  1.1     joerg 	if (asprintf(&upw, "%s:%s", usr, pwd) == -1)
    662  1.1     joerg 		return (-1);
    663  1.1     joerg 	auth = http_base64(upw);
    664  1.1     joerg 	free(upw);
    665  1.1     joerg 	if (auth == NULL)
    666  1.1     joerg 		return (-1);
    667  1.2  christos 	r = http_cmd(conn, "%s: Basic %s\r\n", hdr, auth);
    668  1.1     joerg 	free(auth);
    669  1.1     joerg 	return (r);
    670  1.1     joerg }
    671  1.1     joerg 
    672  1.1     joerg /*
    673  1.1     joerg  * Send an authorization header
    674  1.1     joerg  */
    675  1.1     joerg static int
    676  1.1     joerg http_authorize(conn_t *conn, const char *hdr, const char *p)
    677  1.1     joerg {
    678  1.1     joerg 	/* basic authorization */
    679  1.1     joerg 	if (strncasecmp(p, "basic:", 6) == 0) {
    680  1.1     joerg 		char *user, *pwd, *str;
    681  1.1     joerg 		int r;
    682  1.1     joerg 
    683  1.1     joerg 		/* skip realm */
    684  1.1     joerg 		for (p += 6; *p && *p != ':'; ++p)
    685  1.1     joerg 			/* nothing */ ;
    686  1.1     joerg 		if (!*p || strchr(++p, ':') == NULL)
    687  1.1     joerg 			return (-1);
    688  1.1     joerg 		if ((str = strdup(p)) == NULL)
    689  1.1     joerg 			return (-1); /* XXX */
    690  1.1     joerg 		user = str;
    691  1.1     joerg 		pwd = strchr(str, ':');
    692  1.1     joerg 		*pwd++ = '\0';
    693  1.1     joerg 		r = http_basic_auth(conn, hdr, user, pwd);
    694  1.1     joerg 		free(str);
    695  1.1     joerg 		return (r);
    696  1.1     joerg 	}
    697  1.1     joerg 	return (-1);
    698  1.1     joerg }
    699  1.1     joerg 
    700  1.1     joerg 
    701  1.1     joerg /*****************************************************************************
    702  1.1     joerg  * Helper functions for connecting to a server or proxy
    703  1.1     joerg  */
    704  1.1     joerg 
    705  1.1     joerg /*
    706  1.1     joerg  * Connect to the correct HTTP server or proxy.
    707  1.1     joerg  */
    708  1.1     joerg static conn_t *
    709  1.2  christos http_connect(struct url *URL, struct url *purl, const char *flags, int *cached)
    710  1.1     joerg {
    711  1.1     joerg 	conn_t *conn;
    712  1.1     joerg 	int af, verbose;
    713  1.1     joerg #ifdef TCP_NOPUSH
    714  1.1     joerg 	int val;
    715  1.1     joerg #endif
    716  1.1     joerg 
    717  1.2  christos 	*cached = 1;
    718  1.2  christos 
    719  1.1     joerg #ifdef INET6
    720  1.1     joerg 	af = AF_UNSPEC;
    721  1.1     joerg #else
    722  1.1     joerg 	af = AF_INET;
    723  1.1     joerg #endif
    724  1.1     joerg 
    725  1.1     joerg 	verbose = CHECK_FLAG('v');
    726  1.1     joerg 	if (CHECK_FLAG('4'))
    727  1.1     joerg 		af = AF_INET;
    728  1.1     joerg #ifdef INET6
    729  1.1     joerg 	else if (CHECK_FLAG('6'))
    730  1.1     joerg 		af = AF_INET6;
    731  1.1     joerg #endif
    732  1.1     joerg 
    733  1.1     joerg 	if (purl && strcasecmp(URL->scheme, SCHEME_HTTPS) != 0) {
    734  1.1     joerg 		URL = purl;
    735  1.1     joerg 	} else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0) {
    736  1.1     joerg 		/* can't talk http to an ftp server */
    737  1.1     joerg 		/* XXX should set an error code */
    738  1.1     joerg 		return (NULL);
    739  1.1     joerg 	}
    740  1.1     joerg 
    741  1.2  christos 	if ((conn = fetch_cache_get(URL, af)) != NULL) {
    742  1.2  christos 		*cached = 1;
    743  1.2  christos 		return (conn);
    744  1.2  christos 	}
    745  1.2  christos 
    746  1.2  christos 	if ((conn = fetch_connect(URL, af, verbose)) == NULL)
    747  1.1     joerg 		/* fetch_connect() has already set an error code */
    748  1.1     joerg 		return (NULL);
    749  1.1     joerg 	if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0 &&
    750  1.1     joerg 	    fetch_ssl(conn, verbose) == -1) {
    751  1.1     joerg 		fetch_close(conn);
    752  1.1     joerg 		/* grrr */
    753  1.1     joerg #ifdef EAUTH
    754  1.1     joerg 		errno = EAUTH;
    755  1.1     joerg #else
    756  1.1     joerg 		errno = EPERM;
    757  1.1     joerg #endif
    758  1.1     joerg 		fetch_syserr();
    759  1.1     joerg 		return (NULL);
    760  1.1     joerg 	}
    761  1.1     joerg 
    762  1.1     joerg #ifdef TCP_NOPUSH
    763  1.1     joerg 	val = 1;
    764  1.1     joerg 	setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val, sizeof(val));
    765  1.1     joerg #endif
    766  1.1     joerg 
    767  1.1     joerg 	return (conn);
    768  1.1     joerg }
    769  1.1     joerg 
    770  1.1     joerg static struct url *
    771  1.1     joerg http_get_proxy(struct url * url, const char *flags)
    772  1.1     joerg {
    773  1.1     joerg 	struct url *purl;
    774  1.1     joerg 	char *p;
    775  1.1     joerg 
    776  1.1     joerg 	if (flags != NULL && strchr(flags, 'd') != NULL)
    777  1.1     joerg 		return (NULL);
    778  1.1     joerg 	if (fetch_no_proxy_match(url->host))
    779  1.1     joerg 		return (NULL);
    780  1.1     joerg 	if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
    781  1.1     joerg 	    *p && (purl = fetchParseURL(p))) {
    782  1.1     joerg 		if (!*purl->scheme)
    783  1.1     joerg 			strcpy(purl->scheme, SCHEME_HTTP);
    784  1.1     joerg 		if (!purl->port)
    785  1.1     joerg 			purl->port = fetch_default_proxy_port(purl->scheme);
    786  1.1     joerg 		if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
    787  1.1     joerg 			return (purl);
    788  1.1     joerg 		fetchFreeURL(purl);
    789  1.1     joerg 	}
    790  1.1     joerg 	return (NULL);
    791  1.1     joerg }
    792  1.1     joerg 
    793  1.2  christos static void
    794  1.2  christos set_if_modified_since(conn_t *conn, time_t last_modified)
    795  1.2  christos {
    796  1.2  christos 	static const char weekdays[] = "SunMonTueWedThuFriSat";
    797  1.2  christos 	static const char months[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
    798  1.2  christos 	struct tm tm;
    799  1.2  christos 	char buf[80];
    800  1.2  christos 	gmtime_r(&last_modified, &tm);
    801  1.2  christos 	snprintf(buf, sizeof(buf), "%.3s, %02d %.3s %4d %02d:%02d:%02d GMT",
    802  1.2  christos 	    weekdays + tm.tm_wday * 3, tm.tm_mday, months + tm.tm_mon * 3,
    803  1.2  christos 	    tm.tm_year + 1900, tm.tm_hour, tm.tm_min, tm.tm_sec);
    804  1.2  christos 	http_cmd(conn, "If-Modified-Since: %s\r\n", buf);
    805  1.2  christos }
    806  1.2  christos 
    807  1.2  christos 
    808  1.1     joerg /*****************************************************************************
    809  1.1     joerg  * Core
    810  1.1     joerg  */
    811  1.1     joerg 
    812  1.1     joerg /*
    813  1.1     joerg  * Send a request and process the reply
    814  1.1     joerg  *
    815  1.1     joerg  * XXX This function is way too long, the do..while loop should be split
    816  1.1     joerg  * XXX off into a separate function.
    817  1.1     joerg  */
    818  1.1     joerg fetchIO *
    819  1.1     joerg http_request(struct url *URL, const char *op, struct url_stat *us,
    820  1.1     joerg     struct url *purl, const char *flags)
    821  1.1     joerg {
    822  1.1     joerg 	conn_t *conn;
    823  1.1     joerg 	struct url *url, *new;
    824  1.2  christos 	int chunked, direct, if_modified_since, need_auth, noredirect;
    825  1.2  christos 	int keep_alive, verbose, cached;
    826  1.1     joerg 	int e, i, n, val;
    827  1.1     joerg 	off_t offset, clength, length, size;
    828  1.1     joerg 	time_t mtime;
    829  1.1     joerg 	const char *p;
    830  1.1     joerg 	fetchIO *f;
    831  1.1     joerg 	hdr_t h;
    832  1.1     joerg 	char hbuf[URL_HOSTLEN + 7], *host;
    833  1.1     joerg 
    834  1.1     joerg 	direct = CHECK_FLAG('d');
    835  1.1     joerg 	noredirect = CHECK_FLAG('A');
    836  1.1     joerg 	verbose = CHECK_FLAG('v');
    837  1.2  christos 	if_modified_since = CHECK_FLAG('i');
    838  1.2  christos 	keep_alive = 0;
    839  1.1     joerg 
    840  1.1     joerg 	if (direct && purl) {
    841  1.1     joerg 		fetchFreeURL(purl);
    842  1.1     joerg 		purl = NULL;
    843  1.1     joerg 	}
    844  1.1     joerg 
    845  1.1     joerg 	/* try the provided URL first */
    846  1.1     joerg 	url = URL;
    847  1.1     joerg 
    848  1.1     joerg 	/* if the A flag is set, we only get one try */
    849  1.1     joerg 	n = noredirect ? 1 : MAX_REDIRECT;
    850  1.1     joerg 	i = 0;
    851  1.1     joerg 
    852  1.1     joerg 	e = HTTP_PROTOCOL_ERROR;
    853  1.1     joerg 	need_auth = 0;
    854  1.1     joerg 	do {
    855  1.1     joerg 		new = NULL;
    856  1.1     joerg 		chunked = 0;
    857  1.1     joerg 		offset = 0;
    858  1.1     joerg 		clength = -1;
    859  1.1     joerg 		length = -1;
    860  1.1     joerg 		size = -1;
    861  1.1     joerg 		mtime = 0;
    862  1.1     joerg 
    863  1.1     joerg 		/* check port */
    864  1.1     joerg 		if (!url->port)
    865  1.1     joerg 			url->port = fetch_default_port(url->scheme);
    866  1.1     joerg 
    867  1.1     joerg 		/* were we redirected to an FTP URL? */
    868  1.1     joerg 		if (purl == NULL && strcmp(url->scheme, SCHEME_FTP) == 0) {
    869  1.1     joerg 			if (strcmp(op, "GET") == 0)
    870  1.1     joerg 				return (ftp_request(url, "RETR", NULL, us, purl, flags));
    871  1.1     joerg 			else if (strcmp(op, "HEAD") == 0)
    872  1.1     joerg 				return (ftp_request(url, "STAT", NULL, us, purl, flags));
    873  1.1     joerg 		}
    874  1.1     joerg 
    875  1.1     joerg 		/* connect to server or proxy */
    876  1.2  christos 		if ((conn = http_connect(url, purl, flags, &cached)) == NULL)
    877  1.1     joerg 			goto ouch;
    878  1.1     joerg 
    879  1.1     joerg 		host = url->host;
    880  1.1     joerg #ifdef INET6
    881  1.1     joerg 		if (strchr(url->host, ':')) {
    882  1.1     joerg 			snprintf(hbuf, sizeof(hbuf), "[%s]", url->host);
    883  1.1     joerg 			host = hbuf;
    884  1.1     joerg 		}
    885  1.1     joerg #endif
    886  1.1     joerg 		if (url->port != fetch_default_port(url->scheme)) {
    887  1.1     joerg 			if (host != hbuf) {
    888  1.1     joerg 				strcpy(hbuf, host);
    889  1.1     joerg 				host = hbuf;
    890  1.1     joerg 			}
    891  1.1     joerg 			snprintf(hbuf + strlen(hbuf),
    892  1.1     joerg 			    sizeof(hbuf) - strlen(hbuf), ":%d", url->port);
    893  1.1     joerg 		}
    894  1.1     joerg 
    895  1.1     joerg 		/* send request */
    896  1.1     joerg 		if (verbose)
    897  1.1     joerg 			fetch_info("requesting %s://%s%s",
    898  1.1     joerg 			    url->scheme, host, url->doc);
    899  1.1     joerg 		if (purl) {
    900  1.2  christos 			http_cmd(conn, "%s %s://%s%s HTTP/1.1\r\n",
    901  1.1     joerg 			    op, url->scheme, host, url->doc);
    902  1.1     joerg 		} else {
    903  1.2  christos 			http_cmd(conn, "%s %s HTTP/1.1\r\n",
    904  1.1     joerg 			    op, url->doc);
    905  1.1     joerg 		}
    906  1.1     joerg 
    907  1.2  christos 		if (if_modified_since && url->last_modified > 0)
    908  1.2  christos 			set_if_modified_since(conn, url->last_modified);
    909  1.2  christos 
    910  1.1     joerg 		/* virtual host */
    911  1.2  christos 		http_cmd(conn, "Host: %s\r\n", host);
    912  1.1     joerg 
    913  1.1     joerg 		/* proxy authorization */
    914  1.1     joerg 		if (purl) {
    915  1.1     joerg 			if (*purl->user || *purl->pwd)
    916  1.1     joerg 				http_basic_auth(conn, "Proxy-Authorization",
    917  1.1     joerg 				    purl->user, purl->pwd);
    918  1.1     joerg 			else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL && *p != '\0')
    919  1.1     joerg 				http_authorize(conn, "Proxy-Authorization", p);
    920  1.1     joerg 		}
    921  1.1     joerg 
    922  1.1     joerg 		/* server authorization */
    923  1.1     joerg 		if (need_auth || *url->user || *url->pwd) {
    924  1.1     joerg 			if (*url->user || *url->pwd)
    925  1.1     joerg 				http_basic_auth(conn, "Authorization", url->user, url->pwd);
    926  1.1     joerg 			else if ((p = getenv("HTTP_AUTH")) != NULL && *p != '\0')
    927  1.1     joerg 				http_authorize(conn, "Authorization", p);
    928  1.1     joerg 			else if (fetchAuthMethod && fetchAuthMethod(url) == 0) {
    929  1.1     joerg 				http_basic_auth(conn, "Authorization", url->user, url->pwd);
    930  1.1     joerg 			} else {
    931  1.1     joerg 				http_seterr(HTTP_NEED_AUTH);
    932  1.1     joerg 				goto ouch;
    933  1.1     joerg 			}
    934  1.1     joerg 		}
    935  1.1     joerg 
    936  1.1     joerg 		/* other headers */
    937  1.1     joerg 		if ((p = getenv("HTTP_REFERER")) != NULL && *p != '\0') {
    938  1.1     joerg 			if (strcasecmp(p, "auto") == 0)
    939  1.2  christos 				http_cmd(conn, "Referer: %s://%s%s\r\n",
    940  1.1     joerg 				    url->scheme, host, url->doc);
    941  1.1     joerg 			else
    942  1.2  christos 				http_cmd(conn, "Referer: %s\r\n", p);
    943  1.1     joerg 		}
    944  1.1     joerg 		if ((p = getenv("HTTP_USER_AGENT")) != NULL && *p != '\0')
    945  1.2  christos 			http_cmd(conn, "User-Agent: %s\r\n", p);
    946  1.1     joerg 		else
    947  1.2  christos 			http_cmd(conn, "User-Agent: %s\r\n", _LIBFETCH_VER);
    948  1.1     joerg 		if (url->offset > 0)
    949  1.2  christos 			http_cmd(conn, "Range: bytes=%lld-\r\n", (long long)url->offset);
    950  1.2  christos 		http_cmd(conn, "\r\n");
    951  1.1     joerg 
    952  1.1     joerg 		/*
    953  1.1     joerg 		 * Force the queued request to be dispatched.  Normally, one
    954  1.1     joerg 		 * would do this with shutdown(2) but squid proxies can be
    955  1.1     joerg 		 * configured to disallow such half-closed connections.  To
    956  1.1     joerg 		 * be compatible with such configurations, fiddle with socket
    957  1.1     joerg 		 * options to force the pending data to be written.
    958  1.1     joerg 		 */
    959  1.1     joerg #ifdef TCP_NOPUSH
    960  1.1     joerg 		val = 0;
    961  1.1     joerg 		setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val,
    962  1.1     joerg 			   sizeof(val));
    963  1.1     joerg #endif
    964  1.1     joerg 		val = 1;
    965  1.1     joerg 		setsockopt(conn->sd, IPPROTO_TCP, TCP_NODELAY, &val,
    966  1.2  christos 		    (socklen_t)sizeof(val));
    967  1.1     joerg 
    968  1.1     joerg 		/* get reply */
    969  1.1     joerg 		switch (http_get_reply(conn)) {
    970  1.1     joerg 		case HTTP_OK:
    971  1.1     joerg 		case HTTP_PARTIAL:
    972  1.2  christos 		case HTTP_NOT_MODIFIED:
    973  1.1     joerg 			/* fine */
    974  1.1     joerg 			break;
    975  1.1     joerg 		case HTTP_MOVED_PERM:
    976  1.1     joerg 		case HTTP_MOVED_TEMP:
    977  1.1     joerg 		case HTTP_SEE_OTHER:
    978  1.1     joerg 			/*
    979  1.1     joerg 			 * Not so fine, but we still have to read the
    980  1.1     joerg 			 * headers to get the new location.
    981  1.1     joerg 			 */
    982  1.1     joerg 			break;
    983  1.1     joerg 		case HTTP_NEED_AUTH:
    984  1.1     joerg 			if (need_auth) {
    985  1.1     joerg 				/*
    986  1.1     joerg 				 * We already sent out authorization code,
    987  1.1     joerg 				 * so there's nothing more we can do.
    988  1.1     joerg 				 */
    989  1.1     joerg 				http_seterr(conn->err);
    990  1.1     joerg 				goto ouch;
    991  1.1     joerg 			}
    992  1.1     joerg 			/* try again, but send the password this time */
    993  1.1     joerg 			if (verbose)
    994  1.1     joerg 				fetch_info("server requires authorization");
    995  1.1     joerg 			break;
    996  1.1     joerg 		case HTTP_NEED_PROXY_AUTH:
    997  1.1     joerg 			/*
    998  1.1     joerg 			 * If we're talking to a proxy, we already sent
    999  1.1     joerg 			 * our proxy authorization code, so there's
   1000  1.1     joerg 			 * nothing more we can do.
   1001  1.1     joerg 			 */
   1002  1.1     joerg 			http_seterr(conn->err);
   1003  1.1     joerg 			goto ouch;
   1004  1.1     joerg 		case HTTP_BAD_RANGE:
   1005  1.1     joerg 			/*
   1006  1.1     joerg 			 * This can happen if we ask for 0 bytes because
   1007  1.1     joerg 			 * we already have the whole file.  Consider this
   1008  1.1     joerg 			 * a success for now, and check sizes later.
   1009  1.1     joerg 			 */
   1010  1.1     joerg 			break;
   1011  1.1     joerg 		case HTTP_PROTOCOL_ERROR:
   1012  1.1     joerg 			/* fall through */
   1013  1.1     joerg 		case -1:
   1014  1.2  christos 			--i;
   1015  1.2  christos 			if (cached)
   1016  1.2  christos 				continue;
   1017  1.1     joerg 			fetch_syserr();
   1018  1.1     joerg 			goto ouch;
   1019  1.1     joerg 		default:
   1020  1.1     joerg 			http_seterr(conn->err);
   1021  1.1     joerg 			if (!verbose)
   1022  1.1     joerg 				goto ouch;
   1023  1.1     joerg 			/* fall through so we can get the full error message */
   1024  1.1     joerg 		}
   1025  1.1     joerg 
   1026  1.1     joerg 		/* get headers */
   1027  1.1     joerg 		do {
   1028  1.1     joerg 			switch ((h = http_next_header(conn, &p))) {
   1029  1.1     joerg 			case hdr_syserror:
   1030  1.1     joerg 				fetch_syserr();
   1031  1.1     joerg 				goto ouch;
   1032  1.1     joerg 			case hdr_error:
   1033  1.1     joerg 				http_seterr(HTTP_PROTOCOL_ERROR);
   1034  1.1     joerg 				goto ouch;
   1035  1.2  christos 			case hdr_connection:
   1036  1.2  christos 				/* XXX too weak? */
   1037  1.2  christos 				keep_alive = (strcasecmp(p, "keep-alive") == 0);
   1038  1.2  christos 				break;
   1039  1.1     joerg 			case hdr_content_length:
   1040  1.1     joerg 				http_parse_length(p, &clength);
   1041  1.1     joerg 				break;
   1042  1.1     joerg 			case hdr_content_range:
   1043  1.1     joerg 				http_parse_range(p, &offset, &length, &size);
   1044  1.1     joerg 				break;
   1045  1.1     joerg 			case hdr_last_modified:
   1046  1.1     joerg 				http_parse_mtime(p, &mtime);
   1047  1.1     joerg 				break;
   1048  1.1     joerg 			case hdr_location:
   1049  1.1     joerg 				if (!HTTP_REDIRECT(conn->err))
   1050  1.1     joerg 					break;
   1051  1.1     joerg 				if (new)
   1052  1.1     joerg 					free(new);
   1053  1.1     joerg 				if (verbose)
   1054  1.1     joerg 					fetch_info("%d redirect to %s", conn->err, p);
   1055  1.1     joerg 				if (*p == '/')
   1056  1.1     joerg 					/* absolute path */
   1057  1.1     joerg 					new = fetchMakeURL(url->scheme, url->host, url->port, p,
   1058  1.1     joerg 					    url->user, url->pwd);
   1059  1.1     joerg 				else
   1060  1.1     joerg 					new = fetchParseURL(p);
   1061  1.1     joerg 				if (new == NULL) {
   1062  1.1     joerg 					/* XXX should set an error code */
   1063  1.1     joerg 					goto ouch;
   1064  1.1     joerg 				}
   1065  1.1     joerg 				if (!*new->user && !*new->pwd) {
   1066  1.1     joerg 					strcpy(new->user, url->user);
   1067  1.1     joerg 					strcpy(new->pwd, url->pwd);
   1068  1.1     joerg 				}
   1069  1.1     joerg 				new->offset = url->offset;
   1070  1.1     joerg 				new->length = url->length;
   1071  1.1     joerg 				break;
   1072  1.1     joerg 			case hdr_transfer_encoding:
   1073  1.1     joerg 				/* XXX weak test*/
   1074  1.1     joerg 				chunked = (strcasecmp(p, "chunked") == 0);
   1075  1.1     joerg 				break;
   1076  1.1     joerg 			case hdr_www_authenticate:
   1077  1.1     joerg 				if (conn->err != HTTP_NEED_AUTH)
   1078  1.1     joerg 					break;
   1079  1.1     joerg 				/* if we were smarter, we'd check the method and realm */
   1080  1.1     joerg 				break;
   1081  1.1     joerg 			case hdr_end:
   1082  1.1     joerg 				/* fall through */
   1083  1.1     joerg 			case hdr_unknown:
   1084  1.1     joerg 				/* ignore */
   1085  1.1     joerg 				break;
   1086  1.1     joerg 			}
   1087  1.1     joerg 		} while (h > hdr_end);
   1088  1.1     joerg 
   1089  1.1     joerg 		/* we need to provide authentication */
   1090  1.1     joerg 		if (conn->err == HTTP_NEED_AUTH) {
   1091  1.1     joerg 			e = conn->err;
   1092  1.1     joerg 			need_auth = 1;
   1093  1.1     joerg 			fetch_close(conn);
   1094  1.1     joerg 			conn = NULL;
   1095  1.1     joerg 			continue;
   1096  1.1     joerg 		}
   1097  1.1     joerg 
   1098  1.1     joerg 		/* requested range not satisfiable */
   1099  1.1     joerg 		if (conn->err == HTTP_BAD_RANGE) {
   1100  1.1     joerg 			if (url->offset == size && url->length == 0) {
   1101  1.1     joerg 				/* asked for 0 bytes; fake it */
   1102  1.1     joerg 				offset = url->offset;
   1103  1.1     joerg 				conn->err = HTTP_OK;
   1104  1.1     joerg 				break;
   1105  1.1     joerg 			} else {
   1106  1.1     joerg 				http_seterr(conn->err);
   1107  1.1     joerg 				goto ouch;
   1108  1.1     joerg 			}
   1109  1.1     joerg 		}
   1110  1.1     joerg 
   1111  1.1     joerg 		/* we have a hit or an error */
   1112  1.2  christos 		if (conn->err == HTTP_OK ||
   1113  1.2  christos 		    conn->err == HTTP_PARTIAL ||
   1114  1.2  christos 		    conn->err == HTTP_NOT_MODIFIED ||
   1115  1.2  christos 		    HTTP_ERROR(conn->err))
   1116  1.1     joerg 			break;
   1117  1.1     joerg 
   1118  1.1     joerg 		/* all other cases: we got a redirect */
   1119  1.1     joerg 		e = conn->err;
   1120  1.1     joerg 		need_auth = 0;
   1121  1.1     joerg 		fetch_close(conn);
   1122  1.1     joerg 		conn = NULL;
   1123  1.1     joerg 		if (!new)
   1124  1.1     joerg 			break;
   1125  1.1     joerg 		if (url != URL)
   1126  1.1     joerg 			fetchFreeURL(url);
   1127  1.1     joerg 		url = new;
   1128  1.1     joerg 	} while (++i < n);
   1129  1.1     joerg 
   1130  1.1     joerg 	/* we failed, or ran out of retries */
   1131  1.1     joerg 	if (conn == NULL) {
   1132  1.1     joerg 		http_seterr(e);
   1133  1.1     joerg 		goto ouch;
   1134  1.1     joerg 	}
   1135  1.1     joerg 
   1136  1.1     joerg 	/* check for inconsistencies */
   1137  1.1     joerg 	if (clength != -1 && length != -1 && clength != length) {
   1138  1.1     joerg 		http_seterr(HTTP_PROTOCOL_ERROR);
   1139  1.1     joerg 		goto ouch;
   1140  1.1     joerg 	}
   1141  1.1     joerg 	if (clength == -1)
   1142  1.1     joerg 		clength = length;
   1143  1.1     joerg 	if (clength != -1)
   1144  1.1     joerg 		length = offset + clength;
   1145  1.1     joerg 	if (length != -1 && size != -1 && length != size) {
   1146  1.1     joerg 		http_seterr(HTTP_PROTOCOL_ERROR);
   1147  1.1     joerg 		goto ouch;
   1148  1.1     joerg 	}
   1149  1.1     joerg 	if (size == -1)
   1150  1.1     joerg 		size = length;
   1151  1.1     joerg 
   1152  1.1     joerg 	/* fill in stats */
   1153  1.1     joerg 	if (us) {
   1154  1.1     joerg 		us->size = size;
   1155  1.1     joerg 		us->atime = us->mtime = mtime;
   1156  1.1     joerg 	}
   1157  1.1     joerg 
   1158  1.1     joerg 	/* too far? */
   1159  1.1     joerg 	if (URL->offset > 0 && offset > URL->offset) {
   1160  1.1     joerg 		http_seterr(HTTP_PROTOCOL_ERROR);
   1161  1.1     joerg 		goto ouch;
   1162  1.1     joerg 	}
   1163  1.1     joerg 
   1164  1.1     joerg 	/* report back real offset and size */
   1165  1.1     joerg 	URL->offset = offset;
   1166  1.1     joerg 	URL->length = clength;
   1167  1.1     joerg 
   1168  1.2  christos 	if (clength == -1 && !chunked)
   1169  1.2  christos 		keep_alive = 0;
   1170  1.2  christos 
   1171  1.2  christos 	if (conn->err == HTTP_NOT_MODIFIED) {
   1172  1.2  christos 		http_seterr(HTTP_NOT_MODIFIED);
   1173  1.2  christos 		if (keep_alive) {
   1174  1.2  christos 			fetch_cache_put(conn, fetch_close);
   1175  1.2  christos 			conn = NULL;
   1176  1.2  christos 		}
   1177  1.2  christos 		goto ouch;
   1178  1.2  christos 	}
   1179  1.2  christos 
   1180  1.1     joerg 	/* wrap it up in a fetchIO */
   1181  1.2  christos 	if ((f = http_funopen(conn, chunked, keep_alive, clength)) == NULL) {
   1182  1.1     joerg 		fetch_syserr();
   1183  1.1     joerg 		goto ouch;
   1184  1.1     joerg 	}
   1185  1.1     joerg 
   1186  1.1     joerg 	if (url != URL)
   1187  1.1     joerg 		fetchFreeURL(url);
   1188  1.1     joerg 	if (purl)
   1189  1.1     joerg 		fetchFreeURL(purl);
   1190  1.1     joerg 
   1191  1.1     joerg 	if (HTTP_ERROR(conn->err)) {
   1192  1.2  christos 
   1193  1.2  christos 		if (keep_alive) {
   1194  1.2  christos 			char buf[512];
   1195  1.2  christos 			do {
   1196  1.2  christos 			} while (fetchIO_read(f, buf, sizeof(buf)) > 0);
   1197  1.2  christos 		}
   1198  1.2  christos 
   1199  1.1     joerg 		fetchIO_close(f);
   1200  1.1     joerg 		f = NULL;
   1201  1.1     joerg 	}
   1202  1.1     joerg 
   1203  1.1     joerg 	return (f);
   1204  1.1     joerg 
   1205  1.1     joerg ouch:
   1206  1.1     joerg 	if (url != URL)
   1207  1.1     joerg 		fetchFreeURL(url);
   1208  1.1     joerg 	if (purl)
   1209  1.1     joerg 		fetchFreeURL(purl);
   1210  1.1     joerg 	if (conn != NULL)
   1211  1.1     joerg 		fetch_close(conn);
   1212  1.1     joerg 	return (NULL);
   1213  1.1     joerg }
   1214  1.1     joerg 
   1215  1.1     joerg 
   1216  1.1     joerg /*****************************************************************************
   1217  1.1     joerg  * Entry points
   1218  1.1     joerg  */
   1219  1.1     joerg 
   1220  1.1     joerg /*
   1221  1.1     joerg  * Retrieve and stat a file by HTTP
   1222  1.1     joerg  */
   1223  1.1     joerg fetchIO *
   1224  1.1     joerg fetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags)
   1225  1.1     joerg {
   1226  1.1     joerg 	return (http_request(URL, "GET", us, http_get_proxy(URL, flags), flags));
   1227  1.1     joerg }
   1228  1.1     joerg 
   1229  1.1     joerg /*
   1230  1.1     joerg  * Retrieve a file by HTTP
   1231  1.1     joerg  */
   1232  1.1     joerg fetchIO *
   1233  1.1     joerg fetchGetHTTP(struct url *URL, const char *flags)
   1234  1.1     joerg {
   1235  1.1     joerg 	return (fetchXGetHTTP(URL, NULL, flags));
   1236  1.1     joerg }
   1237  1.1     joerg 
   1238  1.1     joerg /*
   1239  1.1     joerg  * Store a file by HTTP
   1240  1.1     joerg  */
   1241  1.1     joerg fetchIO *
   1242  1.2  christos /*ARGSUSED*/
   1243  1.2  christos fetchPutHTTP(struct url *URL __unused, const char *flags __unused)
   1244  1.1     joerg {
   1245  1.1     joerg 	fprintf(stderr, "fetchPutHTTP(): not implemented\n");
   1246  1.1     joerg 	return (NULL);
   1247  1.1     joerg }
   1248  1.1     joerg 
   1249  1.1     joerg /*
   1250  1.1     joerg  * Get an HTTP document's metadata
   1251  1.1     joerg  */
   1252  1.1     joerg int
   1253  1.1     joerg fetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags)
   1254  1.1     joerg {
   1255  1.1     joerg 	fetchIO *f;
   1256  1.1     joerg 
   1257  1.1     joerg 	f = http_request(URL, "HEAD", us, http_get_proxy(URL, flags), flags);
   1258  1.1     joerg 	if (f == NULL)
   1259  1.1     joerg 		return (-1);
   1260  1.1     joerg 	fetchIO_close(f);
   1261  1.1     joerg 	return (0);
   1262  1.1     joerg }
   1263  1.1     joerg 
   1264  1.1     joerg enum http_states {
   1265  1.1     joerg 	ST_NONE,
   1266  1.1     joerg 	ST_LT,
   1267  1.1     joerg 	ST_LTA,
   1268  1.1     joerg 	ST_TAGA,
   1269  1.1     joerg 	ST_H,
   1270  1.1     joerg 	ST_R,
   1271  1.1     joerg 	ST_E,
   1272  1.1     joerg 	ST_F,
   1273  1.1     joerg 	ST_HREF,
   1274  1.1     joerg 	ST_HREFQ,
   1275  1.1     joerg 	ST_TAG,
   1276  1.1     joerg 	ST_TAGAX,
   1277  1.1     joerg 	ST_TAGAQ
   1278  1.1     joerg };
   1279  1.1     joerg 
   1280  1.1     joerg struct index_parser {
   1281  1.1     joerg 	struct url_list *ue;
   1282  1.1     joerg 	struct url *url;
   1283  1.1     joerg 	enum http_states state;
   1284  1.1     joerg };
   1285  1.1     joerg 
   1286  1.2  christos static ssize_t
   1287  1.1     joerg parse_index(struct index_parser *parser, const char *buf, size_t len)
   1288  1.1     joerg {
   1289  1.1     joerg 	char *end_attr, p = *buf;
   1290  1.1     joerg 
   1291  1.1     joerg 	switch (parser->state) {
   1292  1.1     joerg 	case ST_NONE:
   1293  1.1     joerg 		/* Plain text, not in markup */
   1294  1.1     joerg 		if (p == '<')
   1295  1.1     joerg 			parser->state = ST_LT;
   1296  1.1     joerg 		return 1;
   1297  1.1     joerg 	case ST_LT:
   1298  1.1     joerg 		/* In tag -- "<" already found */
   1299  1.1     joerg 		if (p == '>')
   1300  1.1     joerg 			parser->state = ST_NONE;
   1301  1.1     joerg 		else if (p == 'a' || p == 'A')
   1302  1.1     joerg 			parser->state = ST_LTA;
   1303  1.1     joerg 		else if (!isspace((unsigned char)p))
   1304  1.1     joerg 			parser->state = ST_TAG;
   1305  1.1     joerg 		return 1;
   1306  1.1     joerg 	case ST_LTA:
   1307  1.1     joerg 		/* In tag -- "<a" already found */
   1308  1.1     joerg 		if (p == '>')
   1309  1.1     joerg 			parser->state = ST_NONE;
   1310  1.1     joerg 		else if (p == '"')
   1311  1.1     joerg 			parser->state = ST_TAGAQ;
   1312  1.1     joerg 		else if (isspace((unsigned char)p))
   1313  1.1     joerg 			parser->state = ST_TAGA;
   1314  1.1     joerg 		else
   1315  1.1     joerg 			parser->state = ST_TAG;
   1316  1.1     joerg 		return 1;
   1317  1.1     joerg 	case ST_TAG:
   1318  1.1     joerg 		/* In tag, but not "<a" -- disregard */
   1319  1.1     joerg 		if (p == '>')
   1320  1.1     joerg 			parser->state = ST_NONE;
   1321  1.1     joerg 		return 1;
   1322  1.1     joerg 	case ST_TAGA:
   1323  1.1     joerg 		/* In a-tag -- "<a " already found */
   1324  1.1     joerg 		if (p == '>')
   1325  1.1     joerg 			parser->state = ST_NONE;
   1326  1.1     joerg 		else if (p == '"')
   1327  1.1     joerg 			parser->state = ST_TAGAQ;
   1328  1.1     joerg 		else if (p == 'h' || p == 'H')
   1329  1.1     joerg 			parser->state = ST_H;
   1330  1.1     joerg 		else if (!isspace((unsigned char)p))
   1331  1.1     joerg 			parser->state = ST_TAGAX;
   1332  1.1     joerg 		return 1;
   1333  1.1     joerg 	case ST_TAGAX:
   1334  1.1     joerg 		/* In unknown keyword in a-tag */
   1335  1.1     joerg 		if (p == '>')
   1336  1.1     joerg 			parser->state = ST_NONE;
   1337  1.1     joerg 		else if (p == '"')
   1338  1.1     joerg 			parser->state = ST_TAGAQ;
   1339  1.1     joerg 		else if (isspace((unsigned char)p))
   1340  1.1     joerg 			parser->state = ST_TAGA;
   1341  1.1     joerg 		return 1;
   1342  1.1     joerg 	case ST_TAGAQ:
   1343  1.1     joerg 		/* In a-tag, unknown argument for keys. */
   1344  1.1     joerg 		if (p == '>')
   1345  1.1     joerg 			parser->state = ST_NONE;
   1346  1.1     joerg 		else if (p == '"')
   1347  1.1     joerg 			parser->state = ST_TAGA;
   1348  1.1     joerg 		return 1;
   1349  1.1     joerg 	case ST_H:
   1350  1.1     joerg 		/* In a-tag -- "<a h" already found */
   1351  1.1     joerg 		if (p == '>')
   1352  1.1     joerg 			parser->state = ST_NONE;
   1353  1.1     joerg 		else if (p == '"')
   1354  1.1     joerg 			parser->state = ST_TAGAQ;
   1355  1.1     joerg 		else if (p == 'r' || p == 'R')
   1356  1.1     joerg 			parser->state = ST_R;
   1357  1.1     joerg 		else if (isspace((unsigned char)p))
   1358  1.1     joerg 			parser->state = ST_TAGA;
   1359  1.1     joerg 		else
   1360  1.1     joerg 			parser->state = ST_TAGAX;
   1361  1.1     joerg 		return 1;
   1362  1.1     joerg 	case ST_R:
   1363  1.1     joerg 		/* In a-tag -- "<a hr" already found */
   1364  1.1     joerg 		if (p == '>')
   1365  1.1     joerg 			parser->state = ST_NONE;
   1366  1.1     joerg 		else if (p == '"')
   1367  1.1     joerg 			parser->state = ST_TAGAQ;
   1368  1.1     joerg 		else if (p == 'e' || p == 'E')
   1369  1.1     joerg 			parser->state = ST_E;
   1370  1.1     joerg 		else if (isspace((unsigned char)p))
   1371  1.1     joerg 			parser->state = ST_TAGA;
   1372  1.1     joerg 		else
   1373  1.1     joerg 			parser->state = ST_TAGAX;
   1374  1.1     joerg 		return 1;
   1375  1.1     joerg 	case ST_E:
   1376  1.1     joerg 		/* In a-tag -- "<a hre" already found */
   1377  1.1     joerg 		if (p == '>')
   1378  1.1     joerg 			parser->state = ST_NONE;
   1379  1.1     joerg 		else if (p == '"')
   1380  1.1     joerg 			parser->state = ST_TAGAQ;
   1381  1.1     joerg 		else if (p == 'f' || p == 'F')
   1382  1.1     joerg 			parser->state = ST_F;
   1383  1.1     joerg 		else if (isspace((unsigned char)p))
   1384  1.1     joerg 			parser->state = ST_TAGA;
   1385  1.1     joerg 		else
   1386  1.1     joerg 			parser->state = ST_TAGAX;
   1387  1.1     joerg 		return 1;
   1388  1.1     joerg 	case ST_F:
   1389  1.1     joerg 		/* In a-tag -- "<a href" already found */
   1390  1.1     joerg 		if (p == '>')
   1391  1.1     joerg 			parser->state = ST_NONE;
   1392  1.1     joerg 		else if (p == '"')
   1393  1.1     joerg 			parser->state = ST_TAGAQ;
   1394  1.1     joerg 		else if (p == '=')
   1395  1.1     joerg 			parser->state = ST_HREF;
   1396  1.1     joerg 		else if (!isspace((unsigned char)p))
   1397  1.1     joerg 			parser->state = ST_TAGAX;
   1398  1.1     joerg 		return 1;
   1399  1.1     joerg 	case ST_HREF:
   1400  1.1     joerg 		/* In a-tag -- "<a href=" already found */
   1401  1.1     joerg 		if (p == '>')
   1402  1.1     joerg 			parser->state = ST_NONE;
   1403  1.1     joerg 		else if (p == '"')
   1404  1.1     joerg 			parser->state = ST_HREFQ;
   1405  1.1     joerg 		else if (!isspace((unsigned char)p))
   1406  1.1     joerg 			parser->state = ST_TAGA;
   1407  1.1     joerg 		return 1;
   1408  1.1     joerg 	case ST_HREFQ:
   1409  1.1     joerg 		/* In href of the a-tag */
   1410  1.1     joerg 		end_attr = memchr(buf, '"', len);
   1411  1.1     joerg 		if (end_attr == NULL)
   1412  1.1     joerg 			return 0;
   1413  1.1     joerg 		*end_attr = '\0';
   1414  1.1     joerg 		parser->state = ST_TAGA;
   1415  1.2  christos 		if (fetch_add_entry(parser->ue, parser->url, buf, 1))
   1416  1.2  christos 			return -1;
   1417  1.1     joerg 		return end_attr + 1 - buf;
   1418  1.1     joerg 	}
   1419  1.2  christos 	/* NOTREACHED */
   1420  1.1     joerg 	abort();
   1421  1.1     joerg }
   1422  1.1     joerg 
   1423  1.2  christos struct http_index_cache {
   1424  1.2  christos 	struct http_index_cache *next;
   1425  1.2  christos 	struct url *location;
   1426  1.2  christos 	struct url_list ue;
   1427  1.2  christos };
   1428  1.2  christos 
   1429  1.2  christos static struct http_index_cache *index_cache;
   1430  1.2  christos 
   1431  1.1     joerg /*
   1432  1.1     joerg  * List a directory
   1433  1.1     joerg  */
   1434  1.1     joerg int
   1435  1.2  christos /*ARGSUSED*/
   1436  1.2  christos fetchListHTTP(struct url_list *ue, struct url *url, const char *pattern __unused, const char *flags)
   1437  1.1     joerg {
   1438  1.1     joerg 	fetchIO *f;
   1439  1.1     joerg 	char buf[2 * PATH_MAX];
   1440  1.2  christos 	size_t buf_len, sum_processed;
   1441  1.2  christos 	ssize_t read_len, processed;
   1442  1.1     joerg 	struct index_parser state;
   1443  1.2  christos 	struct http_index_cache *cache = NULL;
   1444  1.2  christos 	int do_cache, ret;
   1445  1.2  christos 
   1446  1.2  christos 	do_cache = CHECK_FLAG('c');
   1447  1.1     joerg 
   1448  1.2  christos 	if (do_cache) {
   1449  1.2  christos 		for (cache = index_cache; cache != NULL; cache = cache->next) {
   1450  1.2  christos 			if (strcmp(cache->location->scheme, url->scheme))
   1451  1.2  christos 				continue;
   1452  1.2  christos 			if (strcmp(cache->location->user, url->user))
   1453  1.2  christos 				continue;
   1454  1.2  christos 			if (strcmp(cache->location->pwd, url->pwd))
   1455  1.2  christos 				continue;
   1456  1.2  christos 			if (strcmp(cache->location->host, url->host))
   1457  1.2  christos 				continue;
   1458  1.2  christos 			if (cache->location->port != url->port)
   1459  1.2  christos 				continue;
   1460  1.2  christos 			if (strcmp(cache->location->doc, url->doc))
   1461  1.2  christos 				continue;
   1462  1.2  christos 			return fetchAppendURLList(ue, &cache->ue);
   1463  1.2  christos 		}
   1464  1.2  christos 
   1465  1.2  christos 		cache = malloc(sizeof(*cache));
   1466  1.2  christos 		fetchInitURLList(&cache->ue);
   1467  1.2  christos 		cache->location = fetchCopyURL(url);
   1468  1.2  christos 	}
   1469  1.1     joerg 
   1470  1.1     joerg 	f = fetchGetHTTP(url, flags);
   1471  1.2  christos 	if (f == NULL) {
   1472  1.2  christos 		if (do_cache) {
   1473  1.2  christos 			fetchFreeURLList(&cache->ue);
   1474  1.2  christos 			fetchFreeURL(cache->location);
   1475  1.2  christos 			free(cache);
   1476  1.2  christos 		}
   1477  1.1     joerg 		return -1;
   1478  1.2  christos 	}
   1479  1.2  christos 
   1480  1.2  christos 	state.url = url;
   1481  1.2  christos 	state.state = ST_NONE;
   1482  1.2  christos 	if (do_cache) {
   1483  1.2  christos 		state.ue = &cache->ue;
   1484  1.2  christos 	} else {
   1485  1.2  christos 		state.ue = ue;
   1486  1.2  christos 	}
   1487  1.1     joerg 
   1488  1.1     joerg 	buf_len = 0;
   1489  1.1     joerg 
   1490  1.1     joerg 	while ((read_len = fetchIO_read(f, buf + buf_len, sizeof(buf) - buf_len)) > 0) {
   1491  1.1     joerg 		buf_len += read_len;
   1492  1.1     joerg 		sum_processed = 0;
   1493  1.1     joerg 		do {
   1494  1.1     joerg 			processed = parse_index(&state, buf + sum_processed, buf_len);
   1495  1.2  christos 			if (processed == -1)
   1496  1.2  christos 				break;
   1497  1.1     joerg 			buf_len -= processed;
   1498  1.1     joerg 			sum_processed += processed;
   1499  1.1     joerg 		} while (processed != 0 && buf_len > 0);
   1500  1.2  christos 		if (processed == -1) {
   1501  1.2  christos 			read_len = -1;
   1502  1.2  christos 			break;
   1503  1.2  christos 		}
   1504  1.1     joerg 		memmove(buf, buf + sum_processed, buf_len);
   1505  1.1     joerg 	}
   1506  1.1     joerg 
   1507  1.1     joerg 	fetchIO_close(f);
   1508  1.2  christos 
   1509  1.2  christos 	ret = read_len < 0 ? -1 : 0;
   1510  1.2  christos 
   1511  1.2  christos 	if (do_cache) {
   1512  1.2  christos 		if (ret == 0) {
   1513  1.2  christos 			cache->next = index_cache;
   1514  1.2  christos 			index_cache = cache;
   1515  1.2  christos 		}
   1516  1.2  christos 
   1517  1.2  christos 		if (fetchAppendURLList(ue, &cache->ue))
   1518  1.2  christos 			ret = -1;
   1519  1.2  christos 	}
   1520  1.2  christos 
   1521  1.2  christos 	return ret;
   1522  1.1     joerg }
   1523