Deleted Added
full compact
fetch.c (135546) fetch.c (174588)
1/*-
2 * Copyright (c) 1998-2004 Dag-Erling Co�dan Sm�rgrav
3 * All rights reserved.
4 *
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

--- 13 unchanged lines hidden (view full) ---

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.
27 */
28
29#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1998-2004 Dag-Erling Co�dan Sm�rgrav
3 * All rights reserved.
4 *
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

--- 13 unchanged lines hidden (view full) ---

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.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/lib/libfetch/fetch.c 135546 2004-09-21 18:35:21Z des $");
30__FBSDID("$FreeBSD: head/lib/libfetch/fetch.c 174588 2007-12-14 10:26:58Z des $");
31
32#include <sys/param.h>
33#include <sys/errno.h>
34
35#include <ctype.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>

--- 12 unchanged lines hidden (view full) ---

51/*** Local data **************************************************************/
52
53/*
54 * Error messages for parser errors
55 */
56#define URL_MALFORMED 1
57#define URL_BAD_SCHEME 2
58#define URL_BAD_PORT 3
31
32#include <sys/param.h>
33#include <sys/errno.h>
34
35#include <ctype.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>

--- 12 unchanged lines hidden (view full) ---

51/*** Local data **************************************************************/
52
53/*
54 * Error messages for parser errors
55 */
56#define URL_MALFORMED 1
57#define URL_BAD_SCHEME 2
58#define URL_BAD_PORT 3
59static struct fetcherr _url_errlist[] = {
59static 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" }
64};
65
66
67/*** Public API **************************************************************/

--- 16 unchanged lines hidden (view full) ---

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));
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" }
64};
65
66
67/*** Public API **************************************************************/

--- 16 unchanged lines hidden (view full) ---

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);
92 url_seterr(URL_BAD_SCHEME);
93 return (NULL);
94}
95
96/*
97 * Select the appropriate protocol for the URL scheme, and return a
98 * read-only stream connected to the document referenced by the URL.
99 */
100FILE *

--- 15 unchanged lines hidden (view full) ---

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));
93 return (NULL);
94}
95
96/*
97 * Select the appropriate protocol for the URL scheme, and return a
98 * read-only stream connected to the document referenced by the URL.
99 */
100FILE *

--- 15 unchanged lines hidden (view full) ---

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);
124 url_seterr(URL_BAD_SCHEME);
125 return (NULL);
126}
127
128/*
129 * Select the appropriate protocol for the URL scheme, and return the
130 * size of the document referenced by the URL if it exists.
131 */
132int

--- 9 unchanged lines hidden (view full) ---

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));
125 return (NULL);
126}
127
128/*
129 * Select the appropriate protocol for the URL scheme, and return the
130 * size of the document referenced by the URL if it exists.
131 */
132int

--- 9 unchanged lines hidden (view full) ---

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);
150 url_seterr(URL_BAD_SCHEME);
151 return (-1);
152}
153
154/*
155 * Select the appropriate protocol for the URL scheme, and return a
156 * list of files in the directory pointed to by the URL.
157 */
158struct url_ent *

--- 5 unchanged lines hidden (view full) ---

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));
151 return (-1);
152}
153
154/*
155 * Select the appropriate protocol for the URL scheme, and return a
156 * list of files in the directory pointed to by the URL.
157 */
158struct url_ent *

--- 5 unchanged lines hidden (view full) ---

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);
172 url_seterr(URL_BAD_SCHEME);
173 return (NULL);
174}
175
176/*
177 * Attempt to parse the given URL; if successful, call fetchXGet().
178 */
179FILE *
180fetchXGetURL(const char *URL, struct url_stat *us, const char *flags)

--- 78 unchanged lines hidden (view full) ---

