http.c revision 184222
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 184222 2008-10-24 07:56:01Z ru $"); 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 133174588Sdeshttp_new_chunk(struct httpio *io) 13437608Sdes{ 13590267Sdes char *p; 13690267Sdes 137174588Sdes if (fetch_getln(io->conn) == -1) 13890267Sdes return (-1); 13990267Sdes 140174761Sdes if (io->conn->buflen < 2 || !isxdigit((unsigned char)*io->conn->buf)) 14190267Sdes return (-1); 14290267Sdes 143174761Sdes for (p = io->conn->buf; *p && !isspace((unsigned char)*p); ++p) { 14490267Sdes if (*p == ';') 14590267Sdes break; 146174761Sdes if (!isxdigit((unsigned char)*p)) 14790267Sdes return (-1); 148174761Sdes if (isdigit((unsigned char)*p)) { 14997859Sdes io->chunksize = io->chunksize * 16 + 15090267Sdes *p - '0'; 15190267Sdes } else { 15297859Sdes io->chunksize = io->chunksize * 16 + 153176036Sdes 10 + tolower((unsigned char)*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 176174588Sdeshttp_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 194174588Sdeshttp_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) { 202174588Sdes if (http_growbuf(io, len) == -1) 20397866Sdes return (-1); 204174588Sdes 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) { 213174588Sdes 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; 225174588Sdes if (http_growbuf(io, len) == -1) 22690267Sdes return (-1); 227174588Sdes 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 236174588Sdes 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 250174588Sdeshttp_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) 263174588Sdes if (http_fillbuf(io, len) < 1) 26490267Sdes break; 26597866Sdes l = io->buflen - io->bufpos; 26690267Sdes if (len < l) 26790267Sdes l = len; 268176105Sdes memcpy(buf + pos, io->buf + io->bufpos, 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 281174588Sdeshttp_writefn(void *v, const char *buf, int len) 28237535Sdes{ 28397859Sdes struct httpio *io = (struct httpio *)v; 28490267Sdes 285174588Sdes return (fetch_write(io->conn, buf, len)); 28637535Sdes} 28737535Sdes 28837608Sdes/* 28937608Sdes * Close function 29037608Sdes */ 29137535Sdesstatic int 292174588Sdeshttp_closefn(void *v) 29337535Sdes{ 29497859Sdes struct httpio *io = (struct httpio *)v; 29590267Sdes int r; 29663012Sdes 297174588Sdes 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 * 308174588Sdeshttp_funopen(conn_t *conn, int chunked) 30937535Sdes{ 31097859Sdes struct httpio *io; 31190267Sdes FILE *f; 31263012Sdes 313109967Sdes if ((io = calloc(1, sizeof(*io))) == NULL) { 314174588Sdes fetch_syserr(); 31590267Sdes return (NULL); 31690267Sdes } 31797859Sdes io->conn = conn; 31897866Sdes io->chunked = chunked; 319174588Sdes f = funopen(io, http_readfn, http_writefn, NULL, http_closefn); 32090267Sdes if (f == NULL) { 321174588Sdes 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 365174588Sdeshttp_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; 378174588Sdes fetch_syserr(); 37990267Sdes return (-1); 38090267Sdes } 38190267Sdes 382174588Sdes r = fetch_putln(conn, msg, len); 38390267Sdes free(msg); 38490267Sdes 38590267Sdes if (r == -1) { 386174588Sdes fetch_syserr(); 38790267Sdes return (-1); 38890267Sdes } 38990267Sdes 39090267Sdes return (0); 39163012Sdes} 39263012Sdes 39363012Sdes/* 39463012Sdes * Get and parse status line 39563012Sdes */ 39663012Sdesstatic int 397174588Sdeshttp_get_reply(conn_t *conn) 39863012Sdes{ 39990267Sdes char *p; 40090267Sdes 401174588Sdes 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 } 420174761Sdes if (*p != ' ' || 421174761Sdes !isdigit((unsigned char)p[1]) || 422174761Sdes !isdigit((unsigned char)p[2]) || 423174761Sdes !isdigit((unsigned char)p[3])) 42490267Sdes return (HTTP_PROTOCOL_ERROR); 42590267Sdes 42697856Sdes conn->err = (p[1] - '0') * 100 + (p[2] - '0') * 10 + (p[3] - '0'); 42797856Sdes return (conn->err); 42837535Sdes} 42937535Sdes 43037608Sdes/* 43190267Sdes * Check a header; if the type matches the given string, return a pointer 43290267Sdes * to the beginning of the value. 43363012Sdes */ 43475891Sarchiestatic const char * 435174588Sdeshttp_match(const char *str, const char *hdr) 43663012Sdes{ 437176036Sdes while (*str && *hdr && 438176036Sdes tolower((unsigned char)*str++) == tolower((unsigned char)*hdr++)) 43990267Sdes /* nothing */; 44090267Sdes if (*str || *hdr != ':') 44190267Sdes return (NULL); 442174761Sdes while (*hdr && isspace((unsigned char)*++hdr)) 44390267Sdes /* nothing */; 44490267Sdes return (hdr); 44563012Sdes} 44663012Sdes 44763012Sdes/* 44863012Sdes * Get the next header and return the appropriate symbolic code. 44963012Sdes */ 45085093Sdesstatic hdr_t 451174588Sdeshttp_next_header(conn_t *conn, const char **p) 45263012Sdes{ 45390267Sdes int i; 45490267Sdes 455174588Sdes if (fetch_getln(conn) == -1) 45690267Sdes return (hdr_syserror); 457174761Sdes while (conn->buflen && isspace((unsigned char)conn->buf[conn->buflen - 1])) 45897856Sdes conn->buflen--; 45997856Sdes conn->buf[conn->buflen] = '\0'; 46097856Sdes if (conn->buflen == 0) 46197856Sdes return (hdr_end); 46290267Sdes /* 46390267Sdes * We could check for malformed headers but we don't really care. 46490267Sdes * A valid header starts with a token immediately followed by a 46590267Sdes * colon; a token is any sequence of non-control, non-whitespace 46690267Sdes * characters except "()<>@,;:\\\"{}". 46790267Sdes */ 46890267Sdes for (i = 0; hdr_names[i].num != hdr_unknown; i++) 469174588Sdes if ((*p = http_match(hdr_names[i].name, conn->buf)) != NULL) 47090267Sdes return (hdr_names[i].num); 47190267Sdes return (hdr_unknown); 47263012Sdes} 47363012Sdes 47463012Sdes/* 47563012Sdes * Parse a last-modified header 47663012Sdes */ 47763716Sdesstatic int 478174588Sdeshttp_parse_mtime(const char *p, time_t *mtime) 47963012Sdes{ 48090267Sdes char locale[64], *r; 48190267Sdes struct tm tm; 48263012Sdes 483109967Sdes strncpy(locale, setlocale(LC_TIME, NULL), sizeof(locale)); 48490267Sdes setlocale(LC_TIME, "C"); 48590267Sdes r = strptime(p, "%a, %d %b %Y %H:%M:%S GMT", &tm); 48690267Sdes /* XXX should add support for date-2 and date-3 */ 48790267Sdes setlocale(LC_TIME, locale); 48890267Sdes if (r == NULL) 48990267Sdes return (-1); 49090267Sdes DEBUG(fprintf(stderr, "last modified: [%04d-%02d-%02d " 49188769Sdes "%02d:%02d:%02d]\n", 49263012Sdes tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, 49363012Sdes tm.tm_hour, tm.tm_min, tm.tm_sec)); 49490267Sdes *mtime = timegm(&tm); 49590267Sdes return (0); 49663012Sdes} 49763012Sdes 49863012Sdes/* 49963012Sdes * Parse a content-length header 50063012Sdes */ 50163716Sdesstatic int 502174588Sdeshttp_parse_length(const char *p, off_t *length) 50363012Sdes{ 50490267Sdes off_t len; 50590267Sdes 506174761Sdes for (len = 0; *p && isdigit((unsigned char)*p); ++p) 50790267Sdes len = len * 10 + (*p - '0'); 50890267Sdes if (*p) 50990267Sdes return (-1); 51090267Sdes DEBUG(fprintf(stderr, "content length: [%lld]\n", 51190267Sdes (long long)len)); 51290267Sdes *length = len; 51390267Sdes return (0); 51463012Sdes} 51563012Sdes 51663012Sdes/* 51763012Sdes * Parse a content-range header 51863012Sdes */ 51963716Sdesstatic int 520174588Sdeshttp_parse_range(const char *p, off_t *offset, off_t *length, off_t *size) 52163012Sdes{ 52290267Sdes off_t first, last, len; 52363716Sdes 52490267Sdes if (strncasecmp(p, "bytes ", 6) != 0) 52590267Sdes return (-1); 526125696Sdes p += 6; 527125696Sdes if (*p == '*') { 528125696Sdes first = last = -1; 529125696Sdes ++p; 530125696Sdes } else { 531174761Sdes for (first = 0; *p && isdigit((unsigned char)*p); ++p) 532125696Sdes first = first * 10 + *p - '0'; 533125696Sdes if (*p != '-') 534125696Sdes return (-1); 535174761Sdes for (last = 0, ++p; *p && isdigit((unsigned char)*p); ++p) 536125696Sdes last = last * 10 + *p - '0'; 537125696Sdes } 53890267Sdes if (first > last || *p != '/') 53990267Sdes return (-1); 540174761Sdes for (len = 0, ++p; *p && isdigit((unsigned char)*p); ++p) 54190267Sdes len = len * 10 + *p - '0'; 54290267Sdes if (*p || len < last - first + 1) 54390267Sdes return (-1); 544125696Sdes if (first == -1) { 545125696Sdes DEBUG(fprintf(stderr, "content range: [*/%lld]\n", 546125696Sdes (long long)len)); 547125696Sdes *length = 0; 548125696Sdes } else { 549125696Sdes DEBUG(fprintf(stderr, "content range: [%lld-%lld/%lld]\n", 550125696Sdes (long long)first, (long long)last, (long long)len)); 551125696Sdes *length = last - first + 1; 552125696Sdes } 55390267Sdes *offset = first; 55490267Sdes *size = len; 55590267Sdes return (0); 55663012Sdes} 55763012Sdes 55890267Sdes 55963012Sdes/***************************************************************************** 56063012Sdes * Helper functions for authorization 56163012Sdes */ 56263012Sdes 56363012Sdes/* 56437608Sdes * Base64 encoding 56537608Sdes */ 56662965Sdesstatic char * 567174588Sdeshttp_base64(const char *src) 56837608Sdes{ 56990267Sdes static const char base64[] = 57090267Sdes "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 57190267Sdes "abcdefghijklmnopqrstuvwxyz" 57290267Sdes "0123456789+/"; 57390267Sdes char *str, *dst; 57490267Sdes size_t l; 57590267Sdes int t, r; 57662965Sdes 57790267Sdes l = strlen(src); 578133280Sdes if ((str = malloc(((l + 2) / 3) * 4 + 1)) == NULL) 57990267Sdes return (NULL); 58090267Sdes dst = str; 58190267Sdes r = 0; 58237608Sdes 58390267Sdes while (l >= 3) { 58490267Sdes t = (src[0] << 16) | (src[1] << 8) | src[2]; 58590267Sdes dst[0] = base64[(t >> 18) & 0x3f]; 58690267Sdes dst[1] = base64[(t >> 12) & 0x3f]; 58790267Sdes dst[2] = base64[(t >> 6) & 0x3f]; 58890267Sdes dst[3] = base64[(t >> 0) & 0x3f]; 58990267Sdes src += 3; l -= 3; 59090267Sdes dst += 4; r += 4; 59190267Sdes } 59237608Sdes 59390267Sdes switch (l) { 59490267Sdes case 2: 59590267Sdes t = (src[0] << 16) | (src[1] << 8); 59690267Sdes dst[0] = base64[(t >> 18) & 0x3f]; 59790267Sdes dst[1] = base64[(t >> 12) & 0x3f]; 59890267Sdes dst[2] = base64[(t >> 6) & 0x3f]; 59990267Sdes dst[3] = '='; 60090267Sdes dst += 4; 60190267Sdes r += 4; 60290267Sdes break; 60390267Sdes case 1: 60490267Sdes t = src[0] << 16; 60590267Sdes dst[0] = base64[(t >> 18) & 0x3f]; 60690267Sdes dst[1] = base64[(t >> 12) & 0x3f]; 60790267Sdes dst[2] = dst[3] = '='; 60890267Sdes dst += 4; 60990267Sdes r += 4; 61090267Sdes break; 61190267Sdes case 0: 61290267Sdes break; 61390267Sdes } 61490267Sdes 61590267Sdes *dst = 0; 61690267Sdes return (str); 61737608Sdes} 61837608Sdes 61937608Sdes/* 62037608Sdes * Encode username and password 62137608Sdes */ 62262965Sdesstatic int 623174588Sdeshttp_basic_auth(conn_t *conn, const char *hdr, const char *usr, const char *pwd) 62437608Sdes{ 62590267Sdes char *upw, *auth; 62690267Sdes int r; 62737608Sdes 62890267Sdes DEBUG(fprintf(stderr, "usr: [%s]\n", usr)); 62990267Sdes DEBUG(fprintf(stderr, "pwd: [%s]\n", pwd)); 63090267Sdes if (asprintf(&upw, "%s:%s", usr, pwd) == -1) 63190267Sdes return (-1); 632174588Sdes auth = http_base64(upw); 63390267Sdes free(upw); 63490267Sdes if (auth == NULL) 63590267Sdes return (-1); 636174588Sdes r = http_cmd(conn, "%s: Basic %s", hdr, auth); 63790267Sdes free(auth); 63890267Sdes return (r); 63962965Sdes} 64062965Sdes 64162965Sdes/* 64262965Sdes * Send an authorization header 64362965Sdes */ 64462965Sdesstatic int 645174588Sdeshttp_authorize(conn_t *conn, const char *hdr, const char *p) 64662965Sdes{ 64790267Sdes /* basic authorization */ 64890267Sdes if (strncasecmp(p, "basic:", 6) == 0) { 64990267Sdes char *user, *pwd, *str; 65090267Sdes int r; 65162965Sdes 65290267Sdes /* skip realm */ 65390267Sdes for (p += 6; *p && *p != ':'; ++p) 65490267Sdes /* nothing */ ; 65590267Sdes if (!*p || strchr(++p, ':') == NULL) 65690267Sdes return (-1); 65790267Sdes if ((str = strdup(p)) == NULL) 65890267Sdes return (-1); /* XXX */ 65990267Sdes user = str; 66090267Sdes pwd = strchr(str, ':'); 66190267Sdes *pwd++ = '\0'; 662174588Sdes r = http_basic_auth(conn, hdr, user, pwd); 66390267Sdes free(str); 66490267Sdes return (r); 66590267Sdes } 66690267Sdes return (-1); 66737608Sdes} 66837608Sdes 66990267Sdes 67063012Sdes/***************************************************************************** 67163012Sdes * Helper functions for connecting to a server or proxy 67263012Sdes */ 67363012Sdes 67437608Sdes/* 67590267Sdes * Connect to the correct HTTP server or proxy. 67663012Sdes */ 67797856Sdesstatic conn_t * 678174588Sdeshttp_connect(struct url *URL, struct url *purl, const char *flags) 67963012Sdes{ 68097856Sdes conn_t *conn; 68190267Sdes int verbose; 682141958Skbyanc int af, val; 68390267Sdes 68463012Sdes#ifdef INET6 68590267Sdes af = AF_UNSPEC; 68660737Sume#else 68790267Sdes af = AF_INET; 68860737Sume#endif 68990267Sdes 69090267Sdes verbose = CHECK_FLAG('v'); 69190267Sdes if (CHECK_FLAG('4')) 69290267Sdes af = AF_INET; 69367043Sdes#ifdef INET6 69490267Sdes else if (CHECK_FLAG('6')) 69590267Sdes af = AF_INET6; 69667043Sdes#endif 69767043Sdes 69897868Sdes if (purl && strcasecmp(URL->scheme, SCHEME_HTTPS) != 0) { 69990267Sdes URL = purl; 70090267Sdes } else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0) { 70190267Sdes /* can't talk http to an ftp server */ 70290267Sdes /* XXX should set an error code */ 70397856Sdes return (NULL); 70490267Sdes } 70590267Sdes 706174588Sdes if ((conn = fetch_connect(URL->host, URL->port, af, verbose)) == NULL) 707174588Sdes /* fetch_connect() has already set an error code */ 70897856Sdes return (NULL); 70997868Sdes if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0 && 710174588Sdes fetch_ssl(conn, verbose) == -1) { 711174588Sdes fetch_close(conn); 71297891Sdes /* grrr */ 71397891Sdes errno = EAUTH; 714174588Sdes fetch_syserr(); 71597868Sdes return (NULL); 71697868Sdes } 717141958Skbyanc 718141958Skbyanc val = 1; 719141958Skbyanc setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val, sizeof(val)); 720141958Skbyanc 72197856Sdes return (conn); 72267043Sdes} 72367043Sdes 72467043Sdesstatic struct url * 725174752Sdeshttp_get_proxy(struct url * url, const char *flags) 72667043Sdes{ 72790267Sdes struct url *purl; 72890267Sdes char *p; 72990267Sdes 730112797Sdes if (flags != NULL && strchr(flags, 'd') != NULL) 731112081Sdes return (NULL); 732174752Sdes if (fetch_no_proxy_match(url->host)) 733174752Sdes return (NULL); 73490267Sdes if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) && 735149414Sdes *p && (purl = fetchParseURL(p))) { 73690267Sdes if (!*purl->scheme) 73790267Sdes strcpy(purl->scheme, SCHEME_HTTP); 73890267Sdes if (!purl->port) 739174588Sdes purl->port = fetch_default_proxy_port(purl->scheme); 74090267Sdes if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0) 74190267Sdes return (purl); 74290267Sdes fetchFreeURL(purl); 74390267Sdes } 74490267Sdes return (NULL); 74560376Sdes} 74660376Sdes 74788771Sdesstatic void 748174588Sdeshttp_print_html(FILE *out, FILE *in) 74988771Sdes{ 75090267Sdes size_t len; 75190267Sdes char *line, *p, *q; 75290267Sdes int comment, tag; 75388771Sdes 75490267Sdes comment = tag = 0; 75590267Sdes while ((line = fgetln(in, &len)) != NULL) { 756174761Sdes while (len && isspace((unsigned char)line[len - 1])) 75790267Sdes --len; 75890267Sdes for (p = q = line; q < line + len; ++q) { 75990267Sdes if (comment && *q == '-') { 76090267Sdes if (q + 2 < line + len && 76190267Sdes strcmp(q, "-->") == 0) { 76290267Sdes tag = comment = 0; 76390267Sdes q += 2; 76490267Sdes } 76590267Sdes } else if (tag && !comment && *q == '>') { 76690267Sdes p = q + 1; 76790267Sdes tag = 0; 76890267Sdes } else if (!tag && *q == '<') { 76990267Sdes if (q > p) 77090267Sdes fwrite(p, q - p, 1, out); 77190267Sdes tag = 1; 77290267Sdes if (q + 3 < line + len && 77390267Sdes strcmp(q, "<!--") == 0) { 77490267Sdes comment = 1; 77590267Sdes q += 3; 77690267Sdes } 77790267Sdes } 77888771Sdes } 77990267Sdes if (!tag && q > p) 78090267Sdes fwrite(p, q - p, 1, out); 78190267Sdes fputc('\n', out); 78288771Sdes } 78388771Sdes} 78488771Sdes 78590267Sdes 78663012Sdes/***************************************************************************** 78763012Sdes * Core 78860954Sdes */ 78960954Sdes 79060954Sdes/* 79163012Sdes * Send a request and process the reply 79297866Sdes * 79397866Sdes * XXX This function is way too long, the do..while loop should be split 79497866Sdes * XXX off into a separate function. 79560376Sdes */ 79667043SdesFILE * 797174588Sdeshttp_request(struct url *URL, const char *op, struct url_stat *us, 79890267Sdes struct url *purl, const char *flags) 79960376Sdes{ 80097856Sdes conn_t *conn; 80190267Sdes struct url *url, *new; 80290267Sdes int chunked, direct, need_auth, noredirect, verbose; 803143049Skbyanc int e, i, n, val; 80490267Sdes off_t offset, clength, length, size; 80590267Sdes time_t mtime; 80690267Sdes const char *p; 80790267Sdes FILE *f; 80890267Sdes hdr_t h; 809107372Sdes char hbuf[MAXHOSTNAMELEN + 7], *host; 81063012Sdes 81190267Sdes direct = CHECK_FLAG('d'); 81290267Sdes noredirect = CHECK_FLAG('A'); 81390267Sdes verbose = CHECK_FLAG('v'); 81460737Sume 81590267Sdes if (direct && purl) { 81690267Sdes fetchFreeURL(purl); 81790267Sdes purl = NULL; 81890267Sdes } 81963716Sdes 82090267Sdes /* try the provided URL first */ 82190267Sdes url = URL; 82263012Sdes 82390267Sdes /* if the A flag is set, we only get one try */ 82490267Sdes n = noredirect ? 1 : MAX_REDIRECT; 82590267Sdes i = 0; 82663012Sdes 82798422Sdes e = HTTP_PROTOCOL_ERROR; 82890267Sdes need_auth = 0; 82990267Sdes do { 83090267Sdes new = NULL; 83190267Sdes chunked = 0; 83290267Sdes offset = 0; 83390267Sdes clength = -1; 83490267Sdes length = -1; 83590267Sdes size = -1; 83690267Sdes mtime = 0; 83790267Sdes 83890267Sdes /* check port */ 83990267Sdes if (!url->port) 840174588Sdes url->port = fetch_default_port(url->scheme); 84190267Sdes 84290267Sdes /* were we redirected to an FTP URL? */ 84390267Sdes if (purl == NULL && strcmp(url->scheme, SCHEME_FTP) == 0) { 84490267Sdes if (strcmp(op, "GET") == 0) 845174588Sdes return (ftp_request(url, "RETR", us, purl, flags)); 84690267Sdes else if (strcmp(op, "HEAD") == 0) 847174588Sdes return (ftp_request(url, "STAT", us, purl, flags)); 84890267Sdes } 84990267Sdes 85090267Sdes /* connect to server or proxy */ 851174588Sdes if ((conn = http_connect(url, purl, flags)) == NULL) 85290267Sdes goto ouch; 85390267Sdes 85490267Sdes host = url->host; 85560737Sume#ifdef INET6 85690267Sdes if (strchr(url->host, ':')) { 85790267Sdes snprintf(hbuf, sizeof(hbuf), "[%s]", url->host); 85890267Sdes host = hbuf; 85990267Sdes } 86060737Sume#endif 861174588Sdes if (url->port != fetch_default_port(url->scheme)) { 862107372Sdes if (host != hbuf) { 863107372Sdes strcpy(hbuf, host); 864107372Sdes host = hbuf; 865107372Sdes } 866107372Sdes snprintf(hbuf + strlen(hbuf), 867107372Sdes sizeof(hbuf) - strlen(hbuf), ":%d", url->port); 868107372Sdes } 86937535Sdes 87090267Sdes /* send request */ 87190267Sdes if (verbose) 872174588Sdes fetch_info("requesting %s://%s%s", 873107372Sdes url->scheme, host, url->doc); 87490267Sdes if (purl) { 875174588Sdes http_cmd(conn, "%s %s://%s%s HTTP/1.1", 876107372Sdes op, url->scheme, host, url->doc); 87790267Sdes } else { 878174588Sdes http_cmd(conn, "%s %s HTTP/1.1", 87990267Sdes op, url->doc); 88090267Sdes } 88137535Sdes 88290267Sdes /* virtual host */ 883174588Sdes http_cmd(conn, "Host: %s", host); 88490267Sdes 88590267Sdes /* proxy authorization */ 88690267Sdes if (purl) { 88790267Sdes if (*purl->user || *purl->pwd) 888174588Sdes http_basic_auth(conn, "Proxy-Authorization", 88990267Sdes purl->user, purl->pwd); 89090267Sdes else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL && *p != '\0') 891174588Sdes http_authorize(conn, "Proxy-Authorization", p); 89290267Sdes } 89390267Sdes 89490267Sdes /* server authorization */ 89590267Sdes if (need_auth || *url->user || *url->pwd) { 89690267Sdes if (*url->user || *url->pwd) 897174588Sdes http_basic_auth(conn, "Authorization", url->user, url->pwd); 89890267Sdes else if ((p = getenv("HTTP_AUTH")) != NULL && *p != '\0') 899174588Sdes http_authorize(conn, "Authorization", p); 90090267Sdes else if (fetchAuthMethod && fetchAuthMethod(url) == 0) { 901174588Sdes http_basic_auth(conn, "Authorization", url->user, url->pwd); 90290267Sdes } else { 903174588Sdes http_seterr(HTTP_NEED_AUTH); 90490267Sdes goto ouch; 90590267Sdes } 90690267Sdes } 90790267Sdes 90890267Sdes /* other headers */ 909107372Sdes if ((p = getenv("HTTP_REFERER")) != NULL && *p != '\0') { 910107372Sdes if (strcasecmp(p, "auto") == 0) 911174588Sdes http_cmd(conn, "Referer: %s://%s%s", 912107372Sdes url->scheme, host, url->doc); 913107372Sdes else 914174588Sdes http_cmd(conn, "Referer: %s", p); 915107372Sdes } 91690267Sdes if ((p = getenv("HTTP_USER_AGENT")) != NULL && *p != '\0') 917174588Sdes http_cmd(conn, "User-Agent: %s", p); 91890267Sdes else 919174588Sdes http_cmd(conn, "User-Agent: %s " _LIBFETCH_VER, getprogname()); 920109693Sdes if (url->offset > 0) 921174588Sdes http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset); 922174588Sdes http_cmd(conn, "Connection: close"); 923174588Sdes http_cmd(conn, ""); 92490267Sdes 925143049Skbyanc /* 926143049Skbyanc * Force the queued request to be dispatched. Normally, one 927143049Skbyanc * would do this with shutdown(2) but squid proxies can be 928143049Skbyanc * configured to disallow such half-closed connections. To 929143049Skbyanc * be compatible with such configurations, fiddle with socket 930143049Skbyanc * options to force the pending data to be written. 931143049Skbyanc */ 932143049Skbyanc val = 0; 933143049Skbyanc setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val, 934143049Skbyanc sizeof(val)); 935143049Skbyanc val = 1; 936143049Skbyanc setsockopt(conn->sd, IPPROTO_TCP, TCP_NODELAY, &val, 937143049Skbyanc sizeof(val)); 938143049Skbyanc 93990267Sdes /* get reply */ 940174588Sdes switch (http_get_reply(conn)) { 94190267Sdes case HTTP_OK: 94290267Sdes case HTTP_PARTIAL: 94390267Sdes /* fine */ 94490267Sdes break; 94590267Sdes case HTTP_MOVED_PERM: 94690267Sdes case HTTP_MOVED_TEMP: 94790267Sdes case HTTP_SEE_OTHER: 94890267Sdes /* 949125695Sdes * Not so fine, but we still have to read the 950125695Sdes * headers to get the new location. 95190267Sdes */ 95290267Sdes break; 95390267Sdes case HTTP_NEED_AUTH: 95490267Sdes if (need_auth) { 95590267Sdes /* 956125695Sdes * We already sent out authorization code, 957125695Sdes * so there's nothing more we can do. 95890267Sdes */ 959174588Sdes http_seterr(conn->err); 96090267Sdes goto ouch; 96190267Sdes } 96290267Sdes /* try again, but send the password this time */ 96390267Sdes if (verbose) 964174588Sdes fetch_info("server requires authorization"); 96590267Sdes break; 96690267Sdes case HTTP_NEED_PROXY_AUTH: 96790267Sdes /* 968125695Sdes * If we're talking to a proxy, we already sent 969125695Sdes * our proxy authorization code, so there's 970125695Sdes * nothing more we can do. 97190267Sdes */ 972174588Sdes http_seterr(conn->err); 97390267Sdes goto ouch; 974125696Sdes case HTTP_BAD_RANGE: 975125696Sdes /* 976125696Sdes * This can happen if we ask for 0 bytes because 977125696Sdes * we already have the whole file. Consider this 978125696Sdes * a success for now, and check sizes later. 979125696Sdes */ 980125696Sdes break; 98190267Sdes case HTTP_PROTOCOL_ERROR: 98290267Sdes /* fall through */ 98390267Sdes case -1: 984174588Sdes fetch_syserr(); 98590267Sdes goto ouch; 98690267Sdes default: 987174588Sdes http_seterr(conn->err); 98890267Sdes if (!verbose) 98990267Sdes goto ouch; 99090267Sdes /* fall through so we can get the full error message */ 99190267Sdes } 99290267Sdes 99390267Sdes /* get headers */ 99490267Sdes do { 995174588Sdes switch ((h = http_next_header(conn, &p))) { 99690267Sdes case hdr_syserror: 997174588Sdes fetch_syserr(); 99890267Sdes goto ouch; 99990267Sdes case hdr_error: 1000174588Sdes http_seterr(HTTP_PROTOCOL_ERROR); 100190267Sdes goto ouch; 100290267Sdes case hdr_content_length: 1003174588Sdes http_parse_length(p, &clength); 100490267Sdes break; 100590267Sdes case hdr_content_range: 1006174588Sdes http_parse_range(p, &offset, &length, &size); 100790267Sdes break; 100890267Sdes case hdr_last_modified: 1009174588Sdes http_parse_mtime(p, &mtime); 101090267Sdes break; 101190267Sdes case hdr_location: 101297856Sdes if (!HTTP_REDIRECT(conn->err)) 101390267Sdes break; 101490267Sdes if (new) 101590267Sdes free(new); 101690267Sdes if (verbose) 1017174588Sdes fetch_info("%d redirect to %s", conn->err, p); 101890267Sdes if (*p == '/') 101990267Sdes /* absolute path */ 102090267Sdes new = fetchMakeURL(url->scheme, url->host, url->port, p, 102190267Sdes url->user, url->pwd); 102290267Sdes else 102390267Sdes new = fetchParseURL(p); 102490267Sdes if (new == NULL) { 102590267Sdes /* XXX should set an error code */ 102690267Sdes DEBUG(fprintf(stderr, "failed to parse new URL\n")); 102790267Sdes goto ouch; 102890267Sdes } 102990267Sdes if (!*new->user && !*new->pwd) { 103090267Sdes strcpy(new->user, url->user); 103190267Sdes strcpy(new->pwd, url->pwd); 103290267Sdes } 103390267Sdes new->offset = url->offset; 103490267Sdes new->length = url->length; 103590267Sdes break; 103690267Sdes case hdr_transfer_encoding: 103790267Sdes /* XXX weak test*/ 103890267Sdes chunked = (strcasecmp(p, "chunked") == 0); 103990267Sdes break; 104090267Sdes case hdr_www_authenticate: 104197856Sdes if (conn->err != HTTP_NEED_AUTH) 104290267Sdes break; 104390267Sdes /* if we were smarter, we'd check the method and realm */ 104490267Sdes break; 104590267Sdes case hdr_end: 104690267Sdes /* fall through */ 104790267Sdes case hdr_unknown: 104890267Sdes /* ignore */ 104990267Sdes break; 105090267Sdes } 105190267Sdes } while (h > hdr_end); 105290267Sdes 105390267Sdes /* we need to provide authentication */ 105497856Sdes if (conn->err == HTTP_NEED_AUTH) { 105598422Sdes e = conn->err; 105690267Sdes need_auth = 1; 1057174588Sdes fetch_close(conn); 105897856Sdes conn = NULL; 105990267Sdes continue; 106090267Sdes } 106190267Sdes 1062125696Sdes /* requested range not satisfiable */ 1063125696Sdes if (conn->err == HTTP_BAD_RANGE) { 1064125696Sdes if (url->offset == size && url->length == 0) { 1065125696Sdes /* asked for 0 bytes; fake it */ 1066125696Sdes offset = url->offset; 1067184222Sru clength = -1; 1068125696Sdes conn->err = HTTP_OK; 1069125696Sdes break; 1070125696Sdes } else { 1071174588Sdes http_seterr(conn->err); 1072125696Sdes goto ouch; 1073125696Sdes } 1074125696Sdes } 1075125696Sdes 1076104404Sru /* we have a hit or an error */ 1077104404Sru if (conn->err == HTTP_OK || conn->err == HTTP_PARTIAL || HTTP_ERROR(conn->err)) 1078104404Sru break; 1079104404Sru 108090267Sdes /* all other cases: we got a redirect */ 108198422Sdes e = conn->err; 108290267Sdes need_auth = 0; 1083174588Sdes fetch_close(conn); 108497856Sdes conn = NULL; 108590267Sdes if (!new) { 108690267Sdes DEBUG(fprintf(stderr, "redirect with no new location\n")); 108790267Sdes break; 108890267Sdes } 108990267Sdes if (url != URL) 109090267Sdes fetchFreeURL(url); 109190267Sdes url = new; 109290267Sdes } while (++i < n); 109390267Sdes 109490267Sdes /* we failed, or ran out of retries */ 109597856Sdes if (conn == NULL) { 1096174588Sdes http_seterr(e); 109763012Sdes goto ouch; 109863012Sdes } 109960376Sdes 110090267Sdes DEBUG(fprintf(stderr, "offset %lld, length %lld," 110190267Sdes " size %lld, clength %lld\n", 110290267Sdes (long long)offset, (long long)length, 110390267Sdes (long long)size, (long long)clength)); 110460376Sdes 110590267Sdes /* check for inconsistencies */ 110690267Sdes if (clength != -1 && length != -1 && clength != length) { 1107174588Sdes http_seterr(HTTP_PROTOCOL_ERROR); 110863012Sdes goto ouch; 110963012Sdes } 111090267Sdes if (clength == -1) 111190267Sdes clength = length; 111290267Sdes if (clength != -1) 111390267Sdes length = offset + clength; 111490267Sdes if (length != -1 && size != -1 && length != size) { 1115174588Sdes http_seterr(HTTP_PROTOCOL_ERROR); 111663012Sdes goto ouch; 111790267Sdes } 111890267Sdes if (size == -1) 111990267Sdes size = length; 112060376Sdes 112190267Sdes /* fill in stats */ 112290267Sdes if (us) { 112390267Sdes us->size = size; 112490267Sdes us->atime = us->mtime = mtime; 112590267Sdes } 112663069Sdes 112790267Sdes /* too far? */ 1128109693Sdes if (URL->offset > 0 && offset > URL->offset) { 1129174588Sdes http_seterr(HTTP_PROTOCOL_ERROR); 113090267Sdes goto ouch; 113177238Sdes } 113260376Sdes 113390267Sdes /* report back real offset and size */ 113490267Sdes URL->offset = offset; 113590267Sdes URL->length = clength; 113637535Sdes 113790267Sdes /* wrap it up in a FILE */ 1138174588Sdes if ((f = http_funopen(conn, chunked)) == NULL) { 1139174588Sdes fetch_syserr(); 114090267Sdes goto ouch; 114190267Sdes } 114263716Sdes 114390267Sdes if (url != URL) 114490267Sdes fetchFreeURL(url); 114590267Sdes if (purl) 114690267Sdes fetchFreeURL(purl); 114763567Sdes 114897856Sdes if (HTTP_ERROR(conn->err)) { 1149174588Sdes http_print_html(stderr, f); 115090267Sdes fclose(f); 115190267Sdes f = NULL; 115290267Sdes } 115363012Sdes 115490267Sdes return (f); 115588771Sdes 115690267Sdesouch: 115790267Sdes if (url != URL) 115890267Sdes fetchFreeURL(url); 115990267Sdes if (purl) 116090267Sdes fetchFreeURL(purl); 116197856Sdes if (conn != NULL) 1162174588Sdes fetch_close(conn); 116390267Sdes return (NULL); 116463012Sdes} 116560189Sdes 116690267Sdes 116763012Sdes/***************************************************************************** 116863012Sdes * Entry points 116963012Sdes */ 117063012Sdes 117163012Sdes/* 117263340Sdes * Retrieve and stat a file by HTTP 117363340Sdes */ 117463340SdesFILE * 117575891SarchiefetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags) 117663340Sdes{ 1177174752Sdes return (http_request(URL, "GET", us, http_get_proxy(URL, flags), flags)); 117863340Sdes} 117963340Sdes 118063340Sdes/* 118163012Sdes * Retrieve a file by HTTP 118263012Sdes */ 118363012SdesFILE * 118475891SarchiefetchGetHTTP(struct url *URL, const char *flags) 118563012Sdes{ 118690267Sdes return (fetchXGetHTTP(URL, NULL, flags)); 118737535Sdes} 118837535Sdes 118963340Sdes/* 119063340Sdes * Store a file by HTTP 119163340Sdes */ 119237535SdesFILE * 119385093SdesfetchPutHTTP(struct url *URL __unused, const char *flags __unused) 119437535Sdes{ 119590267Sdes warnx("fetchPutHTTP(): not implemented"); 119690267Sdes return (NULL); 119737535Sdes} 119840975Sdes 119940975Sdes/* 120040975Sdes * Get an HTTP document's metadata 120140975Sdes */ 120240975Sdesint 120375891SarchiefetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags) 120440975Sdes{ 120590267Sdes FILE *f; 120690267Sdes 1207174752Sdes f = http_request(URL, "HEAD", us, http_get_proxy(URL, flags), flags); 1208112081Sdes if (f == NULL) 120990267Sdes return (-1); 121090267Sdes fclose(f); 121190267Sdes return (0); 121240975Sdes} 121341989Sdes 121441989Sdes/* 121541989Sdes * List a directory 121641989Sdes */ 121741989Sdesstruct url_ent * 121885093SdesfetchListHTTP(struct url *url __unused, const char *flags __unused) 121941989Sdes{ 122090267Sdes warnx("fetchListHTTP(): not implemented"); 122190267Sdes return (NULL); 122241989Sdes} 1223