From 3e8a7979df37e8a8b94e080e2145619aa90c6d03 Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Mon, 27 Feb 2017 17:20:12 +0000 Subject: [PATCH] feat: ensure that the host does not include the port when unnecessary --- src/index.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/index.js b/src/index.js index 21697ed..989fa05 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,9 @@ /* eslint-disable no-process-env */ +import { + parse as parseUrl +} from 'url'; import fetch, { Headers, Request, @@ -38,6 +41,18 @@ export default async (url: string, userConfiguration: ConfigurationType = {}) => configuration.agent = new HttpsProxyAgent(process.env.HTTP_PROXY); } + const urlTokens = parseUrl(url); + + if (!urlTokens.hostname) { + throw new Error('Invalid URL.'); + } + + const host = urlTokens.port === 80 ? urlTokens.host : urlTokens.hostname; + + configuration.headers = configuration.headers || {}; + + configuration.headers.host = host; + return attemptRequest(() => { return fetch(url, configuration); }, configuration.validateResponse || validateResponse, configuration.retry);