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>
35 #include <sys/errno.h>
45 auth_t fetchAuthMethod;
47 char fetchLastErrString[MAXERRSTRING];
49 int fetchRestartCalls = 1;
53 /*** Local data **************************************************************/
56 * Error messages for parser errors
58 #define URL_MALFORMED 1
59 #define URL_BAD_SCHEME 2
60 #define URL_BAD_PORT 3
61 static struct fetcherr url_errlist[] = {
62 { URL_MALFORMED, FETCH_URL, "Malformed URL" },
63 { URL_BAD_SCHEME, FETCH_URL, "Invalid URL scheme" },
64 { URL_BAD_PORT, FETCH_URL, "Invalid server port" },
65 { -1, FETCH_UNKNOWN, "Unknown parser error" }
69 /*** Public API **************************************************************/
72 * Select the appropriate protocol for the URL scheme, and return a
73 * read-only stream connected to the document referenced by the URL.
74 * Also fill out the struct url_stat.
77 fetchXGet(struct url *URL, struct url_stat *us, const char *flags)
82 us->atime = us->mtime = 0;
84 if (strcasecmp(URL->scheme, SCHEME_FILE) == 0)
85 return (fetchXGetFile(URL, us, flags));
86 else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0)
87 return (fetchXGetFTP(URL, us, flags));
88 else if (strcasecmp(URL->scheme, SCHEME_HTTP) == 0)
89 return (fetchXGetHTTP(URL, us, flags));
90 else if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0)
91 return (fetchXGetHTTP(URL, us, flags));
92 url_seterr(URL_BAD_SCHEME);
97 * Select the appropriate protocol for the URL scheme, and return a
98 * read-only stream connected to the document referenced by the URL.
101 fetchGet(struct url *URL, const char *flags)
103 return (fetchXGet(URL, NULL, flags));
107 * Select the appropriate protocol for the URL scheme, and return a
108 * write-only stream connected to the document referenced by the URL.
111 fetchPut(struct url *URL, const char *flags)
114 if (strcasecmp(URL->scheme, SCHEME_FILE) == 0)
115 return (fetchPutFile(URL, flags));
116 else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0)
117 return (fetchPutFTP(URL, flags));
118 else if (strcasecmp(URL->scheme, SCHEME_HTTP) == 0)
119 return (fetchPutHTTP(URL, flags));
120 else if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0)
121 return (fetchPutHTTP(URL, flags));
122 url_seterr(URL_BAD_SCHEME);
127 * Select the appropriate protocol for the URL scheme, and return the
128 * size of the document referenced by the URL if it exists.
131 fetchStat(struct url *URL, struct url_stat *us, const char *flags)
136 us->atime = us->mtime = 0;
138 if (strcasecmp(URL->scheme, SCHEME_FILE) == 0)
139 return (fetchStatFile(URL, us, flags));
140 else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0)
141 return (fetchStatFTP(URL, us, flags));
142 else if (strcasecmp(URL->scheme, SCHEME_HTTP) == 0)
143 return (fetchStatHTTP(URL, us, flags));
144 else if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0)
145 return (fetchStatHTTP(URL, us, flags));
146 url_seterr(URL_BAD_SCHEME);
151 * Select the appropriate protocol for the URL scheme, and return a
152 * list of files in the directory pointed to by the URL.
155 fetchList(struct url *URL, const char *flags)
158 if (strcasecmp(URL->scheme, SCHEME_FILE) == 0)
159 return (fetchListFile(URL, flags));
160 else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0)
161 return (fetchListFTP(URL, flags));
162 else if (strcasecmp(URL->scheme, SCHEME_HTTP) == 0)
163 return (fetchListHTTP(URL, flags));
164 else if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0)
165 return (fetchListHTTP(URL, flags));
166 url_seterr(URL_BAD_SCHEME);
171 * Attempt to parse the given URL; if successful, call fetchXGet().
174 fetchXGetURL(const char *URL, struct url_stat *us, const char *flags)
179 if ((u = fetchParseURL(URL)) == NULL)
182 f = fetchXGet(u, us, flags);
189 * Attempt to parse the given URL; if successful, call fetchGet().
192 fetchGetURL(const char *URL, const char *flags)
194 return (fetchXGetURL(URL, NULL, flags));
198 * Attempt to parse the given URL; if successful, call fetchPut().
201 fetchPutURL(const char *URL, const char *flags)
206 if ((u = fetchParseURL(URL)) == NULL)
209 f = fetchPut(u, flags);
216 * Attempt to parse the given URL; if successful, call fetchStat().
219 fetchStatURL(const char *URL, struct url_stat *us, const char *flags)
224 if ((u = fetchParseURL(URL)) == NULL)
227 s = fetchStat(u, us, flags);
234 * Attempt to parse the given URL; if successful, call fetchList().
237 fetchListURL(const char *URL, const char *flags)
242 if ((u = fetchParseURL(URL)) == NULL)
245 ue = fetchList(u, flags);
255 fetchMakeURL(const char *scheme, const char *host, int port, const char *doc,
256 const char *user, const char *pwd)
260 if (!scheme || (!host && !doc)) {
261 url_seterr(URL_MALFORMED);
265 if (port < 0 || port > 65535) {
266 url_seterr(URL_BAD_PORT);
270 /* allocate struct url */
271 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)
295 * Return value of the given hex digit.
298 fetch_hexval(char ch)
301 if (ch >= '0' && ch <= '9')
303 else if (ch >= 'a' && ch <= 'f')
304 return (ch - 'a' + 10);
305 else if (ch >= 'A' && ch <= 'F')
306 return (ch - 'A' + 10);
311 * Decode percent-encoded URL component from src into dst, stopping at end
312 * of string, or at @ or : separators. Returns a pointer to the unhandled
313 * part of the input string (null terminator, @, or :). No terminator is
314 * written to dst (it is the caller's responsibility).
317 fetch_pctdecode(char *dst, const char *src, size_t dlen)
323 for (s = src; *s != '\0' && *s != '@' && *s != ':'; s++) {
324 if (s[0] == '%' && (d1 = fetch_hexval(s[1])) >= 0 &&
325 (d2 = fetch_hexval(s[2])) >= 0 && (d1 > 0 || d2 > 0)) {
338 * Split an URL into components. URL syntax is:
339 * [method:/][/[user[:pwd]@]host[:port]/][document]
340 * This almost, but not quite, RFC1738 URL syntax.
343 fetchParseURL(const char *URL)
350 /* allocate struct url */
351 if ((u = calloc(1, sizeof(*u))) == NULL) {
358 if ((p = strstr(URL, ":/"))) {
359 snprintf(u->scheme, URL_SCHEMELEN+1,
360 "%.*s", (int)(p - URL), URL);
363 * Only one slash: no host, leave slash as part of document
364 * Two slashes: host follows, strip slashes
371 if (!*URL || *URL == '/' || *URL == '.' ||
372 (u->scheme[0] == '\0' &&
373 strchr(URL, '/') == NULL && strchr(URL, ':') == NULL))
376 p = strpbrk(URL, "/@");
377 if (p && *p == '@') {
379 q = fetch_pctdecode(u->user, URL, URL_USERLEN);
383 q = fetch_pctdecode(u->pwd, q + 1, URL_PWDLEN);
391 if (*p == '[' && (q = strchr(p + 1, ']')) != NULL &&
392 (*++q == '\0' || *q == '/' || *q == ':')) {
393 if ((i = q - p) > MAXHOSTNAMELEN)
395 strncpy(u->host, p, i);
398 for (i = 0; *p && (*p != '/') && (*p != ':'); p++)
399 if (i < MAXHOSTNAMELEN)
405 for (q = ++p; *q && (*q != '/'); q++)
406 if (isdigit((unsigned char)*q))
407 u->port = u->port * 10 + (*q - '0');
410 url_seterr(URL_BAD_PORT);
421 if (strcasecmp(u->scheme, SCHEME_HTTP) == 0 ||
422 strcasecmp(u->scheme, SCHEME_HTTPS) == 0) {
423 const char hexnums[] = "0123456789abcdef";
425 /* percent-escape whitespace. */
426 if ((doc = malloc(strlen(p) * 3 + 1)) == NULL) {
432 if (!isspace((unsigned char)*p)) {
436 *doc++ = hexnums[((unsigned int)*p) >> 4];
437 *doc++ = hexnums[((unsigned int)*p) & 0xf];
442 } else if ((u->doc = strdup(p)) == NULL) {
447 DEBUG(fprintf(stderr,
453 "document: \"%s\"\n",
454 u->scheme, u->user, u->pwd,
455 u->host, u->port, u->doc));
468 fetchFreeURL(struct url *u)