259 */
260struct url *
261fetchMakeURL(const char *scheme, const char *host, int port, const char *doc,
262 const char *user, const char *pwd)
263{
264 struct url *u;
265
266 if (!scheme || (!host && !doc)) {
173 return (NULL);
174}
175
176/*
177 * Attempt to parse the given URL; if successful, call fetchXGet().
178 */
179FILE *
180fetchXGetURL(const char *URL, struct url_stat *us, const char *flags)

--- 78 unchanged lines hidden (view full) ---

259 */
260struct url *
261fetchMakeURL(const char *scheme, const char *host, int port, const char *doc,
262 const char *user, const char *pwd)
263{
264 struct url *u;
265
266 if (!scheme || (!host && !doc)) {
267 _url_seterr(URL_MALFORMED);
267 url_seterr(URL_MALFORMED);
268 return (NULL);
269 }
270
271 if (port < 0 || port > 65535) {
268 return (NULL);
269 }
270
271 if (port < 0 || port > 65535) {
272 _url_seterr(URL_BAD_PORT);
272 url_seterr(URL_BAD_PORT);
273 return (NULL);
274 }
275
276 /* allocate struct url */
277 if ((u = calloc(1, sizeof(*u))) == NULL) {
273 return (NULL);
274 }
275
276 /* allocate struct url */
277 if ((u = calloc(1, sizeof(*u))) == NULL) {
278 _fetch_syserr();
278 fetch_syserr();
279 return (NULL);
280 }
281
282 if ((u->doc = strdup(doc ? doc : "/")) == NULL) {
279 return (NULL);
280 }
281
282 if ((u->doc = strdup(doc ? doc : "/")) == NULL) {
283 _fetch_syserr();
283 fetch_syserr();
284 free(u);
285 return (NULL);
286 }
287
288#define seturl(x) snprintf(u->x, sizeof(u->x), "%s", x)
289 seturl(scheme);
290 seturl(host);
291 seturl(user);

--- 14 unchanged lines hidden (view full) ---

306{
307 char *doc;
308 const char *p, *q;
309 struct url *u;
310 int i;
311
312 /* allocate struct url */
313 if ((u = calloc(1, sizeof(*u))) == NULL) {
284 free(u);
285 return (NULL);
286 }
287
288#define seturl(x) snprintf(u->x, sizeof(u->x), "%s", x)
289 seturl(scheme);
290 seturl(host);
291 seturl(user);

--- 14 unchanged lines hidden (view full) ---

306{
307 char *doc;
308 const char *p, *q;
309 struct url *u;
310 int i;
311
312 /* allocate struct url */
313 if ((u = calloc(1, sizeof(*u))) == NULL) {
314 _fetch_syserr();
314 fetch_syserr();
315 return (NULL);
316 }
317
318 /* scheme name */
319 if ((p = strstr(URL, ":/"))) {
320 snprintf(u->scheme, URL_SCHEMELEN+1,
321 "%.*s", (int)(p - URL), URL);
322 URL = ++p;

--- 45 unchanged lines hidden (view full) ---

368
369 /* port */
370 if (*p == ':') {
371 for (q = ++p; *q && (*q != '/'); q++)
372 if (isdigit(*q))
373 u->port = u->port * 10 + (*q - '0');
374 else {
375 /* invalid port */
315 return (NULL);
316 }
317
318 /* scheme name */
319 if ((p = strstr(URL, ":/"))) {
320 snprintf(u->scheme, URL_SCHEMELEN+1,
321 "%.*s", (int)(p - URL), URL);
322 URL = ++p;

--- 45 unchanged lines hidden (view full) ---

368
369 /* port */
370 if (*p == ':') {
371 for (q = ++p; *q && (*q != '/'); q++)
372 if (isdigit(*q))
373 u->port = u->port * 10 + (*q - '0');
374 else {
375 /* invalid port */
376 _url_seterr(URL_BAD_PORT);
376 url_seterr(URL_BAD_PORT);
377 goto ouch;
378 }
379 p = q;
380 }
381
382nohost:
383 /* document */
384 if (!*p)
385 p = "/";
386
387 if (strcasecmp(u->scheme, SCHEME_HTTP) == 0 ||
388 strcasecmp(u->scheme, SCHEME_HTTPS) == 0) {
389 const char hexnums[] = "0123456789abcdef";
390
391 /* percent-escape whitespace. */
392 if ((doc = malloc(strlen(p) * 3 + 1)) == NULL) {
377 goto ouch;
378 }
379 p = q;
380 }
381
382nohost:
383 /* document */
384 if (!*p)
385 p = "/";
386
387 if (strcasecmp(u->scheme, SCHEME_HTTP) == 0 ||
388 strcasecmp(u->scheme, SCHEME_HTTPS) == 0) {
389 const char hexnums[] = "0123456789abcdef";
390
391 /* percent-escape whitespace. */
392 if ((doc = malloc(strlen(p) * 3 + 1)) == NULL) {
393 _fetch_syserr();
393 fetch_syserr();
394 goto ouch;
395 }
396 u->doc = doc;
397 while (*p != '\0') {
398 if (!isspace(*p)) {
399 *doc++ = *p++;
400 } else {
401 *doc++ = '%';
402 *doc++ = hexnums[((unsigned int)*p) >> 4];
403 *doc++ = hexnums[((unsigned int)*p) & 0xf];
404 p++;
405 }
406 }
407 *doc = '\0';
408 } else if ((u->doc = strdup(p)) == NULL) {
394 goto ouch;
395 }
396 u->doc = doc;
397 while (*p != '\0') {
398 if (!isspace(*p)) {
399 *doc++ = *p++;
400 } else {
401 *doc++ = '%';
402 *doc++ = hexnums[((unsigned int)*p) >> 4];
403 *doc++ = hexnums[((unsigned int)*p) & 0xf];
404 p++;
405 }
406 }
407 *doc = '\0';
408 } else if ((u->doc = strdup(p)) == NULL) {
409 _fetch_syserr();
409 fetch_syserr();
410 goto ouch;
411 }
412
413 DEBUG(fprintf(stderr,
414 "scheme: [%s]\n"
415 "user: [%s]\n"
416 "password: [%s]\n"
417 "host: [%s]\n"

--- 21 unchanged lines hidden ---
410 goto ouch;
411 }
412
413 DEBUG(fprintf(stderr,
414 "scheme: [%s]\n"
415 "user: [%s]\n"
416 "password: [%s]\n"
417 "host: [%s]\n"

--- 21 unchanged lines hidden ---