toeplitz: Add comment.
[dragonfly.git] / lib / libfetch / fetch.c
blob34c76a35a854a57b19868f76e83ed20b0877dde2
1 /*-
2 * Copyright (c) 1998-2004 Dag-Erling Smørgrav
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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>
34 #include <ctype.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
39 #include "fetch.h"
40 #include "common.h"
42 auth_t fetchAuthMethod;
43 int fetchLastErrCode;
44 char fetchLastErrString[MAXERRSTRING];
45 int fetchTimeout;
46 int fetchRestartCalls = 1;
47 int fetchDebug;
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.
73 FILE *
74 fetchXGet(struct url *URL, struct url_stat *us, const char *flags)
77 if (us != NULL) {
78 us->size = -1;
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);
90 return (NULL);
94 * Select the appropriate protocol for the URL scheme, and return a
95 * read-only stream connected to the document referenced by the URL.
97 FILE *
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.
107 FILE *
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);
120 return (NULL);
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)
131 if (us != NULL) {
132 us->size = -1;
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);
144 return (-1);
148 * Select the appropriate protocol for the URL scheme, and return a
149 * list of files in the directory pointed to by the URL.
151 struct url_ent *
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);
164 return (NULL);
168 * Attempt to parse the given URL; if successful, call fetchXGet().
170 FILE *
171 fetchXGetURL(const char *URL, struct url_stat *us, const char *flags)
173 struct url *u;
174 FILE *f;
176 if ((u = fetchParseURL(URL)) == NULL)
177 return (NULL);
179 f = fetchXGet(u, us, flags);
181 fetchFreeURL(u);
182 return (f);
186 * Attempt to parse the given URL; if successful, call fetchGet().
188 FILE *
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().
197 FILE *
198 fetchPutURL(const char *URL, const char *flags)
200 struct url *u;
201 FILE *f;
203 if ((u = fetchParseURL(URL)) == NULL)
204 return (NULL);
206 f = fetchPut(u, flags);
208 fetchFreeURL(u);
209 return (f);
213 * Attempt to parse the given URL; if successful, call fetchStat().
216 fetchStatURL(const char *URL, struct url_stat *us, const char *flags)
218 struct url *u;
219 int s;
221 if ((u = fetchParseURL(URL)) == NULL)
222 return (-1);
224 s = fetchStat(u, us, flags);
226 fetchFreeURL(u);
227 return (s);
231 * Attempt to parse the given URL; if successful, call fetchList().
233 struct url_ent *
234 fetchListURL(const char *URL, const char *flags)
236 struct url *u;
237 struct url_ent *ue;
239 if ((u = fetchParseURL(URL)) == NULL)
240 return (NULL);
242 ue = fetchList(u, flags);
244 fetchFreeURL(u);
245 return (ue);
249 * Make a URL
251 struct url *
252 fetchMakeURL(const char *scheme, const char *host, int port, const char *doc,
253 const char *user, const char *pwd)
255 struct url *u;
257 if (!scheme || (!host && !doc)) {
258 url_seterr(URL_MALFORMED);
259 return (NULL);
262 if (port < 0 || port > 65535) {
263 url_seterr(URL_BAD_PORT);
264 return (NULL);
267 /* allocate struct url */
268 if ((u = calloc(1, sizeof(*u))) == NULL) {
269 fetch_syserr();
270 return (NULL);
273 if ((u->doc = strdup(doc ? doc : "/")) == NULL) {
274 fetch_syserr();
275 free(u);
276 return (NULL);
279 #define seturl(x) snprintf(u->x, sizeof(u->x), "%s", x)
280 seturl(scheme);
281 seturl(host);
282 seturl(user);
283 seturl(pwd);
284 #undef seturl
285 u->port = port;
287 return (u);
291 * Return value of the given hex digit.
293 static int
294 fetch_hexval(char ch)
297 if (ch >= '0' && ch <= '9')
298 return (ch - '0');
299 else if (ch >= 'a' && ch <= 'f')
300 return (ch - 'a' + 10);
301 else if (ch >= 'A' && ch <= 'F')
302 return (ch - 'A' + 10);
303 return (-1);
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).
312 static const char *
313 fetch_pctdecode(char *dst, const char *src, size_t dlen)
315 int d1, d2;
316 char c;
317 const char *s;
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)) {
322 c = d1 << 4 | d2;
323 s += 2;
324 } else {
325 c = *s;
327 if (dlen-- > 0)
328 *dst++ = c;
330 return (s);
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.
338 struct url *
339 fetchParseURL(const char *URL)
341 char *doc;
342 const char *p, *q;
343 struct url *u;
344 int i;
346 /* allocate struct url */
347 if ((u = calloc(1, sizeof(*u))) == NULL) {
348 fetch_syserr();
349 return (NULL);
352 /* scheme name */
353 if ((p = strstr(URL, ":/"))) {
354 snprintf(u->scheme, URL_SCHEMELEN+1,
355 "%.*s", (int)(p - URL), URL);
356 URL = ++p;
358 * Only one slash: no host, leave slash as part of document
359 * Two slashes: host follows, strip slashes
361 if (URL[1] == '/')
362 URL = (p += 2);
363 } else {
364 p = URL;
366 if (!*URL || *URL == '/' || *URL == '.' ||
367 (u->scheme[0] == '\0' &&
368 strchr(URL, '/') == NULL && strchr(URL, ':') == NULL))
369 goto nohost;
371 p = strpbrk(URL, "/@");
372 if (p && *p == '@') {
373 /* username */
374 q = fetch_pctdecode(u->user, URL, URL_USERLEN);
376 /* password */
377 if (*q == ':')
378 q = fetch_pctdecode(u->pwd, q + 1, URL_PWDLEN);
380 p++;
381 } else {
382 p = URL;
385 /* hostname */
386 #ifdef INET6
387 if (*p == '[' && (q = strchr(p + 1, ']')) != NULL &&
388 (*++q == '\0' || *q == '/' || *q == ':')) {
389 if ((i = q - p - 2) > MAXHOSTNAMELEN)
390 i = MAXHOSTNAMELEN;
391 strncpy(u->host, ++p, i);
392 p = q;
393 } else
394 #endif
395 for (i = 0; *p && (*p != '/') && (*p != ':'); p++)
396 if (i < MAXHOSTNAMELEN)
397 u->host[i++] = *p;
399 /* port */
400 if (*p == ':') {
401 for (q = ++p; *q && (*q != '/'); q++)
402 if (isdigit((unsigned char)*q))
403 u->port = u->port * 10 + (*q - '0');
404 else {
405 /* invalid port */
406 url_seterr(URL_BAD_PORT);
407 goto ouch;
409 p = q;
412 nohost:
413 /* document */
414 if (!*p)
415 p = "/";
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) {
423 fetch_syserr();
424 goto ouch;
426 u->doc = doc;
427 while (*p != '\0') {
428 if (!isspace((unsigned char)*p)) {
429 *doc++ = *p++;
430 } else {
431 *doc++ = '%';
432 *doc++ = hexnums[((unsigned int)*p) >> 4];
433 *doc++ = hexnums[((unsigned int)*p) & 0xf];
434 p++;
437 *doc = '\0';
438 } else if ((u->doc = strdup(p)) == NULL) {
439 fetch_syserr();
440 goto ouch;
443 DEBUG(fprintf(stderr,
444 "scheme: [%s]\n"
445 "user: [%s]\n"
446 "password: [%s]\n"
447 "host: [%s]\n"
448 "port: [%d]\n"
449 "document: [%s]\n",
450 u->scheme, u->user, u->pwd,
451 u->host, u->port, u->doc));
453 return (u);
455 ouch:
456 free(u);
457 return (NULL);
461 * Free a URL
463 void
464 fetchFreeURL(struct url *u)
466 free(u->doc);
467 free(u);