2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1998-2004 Dag-Erling Smørgrav
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
36 #include <netinet/in.h>
47 auth_t fetchAuthMethod;
49 char fetchLastErrString[MAXERRSTRING];
51 int fetchRestartCalls = 1;
55 /*** Local data **************************************************************/
58 * Error messages for parser errors
60 #define URL_MALFORMED 1
61 #define URL_BAD_SCHEME 2
62 #define URL_BAD_PORT 3
63 static struct fetcherr url_errlist[] = {
64 { URL_MALFORMED, FETCH_URL, "Malformed URL" },
65 { URL_BAD_SCHEME, FETCH_URL, "Invalid URL scheme" },
66 { URL_BAD_PORT, FETCH_URL, "Invalid server port" },
67 { -1, FETCH_UNKNOWN, "Unknown parser error" }
71 /*** Public API **************************************************************/
74 * Select the appropriate protocol for the URL scheme, and return a
75 * read-only stream connected to the document referenced by the URL.
76 * Also fill out the struct url_stat.
79 fetchXGet(struct url *URL, struct url_stat *us, const char *flags)
84 us->atime = us->mtime = 0;
86 if (strcmp(URL->scheme, SCHEME_FILE) == 0)
87 return (fetchXGetFile(URL, us, flags));
88 else if (strcmp(URL->scheme, SCHEME_FTP) == 0)
89 return (fetchXGetFTP(URL, us, flags));
90 else if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
91 return (fetchXGetHTTP(URL, us, flags));
92 else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
93 return (fetchXGetHTTP(URL, us, flags));
94 url_seterr(URL_BAD_SCHEME);
99 * Select the appropriate protocol for the URL scheme, and return a
100 * read-only stream connected to the document referenced by the URL.
103 fetchGet(struct url *URL, const char *flags)
105 return (fetchXGet(URL, NULL, flags));
109 * Select the appropriate protocol for the URL scheme, and return a
110 * write-only stream connected to the document referenced by the URL.
113 fetchPut(struct url *URL, const char *flags)
116 if (strcmp(URL->scheme, SCHEME_FILE) == 0)
117 return (fetchPutFile(URL, flags));
118 else if (strcmp(URL->scheme, SCHEME_FTP) == 0)
119 return (fetchPutFTP(URL, flags));
120 else if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
121 return (fetchPutHTTP(URL, flags));
122 else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
123 return (fetchPutHTTP(URL, flags));
124 url_seterr(URL_BAD_SCHEME);
129 * Select the appropriate protocol for the URL scheme, and return the
130 * size of the document referenced by the URL if it exists.
133 fetchStat(struct url *URL, struct url_stat *us, const char *flags)
138 us->atime = us->mtime = 0;
140 if (strcmp(URL->scheme, SCHEME_FILE) == 0)
141 return (fetchStatFile(URL, us, flags));
142 else if (strcmp(URL->scheme, SCHEME_FTP) == 0)
143 return (fetchStatFTP(URL, us, flags));
144 else if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
145 return (fetchStatHTTP(URL, us, flags));
146 else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
147 return (fetchStatHTTP(URL, us, flags));
148 url_seterr(URL_BAD_SCHEME);
153 * Select the appropriate protocol for the URL scheme, and return a
154 * list of files in the directory pointed to by the URL.
157 fetchList(struct url *URL, const char *flags)
160 if (strcmp(URL->scheme, SCHEME_FILE) == 0)
161 return (fetchListFile(URL, flags));
162 else if (strcmp(URL->scheme, SCHEME_FTP) == 0)
163 return (fetchListFTP(URL, flags));
164 else if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
165 return (fetchListHTTP(URL, flags));
166 else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
167 return (fetchListHTTP(URL, flags));
168 url_seterr(URL_BAD_SCHEME);
173 * Attempt to parse the given URL; if successful, call fetchXGet().
176 fetchXGetURL(const char *URL, struct url_stat *us, const char *flags)
181 if ((u = fetchParseURL(URL)) == NULL)
184 f = fetchXGet(u, us, flags);
191 * Attempt to parse the given URL; if successful, call fetchGet().
194 fetchGetURL(const char *URL, const char *flags)
196 return (fetchXGetURL(URL, NULL, flags));
200 * Attempt to parse the given URL; if successful, call fetchPut().
203 fetchPutURL(const char *URL, const char *flags)
208 if ((u = fetchParseURL(URL)) == NULL)
211 f = fetchPut(u, flags);
218 * Attempt to parse the given URL; if successful, call fetchStat().
221 fetchStatURL(const char *URL, struct url_stat *us, const char *flags)
226 if ((u = fetchParseURL(URL)) == NULL)
229 s = fetchStat(u, us, flags);
236 * Attempt to parse the given URL; if successful, call fetchList().
239 fetchListURL(const char *URL, const char *flags)
244 if ((u = fetchParseURL(URL)) == NULL)
247 ue = fetchList(u, flags);
257 fetchMakeURL(const char *scheme, const char *host, int port, const char *doc,
258 const char *user, const char *pwd)
262 if (!scheme || (!host && !doc)) {
263 url_seterr(URL_MALFORMED);
267 if (port < 0 || port > 65535) {
268 url_seterr(URL_BAD_PORT);
272 /* allocate struct url */
273 if ((u = calloc(1, sizeof(*u))) == NULL) {
279 if ((u->doc = strdup(doc ? doc : "/")) == NULL) {
285 #define seturl(x) snprintf(u->x, sizeof(u->x), "%s", x)
297 * Return value of the given hex digit.
300 fetch_hexval(char ch)
303 if (ch >= '0' && ch <= '9')
305 else if (ch >= 'a' && ch <= 'f')
306 return (ch - 'a' + 10);
307 else if (ch >= 'A' && ch <= 'F')
308 return (ch - 'A' + 10);
313 * Decode percent-encoded URL component from src into dst, stopping at end
314 * of string, or at @ or : separators. Returns a pointer to the unhandled
315 * part of the input string (null terminator, @, or :). No terminator is
316 * written to dst (it is the caller's responsibility).
319 fetch_pctdecode(char *dst, const char *src, size_t dlen)
325 for (s = src; *s != '\0' && *s != '@' && *s != ':'; s++) {
326 if (s[0] == '%' && (d1 = fetch_hexval(s[1])) >= 0 &&
327 (d2 = fetch_hexval(s[2])) >= 0 && (d1 > 0 || d2 > 0)) {
330 } else if (s[0] == '%') {
331 /* Invalid escape sequence. */
345 * Split an URL into components. URL syntax is:
346 * [method:/][/[user[:pwd]@]host[:port]/][document]
347 * This almost, but not quite, RFC1738 URL syntax.
350 fetchParseURL(const char *URL)
357 /* allocate struct url */
358 if ((u = calloc(1, sizeof(*u))) == NULL) {
365 if ((p = strstr(URL, ":/"))) {
366 if (p - URL > URL_SCHEMELEN)
368 for (i = 0; URL + i < p; i++)
369 u->scheme[i] = tolower((unsigned char)URL[i]);
372 * Only one slash: no host, leave slash as part of document
373 * Two slashes: host follows, strip slashes
380 if (!*URL || *URL == '/' || *URL == '.' ||
381 (u->scheme[0] == '\0' &&
382 strchr(URL, '/') == NULL && strchr(URL, ':') == NULL))
385 p = strpbrk(URL, "/@");
386 if (p && *p == '@') {
388 q = fetch_pctdecode(u->user, URL, URL_USERLEN);
394 q = fetch_pctdecode(u->pwd, q + 1, URL_PWDLEN);
405 q = p + 1 + strspn(p + 1, ":0123456789ABCDEFabcdef");
409 /* valid characters in a DNS name */
410 q = p + strspn(p, "-." "0123456789"
411 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "_"
412 "abcdefghijklmnopqrstuvwxyz");
414 if ((*q != '\0' && *q != '/' && *q != ':') || q - p > MAXHOSTNAMELEN)
416 for (i = 0; p + i < q; i++)
417 u->host[i] = tolower((unsigned char)p[i]);
423 for (n = 0, q = ++p; *q && (*q != '/'); q++) {
424 if (*q >= '0' && *q <= '9' && n < INT_MAX / 10) {
425 n = n * 10 + (*q - '0');
428 url_seterr(URL_BAD_PORT);
432 if (n < 1 || n > IPPORT_MAX)
443 if (strcmp(u->scheme, SCHEME_HTTP) == 0 ||
444 strcmp(u->scheme, SCHEME_HTTPS) == 0) {
445 const char hexnums[] = "0123456789abcdef";
447 /* percent-escape whitespace. */
448 if ((doc = malloc(strlen(p) * 3 + 1)) == NULL) {
454 if (!isspace((unsigned char)*p)) {
458 *doc++ = hexnums[((unsigned int)*p) >> 4];
459 *doc++ = hexnums[((unsigned int)*p) & 0xf];
464 } else if ((u->doc = strdup(p)) == NULL) {
469 DEBUGF("scheme: \"%s\"\n"
474 "document: \"%s\"\n",
475 u->scheme, u->user, u->pwd,
476 u->host, u->port, u->doc);
489 fetchFreeURL(struct url *u)