http.c revision 106207
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 106207 2002-10-30 15:01:29Z 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 9463012Sdes#define HTTP_PROTOCOL_ERROR 999 9560196Sdes 9663012Sdes#define HTTP_REDIRECT(xyz) ((xyz) == HTTP_MOVED_PERM \ 9790267Sdes || (xyz) == HTTP_MOVED_TEMP \ 9890267Sdes || (xyz) == HTTP_SEE_OTHER) 9963012Sdes 10088771Sdes#define HTTP_ERROR(xyz) ((xyz) > 400 && (xyz) < 599) 10163012Sdes 10290267Sdes 10363012Sdes/***************************************************************************** 10463012Sdes * I/O functions for decoding chunked streams 10563012Sdes */ 10663012Sdes 10797859Sdesstruct httpio 10837535Sdes{ 10997858Sdes conn_t *conn; /* connection */ 11097866Sdes int chunked; /* chunked mode */ 11197858Sdes char *buf; /* chunk buffer */ 11297866Sdes size_t bufsize; /* size of chunk buffer */ 11397866Sdes ssize_t buflen; /* amount of data currently in buffer */ 11497866Sdes int bufpos; /* current read offset in buffer */ 11597858Sdes int eof; /* end-of-file flag */ 11697858Sdes int error; /* error flag */ 11797858Sdes size_t chunksize; /* remaining size of current chunk */ 11863281Sdes#ifndef NDEBUG 11990267Sdes size_t total; 12063012Sdes#endif 12137535Sdes}; 12237535Sdes 12337608Sdes/* 12463012Sdes * Get next chunk header 12537608Sdes */ 12637608Sdesstatic int 12797859Sdes_http_new_chunk(struct httpio *io) 12837608Sdes{ 12990267Sdes char *p; 13090267Sdes 13197859Sdes if (_fetch_getln(io->conn) == -1) 13290267Sdes return (-1); 13390267Sdes 13497859Sdes if (io->conn->buflen < 2 || !ishexnumber(*io->conn->buf)) 13590267Sdes return (-1); 13690267Sdes 13797859Sdes for (p = io->conn->buf; *p && !isspace(*p); ++p) { 13890267Sdes if (*p == ';') 13990267Sdes break; 14090267Sdes if (!ishexnumber(*p)) 14190267Sdes return (-1); 14290267Sdes if (isdigit(*p)) { 14397859Sdes io->chunksize = io->chunksize * 16 + 14490267Sdes *p - '0'; 14590267Sdes } else { 14697859Sdes io->chunksize = io->chunksize * 16 + 14790267Sdes 10 + tolower(*p) - 'a'; 14890267Sdes } 14990267Sdes } 15090267Sdes 15163281Sdes#ifndef NDEBUG 15290267Sdes if (fetchDebug) { 15397859Sdes io->total += io->chunksize; 15497859Sdes if (io->chunksize == 0) 155106207Sdes fprintf(stderr, "%s(): end of last chunk\n", __func__); 15690267Sdes else 157106207Sdes fprintf(stderr, "%s(): new chunk: %lu (%lu)\n", 158106207Sdes __func__, (unsigned long)io->chunksize, 159106207Sdes (unsigned long)io->total); 16090267Sdes } 16163012Sdes#endif 16290267Sdes 16397859Sdes return (io->chunksize); 16437608Sdes} 16537608Sdes 16637608Sdes/* 16797866Sdes * Grow the input buffer to at least len bytes 16897866Sdes */ 16997866Sdesstatic inline int 17097866Sdes_http_growbuf(struct httpio *io, size_t len) 17197866Sdes{ 17297866Sdes char *tmp; 17397866Sdes 17497866Sdes if (io->bufsize >= len) 17597866Sdes return (0); 17697866Sdes 17797866Sdes if ((tmp = realloc(io->buf, len)) == NULL) 17897866Sdes return (-1); 17997866Sdes io->buf = tmp; 18097866Sdes io->bufsize = len; 181106044Sdes return (0); 18297866Sdes} 18397866Sdes 18497866Sdes/* 18537608Sdes * Fill the input buffer, do chunk decoding on the fly 18637608Sdes */ 18763012Sdesstatic int 18897866Sdes_http_fillbuf(struct httpio *io, size_t len) 18937535Sdes{ 19097859Sdes if (io->error) 19190267Sdes return (-1); 19297859Sdes if (io->eof) 19390267Sdes return (0); 19490267Sdes 19597866Sdes if (io->chunked == 0) { 19697866Sdes if (_http_growbuf(io, len) == -1) 19797866Sdes return (-1); 198106185Sdes if ((io->buflen = _fetch_read(io->conn, io->buf, len)) == -1) { 199106185Sdes io->error = 1; 20097866Sdes return (-1); 201106185Sdes } 20297866Sdes io->bufpos = 0; 20397866Sdes return (io->buflen); 20497866Sdes } 20597866Sdes 20697859Sdes if (io->chunksize == 0) { 20797859Sdes switch (_http_new_chunk(io)) { 20890267Sdes case -1: 20997859Sdes io->error = 1; 21090267Sdes return (-1); 21190267Sdes case 0: 21297859Sdes io->eof = 1; 21390267Sdes return (0); 21490267Sdes } 21537535Sdes } 21663012Sdes 21797866Sdes if (len > io->chunksize) 21897866Sdes len = io->chunksize; 21997866Sdes if (_http_growbuf(io, len) == -1) 22090267Sdes return (-1); 221106185Sdes if ((io->buflen = _fetch_read(io->conn, io->buf, len)) == -1) { 222106185Sdes io->error = 1; 22397866Sdes return (-1); 224106185Sdes } 22597866Sdes io->chunksize -= io->buflen; 22690267Sdes 22797859Sdes if (io->chunksize == 0) { 22897856Sdes char endl[2]; 22997856Sdes 23097866Sdes if (_fetch_read(io->conn, endl, 2) != 2 || 23197856Sdes endl[0] != '\r' || endl[1] != '\n') 23290267Sdes return (-1); 23390267Sdes } 23490267Sdes 23597866Sdes io->bufpos = 0; 23690267Sdes 23797866Sdes return (io->buflen); 23837535Sdes} 23937535Sdes 24037608Sdes/* 24137608Sdes * Read function 24237608Sdes */ 24337535Sdesstatic int 24463012Sdes_http_readfn(void *v, char *buf, int len) 24537535Sdes{ 24697859Sdes struct httpio *io = (struct httpio *)v; 24790267Sdes int l, pos; 24863012Sdes 24997859Sdes if (io->error) 25090267Sdes return (-1); 25197859Sdes if (io->eof) 25290267Sdes return (0); 25363012Sdes 25490267Sdes for (pos = 0; len > 0; pos += l, len -= l) { 25590267Sdes /* empty buffer */ 25697866Sdes if (!io->buf || io->bufpos == io->buflen) 25797866Sdes if (_http_fillbuf(io, len) < 1) 25890267Sdes break; 25997866Sdes l = io->buflen - io->bufpos; 26090267Sdes if (len < l) 26190267Sdes l = len; 26297866Sdes bcopy(io->buf + io->bufpos, buf + pos, l); 26397866Sdes io->bufpos += l; 26490267Sdes } 26537535Sdes 26697859Sdes if (!pos && io->error) 26790267Sdes return (-1); 26890267Sdes return (pos); 26937535Sdes} 27037535Sdes 27137608Sdes/* 27237608Sdes * Write function 27337608Sdes */ 27437535Sdesstatic int 27563012Sdes_http_writefn(void *v, const char *buf, int len) 27637535Sdes{ 27797859Sdes struct httpio *io = (struct httpio *)v; 27890267Sdes 27997866Sdes return (_fetch_write(io->conn, buf, len)); 28037535Sdes} 28137535Sdes 28237608Sdes/* 28337608Sdes * Close function 28437608Sdes */ 28537535Sdesstatic int 28663012Sdes_http_closefn(void *v) 28737535Sdes{ 28897859Sdes struct httpio *io = (struct httpio *)v; 28990267Sdes int r; 29063012Sdes 29197859Sdes r = _fetch_close(io->conn); 29297859Sdes if (io->buf) 29397859Sdes free(io->buf); 29497859Sdes free(io); 29590267Sdes return (r); 29637535Sdes} 29737535Sdes 29837608Sdes/* 29963012Sdes * Wrap a file descriptor up 30037608Sdes */ 30163012Sdesstatic FILE * 30297866Sdes_http_funopen(conn_t *conn, int chunked) 30337535Sdes{ 30497859Sdes struct httpio *io; 30590267Sdes FILE *f; 30663012Sdes 30797859Sdes if ((io = calloc(1, sizeof *io)) == NULL) { 30890267Sdes _fetch_syserr(); 30990267Sdes return (NULL); 31090267Sdes } 31197859Sdes io->conn = conn; 31297866Sdes io->chunked = chunked; 31397859Sdes f = funopen(io, _http_readfn, _http_writefn, NULL, _http_closefn); 31490267Sdes if (f == NULL) { 31590267Sdes _fetch_syserr(); 31697859Sdes free(io); 31790267Sdes return (NULL); 31890267Sdes } 31990267Sdes return (f); 32063012Sdes} 32163012Sdes 32290267Sdes 32363012Sdes/***************************************************************************** 32463012Sdes * Helper functions for talking to the server and parsing its replies 32563012Sdes */ 32663012Sdes 32763012Sdes/* Header types */ 32863012Sdestypedef enum { 32990267Sdes hdr_syserror = -2, 33090267Sdes hdr_error = -1, 33190267Sdes hdr_end = 0, 33290267Sdes hdr_unknown = 1, 33390267Sdes hdr_content_length, 33490267Sdes hdr_content_range, 33590267Sdes hdr_last_modified, 33690267Sdes hdr_location, 33790267Sdes hdr_transfer_encoding, 33890267Sdes hdr_www_authenticate 33985093Sdes} hdr_t; 34063012Sdes 34163012Sdes/* Names of interesting headers */ 34263012Sdesstatic struct { 34390267Sdes hdr_t num; 34490267Sdes const char *name; 34563012Sdes} hdr_names[] = { 34690267Sdes { hdr_content_length, "Content-Length" }, 34790267Sdes { hdr_content_range, "Content-Range" }, 34890267Sdes { hdr_last_modified, "Last-Modified" }, 34990267Sdes { hdr_location, "Location" }, 35090267Sdes { hdr_transfer_encoding, "Transfer-Encoding" }, 35190267Sdes { hdr_www_authenticate, "WWW-Authenticate" }, 35290267Sdes { hdr_unknown, NULL }, 35363012Sdes}; 35463012Sdes 35563012Sdes/* 35663012Sdes * Send a formatted line; optionally echo to terminal 35763012Sdes */ 35863012Sdesstatic int 35997856Sdes_http_cmd(conn_t *conn, const char *fmt, ...) 36063012Sdes{ 36190267Sdes va_list ap; 36290267Sdes size_t len; 36390267Sdes char *msg; 36490267Sdes int r; 36563012Sdes 36690267Sdes va_start(ap, fmt); 36790267Sdes len = vasprintf(&msg, fmt, ap); 36890267Sdes va_end(ap); 36990267Sdes 37090267Sdes if (msg == NULL) { 37190267Sdes errno = ENOMEM; 37290267Sdes _fetch_syserr(); 37390267Sdes return (-1); 37490267Sdes } 37590267Sdes 37697856Sdes r = _fetch_putln(conn, msg, len); 37790267Sdes free(msg); 37890267Sdes 37990267Sdes if (r == -1) { 38090267Sdes _fetch_syserr(); 38190267Sdes return (-1); 38290267Sdes } 38390267Sdes 38490267Sdes return (0); 38563012Sdes} 38663012Sdes 38763012Sdes/* 38863012Sdes * Get and parse status line 38963012Sdes */ 39063012Sdesstatic int 39197856Sdes_http_get_reply(conn_t *conn) 39263012Sdes{ 39390267Sdes char *p; 39490267Sdes 39597856Sdes if (_fetch_getln(conn) == -1) 39690267Sdes return (-1); 39790267Sdes /* 39890267Sdes * A valid status line looks like "HTTP/m.n xyz reason" where m 39990267Sdes * and n are the major and minor protocol version numbers and xyz 40090267Sdes * is the reply code. 40190267Sdes * Unfortunately, there are servers out there (NCSA 1.5.1, to name 40290267Sdes * just one) that do not send a version number, so we can't rely 40390267Sdes * on finding one, but if we do, insist on it being 1.0 or 1.1. 40490267Sdes * We don't care about the reason phrase. 40590267Sdes */ 40697856Sdes if (strncmp(conn->buf, "HTTP", 4) != 0) 40790267Sdes return (HTTP_PROTOCOL_ERROR); 40897856Sdes p = conn->buf + 4; 40990267Sdes if (*p == '/') { 41090267Sdes if (p[1] != '1' || p[2] != '.' || (p[3] != '0' && p[3] != '1')) 41190267Sdes return (HTTP_PROTOCOL_ERROR); 41290267Sdes p += 4; 41390267Sdes } 41490267Sdes if (*p != ' ' || !isdigit(p[1]) || !isdigit(p[2]) || !isdigit(p[3])) 41590267Sdes return (HTTP_PROTOCOL_ERROR); 41690267Sdes 41797856Sdes conn->err = (p[1] - '0') * 100 + (p[2] - '0') * 10 + (p[3] - '0'); 41897856Sdes return (conn->err); 41937535Sdes} 42037535Sdes 42137608Sdes/* 42290267Sdes * Check a header; if the type matches the given string, return a pointer 42390267Sdes * to the beginning of the value. 42463012Sdes */ 42575891Sarchiestatic const char * 42675891Sarchie_http_match(const char *str, const char *hdr) 42763012Sdes{ 42890267Sdes while (*str && *hdr && tolower(*str++) == tolower(*hdr++)) 42990267Sdes /* nothing */; 43090267Sdes if (*str || *hdr != ':') 43190267Sdes return (NULL); 43290267Sdes while (*hdr && isspace(*++hdr)) 43390267Sdes /* nothing */; 43490267Sdes return (hdr); 43563012Sdes} 43663012Sdes 43763012Sdes/* 43863012Sdes * Get the next header and return the appropriate symbolic code. 43963012Sdes */ 44085093Sdesstatic hdr_t 44197856Sdes_http_next_header(conn_t *conn, const char **p) 44263012Sdes{ 44390267Sdes int i; 44490267Sdes 44597856Sdes if (_fetch_getln(conn) == -1) 44690267Sdes return (hdr_syserror); 44797856Sdes while (conn->buflen && isspace(conn->buf[conn->buflen - 1])) 44897856Sdes conn->buflen--; 44997856Sdes conn->buf[conn->buflen] = '\0'; 45097856Sdes if (conn->buflen == 0) 45197856Sdes return (hdr_end); 45290267Sdes /* 45390267Sdes * We could check for malformed headers but we don't really care. 45490267Sdes * A valid header starts with a token immediately followed by a 45590267Sdes * colon; a token is any sequence of non-control, non-whitespace 45690267Sdes * characters except "()<>@,;:\\\"{}". 45790267Sdes */ 45890267Sdes for (i = 0; hdr_names[i].num != hdr_unknown; i++) 45997856Sdes if ((*p = _http_match(hdr_names[i].name, conn->buf)) != NULL) 46090267Sdes return (hdr_names[i].num); 46190267Sdes return (hdr_unknown); 46263012Sdes} 46363012Sdes 46463012Sdes/* 46563012Sdes * Parse a last-modified header 46663012Sdes */ 46763716Sdesstatic int 46875891Sarchie_http_parse_mtime(const char *p, time_t *mtime) 46963012Sdes{ 47090267Sdes char locale[64], *r; 47190267Sdes struct tm tm; 47263012Sdes 47390267Sdes strncpy(locale, setlocale(LC_TIME, NULL), sizeof locale); 47490267Sdes setlocale(LC_TIME, "C"); 47590267Sdes r = strptime(p, "%a, %d %b %Y %H:%M:%S GMT", &tm); 47690267Sdes /* XXX should add support for date-2 and date-3 */ 47790267Sdes setlocale(LC_TIME, locale); 47890267Sdes if (r == NULL) 47990267Sdes return (-1); 48090267Sdes DEBUG(fprintf(stderr, "last modified: [%04d-%02d-%02d " 48188769Sdes "%02d:%02d:%02d]\n", 48263012Sdes tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, 48363012Sdes tm.tm_hour, tm.tm_min, tm.tm_sec)); 48490267Sdes *mtime = timegm(&tm); 48590267Sdes return (0); 48663012Sdes} 48763012Sdes 48863012Sdes/* 48963012Sdes * Parse a content-length header 49063012Sdes */ 49163716Sdesstatic int 49275891Sarchie_http_parse_length(const char *p, off_t *length) 49363012Sdes{ 49490267Sdes off_t len; 49590267Sdes 49690267Sdes for (len = 0; *p && isdigit(*p); ++p) 49790267Sdes len = len * 10 + (*p - '0'); 49890267Sdes if (*p) 49990267Sdes return (-1); 50090267Sdes DEBUG(fprintf(stderr, "content length: [%lld]\n", 50190267Sdes (long long)len)); 50290267Sdes *length = len; 50390267Sdes return (0); 50463012Sdes} 50563012Sdes 50663012Sdes/* 50763012Sdes * Parse a content-range header 50863012Sdes */ 50963716Sdesstatic int 51075891Sarchie_http_parse_range(const char *p, off_t *offset, off_t *length, off_t *size) 51163012Sdes{ 51290267Sdes off_t first, last, len; 51363716Sdes 51490267Sdes if (strncasecmp(p, "bytes ", 6) != 0) 51590267Sdes return (-1); 51690267Sdes for (first = 0, p += 6; *p && isdigit(*p); ++p) 51790267Sdes first = first * 10 + *p - '0'; 51890267Sdes if (*p != '-') 51990267Sdes return (-1); 52090267Sdes for (last = 0, ++p; *p && isdigit(*p); ++p) 52190267Sdes last = last * 10 + *p - '0'; 52290267Sdes if (first > last || *p != '/') 52390267Sdes return (-1); 52490267Sdes for (len = 0, ++p; *p && isdigit(*p); ++p) 52590267Sdes len = len * 10 + *p - '0'; 52690267Sdes if (*p || len < last - first + 1) 52790267Sdes return (-1); 52890267Sdes DEBUG(fprintf(stderr, "content range: [%lld-%lld/%lld]\n", 52990267Sdes (long long)first, (long long)last, (long long)len)); 53090267Sdes *offset = first; 53190267Sdes *length = last - first + 1; 53290267Sdes *size = len; 53390267Sdes return (0); 53463012Sdes} 53563012Sdes 53690267Sdes 53763012Sdes/***************************************************************************** 53863012Sdes * Helper functions for authorization 53963012Sdes */ 54063012Sdes 54163012Sdes/* 54237608Sdes * Base64 encoding 54337608Sdes */ 54462965Sdesstatic char * 54590267Sdes_http_base64(const char *src) 54637608Sdes{ 54790267Sdes static const char base64[] = 54890267Sdes "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 54990267Sdes "abcdefghijklmnopqrstuvwxyz" 55090267Sdes "0123456789+/"; 55190267Sdes char *str, *dst; 55290267Sdes size_t l; 55390267Sdes int t, r; 55462965Sdes 55590267Sdes l = strlen(src); 55690267Sdes if ((str = malloc(((l + 2) / 3) * 4)) == NULL) 55790267Sdes return (NULL); 55890267Sdes dst = str; 55990267Sdes r = 0; 56037608Sdes 56190267Sdes while (l >= 3) { 56290267Sdes t = (src[0] << 16) | (src[1] << 8) | src[2]; 56390267Sdes dst[0] = base64[(t >> 18) & 0x3f]; 56490267Sdes dst[1] = base64[(t >> 12) & 0x3f]; 56590267Sdes dst[2] = base64[(t >> 6) & 0x3f]; 56690267Sdes dst[3] = base64[(t >> 0) & 0x3f]; 56790267Sdes src += 3; l -= 3; 56890267Sdes dst += 4; r += 4; 56990267Sdes } 57037608Sdes 57190267Sdes switch (l) { 57290267Sdes case 2: 57390267Sdes t = (src[0] << 16) | (src[1] << 8); 57490267Sdes dst[0] = base64[(t >> 18) & 0x3f]; 57590267Sdes dst[1] = base64[(t >> 12) & 0x3f]; 57690267Sdes dst[2] = base64[(t >> 6) & 0x3f]; 57790267Sdes dst[3] = '='; 57890267Sdes dst += 4; 57990267Sdes r += 4; 58090267Sdes break; 58190267Sdes case 1: 58290267Sdes t = src[0] << 16; 58390267Sdes dst[0] = base64[(t >> 18) & 0x3f]; 58490267Sdes dst[1] = base64[(t >> 12) & 0x3f]; 58590267Sdes dst[2] = dst[3] = '='; 58690267Sdes dst += 4; 58790267Sdes r += 4; 58890267Sdes break; 58990267Sdes case 0: 59090267Sdes break; 59190267Sdes } 59290267Sdes 59390267Sdes *dst = 0; 59490267Sdes return (str); 59537608Sdes} 59637608Sdes 59737608Sdes/* 59837608Sdes * Encode username and password 59937608Sdes */ 60062965Sdesstatic int 60197856Sdes_http_basic_auth(conn_t *conn, const char *hdr, const char *usr, const char *pwd) 60237608Sdes{ 60390267Sdes char *upw, *auth; 60490267Sdes int r; 60537608Sdes 60690267Sdes DEBUG(fprintf(stderr, "usr: [%s]\n", usr)); 60790267Sdes DEBUG(fprintf(stderr, "pwd: [%s]\n", pwd)); 60890267Sdes if (asprintf(&upw, "%s:%s", usr, pwd) == -1) 60990267Sdes return (-1); 61090267Sdes auth = _http_base64(upw); 61190267Sdes free(upw); 61290267Sdes if (auth == NULL) 61390267Sdes return (-1); 61497856Sdes r = _http_cmd(conn, "%s: Basic %s", hdr, auth); 61590267Sdes free(auth); 61690267Sdes return (r); 61762965Sdes} 61862965Sdes 61962965Sdes/* 62062965Sdes * Send an authorization header 62162965Sdes */ 62262965Sdesstatic int 62397856Sdes_http_authorize(conn_t *conn, const char *hdr, const char *p) 62462965Sdes{ 62590267Sdes /* basic authorization */ 62690267Sdes if (strncasecmp(p, "basic:", 6) == 0) { 62790267Sdes char *user, *pwd, *str; 62890267Sdes int r; 62962965Sdes 63090267Sdes /* skip realm */ 63190267Sdes for (p += 6; *p && *p != ':'; ++p) 63290267Sdes /* nothing */ ; 63390267Sdes if (!*p || strchr(++p, ':') == NULL) 63490267Sdes return (-1); 63590267Sdes if ((str = strdup(p)) == NULL) 63690267Sdes return (-1); /* XXX */ 63790267Sdes user = str; 63890267Sdes pwd = strchr(str, ':'); 63990267Sdes *pwd++ = '\0'; 64097856Sdes r = _http_basic_auth(conn, hdr, user, pwd); 64190267Sdes free(str); 64290267Sdes return (r); 64390267Sdes } 64490267Sdes return (-1); 64537608Sdes} 64637608Sdes 64790267Sdes 64863012Sdes/***************************************************************************** 64963012Sdes * Helper functions for connecting to a server or proxy 65063012Sdes */ 65163012Sdes 65237608Sdes/* 65390267Sdes * Connect to the correct HTTP server or proxy. 65463012Sdes */ 65597856Sdesstatic conn_t * 65675891Sarchie_http_connect(struct url *URL, struct url *purl, const char *flags) 65763012Sdes{ 65897856Sdes conn_t *conn; 65990267Sdes int verbose; 66097856Sdes int af; 66190267Sdes 66263012Sdes#ifdef INET6 66390267Sdes af = AF_UNSPEC; 66460737Sume#else 66590267Sdes af = AF_INET; 66660737Sume#endif 66790267Sdes 66890267Sdes verbose = CHECK_FLAG('v'); 66990267Sdes if (CHECK_FLAG('4')) 67090267Sdes af = AF_INET; 67167043Sdes#ifdef INET6 67290267Sdes else if (CHECK_FLAG('6')) 67390267Sdes af = AF_INET6; 67467043Sdes#endif 67567043Sdes 67697868Sdes if (purl && strcasecmp(URL->scheme, SCHEME_HTTPS) != 0) { 67790267Sdes URL = purl; 67890267Sdes } else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0) { 67990267Sdes /* can't talk http to an ftp server */ 68090267Sdes /* XXX should set an error code */ 68197856Sdes return (NULL); 68290267Sdes } 68390267Sdes 68497856Sdes if ((conn = _fetch_connect(URL->host, URL->port, af, verbose)) == NULL) 68590267Sdes /* _fetch_connect() has already set an error code */ 68697856Sdes return (NULL); 68797868Sdes if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0 && 68897868Sdes _fetch_ssl(conn, verbose) == -1) { 68997868Sdes _fetch_close(conn); 69097891Sdes /* grrr */ 69197891Sdes errno = EAUTH; 69297891Sdes _fetch_syserr(); 69397868Sdes return (NULL); 69497868Sdes } 69597856Sdes return (conn); 69667043Sdes} 69767043Sdes 69867043Sdesstatic struct url * 69975891Sarchie_http_get_proxy(void) 70067043Sdes{ 70190267Sdes struct url *purl; 70290267Sdes char *p; 70390267Sdes 70490267Sdes if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) && 70590267Sdes (purl = fetchParseURL(p))) { 70690267Sdes if (!*purl->scheme) 70790267Sdes strcpy(purl->scheme, SCHEME_HTTP); 70890267Sdes if (!purl->port) 70990267Sdes purl->port = _fetch_default_proxy_port(purl->scheme); 71090267Sdes if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0) 71190267Sdes return (purl); 71290267Sdes fetchFreeURL(purl); 71390267Sdes } 71490267Sdes return (NULL); 71560376Sdes} 71660376Sdes 71788771Sdesstatic void 71888771Sdes_http_print_html(FILE *out, FILE *in) 71988771Sdes{ 72090267Sdes size_t len; 72190267Sdes char *line, *p, *q; 72290267Sdes int comment, tag; 72388771Sdes 72490267Sdes comment = tag = 0; 72590267Sdes while ((line = fgetln(in, &len)) != NULL) { 72690267Sdes while (len && isspace(line[len - 1])) 72790267Sdes --len; 72890267Sdes for (p = q = line; q < line + len; ++q) { 72990267Sdes if (comment && *q == '-') { 73090267Sdes if (q + 2 < line + len && 73190267Sdes strcmp(q, "-->") == 0) { 73290267Sdes tag = comment = 0; 73390267Sdes q += 2; 73490267Sdes } 73590267Sdes } else if (tag && !comment && *q == '>') { 73690267Sdes p = q + 1; 73790267Sdes tag = 0; 73890267Sdes } else if (!tag && *q == '<') { 73990267Sdes if (q > p) 74090267Sdes fwrite(p, q - p, 1, out); 74190267Sdes tag = 1; 74290267Sdes if (q + 3 < line + len && 74390267Sdes strcmp(q, "<!--") == 0) { 74490267Sdes comment = 1; 74590267Sdes q += 3; 74690267Sdes } 74790267Sdes } 74888771Sdes } 74990267Sdes if (!tag && q > p) 75090267Sdes fwrite(p, q - p, 1, out); 75190267Sdes fputc('\n', out); 75288771Sdes } 75388771Sdes} 75488771Sdes 75590267Sdes 75663012Sdes/***************************************************************************** 75763012Sdes * Core 75860954Sdes */ 75960954Sdes 76060954Sdes/* 76163012Sdes * Send a request and process the reply 76297866Sdes * 76397866Sdes * XXX This function is way too long, the do..while loop should be split 76497866Sdes * XXX off into a separate function. 76560376Sdes */ 76667043SdesFILE * 76775891Sarchie_http_request(struct url *URL, const char *op, struct url_stat *us, 76890267Sdes struct url *purl, const char *flags) 76960376Sdes{ 77097856Sdes conn_t *conn; 77190267Sdes struct url *url, *new; 77290267Sdes int chunked, direct, need_auth, noredirect, verbose; 77398422Sdes int e, i, n; 77490267Sdes off_t offset, clength, length, size; 77590267Sdes time_t mtime; 77690267Sdes const char *p; 77790267Sdes FILE *f; 77890267Sdes hdr_t h; 77990267Sdes char *host; 78060737Sume#ifdef INET6 78190267Sdes char hbuf[MAXHOSTNAMELEN + 1]; 78260737Sume#endif 78363012Sdes 78490267Sdes direct = CHECK_FLAG('d'); 78590267Sdes noredirect = CHECK_FLAG('A'); 78690267Sdes verbose = CHECK_FLAG('v'); 78760737Sume 78890267Sdes if (direct && purl) { 78990267Sdes fetchFreeURL(purl); 79090267Sdes purl = NULL; 79190267Sdes } 79263716Sdes 79390267Sdes /* try the provided URL first */ 79490267Sdes url = URL; 79563012Sdes 79690267Sdes /* if the A flag is set, we only get one try */ 79790267Sdes n = noredirect ? 1 : MAX_REDIRECT; 79890267Sdes i = 0; 79963012Sdes 80098422Sdes e = HTTP_PROTOCOL_ERROR; 80190267Sdes need_auth = 0; 80290267Sdes do { 80390267Sdes new = NULL; 80490267Sdes chunked = 0; 80590267Sdes offset = 0; 80690267Sdes clength = -1; 80790267Sdes length = -1; 80890267Sdes size = -1; 80990267Sdes mtime = 0; 81090267Sdes 81190267Sdes /* check port */ 81290267Sdes if (!url->port) 81390267Sdes url->port = _fetch_default_port(url->scheme); 81490267Sdes 81590267Sdes /* were we redirected to an FTP URL? */ 81690267Sdes if (purl == NULL && strcmp(url->scheme, SCHEME_FTP) == 0) { 81790267Sdes if (strcmp(op, "GET") == 0) 81890267Sdes return (_ftp_request(url, "RETR", us, purl, flags)); 81990267Sdes else if (strcmp(op, "HEAD") == 0) 82090267Sdes return (_ftp_request(url, "STAT", us, purl, flags)); 82190267Sdes } 82290267Sdes 82390267Sdes /* connect to server or proxy */ 82497856Sdes if ((conn = _http_connect(url, purl, flags)) == NULL) 82590267Sdes goto ouch; 82690267Sdes 82790267Sdes host = url->host; 82860737Sume#ifdef INET6 82990267Sdes if (strchr(url->host, ':')) { 83090267Sdes snprintf(hbuf, sizeof(hbuf), "[%s]", url->host); 83190267Sdes host = hbuf; 83290267Sdes } 83360737Sume#endif 83437535Sdes 83590267Sdes /* send request */ 83690267Sdes if (verbose) 83790267Sdes _fetch_info("requesting %s://%s:%d%s", 83890267Sdes url->scheme, host, url->port, url->doc); 83990267Sdes if (purl) { 84097856Sdes _http_cmd(conn, "%s %s://%s:%d%s HTTP/1.1", 84190267Sdes op, url->scheme, host, url->port, url->doc); 84290267Sdes } else { 84397856Sdes _http_cmd(conn, "%s %s HTTP/1.1", 84490267Sdes op, url->doc); 84590267Sdes } 84637535Sdes 84790267Sdes /* virtual host */ 84890267Sdes if (url->port == _fetch_default_port(url->scheme)) 84997856Sdes _http_cmd(conn, "Host: %s", host); 85090267Sdes else 85197856Sdes _http_cmd(conn, "Host: %s:%d", host, url->port); 85290267Sdes 85390267Sdes /* proxy authorization */ 85490267Sdes if (purl) { 85590267Sdes if (*purl->user || *purl->pwd) 85697856Sdes _http_basic_auth(conn, "Proxy-Authorization", 85790267Sdes purl->user, purl->pwd); 85890267Sdes else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL && *p != '\0') 85997856Sdes _http_authorize(conn, "Proxy-Authorization", p); 86090267Sdes } 86190267Sdes 86290267Sdes /* server authorization */ 86390267Sdes if (need_auth || *url->user || *url->pwd) { 86490267Sdes if (*url->user || *url->pwd) 86597856Sdes _http_basic_auth(conn, "Authorization", url->user, url->pwd); 86690267Sdes else if ((p = getenv("HTTP_AUTH")) != NULL && *p != '\0') 86797856Sdes _http_authorize(conn, "Authorization", p); 86890267Sdes else if (fetchAuthMethod && fetchAuthMethod(url) == 0) { 86997856Sdes _http_basic_auth(conn, "Authorization", url->user, url->pwd); 87090267Sdes } else { 87190267Sdes _http_seterr(HTTP_NEED_AUTH); 87290267Sdes goto ouch; 87390267Sdes } 87490267Sdes } 87590267Sdes 87690267Sdes /* other headers */ 87790267Sdes if ((p = getenv("HTTP_USER_AGENT")) != NULL && *p != '\0') 87897856Sdes _http_cmd(conn, "User-Agent: %s", p); 87990267Sdes else 88097856Sdes _http_cmd(conn, "User-Agent: %s " _LIBFETCH_VER, getprogname()); 88190267Sdes if (url->offset) 88297856Sdes _http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset); 88397856Sdes _http_cmd(conn, "Connection: close"); 88497856Sdes _http_cmd(conn, ""); 88590267Sdes 88690267Sdes /* get reply */ 88797856Sdes switch (_http_get_reply(conn)) { 88890267Sdes case HTTP_OK: 88990267Sdes case HTTP_PARTIAL: 89090267Sdes /* fine */ 89190267Sdes break; 89290267Sdes case HTTP_MOVED_PERM: 89390267Sdes case HTTP_MOVED_TEMP: 89490267Sdes case HTTP_SEE_OTHER: 89590267Sdes /* 89690267Sdes * Not so fine, but we still have to read the headers to 89790267Sdes * get the new location. 89890267Sdes */ 89990267Sdes break; 90090267Sdes case HTTP_NEED_AUTH: 90190267Sdes if (need_auth) { 90290267Sdes /* 90390267Sdes * We already sent out authorization code, so there's 90490267Sdes * nothing more we can do. 90590267Sdes */ 90697856Sdes _http_seterr(conn->err); 90790267Sdes goto ouch; 90890267Sdes } 90990267Sdes /* try again, but send the password this time */ 91090267Sdes if (verbose) 91190267Sdes _fetch_info("server requires authorization"); 91290267Sdes break; 91390267Sdes case HTTP_NEED_PROXY_AUTH: 91490267Sdes /* 91590267Sdes * If we're talking to a proxy, we already sent our proxy 91690267Sdes * authorization code, so there's nothing more we can do. 91790267Sdes */ 91897856Sdes _http_seterr(conn->err); 91990267Sdes goto ouch; 92090267Sdes case HTTP_PROTOCOL_ERROR: 92190267Sdes /* fall through */ 92290267Sdes case -1: 92390267Sdes _fetch_syserr(); 92490267Sdes goto ouch; 92590267Sdes default: 92697856Sdes _http_seterr(conn->err); 92790267Sdes if (!verbose) 92890267Sdes goto ouch; 92990267Sdes /* fall through so we can get the full error message */ 93090267Sdes } 93190267Sdes 93290267Sdes /* get headers */ 93390267Sdes do { 93497856Sdes switch ((h = _http_next_header(conn, &p))) { 93590267Sdes case hdr_syserror: 93690267Sdes _fetch_syserr(); 93790267Sdes goto ouch; 93890267Sdes case hdr_error: 93990267Sdes _http_seterr(HTTP_PROTOCOL_ERROR); 94090267Sdes goto ouch; 94190267Sdes case hdr_content_length: 94290267Sdes _http_parse_length(p, &clength); 94390267Sdes break; 94490267Sdes case hdr_content_range: 94590267Sdes _http_parse_range(p, &offset, &length, &size); 94690267Sdes break; 94790267Sdes case hdr_last_modified: 94890267Sdes _http_parse_mtime(p, &mtime); 94990267Sdes break; 95090267Sdes case hdr_location: 95197856Sdes if (!HTTP_REDIRECT(conn->err)) 95290267Sdes break; 95390267Sdes if (new) 95490267Sdes free(new); 95590267Sdes if (verbose) 95697856Sdes _fetch_info("%d redirect to %s", conn->err, p); 95790267Sdes if (*p == '/') 95890267Sdes /* absolute path */ 95990267Sdes new = fetchMakeURL(url->scheme, url->host, url->port, p, 96090267Sdes url->user, url->pwd); 96190267Sdes else 96290267Sdes new = fetchParseURL(p); 96390267Sdes if (new == NULL) { 96490267Sdes /* XXX should set an error code */ 96590267Sdes DEBUG(fprintf(stderr, "failed to parse new URL\n")); 96690267Sdes goto ouch; 96790267Sdes } 96890267Sdes if (!*new->user && !*new->pwd) { 96990267Sdes strcpy(new->user, url->user); 97090267Sdes strcpy(new->pwd, url->pwd); 97190267Sdes } 97290267Sdes new->offset = url->offset; 97390267Sdes new->length = url->length; 97490267Sdes break; 97590267Sdes case hdr_transfer_encoding: 97690267Sdes /* XXX weak test*/ 97790267Sdes chunked = (strcasecmp(p, "chunked") == 0); 97890267Sdes break; 97990267Sdes case hdr_www_authenticate: 98097856Sdes if (conn->err != HTTP_NEED_AUTH) 98190267Sdes break; 98290267Sdes /* if we were smarter, we'd check the method and realm */ 98390267Sdes break; 98490267Sdes case hdr_end: 98590267Sdes /* fall through */ 98690267Sdes case hdr_unknown: 98790267Sdes /* ignore */ 98890267Sdes break; 98990267Sdes } 99090267Sdes } while (h > hdr_end); 99190267Sdes 99290267Sdes /* we need to provide authentication */ 99397856Sdes if (conn->err == HTTP_NEED_AUTH) { 99498422Sdes e = conn->err; 99590267Sdes need_auth = 1; 99697856Sdes _fetch_close(conn); 99797856Sdes conn = NULL; 99890267Sdes continue; 99990267Sdes } 100090267Sdes 1001104404Sru /* we have a hit or an error */ 1002104404Sru if (conn->err == HTTP_OK || conn->err == HTTP_PARTIAL || HTTP_ERROR(conn->err)) 1003104404Sru break; 1004104404Sru 100590267Sdes /* all other cases: we got a redirect */ 100698422Sdes e = conn->err; 100790267Sdes need_auth = 0; 100897856Sdes _fetch_close(conn); 100997856Sdes conn = NULL; 101090267Sdes if (!new) { 101190267Sdes DEBUG(fprintf(stderr, "redirect with no new location\n")); 101290267Sdes break; 101390267Sdes } 101490267Sdes if (url != URL) 101590267Sdes fetchFreeURL(url); 101690267Sdes url = new; 101790267Sdes } while (++i < n); 101890267Sdes 101990267Sdes /* we failed, or ran out of retries */ 102097856Sdes if (conn == NULL) { 102198422Sdes _http_seterr(e); 102263012Sdes goto ouch; 102363012Sdes } 102460376Sdes 102590267Sdes DEBUG(fprintf(stderr, "offset %lld, length %lld," 102690267Sdes " size %lld, clength %lld\n", 102790267Sdes (long long)offset, (long long)length, 102890267Sdes (long long)size, (long long)clength)); 102960376Sdes 103090267Sdes /* check for inconsistencies */ 103190267Sdes if (clength != -1 && length != -1 && clength != length) { 103290267Sdes _http_seterr(HTTP_PROTOCOL_ERROR); 103363012Sdes goto ouch; 103463012Sdes } 103590267Sdes if (clength == -1) 103690267Sdes clength = length; 103790267Sdes if (clength != -1) 103890267Sdes length = offset + clength; 103990267Sdes if (length != -1 && size != -1 && length != size) { 104063012Sdes _http_seterr(HTTP_PROTOCOL_ERROR); 104163012Sdes goto ouch; 104290267Sdes } 104390267Sdes if (size == -1) 104490267Sdes size = length; 104560376Sdes 104690267Sdes /* fill in stats */ 104790267Sdes if (us) { 104890267Sdes us->size = size; 104990267Sdes us->atime = us->mtime = mtime; 105090267Sdes } 105163069Sdes 105290267Sdes /* too far? */ 105390267Sdes if (offset > URL->offset) { 105490267Sdes _http_seterr(HTTP_PROTOCOL_ERROR); 105590267Sdes goto ouch; 105677238Sdes } 105760376Sdes 105890267Sdes /* report back real offset and size */ 105990267Sdes URL->offset = offset; 106090267Sdes URL->length = clength; 106137535Sdes 106290267Sdes /* wrap it up in a FILE */ 106397866Sdes if ((f = _http_funopen(conn, chunked)) == NULL) { 106490267Sdes _fetch_syserr(); 106590267Sdes goto ouch; 106690267Sdes } 106763716Sdes 106890267Sdes if (url != URL) 106990267Sdes fetchFreeURL(url); 107090267Sdes if (purl) 107190267Sdes fetchFreeURL(purl); 107263567Sdes 107397856Sdes if (HTTP_ERROR(conn->err)) { 107490267Sdes _http_print_html(stderr, f); 107590267Sdes fclose(f); 107690267Sdes f = NULL; 107790267Sdes } 107863012Sdes 107990267Sdes return (f); 108088771Sdes 108190267Sdesouch: 108290267Sdes if (url != URL) 108390267Sdes fetchFreeURL(url); 108490267Sdes if (purl) 108590267Sdes fetchFreeURL(purl); 108697856Sdes if (conn != NULL) 108797856Sdes _fetch_close(conn); 108890267Sdes return (NULL); 108963012Sdes} 109060189Sdes 109190267Sdes 109263012Sdes/***************************************************************************** 109363012Sdes * Entry points 109463012Sdes */ 109563012Sdes 109663012Sdes/* 109763340Sdes * Retrieve and stat a file by HTTP 109863340Sdes */ 109963340SdesFILE * 110075891SarchiefetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags) 110163340Sdes{ 110290267Sdes return (_http_request(URL, "GET", us, _http_get_proxy(), flags)); 110363340Sdes} 110463340Sdes 110563340Sdes/* 110663012Sdes * Retrieve a file by HTTP 110763012Sdes */ 110863012SdesFILE * 110975891SarchiefetchGetHTTP(struct url *URL, const char *flags) 111063012Sdes{ 111190267Sdes return (fetchXGetHTTP(URL, NULL, flags)); 111237535Sdes} 111337535Sdes 111463340Sdes/* 111563340Sdes * Store a file by HTTP 111663340Sdes */ 111737535SdesFILE * 111885093SdesfetchPutHTTP(struct url *URL __unused, const char *flags __unused) 111937535Sdes{ 112090267Sdes warnx("fetchPutHTTP(): not implemented"); 112190267Sdes return (NULL); 112237535Sdes} 112340975Sdes 112440975Sdes/* 112540975Sdes * Get an HTTP document's metadata 112640975Sdes */ 112740975Sdesint 112875891SarchiefetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags) 112940975Sdes{ 113090267Sdes FILE *f; 113190267Sdes 113290267Sdes if ((f = _http_request(URL, "HEAD", us, _http_get_proxy(), flags)) == NULL) 113390267Sdes return (-1); 113490267Sdes fclose(f); 113590267Sdes return (0); 113640975Sdes} 113741989Sdes 113841989Sdes/* 113941989Sdes * List a directory 114041989Sdes */ 114141989Sdesstruct url_ent * 114285093SdesfetchListHTTP(struct url *url __unused, const char *flags __unused) 114341989Sdes{ 114490267Sdes warnx("fetchListHTTP(): not implemented"); 114590267Sdes return (NULL); 114641989Sdes} 1147