diff --git a/lib/libfetch/common.c b/lib/libfetch/common.c index c03b1f52b0f..4d05a6b6dcd 100644 --- a/lib/libfetch/common.c +++ b/lib/libfetch/common.c @@ -167,6 +167,40 @@ _fetch_info(char *fmt, ...) /*** 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. */ diff --git a/lib/libfetch/common.h b/lib/libfetch/common.h index 4470c5ba9d9..13723a3f83c 100644 --- a/lib/libfetch/common.h +++ b/lib/libfetch/common.h @@ -33,6 +33,8 @@ #define FTP_DEFAULT_PORT 21 #define HTTP_DEFAULT_PORT 80 +#define FTP_DEFAULT_PROXY_PORT 21 +#define HTTP_DEFAULT_PROXY_PORT 3128 /* Structure used for error message lists */ struct fetcherr { @@ -43,6 +45,8 @@ struct fetcherr { void _fetch_seterr(struct fetcherr *p, int e); void _fetch_syserr(void); 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_getln(int fd, char **buf, size_t *size, size_t *len); int _fetch_putln(int fd, char *str, size_t len); diff --git a/lib/libfetch/ftp.c b/lib/libfetch/ftp.c index 18da8b295f6..3e563429c14 100644 --- a/lib/libfetch/ftp.c +++ b/lib/libfetch/ftp.c @@ -710,19 +710,6 @@ ouch: 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 */ @@ -776,7 +763,7 @@ _ftp_connect(struct url *url, struct url *purl, char *flags) user = url->user; if (!user || !*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); else if (purl) 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 */ if (!url->port) - url->port = _ftp_default_port(); + url->port = _fetch_default_port(url->scheme); /* try to use previously cached connection */ if (_ftp_isconnected(url)) { @@ -890,9 +877,9 @@ _ftp_get_proxy(void) if (((p = getenv("FTP_PROXY")) || (p = getenv("HTTP_PROXY"))) && *p && (purl = fetchParseURL(p)) != NULL) { if (!*purl->scheme) - strcpy(purl->scheme, SCHEME_FTP); + strcpy(purl->scheme, SCHEME_HTTP); if (!purl->port) - purl->port = _ftp_default_port(); + purl->port = _fetch_default_proxy_port(purl->scheme); if (strcasecmp(purl->scheme, SCHEME_FTP) == 0 || strcasecmp(purl->scheme, SCHEME_HTTP) == 0) return purl; diff --git a/lib/libfetch/http.c b/lib/libfetch/http.c index e8d193bf439..9c4bebb3f10 100644 --- a/lib/libfetch/http.c +++ b/lib/libfetch/http.c @@ -608,23 +608,6 @@ _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, 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. */ @@ -672,7 +655,7 @@ _http_get_proxy() if (!*purl->scheme) strcpy(purl->scheme, SCHEME_HTTP); 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) return purl; fetchFreeURL(purl); @@ -733,7 +716,7 @@ _http_request(struct url *URL, char *op, struct url_stat *us, retry: /* check port */ if (!url->port) - url->port = _http_default_port(url->scheme); + url->port = _fetch_default_port(url->scheme); /* connect to server or proxy */ 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 */ - if (url->port == _http_default_port(url->scheme)) + if (url->port == _fetch_default_port(url->scheme)) _http_cmd(fd, "Host: %s", host); else _http_cmd(fd, "Host: %s:%d", host, url->port);