2 * Copyright (c) 1998 Dag-Erling Coïdan 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: src/lib/libfetch/fetch.c,v 1.10.2.14 2003/06/06 06:45:25 des Exp $
29 * $DragonFly: src/lib/libfetch/fetch.c,v 1.2 2003/06/17 04:26:49 dillon Exp $
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)
79 direct = CHECK_FLAG('d');
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)
115 direct = CHECK_FLAG('d');
116 if (strcasecmp(URL->scheme, SCHEME_FILE) == 0)
117 return (fetchPutFile(URL, flags));
118 else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0)
119 return (fetchPutFTP(URL, flags));
120 else if (strcasecmp(URL->scheme, SCHEME_HTTP) == 0)
121 return (fetchPutHTTP(URL, flags));
122 else if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0)
123 return (fetchPutHTTP(URL, flags));
124 _url_seterr(URL_BAD_SCHEME);
129 * Select the appropriate protocol for the URL scheme, and return the
130 * size of the document referenced by the URL if it exists.
133 fetchStat(struct url *URL, struct url_stat *us, const char *flags)
137 direct = CHECK_FLAG('d');
140 us->atime = us->mtime = 0;
142 if (strcasecmp(URL->scheme, SCHEME_FILE) == 0)
143 return (fetchStatFile(URL, us, flags));
144 else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0)
145 return (fetchStatFTP(URL, us, flags));
146 else if (strcasecmp(URL->scheme, SCHEME_HTTP) == 0)
147 return (fetchStatHTTP(URL, us, flags));
148 else if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0)
149 return (fetchStatHTTP(URL, us, flags));
150 _url_seterr(URL_BAD_SCHEME);
155 * Select the appropriate protocol for the URL scheme, and return a
156 * list of files in the directory pointed to by the URL.
159 fetchList(struct url *URL, const char *flags)
163 direct = CHECK_FLAG('d');
164 if (strcasecmp(URL->scheme, SCHEME_FILE) == 0)
165 return (fetchListFile(URL, flags));
166 else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0)
167 return (fetchListFTP(URL, flags));
168 else if (strcasecmp(URL->scheme, SCHEME_HTTP) == 0)
169 return (fetchListHTTP(URL, flags));
170 else if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0)
171 return (fetchListHTTP(URL, flags));
172 _url_seterr(URL_BAD_SCHEME);
177 * Attempt to parse the given URL; if successful, call fetchXGet().
180 fetchXGetURL(const char *URL, struct url_stat *us, const char *flags)
185 if ((u = fetchParseURL(URL)) == NULL)
188 f = fetchXGet(u, us, flags);
195 * Attempt to parse the given URL; if successful, call fetchGet().
198 fetchGetURL(const char *URL, const char *flags)
200 return (fetchXGetURL(URL, NULL, flags));
204 * Attempt to parse the given URL; if successful, call fetchPut().
207 fetchPutURL(const char *URL, const char *flags)
212 if ((u = fetchParseURL(URL)) == NULL)
215 f = fetchPut(u, flags);
222 * Attempt to parse the given URL; if successful, call fetchStat().
225 fetchStatURL(const char *URL, struct url_stat *us, const char *flags)
230 if ((u = fetchParseURL(URL)) == NULL)
233 s = fetchStat(u, us, flags);
240 * Attempt to parse the given URL; if successful, call fetchList().
243 fetchListURL(const char *URL, const char *flags)
248 if ((u = fetchParseURL(URL)) == NULL)
251 ue = fetchList(u, flags);
261 fetchMakeURL(const char *scheme, const char *host, int port, const char *doc,
262 const char *user, const char *pwd)
266 if (!scheme || (!host && !doc)) {
267 _url_seterr(URL_MALFORMED);
271 if (port < 0 || port > 65535) {
272 _url_seterr(URL_BAD_PORT);
276 /* allocate struct url */
277 if ((u = calloc(1, sizeof(*u))) == NULL) {
282 if ((u->doc = strdup(doc ? doc : "/")) == NULL) {
288 #define seturl(x) snprintf(u->x, sizeof(u->x), "%s", x)
300 * Split an URL into components. URL syntax is:
301 * [method:/][/[user[:pwd]@]host[:port]/][document]
302 * This almost, but not quite, RFC1738 URL syntax.
305 fetchParseURL(const char *URL)
312 /* allocate struct url */
313 if ((u = calloc(1, sizeof(*u))) == NULL) {
319 if ((p = strstr(URL, ":/"))) {
320 snprintf(u->scheme, URL_SCHEMELEN+1,
321 "%.*s", (int)(p - URL), URL);
324 * Only one slash: no host, leave slash as part of document
325 * Two slashes: host follows, strip slashes
332 if (!*URL || *URL == '/' || *URL == '.' ||
333 (u->scheme[0] == '\0' &&
334 strchr(URL, '/') == NULL && strchr(URL, ':') == NULL))
337 p = strpbrk(URL, "/@");
338 if (p && *p == '@') {
340 for (q = URL, i = 0; (*q != ':') && (*q != '@'); q++)
346 for (q++, i = 0; (*q != ':') && (*q != '@'); q++)
357 if (*p == '[' && (q = strchr(p + 1, ']')) != NULL &&
358 (*++q == '\0' || *q == '/' || *q == ':')) {
359 if ((i = q - p - 2) > MAXHOSTNAMELEN)
361 strncpy(u->host, ++p, i);
365 for (i = 0; *p && (*p != '/') && (*p != ':'); p++)
366 if (i < MAXHOSTNAMELEN)
371 for (q = ++p; *q && (*q != '/'); q++)
373 u->port = u->port * 10 + (*q - '0');
376 _url_seterr(URL_BAD_PORT);
387 if (strcasecmp(u->scheme, SCHEME_HTTP) == 0 ||
388 strcasecmp(u->scheme, SCHEME_HTTPS) == 0) {
389 const char hexnums[] = "0123456789abcdef";
391 /* percent-escape whitespace. */
392 if ((doc = malloc(strlen(p) * 3 + 1)) == NULL) {
402 *doc++ = hexnums[((unsigned int)*p) >> 4];
403 *doc++ = hexnums[((unsigned int)*p) & 0xf];
408 } else if ((u->doc = strdup(p)) == NULL) {
413 DEBUG(fprintf(stderr,
420 u->scheme, u->user, u->pwd,
421 u->host, u->port, u->doc));
434 fetchFreeURL(struct url *u)