http.c revision 133280
137535Sdes/*- 263012Sdes * Copyright (c) 2000 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 133280 2004-08-07 20:23:50Z 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 7937535Sdes#include "fetch.h" 8040939Sdes#include "common.h" 8141862Sdes#include "httperr.h" 8237535Sdes 8363012Sdes/* Maximum number of redirects to follow */ 8463012Sdes#define MAX_REDIRECT 5 8537535Sdes 8663012Sdes/* Symbolic names for reply codes we care about */ 8763012Sdes#define HTTP_OK 200 8863012Sdes#define HTTP_PARTIAL 206 8963012Sdes#define HTTP_MOVED_PERM 301 9063012Sdes#define HTTP_MOVED_TEMP 302 9163012Sdes#define HTTP_SEE_OTHER 303 9263012Sdes#define HTTP_NEED_AUTH 401 9387317Sdes#define HTTP_NEED_PROXY_AUTH 407 94125696Sdes#define HTTP_BAD_RANGE 416 9563012Sdes#define HTTP_PROTOCOL_ERROR 999 9660196Sdes 9763012Sdes#define HTTP_REDIRECT(xyz) ((xyz) == HTTP_MOVED_PERM \ 9890267Sdes || (xyz) == HTTP_MOVED_TEMP \ 9990267Sdes || (xyz) == HTTP_SEE_OTHER) 10063012Sdes 10188771Sdes#define HTTP_ERROR(xyz) ((xyz) > 400 && (xyz) < 599) 10263012Sdes 10390267Sdes 10463012Sdes/***************************************************************************** 10563012Sdes * I/O functions for decoding chunked streams 10663012Sdes */ 10763012Sdes 10897859Sdesstruct httpio 10937535Sdes{ 11097858Sdes conn_t *conn; /* connection */ 11197866Sdes int chunked; /* chunked mode */ 11297858Sdes char *buf; /* chunk buffer */ 11397866Sdes size_t bufsize; /* size of chunk buffer */ 11497866Sdes ssize_t buflen; /* amount of data currently in buffer */ 11597866Sdes int bufpos; /* current read offset in buffer */ 11697858Sdes int eof; /* end-of-file flag */ 11797858Sdes int error; /* error flag */ 11897858Sdes size_t chunksize; /* remaining size of current chunk */ 11963281Sdes#ifndef NDEBUG 12090267Sdes size_t total; 12163012Sdes#endif 12237535Sdes}; 12337535Sdes 12437608Sdes/* 12563012Sdes * Get next chunk header 12637608Sdes */ 12737608Sdesstatic int 12897859Sdes_http_new_chunk(struct httpio *io) 12937608Sdes{ 13090267Sdes char *p; 13190267Sdes 13297859Sdes if (_fetch_getln(io->conn) == -1) 13390267Sdes return (-1); 13490267Sdes 13597859Sdes if (io->conn->buflen < 2 || !ishexnumber(*io->conn->buf)) 13690267Sdes return (-1); 13790267Sdes 13897859Sdes for (p = io->conn->buf; *p && !isspace(*p); ++p) { 13990267Sdes if (*p == ';') 14090267Sdes break; 14190267Sdes if (!ishexnumber(*p)) 14290267Sdes return (-1); 14390267Sdes if (isdigit(*p)) { 14497859Sdes io->chunksize = io->chunksize * 16 + 14590267Sdes *p - '0'; 14690267Sdes } else { 14797859Sdes io->chunksize = io->chunksize * 16 + 14890267Sdes 10 + tolower(*p) - 'a'; 14990267Sdes } 15090267Sdes } 15190267Sdes 15263281Sdes#ifndef NDEBUG 15390267Sdes if (fetchDebug) { 15497859Sdes io->total += io->chunksize; 15597859Sdes if (io->chunksize == 0) 156106207Sdes fprintf(stderr, "%s(): end of last chunk\n", __func__); 15790267Sdes else 158106207Sdes fprintf(stderr, "%s(): new chunk: %lu (%lu)\n", 159106207Sdes __func__, (unsigned long)io->chunksize, 160106207Sdes (unsigned long)io->total); 16190267Sdes } 16263012Sdes#endif 16390267Sdes 16497859Sdes return (io->chunksize); 16537608Sdes} 16637608Sdes 16737608Sdes/* 16897866Sdes * Grow the input buffer to at least len bytes 16997866Sdes */ 17097866Sdesstatic inline int 17197866Sdes_http_growbuf(struct httpio *io, size_t len) 17297866Sdes{ 17397866Sdes char *tmp; 17497866Sdes 17597866Sdes if (io->bufsize >= len) 17697866Sdes return (0); 17797866Sdes 17897866Sdes if ((tmp = realloc(io->buf, len)) == NULL) 17997866Sdes return (-1); 18097866Sdes io->buf = tmp; 18197866Sdes io->bufsize = len; 182106044Sdes return (0); 18397866Sdes} 18497866Sdes 18597866Sdes/* 18637608Sdes * Fill the input buffer, do chunk decoding on the fly 18737608Sdes */ 18863012Sdesstatic int 18997866Sdes_http_fillbuf(struct httpio *io, size_t len) 19037535Sdes{ 19197859Sdes if (io->error) 19290267Sdes return (-1); 19397859Sdes if (io->eof) 19490267Sdes return (0); 19590267Sdes 19697866Sdes if (io->chunked == 0) { 19797866Sdes if (_http_growbuf(io, len) == -1) 19897866Sdes return (-1); 199106185Sdes if ((io->buflen = _fetch_read(io->conn, io->buf, len)) == -1) { 200106185Sdes io->error = 1; 20197866Sdes return (-1); 202106185Sdes } 20397866Sdes io->bufpos = 0; 20497866Sdes return (io->buflen); 20597866Sdes } 20697866Sdes 20797859Sdes if (io->chunksize == 0) { 20897859Sdes switch (_http_new_chunk(io)) { 20990267Sdes case -1: 21097859Sdes io->error = 1; 21190267Sdes return (-1); 21290267Sdes case 0: 21397859Sdes io->eof = 1; 21490267Sdes return (0); 21590267Sdes } 21637535Sdes } 21763012Sdes 21897866Sdes if (len > io->chunksize) 21997866Sdes len = io->chunksize; 22097866Sdes if (_http_growbuf(io, len) == -1) 22190267Sdes return (-1); 222106185Sdes if ((io->buflen = _fetch_read(io->conn, io->buf, len)) == -1) { 223106185Sdes io->error = 1; 22497866Sdes return (-1); 225106185Sdes } 22697866Sdes io->chunksize -= io->buflen; 22790267Sdes 22897859Sdes if (io->chunksize == 0) { 22997856Sdes char endl[2]; 23097856Sdes 23197866Sdes if (_fetch_read(io->conn, endl, 2) != 2 || 23297856Sdes endl[0] != '\r' || endl[1] != '\n') 23390267Sdes return (-1); 23490267Sdes } 23590267Sdes 23697866Sdes io->bufpos = 0; 23790267Sdes 23897866Sdes return (io->buflen); 23937535Sdes} 24037535Sdes 24137608Sdes/* 24237608Sdes * Read function 24337608Sdes */ 24437535Sdesstatic int 24563012Sdes_http_readfn(void *v, char *buf, int len) 24637535Sdes{ 24797859Sdes struct httpio *io = (struct httpio *)v; 24890267Sdes int l, pos; 24963012Sdes 25097859Sdes if (io->error) 25190267Sdes return (-1); 25297859Sdes if (io->eof) 25390267Sdes return (0); 25463012Sdes 25590267Sdes for (pos = 0; len > 0; pos += l, len -= l) { 25690267Sdes /* empty buffer */ 25797866Sdes if (!io->buf || io->bufpos == io->buflen) 25897866Sdes if (_http_fillbuf(io, len) < 1) 25990267Sdes break; 26097866Sdes l = io->buflen - io->bufpos; 26190267Sdes if (len < l) 26290267Sdes l = len; 26397866Sdes bcopy(io->buf + io->bufpos, buf + pos, l); 26497866Sdes io->bufpos += l; 26590267Sdes } 26637535Sdes 26797859Sdes if (!pos && io->error) 26890267Sdes return (-1); 26990267Sdes return (pos); 27037535Sdes} 27137535Sdes 27237608Sdes/* 27337608Sdes * Write function 27437608Sdes */ 27537535Sdesstatic int 27663012Sdes_http_writefn(void *v, const char *buf, int len) 27737535Sdes{ 27897859Sdes struct httpio *io = (struct httpio *)v; 27990267Sdes 28097866Sdes return (_fetch_write(io->conn, buf, len)); 28137535Sdes} 28237535Sdes 28337608Sdes/* 28437608Sdes * Close function 28537608Sdes */ 28637535Sdesstatic int 28763012Sdes_http_closefn(void *v) 28837535Sdes{ 28997859Sdes struct httpio *io = (struct httpio *)v; 29090267Sdes int r; 29163012Sdes 29297859Sdes r = _fetch_close(io->conn); 29397859Sdes if (io->buf) 29497859Sdes free(io->buf); 29597859Sdes free(io); 29690267Sdes return (r); 29737535Sdes} 29837535Sdes 29937608Sdes/* 30063012Sdes * Wrap a file descriptor up 30137608Sdes */ 30263012Sdesstatic FILE * 30397866Sdes_http_funopen(conn_t *conn, int chunked) 30437535Sdes{ 30597859Sdes struct httpio *io; 30690267Sdes FILE *f; 30763012Sdes 308109967Sdes if ((io = calloc(1, sizeof(*io))) == NULL) { 30990267Sdes _fetch_syserr(); 31090267Sdes return (NULL); 31190267Sdes } 31297859Sdes io->conn = conn; 31397866Sdes io->chunked = chunked; 31497859Sdes f = funopen(io, _http_readfn, _http_writefn, NULL, _http_closefn); 31590267Sdes if (f == NULL) { 31690267Sdes _fetch_syserr(); 31797859Sdes free(io); 31890267Sdes return (NULL); 31990267Sdes } 32090267Sdes return (f); 32163012Sdes} 32263012Sdes 32390267Sdes 32463012Sdes/***************************************************************************** 32563012Sdes * Helper functions for talking to the server and parsing its replies 32663012Sdes */ 32763012Sdes 32863012Sdes/* Header types */ 32963012Sdestypedef enum { 33090267Sdes hdr_syserror = -2, 33190267Sdes hdr_error = -1, 33290267Sdes hdr_end = 0, 33390267Sdes hdr_unknown = 1, 33490267Sdes hdr_content_length, 33590267Sdes hdr_content_range, 33690267Sdes hdr_last_modified, 33790267Sdes hdr_location, 33890267Sdes hdr_transfer_encoding, 33990267Sdes hdr_www_authenticate 34085093Sdes} hdr_t; 34163012Sdes 34263012Sdes/* Names of interesting headers */ 34363012Sdesstatic struct { 34490267Sdes hdr_t num; 34590267Sdes const char *name; 34663012Sdes} hdr_names[] = { 34790267Sdes { hdr_content_length, "Content-Length" }, 34890267Sdes { hdr_content_range, "Content-Range" }, 34990267Sdes { hdr_last_modified, "Last-Modified" }, 35090267Sdes { hdr_location, "Location" }, 35190267Sdes { hdr_transfer_encoding, "Transfer-Encoding" }, 35290267Sdes { hdr_www_authenticate, "WWW-Authenticate" }, 35390267Sdes { hdr_unknown, NULL }, 35463012Sdes}; 35563012Sdes 35663012Sdes/* 35763012Sdes * Send a formatted line; optionally echo to terminal 35863012Sdes */ 35963012Sdesstatic int 36097856Sdes_http_cmd(conn_t *conn, const char *fmt, ...) 36163012Sdes{ 36290267Sdes va_list ap; 36390267Sdes size_t len; 36490267Sdes char *msg; 36590267Sdes int r; 36663012Sdes 36790267Sdes va_start(ap, fmt); 36890267Sdes len = vasprintf(&msg, fmt, ap); 36990267Sdes va_end(ap); 37090267Sdes 37190267Sdes if (msg == NULL) { 37290267Sdes errno = ENOMEM; 37390267Sdes _fetch_syserr(); 37490267Sdes return (-1); 37590267Sdes } 37690267Sdes 37797856Sdes r = _fetch_putln(conn, msg, len); 37890267Sdes free(msg); 37990267Sdes 38090267Sdes if (r == -1) { 38190267Sdes _fetch_syserr(); 38290267Sdes return (-1); 38390267Sdes } 38490267Sdes 38590267Sdes return (0); 38663012Sdes} 38763012Sdes 38863012Sdes/* 38963012Sdes * Get and parse status line 39063012Sdes */ 39163012Sdesstatic int 39297856Sdes_http_get_reply(conn_t *conn) 39363012Sdes{ 39490267Sdes char *p; 39590267Sdes 39697856Sdes if (_fetch_getln(conn) == -1) 39790267Sdes return (-1); 39890267Sdes /* 39990267Sdes * A valid status line looks like "HTTP/m.n xyz reason" where m 40090267Sdes * and n are the major and minor protocol version numbers and xyz 40190267Sdes * is the reply code. 40290267Sdes * Unfortunately, there are servers out there (NCSA 1.5.1, to name 40390267Sdes * just one) that do not send a version number, so we can't rely 40490267Sdes * on finding one, but if we do, insist on it being 1.0 or 1.1. 40590267Sdes * We don't care about the reason phrase. 40690267Sdes */ 40797856Sdes if (strncmp(conn->buf, "HTTP", 4) != 0) 40890267Sdes return (HTTP_PROTOCOL_ERROR); 40997856Sdes p = conn->buf + 4; 41090267Sdes if (*p == '/') { 41190267Sdes if (p[1] != '1' || p[2] != '.' || (p[3] != '0' && p[3] != '1')) 41290267Sdes return (HTTP_PROTOCOL_ERROR); 41390267Sdes p += 4; 41490267Sdes } 41590267Sdes if (*p != ' ' || !isdigit(p[1]) || !isdigit(p[2]) || !isdigit(p[3])) 41690267Sdes return (HTTP_PROTOCOL_ERROR); 41790267Sdes 41897856Sdes conn->err = (p[1] - '0') * 100 + (p[2] - '0') * 10 + (p[3] - '0'); 41997856Sdes return (conn->err); 42037535Sdes} 42137535Sdes 42237608Sdes/* 42390267Sdes * Check a header; if the type matches the given string, return a pointer 42490267Sdes * to the beginning of the value. 42563012Sdes */ 42675891Sarchiestatic const char * 42775891Sarchie_http_match(const char *str, const char *hdr) 42863012Sdes{ 42990267Sdes while (*str && *hdr && tolower(*str++) == tolower(*hdr++)) 43090267Sdes /* nothing */; 43190267Sdes if (*str || *hdr != ':') 43290267Sdes return (NULL); 43390267Sdes while (*hdr && isspace(*++hdr)) 43490267Sdes /* nothing */; 43590267Sdes return (hdr); 43663012Sdes} 43763012Sdes 43863012Sdes/* 43963012Sdes * Get the next header and return the appropriate symbolic code. 44063012Sdes */ 44185093Sdesstatic hdr_t 44297856Sdes_http_next_header(conn_t *conn, const char **p) 44363012Sdes{ 44490267Sdes int i; 44590267Sdes 44697856Sdes if (_fetch_getln(conn) == -1) 44790267Sdes return (hdr_syserror); 44897856Sdes while (conn->buflen && isspace(conn->buf[conn->buflen - 1])) 44997856Sdes conn->buflen--; 45097856Sdes conn->buf[conn->buflen] = '\0'; 45197856Sdes if (conn->buflen == 0) 45297856Sdes return (hdr_end); 45390267Sdes /* 45490267Sdes * We could check for malformed headers but we don't really care. 45590267Sdes * A valid header starts with a token immediately followed by a 45690267Sdes * colon; a token is any sequence of non-control, non-whitespace 45790267Sdes * characters except "()<>@,;:\\\"{}". 45890267Sdes */ 45990267Sdes for (i = 0; hdr_names[i].num != hdr_unknown; i++) 46097856Sdes if ((*p = _http_match(hdr_names[i].name, conn->buf)) != NULL) 46190267Sdes return (hdr_names[i].num); 46290267Sdes return (hdr_unknown); 46363012Sdes} 46463012Sdes 46563012Sdes/* 46663012Sdes * Parse a last-modified header 46763012Sdes */ 46863716Sdesstatic int 46975891Sarchie_http_parse_mtime(const char *p, time_t *mtime) 47063012Sdes{ 47190267Sdes char locale[64], *r; 47290267Sdes struct tm tm; 47363012Sdes 474109967Sdes strncpy(locale, setlocale(LC_TIME, NULL), sizeof(locale)); 47590267Sdes setlocale(LC_TIME, "C"); 47690267Sdes r = strptime(p, "%a, %d %b %Y %H:%M:%S GMT", &tm); 47790267Sdes /* XXX should add support for date-2 and date-3 */ 47890267Sdes setlocale(LC_TIME, locale); 47990267Sdes if (r == NULL) 48090267Sdes return (-1); 48190267Sdes DEBUG(fprintf(stderr, "last modified: [%04d-%02d-%02d " 48288769Sdes "%02d:%02d:%02d]\n", 48363012Sdes tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, 48463012Sdes tm.tm_hour, tm.tm_min, tm.tm_sec)); 48590267Sdes *mtime = timegm(&tm); 48690267Sdes return (0); 48763012Sdes} 48863012Sdes 48963012Sdes/* 49063012Sdes * Parse a content-length header 49163012Sdes */ 49263716Sdesstatic int 49375891Sarchie_http_parse_length(const char *p, off_t *length) 49463012Sdes{ 49590267Sdes off_t len; 49690267Sdes 49790267Sdes for (len = 0; *p && isdigit(*p); ++p) 49890267Sdes len = len * 10 + (*p - '0'); 49990267Sdes if (*p) 50090267Sdes return (-1); 50190267Sdes DEBUG(fprintf(stderr, "content length: [%lld]\n", 50290267Sdes (long long)len)); 50390267Sdes *length = len; 50490267Sdes return (0); 50563012Sdes} 50663012Sdes 50763012Sdes/* 50863012Sdes * Parse a content-range header 50963012Sdes */ 51063716Sdesstatic int 51175891Sarchie_http_parse_range(const char *p, off_t *offset, off_t *length, off_t *size) 51263012Sdes{ 51390267Sdes off_t first, last, len; 51463716Sdes 51590267Sdes if (strncasecmp(p, "bytes ", 6) != 0) 51690267Sdes return (-1); 517125696Sdes p += 6; 518125696Sdes if (*p == '*') { 519125696Sdes first = last = -1; 520125696Sdes ++p; 521125696Sdes } else { 522125696Sdes for (first = 0; *p && isdigit(*p); ++p) 523125696Sdes first = first * 10 + *p - '0'; 524125696Sdes if (*p != '-') 525125696Sdes return (-1); 526125696Sdes for (last = 0, ++p; *p && isdigit(*p); ++p) 527125696Sdes last = last * 10 + *p - '0'; 528125696Sdes } 52990267Sdes if (first > last || *p != '/') 53090267Sdes return (-1); 53190267Sdes for (len = 0, ++p; *p && isdigit(*p); ++p) 53290267Sdes len = len * 10 + *p - '0'; 53390267Sdes if (*p || len < last - first + 1) 53490267Sdes return (-1); 535125696Sdes if (first == -1) { 536125696Sdes DEBUG(fprintf(stderr, "content range: [*/%lld]\n", 537125696Sdes (long long)len)); 538125696Sdes *length = 0; 539125696Sdes } else { 540125696Sdes DEBUG(fprintf(stderr, "content range: [%lld-%lld/%lld]\n", 541125696Sdes (long long)first, (long long)last, (long long)len)); 542125696Sdes *length = last - first + 1; 543125696Sdes } 54490267Sdes *offset = first; 54590267Sdes *size = len; 54690267Sdes return (0); 54763012Sdes} 54863012Sdes 54990267Sdes 55063012Sdes/***************************************************************************** 55163012Sdes * Helper functions for authorization 55263012Sdes */ 55363012Sdes 55463012Sdes/* 55537608Sdes * Base64 encoding 55637608Sdes */ 55762965Sdesstatic char * 55890267Sdes_http_base64(const char *src) 55937608Sdes{ 56090267Sdes static const char base64[] = 56190267Sdes "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 56290267Sdes "abcdefghijklmnopqrstuvwxyz" 56390267Sdes "0123456789+/"; 56490267Sdes char *str, *dst; 56590267Sdes size_t l; 56690267Sdes int t, r; 56762965Sdes 56890267Sdes l = strlen(src); 569133280Sdes if ((str = malloc(((l + 2) / 3) * 4 + 1)) == NULL) 57090267Sdes return (NULL); 57190267Sdes dst = str; 57290267Sdes r = 0; 57337608Sdes 57490267Sdes while (l >= 3) { 57590267Sdes t = (src[0] << 16) | (src[1] << 8) | src[2]; 57690267Sdes dst[0] = base64[(t >> 18) & 0x3f]; 57790267Sdes dst[1] = base64[(t >> 12) & 0x3f]; 57890267Sdes dst[2] = base64[(t >> 6) & 0x3f]; 57990267Sdes dst[3] = base64[(t >> 0) & 0x3f]; 58090267Sdes src += 3; l -= 3; 58190267Sdes dst += 4; r += 4; 58290267Sdes } 58337608Sdes 58490267Sdes switch (l) { 58590267Sdes case 2: 58690267Sdes t = (src[0] << 16) | (src[1] << 8); 58790267Sdes dst[0] = base64[(t >> 18) & 0x3f]; 58890267Sdes dst[1] = base64[(t >> 12) & 0x3f]; 58990267Sdes dst[2] = base64[(t >> 6) & 0x3f]; 59090267Sdes dst[3] = '='; 59190267Sdes dst += 4; 59290267Sdes r += 4; 59390267Sdes break; 59490267Sdes case 1: 59590267Sdes t = src[0] << 16; 59690267Sdes dst[0] = base64[(t >> 18) & 0x3f]; 59790267Sdes dst[1] = base64[(t >> 12) & 0x3f]; 59890267Sdes dst[2] = dst[3] = '='; 59990267Sdes dst += 4; 60090267Sdes r += 4; 60190267Sdes break; 60290267Sdes case 0: 60390267Sdes break; 60490267Sdes } 60590267Sdes 60690267Sdes *dst = 0; 60790267Sdes return (str); 60837608Sdes} 60937608Sdes 61037608Sdes/* 61137608Sdes * Encode username and password 61237608Sdes */ 61362965Sdesstatic int 61497856Sdes_http_basic_auth(conn_t *conn, const char *hdr, const char *usr, const char *pwd) 61537608Sdes{ 61690267Sdes char *upw, *auth; 61790267Sdes int r; 61837608Sdes 61990267Sdes DEBUG(fprintf(stderr, "usr: [%s]\n", usr)); 62090267Sdes DEBUG(fprintf(stderr, "pwd: [%s]\n", pwd)); 62190267Sdes if (asprintf(&upw, "%s:%s", usr, pwd) == -1) 62290267Sdes return (-1); 62390267Sdes auth = _http_base64(upw); 62490267Sdes free(upw); 62590267Sdes if (auth == NULL) 62690267Sdes return (-1); 62797856Sdes r = _http_cmd(conn, "%s: Basic %s", hdr, auth); 62890267Sdes free(auth); 62990267Sdes return (r); 63062965Sdes} 63162965Sdes 63262965Sdes/* 63362965Sdes * Send an authorization header 63462965Sdes */ 63562965Sdesstatic int 63697856Sdes_http_authorize(conn_t *conn, const char *hdr, const char *p) 63762965Sdes{ 63890267Sdes /* basic authorization */ 63990267Sdes if (strncasecmp(p, "basic:", 6) == 0) { 64090267Sdes char *user, *pwd, *str; 64190267Sdes int r; 64262965Sdes 64390267Sdes /* skip realm */ 64490267Sdes for (p += 6; *p && *p != ':'; ++p) 64590267Sdes /* nothing */ ; 64690267Sdes if (!*p || strchr(++p, ':') == NULL) 64790267Sdes return (-1); 64890267Sdes if ((str = strdup(p)) == NULL) 64990267Sdes return (-1); /* XXX */ 65090267Sdes user = str; 65190267Sdes pwd = strchr(str, ':'); 65290267Sdes *pwd++ = '\0'; 65397856Sdes r = _http_basic_auth(conn, hdr, user, pwd); 65490267Sdes free(str); 65590267Sdes return (r); 65690267Sdes } 65790267Sdes return (-1); 65837608Sdes} 65937608Sdes 66090267Sdes 66163012Sdes/***************************************************************************** 66263012Sdes * Helper functions for connecting to a server or proxy 66363012Sdes */ 66463012Sdes 66537608Sdes/* 66690267Sdes * Connect to the correct HTTP server or proxy. 66763012Sdes */ 66897856Sdesstatic conn_t * 66975891Sarchie_http_connect(struct url *URL, struct url *purl, const char *flags) 67063012Sdes{ 67197856Sdes conn_t *conn; 67290267Sdes int verbose; 67397856Sdes int af; 67490267Sdes 67563012Sdes#ifdef INET6 67690267Sdes af = AF_UNSPEC; 67760737Sume#else 67890267Sdes af = AF_INET; 67960737Sume#endif 68090267Sdes 68190267Sdes verbose = CHECK_FLAG('v'); 68290267Sdes if (CHECK_FLAG('4')) 68390267Sdes af = AF_INET; 68467043Sdes#ifdef INET6 68590267Sdes else if (CHECK_FLAG('6')) 68690267Sdes af = AF_INET6; 68767043Sdes#endif 68867043Sdes 68997868Sdes if (purl && strcasecmp(URL->scheme, SCHEME_HTTPS) != 0) { 69090267Sdes URL = purl; 69190267Sdes } else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0) { 69290267Sdes /* can't talk http to an ftp server */ 69390267Sdes /* XXX should set an error code */ 69497856Sdes return (NULL); 69590267Sdes } 69690267Sdes 69797856Sdes if ((conn = _fetch_connect(URL->host, URL->port, af, verbose)) == NULL) 69890267Sdes /* _fetch_connect() has already set an error code */ 69997856Sdes return (NULL); 70097868Sdes if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0 && 70197868Sdes _fetch_ssl(conn, verbose) == -1) { 70297868Sdes _fetch_close(conn); 70397891Sdes /* grrr */ 70497891Sdes errno = EAUTH; 70597891Sdes _fetch_syserr(); 70697868Sdes return (NULL); 70797868Sdes } 70897856Sdes return (conn); 70967043Sdes} 71067043Sdes 71167043Sdesstatic struct url * 712112081Sdes_http_get_proxy(const char *flags) 71367043Sdes{ 71490267Sdes struct url *purl; 71590267Sdes char *p; 71690267Sdes 717112797Sdes if (flags != NULL && strchr(flags, 'd') != NULL) 718112081Sdes return (NULL); 71990267Sdes if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) && 72090267Sdes (purl = fetchParseURL(p))) { 72190267Sdes if (!*purl->scheme) 72290267Sdes strcpy(purl->scheme, SCHEME_HTTP); 72390267Sdes if (!purl->port) 72490267Sdes purl->port = _fetch_default_proxy_port(purl->scheme); 72590267Sdes if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0) 72690267Sdes return (purl); 72790267Sdes fetchFreeURL(purl); 72890267Sdes } 72990267Sdes return (NULL); 73060376Sdes} 73160376Sdes 73288771Sdesstatic void 73388771Sdes_http_print_html(FILE *out, FILE *in) 73488771Sdes{ 73590267Sdes size_t len; 73690267Sdes char *line, *p, *q; 73790267Sdes int comment, tag; 73888771Sdes 73990267Sdes comment = tag = 0; 74090267Sdes while ((line = fgetln(in, &len)) != NULL) { 74190267Sdes while (len && isspace(line[len - 1])) 74290267Sdes --len; 74390267Sdes for (p = q = line; q < line + len; ++q) { 74490267Sdes if (comment && *q == '-') { 74590267Sdes if (q + 2 < line + len && 74690267Sdes strcmp(q, "-->") == 0) { 74790267Sdes tag = comment = 0; 74890267Sdes q += 2; 74990267Sdes } 75090267Sdes } else if (tag && !comment && *q == '>') { 75190267Sdes p = q + 1; 75290267Sdes tag = 0; 75390267Sdes } else if (!tag && *q == '<') { 75490267Sdes if (q > p) 75590267Sdes fwrite(p, q - p, 1, out); 75690267Sdes tag = 1; 75790267Sdes if (q + 3 < line + len && 75890267Sdes strcmp(q, "<!--") == 0) { 75990267Sdes comment = 1; 76090267Sdes q += 3; 76190267Sdes } 76290267Sdes } 76388771Sdes } 76490267Sdes if (!tag && q > p) 76590267Sdes fwrite(p, q - p, 1, out); 76690267Sdes fputc('\n', out); 76788771Sdes } 76888771Sdes} 76988771Sdes 77090267Sdes 77163012Sdes/***************************************************************************** 77263012Sdes * Core 77360954Sdes */ 77460954Sdes 77560954Sdes/* 77663012Sdes * Send a request and process the reply 77797866Sdes * 77897866Sdes * XXX This function is way too long, the do..while loop should be split 77997866Sdes * XXX off into a separate function. 78060376Sdes */ 78167043SdesFILE * 78275891Sarchie_http_request(struct url *URL, const char *op, struct url_stat *us, 78390267Sdes struct url *purl, const char *flags) 78460376Sdes{ 78597856Sdes conn_t *conn; 78690267Sdes struct url *url, *new; 78790267Sdes int chunked, direct, need_auth, noredirect, verbose; 78898422Sdes int e, i, n; 78990267Sdes off_t offset, clength, length, size; 79090267Sdes time_t mtime; 79190267Sdes const char *p; 79290267Sdes FILE *f; 79390267Sdes hdr_t h; 794107372Sdes char hbuf[MAXHOSTNAMELEN + 7], *host; 79563012Sdes 79690267Sdes direct = CHECK_FLAG('d'); 79790267Sdes noredirect = CHECK_FLAG('A'); 79890267Sdes verbose = CHECK_FLAG('v'); 79960737Sume 80090267Sdes if (direct && purl) { 80190267Sdes fetchFreeURL(purl); 80290267Sdes purl = NULL; 80390267Sdes } 80463716Sdes 80590267Sdes /* try the provided URL first */ 80690267Sdes url = URL; 80763012Sdes 80890267Sdes /* if the A flag is set, we only get one try */ 80990267Sdes n = noredirect ? 1 : MAX_REDIRECT; 81090267Sdes i = 0; 81163012Sdes 81298422Sdes e = HTTP_PROTOCOL_ERROR; 81390267Sdes need_auth = 0; 81490267Sdes do { 81590267Sdes new = NULL; 81690267Sdes chunked = 0; 81790267Sdes offset = 0; 81890267Sdes clength = -1; 81990267Sdes length = -1; 82090267Sdes size = -1; 82190267Sdes mtime = 0; 82290267Sdes 82390267Sdes /* check port */ 82490267Sdes if (!url->port) 82590267Sdes url->port = _fetch_default_port(url->scheme); 82690267Sdes 82790267Sdes /* were we redirected to an FTP URL? */ 82890267Sdes if (purl == NULL && strcmp(url->scheme, SCHEME_FTP) == 0) { 82990267Sdes if (strcmp(op, "GET") == 0) 83090267Sdes return (_ftp_request(url, "RETR", us, purl, flags)); 83190267Sdes else if (strcmp(op, "HEAD") == 0) 83290267Sdes return (_ftp_request(url, "STAT", us, purl, flags)); 83390267Sdes } 83490267Sdes 83590267Sdes /* connect to server or proxy */ 83697856Sdes if ((conn = _http_connect(url, purl, flags)) == NULL) 83790267Sdes goto ouch; 83890267Sdes 83990267Sdes host = url->host; 84060737Sume#ifdef INET6 84190267Sdes if (strchr(url->host, ':')) { 84290267Sdes snprintf(hbuf, sizeof(hbuf), "[%s]", url->host); 84390267Sdes host = hbuf; 84490267Sdes } 84560737Sume#endif 846107372Sdes if (url->port != _fetch_default_port(url->scheme)) { 847107372Sdes if (host != hbuf) { 848107372Sdes strcpy(hbuf, host); 849107372Sdes host = hbuf; 850107372Sdes } 851107372Sdes snprintf(hbuf + strlen(hbuf), 852107372Sdes sizeof(hbuf) - strlen(hbuf), ":%d", url->port); 853107372Sdes } 85437535Sdes 85590267Sdes /* send request */ 85690267Sdes if (verbose) 857107372Sdes _fetch_info("requesting %s://%s%s", 858107372Sdes url->scheme, host, url->doc); 85990267Sdes if (purl) { 860107372Sdes _http_cmd(conn, "%s %s://%s%s HTTP/1.1", 861107372Sdes op, url->scheme, host, url->doc); 86290267Sdes } else { 86397856Sdes _http_cmd(conn, "%s %s HTTP/1.1", 86490267Sdes op, url->doc); 86590267Sdes } 86637535Sdes 86790267Sdes /* virtual host */ 868107372Sdes _http_cmd(conn, "Host: %s", host); 86990267Sdes 87090267Sdes /* proxy authorization */ 87190267Sdes if (purl) { 87290267Sdes if (*purl->user || *purl->pwd) 87397856Sdes _http_basic_auth(conn, "Proxy-Authorization", 87490267Sdes purl->user, purl->pwd); 87590267Sdes else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL && *p != '\0') 87697856Sdes _http_authorize(conn, "Proxy-Authorization", p); 87790267Sdes } 87890267Sdes 87990267Sdes /* server authorization */ 88090267Sdes if (need_auth || *url->user || *url->pwd) { 88190267Sdes if (*url->user || *url->pwd) 88297856Sdes _http_basic_auth(conn, "Authorization", url->user, url->pwd); 88390267Sdes else if ((p = getenv("HTTP_AUTH")) != NULL && *p != '\0') 88497856Sdes _http_authorize(conn, "Authorization", p); 88590267Sdes else if (fetchAuthMethod && fetchAuthMethod(url) == 0) { 88697856Sdes _http_basic_auth(conn, "Authorization", url->user, url->pwd); 88790267Sdes } else { 88890267Sdes _http_seterr(HTTP_NEED_AUTH); 88990267Sdes goto ouch; 89090267Sdes } 89190267Sdes } 89290267Sdes 89390267Sdes /* other headers */ 894107372Sdes if ((p = getenv("HTTP_REFERER")) != NULL && *p != '\0') { 895107372Sdes if (strcasecmp(p, "auto") == 0) 896107372Sdes _http_cmd(conn, "Referer: %s://%s%s", 897107372Sdes url->scheme, host, url->doc); 898107372Sdes else 899107372Sdes _http_cmd(conn, "Referer: %s", p); 900107372Sdes } 90190267Sdes if ((p = getenv("HTTP_USER_AGENT")) != NULL && *p != '\0') 90297856Sdes _http_cmd(conn, "User-Agent: %s", p); 90390267Sdes else 90497856Sdes _http_cmd(conn, "User-Agent: %s " _LIBFETCH_VER, getprogname()); 905109693Sdes if (url->offset > 0) 90697856Sdes _http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset); 90797856Sdes _http_cmd(conn, "Connection: close"); 90897856Sdes _http_cmd(conn, ""); 90990267Sdes 91090267Sdes /* get reply */ 91197856Sdes switch (_http_get_reply(conn)) { 91290267Sdes case HTTP_OK: 91390267Sdes case HTTP_PARTIAL: 91490267Sdes /* fine */ 91590267Sdes break; 91690267Sdes case HTTP_MOVED_PERM: 91790267Sdes case HTTP_MOVED_TEMP: 91890267Sdes case HTTP_SEE_OTHER: 91990267Sdes /* 920125695Sdes * Not so fine, but we still have to read the 921125695Sdes * headers to get the new location. 92290267Sdes */ 92390267Sdes break; 92490267Sdes case HTTP_NEED_AUTH: 92590267Sdes if (need_auth) { 92690267Sdes /* 927125695Sdes * We already sent out authorization code, 928125695Sdes * so there's nothing more we can do. 92990267Sdes */ 93097856Sdes _http_seterr(conn->err); 93190267Sdes goto ouch; 93290267Sdes } 93390267Sdes /* try again, but send the password this time */ 93490267Sdes if (verbose) 93590267Sdes _fetch_info("server requires authorization"); 93690267Sdes break; 93790267Sdes case HTTP_NEED_PROXY_AUTH: 93890267Sdes /* 939125695Sdes * If we're talking to a proxy, we already sent 940125695Sdes * our proxy authorization code, so there's 941125695Sdes * nothing more we can do. 94290267Sdes */ 94397856Sdes _http_seterr(conn->err); 94490267Sdes goto ouch; 945125696Sdes case HTTP_BAD_RANGE: 946125696Sdes /* 947125696Sdes * This can happen if we ask for 0 bytes because 948125696Sdes * we already have the whole file. Consider this 949125696Sdes * a success for now, and check sizes later. 950125696Sdes */ 951125696Sdes break; 95290267Sdes case HTTP_PROTOCOL_ERROR: 95390267Sdes /* fall through */ 95490267Sdes case -1: 95590267Sdes _fetch_syserr(); 95690267Sdes goto ouch; 95790267Sdes default: 95897856Sdes _http_seterr(conn->err); 95990267Sdes if (!verbose) 96090267Sdes goto ouch; 96190267Sdes /* fall through so we can get the full error message */ 96290267Sdes } 96390267Sdes 96490267Sdes /* get headers */ 96590267Sdes do { 96697856Sdes switch ((h = _http_next_header(conn, &p))) { 96790267Sdes case hdr_syserror: 96890267Sdes _fetch_syserr(); 96990267Sdes goto ouch; 97090267Sdes case hdr_error: 97190267Sdes _http_seterr(HTTP_PROTOCOL_ERROR); 97290267Sdes goto ouch; 97390267Sdes case hdr_content_length: 97490267Sdes _http_parse_length(p, &clength); 97590267Sdes break; 97690267Sdes case hdr_content_range: 97790267Sdes _http_parse_range(p, &offset, &length, &size); 97890267Sdes break; 97990267Sdes case hdr_last_modified: 98090267Sdes _http_parse_mtime(p, &mtime); 98190267Sdes break; 98290267Sdes case hdr_location: 98397856Sdes if (!HTTP_REDIRECT(conn->err)) 98490267Sdes break; 98590267Sdes if (new) 98690267Sdes free(new); 98790267Sdes if (verbose) 98897856Sdes _fetch_info("%d redirect to %s", conn->err, p); 98990267Sdes if (*p == '/') 99090267Sdes /* absolute path */ 99190267Sdes new = fetchMakeURL(url->scheme, url->host, url->port, p, 99290267Sdes url->user, url->pwd); 99390267Sdes else 99490267Sdes new = fetchParseURL(p); 99590267Sdes if (new == NULL) { 99690267Sdes /* XXX should set an error code */ 99790267Sdes DEBUG(fprintf(stderr, "failed to parse new URL\n")); 99890267Sdes goto ouch; 99990267Sdes } 100090267Sdes if (!*new->user && !*new->pwd) { 100190267Sdes strcpy(new->user, url->user); 100290267Sdes strcpy(new->pwd, url->pwd); 100390267Sdes } 100490267Sdes new->offset = url->offset; 100590267Sdes new->length = url->length; 100690267Sdes break; 100790267Sdes case hdr_transfer_encoding: 100890267Sdes /* XXX weak test*/ 100990267Sdes chunked = (strcasecmp(p, "chunked") == 0); 101090267Sdes break; 101190267Sdes case hdr_www_authenticate: 101297856Sdes if (conn->err != HTTP_NEED_AUTH) 101390267Sdes break; 101490267Sdes /* if we were smarter, we'd check the method and realm */ 101590267Sdes break; 101690267Sdes case hdr_end: 101790267Sdes /* fall through */ 101890267Sdes case hdr_unknown: 101990267Sdes /* ignore */ 102090267Sdes break; 102190267Sdes } 102290267Sdes } while (h > hdr_end); 102390267Sdes 102490267Sdes /* we need to provide authentication */ 102597856Sdes if (conn->err == HTTP_NEED_AUTH) { 102698422Sdes e = conn->err; 102790267Sdes need_auth = 1; 102897856Sdes _fetch_close(conn); 102997856Sdes conn = NULL; 103090267Sdes continue; 103190267Sdes } 103290267Sdes 1033125696Sdes /* requested range not satisfiable */ 1034125696Sdes if (conn->err == HTTP_BAD_RANGE) { 1035125696Sdes if (url->offset == size && url->length == 0) { 1036125696Sdes /* asked for 0 bytes; fake it */ 1037125696Sdes offset = url->offset; 1038125696Sdes conn->err = HTTP_OK; 1039125696Sdes break; 1040125696Sdes } else { 1041125697Sdes _http_seterr(conn->err); 1042125696Sdes goto ouch; 1043125696Sdes } 1044125696Sdes } 1045125696Sdes 1046104404Sru /* we have a hit or an error */ 1047104404Sru if (conn->err == HTTP_OK || conn->err == HTTP_PARTIAL || HTTP_ERROR(conn->err)) 1048104404Sru break; 1049104404Sru 105090267Sdes /* all other cases: we got a redirect */ 105198422Sdes e = conn->err; 105290267Sdes need_auth = 0; 105397856Sdes _fetch_close(conn); 105497856Sdes conn = NULL; 105590267Sdes if (!new) { 105690267Sdes DEBUG(fprintf(stderr, "redirect with no new location\n")); 105790267Sdes break; 105890267Sdes } 105990267Sdes if (url != URL) 106090267Sdes fetchFreeURL(url); 106190267Sdes url = new; 106290267Sdes } while (++i < n); 106390267Sdes 106490267Sdes /* we failed, or ran out of retries */ 106597856Sdes if (conn == NULL) { 106698422Sdes _http_seterr(e); 106763012Sdes goto ouch; 106863012Sdes } 106960376Sdes 107090267Sdes DEBUG(fprintf(stderr, "offset %lld, length %lld," 107190267Sdes " size %lld, clength %lld\n", 107290267Sdes (long long)offset, (long long)length, 107390267Sdes (long long)size, (long long)clength)); 107460376Sdes 107590267Sdes /* check for inconsistencies */ 107690267Sdes if (clength != -1 && length != -1 && clength != length) { 107790267Sdes _http_seterr(HTTP_PROTOCOL_ERROR); 107863012Sdes goto ouch; 107963012Sdes } 108090267Sdes if (clength == -1) 108190267Sdes clength = length; 108290267Sdes if (clength != -1) 108390267Sdes length = offset + clength; 108490267Sdes if (length != -1 && size != -1 && length != size) { 108563012Sdes _http_seterr(HTTP_PROTOCOL_ERROR); 108663012Sdes goto ouch; 108790267Sdes } 108890267Sdes if (size == -1) 108990267Sdes size = length; 109060376Sdes 109190267Sdes /* fill in stats */ 109290267Sdes if (us) { 109390267Sdes us->size = size; 109490267Sdes us->atime = us->mtime = mtime; 109590267Sdes } 109663069Sdes 109790267Sdes /* too far? */ 1098109693Sdes if (URL->offset > 0 && offset > URL->offset) { 109990267Sdes _http_seterr(HTTP_PROTOCOL_ERROR); 110090267Sdes goto ouch; 110177238Sdes } 110260376Sdes 110390267Sdes /* report back real offset and size */ 110490267Sdes URL->offset = offset; 110590267Sdes URL->length = clength; 110637535Sdes 110790267Sdes /* wrap it up in a FILE */ 110897866Sdes if ((f = _http_funopen(conn, chunked)) == NULL) { 110990267Sdes _fetch_syserr(); 111090267Sdes goto ouch; 111190267Sdes } 111263716Sdes 111390267Sdes if (url != URL) 111490267Sdes fetchFreeURL(url); 111590267Sdes if (purl) 111690267Sdes fetchFreeURL(purl); 111763567Sdes 111897856Sdes if (HTTP_ERROR(conn->err)) { 111990267Sdes _http_print_html(stderr, f); 112090267Sdes fclose(f); 112190267Sdes f = NULL; 112290267Sdes } 112363012Sdes 112490267Sdes return (f); 112588771Sdes 112690267Sdesouch: 112790267Sdes if (url != URL) 112890267Sdes fetchFreeURL(url); 112990267Sdes if (purl) 113090267Sdes fetchFreeURL(purl); 113197856Sdes if (conn != NULL) 113297856Sdes _fetch_close(conn); 113390267Sdes return (NULL); 113463012Sdes} 113560189Sdes 113690267Sdes 113763012Sdes/***************************************************************************** 113863012Sdes * Entry points 113963012Sdes */ 114063012Sdes 114163012Sdes/* 114263340Sdes * Retrieve and stat a file by HTTP 114363340Sdes */ 114463340SdesFILE * 114575891SarchiefetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags) 114663340Sdes{ 1147112081Sdes return (_http_request(URL, "GET", us, _http_get_proxy(flags), flags)); 114863340Sdes} 114963340Sdes 115063340Sdes/* 115163012Sdes * Retrieve a file by HTTP 115263012Sdes */ 115363012SdesFILE * 115475891SarchiefetchGetHTTP(struct url *URL, const char *flags) 115563012Sdes{ 115690267Sdes return (fetchXGetHTTP(URL, NULL, flags)); 115737535Sdes} 115837535Sdes 115963340Sdes/* 116063340Sdes * Store a file by HTTP 116163340Sdes */ 116237535SdesFILE * 116385093SdesfetchPutHTTP(struct url *URL __unused, const char *flags __unused) 116437535Sdes{ 116590267Sdes warnx("fetchPutHTTP(): not implemented"); 116690267Sdes return (NULL); 116737535Sdes} 116840975Sdes 116940975Sdes/* 117040975Sdes * Get an HTTP document's metadata 117140975Sdes */ 117240975Sdesint 117375891SarchiefetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags) 117440975Sdes{ 117590267Sdes FILE *f; 117690267Sdes 1177112081Sdes f = _http_request(URL, "HEAD", us, _http_get_proxy(flags), flags); 1178112081Sdes if (f == NULL) 117990267Sdes return (-1); 118090267Sdes fclose(f); 118190267Sdes return (0); 118240975Sdes} 118341989Sdes 118441989Sdes/* 118541989Sdes * List a directory 118641989Sdes */ 118741989Sdesstruct url_ent * 118885093SdesfetchListHTTP(struct url *url __unused, const char *flags __unused) 118941989Sdes{ 119090267Sdes warnx("fetchListHTTP(): not implemented"); 119190267Sdes return (NULL); 119241989Sdes} 1193