http.c revision 169386
137535Sdes/*- 2135546Sdes * Copyright (c) 2000-2004 Dag-Erling Co�dan Sm�rgrav 337535Sdes * All rights reserved. 437535Sdes * 537535Sdes * Redistribution and use in source and binary forms, with or without 637535Sdes * modification, are permitted provided that the following conditions 737535Sdes * are met: 837535Sdes * 1. Redistributions of source code must retain the above copyright 937535Sdes * notice, this list of conditions and the following disclaimer 1037535Sdes * in this position and unchanged. 1137535Sdes * 2. Redistributions in binary form must reproduce the above copyright 1237535Sdes * notice, this list of conditions and the following disclaimer in the 1337535Sdes * documentation and/or other materials provided with the distribution. 1437535Sdes * 3. The name of the author may not be used to endorse or promote products 1563012Sdes * derived from this software without specific prior written permission. 1637535Sdes * 1737535Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1837535Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1937535Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2037535Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2137535Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2237535Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2337535Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2437535Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2537535Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2637535Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2737535Sdes */ 2837535Sdes 2984203Sdillon#include <sys/cdefs.h> 3084203Sdillon__FBSDID("$FreeBSD: head/lib/libfetch/http.c 169386 2007-05-08 19:28:03Z des $"); 3184203Sdillon 3263236Sdes/* 3363236Sdes * The following copyright applies to the base64 code: 3463236Sdes * 3563236Sdes *- 3663236Sdes * Copyright 1997 Massachusetts Institute of Technology 3763236Sdes * 3863236Sdes * Permission to use, copy, modify, and distribute this software and 3963236Sdes * its documentation for any purpose and without fee is hereby 4063236Sdes * granted, provided that both the above copyright notice and this 4163236Sdes * permission notice appear in all copies, that both the above 4263236Sdes * copyright notice and this permission notice appear in all 4363236Sdes * supporting documentation, and that the name of M.I.T. not be used 4463236Sdes * in advertising or publicity pertaining to distribution of the 4563236Sdes * software without specific, written prior permission. M.I.T. makes 4663236Sdes * no representations about the suitability of this software for any 4763236Sdes * purpose. It is provided "as is" without express or implied 4863236Sdes * warranty. 4990267Sdes * 5063236Sdes * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 5163236Sdes * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 5263236Sdes * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 5363236Sdes * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 5463236Sdes * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 5563236Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 5663236Sdes * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 5763236Sdes * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 5863236Sdes * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 5963236Sdes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 6063236Sdes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 6163236Sdes * SUCH DAMAGE. 6263236Sdes */ 6363236Sdes 6437535Sdes#include <sys/param.h> 6560737Sume#include <sys/socket.h> 6637535Sdes 6763012Sdes#include <ctype.h> 6837535Sdes#include <err.h> 6963012Sdes#include <errno.h> 7060376Sdes#include <locale.h> 7160189Sdes#include <netdb.h> 7237608Sdes#include <stdarg.h> 7337535Sdes#include <stdio.h> 7437535Sdes#include <stdlib.h> 7537535Sdes#include <string.h> 7660376Sdes#include <time.h> 7737535Sdes#include <unistd.h> 7837535Sdes 79141958Skbyanc#include <netinet/in.h> 80141958Skbyanc#include <netinet/tcp.h> 81141958Skbyanc 8237535Sdes#include "fetch.h" 8340939Sdes#include "common.h" 8441862Sdes#include "httperr.h" 8537535Sdes 8663012Sdes/* Maximum number of redirects to follow */ 8763012Sdes#define MAX_REDIRECT 5 8837535Sdes 8963012Sdes/* Symbolic names for reply codes we care about */ 9063012Sdes#define HTTP_OK 200 9163012Sdes#define HTTP_PARTIAL 206 9263012Sdes#define HTTP_MOVED_PERM 301 9363012Sdes#define HTTP_MOVED_TEMP 302 9463012Sdes#define HTTP_SEE_OTHER 303 95169386Sdes#define HTTP_TEMP_REDIRECT 307 9663012Sdes#define HTTP_NEED_AUTH 401 9787317Sdes#define HTTP_NEED_PROXY_AUTH 407 98125696Sdes#define HTTP_BAD_RANGE 416 9963012Sdes#define HTTP_PROTOCOL_ERROR 999 10060196Sdes 10163012Sdes#define HTTP_REDIRECT(xyz) ((xyz) == HTTP_MOVED_PERM \ 10290267Sdes || (xyz) == HTTP_MOVED_TEMP \ 103169386Sdes || (xyz) == HTTP_TEMP_REDIRECT \ 10490267Sdes || (xyz) == HTTP_SEE_OTHER) 10563012Sdes 10688771Sdes#define HTTP_ERROR(xyz) ((xyz) > 400 && (xyz) < 599) 10763012Sdes 10890267Sdes 10963012Sdes/***************************************************************************** 11063012Sdes * I/O functions for decoding chunked streams 11163012Sdes */ 11263012Sdes 11397859Sdesstruct httpio 11437535Sdes{ 11597858Sdes conn_t *conn; /* connection */ 11697866Sdes int chunked; /* chunked mode */ 11797858Sdes char *buf; /* chunk buffer */ 11897866Sdes size_t bufsize; /* size of chunk buffer */ 11997866Sdes ssize_t buflen; /* amount of data currently in buffer */ 12097866Sdes int bufpos; /* current read offset in buffer */ 12197858Sdes int eof; /* end-of-file flag */ 12297858Sdes int error; /* error flag */ 12397858Sdes size_t chunksize; /* remaining size of current chunk */ 12463281Sdes#ifndef NDEBUG 12590267Sdes size_t total; 12663012Sdes#endif 12737535Sdes}; 12837535Sdes 12937608Sdes/* 13063012Sdes * Get next chunk header 13137608Sdes */ 13237608Sdesstatic int 13397859Sdes_http_new_chunk(struct httpio *io) 13437608Sdes{ 13590267Sdes char *p; 13690267Sdes 13797859Sdes if (_fetch_getln(io->conn) == -1) 13890267Sdes return (-1); 13990267Sdes 14097859Sdes if (io->conn->buflen < 2 || !ishexnumber(*io->conn->buf)) 14190267Sdes return (-1); 14290267Sdes 14397859Sdes for (p = io->conn->buf; *p && !isspace(*p); ++p) { 14490267Sdes if (*p == ';') 14590267Sdes break; 14690267Sdes if (!ishexnumber(*p)) 14790267Sdes return (-1); 14890267Sdes if (isdigit(*p)) { 14997859Sdes io->chunksize = io->chunksize * 16 + 15090267Sdes *p - '0'; 15190267Sdes } else { 15297859Sdes io->chunksize = io->chunksize * 16 + 15390267Sdes 10 + tolower(*p) - 'a'; 15490267Sdes } 15590267Sdes } 15690267Sdes 15763281Sdes#ifndef NDEBUG 15890267Sdes if (fetchDebug) { 15997859Sdes io->total += io->chunksize; 16097859Sdes if (io->chunksize == 0) 161106207Sdes fprintf(stderr, "%s(): end of last chunk\n", __func__); 16290267Sdes else 163106207Sdes fprintf(stderr, "%s(): new chunk: %lu (%lu)\n", 164106207Sdes __func__, (unsigned long)io->chunksize, 165106207Sdes (unsigned long)io->total); 16690267Sdes } 16763012Sdes#endif 16890267Sdes 16997859Sdes return (io->chunksize); 17037608Sdes} 17137608Sdes 17237608Sdes/* 17397866Sdes * Grow the input buffer to at least len bytes 17497866Sdes */ 17597866Sdesstatic inline int 17697866Sdes_http_growbuf(struct httpio *io, size_t len) 17797866Sdes{ 17897866Sdes char *tmp; 17997866Sdes 18097866Sdes if (io->bufsize >= len) 18197866Sdes return (0); 18297866Sdes 18397866Sdes if ((tmp = realloc(io->buf, len)) == NULL) 18497866Sdes return (-1); 18597866Sdes io->buf = tmp; 18697866Sdes io->bufsize = len; 187106044Sdes return (0); 18897866Sdes} 18997866Sdes 19097866Sdes/* 19137608Sdes * Fill the input buffer, do chunk decoding on the fly 19237608Sdes */ 19363012Sdesstatic int 19497866Sdes_http_fillbuf(struct httpio *io, size_t len) 19537535Sdes{ 19697859Sdes if (io->error) 19790267Sdes return (-1); 19897859Sdes if (io->eof) 19990267Sdes return (0); 20090267Sdes 20197866Sdes if (io->chunked == 0) { 20297866Sdes if (_http_growbuf(io, len) == -1) 20397866Sdes return (-1); 204106185Sdes if ((io->buflen = _fetch_read(io->conn, io->buf, len)) == -1) { 205106185Sdes io->error = 1; 20697866Sdes return (-1); 207106185Sdes } 20897866Sdes io->bufpos = 0; 20997866Sdes return (io->buflen); 21097866Sdes } 21197866Sdes 21297859Sdes if (io->chunksize == 0) { 21397859Sdes switch (_http_new_chunk(io)) { 21490267Sdes case -1: 21597859Sdes io->error = 1; 21690267Sdes return (-1); 21790267Sdes case 0: 21897859Sdes io->eof = 1; 21990267Sdes return (0); 22090267Sdes } 22137535Sdes } 22263012Sdes 22397866Sdes if (len > io->chunksize) 22497866Sdes len = io->chunksize; 22597866Sdes if (_http_growbuf(io, len) == -1) 22690267Sdes return (-1); 227106185Sdes if ((io->buflen = _fetch_read(io->conn, io->buf, len)) == -1) { 228106185Sdes io->error = 1; 22997866Sdes return (-1); 230106185Sdes } 23197866Sdes io->chunksize -= io->buflen; 23290267Sdes 23397859Sdes if (io->chunksize == 0) { 23497856Sdes char endl[2]; 23597856Sdes 23697866Sdes if (_fetch_read(io->conn, endl, 2) != 2 || 23797856Sdes endl[0] != '\r' || endl[1] != '\n') 23890267Sdes return (-1); 23990267Sdes } 24090267Sdes 24197866Sdes io->bufpos = 0; 24290267Sdes 24397866Sdes return (io->buflen); 24437535Sdes} 24537535Sdes 24637608Sdes/* 24737608Sdes * Read function 24837608Sdes */ 24937535Sdesstatic int 25063012Sdes_http_readfn(void *v, char *buf, int len) 25137535Sdes{ 25297859Sdes struct httpio *io = (struct httpio *)v; 25390267Sdes int l, pos; 25463012Sdes 25597859Sdes if (io->error) 25690267Sdes return (-1); 25797859Sdes if (io->eof) 25890267Sdes return (0); 25963012Sdes 26090267Sdes for (pos = 0; len > 0; pos += l, len -= l) { 26190267Sdes /* empty buffer */ 26297866Sdes if (!io->buf || io->bufpos == io->buflen) 26397866Sdes if (_http_fillbuf(io, len) < 1) 26490267Sdes break; 26597866Sdes l = io->buflen - io->bufpos; 26690267Sdes if (len < l) 26790267Sdes l = len; 26897866Sdes bcopy(io->buf + io->bufpos, buf + pos, l); 26997866Sdes io->bufpos += l; 27090267Sdes } 27137535Sdes 27297859Sdes if (!pos && io->error) 27390267Sdes return (-1); 27490267Sdes return (pos); 27537535Sdes} 27637535Sdes 27737608Sdes/* 27837608Sdes * Write function 27937608Sdes */ 28037535Sdesstatic int 28163012Sdes_http_writefn(void *v, const char *buf, int len) 28237535Sdes{ 28397859Sdes struct httpio *io = (struct httpio *)v; 28490267Sdes 28597866Sdes return (_fetch_write(io->conn, buf, len)); 28637535Sdes} 28737535Sdes 28837608Sdes/* 28937608Sdes * Close function 29037608Sdes */ 29137535Sdesstatic int 29263012Sdes_http_closefn(void *v) 29337535Sdes{ 29497859Sdes struct httpio *io = (struct httpio *)v; 29590267Sdes int r; 29663012Sdes 29797859Sdes r = _fetch_close(io->conn); 29897859Sdes if (io->buf) 29997859Sdes free(io->buf); 30097859Sdes free(io); 30190267Sdes return (r); 30237535Sdes} 30337535Sdes 30437608Sdes/* 30563012Sdes * Wrap a file descriptor up 30637608Sdes */ 30763012Sdesstatic FILE * 30897866Sdes_http_funopen(conn_t *conn, int chunked) 30937535Sdes{ 31097859Sdes struct httpio *io; 31190267Sdes FILE *f; 31263012Sdes 313109967Sdes if ((io = calloc(1, sizeof(*io))) == NULL) { 31490267Sdes _fetch_syserr(); 31590267Sdes return (NULL); 31690267Sdes } 31797859Sdes io->conn = conn; 31897866Sdes io->chunked = chunked; 31997859Sdes f = funopen(io, _http_readfn, _http_writefn, NULL, _http_closefn); 32090267Sdes if (f == NULL) { 32190267Sdes _fetch_syserr(); 32297859Sdes free(io); 32390267Sdes return (NULL); 32490267Sdes } 32590267Sdes return (f); 32663012Sdes} 32763012Sdes 32890267Sdes 32963012Sdes/***************************************************************************** 33063012Sdes * Helper functions for talking to the server and parsing its replies 33163012Sdes */ 33263012Sdes 33363012Sdes/* Header types */ 33463012Sdestypedef enum { 33590267Sdes hdr_syserror = -2, 33690267Sdes hdr_error = -1, 33790267Sdes hdr_end = 0, 33890267Sdes hdr_unknown = 1, 33990267Sdes hdr_content_length, 34090267Sdes hdr_content_range, 34190267Sdes hdr_last_modified, 34290267Sdes hdr_location, 34390267Sdes hdr_transfer_encoding, 34490267Sdes hdr_www_authenticate 34585093Sdes} hdr_t; 34663012Sdes 34763012Sdes/* Names of interesting headers */ 34863012Sdesstatic struct { 34990267Sdes hdr_t num; 35090267Sdes const char *name; 35163012Sdes} hdr_names[] = { 35290267Sdes { hdr_content_length, "Content-Length" }, 35390267Sdes { hdr_content_range, "Content-Range" }, 35490267Sdes { hdr_last_modified, "Last-Modified" }, 35590267Sdes { hdr_location, "Location" }, 35690267Sdes { hdr_transfer_encoding, "Transfer-Encoding" }, 35790267Sdes { hdr_www_authenticate, "WWW-Authenticate" }, 35890267Sdes { hdr_unknown, NULL }, 35963012Sdes}; 36063012Sdes 36163012Sdes/* 36263012Sdes * Send a formatted line; optionally echo to terminal 36363012Sdes */ 36463012Sdesstatic int 36597856Sdes_http_cmd(conn_t *conn, const char *fmt, ...) 36663012Sdes{ 36790267Sdes va_list ap; 36890267Sdes size_t len; 36990267Sdes char *msg; 37090267Sdes int r; 37163012Sdes 37290267Sdes va_start(ap, fmt); 37390267Sdes len = vasprintf(&msg, fmt, ap); 37490267Sdes va_end(ap); 37590267Sdes 37690267Sdes if (msg == NULL) { 37790267Sdes errno = ENOMEM; 37890267Sdes _fetch_syserr(); 37990267Sdes return (-1); 38090267Sdes } 38190267Sdes 38297856Sdes r = _fetch_putln(conn, msg, len); 38390267Sdes free(msg); 38490267Sdes 38590267Sdes if (r == -1) { 38690267Sdes _fetch_syserr(); 38790267Sdes return (-1); 38890267Sdes } 38990267Sdes 39090267Sdes return (0); 39163012Sdes} 39263012Sdes 39363012Sdes/* 39463012Sdes * Get and parse status line 39563012Sdes */ 39663012Sdesstatic int 39797856Sdes_http_get_reply(conn_t *conn) 39863012Sdes{ 39990267Sdes char *p; 40090267Sdes 40197856Sdes if (_fetch_getln(conn) == -1) 40290267Sdes return (-1); 40390267Sdes /* 40490267Sdes * A valid status line looks like "HTTP/m.n xyz reason" where m 40590267Sdes * and n are the major and minor protocol version numbers and xyz 40690267Sdes * is the reply code. 40790267Sdes * Unfortunately, there are servers out there (NCSA 1.5.1, to name 40890267Sdes * just one) that do not send a version number, so we can't rely 40990267Sdes * on finding one, but if we do, insist on it being 1.0 or 1.1. 41090267Sdes * We don't care about the reason phrase. 41190267Sdes */ 41297856Sdes if (strncmp(conn->buf, "HTTP", 4) != 0) 41390267Sdes return (HTTP_PROTOCOL_ERROR); 41497856Sdes p = conn->buf + 4; 41590267Sdes if (*p == '/') { 41690267Sdes if (p[1] != '1' || p[2] != '.' || (p[3] != '0' && p[3] != '1')) 41790267Sdes return (HTTP_PROTOCOL_ERROR); 41890267Sdes p += 4; 41990267Sdes } 42090267Sdes if (*p != ' ' || !isdigit(p[1]) || !isdigit(p[2]) || !isdigit(p[3])) 42190267Sdes return (HTTP_PROTOCOL_ERROR); 42290267Sdes 42397856Sdes conn->err = (p[1] - '0') * 100 + (p[2] - '0') * 10 + (p[3] - '0'); 42497856Sdes return (conn->err); 42537535Sdes} 42637535Sdes 42737608Sdes/* 42890267Sdes * Check a header; if the type matches the given string, return a pointer 42990267Sdes * to the beginning of the value. 43063012Sdes */ 43175891Sarchiestatic const char * 43275891Sarchie_http_match(const char *str, const char *hdr) 43363012Sdes{ 43490267Sdes while (*str && *hdr && tolower(*str++) == tolower(*hdr++)) 43590267Sdes /* nothing */; 43690267Sdes if (*str || *hdr != ':') 43790267Sdes return (NULL); 43890267Sdes while (*hdr && isspace(*++hdr)) 43990267Sdes /* nothing */; 44090267Sdes return (hdr); 44163012Sdes} 44263012Sdes 44363012Sdes/* 44463012Sdes * Get the next header and return the appropriate symbolic code. 44563012Sdes */ 44685093Sdesstatic hdr_t 44797856Sdes_http_next_header(conn_t *conn, const char **p) 44863012Sdes{ 44990267Sdes int i; 45090267Sdes 45197856Sdes if (_fetch_getln(conn) == -1) 45290267Sdes return (hdr_syserror); 45397856Sdes while (conn->buflen && isspace(conn->buf[conn->buflen - 1])) 45497856Sdes conn->buflen--; 45597856Sdes conn->buf[conn->buflen] = '\0'; 45697856Sdes if (conn->buflen == 0) 45797856Sdes return (hdr_end); 45890267Sdes /* 45990267Sdes * We could check for malformed headers but we don't really care. 46090267Sdes * A valid header starts with a token immediately followed by a 46190267Sdes * colon; a token is any sequence of non-control, non-whitespace 46290267Sdes * characters except "()<>@,;:\\\"{}". 46390267Sdes */ 46490267Sdes for (i = 0; hdr_names[i].num != hdr_unknown; i++) 46597856Sdes if ((*p = _http_match(hdr_names[i].name, conn->buf)) != NULL) 46690267Sdes return (hdr_names[i].num); 46790267Sdes return (hdr_unknown); 46863012Sdes} 46963012Sdes 47063012Sdes/* 47163012Sdes * Parse a last-modified header 47263012Sdes */ 47363716Sdesstatic int 47475891Sarchie_http_parse_mtime(const char *p, time_t *mtime) 47563012Sdes{ 47690267Sdes char locale[64], *r; 47790267Sdes struct tm tm; 47863012Sdes 479109967Sdes strncpy(locale, setlocale(LC_TIME, NULL), sizeof(locale)); 48090267Sdes setlocale(LC_TIME, "C"); 48190267Sdes r = strptime(p, "%a, %d %b %Y %H:%M:%S GMT", &tm); 48290267Sdes /* XXX should add support for date-2 and date-3 */ 48390267Sdes setlocale(LC_TIME, locale); 48490267Sdes if (r == NULL) 48590267Sdes return (-1); 48690267Sdes DEBUG(fprintf(stderr, "last modified: [%04d-%02d-%02d " 48788769Sdes "%02d:%02d:%02d]\n", 48863012Sdes tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, 48963012Sdes tm.tm_hour, tm.tm_min, tm.tm_sec)); 49090267Sdes *mtime = timegm(&tm); 49190267Sdes return (0); 49263012Sdes} 49363012Sdes 49463012Sdes/* 49563012Sdes * Parse a content-length header 49663012Sdes */ 49763716Sdesstatic int 49875891Sarchie_http_parse_length(const char *p, off_t *length) 49963012Sdes{ 50090267Sdes off_t len; 50190267Sdes 50290267Sdes for (len = 0; *p && isdigit(*p); ++p) 50390267Sdes len = len * 10 + (*p - '0'); 50490267Sdes if (*p) 50590267Sdes return (-1); 50690267Sdes DEBUG(fprintf(stderr, "content length: [%lld]\n", 50790267Sdes (long long)len)); 50890267Sdes *length = len; 50990267Sdes return (0); 51063012Sdes} 51163012Sdes 51263012Sdes/* 51363012Sdes * Parse a content-range header 51463012Sdes */ 51563716Sdesstatic int 51675891Sarchie_http_parse_range(const char *p, off_t *offset, off_t *length, off_t *size) 51763012Sdes{ 51890267Sdes off_t first, last, len; 51963716Sdes 52090267Sdes if (strncasecmp(p, "bytes ", 6) != 0) 52190267Sdes return (-1); 522125696Sdes p += 6; 523125696Sdes if (*p == '*') { 524125696Sdes first = last = -1; 525125696Sdes ++p; 526125696Sdes } else { 527125696Sdes for (first = 0; *p && isdigit(*p); ++p) 528125696Sdes first = first * 10 + *p - '0'; 529125696Sdes if (*p != '-') 530125696Sdes return (-1); 531125696Sdes for (last = 0, ++p; *p && isdigit(*p); ++p) 532125696Sdes last = last * 10 + *p - '0'; 533125696Sdes } 53490267Sdes if (first > last || *p != '/') 53590267Sdes return (-1); 53690267Sdes for (len = 0, ++p; *p && isdigit(*p); ++p) 53790267Sdes len = len * 10 + *p - '0'; 53890267Sdes if (*p || len < last - first + 1) 53990267Sdes return (-1); 540125696Sdes if (first == -1) { 541125696Sdes DEBUG(fprintf(stderr, "content range: [*/%lld]\n", 542125696Sdes (long long)len)); 543125696Sdes *length = 0; 544125696Sdes } else { 545125696Sdes DEBUG(fprintf(stderr, "content range: [%lld-%lld/%lld]\n", 546125696Sdes (long long)first, (long long)last, (long long)len)); 547125696Sdes *length = last - first + 1; 548125696Sdes } 54990267Sdes *offset = first; 55090267Sdes *size = len; 55190267Sdes return (0); 55263012Sdes} 55363012Sdes 55490267Sdes 55563012Sdes/***************************************************************************** 55663012Sdes * Helper functions for authorization 55763012Sdes */ 55863012Sdes 55963012Sdes/* 56037608Sdes * Base64 encoding 56137608Sdes */ 56262965Sdesstatic char * 56390267Sdes_http_base64(const char *src) 56437608Sdes{ 56590267Sdes static const char base64[] = 56690267Sdes "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 56790267Sdes "abcdefghijklmnopqrstuvwxyz" 56890267Sdes "0123456789+/"; 56990267Sdes char *str, *dst; 57090267Sdes size_t l; 57190267Sdes int t, r; 57262965Sdes 57390267Sdes l = strlen(src); 574133280Sdes if ((str = malloc(((l + 2) / 3) * 4 + 1)) == NULL) 57590267Sdes return (NULL); 57690267Sdes dst = str; 57790267Sdes r = 0; 57837608Sdes 57990267Sdes while (l >= 3) { 58090267Sdes t = (src[0] << 16) | (src[1] << 8) | src[2]; 58190267Sdes dst[0] = base64[(t >> 18) & 0x3f]; 58290267Sdes dst[1] = base64[(t >> 12) & 0x3f]; 58390267Sdes dst[2] = base64[(t >> 6) & 0x3f]; 58490267Sdes dst[3] = base64[(t >> 0) & 0x3f]; 58590267Sdes src += 3; l -= 3; 58690267Sdes dst += 4; r += 4; 58790267Sdes } 58837608Sdes 58990267Sdes switch (l) { 59090267Sdes case 2: 59190267Sdes t = (src[0] << 16) | (src[1] << 8); 59290267Sdes dst[0] = base64[(t >> 18) & 0x3f]; 59390267Sdes dst[1] = base64[(t >> 12) & 0x3f]; 59490267Sdes dst[2] = base64[(t >> 6) & 0x3f]; 59590267Sdes dst[3] = '='; 59690267Sdes dst += 4; 59790267Sdes r += 4; 59890267Sdes break; 59990267Sdes case 1: 60090267Sdes t = src[0] << 16; 60190267Sdes dst[0] = base64[(t >> 18) & 0x3f]; 60290267Sdes dst[1] = base64[(t >> 12) & 0x3f]; 60390267Sdes dst[2] = dst[3] = '='; 60490267Sdes dst += 4; 60590267Sdes r += 4; 60690267Sdes break; 60790267Sdes case 0: 60890267Sdes break; 60990267Sdes } 61090267Sdes 61190267Sdes *dst = 0; 61290267Sdes return (str); 61337608Sdes} 61437608Sdes 61537608Sdes/* 61637608Sdes * Encode username and password 61737608Sdes */ 61862965Sdesstatic int 61997856Sdes_http_basic_auth(conn_t *conn, const char *hdr, const char *usr, const char *pwd) 62037608Sdes{ 62190267Sdes char *upw, *auth; 62290267Sdes int r; 62337608Sdes 62490267Sdes DEBUG(fprintf(stderr, "usr: [%s]\n", usr)); 62590267Sdes DEBUG(fprintf(stderr, "pwd: [%s]\n", pwd)); 62690267Sdes if (asprintf(&upw, "%s:%s", usr, pwd) == -1) 62790267Sdes return (-1); 62890267Sdes auth = _http_base64(upw); 62990267Sdes free(upw); 63090267Sdes if (auth == NULL) 63190267Sdes return (-1); 63297856Sdes r = _http_cmd(conn, "%s: Basic %s", hdr, auth); 63390267Sdes free(auth); 63490267Sdes return (r); 63562965Sdes} 63662965Sdes 63762965Sdes/* 63862965Sdes * Send an authorization header 63962965Sdes */ 64062965Sdesstatic int 64197856Sdes_http_authorize(conn_t *conn, const char *hdr, const char *p) 64262965Sdes{ 64390267Sdes /* basic authorization */ 64490267Sdes if (strncasecmp(p, "basic:", 6) == 0) { 64590267Sdes char *user, *pwd, *str; 64690267Sdes int r; 64762965Sdes 64890267Sdes /* skip realm */ 64990267Sdes for (p += 6; *p && *p != ':'; ++p) 65090267Sdes /* nothing */ ; 65190267Sdes if (!*p || strchr(++p, ':') == NULL) 65290267Sdes return (-1); 65390267Sdes if ((str = strdup(p)) == NULL) 65490267Sdes return (-1); /* XXX */ 65590267Sdes user = str; 65690267Sdes pwd = strchr(str, ':'); 65790267Sdes *pwd++ = '\0'; 65897856Sdes r = _http_basic_auth(conn, hdr, user, pwd); 65990267Sdes free(str); 66090267Sdes return (r); 66190267Sdes } 66290267Sdes return (-1); 66337608Sdes} 66437608Sdes 66590267Sdes 66663012Sdes/***************************************************************************** 66763012Sdes * Helper functions for connecting to a server or proxy 66863012Sdes */ 66963012Sdes 67037608Sdes/* 67190267Sdes * Connect to the correct HTTP server or proxy. 67263012Sdes */ 67397856Sdesstatic conn_t * 67475891Sarchie_http_connect(struct url *URL, struct url *purl, const char *flags) 67563012Sdes{ 67697856Sdes conn_t *conn; 67790267Sdes int verbose; 678141958Skbyanc int af, val; 67990267Sdes 68063012Sdes#ifdef INET6 68190267Sdes af = AF_UNSPEC; 68260737Sume#else 68390267Sdes af = AF_INET; 68460737Sume#endif 68590267Sdes 68690267Sdes verbose = CHECK_FLAG('v'); 68790267Sdes if (CHECK_FLAG('4')) 68890267Sdes af = AF_INET; 68967043Sdes#ifdef INET6 69090267Sdes else if (CHECK_FLAG('6')) 69190267Sdes af = AF_INET6; 69267043Sdes#endif 69367043Sdes 69497868Sdes if (purl && strcasecmp(URL->scheme, SCHEME_HTTPS) != 0) { 69590267Sdes URL = purl; 69690267Sdes } else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0) { 69790267Sdes /* can't talk http to an ftp server */ 69890267Sdes /* XXX should set an error code */ 69997856Sdes return (NULL); 70090267Sdes } 70190267Sdes 70297856Sdes if ((conn = _fetch_connect(URL->host, URL->port, af, verbose)) == NULL) 70390267Sdes /* _fetch_connect() has already set an error code */ 70497856Sdes return (NULL); 70597868Sdes if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0 && 70697868Sdes _fetch_ssl(conn, verbose) == -1) { 70797868Sdes _fetch_close(conn); 70897891Sdes /* grrr */ 70997891Sdes errno = EAUTH; 71097891Sdes _fetch_syserr(); 71197868Sdes return (NULL); 71297868Sdes } 713141958Skbyanc 714141958Skbyanc val = 1; 715141958Skbyanc setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val, sizeof(val)); 716141958Skbyanc 71797856Sdes return (conn); 71867043Sdes} 71967043Sdes 72067043Sdesstatic struct url * 721112081Sdes_http_get_proxy(const char *flags) 72267043Sdes{ 72390267Sdes struct url *purl; 72490267Sdes char *p; 72590267Sdes 726112797Sdes if (flags != NULL && strchr(flags, 'd') != NULL) 727112081Sdes return (NULL); 72890267Sdes if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) && 729149414Sdes *p && (purl = fetchParseURL(p))) { 73090267Sdes if (!*purl->scheme) 73190267Sdes strcpy(purl->scheme, SCHEME_HTTP); 73290267Sdes if (!purl->port) 73390267Sdes purl->port = _fetch_default_proxy_port(purl->scheme); 73490267Sdes if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0) 73590267Sdes return (purl); 73690267Sdes fetchFreeURL(purl); 73790267Sdes } 73890267Sdes return (NULL); 73960376Sdes} 74060376Sdes 74188771Sdesstatic void 74288771Sdes_http_print_html(FILE *out, FILE *in) 74388771Sdes{ 74490267Sdes size_t len; 74590267Sdes char *line, *p, *q; 74690267Sdes int comment, tag; 74788771Sdes 74890267Sdes comment = tag = 0; 74990267Sdes while ((line = fgetln(in, &len)) != NULL) { 75090267Sdes while (len && isspace(line[len - 1])) 75190267Sdes --len; 75290267Sdes for (p = q = line; q < line + len; ++q) { 75390267Sdes if (comment && *q == '-') { 75490267Sdes if (q + 2 < line + len && 75590267Sdes strcmp(q, "-->") == 0) { 75690267Sdes tag = comment = 0; 75790267Sdes q += 2; 75890267Sdes } 75990267Sdes } else if (tag && !comment && *q == '>') { 76090267Sdes p = q + 1; 76190267Sdes tag = 0; 76290267Sdes } else if (!tag && *q == '<') { 76390267Sdes if (q > p) 76490267Sdes fwrite(p, q - p, 1, out); 76590267Sdes tag = 1; 76690267Sdes if (q + 3 < line + len && 76790267Sdes strcmp(q, "<!--") == 0) { 76890267Sdes comment = 1; 76990267Sdes q += 3; 77090267Sdes } 77190267Sdes } 77288771Sdes } 77390267Sdes if (!tag && q > p) 77490267Sdes fwrite(p, q - p, 1, out); 77590267Sdes fputc('\n', out); 77688771Sdes } 77788771Sdes} 77888771Sdes 77990267Sdes 78063012Sdes/***************************************************************************** 78163012Sdes * Core 78260954Sdes */ 78360954Sdes 78460954Sdes/* 78563012Sdes * Send a request and process the reply 78697866Sdes * 78797866Sdes * XXX This function is way too long, the do..while loop should be split 78897866Sdes * XXX off into a separate function. 78960376Sdes */ 79067043SdesFILE * 79175891Sarchie_http_request(struct url *URL, const char *op, struct url_stat *us, 79290267Sdes struct url *purl, const char *flags) 79360376Sdes{ 79497856Sdes conn_t *conn; 79590267Sdes struct url *url, *new; 79690267Sdes int chunked, direct, need_auth, noredirect, verbose; 797143049Skbyanc int e, i, n, val; 79890267Sdes off_t offset, clength, length, size; 79990267Sdes time_t mtime; 80090267Sdes const char *p; 80190267Sdes FILE *f; 80290267Sdes hdr_t h; 803107372Sdes char hbuf[MAXHOSTNAMELEN + 7], *host; 80463012Sdes 80590267Sdes direct = CHECK_FLAG('d'); 80690267Sdes noredirect = CHECK_FLAG('A'); 80790267Sdes verbose = CHECK_FLAG('v'); 80860737Sume 80990267Sdes if (direct && purl) { 81090267Sdes fetchFreeURL(purl); 81190267Sdes purl = NULL; 81290267Sdes } 81363716Sdes 81490267Sdes /* try the provided URL first */ 81590267Sdes url = URL; 81663012Sdes 81790267Sdes /* if the A flag is set, we only get one try */ 81890267Sdes n = noredirect ? 1 : MAX_REDIRECT; 81990267Sdes i = 0; 82063012Sdes 82198422Sdes e = HTTP_PROTOCOL_ERROR; 82290267Sdes need_auth = 0; 82390267Sdes do { 82490267Sdes new = NULL; 82590267Sdes chunked = 0; 82690267Sdes offset = 0; 82790267Sdes clength = -1; 82890267Sdes length = -1; 82990267Sdes size = -1; 83090267Sdes mtime = 0; 83190267Sdes 83290267Sdes /* check port */ 83390267Sdes if (!url->port) 83490267Sdes url->port = _fetch_default_port(url->scheme); 83590267Sdes 83690267Sdes /* were we redirected to an FTP URL? */ 83790267Sdes if (purl == NULL && strcmp(url->scheme, SCHEME_FTP) == 0) { 83890267Sdes if (strcmp(op, "GET") == 0) 83990267Sdes return (_ftp_request(url, "RETR", us, purl, flags)); 84090267Sdes else if (strcmp(op, "HEAD") == 0) 84190267Sdes return (_ftp_request(url, "STAT", us, purl, flags)); 84290267Sdes } 84390267Sdes 84490267Sdes /* connect to server or proxy */ 84597856Sdes if ((conn = _http_connect(url, purl, flags)) == NULL) 84690267Sdes goto ouch; 84790267Sdes 84890267Sdes host = url->host; 84960737Sume#ifdef INET6 85090267Sdes if (strchr(url->host, ':')) { 85190267Sdes snprintf(hbuf, sizeof(hbuf), "[%s]", url->host); 85290267Sdes host = hbuf; 85390267Sdes } 85460737Sume#endif 855107372Sdes if (url->port != _fetch_default_port(url->scheme)) { 856107372Sdes if (host != hbuf) { 857107372Sdes strcpy(hbuf, host); 858107372Sdes host = hbuf; 859107372Sdes } 860107372Sdes snprintf(hbuf + strlen(hbuf), 861107372Sdes sizeof(hbuf) - strlen(hbuf), ":%d", url->port); 862107372Sdes } 86337535Sdes 86490267Sdes /* send request */ 86590267Sdes if (verbose) 866107372Sdes _fetch_info("requesting %s://%s%s", 867107372Sdes url->scheme, host, url->doc); 86890267Sdes if (purl) { 869107372Sdes _http_cmd(conn, "%s %s://%s%s HTTP/1.1", 870107372Sdes op, url->scheme, host, url->doc); 87190267Sdes } else { 87297856Sdes _http_cmd(conn, "%s %s HTTP/1.1", 87390267Sdes op, url->doc); 87490267Sdes } 87537535Sdes 87690267Sdes /* virtual host */ 877107372Sdes _http_cmd(conn, "Host: %s", host); 87890267Sdes 87990267Sdes /* proxy authorization */ 88090267Sdes if (purl) { 88190267Sdes if (*purl->user || *purl->pwd) 88297856Sdes _http_basic_auth(conn, "Proxy-Authorization", 88390267Sdes purl->user, purl->pwd); 88490267Sdes else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL && *p != '\0') 88597856Sdes _http_authorize(conn, "Proxy-Authorization", p); 88690267Sdes } 88790267Sdes 88890267Sdes /* server authorization */ 88990267Sdes if (need_auth || *url->user || *url->pwd) { 89090267Sdes if (*url->user || *url->pwd) 89197856Sdes _http_basic_auth(conn, "Authorization", url->user, url->pwd); 89290267Sdes else if ((p = getenv("HTTP_AUTH")) != NULL && *p != '\0') 89397856Sdes _http_authorize(conn, "Authorization", p); 89490267Sdes else if (fetchAuthMethod && fetchAuthMethod(url) == 0) { 89597856Sdes _http_basic_auth(conn, "Authorization", url->user, url->pwd); 89690267Sdes } else { 89790267Sdes _http_seterr(HTTP_NEED_AUTH); 89890267Sdes goto ouch; 89990267Sdes } 90090267Sdes } 90190267Sdes 90290267Sdes /* other headers */ 903107372Sdes if ((p = getenv("HTTP_REFERER")) != NULL && *p != '\0') { 904107372Sdes if (strcasecmp(p, "auto") == 0) 905107372Sdes _http_cmd(conn, "Referer: %s://%s%s", 906107372Sdes url->scheme, host, url->doc); 907107372Sdes else 908107372Sdes _http_cmd(conn, "Referer: %s", p); 909107372Sdes } 91090267Sdes if ((p = getenv("HTTP_USER_AGENT")) != NULL && *p != '\0') 91197856Sdes _http_cmd(conn, "User-Agent: %s", p); 91290267Sdes else 91397856Sdes _http_cmd(conn, "User-Agent: %s " _LIBFETCH_VER, getprogname()); 914109693Sdes if (url->offset > 0) 91597856Sdes _http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset); 91697856Sdes _http_cmd(conn, "Connection: close"); 91797856Sdes _http_cmd(conn, ""); 91890267Sdes 919143049Skbyanc /* 920143049Skbyanc * Force the queued request to be dispatched. Normally, one 921143049Skbyanc * would do this with shutdown(2) but squid proxies can be 922143049Skbyanc * configured to disallow such half-closed connections. To 923143049Skbyanc * be compatible with such configurations, fiddle with socket 924143049Skbyanc * options to force the pending data to be written. 925143049Skbyanc */ 926143049Skbyanc val = 0; 927143049Skbyanc setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val, 928143049Skbyanc sizeof(val)); 929143049Skbyanc val = 1; 930143049Skbyanc setsockopt(conn->sd, IPPROTO_TCP, TCP_NODELAY, &val, 931143049Skbyanc sizeof(val)); 932143049Skbyanc 93390267Sdes /* get reply */ 93497856Sdes switch (_http_get_reply(conn)) { 93590267Sdes case HTTP_OK: 93690267Sdes case HTTP_PARTIAL: 93790267Sdes /* fine */ 93890267Sdes break; 93990267Sdes case HTTP_MOVED_PERM: 94090267Sdes case HTTP_MOVED_TEMP: 94190267Sdes case HTTP_SEE_OTHER: 94290267Sdes /* 943125695Sdes * Not so fine, but we still have to read the 944125695Sdes * headers to get the new location. 94590267Sdes */ 94690267Sdes break; 94790267Sdes case HTTP_NEED_AUTH: 94890267Sdes if (need_auth) { 94990267Sdes /* 950125695Sdes * We already sent out authorization code, 951125695Sdes * so there's nothing more we can do. 95290267Sdes */ 95397856Sdes _http_seterr(conn->err); 95490267Sdes goto ouch; 95590267Sdes } 95690267Sdes /* try again, but send the password this time */ 95790267Sdes if (verbose) 95890267Sdes _fetch_info("server requires authorization"); 95990267Sdes break; 96090267Sdes case HTTP_NEED_PROXY_AUTH: 96190267Sdes /* 962125695Sdes * If we're talking to a proxy, we already sent 963125695Sdes * our proxy authorization code, so there's 964125695Sdes * nothing more we can do. 96590267Sdes */ 96697856Sdes _http_seterr(conn->err); 96790267Sdes goto ouch; 968125696Sdes case HTTP_BAD_RANGE: 969125696Sdes /* 970125696Sdes * This can happen if we ask for 0 bytes because 971125696Sdes * we already have the whole file. Consider this 972125696Sdes * a success for now, and check sizes later. 973125696Sdes */ 974125696Sdes break; 97590267Sdes case HTTP_PROTOCOL_ERROR: 97690267Sdes /* fall through */ 97790267Sdes case -1: 97890267Sdes _fetch_syserr(); 97990267Sdes goto ouch; 98090267Sdes default: 98197856Sdes _http_seterr(conn->err); 98290267Sdes if (!verbose) 98390267Sdes goto ouch; 98490267Sdes /* fall through so we can get the full error message */ 98590267Sdes } 98690267Sdes 98790267Sdes /* get headers */ 98890267Sdes do { 98997856Sdes switch ((h = _http_next_header(conn, &p))) { 99090267Sdes case hdr_syserror: 99190267Sdes _fetch_syserr(); 99290267Sdes goto ouch; 99390267Sdes case hdr_error: 99490267Sdes _http_seterr(HTTP_PROTOCOL_ERROR); 99590267Sdes goto ouch; 99690267Sdes case hdr_content_length: 99790267Sdes _http_parse_length(p, &clength); 99890267Sdes break; 99990267Sdes case hdr_content_range: 100090267Sdes _http_parse_range(p, &offset, &length, &size); 100190267Sdes break; 100290267Sdes case hdr_last_modified: 100390267Sdes _http_parse_mtime(p, &mtime); 100490267Sdes break; 100590267Sdes case hdr_location: 100697856Sdes if (!HTTP_REDIRECT(conn->err)) 100790267Sdes break; 100890267Sdes if (new) 100990267Sdes free(new); 101090267Sdes if (verbose) 101197856Sdes _fetch_info("%d redirect to %s", conn->err, p); 101290267Sdes if (*p == '/') 101390267Sdes /* absolute path */ 101490267Sdes new = fetchMakeURL(url->scheme, url->host, url->port, p, 101590267Sdes url->user, url->pwd); 101690267Sdes else 101790267Sdes new = fetchParseURL(p); 101890267Sdes if (new == NULL) { 101990267Sdes /* XXX should set an error code */ 102090267Sdes DEBUG(fprintf(stderr, "failed to parse new URL\n")); 102190267Sdes goto ouch; 102290267Sdes } 102390267Sdes if (!*new->user && !*new->pwd) { 102490267Sdes strcpy(new->user, url->user); 102590267Sdes strcpy(new->pwd, url->pwd); 102690267Sdes } 102790267Sdes new->offset = url->offset; 102890267Sdes new->length = url->length; 102990267Sdes break; 103090267Sdes case hdr_transfer_encoding: 103190267Sdes /* XXX weak test*/ 103290267Sdes chunked = (strcasecmp(p, "chunked") == 0); 103390267Sdes break; 103490267Sdes case hdr_www_authenticate: 103597856Sdes if (conn->err != HTTP_NEED_AUTH) 103690267Sdes break; 103790267Sdes /* if we were smarter, we'd check the method and realm */ 103890267Sdes break; 103990267Sdes case hdr_end: 104090267Sdes /* fall through */ 104190267Sdes case hdr_unknown: 104290267Sdes /* ignore */ 104390267Sdes break; 104490267Sdes } 104590267Sdes } while (h > hdr_end); 104690267Sdes 104790267Sdes /* we need to provide authentication */ 104897856Sdes if (conn->err == HTTP_NEED_AUTH) { 104998422Sdes e = conn->err; 105090267Sdes need_auth = 1; 105197856Sdes _fetch_close(conn); 105297856Sdes conn = NULL; 105390267Sdes continue; 105490267Sdes } 105590267Sdes 1056125696Sdes /* requested range not satisfiable */ 1057125696Sdes if (conn->err == HTTP_BAD_RANGE) { 1058125696Sdes if (url->offset == size && url->length == 0) { 1059125696Sdes /* asked for 0 bytes; fake it */ 1060125696Sdes offset = url->offset; 1061125696Sdes conn->err = HTTP_OK; 1062125696Sdes break; 1063125696Sdes } else { 1064125697Sdes _http_seterr(conn->err); 1065125696Sdes goto ouch; 1066125696Sdes } 1067125696Sdes } 1068125696Sdes 1069104404Sru /* we have a hit or an error */ 1070104404Sru if (conn->err == HTTP_OK || conn->err == HTTP_PARTIAL || HTTP_ERROR(conn->err)) 1071104404Sru break; 1072104404Sru 107390267Sdes /* all other cases: we got a redirect */ 107498422Sdes e = conn->err; 107590267Sdes need_auth = 0; 107697856Sdes _fetch_close(conn); 107797856Sdes conn = NULL; 107890267Sdes if (!new) { 107990267Sdes DEBUG(fprintf(stderr, "redirect with no new location\n")); 108090267Sdes break; 108190267Sdes } 108290267Sdes if (url != URL) 108390267Sdes fetchFreeURL(url); 108490267Sdes url = new; 108590267Sdes } while (++i < n); 108690267Sdes 108790267Sdes /* we failed, or ran out of retries */ 108897856Sdes if (conn == NULL) { 108998422Sdes _http_seterr(e); 109063012Sdes goto ouch; 109163012Sdes } 109260376Sdes 109390267Sdes DEBUG(fprintf(stderr, "offset %lld, length %lld," 109490267Sdes " size %lld, clength %lld\n", 109590267Sdes (long long)offset, (long long)length, 109690267Sdes (long long)size, (long long)clength)); 109760376Sdes 109890267Sdes /* check for inconsistencies */ 109990267Sdes if (clength != -1 && length != -1 && clength != length) { 110090267Sdes _http_seterr(HTTP_PROTOCOL_ERROR); 110163012Sdes goto ouch; 110263012Sdes } 110390267Sdes if (clength == -1) 110490267Sdes clength = length; 110590267Sdes if (clength != -1) 110690267Sdes length = offset + clength; 110790267Sdes if (length != -1 && size != -1 && length != size) { 110863012Sdes _http_seterr(HTTP_PROTOCOL_ERROR); 110963012Sdes goto ouch; 111090267Sdes } 111190267Sdes if (size == -1) 111290267Sdes size = length; 111360376Sdes 111490267Sdes /* fill in stats */ 111590267Sdes if (us) { 111690267Sdes us->size = size; 111790267Sdes us->atime = us->mtime = mtime; 111890267Sdes } 111963069Sdes 112090267Sdes /* too far? */ 1121109693Sdes if (URL->offset > 0 && offset > URL->offset) { 112290267Sdes _http_seterr(HTTP_PROTOCOL_ERROR); 112390267Sdes goto ouch; 112477238Sdes } 112560376Sdes 112690267Sdes /* report back real offset and size */ 112790267Sdes URL->offset = offset; 112890267Sdes URL->length = clength; 112937535Sdes 113090267Sdes /* wrap it up in a FILE */ 113197866Sdes if ((f = _http_funopen(conn, chunked)) == NULL) { 113290267Sdes _fetch_syserr(); 113390267Sdes goto ouch; 113490267Sdes } 113563716Sdes 113690267Sdes if (url != URL) 113790267Sdes fetchFreeURL(url); 113890267Sdes if (purl) 113990267Sdes fetchFreeURL(purl); 114063567Sdes 114197856Sdes if (HTTP_ERROR(conn->err)) { 114290267Sdes _http_print_html(stderr, f); 114390267Sdes fclose(f); 114490267Sdes f = NULL; 114590267Sdes } 114663012Sdes 114790267Sdes return (f); 114888771Sdes 114990267Sdesouch: 115090267Sdes if (url != URL) 115190267Sdes fetchFreeURL(url); 115290267Sdes if (purl) 115390267Sdes fetchFreeURL(purl); 115497856Sdes if (conn != NULL) 115597856Sdes _fetch_close(conn); 115690267Sdes return (NULL); 115763012Sdes} 115860189Sdes 115990267Sdes 116063012Sdes/***************************************************************************** 116163012Sdes * Entry points 116263012Sdes */ 116363012Sdes 116463012Sdes/* 116563340Sdes * Retrieve and stat a file by HTTP 116663340Sdes */ 116763340SdesFILE * 116875891SarchiefetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags) 116963340Sdes{ 1170112081Sdes return (_http_request(URL, "GET", us, _http_get_proxy(flags), flags)); 117163340Sdes} 117263340Sdes 117363340Sdes/* 117463012Sdes * Retrieve a file by HTTP 117563012Sdes */ 117663012SdesFILE * 117775891SarchiefetchGetHTTP(struct url *URL, const char *flags) 117863012Sdes{ 117990267Sdes return (fetchXGetHTTP(URL, NULL, flags)); 118037535Sdes} 118137535Sdes 118263340Sdes/* 118363340Sdes * Store a file by HTTP 118463340Sdes */ 118537535SdesFILE * 118685093SdesfetchPutHTTP(struct url *URL __unused, const char *flags __unused) 118737535Sdes{ 118890267Sdes warnx("fetchPutHTTP(): not implemented"); 118990267Sdes return (NULL); 119037535Sdes} 119140975Sdes 119240975Sdes/* 119340975Sdes * Get an HTTP document's metadata 119440975Sdes */ 119540975Sdesint 119675891SarchiefetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags) 119740975Sdes{ 119890267Sdes FILE *f; 119990267Sdes 1200112081Sdes f = _http_request(URL, "HEAD", us, _http_get_proxy(flags), flags); 1201112081Sdes if (f == NULL) 120290267Sdes return (-1); 120390267Sdes fclose(f); 120490267Sdes return (0); 120540975Sdes} 120641989Sdes 120741989Sdes/* 120841989Sdes * List a directory 120941989Sdes */ 121041989Sdesstruct url_ent * 121185093SdesfetchListHTTP(struct url *url __unused, const char *flags __unused) 121241989Sdes{ 121390267Sdes warnx("fetchListHTTP(): not implemented"); 121490267Sdes return (NULL); 121541989Sdes} 1216