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.78 2007/05/08 19:28:03 des 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>
79 #include <netinet/in.h>
80 #include <netinet/tcp.h>
86 /* Maximum number of redirects to follow */
87 #define MAX_REDIRECT 5
89 /* Symbolic names for reply codes we care about */
91 #define HTTP_PARTIAL 206
92 #define HTTP_MOVED_PERM 301
93 #define HTTP_MOVED_TEMP 302
94 #define HTTP_SEE_OTHER 303
95 #define HTTP_TEMP_REDIRECT 307
96 #define HTTP_NEED_AUTH 401
97 #define HTTP_NEED_PROXY_AUTH 407
98 #define HTTP_BAD_RANGE 416
99 #define HTTP_PROTOCOL_ERROR 999
101 #define HTTP_REDIRECT(xyz) ((xyz) == HTTP_MOVED_PERM \
102 || (xyz) == HTTP_MOVED_TEMP \
103 || (xyz) == HTTP_TEMP_REDIRECT \
104 || (xyz) == HTTP_SEE_OTHER)
106 #define HTTP_ERROR(xyz) ((xyz) > 400 && (xyz) < 599)
109 /*****************************************************************************
110 * I/O functions for decoding chunked streams
115 conn_t
*conn
; /* connection */
116 int chunked
; /* chunked mode */
117 char *buf
; /* chunk buffer */
118 size_t bufsize
; /* size of chunk buffer */
119 ssize_t buflen
; /* amount of data currently in buffer */
120 int bufpos
; /* current read offset in buffer */
121 int eof
; /* end-of-file flag */
122 int error
; /* error flag */
123 size_t chunksize
; /* remaining size of current chunk */
130 * Get next chunk header
133 _http_new_chunk(struct httpio
*io
)
137 if (_fetch_getln(io
->conn
) == -1)
140 if (io
->conn
->buflen
< 2 || !isxdigit(*io
->conn
->buf
))
143 for (p
= io
->conn
->buf
; *p
&& !isspace(*p
); ++p
) {
149 io
->chunksize
= io
->chunksize
* 16 +
152 io
->chunksize
= io
->chunksize
* 16 +
153 10 + tolower(*p
) - 'a';
159 io
->total
+= io
->chunksize
;
160 if (io
->chunksize
== 0)
161 fprintf(stderr
, "%s(): end of last chunk\n", __func__
);
163 fprintf(stderr
, "%s(): new chunk: %lu (%lu)\n",
164 __func__
, (unsigned long)io
->chunksize
,
165 (unsigned long)io
->total
);
169 return (io
->chunksize
);
173 * Grow the input buffer to at least len bytes
176 _http_growbuf(struct httpio
*io
, size_t len
)
180 if (io
->bufsize
>= len
)
183 if ((tmp
= realloc(io
->buf
, len
)) == NULL
)
191 * Fill the input buffer, do chunk decoding on the fly
194 _http_fillbuf(struct httpio
*io
, size_t len
)
201 if (io
->chunked
== 0) {
202 if (_http_growbuf(io
, len
) == -1)
204 if ((io
->buflen
= _fetch_read(io
->conn
, io
->buf
, len
)) == -1) {
212 if (io
->chunksize
== 0) {
213 switch (_http_new_chunk(io
)) {
223 if (len
> io
->chunksize
)
225 if (_http_growbuf(io
, len
) == -1)
227 if ((io
->buflen
= _fetch_read(io
->conn
, io
->buf
, len
)) == -1) {
231 io
->chunksize
-= io
->buflen
;
233 if (io
->chunksize
== 0) {
236 if (_fetch_read(io
->conn
, endl
, 2) != 2 ||
237 endl
[0] != '\r' || endl
[1] != '\n')
250 _http_readfn(void *v
, char *buf
, int len
)
252 struct httpio
*io
= (struct httpio
*)v
;
260 for (pos
= 0; len
> 0; pos
+= l
, len
-= l
) {
262 if (!io
->buf
|| io
->bufpos
== io
->buflen
)
263 if (_http_fillbuf(io
, len
) < 1)
265 l
= io
->buflen
- io
->bufpos
;
268 bcopy(io
->buf
+ io
->bufpos
, buf
+ pos
, l
);
272 if (!pos
&& io
->error
)
281 _http_writefn(void *v
, const char *buf
, int len
)
283 struct httpio
*io
= (struct httpio
*)v
;
285 return (_fetch_write(io
->conn
, buf
, len
));
292 _http_closefn(void *v
)
294 struct httpio
*io
= (struct httpio
*)v
;
297 r
= _fetch_close(io
->conn
);
305 * Wrap a file descriptor up
308 _http_funopen(conn_t
*conn
, int chunked
)
313 if ((io
= calloc(1, sizeof(*io
))) == NULL
) {
318 io
->chunked
= chunked
;
319 f
= funopen(io
, _http_readfn
, _http_writefn
, NULL
, _http_closefn
);
329 /*****************************************************************************
330 * Helper functions for talking to the server and parsing its replies
343 hdr_transfer_encoding
,
347 /* Names of interesting headers */
352 { hdr_content_length
, "Content-Length" },
353 { hdr_content_range
, "Content-Range" },
354 { hdr_last_modified
, "Last-Modified" },
355 { hdr_location
, "Location" },
356 { hdr_transfer_encoding
, "Transfer-Encoding" },
357 { hdr_www_authenticate
, "WWW-Authenticate" },
358 { hdr_unknown
, NULL
},
362 * Send a formatted line; optionally echo to terminal
365 _http_cmd(conn_t
*conn
, const char *fmt
, ...)
373 len
= vasprintf(&msg
, fmt
, ap
);
382 r
= _fetch_putln(conn
, msg
, len
);
394 * Get and parse status line
397 _http_get_reply(conn_t
*conn
)
401 if (_fetch_getln(conn
) == -1)
404 * A valid status line looks like "HTTP/m.n xyz reason" where m
405 * and n are the major and minor protocol version numbers and xyz
407 * Unfortunately, there are servers out there (NCSA 1.5.1, to name
408 * just one) that do not send a version number, so we can't rely
409 * on finding one, but if we do, insist on it being 1.0 or 1.1.
410 * We don't care about the reason phrase.
412 if (strncmp(conn
->buf
, "HTTP", 4) != 0)
413 return (HTTP_PROTOCOL_ERROR
);
416 if (p
[1] != '1' || p
[2] != '.' || (p
[3] != '0' && p
[3] != '1'))
417 return (HTTP_PROTOCOL_ERROR
);
420 if (*p
!= ' ' || !isdigit(p
[1]) || !isdigit(p
[2]) || !isdigit(p
[3]))
421 return (HTTP_PROTOCOL_ERROR
);
423 conn
->err
= (p
[1] - '0') * 100 + (p
[2] - '0') * 10 + (p
[3] - '0');
428 * Check a header; if the type matches the given string, return a pointer
429 * to the beginning of the value.
432 _http_match(const char *str
, const char *hdr
)
434 while (*str
&& *hdr
&& tolower(*str
++) == tolower(*hdr
++))
436 if (*str
|| *hdr
!= ':')
438 while (*hdr
&& isspace(*++hdr
))
444 * Get the next header and return the appropriate symbolic code.
447 _http_next_header(conn_t
*conn
, const char **p
)
451 if (_fetch_getln(conn
) == -1)
452 return (hdr_syserror
);
453 while (conn
->buflen
&& isspace(conn
->buf
[conn
->buflen
- 1]))
455 conn
->buf
[conn
->buflen
] = '\0';
456 if (conn
->buflen
== 0)
459 * We could check for malformed headers but we don't really care.
460 * A valid header starts with a token immediately followed by a
461 * colon; a token is any sequence of non-control, non-whitespace
462 * characters except "()<>@,;:\\\"{}".
464 for (i
= 0; hdr_names
[i
].num
!= hdr_unknown
; i
++)
465 if ((*p
= _http_match(hdr_names
[i
].name
, conn
->buf
)) != NULL
)
466 return (hdr_names
[i
].num
);
467 return (hdr_unknown
);
471 * Parse a last-modified header
474 _http_parse_mtime(const char *p
, time_t *mtime
)
479 strncpy(locale
, setlocale(LC_TIME
, NULL
), sizeof(locale
));
480 setlocale(LC_TIME
, "C");
481 r
= strptime(p
, "%a, %d %b %Y %H:%M:%S GMT", &tm
);
482 /* XXX should add support for date-2 and date-3 */
483 setlocale(LC_TIME
, locale
);
486 DEBUG(fprintf(stderr
, "last modified: [%04d-%02d-%02d "
488 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
,
489 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
));
490 *mtime
= timegm(&tm
);
495 * Parse a content-length header
498 _http_parse_length(const char *p
, off_t
*length
)
502 for (len
= 0; *p
&& isdigit(*p
); ++p
)
503 len
= len
* 10 + (*p
- '0');
506 DEBUG(fprintf(stderr
, "content length: [%lld]\n",
513 * Parse a content-range header
516 _http_parse_range(const char *p
, off_t
*offset
, off_t
*length
, off_t
*size
)
518 off_t first
, last
, len
;
520 if (strncasecmp(p
, "bytes ", 6) != 0)
527 for (first
= 0; *p
&& isdigit(*p
); ++p
)
528 first
= first
* 10 + *p
- '0';
531 for (last
= 0, ++p
; *p
&& isdigit(*p
); ++p
)
532 last
= last
* 10 + *p
- '0';
534 if (first
> last
|| *p
!= '/')
536 for (len
= 0, ++p
; *p
&& isdigit(*p
); ++p
)
537 len
= len
* 10 + *p
- '0';
538 if (*p
|| len
< last
- first
+ 1)
541 DEBUG(fprintf(stderr
, "content range: [*/%lld]\n",
545 DEBUG(fprintf(stderr
, "content range: [%lld-%lld/%lld]\n",
546 (long long)first
, (long long)last
, (long long)len
));
547 *length
= last
- first
+ 1;
555 /*****************************************************************************
556 * Helper functions for authorization
563 _http_base64(const char *src
)
565 static const char base64
[] =
566 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
567 "abcdefghijklmnopqrstuvwxyz"
574 if ((str
= malloc(((l
+ 2) / 3) * 4 + 1)) == NULL
)
580 t
= (src
[0] << 16) | (src
[1] << 8) | src
[2];
581 dst
[0] = base64
[(t
>> 18) & 0x3f];
582 dst
[1] = base64
[(t
>> 12) & 0x3f];
583 dst
[2] = base64
[(t
>> 6) & 0x3f];
584 dst
[3] = base64
[(t
>> 0) & 0x3f];
591 t
= (src
[0] << 16) | (src
[1] << 8);
592 dst
[0] = base64
[(t
>> 18) & 0x3f];
593 dst
[1] = base64
[(t
>> 12) & 0x3f];
594 dst
[2] = base64
[(t
>> 6) & 0x3f];
601 dst
[0] = base64
[(t
>> 18) & 0x3f];
602 dst
[1] = base64
[(t
>> 12) & 0x3f];
603 dst
[2] = dst
[3] = '=';
616 * Encode username and password
619 _http_basic_auth(conn_t
*conn
, const char *hdr
, const char *usr
, const char *pwd
)
624 DEBUG(fprintf(stderr
, "usr: [%s]\n", usr
));
625 DEBUG(fprintf(stderr
, "pwd: [%s]\n", pwd
));
626 if (asprintf(&upw
, "%s:%s", usr
, pwd
) == -1)
628 auth
= _http_base64(upw
);
632 r
= _http_cmd(conn
, "%s: Basic %s", hdr
, auth
);
638 * Send an authorization header
641 _http_authorize(conn_t
*conn
, const char *hdr
, const char *p
)
643 /* basic authorization */
644 if (strncasecmp(p
, "basic:", 6) == 0) {
645 char *user
, *pwd
, *str
;
649 for (p
+= 6; *p
&& *p
!= ':'; ++p
)
651 if (!*p
|| strchr(++p
, ':') == NULL
)
653 if ((str
= strdup(p
)) == NULL
)
654 return (-1); /* XXX */
656 pwd
= strchr(str
, ':');
658 r
= _http_basic_auth(conn
, hdr
, user
, pwd
);
666 /*****************************************************************************
667 * Helper functions for connecting to a server or proxy
671 * Connect to the correct HTTP server or proxy.
674 _http_connect(struct url
*URL
, struct url
*purl
, const char *flags
)
686 verbose
= CHECK_FLAG('v');
690 else if (CHECK_FLAG('6'))
694 if (purl
&& strcasecmp(URL
->scheme
, SCHEME_HTTPS
) != 0) {
696 } else if (strcasecmp(URL
->scheme
, SCHEME_FTP
) == 0) {
697 /* can't talk http to an ftp server */
698 /* XXX should set an error code */
702 if ((conn
= _fetch_connect(URL
->host
, URL
->port
, af
, verbose
)) == NULL
)
703 /* _fetch_connect() has already set an error code */
705 if (strcasecmp(URL
->scheme
, SCHEME_HTTPS
) == 0 &&
706 _fetch_ssl(conn
, verbose
) == -1) {
715 setsockopt(conn
->sd
, IPPROTO_TCP
, TCP_NOPUSH
, &val
, sizeof(val
));
721 _http_get_proxy(const char *flags
)
726 if (flags
!= NULL
&& strchr(flags
, 'd') != NULL
)
728 if (((p
= getenv("HTTP_PROXY")) || (p
= getenv("http_proxy"))) &&
729 *p
&& (purl
= fetchParseURL(p
))) {
731 strcpy(purl
->scheme
, SCHEME_HTTP
);
733 purl
->port
= _fetch_default_proxy_port(purl
->scheme
);
734 if (strcasecmp(purl
->scheme
, SCHEME_HTTP
) == 0)
742 _http_print_html(FILE *out
, FILE *in
)
749 while ((line
= fgetln(in
, &len
)) != NULL
) {
750 while (len
&& isspace(line
[len
- 1]))
752 for (p
= q
= line
; q
< line
+ len
; ++q
) {
753 if (comment
&& *q
== '-') {
754 if (q
+ 2 < line
+ len
&&
755 strcmp(q
, "-->") == 0) {
759 } else if (tag
&& !comment
&& *q
== '>') {
762 } else if (!tag
&& *q
== '<') {
764 fwrite(p
, q
- p
, 1, out
);
766 if (q
+ 3 < line
+ len
&&
767 strcmp(q
, "<!--") == 0) {
774 fwrite(p
, q
- p
, 1, out
);
780 /*****************************************************************************
785 * Send a request and process the reply
787 * XXX This function is way too long, the do..while loop should be split
788 * XXX off into a separate function.
791 _http_request(struct url
*URL
, const char *op
, struct url_stat
*us
,
792 struct url
*purl
, const char *flags
)
795 struct url
*url
, *new;
796 int chunked
, direct
, need_auth
, noredirect
, verbose
;
798 off_t offset
, clength
, length
, size
;
803 char hbuf
[MAXHOSTNAMELEN
+ 7], *host
;
805 direct
= CHECK_FLAG('d');
806 noredirect
= CHECK_FLAG('A');
807 verbose
= CHECK_FLAG('v');
809 if (direct
&& purl
) {
814 /* try the provided URL first */
817 /* if the A flag is set, we only get one try */
818 n
= noredirect
? 1 : MAX_REDIRECT
;
821 e
= HTTP_PROTOCOL_ERROR
;
834 url
->port
= _fetch_default_port(url
->scheme
);
836 /* were we redirected to an FTP URL? */
837 if (purl
== NULL
&& strcmp(url
->scheme
, SCHEME_FTP
) == 0) {
838 if (strcmp(op
, "GET") == 0)
839 return (_ftp_request(url
, "RETR", us
, purl
, flags
));
840 else if (strcmp(op
, "HEAD") == 0)
841 return (_ftp_request(url
, "STAT", us
, purl
, flags
));
844 /* connect to server or proxy */
845 if ((conn
= _http_connect(url
, purl
, flags
)) == NULL
)
850 if (strchr(url
->host
, ':')) {
851 snprintf(hbuf
, sizeof(hbuf
), "[%s]", url
->host
);
855 if (url
->port
!= _fetch_default_port(url
->scheme
)) {
860 snprintf(hbuf
+ strlen(hbuf
),
861 sizeof(hbuf
) - strlen(hbuf
), ":%d", url
->port
);
866 _fetch_info("requesting %s://%s%s",
867 url
->scheme
, host
, url
->doc
);
869 _http_cmd(conn
, "%s %s://%s%s HTTP/1.1",
870 op
, url
->scheme
, host
, url
->doc
);
872 _http_cmd(conn
, "%s %s HTTP/1.1",
877 _http_cmd(conn
, "Host: %s", host
);
879 /* proxy authorization */
881 if (*purl
->user
|| *purl
->pwd
)
882 _http_basic_auth(conn
, "Proxy-Authorization",
883 purl
->user
, purl
->pwd
);
884 else if ((p
= getenv("HTTP_PROXY_AUTH")) != NULL
&& *p
!= '\0')
885 _http_authorize(conn
, "Proxy-Authorization", p
);
888 /* server authorization */
889 if (need_auth
|| *url
->user
|| *url
->pwd
) {
890 if (*url
->user
|| *url
->pwd
)
891 _http_basic_auth(conn
, "Authorization", url
->user
, url
->pwd
);
892 else if ((p
= getenv("HTTP_AUTH")) != NULL
&& *p
!= '\0')
893 _http_authorize(conn
, "Authorization", p
);
894 else if (fetchAuthMethod
&& fetchAuthMethod(url
) == 0) {
895 _http_basic_auth(conn
, "Authorization", url
->user
, url
->pwd
);
897 _http_seterr(HTTP_NEED_AUTH
);
903 if ((p
= getenv("HTTP_REFERER")) != NULL
&& *p
!= '\0') {
904 if (strcasecmp(p
, "auto") == 0)
905 _http_cmd(conn
, "Referer: %s://%s%s",
906 url
->scheme
, host
, url
->doc
);
908 _http_cmd(conn
, "Referer: %s", p
);
910 if ((p
= getenv("HTTP_USER_AGENT")) != NULL
&& *p
!= '\0')
911 _http_cmd(conn
, "User-Agent: %s", p
);
913 _http_cmd(conn
, "User-Agent: %s " _LIBFETCH_VER
, getprogname());
915 _http_cmd(conn
, "Range: bytes=%lld-", (long long)url
->offset
);
916 _http_cmd(conn
, "Connection: close");
920 * Force the queued request to be dispatched. Normally, one
921 * would do this with shutdown(2) but squid proxies can be
922 * configured to disallow such half-closed connections. To
923 * be compatible with such configurations, fiddle with socket
924 * options to force the pending data to be written.
927 setsockopt(conn
->sd
, IPPROTO_TCP
, TCP_NOPUSH
, &val
,
930 setsockopt(conn
->sd
, IPPROTO_TCP
, TCP_NODELAY
, &val
,
934 switch (_http_get_reply(conn
)) {
939 case HTTP_MOVED_PERM
:
940 case HTTP_MOVED_TEMP
:
943 * Not so fine, but we still have to read the
944 * headers to get the new location.
950 * We already sent out authorization code,
951 * so there's nothing more we can do.
953 _http_seterr(conn
->err
);
956 /* try again, but send the password this time */
958 _fetch_info("server requires authorization");
960 case HTTP_NEED_PROXY_AUTH
:
962 * If we're talking to a proxy, we already sent
963 * our proxy authorization code, so there's
964 * nothing more we can do.
966 _http_seterr(conn
->err
);
970 * This can happen if we ask for 0 bytes because
971 * we already have the whole file. Consider this
972 * a success for now, and check sizes later.
975 case HTTP_PROTOCOL_ERROR
:
981 _http_seterr(conn
->err
);
984 /* fall through so we can get the full error message */
989 switch ((h
= _http_next_header(conn
, &p
))) {
994 _http_seterr(HTTP_PROTOCOL_ERROR
);
996 case hdr_content_length
:
997 _http_parse_length(p
, &clength
);
999 case hdr_content_range
:
1000 _http_parse_range(p
, &offset
, &length
, &size
);
1002 case hdr_last_modified
:
1003 _http_parse_mtime(p
, &mtime
);
1006 if (!HTTP_REDIRECT(conn
->err
))
1011 _fetch_info("%d redirect to %s", conn
->err
, p
);
1014 new = fetchMakeURL(url
->scheme
, url
->host
, url
->port
, p
,
1015 url
->user
, url
->pwd
);
1017 new = fetchParseURL(p
);
1019 /* XXX should set an error code */
1020 DEBUG(fprintf(stderr
, "failed to parse new URL\n"));
1023 if (!*new->user
&& !*new->pwd
) {
1024 strcpy(new->user
, url
->user
);
1025 strcpy(new->pwd
, url
->pwd
);
1027 new->offset
= url
->offset
;
1028 new->length
= url
->length
;
1030 case hdr_transfer_encoding
:
1032 chunked
= (strcasecmp(p
, "chunked") == 0);
1034 case hdr_www_authenticate
:
1035 if (conn
->err
!= HTTP_NEED_AUTH
)
1037 /* if we were smarter, we'd check the method and realm */
1045 } while (h
> hdr_end
);
1047 /* we need to provide authentication */
1048 if (conn
->err
== HTTP_NEED_AUTH
) {
1056 /* requested range not satisfiable */
1057 if (conn
->err
== HTTP_BAD_RANGE
) {
1058 if (url
->offset
== size
&& url
->length
== 0) {
1059 /* asked for 0 bytes; fake it */
1060 offset
= url
->offset
;
1061 conn
->err
= HTTP_OK
;
1064 _http_seterr(conn
->err
);
1069 /* we have a hit or an error */
1070 if (conn
->err
== HTTP_OK
|| conn
->err
== HTTP_PARTIAL
|| HTTP_ERROR(conn
->err
))
1073 /* all other cases: we got a redirect */
1079 DEBUG(fprintf(stderr
, "redirect with no new location\n"));
1087 /* we failed, or ran out of retries */
1093 DEBUG(fprintf(stderr
, "offset %lld, length %lld,"
1094 " size %lld, clength %lld\n",
1095 (long long)offset
, (long long)length
,
1096 (long long)size
, (long long)clength
));
1098 /* check for inconsistencies */
1099 if (clength
!= -1 && length
!= -1 && clength
!= length
) {
1100 _http_seterr(HTTP_PROTOCOL_ERROR
);
1106 length
= offset
+ clength
;
1107 if (length
!= -1 && size
!= -1 && length
!= size
) {
1108 _http_seterr(HTTP_PROTOCOL_ERROR
);
1117 us
->atime
= us
->mtime
= mtime
;
1121 if (URL
->offset
> 0 && offset
> URL
->offset
) {
1122 _http_seterr(HTTP_PROTOCOL_ERROR
);
1126 /* report back real offset and size */
1127 URL
->offset
= offset
;
1128 URL
->length
= clength
;
1130 /* wrap it up in a FILE */
1131 if ((f
= _http_funopen(conn
, chunked
)) == NULL
) {
1141 if (HTTP_ERROR(conn
->err
)) {
1142 _http_print_html(stderr
, f
);
1160 /*****************************************************************************
1165 * Retrieve and stat a file by HTTP
1168 fetchXGetHTTP(struct url
*URL
, struct url_stat
*us
, const char *flags
)
1170 return (_http_request(URL
, "GET", us
, _http_get_proxy(flags
), flags
));
1174 * Retrieve a file by HTTP
1177 fetchGetHTTP(struct url
*URL
, const char *flags
)
1179 return (fetchXGetHTTP(URL
, NULL
, flags
));
1183 * Store a file by HTTP
1186 fetchPutHTTP(struct url
*URL __unused
, const char *flags __unused
)
1188 warnx("fetchPutHTTP(): not implemented");
1193 * Get an HTTP document's metadata
1196 fetchStatHTTP(struct url
*URL
, struct url_stat
*us
, const char *flags
)
1200 f
= _http_request(URL
, "HEAD", us
, _http_get_proxy(flags
), flags
);
1211 fetchListHTTP(struct url
*url __unused
, const char *flags __unused
)
1213 warnx("fetchListHTTP(): not implemented");