1998-07-09 16:52:44 +00:00
|
|
|
/*-
|
2017-11-26 02:00:33 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
2011-10-19 11:43:51 +00:00
|
|
|
* Copyright (c) 1998-2004 Dag-Erling Smørgrav
|
1998-07-09 16:52:44 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer
|
|
|
|
* in this position and unchanged.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2001-09-30 21:36:09 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1998-07-09 16:52:44 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
2018-11-27 10:45:14 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
1998-07-09 16:52:44 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "fetch.h"
|
Second of a series of cleanups to libfetch.
This commit introduces the following features:
a) the fetchStat*() functions, which return meta-information for a
document, such as size, modification time, etc.
b) the use of the com_err(3) facilities to report errors.
It also fixes a bunch of style bugs and a few logic bugs and somewhat
improves the man page.
Changed files, in alphabetical order:
Makefile:
Don't generate macros in {ftp,http}err.c.
Generate category fields for the error message lists.
Compile the error table.
Install fetch_err.h along with fetch.h.
common.c:
Remove the _netdb_errstring() macro, and add FETCH_ERR_NETDB to the
error code in the _netdb_seterr() macro.
Add categories to the _netdb_errlist table.
Report errors through the Common Error library.
common.h:
Add the DEBUG macros.
Add prototype for fetchConnect().
Remove the prototype for _fetch_errstring(), which is local to common.c
Add a categroy field to struct fetcherr, and define constants for
error categories.
Define macros for _{url,netdb,ftp,http}_seterr().
errors.et: (new file)
List error categories.
fetch.3:
Document the fetchStat*() functions.
Move the "unimplemented functionality" comments from NOTES to BUGS.
Document that applications which use libfetch must also use
libcom_err, and list existing error codes.
Undocument fetchLastErr{Code,String}.
Remove the (empty) DIAGNOSTICS section.
Mention Eugene Skepner in the AUTHORS section.
fetch.c:
Move the DEBUG macros to common.c
Add fetchStat() and fetchStatURL().
Generate error messages for URL parser errors, and fix a minor bug
in the parser.
Use 'struct url' instead of 'url_t'.
Remove fetchLastErr{Code,String}.
fetch.h:
Use 'struct url' instead of 'url_t', and remove the typedef.
Define struct url_stat (used by fetchStat()).
Add prototypes for fetchStat*().
Remove the declarations for fetchLastErr{Code,String}.
Include fetch_err.h.
fetch_err.et: (new file)
Error table for libfetch.
file.c:
Add fetchStatFile().
Use 'struct url' instead of 'url_t'.
ftp.c:
Add fetchStatFTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErrCode.
ftp.errors:
Add categories to all error messages.
http.c:
Add fetchStatHTTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErr{Code,Text}.
http.errors:
Add categories to all error messages.
Prompted by: jkh and Eugene Skepner
Numerous sugestions from: Garett Wollman and Eugene Skepner
1998-11-06 22:14:08 +00:00
|
|
|
#include "common.h"
|
1998-07-09 16:52:44 +00:00
|
|
|
|
2001-05-26 19:37:15 +00:00
|
|
|
auth_t fetchAuthMethod;
|
2000-05-25 16:24:31 +00:00
|
|
|
int fetchLastErrCode;
|
|
|
|
char fetchLastErrString[MAXERRSTRING];
|
|
|
|
int fetchTimeout;
|
2000-07-17 20:49:39 +00:00
|
|
|
int fetchRestartCalls = 1;
|
2001-12-09 15:05:19 +00:00
|
|
|
int fetchDebug;
|
1998-12-16 10:24:55 +00:00
|
|
|
|
|
|
|
|
Second of a series of cleanups to libfetch.
This commit introduces the following features:
a) the fetchStat*() functions, which return meta-information for a
document, such as size, modification time, etc.
b) the use of the com_err(3) facilities to report errors.
It also fixes a bunch of style bugs and a few logic bugs and somewhat
improves the man page.
Changed files, in alphabetical order:
Makefile:
Don't generate macros in {ftp,http}err.c.
Generate category fields for the error message lists.
Compile the error table.
Install fetch_err.h along with fetch.h.
common.c:
Remove the _netdb_errstring() macro, and add FETCH_ERR_NETDB to the
error code in the _netdb_seterr() macro.
Add categories to the _netdb_errlist table.
Report errors through the Common Error library.
common.h:
Add the DEBUG macros.
Add prototype for fetchConnect().
Remove the prototype for _fetch_errstring(), which is local to common.c
Add a categroy field to struct fetcherr, and define constants for
error categories.
Define macros for _{url,netdb,ftp,http}_seterr().
errors.et: (new file)
List error categories.
fetch.3:
Document the fetchStat*() functions.
Move the "unimplemented functionality" comments from NOTES to BUGS.
Document that applications which use libfetch must also use
libcom_err, and list existing error codes.
Undocument fetchLastErr{Code,String}.
Remove the (empty) DIAGNOSTICS section.
Mention Eugene Skepner in the AUTHORS section.
fetch.c:
Move the DEBUG macros to common.c
Add fetchStat() and fetchStatURL().
Generate error messages for URL parser errors, and fix a minor bug
in the parser.
Use 'struct url' instead of 'url_t'.
Remove fetchLastErr{Code,String}.
fetch.h:
Use 'struct url' instead of 'url_t', and remove the typedef.
Define struct url_stat (used by fetchStat()).
Add prototypes for fetchStat*().
Remove the declarations for fetchLastErr{Code,String}.
Include fetch_err.h.
fetch_err.et: (new file)
Error table for libfetch.
file.c:
Add fetchStatFile().
Use 'struct url' instead of 'url_t'.
ftp.c:
Add fetchStatFTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErrCode.
ftp.errors:
Add categories to all error messages.
http.c:
Add fetchStatHTTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErr{Code,Text}.
http.errors:
Add categories to all error messages.
Prompted by: jkh and Eugene Skepner
Numerous sugestions from: Garett Wollman and Eugene Skepner
1998-11-06 22:14:08 +00:00
|
|
|
/*** Local data **************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Error messages for parser errors
|
|
|
|
*/
|
|
|
|
#define URL_MALFORMED 1
|
|
|
|
#define URL_BAD_SCHEME 2
|
|
|
|
#define URL_BAD_PORT 3
|
2007-12-14 10:26:58 +00:00
|
|
|
static struct fetcherr url_errlist[] = {
|
2002-02-05 22:13:51 +00:00
|
|
|
{ URL_MALFORMED, FETCH_URL, "Malformed URL" },
|
|
|
|
{ URL_BAD_SCHEME, FETCH_URL, "Invalid URL scheme" },
|
|
|
|
{ URL_BAD_PORT, FETCH_URL, "Invalid server port" },
|
|
|
|
{ -1, FETCH_UNKNOWN, "Unknown parser error" }
|
Second of a series of cleanups to libfetch.
This commit introduces the following features:
a) the fetchStat*() functions, which return meta-information for a
document, such as size, modification time, etc.
b) the use of the com_err(3) facilities to report errors.
It also fixes a bunch of style bugs and a few logic bugs and somewhat
improves the man page.
Changed files, in alphabetical order:
Makefile:
Don't generate macros in {ftp,http}err.c.
Generate category fields for the error message lists.
Compile the error table.
Install fetch_err.h along with fetch.h.
common.c:
Remove the _netdb_errstring() macro, and add FETCH_ERR_NETDB to the
error code in the _netdb_seterr() macro.
Add categories to the _netdb_errlist table.
Report errors through the Common Error library.
common.h:
Add the DEBUG macros.
Add prototype for fetchConnect().
Remove the prototype for _fetch_errstring(), which is local to common.c
Add a categroy field to struct fetcherr, and define constants for
error categories.
Define macros for _{url,netdb,ftp,http}_seterr().
errors.et: (new file)
List error categories.
fetch.3:
Document the fetchStat*() functions.
Move the "unimplemented functionality" comments from NOTES to BUGS.
Document that applications which use libfetch must also use
libcom_err, and list existing error codes.
Undocument fetchLastErr{Code,String}.
Remove the (empty) DIAGNOSTICS section.
Mention Eugene Skepner in the AUTHORS section.
fetch.c:
Move the DEBUG macros to common.c
Add fetchStat() and fetchStatURL().
Generate error messages for URL parser errors, and fix a minor bug
in the parser.
Use 'struct url' instead of 'url_t'.
Remove fetchLastErr{Code,String}.
fetch.h:
Use 'struct url' instead of 'url_t', and remove the typedef.
Define struct url_stat (used by fetchStat()).
Add prototypes for fetchStat*().
Remove the declarations for fetchLastErr{Code,String}.
Include fetch_err.h.
fetch_err.et: (new file)
Error table for libfetch.
file.c:
Add fetchStatFile().
Use 'struct url' instead of 'url_t'.
ftp.c:
Add fetchStatFTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErrCode.
ftp.errors:
Add categories to all error messages.
http.c:
Add fetchStatHTTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErr{Code,Text}.
http.errors:
Add categories to all error messages.
Prompted by: jkh and Eugene Skepner
Numerous sugestions from: Garett Wollman and Eugene Skepner
1998-11-06 22:14:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*** Public API **************************************************************/
|
1998-07-09 16:52:44 +00:00
|
|
|
|
1998-11-05 19:48:17 +00:00
|
|
|
/*
|
|
|
|
* Select the appropriate protocol for the URL scheme, and return a
|
|
|
|
* read-only stream connected to the document referenced by the URL.
|
2000-07-17 21:25:00 +00:00
|
|
|
* Also fill out the struct url_stat.
|
1998-11-05 19:48:17 +00:00
|
|
|
*/
|
1998-08-17 09:30:19 +00:00
|
|
|
FILE *
|
2001-04-24 00:06:21 +00:00
|
|
|
fetchXGet(struct url *URL, struct url_stat *us, const char *flags)
|
1998-08-17 09:30:19 +00:00
|
|
|
{
|
2002-02-05 22:13:51 +00:00
|
|
|
|
2003-01-22 17:53:15 +00:00
|
|
|
if (us != NULL) {
|
|
|
|
us->size = -1;
|
|
|
|
us->atime = us->mtime = 0;
|
|
|
|
}
|
2018-11-27 10:45:14 +00:00
|
|
|
if (strcmp(URL->scheme, SCHEME_FILE) == 0)
|
2002-02-05 22:13:51 +00:00
|
|
|
return (fetchXGetFile(URL, us, flags));
|
2018-11-27 10:45:14 +00:00
|
|
|
else if (strcmp(URL->scheme, SCHEME_FTP) == 0)
|
2002-06-05 12:46:36 +00:00
|
|
|
return (fetchXGetFTP(URL, us, flags));
|
2018-11-27 10:45:14 +00:00
|
|
|
else if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
|
2002-02-05 22:13:51 +00:00
|
|
|
return (fetchXGetHTTP(URL, us, flags));
|
2018-11-27 10:45:14 +00:00
|
|
|
else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
|
2002-06-05 12:46:36 +00:00
|
|
|
return (fetchXGetHTTP(URL, us, flags));
|
2007-12-14 10:26:58 +00:00
|
|
|
url_seterr(URL_BAD_SCHEME);
|
2002-06-05 12:46:36 +00:00
|
|
|
return (NULL);
|
1998-08-17 09:30:19 +00:00
|
|
|
}
|
|
|
|
|
2000-07-17 21:25:00 +00:00
|
|
|
/*
|
|
|
|
* Select the appropriate protocol for the URL scheme, and return a
|
|
|
|
* read-only stream connected to the document referenced by the URL.
|
|
|
|
*/
|
|
|
|
FILE *
|
2001-04-24 00:06:21 +00:00
|
|
|
fetchGet(struct url *URL, const char *flags)
|
2000-07-17 21:25:00 +00:00
|
|
|
{
|
2002-02-05 22:13:51 +00:00
|
|
|
return (fetchXGet(URL, NULL, flags));
|
2000-07-17 21:25:00 +00:00
|
|
|
}
|
|
|
|
|
1998-11-05 19:48:17 +00:00
|
|
|
/*
|
|
|
|
* Select the appropriate protocol for the URL scheme, and return a
|
|
|
|
* write-only stream connected to the document referenced by the URL.
|
|
|
|
*/
|
1998-08-17 09:30:19 +00:00
|
|
|
FILE *
|
2001-04-24 00:06:21 +00:00
|
|
|
fetchPut(struct url *URL, const char *flags)
|
1998-08-17 09:30:19 +00:00
|
|
|
{
|
2002-02-05 22:13:51 +00:00
|
|
|
|
2018-11-27 10:45:14 +00:00
|
|
|
if (strcmp(URL->scheme, SCHEME_FILE) == 0)
|
2002-02-05 22:13:51 +00:00
|
|
|
return (fetchPutFile(URL, flags));
|
2018-11-27 10:45:14 +00:00
|
|
|
else if (strcmp(URL->scheme, SCHEME_FTP) == 0)
|
2002-06-05 12:46:36 +00:00
|
|
|
return (fetchPutFTP(URL, flags));
|
2018-11-27 10:45:14 +00:00
|
|
|
else if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
|
2002-02-05 22:13:51 +00:00
|
|
|
return (fetchPutHTTP(URL, flags));
|
2018-11-27 10:45:14 +00:00
|
|
|
else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
|
2002-06-05 12:46:36 +00:00
|
|
|
return (fetchPutHTTP(URL, flags));
|
2007-12-14 10:26:58 +00:00
|
|
|
url_seterr(URL_BAD_SCHEME);
|
2002-06-05 12:46:36 +00:00
|
|
|
return (NULL);
|
Second of a series of cleanups to libfetch.
This commit introduces the following features:
a) the fetchStat*() functions, which return meta-information for a
document, such as size, modification time, etc.
b) the use of the com_err(3) facilities to report errors.
It also fixes a bunch of style bugs and a few logic bugs and somewhat
improves the man page.
Changed files, in alphabetical order:
Makefile:
Don't generate macros in {ftp,http}err.c.
Generate category fields for the error message lists.
Compile the error table.
Install fetch_err.h along with fetch.h.
common.c:
Remove the _netdb_errstring() macro, and add FETCH_ERR_NETDB to the
error code in the _netdb_seterr() macro.
Add categories to the _netdb_errlist table.
Report errors through the Common Error library.
common.h:
Add the DEBUG macros.
Add prototype for fetchConnect().
Remove the prototype for _fetch_errstring(), which is local to common.c
Add a categroy field to struct fetcherr, and define constants for
error categories.
Define macros for _{url,netdb,ftp,http}_seterr().
errors.et: (new file)
List error categories.
fetch.3:
Document the fetchStat*() functions.
Move the "unimplemented functionality" comments from NOTES to BUGS.
Document that applications which use libfetch must also use
libcom_err, and list existing error codes.
Undocument fetchLastErr{Code,String}.
Remove the (empty) DIAGNOSTICS section.
Mention Eugene Skepner in the AUTHORS section.
fetch.c:
Move the DEBUG macros to common.c
Add fetchStat() and fetchStatURL().
Generate error messages for URL parser errors, and fix a minor bug
in the parser.
Use 'struct url' instead of 'url_t'.
Remove fetchLastErr{Code,String}.
fetch.h:
Use 'struct url' instead of 'url_t', and remove the typedef.
Define struct url_stat (used by fetchStat()).
Add prototypes for fetchStat*().
Remove the declarations for fetchLastErr{Code,String}.
Include fetch_err.h.
fetch_err.et: (new file)
Error table for libfetch.
file.c:
Add fetchStatFile().
Use 'struct url' instead of 'url_t'.
ftp.c:
Add fetchStatFTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErrCode.
ftp.errors:
Add categories to all error messages.
http.c:
Add fetchStatHTTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErr{Code,Text}.
http.errors:
Add categories to all error messages.
Prompted by: jkh and Eugene Skepner
Numerous sugestions from: Garett Wollman and Eugene Skepner
1998-11-06 22:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Select the appropriate protocol for the URL scheme, and return the
|
|
|
|
* size of the document referenced by the URL if it exists.
|
|
|
|
*/
|
|
|
|
int
|
2001-04-24 00:06:21 +00:00
|
|
|
fetchStat(struct url *URL, struct url_stat *us, const char *flags)
|
Second of a series of cleanups to libfetch.
This commit introduces the following features:
a) the fetchStat*() functions, which return meta-information for a
document, such as size, modification time, etc.
b) the use of the com_err(3) facilities to report errors.
It also fixes a bunch of style bugs and a few logic bugs and somewhat
improves the man page.
Changed files, in alphabetical order:
Makefile:
Don't generate macros in {ftp,http}err.c.
Generate category fields for the error message lists.
Compile the error table.
Install fetch_err.h along with fetch.h.
common.c:
Remove the _netdb_errstring() macro, and add FETCH_ERR_NETDB to the
error code in the _netdb_seterr() macro.
Add categories to the _netdb_errlist table.
Report errors through the Common Error library.
common.h:
Add the DEBUG macros.
Add prototype for fetchConnect().
Remove the prototype for _fetch_errstring(), which is local to common.c
Add a categroy field to struct fetcherr, and define constants for
error categories.
Define macros for _{url,netdb,ftp,http}_seterr().
errors.et: (new file)
List error categories.
fetch.3:
Document the fetchStat*() functions.
Move the "unimplemented functionality" comments from NOTES to BUGS.
Document that applications which use libfetch must also use
libcom_err, and list existing error codes.
Undocument fetchLastErr{Code,String}.
Remove the (empty) DIAGNOSTICS section.
Mention Eugene Skepner in the AUTHORS section.
fetch.c:
Move the DEBUG macros to common.c
Add fetchStat() and fetchStatURL().
Generate error messages for URL parser errors, and fix a minor bug
in the parser.
Use 'struct url' instead of 'url_t'.
Remove fetchLastErr{Code,String}.
fetch.h:
Use 'struct url' instead of 'url_t', and remove the typedef.
Define struct url_stat (used by fetchStat()).
Add prototypes for fetchStat*().
Remove the declarations for fetchLastErr{Code,String}.
Include fetch_err.h.
fetch_err.et: (new file)
Error table for libfetch.
file.c:
Add fetchStatFile().
Use 'struct url' instead of 'url_t'.
ftp.c:
Add fetchStatFTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErrCode.
ftp.errors:
Add categories to all error messages.
http.c:
Add fetchStatHTTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErr{Code,Text}.
http.errors:
Add categories to all error messages.
Prompted by: jkh and Eugene Skepner
Numerous sugestions from: Garett Wollman and Eugene Skepner
1998-11-06 22:14:08 +00:00
|
|
|
{
|
2002-02-05 22:13:51 +00:00
|
|
|
|
2003-01-22 17:53:15 +00:00
|
|
|
if (us != NULL) {
|
|
|
|
us->size = -1;
|
|
|
|
us->atime = us->mtime = 0;
|
|
|
|
}
|
2018-11-27 10:45:14 +00:00
|
|
|
if (strcmp(URL->scheme, SCHEME_FILE) == 0)
|
2002-02-05 22:13:51 +00:00
|
|
|
return (fetchStatFile(URL, us, flags));
|
2018-11-27 10:45:14 +00:00
|
|
|
else if (strcmp(URL->scheme, SCHEME_FTP) == 0)
|
2002-02-05 22:13:51 +00:00
|
|
|
return (fetchStatFTP(URL, us, flags));
|
2018-11-27 10:45:14 +00:00
|
|
|
else if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
|
2002-06-05 12:46:36 +00:00
|
|
|
return (fetchStatHTTP(URL, us, flags));
|
2018-11-27 10:45:14 +00:00
|
|
|
else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
|
2002-06-05 12:46:36 +00:00
|
|
|
return (fetchStatHTTP(URL, us, flags));
|
2007-12-14 10:26:58 +00:00
|
|
|
url_seterr(URL_BAD_SCHEME);
|
2002-02-05 22:13:51 +00:00
|
|
|
return (-1);
|
1998-08-17 09:30:19 +00:00
|
|
|
}
|
|
|
|
|
1998-12-21 19:41:50 +00:00
|
|
|
/*
|
|
|
|
* Select the appropriate protocol for the URL scheme, and return a
|
|
|
|
* list of files in the directory pointed to by the URL.
|
|
|
|
*/
|
|
|
|
struct url_ent *
|
2001-04-24 00:06:21 +00:00
|
|
|
fetchList(struct url *URL, const char *flags)
|
1998-12-21 19:41:50 +00:00
|
|
|
{
|
2002-02-05 22:13:51 +00:00
|
|
|
|
2018-11-27 10:45:14 +00:00
|
|
|
if (strcmp(URL->scheme, SCHEME_FILE) == 0)
|
2002-02-05 22:13:51 +00:00
|
|
|
return (fetchListFile(URL, flags));
|
2018-11-27 10:45:14 +00:00
|
|
|
else if (strcmp(URL->scheme, SCHEME_FTP) == 0)
|
2002-02-05 22:13:51 +00:00
|
|
|
return (fetchListFTP(URL, flags));
|
2018-11-27 10:45:14 +00:00
|
|
|
else if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
|
2002-06-05 12:46:36 +00:00
|
|
|
return (fetchListHTTP(URL, flags));
|
2018-11-27 10:45:14 +00:00
|
|
|
else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
|
2002-06-05 12:46:36 +00:00
|
|
|
return (fetchListHTTP(URL, flags));
|
2007-12-14 10:26:58 +00:00
|
|
|
url_seterr(URL_BAD_SCHEME);
|
2002-02-05 22:13:51 +00:00
|
|
|
return (NULL);
|
1998-12-21 19:41:50 +00:00
|
|
|
}
|
|
|
|
|
1998-11-05 19:48:17 +00:00
|
|
|
/*
|
2000-07-17 21:25:00 +00:00
|
|
|
* Attempt to parse the given URL; if successful, call fetchXGet().
|
1998-11-05 19:48:17 +00:00
|
|
|
*/
|
1998-07-09 16:52:44 +00:00
|
|
|
FILE *
|
2001-04-24 00:06:21 +00:00
|
|
|
fetchXGetURL(const char *URL, struct url_stat *us, const char *flags)
|
1998-07-09 16:52:44 +00:00
|
|
|
{
|
2002-02-05 22:13:51 +00:00
|
|
|
struct url *u;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((u = fetchParseURL(URL)) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
f = fetchXGet(u, us, flags);
|
|
|
|
|
|
|
|
fetchFreeURL(u);
|
|
|
|
return (f);
|
1998-07-09 16:52:44 +00:00
|
|
|
}
|
|
|
|
|
2000-07-17 21:25:00 +00:00
|
|
|
/*
|
|
|
|
* Attempt to parse the given URL; if successful, call fetchGet().
|
|
|
|
*/
|
|
|
|
FILE *
|
2001-04-24 00:06:21 +00:00
|
|
|
fetchGetURL(const char *URL, const char *flags)
|
2000-07-17 21:25:00 +00:00
|
|
|
{
|
2002-02-05 22:13:51 +00:00
|
|
|
return (fetchXGetURL(URL, NULL, flags));
|
2000-07-17 21:25:00 +00:00
|
|
|
}
|
1998-07-09 16:52:44 +00:00
|
|
|
|
1998-11-05 19:48:17 +00:00
|
|
|
/*
|
|
|
|
* Attempt to parse the given URL; if successful, call fetchPut().
|
|
|
|
*/
|
1998-07-09 16:52:44 +00:00
|
|
|
FILE *
|
2001-04-24 00:06:21 +00:00
|
|
|
fetchPutURL(const char *URL, const char *flags)
|
1998-07-09 16:52:44 +00:00
|
|
|
{
|
2002-02-05 22:13:51 +00:00
|
|
|
struct url *u;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((u = fetchParseURL(URL)) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
f = fetchPut(u, flags);
|
|
|
|
|
|
|
|
fetchFreeURL(u);
|
|
|
|
return (f);
|
1998-07-09 16:52:44 +00:00
|
|
|
}
|
|
|
|
|
Second of a series of cleanups to libfetch.
This commit introduces the following features:
a) the fetchStat*() functions, which return meta-information for a
document, such as size, modification time, etc.
b) the use of the com_err(3) facilities to report errors.
It also fixes a bunch of style bugs and a few logic bugs and somewhat
improves the man page.
Changed files, in alphabetical order:
Makefile:
Don't generate macros in {ftp,http}err.c.
Generate category fields for the error message lists.
Compile the error table.
Install fetch_err.h along with fetch.h.
common.c:
Remove the _netdb_errstring() macro, and add FETCH_ERR_NETDB to the
error code in the _netdb_seterr() macro.
Add categories to the _netdb_errlist table.
Report errors through the Common Error library.
common.h:
Add the DEBUG macros.
Add prototype for fetchConnect().
Remove the prototype for _fetch_errstring(), which is local to common.c
Add a categroy field to struct fetcherr, and define constants for
error categories.
Define macros for _{url,netdb,ftp,http}_seterr().
errors.et: (new file)
List error categories.
fetch.3:
Document the fetchStat*() functions.
Move the "unimplemented functionality" comments from NOTES to BUGS.
Document that applications which use libfetch must also use
libcom_err, and list existing error codes.
Undocument fetchLastErr{Code,String}.
Remove the (empty) DIAGNOSTICS section.
Mention Eugene Skepner in the AUTHORS section.
fetch.c:
Move the DEBUG macros to common.c
Add fetchStat() and fetchStatURL().
Generate error messages for URL parser errors, and fix a minor bug
in the parser.
Use 'struct url' instead of 'url_t'.
Remove fetchLastErr{Code,String}.
fetch.h:
Use 'struct url' instead of 'url_t', and remove the typedef.
Define struct url_stat (used by fetchStat()).
Add prototypes for fetchStat*().
Remove the declarations for fetchLastErr{Code,String}.
Include fetch_err.h.
fetch_err.et: (new file)
Error table for libfetch.
file.c:
Add fetchStatFile().
Use 'struct url' instead of 'url_t'.
ftp.c:
Add fetchStatFTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErrCode.
ftp.errors:
Add categories to all error messages.
http.c:
Add fetchStatHTTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErr{Code,Text}.
http.errors:
Add categories to all error messages.
Prompted by: jkh and Eugene Skepner
Numerous sugestions from: Garett Wollman and Eugene Skepner
1998-11-06 22:14:08 +00:00
|
|
|
/*
|
|
|
|
* Attempt to parse the given URL; if successful, call fetchStat().
|
|
|
|
*/
|
|
|
|
int
|
2001-04-24 00:06:21 +00:00
|
|
|
fetchStatURL(const char *URL, struct url_stat *us, const char *flags)
|
Second of a series of cleanups to libfetch.
This commit introduces the following features:
a) the fetchStat*() functions, which return meta-information for a
document, such as size, modification time, etc.
b) the use of the com_err(3) facilities to report errors.
It also fixes a bunch of style bugs and a few logic bugs and somewhat
improves the man page.
Changed files, in alphabetical order:
Makefile:
Don't generate macros in {ftp,http}err.c.
Generate category fields for the error message lists.
Compile the error table.
Install fetch_err.h along with fetch.h.
common.c:
Remove the _netdb_errstring() macro, and add FETCH_ERR_NETDB to the
error code in the _netdb_seterr() macro.
Add categories to the _netdb_errlist table.
Report errors through the Common Error library.
common.h:
Add the DEBUG macros.
Add prototype for fetchConnect().
Remove the prototype for _fetch_errstring(), which is local to common.c
Add a categroy field to struct fetcherr, and define constants for
error categories.
Define macros for _{url,netdb,ftp,http}_seterr().
errors.et: (new file)
List error categories.
fetch.3:
Document the fetchStat*() functions.
Move the "unimplemented functionality" comments from NOTES to BUGS.
Document that applications which use libfetch must also use
libcom_err, and list existing error codes.
Undocument fetchLastErr{Code,String}.
Remove the (empty) DIAGNOSTICS section.
Mention Eugene Skepner in the AUTHORS section.
fetch.c:
Move the DEBUG macros to common.c
Add fetchStat() and fetchStatURL().
Generate error messages for URL parser errors, and fix a minor bug
in the parser.
Use 'struct url' instead of 'url_t'.
Remove fetchLastErr{Code,String}.
fetch.h:
Use 'struct url' instead of 'url_t', and remove the typedef.
Define struct url_stat (used by fetchStat()).
Add prototypes for fetchStat*().
Remove the declarations for fetchLastErr{Code,String}.
Include fetch_err.h.
fetch_err.et: (new file)
Error table for libfetch.
file.c:
Add fetchStatFile().
Use 'struct url' instead of 'url_t'.
ftp.c:
Add fetchStatFTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErrCode.
ftp.errors:
Add categories to all error messages.
http.c:
Add fetchStatHTTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErr{Code,Text}.
http.errors:
Add categories to all error messages.
Prompted by: jkh and Eugene Skepner
Numerous sugestions from: Garett Wollman and Eugene Skepner
1998-11-06 22:14:08 +00:00
|
|
|
{
|
2002-02-05 22:13:51 +00:00
|
|
|
struct url *u;
|
|
|
|
int s;
|
Second of a series of cleanups to libfetch.
This commit introduces the following features:
a) the fetchStat*() functions, which return meta-information for a
document, such as size, modification time, etc.
b) the use of the com_err(3) facilities to report errors.
It also fixes a bunch of style bugs and a few logic bugs and somewhat
improves the man page.
Changed files, in alphabetical order:
Makefile:
Don't generate macros in {ftp,http}err.c.
Generate category fields for the error message lists.
Compile the error table.
Install fetch_err.h along with fetch.h.
common.c:
Remove the _netdb_errstring() macro, and add FETCH_ERR_NETDB to the
error code in the _netdb_seterr() macro.
Add categories to the _netdb_errlist table.
Report errors through the Common Error library.
common.h:
Add the DEBUG macros.
Add prototype for fetchConnect().
Remove the prototype for _fetch_errstring(), which is local to common.c
Add a categroy field to struct fetcherr, and define constants for
error categories.
Define macros for _{url,netdb,ftp,http}_seterr().
errors.et: (new file)
List error categories.
fetch.3:
Document the fetchStat*() functions.
Move the "unimplemented functionality" comments from NOTES to BUGS.
Document that applications which use libfetch must also use
libcom_err, and list existing error codes.
Undocument fetchLastErr{Code,String}.
Remove the (empty) DIAGNOSTICS section.
Mention Eugene Skepner in the AUTHORS section.
fetch.c:
Move the DEBUG macros to common.c
Add fetchStat() and fetchStatURL().
Generate error messages for URL parser errors, and fix a minor bug
in the parser.
Use 'struct url' instead of 'url_t'.
Remove fetchLastErr{Code,String}.
fetch.h:
Use 'struct url' instead of 'url_t', and remove the typedef.
Define struct url_stat (used by fetchStat()).
Add prototypes for fetchStat*().
Remove the declarations for fetchLastErr{Code,String}.
Include fetch_err.h.
fetch_err.et: (new file)
Error table for libfetch.
file.c:
Add fetchStatFile().
Use 'struct url' instead of 'url_t'.
ftp.c:
Add fetchStatFTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErrCode.
ftp.errors:
Add categories to all error messages.
http.c:
Add fetchStatHTTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErr{Code,Text}.
http.errors:
Add categories to all error messages.
Prompted by: jkh and Eugene Skepner
Numerous sugestions from: Garett Wollman and Eugene Skepner
1998-11-06 22:14:08 +00:00
|
|
|
|
2002-02-05 22:13:51 +00:00
|
|
|
if ((u = fetchParseURL(URL)) == NULL)
|
|
|
|
return (-1);
|
Second of a series of cleanups to libfetch.
This commit introduces the following features:
a) the fetchStat*() functions, which return meta-information for a
document, such as size, modification time, etc.
b) the use of the com_err(3) facilities to report errors.
It also fixes a bunch of style bugs and a few logic bugs and somewhat
improves the man page.
Changed files, in alphabetical order:
Makefile:
Don't generate macros in {ftp,http}err.c.
Generate category fields for the error message lists.
Compile the error table.
Install fetch_err.h along with fetch.h.
common.c:
Remove the _netdb_errstring() macro, and add FETCH_ERR_NETDB to the
error code in the _netdb_seterr() macro.
Add categories to the _netdb_errlist table.
Report errors through the Common Error library.
common.h:
Add the DEBUG macros.
Add prototype for fetchConnect().
Remove the prototype for _fetch_errstring(), which is local to common.c
Add a categroy field to struct fetcherr, and define constants for
error categories.
Define macros for _{url,netdb,ftp,http}_seterr().
errors.et: (new file)
List error categories.
fetch.3:
Document the fetchStat*() functions.
Move the "unimplemented functionality" comments from NOTES to BUGS.
Document that applications which use libfetch must also use
libcom_err, and list existing error codes.
Undocument fetchLastErr{Code,String}.
Remove the (empty) DIAGNOSTICS section.
Mention Eugene Skepner in the AUTHORS section.
fetch.c:
Move the DEBUG macros to common.c
Add fetchStat() and fetchStatURL().
Generate error messages for URL parser errors, and fix a minor bug
in the parser.
Use 'struct url' instead of 'url_t'.
Remove fetchLastErr{Code,String}.
fetch.h:
Use 'struct url' instead of 'url_t', and remove the typedef.
Define struct url_stat (used by fetchStat()).
Add prototypes for fetchStat*().
Remove the declarations for fetchLastErr{Code,String}.
Include fetch_err.h.
fetch_err.et: (new file)
Error table for libfetch.
file.c:
Add fetchStatFile().
Use 'struct url' instead of 'url_t'.
ftp.c:
Add fetchStatFTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErrCode.
ftp.errors:
Add categories to all error messages.
http.c:
Add fetchStatHTTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErr{Code,Text}.
http.errors:
Add categories to all error messages.
Prompted by: jkh and Eugene Skepner
Numerous sugestions from: Garett Wollman and Eugene Skepner
1998-11-06 22:14:08 +00:00
|
|
|
|
2002-02-05 22:13:51 +00:00
|
|
|
s = fetchStat(u, us, flags);
|
Second of a series of cleanups to libfetch.
This commit introduces the following features:
a) the fetchStat*() functions, which return meta-information for a
document, such as size, modification time, etc.
b) the use of the com_err(3) facilities to report errors.
It also fixes a bunch of style bugs and a few logic bugs and somewhat
improves the man page.
Changed files, in alphabetical order:
Makefile:
Don't generate macros in {ftp,http}err.c.
Generate category fields for the error message lists.
Compile the error table.
Install fetch_err.h along with fetch.h.
common.c:
Remove the _netdb_errstring() macro, and add FETCH_ERR_NETDB to the
error code in the _netdb_seterr() macro.
Add categories to the _netdb_errlist table.
Report errors through the Common Error library.
common.h:
Add the DEBUG macros.
Add prototype for fetchConnect().
Remove the prototype for _fetch_errstring(), which is local to common.c
Add a categroy field to struct fetcherr, and define constants for
error categories.
Define macros for _{url,netdb,ftp,http}_seterr().
errors.et: (new file)
List error categories.
fetch.3:
Document the fetchStat*() functions.
Move the "unimplemented functionality" comments from NOTES to BUGS.
Document that applications which use libfetch must also use
libcom_err, and list existing error codes.
Undocument fetchLastErr{Code,String}.
Remove the (empty) DIAGNOSTICS section.
Mention Eugene Skepner in the AUTHORS section.
fetch.c:
Move the DEBUG macros to common.c
Add fetchStat() and fetchStatURL().
Generate error messages for URL parser errors, and fix a minor bug
in the parser.
Use 'struct url' instead of 'url_t'.
Remove fetchLastErr{Code,String}.
fetch.h:
Use 'struct url' instead of 'url_t', and remove the typedef.
Define struct url_stat (used by fetchStat()).
Add prototypes for fetchStat*().
Remove the declarations for fetchLastErr{Code,String}.
Include fetch_err.h.
fetch_err.et: (new file)
Error table for libfetch.
file.c:
Add fetchStatFile().
Use 'struct url' instead of 'url_t'.
ftp.c:
Add fetchStatFTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErrCode.
ftp.errors:
Add categories to all error messages.
http.c:
Add fetchStatHTTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErr{Code,Text}.
http.errors:
Add categories to all error messages.
Prompted by: jkh and Eugene Skepner
Numerous sugestions from: Garett Wollman and Eugene Skepner
1998-11-06 22:14:08 +00:00
|
|
|
|
2002-02-05 22:13:51 +00:00
|
|
|
fetchFreeURL(u);
|
|
|
|
return (s);
|
Second of a series of cleanups to libfetch.
This commit introduces the following features:
a) the fetchStat*() functions, which return meta-information for a
document, such as size, modification time, etc.
b) the use of the com_err(3) facilities to report errors.
It also fixes a bunch of style bugs and a few logic bugs and somewhat
improves the man page.
Changed files, in alphabetical order:
Makefile:
Don't generate macros in {ftp,http}err.c.
Generate category fields for the error message lists.
Compile the error table.
Install fetch_err.h along with fetch.h.
common.c:
Remove the _netdb_errstring() macro, and add FETCH_ERR_NETDB to the
error code in the _netdb_seterr() macro.
Add categories to the _netdb_errlist table.
Report errors through the Common Error library.
common.h:
Add the DEBUG macros.
Add prototype for fetchConnect().
Remove the prototype for _fetch_errstring(), which is local to common.c
Add a categroy field to struct fetcherr, and define constants for
error categories.
Define macros for _{url,netdb,ftp,http}_seterr().
errors.et: (new file)
List error categories.
fetch.3:
Document the fetchStat*() functions.
Move the "unimplemented functionality" comments from NOTES to BUGS.
Document that applications which use libfetch must also use
libcom_err, and list existing error codes.
Undocument fetchLastErr{Code,String}.
Remove the (empty) DIAGNOSTICS section.
Mention Eugene Skepner in the AUTHORS section.
fetch.c:
Move the DEBUG macros to common.c
Add fetchStat() and fetchStatURL().
Generate error messages for URL parser errors, and fix a minor bug
in the parser.
Use 'struct url' instead of 'url_t'.
Remove fetchLastErr{Code,String}.
fetch.h:
Use 'struct url' instead of 'url_t', and remove the typedef.
Define struct url_stat (used by fetchStat()).
Add prototypes for fetchStat*().
Remove the declarations for fetchLastErr{Code,String}.
Include fetch_err.h.
fetch_err.et: (new file)
Error table for libfetch.
file.c:
Add fetchStatFile().
Use 'struct url' instead of 'url_t'.
ftp.c:
Add fetchStatFTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErrCode.
ftp.errors:
Add categories to all error messages.
http.c:
Add fetchStatHTTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErr{Code,Text}.
http.errors:
Add categories to all error messages.
Prompted by: jkh and Eugene Skepner
Numerous sugestions from: Garett Wollman and Eugene Skepner
1998-11-06 22:14:08 +00:00
|
|
|
}
|
|
|
|
|
1998-12-21 19:41:50 +00:00
|
|
|
/*
|
|
|
|
* Attempt to parse the given URL; if successful, call fetchList().
|
|
|
|
*/
|
|
|
|
struct url_ent *
|
2001-04-24 00:06:21 +00:00
|
|
|
fetchListURL(const char *URL, const char *flags)
|
1998-12-21 19:41:50 +00:00
|
|
|
{
|
2002-02-05 22:13:51 +00:00
|
|
|
struct url *u;
|
|
|
|
struct url_ent *ue;
|
1998-12-21 19:41:50 +00:00
|
|
|
|
2002-02-05 22:13:51 +00:00
|
|
|
if ((u = fetchParseURL(URL)) == NULL)
|
|
|
|
return (NULL);
|
1998-12-21 19:41:50 +00:00
|
|
|
|
2002-02-05 22:13:51 +00:00
|
|
|
ue = fetchList(u, flags);
|
1998-12-21 19:41:50 +00:00
|
|
|
|
2002-02-05 22:13:51 +00:00
|
|
|
fetchFreeURL(u);
|
|
|
|
return (ue);
|
1998-12-21 19:41:50 +00:00
|
|
|
}
|
|
|
|
|
2000-05-25 16:46:09 +00:00
|
|
|
/*
|
|
|
|
* Make a URL
|
|
|
|
*/
|
|
|
|
struct url *
|
2001-04-24 00:06:21 +00:00
|
|
|
fetchMakeURL(const char *scheme, const char *host, int port, const char *doc,
|
|
|
|
const char *user, const char *pwd)
|
2000-05-25 16:46:09 +00:00
|
|
|
{
|
2002-02-05 22:13:51 +00:00
|
|
|
struct url *u;
|
|
|
|
|
|
|
|
if (!scheme || (!host && !doc)) {
|
2007-12-14 10:26:58 +00:00
|
|
|
url_seterr(URL_MALFORMED);
|
2002-02-05 22:13:51 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port < 0 || port > 65535) {
|
2007-12-14 10:26:58 +00:00
|
|
|
url_seterr(URL_BAD_PORT);
|
2002-02-05 22:13:51 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate struct url */
|
2003-01-28 08:04:40 +00:00
|
|
|
if ((u = calloc(1, sizeof(*u))) == NULL) {
|
2007-12-14 10:26:58 +00:00
|
|
|
fetch_syserr();
|
2002-02-05 22:13:51 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
2018-05-29 13:07:36 +00:00
|
|
|
u->netrcfd = -1;
|
2002-02-05 22:13:51 +00:00
|
|
|
|
|
|
|
if ((u->doc = strdup(doc ? doc : "/")) == NULL) {
|
2007-12-14 10:26:58 +00:00
|
|
|
fetch_syserr();
|
2002-02-05 22:13:51 +00:00
|
|
|
free(u);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2003-01-28 08:04:40 +00:00
|
|
|
#define seturl(x) snprintf(u->x, sizeof(u->x), "%s", x)
|
2002-02-05 22:13:51 +00:00
|
|
|
seturl(scheme);
|
|
|
|
seturl(host);
|
|
|
|
seturl(user);
|
|
|
|
seturl(pwd);
|
2000-05-25 16:46:09 +00:00
|
|
|
#undef seturl
|
2002-02-05 22:13:51 +00:00
|
|
|
u->port = port;
|
2000-05-25 16:46:09 +00:00
|
|
|
|
2002-02-05 22:13:51 +00:00
|
|
|
return (u);
|
2000-05-25 16:46:09 +00:00
|
|
|
}
|
|
|
|
|
2012-04-11 15:42:02 +00:00
|
|
|
/*
|
|
|
|
* Return value of the given hex digit.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fetch_hexval(char ch)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (ch >= '0' && ch <= '9')
|
|
|
|
return (ch - '0');
|
|
|
|
else if (ch >= 'a' && ch <= 'f')
|
|
|
|
return (ch - 'a' + 10);
|
|
|
|
else if (ch >= 'A' && ch <= 'F')
|
|
|
|
return (ch - 'A' + 10);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decode percent-encoded URL component from src into dst, stopping at end
|
|
|
|
* of string, or at @ or : separators. Returns a pointer to the unhandled
|
|
|
|
* part of the input string (null terminator, @, or :). No terminator is
|
|
|
|
* written to dst (it is the caller's responsibility).
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
fetch_pctdecode(char *dst, const char *src, size_t dlen)
|
|
|
|
{
|
|
|
|
int d1, d2;
|
|
|
|
char c;
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
for (s = src; *s != '\0' && *s != '@' && *s != ':'; s++) {
|
|
|
|
if (s[0] == '%' && (d1 = fetch_hexval(s[1])) >= 0 &&
|
|
|
|
(d2 = fetch_hexval(s[2])) >= 0 && (d1 > 0 || d2 > 0)) {
|
|
|
|
c = d1 << 4 | d2;
|
|
|
|
s += 2;
|
2020-02-05 16:55:00 +00:00
|
|
|
} else if (s[0] == '%') {
|
|
|
|
/* Invalid escape sequence. */
|
|
|
|
return (NULL);
|
2012-04-11 15:42:02 +00:00
|
|
|
} else {
|
|
|
|
c = *s;
|
|
|
|
}
|
|
|
|
if (dlen-- > 0)
|
|
|
|
*dst++ = c;
|
2020-01-28 18:37:18 +00:00
|
|
|
else
|
|
|
|
return (NULL);
|
2012-04-11 15:42:02 +00:00
|
|
|
}
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
1998-07-09 16:52:44 +00:00
|
|
|
/*
|
|
|
|
* Split an URL into components. URL syntax is:
|
2000-10-12 22:07:49 +00:00
|
|
|
* [method:/][/[user[:pwd]@]host[:port]/][document]
|
1998-07-09 16:52:44 +00:00
|
|
|
* This almost, but not quite, RFC1738 URL syntax.
|
|
|
|
*/
|
Second of a series of cleanups to libfetch.
This commit introduces the following features:
a) the fetchStat*() functions, which return meta-information for a
document, such as size, modification time, etc.
b) the use of the com_err(3) facilities to report errors.
It also fixes a bunch of style bugs and a few logic bugs and somewhat
improves the man page.
Changed files, in alphabetical order:
Makefile:
Don't generate macros in {ftp,http}err.c.
Generate category fields for the error message lists.
Compile the error table.
Install fetch_err.h along with fetch.h.
common.c:
Remove the _netdb_errstring() macro, and add FETCH_ERR_NETDB to the
error code in the _netdb_seterr() macro.
Add categories to the _netdb_errlist table.
Report errors through the Common Error library.
common.h:
Add the DEBUG macros.
Add prototype for fetchConnect().
Remove the prototype for _fetch_errstring(), which is local to common.c
Add a categroy field to struct fetcherr, and define constants for
error categories.
Define macros for _{url,netdb,ftp,http}_seterr().
errors.et: (new file)
List error categories.
fetch.3:
Document the fetchStat*() functions.
Move the "unimplemented functionality" comments from NOTES to BUGS.
Document that applications which use libfetch must also use
libcom_err, and list existing error codes.
Undocument fetchLastErr{Code,String}.
Remove the (empty) DIAGNOSTICS section.
Mention Eugene Skepner in the AUTHORS section.
fetch.c:
Move the DEBUG macros to common.c
Add fetchStat() and fetchStatURL().
Generate error messages for URL parser errors, and fix a minor bug
in the parser.
Use 'struct url' instead of 'url_t'.
Remove fetchLastErr{Code,String}.
fetch.h:
Use 'struct url' instead of 'url_t', and remove the typedef.
Define struct url_stat (used by fetchStat()).
Add prototypes for fetchStat*().
Remove the declarations for fetchLastErr{Code,String}.
Include fetch_err.h.
fetch_err.et: (new file)
Error table for libfetch.
file.c:
Add fetchStatFile().
Use 'struct url' instead of 'url_t'.
ftp.c:
Add fetchStatFTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErrCode.
ftp.errors:
Add categories to all error messages.
http.c:
Add fetchStatHTTP().
Use 'struct url' instead of 'url_t'.
Don't use fetchLastErr{Code,Text}.
http.errors:
Add categories to all error messages.
Prompted by: jkh and Eugene Skepner
Numerous sugestions from: Garett Wollman and Eugene Skepner
1998-11-06 22:14:08 +00:00
|
|
|
struct url *
|
2001-04-24 00:06:21 +00:00
|
|
|
fetchParseURL(const char *URL)
|
1998-07-09 16:52:44 +00:00
|
|
|
{
|
2002-02-05 22:13:51 +00:00
|
|
|
char *doc;
|
|
|
|
const char *p, *q;
|
|
|
|
struct url *u;
|
2018-11-27 10:45:14 +00:00
|
|
|
int i, n;
|
2002-02-05 22:13:51 +00:00
|
|
|
|
|
|
|
/* allocate struct url */
|
2003-01-28 08:04:40 +00:00
|
|
|
if ((u = calloc(1, sizeof(*u))) == NULL) {
|
2007-12-14 10:26:58 +00:00
|
|
|
fetch_syserr();
|
2002-02-05 22:13:51 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
2018-05-29 13:07:36 +00:00
|
|
|
u->netrcfd = -1;
|
2002-02-05 22:13:51 +00:00
|
|
|
|
|
|
|
/* scheme name */
|
|
|
|
if ((p = strstr(URL, ":/"))) {
|
2018-11-27 10:45:14 +00:00
|
|
|
if (p - URL > URL_SCHEMELEN)
|
|
|
|
goto ouch;
|
|
|
|
for (i = 0; URL + i < p; i++)
|
|
|
|
u->scheme[i] = tolower((unsigned char)URL[i]);
|
2002-02-05 22:13:51 +00:00
|
|
|
URL = ++p;
|
|
|
|
/*
|
|
|
|
* Only one slash: no host, leave slash as part of document
|
|
|
|
* Two slashes: host follows, strip slashes
|
|
|
|
*/
|
|
|
|
if (URL[1] == '/')
|
|
|
|
URL = (p += 2);
|
|
|
|
} else {
|
|
|
|
p = URL;
|
|
|
|
}
|
|
|
|
if (!*URL || *URL == '/' || *URL == '.' ||
|
|
|
|
(u->scheme[0] == '\0' &&
|
|
|
|
strchr(URL, '/') == NULL && strchr(URL, ':') == NULL))
|
|
|
|
goto nohost;
|
|
|
|
|
|
|
|
p = strpbrk(URL, "/@");
|
|
|
|
if (p && *p == '@') {
|
|
|
|
/* username */
|
2012-04-11 15:42:02 +00:00
|
|
|
q = fetch_pctdecode(u->user, URL, URL_USERLEN);
|
2020-01-28 18:37:18 +00:00
|
|
|
if (q == NULL)
|
|
|
|
goto ouch;
|
2002-02-05 22:13:51 +00:00
|
|
|
|
|
|
|
/* password */
|
2020-01-28 18:37:18 +00:00
|
|
|
if (*q == ':') {
|
2013-06-29 15:51:27 +00:00
|
|
|
q = fetch_pctdecode(u->pwd, q + 1, URL_PWDLEN);
|
2020-01-28 18:37:18 +00:00
|
|
|
if (q == NULL)
|
|
|
|
goto ouch;
|
|
|
|
}
|
2002-02-05 22:13:51 +00:00
|
|
|
p++;
|
|
|
|
} else {
|
|
|
|
p = URL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* hostname */
|
2018-11-27 10:45:14 +00:00
|
|
|
if (*p == '[') {
|
|
|
|
q = p + 1 + strspn(p + 1, ":0123456789ABCDEFabcdef");
|
|
|
|
if (*q++ != ']')
|
|
|
|
goto ouch;
|
2017-03-17 14:18:52 +00:00
|
|
|
} else {
|
2018-11-27 10:45:14 +00:00
|
|
|
/* valid characters in a DNS name */
|
|
|
|
q = p + strspn(p, "-." "0123456789"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" "_"
|
|
|
|
"abcdefghijklmnopqrstuvwxyz");
|
2017-03-17 14:18:52 +00:00
|
|
|
}
|
2018-11-27 10:45:14 +00:00
|
|
|
if ((*q != '\0' && *q != '/' && *q != ':') || q - p > MAXHOSTNAMELEN)
|
|
|
|
goto ouch;
|
|
|
|
for (i = 0; p + i < q; i++)
|
|
|
|
u->host[i] = tolower((unsigned char)p[i]);
|
|
|
|
u->host[i] = '\0';
|
|
|
|
p = q;
|
2002-02-05 22:13:51 +00:00
|
|
|
|
|
|
|
/* port */
|
|
|
|
if (*p == ':') {
|
2018-11-27 10:45:14 +00:00
|
|
|
for (n = 0, q = ++p; *q && (*q != '/'); q++) {
|
|
|
|
if (*q >= '0' && *q <= '9' && n < INT_MAX / 10) {
|
|
|
|
n = n * 10 + (*q - '0');
|
|
|
|
} else {
|
2002-02-05 22:13:51 +00:00
|
|
|
/* invalid port */
|
2007-12-14 10:26:58 +00:00
|
|
|
url_seterr(URL_BAD_PORT);
|
2002-02-05 22:13:51 +00:00
|
|
|
goto ouch;
|
|
|
|
}
|
2018-11-27 10:45:14 +00:00
|
|
|
}
|
|
|
|
if (n < 1 || n > IPPORT_MAX)
|
|
|
|
goto ouch;
|
|
|
|
u->port = n;
|
2002-02-05 22:13:51 +00:00
|
|
|
p = q;
|
|
|
|
}
|
1998-07-09 16:52:44 +00:00
|
|
|
|
|
|
|
nohost:
|
2002-02-05 22:13:51 +00:00
|
|
|
/* document */
|
|
|
|
if (!*p)
|
|
|
|
p = "/";
|
|
|
|
|
2018-11-27 10:45:14 +00:00
|
|
|
if (strcmp(u->scheme, SCHEME_HTTP) == 0 ||
|
|
|
|
strcmp(u->scheme, SCHEME_HTTPS) == 0) {
|
2002-02-05 22:13:51 +00:00
|
|
|
const char hexnums[] = "0123456789abcdef";
|
|
|
|
|
|
|
|
/* percent-escape whitespace. */
|
|
|
|
if ((doc = malloc(strlen(p) * 3 + 1)) == NULL) {
|
2007-12-14 10:26:58 +00:00
|
|
|
fetch_syserr();
|
2002-02-05 22:13:51 +00:00
|
|
|
goto ouch;
|
|
|
|
}
|
|
|
|
u->doc = doc;
|
|
|
|
while (*p != '\0') {
|
2007-12-19 00:26:36 +00:00
|
|
|
if (!isspace((unsigned char)*p)) {
|
2002-02-05 22:13:51 +00:00
|
|
|
*doc++ = *p++;
|
|
|
|
} else {
|
|
|
|
*doc++ = '%';
|
|
|
|
*doc++ = hexnums[((unsigned int)*p) >> 4];
|
|
|
|
*doc++ = hexnums[((unsigned int)*p) & 0xf];
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*doc = '\0';
|
|
|
|
} else if ((u->doc = strdup(p)) == NULL) {
|
2007-12-14 10:26:58 +00:00
|
|
|
fetch_syserr();
|
2002-02-05 22:13:51 +00:00
|
|
|
goto ouch;
|
2000-10-21 14:58:18 +00:00
|
|
|
}
|
2002-02-05 22:13:51 +00:00
|
|
|
|
2018-05-29 10:28:20 +00:00
|
|
|
DEBUGF("scheme: \"%s\"\n"
|
|
|
|
"user: \"%s\"\n"
|
|
|
|
"password: \"%s\"\n"
|
|
|
|
"host: \"%s\"\n"
|
|
|
|
"port: \"%d\"\n"
|
|
|
|
"document: \"%s\"\n",
|
|
|
|
u->scheme, u->user, u->pwd,
|
|
|
|
u->host, u->port, u->doc);
|
2002-02-05 22:13:51 +00:00
|
|
|
|
|
|
|
return (u);
|
1998-07-09 16:52:44 +00:00
|
|
|
|
|
|
|
ouch:
|
2002-02-05 22:13:51 +00:00
|
|
|
free(u);
|
|
|
|
return (NULL);
|
1998-07-09 16:52:44 +00:00
|
|
|
}
|
2000-05-11 13:31:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Free a URL
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
fetchFreeURL(struct url *u)
|
|
|
|
{
|
2002-02-05 22:13:51 +00:00
|
|
|
free(u->doc);
|
|
|
|
free(u);
|
2000-05-11 13:31:02 +00:00
|
|
|
}
|