2 * Copyright (c) 2000-2004 Dag-Erling Coïdan Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/lib/libfetch/http.c,v 1.86 2008/12/15 08:27:44 murray Exp $
29 * $DragonFly: src/lib/libfetch/http.c,v 1.4 2007/08/05 21:48:12 swildner Exp $
33 * The following copyright applies to the base64 code:
36 * Copyright 1997 Massachusetts Institute of Technology
38 * Permission to use, copy, modify, and distribute this software and
39 * its documentation for any purpose and without fee is hereby
40 * granted, provided that both the above copyright notice and this
41 * permission notice appear in all copies, that both the above
42 * copyright notice and this permission notice appear in all
43 * supporting documentation, and that the name of M.I.T. not be used
44 * in advertising or publicity pertaining to distribution of the
45 * software without specific, written prior permission. M.I.T. makes
46 * no representations about the suitability of this software for any
47 * purpose. It is provided "as is" without express or implied
50 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
51 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
52 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
53 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
54 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
57 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
58 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
59 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
60 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 #include <sys/param.h>
65 #include <sys/socket.h>
80 #include <netinet/in.h>
81 #include <netinet/tcp.h>
87 /* Maximum number of redirects to follow */
88 #define MAX_REDIRECT 20
90 /* Symbolic names for reply codes we care about */
92 #define HTTP_PARTIAL 206
93 #define HTTP_MOVED_PERM 301
94 #define HTTP_MOVED_TEMP 302
95 #define HTTP_SEE_OTHER 303
96 #define HTTP_NOT_MODIFIED 304
97 #define HTTP_TEMP_REDIRECT 307
98 #define HTTP_NEED_AUTH 401
99 #define HTTP_NEED_PROXY_AUTH 407
100 #define HTTP_BAD_RANGE 416
101 #define HTTP_PROTOCOL_ERROR 999
103 #define HTTP_REDIRECT(xyz) ((xyz) == HTTP_MOVED_PERM \
104 || (xyz) == HTTP_MOVED_TEMP \
105 || (xyz) == HTTP_TEMP_REDIRECT \
106 || (xyz) == HTTP_SEE_OTHER)
108 #define HTTP_ERROR(xyz) ((xyz) > 400 && (xyz) < 599)
111 /*****************************************************************************
112 * I/O functions for decoding chunked streams
117 conn_t *conn; /* connection */
118 int chunked; /* chunked mode */
119 char *buf; /* chunk buffer */
120 size_t bufsize; /* size of chunk buffer */
121 ssize_t buflen; /* amount of data currently in buffer */
122 int bufpos; /* current read offset in buffer */
123 int eof; /* end-of-file flag */
124 int error; /* error flag */
125 size_t chunksize; /* remaining size of current chunk */
131 static int http_cmd(conn_t *, const char *, ...) __printflike(2, 3);
134 * Get next chunk header
137 http_new_chunk(struct httpio *io)
141 if (fetch_getln(io->conn) == -1)
144 if (io->conn->buflen < 2 || !isxdigit((unsigned char)*io->conn->buf))
147 for (p = io->conn->buf; *p && !isspace((unsigned char)*p); ++p) {
150 if (!isxdigit((unsigned char)*p))
152 if (isdigit((unsigned char)*p)) {
153 io->chunksize = io->chunksize * 16 +
156 io->chunksize = io->chunksize * 16 +
157 10 + tolower((unsigned char)*p) - 'a';
163 io->total += io->chunksize;
164 if (io->chunksize == 0)
165 fprintf(stderr, "%s(): end of last chunk\n", __func__);
167 fprintf(stderr, "%s(): new chunk: %lu (%lu)\n",
168 __func__, (unsigned long)io->chunksize,
169 (unsigned long)io->total);
173 return (io->chunksize);
177 * Grow the input buffer to at least len bytes
180 http_growbuf(struct httpio *io, size_t len)
184 if (io->bufsize >= len)
187 if ((tmp = realloc(io->buf, len)) == NULL)
195 * Fill the input buffer, do chunk decoding on the fly
198 http_fillbuf(struct httpio *io, size_t len)
207 if (io->chunked == 0) {
208 if (http_growbuf(io, len) == -1)
210 if ((nbytes = fetch_read(io->conn, io->buf, len)) == -1) {
219 if (io->chunksize == 0) {
220 switch (http_new_chunk(io)) {
230 if (len > io->chunksize)
232 if (http_growbuf(io, len) == -1)
234 if ((nbytes = fetch_read(io->conn, io->buf, len)) == -1) {
239 io->chunksize -= io->buflen;
241 if (io->chunksize == 0) {
244 if (fetch_read(io->conn, endl, 2) != 2 ||
245 endl[0] != '\r' || endl[1] != '\n')
258 http_readfn(void *v, char *buf, int len)
260 struct httpio *io = (struct httpio *)v;
268 for (pos = 0; len > 0; pos += l, len -= l) {
270 if (!io->buf || io->bufpos == io->buflen)
271 if (http_fillbuf(io, len) < 1)
273 l = io->buflen - io->bufpos;
276 memcpy(buf + pos, io->buf + io->bufpos, l);
280 if (!pos && io->error) {
281 if (io->error == EINTR)
292 http_writefn(void *v, const char *buf, int len)
294 struct httpio *io = (struct httpio *)v;
296 return (fetch_write(io->conn, buf, len));
303 http_closefn(void *v)
305 struct httpio *io = (struct httpio *)v;
308 r = fetch_close(io->conn);
316 * Wrap a file descriptor up
319 http_funopen(conn_t *conn, int chunked)
324 if ((io = calloc(1, sizeof(*io))) == NULL) {
329 io->chunked = chunked;
330 f = funopen(io, http_readfn, http_writefn, NULL, http_closefn);
340 /*****************************************************************************
341 * Helper functions for talking to the server and parsing its replies
354 hdr_transfer_encoding,
358 /* Names of interesting headers */
363 { hdr_content_length, "Content-Length" },
364 { hdr_content_range, "Content-Range" },
365 { hdr_last_modified, "Last-Modified" },
366 { hdr_location, "Location" },
367 { hdr_transfer_encoding, "Transfer-Encoding" },
368 { hdr_www_authenticate, "WWW-Authenticate" },
369 { hdr_unknown, NULL },
373 * Send a formatted line; optionally echo to terminal
376 http_cmd(conn_t *conn, const char *fmt, ...)
384 len = vasprintf(&msg, fmt, ap);
393 r = fetch_putln(conn, msg, len);
405 * Get and parse status line
408 http_get_reply(conn_t *conn)
412 if (fetch_getln(conn) == -1)
415 * A valid status line looks like "HTTP/m.n xyz reason" where m
416 * and n are the major and minor protocol version numbers and xyz
418 * Unfortunately, there are servers out there (NCSA 1.5.1, to name
419 * just one) that do not send a version number, so we can't rely
420 * on finding one, but if we do, insist on it being 1.0 or 1.1.
421 * We don't care about the reason phrase.
423 if (strncmp(conn->buf, "HTTP", 4) != 0)
424 return (HTTP_PROTOCOL_ERROR);
427 if (p[1] != '1' || p[2] != '.' || (p[3] != '0' && p[3] != '1'))
428 return (HTTP_PROTOCOL_ERROR);
432 !isdigit((unsigned char)p[1]) ||
433 !isdigit((unsigned char)p[2]) ||
434 !isdigit((unsigned char)p[3]))
435 return (HTTP_PROTOCOL_ERROR);
437 conn->err = (p[1] - '0') * 100 + (p[2] - '0') * 10 + (p[3] - '0');
442 * Check a header; if the type matches the given string, return a pointer
443 * to the beginning of the value.
446 http_match(const char *str, const char *hdr)
448 while (*str && *hdr &&
449 tolower((unsigned char)*str++) == tolower((unsigned char)*hdr++))
451 if (*str || *hdr != ':')
453 while (*hdr && isspace((unsigned char)*++hdr))
459 * Get the next header and return the appropriate symbolic code.
462 http_next_header(conn_t *conn, const char **p)
466 if (fetch_getln(conn) == -1)
467 return (hdr_syserror);
468 while (conn->buflen && isspace((unsigned char)conn->buf[conn->buflen - 1]))
470 conn->buf[conn->buflen] = '\0';
471 if (conn->buflen == 0)
474 * We could check for malformed headers but we don't really care.
475 * A valid header starts with a token immediately followed by a
476 * colon; a token is any sequence of non-control, non-whitespace
477 * characters except "()<>@,;:\\\"{}".
479 for (i = 0; hdr_names[i].num != hdr_unknown; i++)
480 if ((*p = http_match(hdr_names[i].name, conn->buf)) != NULL)
481 return (hdr_names[i].num);
482 return (hdr_unknown);
486 * Parse a last-modified header
489 http_parse_mtime(const char *p, time_t *mtime)
494 strncpy(locale, setlocale(LC_TIME, NULL), sizeof(locale));
495 setlocale(LC_TIME, "C");
496 r = strptime(p, "%a, %d %b %Y %H:%M:%S GMT", &tm);
497 /* XXX should add support for date-2 and date-3 */
498 setlocale(LC_TIME, locale);
501 DEBUG(fprintf(stderr, "last modified: [%04d-%02d-%02d "
503 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
504 tm.tm_hour, tm.tm_min, tm.tm_sec));
505 *mtime = timegm(&tm);
510 * Parse a content-length header
513 http_parse_length(const char *p, off_t *length)
517 for (len = 0; *p && isdigit((unsigned char)*p); ++p)
518 len = len * 10 + (*p - '0');
521 DEBUG(fprintf(stderr, "content length: [%lld]\n",
528 * Parse a content-range header
531 http_parse_range(const char *p, off_t *offset, off_t *length, off_t *size)
533 off_t first, last, len;
535 if (strncasecmp(p, "bytes ", 6) != 0)
542 for (first = 0; *p && isdigit((unsigned char)*p); ++p)
543 first = first * 10 + *p - '0';
546 for (last = 0, ++p; *p && isdigit((unsigned char)*p); ++p)
547 last = last * 10 + *p - '0';
549 if (first > last || *p != '/')
551 for (len = 0, ++p; *p && isdigit((unsigned char)*p); ++p)
552 len = len * 10 + *p - '0';
553 if (*p || len < last - first + 1)
556 DEBUG(fprintf(stderr, "content range: [*/%lld]\n",
560 DEBUG(fprintf(stderr, "content range: [%lld-%lld/%lld]\n",
561 (long long)first, (long long)last, (long long)len));
562 *length = last - first + 1;
570 /*****************************************************************************
571 * Helper functions for authorization
578 http_base64(const char *src)
580 static const char base64[] =
581 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
582 "abcdefghijklmnopqrstuvwxyz"
589 if ((str = malloc(((l + 2) / 3) * 4 + 1)) == NULL)
595 t = (src[0] << 16) | (src[1] << 8) | src[2];
596 dst[0] = base64[(t >> 18) & 0x3f];
597 dst[1] = base64[(t >> 12) & 0x3f];
598 dst[2] = base64[(t >> 6) & 0x3f];
599 dst[3] = base64[(t >> 0) & 0x3f];
606 t = (src[0] << 16) | (src[1] << 8);
607 dst[0] = base64[(t >> 18) & 0x3f];
608 dst[1] = base64[(t >> 12) & 0x3f];
609 dst[2] = base64[(t >> 6) & 0x3f];
616 dst[0] = base64[(t >> 18) & 0x3f];
617 dst[1] = base64[(t >> 12) & 0x3f];
618 dst[2] = dst[3] = '=';
631 * Encode username and password
634 http_basic_auth(conn_t *conn, const char *hdr, const char *usr, const char *pwd)
639 DEBUG(fprintf(stderr, "usr: [%s]\n", usr));
640 DEBUG(fprintf(stderr, "pwd: [%s]\n", pwd));
641 if (asprintf(&upw, "%s:%s", usr, pwd) == -1)
643 auth = http_base64(upw);
647 r = http_cmd(conn, "%s: Basic %s", hdr, auth);
653 * Send an authorization header
656 http_authorize(conn_t *conn, const char *hdr, const char *p)
658 /* basic authorization */
659 if (strncasecmp(p, "basic:", 6) == 0) {
660 char *user, *pwd, *str;
664 for (p += 6; *p && *p != ':'; ++p)
666 if (!*p || strchr(++p, ':') == NULL)
668 if ((str = strdup(p)) == NULL)
669 return (-1); /* XXX */
671 pwd = strchr(str, ':');
673 r = http_basic_auth(conn, hdr, user, pwd);
681 /*****************************************************************************
682 * Helper functions for connecting to a server or proxy
686 * Connect to the correct HTTP server or proxy.
689 http_connect(struct url *URL, struct url *purl, const char *flags)
701 verbose = CHECK_FLAG('v');
705 else if (CHECK_FLAG('6'))
709 if (purl && strcasecmp(URL->scheme, SCHEME_HTTPS) != 0) {
711 } else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0) {
712 /* can't talk http to an ftp server */
713 /* XXX should set an error code */
717 if ((conn = fetch_connect(URL->host, URL->port, af, verbose)) == NULL)
718 /* fetch_connect() has already set an error code */
720 if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0 &&
721 fetch_ssl(conn, verbose) == -1) {
730 setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val, sizeof(val));
736 http_get_proxy(struct url * url, const char *flags)
741 if (flags != NULL && strchr(flags, 'd') != NULL)
743 if (fetch_no_proxy_match(url->host))
745 if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
746 *p && (purl = fetchParseURL(p))) {
748 strcpy(purl->scheme, SCHEME_HTTP);
750 purl->port = fetch_default_proxy_port(purl->scheme);
751 if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
759 http_print_html(FILE *out, FILE *in)
766 while ((line = fgetln(in, &len)) != NULL) {
767 while (len && isspace((unsigned char)line[len - 1]))
769 for (p = q = line; q < line + len; ++q) {
770 if (comment && *q == '-') {
771 if (q + 2 < line + len &&
772 strcmp(q, "-->") == 0) {
776 } else if (tag && !comment && *q == '>') {
779 } else if (!tag && *q == '<') {
781 fwrite(p, q - p, 1, out);
783 if (q + 3 < line + len &&
784 strcmp(q, "<!--") == 0) {
791 fwrite(p, q - p, 1, out);
797 /*****************************************************************************
802 * Send a request and process the reply
804 * XXX This function is way too long, the do..while loop should be split
805 * XXX off into a separate function.
808 http_request(struct url *URL, const char *op, struct url_stat *us,
809 struct url *purl, const char *flags)
812 char hbuf[MAXHOSTNAMELEN + 7], *host;
814 struct url *url, *new;
815 int chunked, direct, ims, need_auth, noredirect, verbose;
817 off_t offset, clength, length, size;
822 struct tm *timestruct;
824 direct = CHECK_FLAG('d');
825 noredirect = CHECK_FLAG('A');
826 verbose = CHECK_FLAG('v');
827 ims = CHECK_FLAG('i');
829 if (direct && purl) {
834 /* try the provided URL first */
837 /* if the A flag is set, we only get one try */
838 n = noredirect ? 1 : MAX_REDIRECT;
841 e = HTTP_PROTOCOL_ERROR;
854 url->port = fetch_default_port(url->scheme);
856 /* were we redirected to an FTP URL? */
857 if (purl == NULL && strcmp(url->scheme, SCHEME_FTP) == 0) {
858 if (strcmp(op, "GET") == 0)
859 return (ftp_request(url, "RETR", us, purl, flags));
860 else if (strcmp(op, "HEAD") == 0)
861 return (ftp_request(url, "STAT", us, purl, flags));
864 /* connect to server or proxy */
865 if ((conn = http_connect(url, purl, flags)) == NULL)
870 if (strchr(url->host, ':')) {
871 snprintf(hbuf, sizeof(hbuf), "[%s]", url->host);
875 if (url->port != fetch_default_port(url->scheme)) {
880 snprintf(hbuf + strlen(hbuf),
881 sizeof(hbuf) - strlen(hbuf), ":%d", url->port);
886 fetch_info("requesting %s://%s%s",
887 url->scheme, host, url->doc);
889 http_cmd(conn, "%s %s://%s%s HTTP/1.1",
890 op, url->scheme, host, url->doc);
892 http_cmd(conn, "%s %s HTTP/1.1",
896 if (ims && url->ims_time) {
897 timestruct = gmtime((time_t *)&url->ims_time);
898 (void)strftime(timebuf, 80, "%a, %d %b %Y %T GMT",
901 fetch_info("If-Modified-Since: %s", timebuf);
902 http_cmd(conn, "If-Modified-Since: %s", timebuf);
905 http_cmd(conn, "Host: %s", host);
907 /* proxy authorization */
909 if (*purl->user || *purl->pwd)
910 http_basic_auth(conn, "Proxy-Authorization",
911 purl->user, purl->pwd);
912 else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL && *p != '\0')
913 http_authorize(conn, "Proxy-Authorization", p);
916 /* server authorization */
917 if (need_auth || *url->user || *url->pwd) {
918 if (*url->user || *url->pwd)
919 http_basic_auth(conn, "Authorization", url->user, url->pwd);
920 else if ((p = getenv("HTTP_AUTH")) != NULL && *p != '\0')
921 http_authorize(conn, "Authorization", p);
922 else if (fetchAuthMethod && fetchAuthMethod(url) == 0) {
923 http_basic_auth(conn, "Authorization", url->user, url->pwd);
925 http_seterr(HTTP_NEED_AUTH);
931 if ((p = getenv("HTTP_REFERER")) != NULL && *p != '\0') {
932 if (strcasecmp(p, "auto") == 0)
933 http_cmd(conn, "Referer: %s://%s%s",
934 url->scheme, host, url->doc);
936 http_cmd(conn, "Referer: %s", p);
938 if ((p = getenv("HTTP_USER_AGENT")) != NULL && *p != '\0')
939 http_cmd(conn, "User-Agent: %s", p);
941 http_cmd(conn, "User-Agent: %s " _LIBFETCH_VER, getprogname());
943 http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset);
944 http_cmd(conn, "Connection: close");
945 http_cmd(conn, "%s", "");
948 * Force the queued request to be dispatched. Normally, one
949 * would do this with shutdown(2) but squid proxies can be
950 * configured to disallow such half-closed connections. To
951 * be compatible with such configurations, fiddle with socket
952 * options to force the pending data to be written.
955 setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val,
958 setsockopt(conn->sd, IPPROTO_TCP, TCP_NODELAY, &val,
962 switch (http_get_reply(conn)) {
965 case HTTP_NOT_MODIFIED:
968 case HTTP_MOVED_PERM:
969 case HTTP_MOVED_TEMP:
972 * Not so fine, but we still have to read the
973 * headers to get the new location.
979 * We already sent out authorization code,
980 * so there's nothing more we can do.
982 http_seterr(conn->err);
985 /* try again, but send the password this time */
987 fetch_info("server requires authorization");
989 case HTTP_NEED_PROXY_AUTH:
991 * If we're talking to a proxy, we already sent
992 * our proxy authorization code, so there's
993 * nothing more we can do.
995 http_seterr(conn->err);
999 * This can happen if we ask for 0 bytes because
1000 * we already have the whole file. Consider this
1001 * a success for now, and check sizes later.
1004 case HTTP_PROTOCOL_ERROR:
1010 http_seterr(conn->err);
1013 /* fall through so we can get the full error message */
1018 switch ((h = http_next_header(conn, &p))) {
1023 http_seterr(HTTP_PROTOCOL_ERROR);
1025 case hdr_content_length:
1026 http_parse_length(p, &clength);
1028 case hdr_content_range:
1029 http_parse_range(p, &offset, &length, &size);
1031 case hdr_last_modified:
1032 http_parse_mtime(p, &mtime);
1035 if (!HTTP_REDIRECT(conn->err))
1040 fetch_info("%d redirect to %s", conn->err, p);
1043 new = fetchMakeURL(url->scheme, url->host, url->port, p,
1044 url->user, url->pwd);
1046 new = fetchParseURL(p);
1048 /* XXX should set an error code */
1049 DEBUG(fprintf(stderr, "failed to parse new URL\n"));
1052 if (!*new->user && !*new->pwd) {
1053 strcpy(new->user, url->user);
1054 strcpy(new->pwd, url->pwd);
1056 new->offset = url->offset;
1057 new->length = url->length;
1059 case hdr_transfer_encoding:
1061 chunked = (strcasecmp(p, "chunked") == 0);
1063 case hdr_www_authenticate:
1064 if (conn->err != HTTP_NEED_AUTH)
1066 /* if we were smarter, we'd check the method and realm */
1074 } while (h > hdr_end);
1076 /* we need to provide authentication */
1077 if (conn->err == HTTP_NEED_AUTH) {
1085 /* requested range not satisfiable */
1086 if (conn->err == HTTP_BAD_RANGE) {
1087 if (url->offset == size && url->length == 0) {
1088 /* asked for 0 bytes; fake it */
1089 offset = url->offset;
1091 conn->err = HTTP_OK;
1094 http_seterr(conn->err);
1099 /* we have a hit or an error */
1100 if (conn->err == HTTP_OK
1101 || conn->err == HTTP_NOT_MODIFIED
1102 || conn->err == HTTP_PARTIAL
1103 || HTTP_ERROR(conn->err))
1106 /* all other cases: we got a redirect */
1112 DEBUG(fprintf(stderr, "redirect with no new location\n"));
1120 /* we failed, or ran out of retries */
1126 DEBUG(fprintf(stderr, "offset %lld, length %lld,"
1127 " size %lld, clength %lld\n",
1128 (long long)offset, (long long)length,
1129 (long long)size, (long long)clength));
1131 if (conn->err == HTTP_NOT_MODIFIED) {
1132 http_seterr(HTTP_NOT_MODIFIED);
1136 /* check for inconsistencies */
1137 if (clength != -1 && length != -1 && clength != length) {
1138 http_seterr(HTTP_PROTOCOL_ERROR);
1144 length = offset + clength;
1145 if (length != -1 && size != -1 && length != size) {
1146 http_seterr(HTTP_PROTOCOL_ERROR);
1155 us->atime = us->mtime = mtime;
1159 if (URL->offset > 0 && offset > URL->offset) {
1160 http_seterr(HTTP_PROTOCOL_ERROR);
1164 /* report back real offset and size */
1165 URL->offset = offset;
1166 URL->length = clength;
1168 /* wrap it up in a FILE */
1169 if ((f = http_funopen(conn, chunked)) == NULL) {
1179 if (HTTP_ERROR(conn->err)) {
1180 http_print_html(stderr, f);
1198 /*****************************************************************************
1203 * Retrieve and stat a file by HTTP
1206 fetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags)
1208 return (http_request(URL, "GET", us, http_get_proxy(URL, flags), flags));
1212 * Retrieve a file by HTTP
1215 fetchGetHTTP(struct url *URL, const char *flags)
1217 return (fetchXGetHTTP(URL, NULL, flags));
1221 * Store a file by HTTP
1224 fetchPutHTTP(struct url *URL __unused, const char *flags __unused)
1226 warnx("fetchPutHTTP(): not implemented");
1231 * Get an HTTP document's metadata
1234 fetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags)
1238 f = http_request(URL, "HEAD", us, http_get_proxy(URL, flags), flags);
1249 fetchListHTTP(struct url *url __unused, const char *flags __unused)
1251 warnx("fetchListHTTP(): not implemented");