2 * Copyright (c) 1998-2004 Dag-Erling 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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/errno.h>
43 auth_t fetchAuthMethod;
45 char fetchLastErrString[MAXERRSTRING];
47 int fetchRestartCalls = 1;
51 /*** Local data **************************************************************/
54 * Error messages for parser errors
56 #define URL_MALFORMED 1
57 #define URL_BAD_SCHEME 2
58 #define URL_BAD_PORT 3
59 static struct fetcherr url_errlist[] = {
60 { URL_MALFORMED, FETCH_URL, "Malformed URL" },
61 { URL_BAD_SCHEME, FETCH_URL, "Invalid URL scheme" },
62 { URL_BAD_PORT, FETCH_URL, "Invalid server port" },
63 { -1, FETCH_UNKNOWN, "Unknown parser error" }
67 /*** Public API **************************************************************/
70 * Select the appropriate protocol for the URL scheme, and return a
71 * read-only stream connected to the document referenced by the URL.
72 * Also fill out the struct url_stat.
75 fetchXGet(struct url *URL, struct url_stat *us, const char *flags)
80 us->atime = us->mtime = 0;
82 if (strcasecmp(URL->scheme, SCHEME_FILE) == 0)
83 return (fetchXGetFile(URL, us, flags));
84 else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0)
85 return (fetchXGetFTP(URL, us, flags));
86 else if (strcasecmp(URL->scheme, SCHEME_HTTP) == 0)
87 return (fetchXGetHTTP(URL, us, flags));
88 else if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0)
89 return (fetchXGetHTTP(URL, us, flags));
90 url_seterr(URL_BAD_SCHEME);
95 * Select the appropriate protocol for the URL scheme, and return a
96 * read-only stream connected to the document referenced by the URL.
99 fetchGet(struct url *URL, const char *flags)
101 return (fetchXGet(URL, NULL, flags));
105 * Select the appropriate protocol for the URL scheme, and return a
106 * write-only stream connected to the document referenced by the URL.
109 fetchPut(struct url *URL, const char *flags)
112 if (strcasecmp(URL->scheme, SCHEME_FILE) == 0)
113 return (fetchPutFile(URL, flags));
114 else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0)
115 return (fetchPutFTP(URL, flags));
116 else if (strcasecmp(URL->scheme, SCHEME_HTTP) == 0)
117 return (fetchPutHTTP(URL, flags));
118 else if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0)
119 return (fetchPutHTTP(URL, flags));
120 url_seterr(URL_BAD_SCHEME);
125 * Select the appropriate protocol for the URL scheme, and return the
126 * size of the document referenced by the URL if it exists.
129 fetchStat(struct url *URL, struct url_stat *us, const char *flags)
134 us->atime = us->mtime = 0;
136 if (strcasecmp(URL->scheme, SCHEME_FILE) == 0)
137 return (fetchStatFile(URL, us, flags));
138 else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0)
139 return (fetchStatFTP(URL, us, flags));
140 else if (strcasecmp(URL->scheme, SCHEME_HTTP) == 0)
141 return (fetchStatHTTP(URL, us, flags));
142 else if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0)
143 return (fetchStatHTTP(URL, us, flags));
144 url_seterr(URL_BAD_SCHEME);
149 * Select the appropriate protocol for the URL scheme, and return a
150 * list of files in the directory pointed to by the URL.
153 fetchList(struct url *URL, const char *flags)
156 if (strcasecmp(URL->scheme, SCHEME_FILE) == 0)
157 return (fetchListFile(URL, flags));
158 else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0)
159 return (fetchListFTP(URL, flags));
160 else if (strcasecmp(URL->scheme, SCHEME_HTTP) == 0)
161 return (fetchListHTTP(URL, flags));
162 else if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0)
163 return (fetchListHTTP(URL, flags));
164 url_seterr(URL_BAD_SCHEME);
169 * Attempt to parse the given URL; if successful, call fetchXGet().
172 fetchXGetURL(const char *URL, struct url_stat *us, const char *flags)
177 if ((u = fetchParseURL(URL)) == NULL)
180 f = fetchXGet(u, us, flags);
187 * Attempt to parse the given URL; if successful, call fetchGet().
190 fetchGetURL(const char *URL, const char *flags)
192 return (fetchXGetURL(URL, NULL, flags));
196 * Attempt to parse the given URL; if successful, call fetchPut().
199 fetchPutURL(const char *URL, const char *flags)
204 if ((u = fetchParseURL(URL)) == NULL)
207 f = fetchPut(u, flags);
214 * Attempt to parse the given URL; if successful, call fetchStat().
217 fetchStatURL(const char *URL, struct url_stat *us, const char *flags)
222 if ((u = fetchParseURL(URL)) == NULL)
225 s = fetchStat(u, us, flags);
232 * Attempt to parse the given URL; if successful, call fetchList().
235 fetchListURL(const char *URL, const char *flags)
240 if ((u = fetchParseURL(URL)) == NULL)
243 ue = fetchList(u, flags);
253 fetchMakeURL(const char *scheme, const char *host, int port, const char *doc,
254 const char *user, const char *pwd)
258 if (!scheme || (!host && !doc)) {
259 url_seterr(URL_MALFORMED);
263 if (port < 0 || port > 65535) {
264 url_seterr(URL_BAD_PORT);
268 /* allocate struct url */
269 if ((u = calloc(1, sizeof(*u))) == NULL) {
274 if ((u->doc = strdup(doc ? doc : "/")) == NULL) {
280 #define seturl(x) snprintf(u->x, sizeof(u->x), "%s", x)
293 * Return value of the given hex digit.
296 fetch_hexval(char ch)
299 if (ch >= '0' && ch <= '9')
301 else if (ch >= 'a' && ch <= 'f')
302 return (ch - 'a' + 10);
303 else if (ch >= 'A' && ch <= 'F')
304 return (ch - 'A' + 10);
309 * Decode percent-encoded URL component from src into dst, stopping at end
310 * of string, or at @ or : separators. Returns a pointer to the unhandled
311 * part of the input string (null terminator, @, or :). No terminator is
312 * written to dst (it is the caller's responsibility).
315 fetch_pctdecode(char *dst, const char *src, size_t dlen)
321 for (s = src; *s != '\0' && *s != '@' && *s != ':'; s++) {
322 if (s[0] == '%' && (d1 = fetch_hexval(s[1])) >= 0 &&
323 (d2 = fetch_hexval(s[2])) >= 0 && (d1 > 0 || d2 > 0)) {
336 * Split an URL into components. URL syntax is:
337 * [method:/][/[user[:pwd]@]host[:port]/][document]
338 * This almost, but not quite, RFC1738 URL syntax.
341 fetchParseURL(const char *URL)
348 /* allocate struct url */
349 if ((u = calloc(1, sizeof(*u))) == NULL) {
356 if ((p = strstr(URL, ":/"))) {
357 snprintf(u->scheme, URL_SCHEMELEN+1,
358 "%.*s", (int)(p - URL), URL);
361 * Only one slash: no host, leave slash as part of document
362 * Two slashes: host follows, strip slashes
369 if (!*URL || *URL == '/' || *URL == '.' ||
370 (u->scheme[0] == '\0' &&
371 strchr(URL, '/') == NULL && strchr(URL, ':') == NULL))
374 p = strpbrk(URL, "/@");
375 if (p && *p == '@') {
377 q = fetch_pctdecode(u->user, URL, URL_USERLEN);
381 q = fetch_pctdecode(u->pwd, q + 1, URL_PWDLEN);
389 if (*p == '[' && (q = strchr(p + 1, ']')) != NULL &&
390 (*++q == '\0' || *q == '/' || *q == ':')) {
391 if ((i = q - p) > MAXHOSTNAMELEN)
393 strncpy(u->host, p, i);
396 for (i = 0; *p && (*p != '/') && (*p != ':'); p++)
397 if (i < MAXHOSTNAMELEN)
403 for (q = ++p; *q && (*q != '/'); q++)
404 if (isdigit((unsigned char)*q))
405 u->port = u->port * 10 + (*q - '0');
408 url_seterr(URL_BAD_PORT);
419 if (strcasecmp(u->scheme, SCHEME_HTTP) == 0 ||
420 strcasecmp(u->scheme, SCHEME_HTTPS) == 0) {
421 const char hexnums[] = "0123456789abcdef";
423 /* percent-escape whitespace. */
424 if ((doc = malloc(strlen(p) * 3 + 1)) == NULL) {
430 if (!isspace((unsigned char)*p)) {
434 *doc++ = hexnums[((unsigned int)*p) >> 4];
435 *doc++ = hexnums[((unsigned int)*p) & 0xf];
440 } else if ((u->doc = strdup(p)) == NULL) {
445 DEBUG(fprintf(stderr,
451 "document: \"%s\"\n",
452 u->scheme, u->user, u->pwd,
453 u->host, u->port, u->doc));
466 fetchFreeURL(struct url *u)