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.
28 * $FreeBSD: head/lib/libfetch/fetch.c 252375 2013-06-29 15:51:27Z kientzle $
31 #include <sys/param.h>
32 #include <sys/errno.h>
42 auth_t fetchAuthMethod;
44 char fetchLastErrString[MAXERRSTRING];
46 int fetchRestartCalls = 1;
50 /*** Local data **************************************************************/
53 * Error messages for parser errors
55 #define URL_MALFORMED 1
56 #define URL_BAD_SCHEME 2
57 #define URL_BAD_PORT 3
58 static struct fetcherr url_errlist[] = {
59 { URL_MALFORMED, FETCH_URL, "Malformed URL" },
60 { URL_BAD_SCHEME, FETCH_URL, "Invalid URL scheme" },
61 { URL_BAD_PORT, FETCH_URL, "Invalid server port" },
62 { -1, FETCH_UNKNOWN, "Unknown parser error" }
66 /*** Public API **************************************************************/
69 * Select the appropriate protocol for the URL scheme, and return a
70 * read-only stream connected to the document referenced by the URL.
71 * Also fill out the struct url_stat.
74 fetchXGet(struct url *URL, struct url_stat *us, const char *flags)
79 us->atime = us->mtime = 0;
81 if (strcasecmp(URL->scheme, SCHEME_FILE) == 0)
82 return (fetchXGetFile(URL, us, flags));
83 else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0)
84 return (fetchXGetFTP(URL, us, flags));
85 else if (strcasecmp(URL->scheme, SCHEME_HTTP) == 0)
86 return (fetchXGetHTTP(URL, us, flags));
87 else if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0)
88 return (fetchXGetHTTP(URL, us, flags));
89 url_seterr(URL_BAD_SCHEME);
94 * Select the appropriate protocol for the URL scheme, and return a
95 * read-only stream connected to the document referenced by the URL.
98 fetchGet(struct url *URL, const char *flags)
100 return (fetchXGet(URL, NULL, flags));
104 * Select the appropriate protocol for the URL scheme, and return a
105 * write-only stream connected to the document referenced by the URL.
108 fetchPut(struct url *URL, const char *flags)
111 if (strcasecmp(URL->scheme, SCHEME_FILE) == 0)
112 return (fetchPutFile(URL, flags));
113 else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0)
114 return (fetchPutFTP(URL, flags));
115 else if (strcasecmp(URL->scheme, SCHEME_HTTP) == 0)
116 return (fetchPutHTTP(URL, flags));
117 else if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0)
118 return (fetchPutHTTP(URL, flags));
119 url_seterr(URL_BAD_SCHEME);
124 * Select the appropriate protocol for the URL scheme, and return the
125 * size of the document referenced by the URL if it exists.
128 fetchStat(struct url *URL, struct url_stat *us, const char *flags)
133 us->atime = us->mtime = 0;
135 if (strcasecmp(URL->scheme, SCHEME_FILE) == 0)
136 return (fetchStatFile(URL, us, flags));
137 else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0)
138 return (fetchStatFTP(URL, us, flags));
139 else if (strcasecmp(URL->scheme, SCHEME_HTTP) == 0)
140 return (fetchStatHTTP(URL, us, flags));
141 else if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0)
142 return (fetchStatHTTP(URL, us, flags));
143 url_seterr(URL_BAD_SCHEME);
148 * Select the appropriate protocol for the URL scheme, and return a
149 * list of files in the directory pointed to by the URL.
152 fetchList(struct url *URL, const char *flags)
155 if (strcasecmp(URL->scheme, SCHEME_FILE) == 0)
156 return (fetchListFile(URL, flags));
157 else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0)
158 return (fetchListFTP(URL, flags));
159 else if (strcasecmp(URL->scheme, SCHEME_HTTP) == 0)
160 return (fetchListHTTP(URL, flags));
161 else if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0)
162 return (fetchListHTTP(URL, flags));
163 url_seterr(URL_BAD_SCHEME);
168 * Attempt to parse the given URL; if successful, call fetchXGet().
171 fetchXGetURL(const char *URL, struct url_stat *us, const char *flags)
176 if ((u = fetchParseURL(URL)) == NULL)
179 f = fetchXGet(u, us, flags);
186 * Attempt to parse the given URL; if successful, call fetchGet().
189 fetchGetURL(const char *URL, const char *flags)
191 return (fetchXGetURL(URL, NULL, flags));
195 * Attempt to parse the given URL; if successful, call fetchPut().
198 fetchPutURL(const char *URL, const char *flags)
203 if ((u = fetchParseURL(URL)) == NULL)
206 f = fetchPut(u, flags);
213 * Attempt to parse the given URL; if successful, call fetchStat().
216 fetchStatURL(const char *URL, struct url_stat *us, const char *flags)
221 if ((u = fetchParseURL(URL)) == NULL)
224 s = fetchStat(u, us, flags);
231 * Attempt to parse the given URL; if successful, call fetchList().
234 fetchListURL(const char *URL, const char *flags)
239 if ((u = fetchParseURL(URL)) == NULL)
242 ue = fetchList(u, flags);
252 fetchMakeURL(const char *scheme, const char *host, int port, const char *doc,
253 const char *user, const char *pwd)
257 if (!scheme || (!host && !doc)) {
258 url_seterr(URL_MALFORMED);
262 if (port < 0 || port > 65535) {
263 url_seterr(URL_BAD_PORT);
267 /* allocate struct url */
268 if ((u = calloc(1, sizeof(*u))) == NULL) {
273 if ((u->doc = strdup(doc ? doc : "/")) == NULL) {
279 #define seturl(x) snprintf(u->x, sizeof(u->x), "%s", x)
291 * Return value of the given hex digit.
294 fetch_hexval(char ch)
297 if (ch >= '0' && ch <= '9')
299 else if (ch >= 'a' && ch <= 'f')
300 return (ch - 'a' + 10);
301 else if (ch >= 'A' && ch <= 'F')
302 return (ch - 'A' + 10);
307 * Decode percent-encoded URL component from src into dst, stopping at end
308 * of string, or at @ or : separators. Returns a pointer to the unhandled
309 * part of the input string (null terminator, @, or :). No terminator is
310 * written to dst (it is the caller's responsibility).
313 fetch_pctdecode(char *dst, const char *src, size_t dlen)
319 for (s = src; *s != '\0' && *s != '@' && *s != ':'; s++) {
320 if (s[0] == '%' && (d1 = fetch_hexval(s[1])) >= 0 &&
321 (d2 = fetch_hexval(s[2])) >= 0 && (d1 > 0 || d2 > 0)) {
334 * Split an URL into components. URL syntax is:
335 * [method:/][/[user[:pwd]@]host[:port]/][document]
336 * This almost, but not quite, RFC1738 URL syntax.
339 fetchParseURL(const char *URL)
346 /* allocate struct url */
347 if ((u = calloc(1, sizeof(*u))) == NULL) {
353 if ((p = strstr(URL, ":/"))) {
354 snprintf(u->scheme, URL_SCHEMELEN+1,
355 "%.*s", (int)(p - URL), URL);
358 * Only one slash: no host, leave slash as part of document
359 * Two slashes: host follows, strip slashes
366 if (!*URL || *URL == '/' || *URL == '.' ||
367 (u->scheme[0] == '\0' &&
368 strchr(URL, '/') == NULL && strchr(URL, ':') == NULL))
371 p = strpbrk(URL, "/@");
372 if (p && *p == '@') {
374 q = fetch_pctdecode(u->user, URL, URL_USERLEN);
378 q = fetch_pctdecode(u->pwd, q + 1, URL_PWDLEN);
387 if (*p == '[' && (q = strchr(p + 1, ']')) != NULL &&
388 (*++q == '\0' || *q == '/' || *q == ':')) {
389 if ((i = q - p - 2) > MAXHOSTNAMELEN)
391 strncpy(u->host, ++p, i);
395 for (i = 0; *p && (*p != '/') && (*p != ':'); p++)
396 if (i < MAXHOSTNAMELEN)
401 for (q = ++p; *q && (*q != '/'); q++)
402 if (isdigit((unsigned char)*q))
403 u->port = u->port * 10 + (*q - '0');
406 url_seterr(URL_BAD_PORT);
417 if (strcasecmp(u->scheme, SCHEME_HTTP) == 0 ||
418 strcasecmp(u->scheme, SCHEME_HTTPS) == 0) {
419 const char hexnums[] = "0123456789abcdef";
421 /* percent-escape whitespace. */
422 if ((doc = malloc(strlen(p) * 3 + 1)) == NULL) {
428 if (!isspace((unsigned char)*p)) {
432 *doc++ = hexnums[((unsigned int)*p) >> 4];
433 *doc++ = hexnums[((unsigned int)*p) & 0xf];
438 } else if ((u->doc = strdup(p)) == NULL) {
443 DEBUG(fprintf(stderr,
450 u->scheme, u->user, u->pwd,
451 u->host, u->port, u->doc));
464 fetchFreeURL(struct url *u)