From 10851dc4ad91601a58cf679252ebc43d2f17e1c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Tue, 25 Jul 2000 11:45:38 +0000 Subject: [PATCH] Centralize the default port finding code. Work around YA Apache bug: don't send port in Host: header if it's the default port. --- lib/libfetch/common.h | 3 +++ lib/libfetch/ftp.c | 34 +++++++++++++++++----------------- lib/libfetch/http.c | 39 +++++++++++++++++++++++---------------- 3 files changed, 43 insertions(+), 33 deletions(-) diff --git a/lib/libfetch/common.h b/lib/libfetch/common.h index c39e879434b0..970153a59499 100644 --- a/lib/libfetch/common.h +++ b/lib/libfetch/common.h @@ -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; diff --git a/lib/libfetch/ftp.c b/lib/libfetch/ftp.c index a5eb1d351734..8e38beb7bf27 100644 --- a/lib/libfetch/ftp.c +++ b/lib/libfetch/ftp.c @@ -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)) { diff --git a/lib/libfetch/http.c b/lib/libfetch/http.c index 090114283286..7f6f437c3893 100644 --- a/lib/libfetch/http.c +++ b/lib/libfetch/http.c @@ -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);