2 * Copyright (c) 1998-2004 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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: src/lib/libfetch/fetch.c,v 1.37.6.1 2006/11/11 00:16:07 des Exp $");
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
)
81 us
->atime
= us
->mtime
= 0;
83 if (strcasecmp(URL
->scheme
, SCHEME_FILE
) == 0)
84 return (fetchXGetFile(URL
, us
, flags
));
85 else if (strcasecmp(URL
->scheme
, SCHEME_FTP
) == 0)
86 return (fetchXGetFTP(URL
, us
, flags
));
87 else if (strcasecmp(URL
->scheme
, SCHEME_HTTP
) == 0)
88 return (fetchXGetHTTP(URL
, us
, flags
));
89 else if (strcasecmp(URL
->scheme
, SCHEME_HTTPS
) == 0)
90 return (fetchXGetHTTP(URL
, us
, flags
));
91 _url_seterr(URL_BAD_SCHEME
);
96 * Select the appropriate protocol for the URL scheme, and return a
97 * read-only stream connected to the document referenced by the URL.
100 fetchGet(struct url
*URL
, const char *flags
)
102 return (fetchXGet(URL
, NULL
, flags
));
106 * Select the appropriate protocol for the URL scheme, and return a
107 * write-only stream connected to the document referenced by the URL.
110 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
)
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
)
154 if (strcasecmp(URL
->scheme
, SCHEME_FILE
) == 0)
155 return (fetchListFile(URL
, flags
));
156 else if (strcasecmp(URL
->scheme
, SCHEME_FTP
) == 0)
157 return (fetchListFTP(URL
, flags
));
158 else if (strcasecmp(URL
->scheme
, SCHEME_HTTP
) == 0)
159 return (fetchListHTTP(URL
, flags
));
160 else if (strcasecmp(URL
->scheme
, SCHEME_HTTPS
) == 0)
161 return (fetchListHTTP(URL
, flags
));
162 _url_seterr(URL_BAD_SCHEME
);
167 * Attempt to parse the given URL; if successful, call fetchXGet().
170 fetchXGetURL(const char *URL
, struct url_stat
*us
, const char *flags
)
175 if ((u
= fetchParseURL(URL
)) == NULL
)
178 f
= fetchXGet(u
, us
, flags
);
185 * Attempt to parse the given URL; if successful, call fetchGet().
188 fetchGetURL(const char *URL
, const char *flags
)
190 return (fetchXGetURL(URL
, NULL
, flags
));
194 * Attempt to parse the given URL; if successful, call fetchPut().
197 fetchPutURL(const char *URL
, const char *flags
)
202 if ((u
= fetchParseURL(URL
)) == NULL
)
205 f
= fetchPut(u
, flags
);
212 * Attempt to parse the given URL; if successful, call fetchStat().
215 fetchStatURL(const char *URL
, struct url_stat
*us
, const char *flags
)
220 if ((u
= fetchParseURL(URL
)) == NULL
)
223 s
= fetchStat(u
, us
, flags
);
230 * Attempt to parse the given URL; if successful, call fetchList().
233 fetchListURL(const char *URL
, const char *flags
)
238 if ((u
= fetchParseURL(URL
)) == NULL
)
241 ue
= fetchList(u
, flags
);
251 fetchMakeURL(const char *scheme
, const char *host
, int port
, const char *doc
,
252 const char *user
, const char *pwd
)
256 if (!scheme
|| (!host
&& !doc
)) {
257 _url_seterr(URL_MALFORMED
);
261 if (port
< 0 || port
> 65535) {
262 _url_seterr(URL_BAD_PORT
);
266 /* allocate struct url */
267 if ((u
= calloc(1, sizeof(*u
))) == NULL
) {
272 if ((u
->doc
= strdup(doc
? doc
: "/")) == NULL
) {
278 #define seturl(x) snprintf(u->x, sizeof(u->x), "%s", x)
290 * Split an URL into components. URL syntax is:
291 * [method:/][/[user[:pwd]@]host[:port]/][document]
292 * This almost, but not quite, RFC1738 URL syntax.
295 fetchParseURL(const char *URL
)
302 /* allocate struct url */
303 if ((u
= calloc(1, sizeof(*u
))) == NULL
) {
309 if ((p
= strstr(URL
, ":/")) != NULL
) {
310 snprintf(u
->scheme
, URL_SCHEMELEN
+1,
311 "%.*s", (int)(p
- URL
), URL
);
314 * Only one slash: no host, leave slash as part of document
315 * Two slashes: host follows, strip slashes
322 if (!*URL
|| *URL
== '/' || *URL
== '.' ||
323 (u
->scheme
[0] == '\0' &&
324 strchr(URL
, '/') == NULL
&& strchr(URL
, ':') == NULL
))
327 p
= strpbrk(URL
, "/@");
328 if (p
&& *p
== '@') {
330 for (q
= URL
, i
= 0; (*q
!= ':') && (*q
!= '@'); q
++)
336 for (q
++, i
= 0; (*q
!= ':') && (*q
!= '@'); q
++)
347 if (*p
== '[' && (q
= strchr(p
+ 1, ']')) != NULL
&&
348 (*++q
== '\0' || *q
== '/' || *q
== ':')) {
349 if ((i
= q
- p
- 2) > MAXHOSTNAMELEN
)
351 strncpy(u
->host
, ++p
, i
);
355 for (i
= 0; *p
&& (*p
!= '/') && (*p
!= ':'); p
++)
356 if (i
< MAXHOSTNAMELEN
)
361 for (q
= ++p
; *q
&& (*q
!= '/'); q
++)
362 if (isdigit((unsigned)*q
))
363 u
->port
= u
->port
* 10 + (*q
- '0');
366 _url_seterr(URL_BAD_PORT
);
377 if (strcasecmp(u
->scheme
, SCHEME_HTTP
) == 0 ||
378 strcasecmp(u
->scheme
, SCHEME_HTTPS
) == 0) {
379 const char hexnums
[] = "0123456789abcdef";
381 /* percent-escape whitespace. */
382 if ((doc
= malloc(strlen(p
) * 3 + 1)) == NULL
) {
388 if (!isspace((unsigned)*p
)) {
392 *doc
++ = hexnums
[((unsigned int)*p
) >> 4];
393 *doc
++ = hexnums
[((unsigned int)*p
) & 0xf];
398 } else if ((u
->doc
= strdup(p
)) == NULL
) {
403 DEBUG(fprintf(stderr
,
410 u
->scheme
, u
->user
, u
->pwd
,
411 u
->host
, u
->port
, u
->doc
));
424 fetchFreeURL(struct url
*u
)