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 5
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)
205 if (io->chunked == 0) {
206 if (http_growbuf(io, len) == -1)
208 if ((io->buflen = fetch_read(io->conn, io->buf, len)) == -1) {
216 if (io->chunksize == 0) {
217 switch (http_new_chunk(io)) {
227 if (len > io->chunksize)
229 if (http_growbuf(io, len) == -1)
231 if ((io->buflen = fetch_read(io->conn, io->buf, len)) == -1) {
235 io->chunksize -= io->buflen;
237 if (io->chunksize == 0) {
240 if (fetch_read(io->conn, endl, 2) != 2 ||
241 endl[0] != '\r' || endl[1] != '\n')
254 http_readfn(void *v, char *buf, int len)
256 struct httpio *io = (struct httpio *)v;
264 for (pos = 0; len > 0; pos += l, len -= l) {
266 if (!io->buf || io->bufpos == io->buflen)
267 if (http_fillbuf(io, len) < 1)
269 l = io->buflen - io->bufpos;
272 memcpy(buf + pos, io->buf + io->bufpos, l);
276 if (!pos && io->error)
285 http_writefn(void *v, const char *buf, int len)
287 struct httpio *io = (struct httpio *)v;
289 return (fetch_write(io->conn, buf, len));
296 http_closefn(void *v)
298 struct httpio *io = (struct httpio *)v;
301 r = fetch_close(io->conn);
309 * Wrap a file descriptor up
312 http_funopen(conn_t *conn, int chunked)
317 if ((io = calloc(1, sizeof(*io))) == NULL) {
322 io->chunked = chunked;
323 f = funopen(io, http_readfn, http_writefn, NULL, http_closefn);
333 /*****************************************************************************
334 * Helper functions for talking to the server and parsing its replies
347 hdr_transfer_encoding,
351 /* Names of interesting headers */
356 { hdr_content_length, "Content-Length" },
357 { hdr_content_range, "Content-Range" },
358 { hdr_last_modified, "Last-Modified" },
359 { hdr_location, "Location" },
360 { hdr_transfer_encoding, "Transfer-Encoding" },
361 { hdr_www_authenticate, "WWW-Authenticate" },
362 { hdr_unknown, NULL },
366 * Send a formatted line; optionally echo to terminal
369 http_cmd(conn_t *conn, const char *fmt, ...)
377 len = vasprintf(&msg, fmt, ap);
386 r = fetch_putln(conn, msg, len);
398 * Get and parse status line
401 http_get_reply(conn_t *conn)
405 if (fetch_getln(conn) == -1)
408 * A valid status line looks like "HTTP/m.n xyz reason" where m
409 * and n are the major and minor protocol version numbers and xyz
411 * Unfortunately, there are servers out there (NCSA 1.5.1, to name
412 * just one) that do not send a version number, so we can't rely
413 * on finding one, but if we do, insist on it being 1.0 or 1.1.
414 * We don't care about the reason phrase.
416 if (strncmp(conn->buf, "HTTP", 4) != 0)
417 return (HTTP_PROTOCOL_ERROR);
420 if (p[1] != '1' || p[2] != '.' || (p[3] != '0' && p[3] != '1'))
421 return (HTTP_PROTOCOL_ERROR);
425 !isdigit((unsigned char)p[1]) ||
426 !isdigit((unsigned char)p[2]) ||
427 !isdigit((unsigned char)p[3]))
428 return (HTTP_PROTOCOL_ERROR);
430 conn->err = (p[1] - '0') * 100 + (p[2] - '0') * 10 + (p[3] - '0');
435 * Check a header; if the type matches the given string, return a pointer
436 * to the beginning of the value.
439 http_match(const char *str, const char *hdr)
441 while (*str && *hdr &&
442 tolower((unsigned char)*str++) == tolower((unsigned char)*hdr++))
444 if (*str || *hdr != ':')
446 while (*hdr && isspace((unsigned char)*++hdr))
452 * Get the next header and return the appropriate symbolic code.
455 http_next_header(conn_t *conn, const char **p)
459 if (fetch_getln(conn) == -1)
460 return (hdr_syserror);
461 while (conn->buflen && isspace((unsigned char)conn->buf[conn->buflen - 1]))
463 conn->buf[conn->buflen] = '\0';
464 if (conn->buflen == 0)
467 * We could check for malformed headers but we don't really care.
468 * A valid header starts with a token immediately followed by a
469 * colon; a token is any sequence of non-control, non-whitespace
470 * characters except "()<>@,;:\\\"{}".
472 for (i = 0; hdr_names[i].num != hdr_unknown; i++)
473 if ((*p = http_match(hdr_names[i].name, conn->buf)) != NULL)
474 return (hdr_names[i].num);
475 return (hdr_unknown);
479 * Parse a last-modified header
482 http_parse_mtime(const char *p, time_t *mtime)
487 strncpy(locale, setlocale(LC_TIME, NULL), sizeof(locale));
488 setlocale(LC_TIME, "C");
489 r = strptime(p, "%a, %d %b %Y %H:%M:%S GMT", &tm);
490 /* XXX should add support for date-2 and date-3 */
491 setlocale(LC_TIME, locale);
494 DEBUG(fprintf(stderr, "last modified: [%04d-%02d-%02d "
496 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
497 tm.tm_hour, tm.tm_min, tm.tm_sec));
498 *mtime = timegm(&tm);
503 * Parse a content-length header
506 http_parse_length(const char *p, off_t *length)
510 for (len = 0; *p && isdigit((unsigned char)*p); ++p)
511 len = len * 10 + (*p - '0');
514 DEBUG(fprintf(stderr, "content length: [%lld]\n",
521 * Parse a content-range header
524 http_parse_range(const char *p, off_t *offset, off_t *length, off_t *size)
526 off_t first, last, len;
528 if (strncasecmp(p, "bytes ", 6) != 0)
535 for (first = 0; *p && isdigit((unsigned char)*p); ++p)
536 first = first * 10 + *p - '0';
539 for (last = 0, ++p; *p && isdigit((unsigned char)*p); ++p)
540 last = last * 10 + *p - '0';
542 if (first > last || *p != '/')
544 for (len = 0, ++p; *p && isdigit((unsigned char)*p); ++p)
545 len = len * 10 + *p - '0';
546 if (*p || len < last - first + 1)
549 DEBUG(fprintf(stderr, "content range: [*/%lld]\n",
553 DEBUG(fprintf(stderr, "content range: [%lld-%lld/%lld]\n",
554 (long long)first, (long long)last, (long long)len));
555 *length = last - first + 1;
563 /*****************************************************************************
564 * Helper functions for authorization
571 http_base64(const char *src)
573 static const char base64[] =
574 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
575 "abcdefghijklmnopqrstuvwxyz"
582 if ((str = malloc(((l + 2) / 3) * 4 + 1)) == NULL)
588 t = (src[0] << 16) | (src[1] << 8) | src[2];
589 dst[0] = base64[(t >> 18) & 0x3f];
590 dst[1] = base64[(t >> 12) & 0x3f];
591 dst[2] = base64[(t >> 6) & 0x3f];
592 dst[3] = base64[(t >> 0) & 0x3f];
599 t = (src[0] << 16) | (src[1] << 8);
600 dst[0] = base64[(t >> 18) & 0x3f];
601 dst[1] = base64[(t >> 12) & 0x3f];
602 dst[2] = base64[(t >> 6) & 0x3f];
609 dst[0] = base64[(t >> 18) & 0x3f];
610 dst[1] = base64[(t >> 12) & 0x3f];
611 dst[2] = dst[3] = '=';
624 * Encode username and password
627 http_basic_auth(conn_t *conn, const char *hdr, const char *usr, const char *pwd)
632 DEBUG(fprintf(stderr, "usr: [%s]\n", usr));
633 DEBUG(fprintf(stderr, "pwd: [%s]\n", pwd));
634 if (asprintf(&upw, "%s:%s", usr, pwd) == -1)
636 auth = http_base64(upw);
640 r = http_cmd(conn, "%s: Basic %s", hdr, auth);
646 * Send an authorization header
649 http_authorize(conn_t *conn, const char *hdr, const char *p)
651 /* basic authorization */
652 if (strncasecmp(p, "basic:", 6) == 0) {
653 char *user, *pwd, *str;
657 for (p += 6; *p && *p != ':'; ++p)
659 if (!*p || strchr(++p, ':') == NULL)
661 if ((str = strdup(p)) == NULL)
662 return (-1); /* XXX */
664 pwd = strchr(str, ':');
666 r = http_basic_auth(conn, hdr, user, pwd);
674 /*****************************************************************************
675 * Helper functions for connecting to a server or proxy
679 * Connect to the correct HTTP server or proxy.
682 http_connect(struct url *URL, struct url *purl, const char *flags)
694 verbose = CHECK_FLAG('v');
698 else if (CHECK_FLAG('6'))
702 if (purl && strcasecmp(URL->scheme, SCHEME_HTTPS) != 0) {
704 } else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0) {
705 /* can't talk http to an ftp server */
706 /* XXX should set an error code */
710 if ((conn = fetch_connect(URL->host, URL->port, af, verbose)) == NULL)
711 /* fetch_connect() has already set an error code */
713 if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0 &&
714 fetch_ssl(conn, verbose) == -1) {
723 setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val, sizeof(val));
729 http_get_proxy(struct url * url, const char *flags)
734 if (flags != NULL && strchr(flags, 'd') != NULL)
736 if (fetch_no_proxy_match(url->host))
738 if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
739 *p && (purl = fetchParseURL(p))) {
741 strcpy(purl->scheme, SCHEME_HTTP);
743 purl->port = fetch_default_proxy_port(purl->scheme);
744 if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
752 http_print_html(FILE *out, FILE *in)
759 while ((line = fgetln(in, &len)) != NULL) {
760 while (len && isspace((unsigned char)line[len - 1]))
762 for (p = q = line; q < line + len; ++q) {
763 if (comment && *q == '-') {
764 if (q + 2 < line + len &&
765 strcmp(q, "-->") == 0) {
769 } else if (tag && !comment && *q == '>') {
772 } else if (!tag && *q == '<') {
774 fwrite(p, q - p, 1, out);
776 if (q + 3 < line + len &&
777 strcmp(q, "<!--") == 0) {
784 fwrite(p, q - p, 1, out);
790 /*****************************************************************************
795 * Send a request and process the reply
797 * XXX This function is way too long, the do..while loop should be split
798 * XXX off into a separate function.
801 http_request(struct url *URL, const char *op, struct url_stat *us,
802 struct url *purl, const char *flags)
805 char hbuf[MAXHOSTNAMELEN + 7], *host;
807 struct url *url, *new;
808 int chunked, direct, ims, need_auth, noredirect, verbose;
810 off_t offset, clength, length, size;
815 struct tm *timestruct;
817 direct = CHECK_FLAG('d');
818 noredirect = CHECK_FLAG('A');
819 verbose = CHECK_FLAG('v');
820 ims = CHECK_FLAG('i');
822 if (direct && purl) {
827 /* try the provided URL first */
830 /* if the A flag is set, we only get one try */
831 n = noredirect ? 1 : MAX_REDIRECT;
834 e = HTTP_PROTOCOL_ERROR;
847 url->port = fetch_default_port(url->scheme);
849 /* were we redirected to an FTP URL? */
850 if (purl == NULL && strcmp(url->scheme, SCHEME_FTP) == 0) {
851 if (strcmp(op, "GET") == 0)
852 return (ftp_request(url, "RETR", us, purl, flags));
853 else if (strcmp(op, "HEAD") == 0)
854 return (ftp_request(url, "STAT", us, purl, flags));
857 /* connect to server or proxy */
858 if ((conn = http_connect(url, purl, flags)) == NULL)
863 if (strchr(url->host, ':')) {
864 snprintf(hbuf, sizeof(hbuf), "[%s]", url->host);
868 if (url->port != fetch_default_port(url->scheme)) {
873 snprintf(hbuf + strlen(hbuf),
874 sizeof(hbuf) - strlen(hbuf), ":%d", url->port);
879 fetch_info("requesting %s://%s%s",
880 url->scheme, host, url->doc);
882 http_cmd(conn, "%s %s://%s%s HTTP/1.1",
883 op, url->scheme, host, url->doc);
885 http_cmd(conn, "%s %s HTTP/1.1",
889 if (ims && url->ims_time) {
890 timestruct = gmtime((time_t *)&url->ims_time);
891 (void)strftime(timebuf, 80, "%a, %d %b %Y %T GMT",
894 fetch_info("If-Modified-Since: %s", timebuf);
895 http_cmd(conn, "If-Modified-Since: %s", timebuf);
898 http_cmd(conn, "Host: %s", host);
900 /* proxy authorization */
902 if (*purl->user || *purl->pwd)
903 http_basic_auth(conn, "Proxy-Authorization",
904 purl->user, purl->pwd);
905 else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL && *p != '\0')
906 http_authorize(conn, "Proxy-Authorization", p);
909 /* server authorization */
910 if (need_auth || *url->user || *url->pwd) {
911 if (*url->user || *url->pwd)
912 http_basic_auth(conn, "Authorization", url->user, url->pwd);
913 else if ((p = getenv("HTTP_AUTH")) != NULL && *p != '\0')
914 http_authorize(conn, "Authorization", p);
915 else if (fetchAuthMethod && fetchAuthMethod(url) == 0) {
916 http_basic_auth(conn, "Authorization", url->user, url->pwd);
918 http_seterr(HTTP_NEED_AUTH);
924 if ((p = getenv("HTTP_REFERER")) != NULL && *p != '\0') {
925 if (strcasecmp(p, "auto") == 0)
926 http_cmd(conn, "Referer: %s://%s%s",
927 url->scheme, host, url->doc);
929 http_cmd(conn, "Referer: %s", p);
931 if ((p = getenv("HTTP_USER_AGENT")) != NULL && *p != '\0')
932 http_cmd(conn, "User-Agent: %s", p);
934 http_cmd(conn, "User-Agent: %s " _LIBFETCH_VER, getprogname());
936 http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset);
937 http_cmd(conn, "Connection: close");
938 http_cmd(conn, "%s", "");
941 * Force the queued request to be dispatched. Normally, one
942 * would do this with shutdown(2) but squid proxies can be
943 * configured to disallow such half-closed connections. To
944 * be compatible with such configurations, fiddle with socket
945 * options to force the pending data to be written.
948 setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val,
951 setsockopt(conn->sd, IPPROTO_TCP, TCP_NODELAY, &val,
955 switch (http_get_reply(conn)) {
958 case HTTP_NOT_MODIFIED:
961 case HTTP_MOVED_PERM:
962 case HTTP_MOVED_TEMP:
965 * Not so fine, but we still have to read the
966 * headers to get the new location.
972 * We already sent out authorization code,
973 * so there's nothing more we can do.
975 http_seterr(conn->err);
978 /* try again, but send the password this time */
980 fetch_info("server requires authorization");
982 case HTTP_NEED_PROXY_AUTH:
984 * If we're talking to a proxy, we already sent
985 * our proxy authorization code, so there's
986 * nothing more we can do.
988 http_seterr(conn->err);
992 * This can happen if we ask for 0 bytes because
993 * we already have the whole file. Consider this
994 * a success for now, and check sizes later.
997 case HTTP_PROTOCOL_ERROR:
1003 http_seterr(conn->err);
1006 /* fall through so we can get the full error message */
1011 switch ((h = http_next_header(conn, &p))) {
1016 http_seterr(HTTP_PROTOCOL_ERROR);
1018 case hdr_content_length:
1019 http_parse_length(p, &clength);
1021 case hdr_content_range:
1022 http_parse_range(p, &offset, &length, &size);
1024 case hdr_last_modified:
1025 http_parse_mtime(p, &mtime);
1028 if (!HTTP_REDIRECT(conn->err))
1033 fetch_info("%d redirect to %s", conn->err, p);
1036 new = fetchMakeURL(url->scheme, url->host, url->port, p,
1037 url->user, url->pwd);
1039 new = fetchParseURL(p);
1041 /* XXX should set an error code */
1042 DEBUG(fprintf(stderr, "failed to parse new URL\n"));
1045 if (!*new->user && !*new->pwd) {
1046 strcpy(new->user, url->user);
1047 strcpy(new->pwd, url->pwd);
1049 new->offset = url->offset;
1050 new->length = url->length;
1052 case hdr_transfer_encoding:
1054 chunked = (strcasecmp(p, "chunked") == 0);
1056 case hdr_www_authenticate:
1057 if (conn->err != HTTP_NEED_AUTH)
1059 /* if we were smarter, we'd check the method and realm */
1067 } while (h > hdr_end);
1069 /* we need to provide authentication */
1070 if (conn->err == HTTP_NEED_AUTH) {
1078 /* requested range not satisfiable */
1079 if (conn->err == HTTP_BAD_RANGE) {
1080 if (url->offset == size && url->length == 0) {
1081 /* asked for 0 bytes; fake it */
1082 offset = url->offset;
1084 conn->err = HTTP_OK;
1087 http_seterr(conn->err);
1092 /* we have a hit or an error */
1093 if (conn->err == HTTP_OK
1094 || conn->err == HTTP_NOT_MODIFIED
1095 || conn->err == HTTP_PARTIAL
1096 || HTTP_ERROR(conn->err))
1099 /* all other cases: we got a redirect */
1105 DEBUG(fprintf(stderr, "redirect with no new location\n"));
1113 /* we failed, or ran out of retries */
1119 DEBUG(fprintf(stderr, "offset %lld, length %lld,"
1120 " size %lld, clength %lld\n",
1121 (long long)offset, (long long)length,
1122 (long long)size, (long long)clength));
1124 if (conn->err == HTTP_NOT_MODIFIED) {
1125 http_seterr(HTTP_NOT_MODIFIED);
1129 /* check for inconsistencies */
1130 if (clength != -1 && length != -1 && clength != length) {
1131 http_seterr(HTTP_PROTOCOL_ERROR);
1137 length = offset + clength;
1138 if (length != -1 && size != -1 && length != size) {
1139 http_seterr(HTTP_PROTOCOL_ERROR);
1148 us->atime = us->mtime = mtime;
1152 if (URL->offset > 0 && offset > URL->offset) {
1153 http_seterr(HTTP_PROTOCOL_ERROR);
1157 /* report back real offset and size */
1158 URL->offset = offset;
1159 URL->length = clength;
1161 /* wrap it up in a FILE */
1162 if ((f = http_funopen(conn, chunked)) == NULL) {
1172 if (HTTP_ERROR(conn->err)) {
1173 http_print_html(stderr, f);
1191 /*****************************************************************************
1196 * Retrieve and stat a file by HTTP
1199 fetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags)
1201 return (http_request(URL, "GET", us, http_get_proxy(URL, flags), flags));
1205 * Retrieve a file by HTTP
1208 fetchGetHTTP(struct url *URL, const char *flags)
1210 return (fetchXGetHTTP(URL, NULL, flags));
1214 * Store a file by HTTP
1217 fetchPutHTTP(struct url *URL __unused, const char *flags __unused)
1219 warnx("fetchPutHTTP(): not implemented");
1224 * Get an HTTP document's metadata
1227 fetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags)
1231 f = http_request(URL, "HEAD", us, http_get_proxy(URL, flags), flags);
1242 fetchListHTTP(struct url *url __unused, const char *flags __unused)
1244 warnx("fetchListHTTP(): not implemented");