http.c revision 125695
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 125695 2004-02-11 09:23:35Z 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 307109967Sdes 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 473109967Sdes 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 * 699112081Sdes_http_get_proxy(const char *flags) 70067043Sdes{ 70190267Sdes struct url *purl; 70290267Sdes char *p; 70390267Sdes 704112797Sdes if (flags != NULL && strchr(flags, 'd') != NULL) 705112081Sdes return (NULL); 70690267Sdes if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) && 70790267Sdes (purl = fetchParseURL(p))) { 70890267Sdes if (!*purl->scheme) 70990267Sdes strcpy(purl->scheme, SCHEME_HTTP); 71090267Sdes if (!purl->port) 71190267Sdes purl->port = _fetch_default_proxy_port(purl->scheme); 71290267Sdes if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0) 71390267Sdes return (purl); 71490267Sdes fetchFreeURL(purl); 71590267Sdes } 71690267Sdes return (NULL); 71760376Sdes} 71860376Sdes 71988771Sdesstatic void 72088771Sdes_http_print_html(FILE *out, FILE *in) 72188771Sdes{ 72290267Sdes size_t len; 72390267Sdes char *line, *p, *q; 72490267Sdes int comment, tag; 72588771Sdes 72690267Sdes comment = tag = 0; 72790267Sdes while ((line = fgetln(in, &len)) != NULL) { 72890267Sdes while (len && isspace(line[len - 1])) 72990267Sdes --len; 73090267Sdes for (p = q = line; q < line + len; ++q) { 73190267Sdes if (comment && *q == '-') { 73290267Sdes if (q + 2 < line + len && 73390267Sdes strcmp(q, "-->") == 0) { 73490267Sdes tag = comment = 0; 73590267Sdes q += 2; 73690267Sdes } 73790267Sdes } else if (tag && !comment && *q == '>') { 73890267Sdes p = q + 1; 73990267Sdes tag = 0; 74090267Sdes } else if (!tag && *q == '<') { 74190267Sdes if (q > p) 74290267Sdes fwrite(p, q - p, 1, out); 74390267Sdes tag = 1; 74490267Sdes if (q + 3 < line + len && 74590267Sdes strcmp(q, "<!--") == 0) { 74690267Sdes comment = 1; 74790267Sdes q += 3; 74890267Sdes } 74990267Sdes } 75088771Sdes } 75190267Sdes if (!tag && q > p) 75290267Sdes fwrite(p, q - p, 1, out); 75390267Sdes fputc('\n', out); 75488771Sdes } 75588771Sdes} 75688771Sdes 75790267Sdes 75863012Sdes/***************************************************************************** 75963012Sdes * Core 76060954Sdes */ 76160954Sdes 76260954Sdes/* 76363012Sdes * Send a request and process the reply 76497866Sdes * 76597866Sdes * XXX This function is way too long, the do..while loop should be split 76697866Sdes * XXX off into a separate function. 76760376Sdes */ 76867043SdesFILE * 76975891Sarchie_http_request(struct url *URL, const char *op, struct url_stat *us, 77090267Sdes struct url *purl, const char *flags) 77160376Sdes{ 77297856Sdes conn_t *conn; 77390267Sdes struct url *url, *new; 77490267Sdes int chunked, direct, need_auth, noredirect, verbose; 77598422Sdes int e, i, n; 77690267Sdes off_t offset, clength, length, size; 77790267Sdes time_t mtime; 77890267Sdes const char *p; 77990267Sdes FILE *f; 78090267Sdes hdr_t h; 781107372Sdes char hbuf[MAXHOSTNAMELEN + 7], *host; 78263012Sdes 78390267Sdes direct = CHECK_FLAG('d'); 78490267Sdes noredirect = CHECK_FLAG('A'); 78590267Sdes verbose = CHECK_FLAG('v'); 78660737Sume 78790267Sdes if (direct && purl) { 78890267Sdes fetchFreeURL(purl); 78990267Sdes purl = NULL; 79090267Sdes } 79163716Sdes 79290267Sdes /* try the provided URL first */ 79390267Sdes url = URL; 79463012Sdes 79590267Sdes /* if the A flag is set, we only get one try */ 79690267Sdes n = noredirect ? 1 : MAX_REDIRECT; 79790267Sdes i = 0; 79863012Sdes 79998422Sdes e = HTTP_PROTOCOL_ERROR; 80090267Sdes need_auth = 0; 80190267Sdes do { 80290267Sdes new = NULL; 80390267Sdes chunked = 0; 80490267Sdes offset = 0; 80590267Sdes clength = -1; 80690267Sdes length = -1; 80790267Sdes size = -1; 80890267Sdes mtime = 0; 80990267Sdes 81090267Sdes /* check port */ 81190267Sdes if (!url->port) 81290267Sdes url->port = _fetch_default_port(url->scheme); 81390267Sdes 81490267Sdes /* were we redirected to an FTP URL? */ 81590267Sdes if (purl == NULL && strcmp(url->scheme, SCHEME_FTP) == 0) { 81690267Sdes if (strcmp(op, "GET") == 0) 81790267Sdes return (_ftp_request(url, "RETR", us, purl, flags)); 81890267Sdes else if (strcmp(op, "HEAD") == 0) 81990267Sdes return (_ftp_request(url, "STAT", us, purl, flags)); 82090267Sdes } 82190267Sdes 82290267Sdes /* connect to server or proxy */ 82397856Sdes if ((conn = _http_connect(url, purl, flags)) == NULL) 82490267Sdes goto ouch; 82590267Sdes 82690267Sdes host = url->host; 82760737Sume#ifdef INET6 82890267Sdes if (strchr(url->host, ':')) { 82990267Sdes snprintf(hbuf, sizeof(hbuf), "[%s]", url->host); 83090267Sdes host = hbuf; 83190267Sdes } 83260737Sume#endif 833107372Sdes if (url->port != _fetch_default_port(url->scheme)) { 834107372Sdes if (host != hbuf) { 835107372Sdes strcpy(hbuf, host); 836107372Sdes host = hbuf; 837107372Sdes } 838107372Sdes snprintf(hbuf + strlen(hbuf), 839107372Sdes sizeof(hbuf) - strlen(hbuf), ":%d", url->port); 840107372Sdes } 84137535Sdes 84290267Sdes /* send request */ 84390267Sdes if (verbose) 844107372Sdes _fetch_info("requesting %s://%s%s", 845107372Sdes url->scheme, host, url->doc); 84690267Sdes if (purl) { 847107372Sdes _http_cmd(conn, "%s %s://%s%s HTTP/1.1", 848107372Sdes op, url->scheme, host, url->doc); 84990267Sdes } else { 85097856Sdes _http_cmd(conn, "%s %s HTTP/1.1", 85190267Sdes op, url->doc); 85290267Sdes } 85337535Sdes 85490267Sdes /* virtual host */ 855107372Sdes _http_cmd(conn, "Host: %s", host); 85690267Sdes 85790267Sdes /* proxy authorization */ 85890267Sdes if (purl) { 85990267Sdes if (*purl->user || *purl->pwd) 86097856Sdes _http_basic_auth(conn, "Proxy-Authorization", 86190267Sdes purl->user, purl->pwd); 86290267Sdes else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL && *p != '\0') 86397856Sdes _http_authorize(conn, "Proxy-Authorization", p); 86490267Sdes } 86590267Sdes 86690267Sdes /* server authorization */ 86790267Sdes if (need_auth || *url->user || *url->pwd) { 86890267Sdes if (*url->user || *url->pwd) 86997856Sdes _http_basic_auth(conn, "Authorization", url->user, url->pwd); 87090267Sdes else if ((p = getenv("HTTP_AUTH")) != NULL && *p != '\0') 87197856Sdes _http_authorize(conn, "Authorization", p); 87290267Sdes else if (fetchAuthMethod && fetchAuthMethod(url) == 0) { 87397856Sdes _http_basic_auth(conn, "Authorization", url->user, url->pwd); 87490267Sdes } else { 87590267Sdes _http_seterr(HTTP_NEED_AUTH); 87690267Sdes goto ouch; 87790267Sdes } 87890267Sdes } 87990267Sdes 88090267Sdes /* other headers */ 881107372Sdes if ((p = getenv("HTTP_REFERER")) != NULL && *p != '\0') { 882107372Sdes if (strcasecmp(p, "auto") == 0) 883107372Sdes _http_cmd(conn, "Referer: %s://%s%s", 884107372Sdes url->scheme, host, url->doc); 885107372Sdes else 886107372Sdes _http_cmd(conn, "Referer: %s", p); 887107372Sdes } 88890267Sdes if ((p = getenv("HTTP_USER_AGENT")) != NULL && *p != '\0') 88997856Sdes _http_cmd(conn, "User-Agent: %s", p); 89090267Sdes else 89197856Sdes _http_cmd(conn, "User-Agent: %s " _LIBFETCH_VER, getprogname()); 892109693Sdes if (url->offset > 0) 89397856Sdes _http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset); 89497856Sdes _http_cmd(conn, "Connection: close"); 89597856Sdes _http_cmd(conn, ""); 89690267Sdes 89790267Sdes /* get reply */ 89897856Sdes switch (_http_get_reply(conn)) { 89990267Sdes case HTTP_OK: 90090267Sdes case HTTP_PARTIAL: 90190267Sdes /* fine */ 90290267Sdes break; 90390267Sdes case HTTP_MOVED_PERM: 90490267Sdes case HTTP_MOVED_TEMP: 90590267Sdes case HTTP_SEE_OTHER: 90690267Sdes /* 907125695Sdes * Not so fine, but we still have to read the 908125695Sdes * headers to get the new location. 90990267Sdes */ 91090267Sdes break; 91190267Sdes case HTTP_NEED_AUTH: 91290267Sdes if (need_auth) { 91390267Sdes /* 914125695Sdes * We already sent out authorization code, 915125695Sdes * so there's nothing more we can do. 91690267Sdes */ 91797856Sdes _http_seterr(conn->err); 91890267Sdes goto ouch; 91990267Sdes } 92090267Sdes /* try again, but send the password this time */ 92190267Sdes if (verbose) 92290267Sdes _fetch_info("server requires authorization"); 92390267Sdes break; 92490267Sdes case HTTP_NEED_PROXY_AUTH: 92590267Sdes /* 926125695Sdes * If we're talking to a proxy, we already sent 927125695Sdes * our proxy authorization code, so there's 928125695Sdes * nothing more we can do. 92990267Sdes */ 93097856Sdes _http_seterr(conn->err); 93190267Sdes goto ouch; 93290267Sdes case HTTP_PROTOCOL_ERROR: 93390267Sdes /* fall through */ 93490267Sdes case -1: 93590267Sdes _fetch_syserr(); 93690267Sdes goto ouch; 93790267Sdes default: 93897856Sdes _http_seterr(conn->err); 93990267Sdes if (!verbose) 94090267Sdes goto ouch; 94190267Sdes /* fall through so we can get the full error message */ 94290267Sdes } 94390267Sdes 94490267Sdes /* get headers */ 94590267Sdes do { 94697856Sdes switch ((h = _http_next_header(conn, &p))) { 94790267Sdes case hdr_syserror: 94890267Sdes _fetch_syserr(); 94990267Sdes goto ouch; 95090267Sdes case hdr_error: 95190267Sdes _http_seterr(HTTP_PROTOCOL_ERROR); 95290267Sdes goto ouch; 95390267Sdes case hdr_content_length: 95490267Sdes _http_parse_length(p, &clength); 95590267Sdes break; 95690267Sdes case hdr_content_range: 95790267Sdes _http_parse_range(p, &offset, &length, &size); 95890267Sdes break; 95990267Sdes case hdr_last_modified: 96090267Sdes _http_parse_mtime(p, &mtime); 96190267Sdes break; 96290267Sdes case hdr_location: 96397856Sdes if (!HTTP_REDIRECT(conn->err)) 96490267Sdes break; 96590267Sdes if (new) 96690267Sdes free(new); 96790267Sdes if (verbose) 96897856Sdes _fetch_info("%d redirect to %s", conn->err, p); 96990267Sdes if (*p == '/') 97090267Sdes /* absolute path */ 97190267Sdes new = fetchMakeURL(url->scheme, url->host, url->port, p, 97290267Sdes url->user, url->pwd); 97390267Sdes else 97490267Sdes new = fetchParseURL(p); 97590267Sdes if (new == NULL) { 97690267Sdes /* XXX should set an error code */ 97790267Sdes DEBUG(fprintf(stderr, "failed to parse new URL\n")); 97890267Sdes goto ouch; 97990267Sdes } 98090267Sdes if (!*new->user && !*new->pwd) { 98190267Sdes strcpy(new->user, url->user); 98290267Sdes strcpy(new->pwd, url->pwd); 98390267Sdes } 98490267Sdes new->offset = url->offset; 98590267Sdes new->length = url->length; 98690267Sdes break; 98790267Sdes case hdr_transfer_encoding: 98890267Sdes /* XXX weak test*/ 98990267Sdes chunked = (strcasecmp(p, "chunked") == 0); 99090267Sdes break; 99190267Sdes case hdr_www_authenticate: 99297856Sdes if (conn->err != HTTP_NEED_AUTH) 99390267Sdes break; 99490267Sdes /* if we were smarter, we'd check the method and realm */ 99590267Sdes break; 99690267Sdes case hdr_end: 99790267Sdes /* fall through */ 99890267Sdes case hdr_unknown: 99990267Sdes /* ignore */ 100090267Sdes break; 100190267Sdes } 100290267Sdes } while (h > hdr_end); 100390267Sdes 100490267Sdes /* we need to provide authentication */ 100597856Sdes if (conn->err == HTTP_NEED_AUTH) { 100698422Sdes e = conn->err; 100790267Sdes need_auth = 1; 100897856Sdes _fetch_close(conn); 100997856Sdes conn = NULL; 101090267Sdes continue; 101190267Sdes } 101290267Sdes 1013104404Sru /* we have a hit or an error */ 1014104404Sru if (conn->err == HTTP_OK || conn->err == HTTP_PARTIAL || HTTP_ERROR(conn->err)) 1015104404Sru break; 1016104404Sru 101790267Sdes /* all other cases: we got a redirect */ 101898422Sdes e = conn->err; 101990267Sdes need_auth = 0; 102097856Sdes _fetch_close(conn); 102197856Sdes conn = NULL; 102290267Sdes if (!new) { 102390267Sdes DEBUG(fprintf(stderr, "redirect with no new location\n")); 102490267Sdes break; 102590267Sdes } 102690267Sdes if (url != URL) 102790267Sdes fetchFreeURL(url); 102890267Sdes url = new; 102990267Sdes } while (++i < n); 103090267Sdes 103190267Sdes /* we failed, or ran out of retries */ 103297856Sdes if (conn == NULL) { 103398422Sdes _http_seterr(e); 103463012Sdes goto ouch; 103563012Sdes } 103660376Sdes 103790267Sdes DEBUG(fprintf(stderr, "offset %lld, length %lld," 103890267Sdes " size %lld, clength %lld\n", 103990267Sdes (long long)offset, (long long)length, 104090267Sdes (long long)size, (long long)clength)); 104160376Sdes 104290267Sdes /* check for inconsistencies */ 104390267Sdes if (clength != -1 && length != -1 && clength != length) { 104490267Sdes _http_seterr(HTTP_PROTOCOL_ERROR); 104563012Sdes goto ouch; 104663012Sdes } 104790267Sdes if (clength == -1) 104890267Sdes clength = length; 104990267Sdes if (clength != -1) 105090267Sdes length = offset + clength; 105190267Sdes if (length != -1 && size != -1 && length != size) { 105263012Sdes _http_seterr(HTTP_PROTOCOL_ERROR); 105363012Sdes goto ouch; 105490267Sdes } 105590267Sdes if (size == -1) 105690267Sdes size = length; 105760376Sdes 105890267Sdes /* fill in stats */ 105990267Sdes if (us) { 106090267Sdes us->size = size; 106190267Sdes us->atime = us->mtime = mtime; 106290267Sdes } 106363069Sdes 106490267Sdes /* too far? */ 1065109693Sdes if (URL->offset > 0 && offset > URL->offset) { 106690267Sdes _http_seterr(HTTP_PROTOCOL_ERROR); 106790267Sdes goto ouch; 106877238Sdes } 106960376Sdes 107090267Sdes /* report back real offset and size */ 107190267Sdes URL->offset = offset; 107290267Sdes URL->length = clength; 107337535Sdes 107490267Sdes /* wrap it up in a FILE */ 107597866Sdes if ((f = _http_funopen(conn, chunked)) == NULL) { 107690267Sdes _fetch_syserr(); 107790267Sdes goto ouch; 107890267Sdes } 107963716Sdes 108090267Sdes if (url != URL) 108190267Sdes fetchFreeURL(url); 108290267Sdes if (purl) 108390267Sdes fetchFreeURL(purl); 108463567Sdes 108597856Sdes if (HTTP_ERROR(conn->err)) { 108690267Sdes _http_print_html(stderr, f); 108790267Sdes fclose(f); 108890267Sdes f = NULL; 108990267Sdes } 109063012Sdes 109190267Sdes return (f); 109288771Sdes 109390267Sdesouch: 109490267Sdes if (url != URL) 109590267Sdes fetchFreeURL(url); 109690267Sdes if (purl) 109790267Sdes fetchFreeURL(purl); 109897856Sdes if (conn != NULL) 109997856Sdes _fetch_close(conn); 110090267Sdes return (NULL); 110163012Sdes} 110260189Sdes 110390267Sdes 110463012Sdes/***************************************************************************** 110563012Sdes * Entry points 110663012Sdes */ 110763012Sdes 110863012Sdes/* 110963340Sdes * Retrieve and stat a file by HTTP 111063340Sdes */ 111163340SdesFILE * 111275891SarchiefetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags) 111363340Sdes{ 1114112081Sdes return (_http_request(URL, "GET", us, _http_get_proxy(flags), flags)); 111563340Sdes} 111663340Sdes 111763340Sdes/* 111863012Sdes * Retrieve a file by HTTP 111963012Sdes */ 112063012SdesFILE * 112175891SarchiefetchGetHTTP(struct url *URL, const char *flags) 112263012Sdes{ 112390267Sdes return (fetchXGetHTTP(URL, NULL, flags)); 112437535Sdes} 112537535Sdes 112663340Sdes/* 112763340Sdes * Store a file by HTTP 112863340Sdes */ 112937535SdesFILE * 113085093SdesfetchPutHTTP(struct url *URL __unused, const char *flags __unused) 113137535Sdes{ 113290267Sdes warnx("fetchPutHTTP(): not implemented"); 113390267Sdes return (NULL); 113437535Sdes} 113540975Sdes 113640975Sdes/* 113740975Sdes * Get an HTTP document's metadata 113840975Sdes */ 113940975Sdesint 114075891SarchiefetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags) 114140975Sdes{ 114290267Sdes FILE *f; 114390267Sdes 1144112081Sdes f = _http_request(URL, "HEAD", us, _http_get_proxy(flags), flags); 1145112081Sdes if (f == NULL) 114690267Sdes return (-1); 114790267Sdes fclose(f); 114890267Sdes return (0); 114940975Sdes} 115041989Sdes 115141989Sdes/* 115241989Sdes * List a directory 115341989Sdes */ 115441989Sdesstruct url_ent * 115585093SdesfetchListHTTP(struct url *url __unused, const char *flags __unused) 115641989Sdes{ 115790267Sdes warnx("fetchListHTTP(): not implemented"); 115890267Sdes return (NULL); 115941989Sdes} 1160