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/param.h>
33 #include <netinet/in.h>
44 auth_t fetchAuthMethod;
46 char fetchLastErrString[MAXERRSTRING];
48 int fetchRestartCalls = 1;
52 /*** Local data **************************************************************/
55 * Error messages for parser errors
57 #define URL_MALFORMED 1
58 #define URL_BAD_SCHEME 2
59 #define URL_BAD_PORT 3
60 static struct fetcherr url_errlist[] = {
61 { URL_MALFORMED, FETCH_URL, "Malformed URL" },
62 { URL_BAD_SCHEME, FETCH_URL, "Invalid URL scheme" },
63 { URL_BAD_PORT, FETCH_URL, "Invalid server port" },
64 { -1, FETCH_UNKNOWN, "Unknown parser error" }
68 /*** Public API **************************************************************/
71 * Select the appropriate protocol for the URL scheme, and return a
72 * read-only stream connected to the document referenced by the URL.
73 * Also fill out the struct url_stat.
76 fetchXGet(struct url *URL, struct url_stat *us, const char *flags)
81 us->atime = us->mtime = 0;
83 if (strcmp(URL->scheme, SCHEME_FILE) == 0)
84 return (fetchXGetFile(URL, us, flags));
85 else if (strcmp(URL->scheme, SCHEME_FTP) == 0)
86 return (fetchXGetFTP(URL, us, flags));
87 else if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
88 return (fetchXGetHTTP(URL, us, flags));
89 else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
90 return (fetchXGetHTTP(URL, us, flags));
91 url_seterr(URL_BAD_SCHEME);
96 * Select the appropriate protocol for the URL scheme, and return a
97 * read-only stream connected to the document referenced by the URL.
100 fetchGet(struct url *URL, const char *flags)
102 return (fetchXGet(URL, NULL, flags));
106 * Select the appropriate protocol for the URL scheme, and return a
107 * write-only stream connected to the document referenced by the URL.
110 fetchPut(struct url *URL, const char *flags)
113 if (strcmp(URL->scheme, SCHEME_FILE) == 0)
114 return (fetchPutFile(URL, flags));
115 else if (strcmp(URL->scheme, SCHEME_FTP) == 0)
116 return (fetchPutFTP(URL, flags));
117 else if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
118 return (fetchPutHTTP(URL, flags));
119 else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
120 return (fetchPutHTTP(URL, flags));
121 url_seterr(URL_BAD_SCHEME);
126 * Select the appropriate protocol for the URL scheme, and return the
127 * size of the document referenced by the URL if it exists.
130 fetchStat(struct url *URL, struct url_stat *us, const char *flags)
135 us->atime = us->mtime = 0;
137 if (strcmp(URL->scheme, SCHEME_FILE) == 0)
138 return (fetchStatFile(URL, us, flags));
139 else if (strcmp(URL->scheme, SCHEME_FTP) == 0)
140 return (fetchStatFTP(URL, us, flags));
141 else if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
142 return (fetchStatHTTP(URL, us, flags));
143 else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
144 return (fetchStatHTTP(URL, us, flags));
145 url_seterr(URL_BAD_SCHEME);
150 * Select the appropriate protocol for the URL scheme, and return a
151 * list of files in the directory pointed to by the URL.
154 fetchList(struct url *URL, const char *flags)
157 if (strcmp(URL->scheme, SCHEME_FILE) == 0)
158 return (fetchListFile(URL, flags));
159 else if (strcmp(URL->scheme, SCHEME_FTP) == 0)
160 return (fetchListFTP(URL, flags));
161 else if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
162 return (fetchListHTTP(URL, flags));
163 else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
164 return (fetchListHTTP(URL, flags));
165 url_seterr(URL_BAD_SCHEME);
170 * Attempt to parse the given URL; if successful, call fetchXGet().
173 fetchXGetURL(const char *URL, struct url_stat *us, const char *flags)
178 if ((u = fetchParseURL(URL)) == NULL)
181 f = fetchXGet(u, us, flags);
188 * Attempt to parse the given URL; if successful, call fetchGet().
191 fetchGetURL(const char *URL, const char *flags)
193 return (fetchXGetURL(URL, NULL, flags));
197 * Attempt to parse the given URL; if successful, call fetchPut().
200 fetchPutURL(const char *URL, const char *flags)
205 if ((u = fetchParseURL(URL)) == NULL)
208 f = fetchPut(u, flags);
215 * Attempt to parse the given URL; if successful, call fetchStat().
218 fetchStatURL(const char *URL, struct url_stat *us, const char *flags)
223 if ((u = fetchParseURL(URL)) == NULL)
226 s = fetchStat(u, us, flags);
233 * Attempt to parse the given URL; if successful, call fetchList().
236 fetchListURL(const char *URL, const char *flags)
241 if ((u = fetchParseURL(URL)) == NULL)
244 ue = fetchList(u, flags);
254 fetchMakeURL(const char *scheme, const char *host, int port, const char *doc,
255 const char *user, const char *pwd)
259 if (!scheme || (!host && !doc)) {
260 url_seterr(URL_MALFORMED);
264 if (port < 0 || port > 65535) {
265 url_seterr(URL_BAD_PORT);
269 /* allocate struct url */
270 if ((u = calloc(1, sizeof(*u))) == NULL) {
276 if ((u->doc = strdup(doc ? doc : "/")) == NULL) {
282 #define seturl(x) snprintf(u->x, sizeof(u->x), "%s", x)
294 * Return value of the given hex digit.
297 fetch_hexval(char ch)
300 if (ch >= '0' && ch <= '9')
302 else if (ch >= 'a' && ch <= 'f')
303 return (ch - 'a' + 10);
304 else if (ch >= 'A' && ch <= 'F')
305 return (ch - 'A' + 10);
310 * Decode percent-encoded URL component from src into dst, stopping at end
311 * of string, or at @ or : separators. Returns a pointer to the unhandled
312 * part of the input string (null terminator, @, or :). No terminator is
313 * written to dst (it is the caller's responsibility).
316 fetch_pctdecode(char *dst, const char *src, size_t dlen)
322 for (s = src; *s != '\0' && *s != '@' && *s != ':'; s++) {
323 if (s[0] == '%' && (d1 = fetch_hexval(s[1])) >= 0 &&
324 (d2 = fetch_hexval(s[2])) >= 0 && (d1 > 0 || d2 > 0)) {
327 } else if (s[0] == '%') {
328 /* Invalid escape sequence. */
342 * Split an URL into components. URL syntax is:
343 * [method:/][/[user[:pwd]@]host[:port]/][document]
344 * This almost, but not quite, RFC1738 URL syntax.
347 fetchParseURL(const char *URL)
354 /* allocate struct url */
355 if ((u = calloc(1, sizeof(*u))) == NULL) {
362 if ((p = strstr(URL, ":/"))) {
363 if (p - URL > URL_SCHEMELEN)
365 for (i = 0; URL + i < p; i++)
366 u->scheme[i] = tolower((unsigned char)URL[i]);
369 * Only one slash: no host, leave slash as part of document
370 * Two slashes: host follows, strip slashes
377 if (!*URL || *URL == '/' || *URL == '.' ||
378 (u->scheme[0] == '\0' &&
379 strchr(URL, '/') == NULL && strchr(URL, ':') == NULL))
382 p = strpbrk(URL, "/@");
383 if (p && *p == '@') {
385 q = fetch_pctdecode(u->user, URL, URL_USERLEN);
391 q = fetch_pctdecode(u->pwd, q + 1, URL_PWDLEN);
402 q = p + 1 + strspn(p + 1, ":0123456789ABCDEFabcdef");
406 /* valid characters in a DNS name */
407 q = p + strspn(p, "-." "0123456789"
408 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "_"
409 "abcdefghijklmnopqrstuvwxyz");
411 if ((*q != '\0' && *q != '/' && *q != ':') || q - p > MAXHOSTNAMELEN)
413 for (i = 0; p + i < q; i++)
414 u->host[i] = tolower((unsigned char)p[i]);
420 for (n = 0, q = ++p; *q && (*q != '/'); q++) {
421 if (*q >= '0' && *q <= '9' && n < INT_MAX / 10) {
422 n = n * 10 + (*q - '0');
425 url_seterr(URL_BAD_PORT);
429 if (p != q && (n < 1 || n > IPPORT_MAX))
440 if (strcmp(u->scheme, SCHEME_HTTP) == 0 ||
441 strcmp(u->scheme, SCHEME_HTTPS) == 0) {
442 const char hexnums[] = "0123456789abcdef";
444 /* percent-escape whitespace. */
445 if ((doc = malloc(strlen(p) * 3 + 1)) == NULL) {
451 if (!isspace((unsigned char)*p)) {
455 *doc++ = hexnums[((unsigned int)*p) >> 4];
456 *doc++ = hexnums[((unsigned int)*p) & 0xf];
461 } else if ((u->doc = strdup(p)) == NULL) {
466 DEBUGF("scheme: \"%s\"\n"
471 "document: \"%s\"\n",
472 u->scheme, u->user, u->pwd,
473 u->host, u->port, u->doc);
486 fetchFreeURL(struct url *u)