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