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