Centralize the default port finding code.

Work around YA Apache bug: don't send port in Host: header if it's the
default port.
This commit is contained in:
Dag-Erling Smørgrav 2000-07-25 11:45:38 +00:00
parent 8bf72aef7c
commit 10851dc4ad
3 changed files with 43 additions and 33 deletions

View File

@ -31,6 +31,9 @@
#ifndef _COMMON_H_INCLUDED
#define _COMMON_H_INCLUDED
#define FTP_DEFAULT_PORT 21
#define HTTP_DEFAULT_PORT 80
/* Structure used for error message lists */
struct fetcherr {
const int num, cat;

View File

@ -75,7 +75,6 @@
#define FTP_ANONYMOUS_USER "ftp"
#define FTP_ANONYMOUS_PASSWORD "ftp"
#define FTP_DEFAULT_PORT 21
#define FTP_OPEN_DATA_CONNECTION 150
#define FTP_OK 200
@ -569,6 +568,19 @@ _ftp_transfer(int cd, char *oper, char *file,
return NULL;
}
/*
* Return default port
*/
static int
_ftp_default_port(void)
{
struct servent *se;
if ((se = getservbyname("ftp", "tcp")) != NULL)
return ntohs(se->s_port);
return FTP_DEFAULT_PORT;
}
/*
* Log on to FTP server
*/
@ -611,14 +623,8 @@ _ftp_connect(char *host, int port, char *user, char *pwd, char *flags)
/* XXX we should emit some kind of warning */
}
}
if (!pp) {
struct servent *se;
if ((se = getservbyname("ftp", "tcp")) != NULL)
pp = ntohs(se->s_port);
else
pp = FTP_DEFAULT_PORT;
}
if (!pp)
pp = _ftp_default_port();
if (q) {
#ifdef INET6
if (q > p && *p == '[' && *(q - 1) == ']') {
@ -732,14 +738,8 @@ _ftp_cached_connect(struct url *url, char *flags)
cd = -1;
/* set default port */
if (!url->port) {
struct servent *se;
if ((se = getservbyname("ftp", "tcp")) != NULL)
url->port = ntohs(se->s_port);
else
url->port = FTP_DEFAULT_PORT;
}
if (!url->port)
url->port = _ftp_default_port();
/* try to use previously cached connection */
if (_ftp_isconnected(url)) {

View File

@ -599,6 +599,23 @@ _http_authorize(int fd, char *hdr, char *p)
* 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, "ftp") == 0)
return FTP_DEFAULT_PORT;
if (strcasecmp(scheme, "http") == 0)
return HTTP_DEFAULT_PORT;
return 0;
}
/*
* Connect to the specified HTTP proxy server.
*/
@ -715,21 +732,8 @@ _http_connect(struct url *URL, int *proxy, char *flags)
af = AF_INET6;
/* check port */
if (!URL->port) {
struct servent *se;
/* Scheme can be ftp if we're using a proxy */
if (strcasecmp(URL->scheme, "ftp") == 0)
if ((se = getservbyname("ftp", "tcp")) != NULL)
URL->port = ntohs(se->s_port);
else
URL->port = 21;
else
if ((se = getservbyname("http", "tcp")) != NULL)
URL->port = ntohs(se->s_port);
else
URL->port = 80;
}
if (!URL->port)
URL->port = _http_default_port(URL->scheme);
if (!direct && (p = getenv("HTTP_PROXY")) != NULL && *p != '\0') {
/* attempt to connect to proxy server */
@ -840,7 +844,10 @@ _http_request(struct url *URL, char *op, struct url_stat *us, char *flags)
}
/* other headers */
_http_cmd(fd, "Host: %s:%d", host, url->port);
if (url->port == _http_default_port(url->scheme))
_http_cmd(fd, "Host: %s", host);
else
_http_cmd(fd, "Host: %s:%d", host, url->port);
_http_cmd(fd, "User-Agent: %s " _LIBFETCH_VER, __progname);
if (url->offset)
_http_cmd(fd, "Range: bytes=%lld-", url->offset);