summaryrefslogtreecommitdiffstats
path: root/lib/libfetch
diff options
context:
space:
mode:
authordes <des@FreeBSD.org>2000-07-25 11:45:38 +0000
committerdes <des@FreeBSD.org>2000-07-25 11:45:38 +0000
commit2de7b8d72b9f8324bd4e07219ca1dc1689ba4614 (patch)
tree11d2f391d90ea74c6b5e72fd1d769d97edc3e6ee /lib/libfetch
parent64de57e4e28f41b53af571e2f918c940e2286097 (diff)
downloadFreeBSD-src-2de7b8d72b9f8324bd4e07219ca1dc1689ba4614.zip
FreeBSD-src-2de7b8d72b9f8324bd4e07219ca1dc1689ba4614.tar.gz
Centralize the default port finding code.
Work around YA Apache bug: don't send port in Host: header if it's the default port.
Diffstat (limited to 'lib/libfetch')
-rw-r--r--lib/libfetch/common.h3
-rw-r--r--lib/libfetch/ftp.c34
-rw-r--r--lib/libfetch/http.c39
3 files changed, 43 insertions, 33 deletions
diff --git a/lib/libfetch/common.h b/lib/libfetch/common.h
index c39e879..970153a 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 a5eb1d3..8e38beb 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
@@ -570,6 +569,19 @@ ouch:
}
/*
+ * 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
*/
static int
@@ -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 0901142..7f6f437 100644
--- a/lib/libfetch/http.c
+++ b/lib/libfetch/http.c
@@ -600,6 +600,23 @@ _http_authorize(int fd, char *hdr, char *p)
*/
/*
+ * 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.
*/
static int
@@ -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);
OpenPOWER on IntegriCloud