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