http.c revision 174761
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 174761 2007-12-19 00:26:36Z 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 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 + 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 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; 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 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{ 43790267Sdes while (*str && *hdr && tolower(*str++) == tolower(*hdr++)) 43890267Sdes /* nothing */; 43990267Sdes if (*str || *hdr != ':') 44090267Sdes return (NULL); 441174761Sdes while (*hdr && isspace((unsigned char)*++hdr)) 44290267Sdes /* nothing */; 44390267Sdes return (hdr); 44463012Sdes} 44563012Sdes 44663012Sdes/* 44763012Sdes * Get the next header and return the appropriate symbolic code. 44863012Sdes */ 44985093Sdesstatic hdr_t 450174588Sdeshttp_next_header(conn_t *conn, const char **p) 45163012Sdes{ 45290267Sdes int i; 45390267Sdes 454174588Sdes if (fetch_getln(conn) == -1) 45590267Sdes return (hdr_syserror); 456174761Sdes while (conn->buflen && isspace((unsigned char)conn->buf[conn->buflen - 1])) 45797856Sdes conn->buflen--; 45897856Sdes conn->buf[conn->buflen] = '\0'; 45997856Sdes if (conn->buflen == 0) 46097856Sdes return (hdr_end); 46190267Sdes /* 46290267Sdes * We could check for malformed headers but we don't really care. 46390267Sdes * A valid header starts with a token immediately followed by a 46490267Sdes * colon; a token is any sequence of non-control, non-whitespace 46590267Sdes * characters except "()<>@,;:\\\"{}". 46690267Sdes */ 46790267Sdes for (i = 0; hdr_names[i].num != hdr_unknown; i++) 468174588Sdes if ((*p = http_match(hdr_names[i].name, conn->buf)) != NULL) 46990267Sdes return (hdr_names[i].num); 47090267Sdes return (hdr_unknown); 47163012Sdes} 47263012Sdes 47363012Sdes/* 47463012Sdes * Parse a last-modified header 47563012Sdes */ 47663716Sdesstatic int 477174588Sdeshttp_parse_mtime(const char *p, time_t *mtime) 47863012Sdes{ 47990267Sdes char locale[64], *r; 48090267Sdes struct tm tm; 48163012Sdes 482109967Sdes strncpy(locale, setlocale(LC_TIME, NULL), sizeof(locale)); 48390267Sdes setlocale(LC_TIME, "C"); 48490267Sdes r = strptime(p, "%a, %d %b %Y %H:%M:%S GMT", &tm); 48590267Sdes /* XXX should add support for date-2 and date-3 */ 48690267Sdes setlocale(LC_TIME, locale); 48790267Sdes if (r == NULL) 48890267Sdes return (-1); 48990267Sdes DEBUG(fprintf(stderr, "last modified: [%04d-%02d-%02d " 49088769Sdes "%02d:%02d:%02d]\n", 49163012Sdes tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, 49263012Sdes tm.tm_hour, tm.tm_min, tm.tm_sec)); 49390267Sdes *mtime = timegm(&tm); 49490267Sdes return (0); 49563012Sdes} 49663012Sdes 49763012Sdes/* 49863012Sdes * Parse a content-length header 49963012Sdes */ 50063716Sdesstatic int 501174588Sdeshttp_parse_length(const char *p, off_t *length) 50263012Sdes{ 50390267Sdes off_t len; 50490267Sdes 505174761Sdes for (len = 0; *p && isdigit((unsigned char)*p); ++p) 50690267Sdes len = len * 10 + (*p - '0'); 50790267Sdes if (*p) 50890267Sdes return (-1); 50990267Sdes DEBUG(fprintf(stderr, "content length: [%lld]\n", 51090267Sdes (long long)len)); 51190267Sdes *length = len; 51290267Sdes return (0); 51363012Sdes} 51463012Sdes 51563012Sdes/* 51663012Sdes * Parse a content-range header 51763012Sdes */ 51863716Sdesstatic int 519174588Sdeshttp_parse_range(const char *p, off_t *offset, off_t *length, off_t *size) 52063012Sdes{ 52190267Sdes off_t first, last, len; 52263716Sdes 52390267Sdes if (strncasecmp(p, "bytes ", 6) != 0) 52490267Sdes return (-1); 525125696Sdes p += 6; 526125696Sdes if (*p == '*') { 527125696Sdes first = last = -1; 528125696Sdes ++p; 529125696Sdes } else { 530174761Sdes for (first = 0; *p && isdigit((unsigned char)*p); ++p) 531125696Sdes first = first * 10 + *p - '0'; 532125696Sdes if (*p != '-') 533125696Sdes return (-1); 534174761Sdes for (last = 0, ++p; *p && isdigit((unsigned char)*p); ++p) 535125696Sdes last = last * 10 + *p - '0'; 536125696Sdes } 53790267Sdes if (first > last || *p != '/') 53890267Sdes return (-1); 539174761Sdes for (len = 0, ++p; *p && isdigit((unsigned char)*p); ++p) 54090267Sdes len = len * 10 + *p - '0'; 54190267Sdes if (*p || len < last - first + 1) 54290267Sdes return (-1); 543125696Sdes if (first == -1) { 544125696Sdes DEBUG(fprintf(stderr, "content range: [*/%lld]\n", 545125696Sdes (long long)len)); 546125696Sdes *length = 0; 547125696Sdes } else { 548125696Sdes DEBUG(fprintf(stderr, "content range: [%lld-%lld/%lld]\n", 549125696Sdes (long long)first, (long long)last, (long long)len)); 550125696Sdes *length = last - first + 1; 551125696Sdes } 55290267Sdes *offset = first; 55390267Sdes *size = len; 55490267Sdes return (0); 55563012Sdes} 55663012Sdes 55790267Sdes 55863012Sdes/***************************************************************************** 55963012Sdes * Helper functions for authorization 56063012Sdes */ 56163012Sdes 56263012Sdes/* 56337608Sdes * Base64 encoding 56437608Sdes */ 56562965Sdesstatic char * 566174588Sdeshttp_base64(const char *src) 56737608Sdes{ 56890267Sdes static const char base64[] = 56990267Sdes "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 57090267Sdes "abcdefghijklmnopqrstuvwxyz" 57190267Sdes "0123456789+/"; 57290267Sdes char *str, *dst; 57390267Sdes size_t l; 57490267Sdes int t, r; 57562965Sdes 57690267Sdes l = strlen(src); 577133280Sdes if ((str = malloc(((l + 2) / 3) * 4 + 1)) == NULL) 57890267Sdes return (NULL); 57990267Sdes dst = str; 58090267Sdes r = 0; 58137608Sdes 58290267Sdes while (l >= 3) { 58390267Sdes t = (src[0] << 16) | (src[1] << 8) | src[2]; 58490267Sdes dst[0] = base64[(t >> 18) & 0x3f]; 58590267Sdes dst[1] = base64[(t >> 12) & 0x3f]; 58690267Sdes dst[2] = base64[(t >> 6) & 0x3f]; 58790267Sdes dst[3] = base64[(t >> 0) & 0x3f]; 58890267Sdes src += 3; l -= 3; 58990267Sdes dst += 4; r += 4; 59090267Sdes } 59137608Sdes 59290267Sdes switch (l) { 59390267Sdes case 2: 59490267Sdes t = (src[0] << 16) | (src[1] << 8); 59590267Sdes dst[0] = base64[(t >> 18) & 0x3f]; 59690267Sdes dst[1] = base64[(t >> 12) & 0x3f]; 59790267Sdes dst[2] = base64[(t >> 6) & 0x3f]; 59890267Sdes dst[3] = '='; 59990267Sdes dst += 4; 60090267Sdes r += 4; 60190267Sdes break; 60290267Sdes case 1: 60390267Sdes t = src[0] << 16; 60490267Sdes dst[0] = base64[(t >> 18) & 0x3f]; 60590267Sdes dst[1] = base64[(t >> 12) & 0x3f]; 60690267Sdes dst[2] = dst[3] = '='; 60790267Sdes dst += 4; 60890267Sdes r += 4; 60990267Sdes break; 61090267Sdes case 0: 61190267Sdes break; 61290267Sdes } 61390267Sdes 61490267Sdes *dst = 0; 61590267Sdes return (str); 61637608Sdes} 61737608Sdes 61837608Sdes/* 61937608Sdes * Encode username and password 62037608Sdes */ 62162965Sdesstatic int 622174588Sdeshttp_basic_auth(conn_t *conn, const char *hdr, const char *usr, const char *pwd) 62337608Sdes{ 62490267Sdes char *upw, *auth; 62590267Sdes int r; 62637608Sdes 62790267Sdes DEBUG(fprintf(stderr, "usr: [%s]\n", usr)); 62890267Sdes DEBUG(fprintf(stderr, "pwd: [%s]\n", pwd)); 62990267Sdes if (asprintf(&upw, "%s:%s", usr, pwd) == -1) 63090267Sdes return (-1); 631174588Sdes auth = http_base64(upw); 63290267Sdes free(upw); 63390267Sdes if (auth == NULL) 63490267Sdes return (-1); 635174588Sdes r = http_cmd(conn, "%s: Basic %s", hdr, auth); 63690267Sdes free(auth); 63790267Sdes return (r); 63862965Sdes} 63962965Sdes 64062965Sdes/* 64162965Sdes * Send an authorization header 64262965Sdes */ 64362965Sdesstatic int 644174588Sdeshttp_authorize(conn_t *conn, const char *hdr, const char *p) 64562965Sdes{ 64690267Sdes /* basic authorization */ 64790267Sdes if (strncasecmp(p, "basic:", 6) == 0) { 64890267Sdes char *user, *pwd, *str; 64990267Sdes int r; 65062965Sdes 65190267Sdes /* skip realm */ 65290267Sdes for (p += 6; *p && *p != ':'; ++p) 65390267Sdes /* nothing */ ; 65490267Sdes if (!*p || strchr(++p, ':') == NULL) 65590267Sdes return (-1); 65690267Sdes if ((str = strdup(p)) == NULL) 65790267Sdes return (-1); /* XXX */ 65890267Sdes user = str; 65990267Sdes pwd = strchr(str, ':'); 66090267Sdes *pwd++ = '\0'; 661174588Sdes r = http_basic_auth(conn, hdr, user, pwd); 66290267Sdes free(str); 66390267Sdes return (r); 66490267Sdes } 66590267Sdes return (-1); 66637608Sdes} 66737608Sdes 66890267Sdes 66963012Sdes/***************************************************************************** 67063012Sdes * Helper functions for connecting to a server or proxy 67163012Sdes */ 67263012Sdes 67337608Sdes/* 67490267Sdes * Connect to the correct HTTP server or proxy. 67563012Sdes */ 67697856Sdesstatic conn_t * 677174588Sdeshttp_connect(struct url *URL, struct url *purl, const char *flags) 67863012Sdes{ 67997856Sdes conn_t *conn; 68090267Sdes int verbose; 681141958Skbyanc int af, val; 68290267Sdes 68363012Sdes#ifdef INET6 68490267Sdes af = AF_UNSPEC; 68560737Sume#else 68690267Sdes af = AF_INET; 68760737Sume#endif 68890267Sdes 68990267Sdes verbose = CHECK_FLAG('v'); 69090267Sdes if (CHECK_FLAG('4')) 69190267Sdes af = AF_INET; 69267043Sdes#ifdef INET6 69390267Sdes else if (CHECK_FLAG('6')) 69490267Sdes af = AF_INET6; 69567043Sdes#endif 69667043Sdes 69797868Sdes if (purl && strcasecmp(URL->scheme, SCHEME_HTTPS) != 0) { 69890267Sdes URL = purl; 69990267Sdes } else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0) { 70090267Sdes /* can't talk http to an ftp server */ 70190267Sdes /* XXX should set an error code */ 70297856Sdes return (NULL); 70390267Sdes } 70490267Sdes 705174588Sdes if ((conn = fetch_connect(URL->host, URL->port, af, verbose)) == NULL) 706174588Sdes /* fetch_connect() has already set an error code */ 70797856Sdes return (NULL); 70897868Sdes if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0 && 709174588Sdes fetch_ssl(conn, verbose) == -1) { 710174588Sdes fetch_close(conn); 71197891Sdes /* grrr */ 71297891Sdes errno = EAUTH; 713174588Sdes fetch_syserr(); 71497868Sdes return (NULL); 71597868Sdes } 716141958Skbyanc 717141958Skbyanc val = 1; 718141958Skbyanc setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val, sizeof(val)); 719141958Skbyanc 72097856Sdes return (conn); 72167043Sdes} 72267043Sdes 72367043Sdesstatic struct url * 724174752Sdeshttp_get_proxy(struct url * url, const char *flags) 72567043Sdes{ 72690267Sdes struct url *purl; 72790267Sdes char *p; 72890267Sdes 729112797Sdes if (flags != NULL && strchr(flags, 'd') != NULL) 730112081Sdes return (NULL); 731174752Sdes if (fetch_no_proxy_match(url->host)) 732174752Sdes return (NULL); 73390267Sdes if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) && 734149414Sdes *p && (purl = fetchParseURL(p))) { 73590267Sdes if (!*purl->scheme) 73690267Sdes strcpy(purl->scheme, SCHEME_HTTP); 73790267Sdes if (!purl->port) 738174588Sdes purl->port = fetch_default_proxy_port(purl->scheme); 73990267Sdes if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0) 74090267Sdes return (purl); 74190267Sdes fetchFreeURL(purl); 74290267Sdes } 74390267Sdes return (NULL); 74460376Sdes} 74560376Sdes 74688771Sdesstatic void 747174588Sdeshttp_print_html(FILE *out, FILE *in) 74888771Sdes{ 74990267Sdes size_t len; 75090267Sdes char *line, *p, *q; 75190267Sdes int comment, tag; 75288771Sdes 75390267Sdes comment = tag = 0; 75490267Sdes while ((line = fgetln(in, &len)) != NULL) { 755174761Sdes while (len && isspace((unsigned char)line[len - 1])) 75690267Sdes --len; 75790267Sdes for (p = q = line; q < line + len; ++q) { 75890267Sdes if (comment && *q == '-') { 75990267Sdes if (q + 2 < line + len && 76090267Sdes strcmp(q, "-->") == 0) { 76190267Sdes tag = comment = 0; 76290267Sdes q += 2; 76390267Sdes } 76490267Sdes } else if (tag && !comment && *q == '>') { 76590267Sdes p = q + 1; 76690267Sdes tag = 0; 76790267Sdes } else if (!tag && *q == '<') { 76890267Sdes if (q > p) 76990267Sdes fwrite(p, q - p, 1, out); 77090267Sdes tag = 1; 77190267Sdes if (q + 3 < line + len && 77290267Sdes strcmp(q, "<!--") == 0) { 77390267Sdes comment = 1; 77490267Sdes q += 3; 77590267Sdes } 77690267Sdes } 77788771Sdes } 77890267Sdes if (!tag && q > p) 77990267Sdes fwrite(p, q - p, 1, out); 78090267Sdes fputc('\n', out); 78188771Sdes } 78288771Sdes} 78388771Sdes 78490267Sdes 78563012Sdes/***************************************************************************** 78663012Sdes * Core 78760954Sdes */ 78860954Sdes 78960954Sdes/* 79063012Sdes * Send a request and process the reply 79197866Sdes * 79297866Sdes * XXX This function is way too long, the do..while loop should be split 79397866Sdes * XXX off into a separate function. 79460376Sdes */ 79567043SdesFILE * 796174588Sdeshttp_request(struct url *URL, const char *op, struct url_stat *us, 79790267Sdes struct url *purl, const char *flags) 79860376Sdes{ 79997856Sdes conn_t *conn; 80090267Sdes struct url *url, *new; 80190267Sdes int chunked, direct, need_auth, noredirect, verbose; 802143049Skbyanc int e, i, n, val; 80390267Sdes off_t offset, clength, length, size; 80490267Sdes time_t mtime; 80590267Sdes const char *p; 80690267Sdes FILE *f; 80790267Sdes hdr_t h; 808107372Sdes char hbuf[MAXHOSTNAMELEN + 7], *host; 80963012Sdes 81090267Sdes direct = CHECK_FLAG('d'); 81190267Sdes noredirect = CHECK_FLAG('A'); 81290267Sdes verbose = CHECK_FLAG('v'); 81360737Sume 81490267Sdes if (direct && purl) { 81590267Sdes fetchFreeURL(purl); 81690267Sdes purl = NULL; 81790267Sdes } 81863716Sdes 81990267Sdes /* try the provided URL first */ 82090267Sdes url = URL; 82163012Sdes 82290267Sdes /* if the A flag is set, we only get one try */ 82390267Sdes n = noredirect ? 1 : MAX_REDIRECT; 82490267Sdes i = 0; 82563012Sdes 82698422Sdes e = HTTP_PROTOCOL_ERROR; 82790267Sdes need_auth = 0; 82890267Sdes do { 82990267Sdes new = NULL; 83090267Sdes chunked = 0; 83190267Sdes offset = 0; 83290267Sdes clength = -1; 83390267Sdes length = -1; 83490267Sdes size = -1; 83590267Sdes mtime = 0; 83690267Sdes 83790267Sdes /* check port */ 83890267Sdes if (!url->port) 839174588Sdes url->port = fetch_default_port(url->scheme); 84090267Sdes 84190267Sdes /* were we redirected to an FTP URL? */ 84290267Sdes if (purl == NULL && strcmp(url->scheme, SCHEME_FTP) == 0) { 84390267Sdes if (strcmp(op, "GET") == 0) 844174588Sdes return (ftp_request(url, "RETR", us, purl, flags)); 84590267Sdes else if (strcmp(op, "HEAD") == 0) 846174588Sdes return (ftp_request(url, "STAT", us, purl, flags)); 84790267Sdes } 84890267Sdes 84990267Sdes /* connect to server or proxy */ 850174588Sdes if ((conn = http_connect(url, purl, flags)) == NULL) 85190267Sdes goto ouch; 85290267Sdes 85390267Sdes host = url->host; 85460737Sume#ifdef INET6 85590267Sdes if (strchr(url->host, ':')) { 85690267Sdes snprintf(hbuf, sizeof(hbuf), "[%s]", url->host); 85790267Sdes host = hbuf; 85890267Sdes } 85960737Sume#endif 860174588Sdes if (url->port != fetch_default_port(url->scheme)) { 861107372Sdes if (host != hbuf) { 862107372Sdes strcpy(hbuf, host); 863107372Sdes host = hbuf; 864107372Sdes } 865107372Sdes snprintf(hbuf + strlen(hbuf), 866107372Sdes sizeof(hbuf) - strlen(hbuf), ":%d", url->port); 867107372Sdes } 86837535Sdes 86990267Sdes /* send request */ 87090267Sdes if (verbose) 871174588Sdes fetch_info("requesting %s://%s%s", 872107372Sdes url->scheme, host, url->doc); 87390267Sdes if (purl) { 874174588Sdes http_cmd(conn, "%s %s://%s%s HTTP/1.1", 875107372Sdes op, url->scheme, host, url->doc); 87690267Sdes } else { 877174588Sdes http_cmd(conn, "%s %s HTTP/1.1", 87890267Sdes op, url->doc); 87990267Sdes } 88037535Sdes 88190267Sdes /* virtual host */ 882174588Sdes http_cmd(conn, "Host: %s", host); 88390267Sdes 88490267Sdes /* proxy authorization */ 88590267Sdes if (purl) { 88690267Sdes if (*purl->user || *purl->pwd) 887174588Sdes http_basic_auth(conn, "Proxy-Authorization", 88890267Sdes purl->user, purl->pwd); 88990267Sdes else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL && *p != '\0') 890174588Sdes http_authorize(conn, "Proxy-Authorization", p); 89190267Sdes } 89290267Sdes 89390267Sdes /* server authorization */ 89490267Sdes if (need_auth || *url->user || *url->pwd) { 89590267Sdes if (*url->user || *url->pwd) 896174588Sdes http_basic_auth(conn, "Authorization", url->user, url->pwd); 89790267Sdes else if ((p = getenv("HTTP_AUTH")) != NULL && *p != '\0') 898174588Sdes http_authorize(conn, "Authorization", p); 89990267Sdes else if (fetchAuthMethod && fetchAuthMethod(url) == 0) { 900174588Sdes http_basic_auth(conn, "Authorization", url->user, url->pwd); 90190267Sdes } else { 902174588Sdes http_seterr(HTTP_NEED_AUTH); 90390267Sdes goto ouch; 90490267Sdes } 90590267Sdes } 90690267Sdes 90790267Sdes /* other headers */ 908107372Sdes if ((p = getenv("HTTP_REFERER")) != NULL && *p != '\0') { 909107372Sdes if (strcasecmp(p, "auto") == 0) 910174588Sdes http_cmd(conn, "Referer: %s://%s%s", 911107372Sdes url->scheme, host, url->doc); 912107372Sdes else 913174588Sdes http_cmd(conn, "Referer: %s", p); 914107372Sdes } 91590267Sdes if ((p = getenv("HTTP_USER_AGENT")) != NULL && *p != '\0') 916174588Sdes http_cmd(conn, "User-Agent: %s", p); 91790267Sdes else 918174588Sdes http_cmd(conn, "User-Agent: %s " _LIBFETCH_VER, getprogname()); 919109693Sdes if (url->offset > 0) 920174588Sdes http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset); 921174588Sdes http_cmd(conn, "Connection: close"); 922174588Sdes http_cmd(conn, ""); 92390267Sdes 924143049Skbyanc /* 925143049Skbyanc * Force the queued request to be dispatched. Normally, one 926143049Skbyanc * would do this with shutdown(2) but squid proxies can be 927143049Skbyanc * configured to disallow such half-closed connections. To 928143049Skbyanc * be compatible with such configurations, fiddle with socket 929143049Skbyanc * options to force the pending data to be written. 930143049Skbyanc */ 931143049Skbyanc val = 0; 932143049Skbyanc setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val, 933143049Skbyanc sizeof(val)); 934143049Skbyanc val = 1; 935143049Skbyanc setsockopt(conn->sd, IPPROTO_TCP, TCP_NODELAY, &val, 936143049Skbyanc sizeof(val)); 937143049Skbyanc 93890267Sdes /* get reply */ 939174588Sdes switch (http_get_reply(conn)) { 94090267Sdes case HTTP_OK: 94190267Sdes case HTTP_PARTIAL: 94290267Sdes /* fine */ 94390267Sdes break; 94490267Sdes case HTTP_MOVED_PERM: 94590267Sdes case HTTP_MOVED_TEMP: 94690267Sdes case HTTP_SEE_OTHER: 94790267Sdes /* 948125695Sdes * Not so fine, but we still have to read the 949125695Sdes * headers to get the new location. 95090267Sdes */ 95190267Sdes break; 95290267Sdes case HTTP_NEED_AUTH: 95390267Sdes if (need_auth) { 95490267Sdes /* 955125695Sdes * We already sent out authorization code, 956125695Sdes * so there's nothing more we can do. 95790267Sdes */ 958174588Sdes http_seterr(conn->err); 95990267Sdes goto ouch; 96090267Sdes } 96190267Sdes /* try again, but send the password this time */ 96290267Sdes if (verbose) 963174588Sdes fetch_info("server requires authorization"); 96490267Sdes break; 96590267Sdes case HTTP_NEED_PROXY_AUTH: 96690267Sdes /* 967125695Sdes * If we're talking to a proxy, we already sent 968125695Sdes * our proxy authorization code, so there's 969125695Sdes * nothing more we can do. 97090267Sdes */ 971174588Sdes http_seterr(conn->err); 97290267Sdes goto ouch; 973125696Sdes case HTTP_BAD_RANGE: 974125696Sdes /* 975125696Sdes * This can happen if we ask for 0 bytes because 976125696Sdes * we already have the whole file. Consider this 977125696Sdes * a success for now, and check sizes later. 978125696Sdes */ 979125696Sdes break; 98090267Sdes case HTTP_PROTOCOL_ERROR: 98190267Sdes /* fall through */ 98290267Sdes case -1: 983174588Sdes fetch_syserr(); 98490267Sdes goto ouch; 98590267Sdes default: 986174588Sdes http_seterr(conn->err); 98790267Sdes if (!verbose) 98890267Sdes goto ouch; 98990267Sdes /* fall through so we can get the full error message */ 99090267Sdes } 99190267Sdes 99290267Sdes /* get headers */ 99390267Sdes do { 994174588Sdes switch ((h = http_next_header(conn, &p))) { 99590267Sdes case hdr_syserror: 996174588Sdes fetch_syserr(); 99790267Sdes goto ouch; 99890267Sdes case hdr_error: 999174588Sdes http_seterr(HTTP_PROTOCOL_ERROR); 100090267Sdes goto ouch; 100190267Sdes case hdr_content_length: 1002174588Sdes http_parse_length(p, &clength); 100390267Sdes break; 100490267Sdes case hdr_content_range: 1005174588Sdes http_parse_range(p, &offset, &length, &size); 100690267Sdes break; 100790267Sdes case hdr_last_modified: 1008174588Sdes http_parse_mtime(p, &mtime); 100990267Sdes break; 101090267Sdes case hdr_location: 101197856Sdes if (!HTTP_REDIRECT(conn->err)) 101290267Sdes break; 101390267Sdes if (new) 101490267Sdes free(new); 101590267Sdes if (verbose) 1016174588Sdes fetch_info("%d redirect to %s", conn->err, p); 101790267Sdes if (*p == '/') 101890267Sdes /* absolute path */ 101990267Sdes new = fetchMakeURL(url->scheme, url->host, url->port, p, 102090267Sdes url->user, url->pwd); 102190267Sdes else 102290267Sdes new = fetchParseURL(p); 102390267Sdes if (new == NULL) { 102490267Sdes /* XXX should set an error code */ 102590267Sdes DEBUG(fprintf(stderr, "failed to parse new URL\n")); 102690267Sdes goto ouch; 102790267Sdes } 102890267Sdes if (!*new->user && !*new->pwd) { 102990267Sdes strcpy(new->user, url->user); 103090267Sdes strcpy(new->pwd, url->pwd); 103190267Sdes } 103290267Sdes new->offset = url->offset; 103390267Sdes new->length = url->length; 103490267Sdes break; 103590267Sdes case hdr_transfer_encoding: 103690267Sdes /* XXX weak test*/ 103790267Sdes chunked = (strcasecmp(p, "chunked") == 0); 103890267Sdes break; 103990267Sdes case hdr_www_authenticate: 104097856Sdes if (conn->err != HTTP_NEED_AUTH) 104190267Sdes break; 104290267Sdes /* if we were smarter, we'd check the method and realm */ 104390267Sdes break; 104490267Sdes case hdr_end: 104590267Sdes /* fall through */ 104690267Sdes case hdr_unknown: 104790267Sdes /* ignore */ 104890267Sdes break; 104990267Sdes } 105090267Sdes } while (h > hdr_end); 105190267Sdes 105290267Sdes /* we need to provide authentication */ 105397856Sdes if (conn->err == HTTP_NEED_AUTH) { 105498422Sdes e = conn->err; 105590267Sdes need_auth = 1; 1056174588Sdes fetch_close(conn); 105797856Sdes conn = NULL; 105890267Sdes continue; 105990267Sdes } 106090267Sdes 1061125696Sdes /* requested range not satisfiable */ 1062125696Sdes if (conn->err == HTTP_BAD_RANGE) { 1063125696Sdes if (url->offset == size && url->length == 0) { 1064125696Sdes /* asked for 0 bytes; fake it */ 1065125696Sdes offset = url->offset; 1066125696Sdes conn->err = HTTP_OK; 1067125696Sdes break; 1068125696Sdes } else { 1069174588Sdes http_seterr(conn->err); 1070125696Sdes goto ouch; 1071125696Sdes } 1072125696Sdes } 1073125696Sdes 1074104404Sru /* we have a hit or an error */ 1075104404Sru if (conn->err == HTTP_OK || conn->err == HTTP_PARTIAL || HTTP_ERROR(conn->err)) 1076104404Sru break; 1077104404Sru 107890267Sdes /* all other cases: we got a redirect */ 107998422Sdes e = conn->err; 108090267Sdes need_auth = 0; 1081174588Sdes fetch_close(conn); 108297856Sdes conn = NULL; 108390267Sdes if (!new) { 108490267Sdes DEBUG(fprintf(stderr, "redirect with no new location\n")); 108590267Sdes break; 108690267Sdes } 108790267Sdes if (url != URL) 108890267Sdes fetchFreeURL(url); 108990267Sdes url = new; 109090267Sdes } while (++i < n); 109190267Sdes 109290267Sdes /* we failed, or ran out of retries */ 109397856Sdes if (conn == NULL) { 1094174588Sdes http_seterr(e); 109563012Sdes goto ouch; 109663012Sdes } 109760376Sdes 109890267Sdes DEBUG(fprintf(stderr, "offset %lld, length %lld," 109990267Sdes " size %lld, clength %lld\n", 110090267Sdes (long long)offset, (long long)length, 110190267Sdes (long long)size, (long long)clength)); 110260376Sdes 110390267Sdes /* check for inconsistencies */ 110490267Sdes if (clength != -1 && length != -1 && clength != length) { 1105174588Sdes http_seterr(HTTP_PROTOCOL_ERROR); 110663012Sdes goto ouch; 110763012Sdes } 110890267Sdes if (clength == -1) 110990267Sdes clength = length; 111090267Sdes if (clength != -1) 111190267Sdes length = offset + clength; 111290267Sdes if (length != -1 && size != -1 && length != size) { 1113174588Sdes http_seterr(HTTP_PROTOCOL_ERROR); 111463012Sdes goto ouch; 111590267Sdes } 111690267Sdes if (size == -1) 111790267Sdes size = length; 111860376Sdes 111990267Sdes /* fill in stats */ 112090267Sdes if (us) { 112190267Sdes us->size = size; 112290267Sdes us->atime = us->mtime = mtime; 112390267Sdes } 112463069Sdes 112590267Sdes /* too far? */ 1126109693Sdes if (URL->offset > 0 && offset > URL->offset) { 1127174588Sdes http_seterr(HTTP_PROTOCOL_ERROR); 112890267Sdes goto ouch; 112977238Sdes } 113060376Sdes 113190267Sdes /* report back real offset and size */ 113290267Sdes URL->offset = offset; 113390267Sdes URL->length = clength; 113437535Sdes 113590267Sdes /* wrap it up in a FILE */ 1136174588Sdes if ((f = http_funopen(conn, chunked)) == NULL) { 1137174588Sdes fetch_syserr(); 113890267Sdes goto ouch; 113990267Sdes } 114063716Sdes 114190267Sdes if (url != URL) 114290267Sdes fetchFreeURL(url); 114390267Sdes if (purl) 114490267Sdes fetchFreeURL(purl); 114563567Sdes 114697856Sdes if (HTTP_ERROR(conn->err)) { 1147174588Sdes http_print_html(stderr, f); 114890267Sdes fclose(f); 114990267Sdes f = NULL; 115090267Sdes } 115163012Sdes 115290267Sdes return (f); 115388771Sdes 115490267Sdesouch: 115590267Sdes if (url != URL) 115690267Sdes fetchFreeURL(url); 115790267Sdes if (purl) 115890267Sdes fetchFreeURL(purl); 115997856Sdes if (conn != NULL) 1160174588Sdes fetch_close(conn); 116190267Sdes return (NULL); 116263012Sdes} 116360189Sdes 116490267Sdes 116563012Sdes/***************************************************************************** 116663012Sdes * Entry points 116763012Sdes */ 116863012Sdes 116963012Sdes/* 117063340Sdes * Retrieve and stat a file by HTTP 117163340Sdes */ 117263340SdesFILE * 117375891SarchiefetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags) 117463340Sdes{ 1175174752Sdes return (http_request(URL, "GET", us, http_get_proxy(URL, flags), flags)); 117663340Sdes} 117763340Sdes 117863340Sdes/* 117963012Sdes * Retrieve a file by HTTP 118063012Sdes */ 118163012SdesFILE * 118275891SarchiefetchGetHTTP(struct url *URL, const char *flags) 118363012Sdes{ 118490267Sdes return (fetchXGetHTTP(URL, NULL, flags)); 118537535Sdes} 118637535Sdes 118763340Sdes/* 118863340Sdes * Store a file by HTTP 118963340Sdes */ 119037535SdesFILE * 119185093SdesfetchPutHTTP(struct url *URL __unused, const char *flags __unused) 119237535Sdes{ 119390267Sdes warnx("fetchPutHTTP(): not implemented"); 119490267Sdes return (NULL); 119537535Sdes} 119640975Sdes 119740975Sdes/* 119840975Sdes * Get an HTTP document's metadata 119940975Sdes */ 120040975Sdesint 120175891SarchiefetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags) 120240975Sdes{ 120390267Sdes FILE *f; 120490267Sdes 1205174752Sdes f = http_request(URL, "HEAD", us, http_get_proxy(URL, flags), flags); 1206112081Sdes if (f == NULL) 120790267Sdes return (-1); 120890267Sdes fclose(f); 120990267Sdes return (0); 121040975Sdes} 121141989Sdes 121241989Sdes/* 121341989Sdes * List a directory 121441989Sdes */ 121541989Sdesstruct url_ent * 121685093SdesfetchListHTTP(struct url *url __unused, const char *flags __unused) 121741989Sdes{ 121890267Sdes warnx("fetchListHTTP(): not implemented"); 121990267Sdes return (NULL); 122041989Sdes} 1221