Use the documented (and historical) defaults. Centralize the decision logic

in order to avoid this bug in the future.

Submitted by:	se
This commit is contained in:
Dag-Erling Smørgrav 2000-11-10 08:43:40 +00:00
parent fb75554e54
commit e828ada709
4 changed files with 45 additions and 37 deletions

View File

@ -167,6 +167,40 @@ _fetch_info(char *fmt, ...)
/*** Network-related utility functions ***************************************/ /*** Network-related utility functions ***************************************/
/*
* Return the default port for a scheme
*/
int
_fetch_default_port(char *scheme)
{
struct servent *se;
if ((se = getservbyname(scheme, "tcp")) != NULL)
return ntohs(se->s_port);
if (strcasecmp(scheme, SCHEME_FTP) == 0)
return FTP_DEFAULT_PORT;
if (strcasecmp(scheme, SCHEME_HTTP) == 0)
return HTTP_DEFAULT_PORT;
return 0;
}
/*
* Return the default proxy port for a scheme
*/
int
_fetch_default_proxy_port(char *scheme)
{
struct servent *se;
if ((se = getservbyname(scheme, "tcp")) != NULL)
return ntohs(se->s_port);
if (strcasecmp(scheme, SCHEME_FTP) == 0)
return FTP_DEFAULT_PROXY_PORT;
if (strcasecmp(scheme, SCHEME_HTTP) == 0)
return HTTP_DEFAULT_PROXY_PORT;
return 0;
}
/* /*
* Establish a TCP connection to the specified port on the specified host. * Establish a TCP connection to the specified port on the specified host.
*/ */

View File

@ -33,6 +33,8 @@
#define FTP_DEFAULT_PORT 21 #define FTP_DEFAULT_PORT 21
#define HTTP_DEFAULT_PORT 80 #define HTTP_DEFAULT_PORT 80
#define FTP_DEFAULT_PROXY_PORT 21
#define HTTP_DEFAULT_PROXY_PORT 3128
/* Structure used for error message lists */ /* Structure used for error message lists */
struct fetcherr { struct fetcherr {
@ -43,6 +45,8 @@ struct fetcherr {
void _fetch_seterr(struct fetcherr *p, int e); void _fetch_seterr(struct fetcherr *p, int e);
void _fetch_syserr(void); void _fetch_syserr(void);
void _fetch_info(char *fmt, ...); void _fetch_info(char *fmt, ...);
int _fetch_default_port(char *);
int _fetch_default_proxy_port(char *);
int _fetch_connect(char *host, int port, int af, int verbose); int _fetch_connect(char *host, int port, int af, int verbose);
int _fetch_getln(int fd, char **buf, size_t *size, size_t *len); int _fetch_getln(int fd, char **buf, size_t *size, size_t *len);
int _fetch_putln(int fd, char *str, size_t len); int _fetch_putln(int fd, char *str, size_t len);

View File

@ -710,19 +710,6 @@ ouch:
return NULL; return NULL;
} }
/*
* Return default port
*/
static int
_ftp_default_port(void)
{
struct servent *se;
if ((se = getservbyname(SCHEME_FTP, "tcp")) != NULL)
return ntohs(se->s_port);
return FTP_DEFAULT_PORT;
}
/* /*
* Log on to FTP server * Log on to FTP server
*/ */
@ -776,7 +763,7 @@ _ftp_connect(struct url *url, struct url *purl, char *flags)
user = url->user; user = url->user;
if (!user || !*user) if (!user || !*user)
user = FTP_ANONYMOUS_USER; user = FTP_ANONYMOUS_USER;
if (purl && url->port == FTP_DEFAULT_PORT) if (purl && url->port == _fetch_default_port(url->scheme))
e = _ftp_cmd(cd, "USER %s@%s", user, url->host); e = _ftp_cmd(cd, "USER %s@%s", user, url->host);
else if (purl) else if (purl)
e = _ftp_cmd(cd, "USER %s@%s@%d", user, url->host, url->port); e = _ftp_cmd(cd, "USER %s@%s@%d", user, url->host, url->port);
@ -859,7 +846,7 @@ _ftp_cached_connect(struct url *url, struct url *purl, char *flags)
/* set default port */ /* set default port */
if (!url->port) if (!url->port)
url->port = _ftp_default_port(); url->port = _fetch_default_port(url->scheme);
/* try to use previously cached connection */ /* try to use previously cached connection */
if (_ftp_isconnected(url)) { if (_ftp_isconnected(url)) {
@ -890,9 +877,9 @@ _ftp_get_proxy(void)
if (((p = getenv("FTP_PROXY")) || (p = getenv("HTTP_PROXY"))) && if (((p = getenv("FTP_PROXY")) || (p = getenv("HTTP_PROXY"))) &&
*p && (purl = fetchParseURL(p)) != NULL) { *p && (purl = fetchParseURL(p)) != NULL) {
if (!*purl->scheme) if (!*purl->scheme)
strcpy(purl->scheme, SCHEME_FTP); strcpy(purl->scheme, SCHEME_HTTP);
if (!purl->port) if (!purl->port)
purl->port = _ftp_default_port(); purl->port = _fetch_default_proxy_port(purl->scheme);
if (strcasecmp(purl->scheme, SCHEME_FTP) == 0 || if (strcasecmp(purl->scheme, SCHEME_FTP) == 0 ||
strcasecmp(purl->scheme, SCHEME_HTTP) == 0) strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
return purl; return purl;

View File

@ -608,23 +608,6 @@ _http_authorize(int fd, char *hdr, char *p)
* Helper functions for connecting to a server or proxy * Helper functions for connecting to a server or proxy
*/ */
/*
* Return the default port for this scheme
*/
static int
_http_default_port(char *scheme)
{
struct servent *se;
if ((se = getservbyname(scheme, "tcp")) != NULL)
return ntohs(se->s_port);
if (strcasecmp(scheme, SCHEME_FTP) == 0)
return FTP_DEFAULT_PORT;
if (strcasecmp(scheme, SCHEME_HTTP) == 0)
return HTTP_DEFAULT_PORT;
return 0;
}
/* /*
* Connect to the correct HTTP server or proxy. * Connect to the correct HTTP server or proxy.
*/ */
@ -672,7 +655,7 @@ _http_get_proxy()
if (!*purl->scheme) if (!*purl->scheme)
strcpy(purl->scheme, SCHEME_HTTP); strcpy(purl->scheme, SCHEME_HTTP);
if (!purl->port) if (!purl->port)
purl->port = _http_default_port(SCHEME_HTTP); purl->port = _fetch_default_proxy_port(purl->scheme);
if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0) if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
return purl; return purl;
fetchFreeURL(purl); fetchFreeURL(purl);
@ -733,7 +716,7 @@ _http_request(struct url *URL, char *op, struct url_stat *us,
retry: retry:
/* check port */ /* check port */
if (!url->port) if (!url->port)
url->port = _http_default_port(url->scheme); url->port = _fetch_default_port(url->scheme);
/* connect to server or proxy */ /* connect to server or proxy */
if ((fd = _http_connect(url, purl, flags)) == -1) if ((fd = _http_connect(url, purl, flags)) == -1)
@ -781,7 +764,7 @@ _http_request(struct url *URL, char *op, struct url_stat *us,
} }
/* other headers */ /* other headers */
if (url->port == _http_default_port(url->scheme)) if (url->port == _fetch_default_port(url->scheme))
_http_cmd(fd, "Host: %s", host); _http_cmd(fd, "Host: %s", host);
else else
_http_cmd(fd, "Host: %s:%d", host, url->port); _http_cmd(fd, "Host: %s:%d", host, url->port);