Skip to content

Commit

Permalink
feat: ensure that the host does not include the port when unnecessary
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Feb 27, 2017
1 parent f482829 commit 3e8a797
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

/* eslint-disable no-process-env */

import {
parse as parseUrl
} from 'url';
import fetch, {
Headers,
Request,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 3e8a797

Please sign in to comment.