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.
30 * $FreeBSD: head/lib/libfetch/fetch.c 341013 2018-11-27 10:45:14Z des $
33 #include <sys/param.h>
35 #include <netinet/in.h>
46 auth_t fetchAuthMethod;
48 char fetchLastErrString[MAXERRSTRING];
50 int fetchRestartCalls = 1;
54 /*** Local data **************************************************************/
57 * Error messages for parser errors
59 #define URL_MALFORMED 1
60 #define URL_BAD_SCHEME 2
61 #define URL_BAD_PORT 3
62 static struct fetcherr url_errlist[] = {
63 { URL_MALFORMED, FETCH_URL, "Malformed URL" },
64 { URL_BAD_SCHEME, FETCH_URL, "Invalid URL scheme" },
65 { URL_BAD_PORT, FETCH_URL, "Invalid server port" },
66 { -1, FETCH_UNKNOWN, "Unknown parser error" }
70 /*** Public API **************************************************************/
73 * Select the appropriate protocol for the URL scheme, and return a
74 * read-only stream connected to the document referenced by the URL.
75 * Also fill out the struct url_stat.
78 fetchXGet(struct url *URL, struct url_stat *us, const char *flags)
83 us->atime = us->mtime = 0;
85 if (strcmp(URL->scheme, SCHEME_FILE) == 0)
86 return (fetchXGetFile(URL, us, flags));
87 else if (strcmp(URL->scheme, SCHEME_FTP) == 0)
88 return (fetchXGetFTP(URL, us, flags));
89 else if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
90 return (fetchXGetHTTP(URL, us, flags));
91 else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
92 return (fetchXGetHTTP(URL, us, flags));
93 url_seterr(URL_BAD_SCHEME);
98 * Select the appropriate protocol for the URL scheme, and return a
99 * read-only stream connected to the document referenced by the URL.
102 fetchGet(struct url *URL, const char *flags)
104 return (fetchXGet(URL, NULL, flags));
108 * Select the appropriate protocol for the URL scheme, and return a
109 * write-only stream connected to the document referenced by the URL.
112 fetchPut(struct url *URL, const char *flags)
115 if (strcmp(URL->scheme, SCHEME_FILE) == 0)
116 return (fetchPutFile(URL, flags));
117 else if (strcmp(URL->scheme, SCHEME_FTP) == 0)
118 return (fetchPutFTP(URL, flags));
119 else if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
120 return (fetchPutHTTP(URL, flags));
121 else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
122 return (fetchPutHTTP(URL, flags));
123 url_seterr(URL_BAD_SCHEME);
128 * Select the appropriate protocol for the URL scheme, and return the
129 * size of the document referenced by the URL if it exists.
132 fetchStat(struct url *URL, struct url_stat *us, const char *flags)
137 us->atime = us->mtime = 0;
139 if (strcmp(URL->scheme, SCHEME_FILE) == 0)
140 return (fetchStatFile(URL, us, flags));
141 else if (strcmp(URL->scheme, SCHEME_FTP) == 0)
142 return (fetchStatFTP(URL, us, flags));
143 else if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
144 return (fetchStatHTTP(URL, us, flags));
145 else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
146 return (fetchStatHTTP(URL, us, flags));
147 url_seterr(URL_BAD_SCHEME);
152 * Select the appropriate protocol for the URL scheme, and return a
153 * list of files in the directory pointed to by the URL.
156 fetchList(struct url *URL, const char *flags)
159 if (strcmp(URL->scheme, SCHEME_FILE) == 0)
160 return (fetchListFile(URL, flags));
161 else if (strcmp(URL->scheme, SCHEME_FTP) == 0)
162 return (fetchListFTP(URL, flags));
163 else if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
164 return (fetchListHTTP(URL, flags));
165 else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
166 return (fetchListHTTP(URL, flags));
167 url_seterr(URL_BAD_SCHEME);
172 * Attempt to parse the given URL; if successful, call fetchXGet().
175 fetchXGetURL(const char *URL, struct url_stat *us, const char *flags)
180 if ((u = fetchParseURL(URL)) == NULL)
183 f = fetchXGet(u, us, flags);
190 * Attempt to parse the given URL; if successful, call fetchGet().
193 fetchGetURL(const char *URL, const char *flags)
195 return (fetchXGetURL(URL, NULL, flags));
199 * Attempt to parse the given URL; if successful, call fetchPut().
202 fetchPutURL(const char *URL, const char *flags)
207 if ((u = fetchParseURL(URL)) == NULL)
210 f = fetchPut(u, flags);
217 * Attempt to parse the given URL; if successful, call fetchStat().
220 fetchStatURL(const char *URL, struct url_stat *us, const char *flags)
225 if ((u = fetchParseURL(URL)) == NULL)
228 s = fetchStat(u, us, flags);
235 * Attempt to parse the given URL; if successful, call fetchList().
238 fetchListURL(const char *URL, const char *flags)
243 if ((u = fetchParseURL(URL)) == NULL)
246 ue = fetchList(u, flags);
256 fetchMakeURL(const char *scheme, const char *host, int port, const char *doc,
257 const char *user, const char *pwd)
261 if (!scheme || (!host && !doc)) {
262 url_seterr(URL_MALFORMED);
266 if (port < 0 || port > 65535) {
267 url_seterr(URL_BAD_PORT);
271 /* allocate struct url */
272 if ((u = calloc(1, sizeof(*u))) == NULL) {
278 if ((u->doc = strdup(doc ? doc : "/")) == NULL) {
284 #define seturl(x) snprintf(u->x, sizeof(u->x), "%s", x)
296 * Return value of the given hex digit.
299 fetch_hexval(char ch)
302 if (ch >= '0' && ch <= '9')
304 else if (ch >= 'a' && ch <= 'f')
305 return (ch - 'a' + 10);
306 else if (ch >= 'A' && ch <= 'F')
307 return (ch - 'A' + 10);
312 * Decode percent-encoded URL component from src into dst, stopping at end
313 * of string, or at @ or : separators. Returns a pointer to the unhandled
314 * part of the input string (null terminator, @, or :). No terminator is
315 * written to dst (it is the caller's responsibility).
318 fetch_pctdecode(char *dst, const char *src, size_t dlen)
324 for (s = src; *s != '\0' && *s != '@' && *s != ':'; s++) {
325 if (s[0] == '%' && (d1 = fetch_hexval(s[1])) >= 0 &&
326 (d2 = fetch_hexval(s[2])) >= 0 && (d1 > 0 || d2 > 0)) {
341 * Split an URL into components. URL syntax is:
342 * [method:/][/[user[:pwd]@]host[:port]/][document]
343 * This almost, but not quite, RFC1738 URL syntax.
346 fetchParseURL(const char *URL)
353 /* allocate struct url */
354 if ((u = calloc(1, sizeof(*u))) == NULL) {
361 if ((p = strstr(URL, ":/"))) {
362 if (p - URL > URL_SCHEMELEN)
364 for (i = 0; URL + i < p; i++)
365 u->scheme[i] = tolower((unsigned char)URL[i]);
368 * Only one slash: no host, leave slash as part of document
369 * Two slashes: host follows, strip slashes
376 if (!*URL || *URL == '/' || *URL == '.' ||
377 (u->scheme[0] == '\0' &&
378 strchr(URL, '/') == NULL && strchr(URL, ':') == NULL))
381 p = strpbrk(URL, "/@");
382 if (p && *p == '@') {
384 q = fetch_pctdecode(u->user, URL, URL_USERLEN);
390 q = fetch_pctdecode(u->pwd, q + 1, URL_PWDLEN);
401 q = p + 1 + strspn(p + 1, ":0123456789ABCDEFabcdef");
405 /* valid characters in a DNS name */
406 q = p + strspn(p, "-." "0123456789"
407 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "_"
408 "abcdefghijklmnopqrstuvwxyz");
410 if ((*q != '\0' && *q != '/' && *q != ':') || q - p > MAXHOSTNAMELEN)
412 for (i = 0; p + i < q; i++)
413 u->host[i] = tolower((unsigned char)p[i]);
419 for (n = 0, q = ++p; *q && (*q != '/'); q++) {
420 if (*q >= '0' && *q <= '9' && n < INT_MAX / 10) {
421 n = n * 10 + (*q - '0');
424 url_seterr(URL_BAD_PORT);
428 if (n < 1 || n > IPPORT_MAX)
439 if (strcmp(u->scheme, SCHEME_HTTP) == 0 ||
440 strcmp(u->scheme, SCHEME_HTTPS) == 0) {
441 const char hexnums[] = "0123456789abcdef";
443 /* percent-escape whitespace. */
444 if ((doc = malloc(strlen(p) * 3 + 1)) == NULL) {
450 if (!isspace((unsigned char)*p)) {
454 *doc++ = hexnums[((unsigned int)*p) >> 4];
455 *doc++ = hexnums[((unsigned int)*p) & 0xf];
460 } else if ((u->doc = strdup(p)) == NULL) {
465 DEBUGF("scheme: \"%s\"\n"
470 "document: \"%s\"\n",
471 u->scheme, u->user, u->pwd,
472 u->host, u->port, u->doc);
485 fetchFreeURL(struct url *u)