Skip to content

Commit 8aac536

Browse files
authored
Fix custom Host header with arbitrary case (node-fetch#430)
Regression since 1592ca1. Fixes: node-fetch#416 Fixes: node-fetch#425
1 parent c012c41 commit 8aac536

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

src/headers.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,25 @@ Object.defineProperty(HeadersIteratorPrototype, Symbol.toStringTag, {
323323
configurable: true
324324
});
325325

326+
/**
327+
* Export the Headers object in a form that Node.js can consume.
328+
*
329+
* @param Headers headers
330+
* @return Object
331+
*/
332+
export function exportNodeCompatibleHeaders(headers) {
333+
const obj = Object.assign({ __proto__: null }, headers[MAP]);
334+
335+
// http.request() only supports string as Host header. This hack makes
336+
// specifying custom Host header possible.
337+
const hostHeaderKey = find(headers[MAP], 'Host');
338+
if (hostHeaderKey !== undefined) {
339+
obj[hostHeaderKey] = obj[hostHeaderKey][0];
340+
}
341+
342+
return obj;
343+
}
344+
326345
/**
327346
* Create a Headers object from an object of headers, ignoring those that do
328347
* not conform to HTTP grammar productions.

src/index.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ export default function fetch(url, opts) {
4343

4444
const send = (options.protocol === 'https:' ? https : http).request;
4545

46-
// http.request only support string as host header, this hack make custom host header possible
47-
if (options.headers.host) {
48-
options.headers.host = options.headers.host[0];
49-
}
50-
5146
// send request
5247
const req = send(options);
5348
let reqTimeout;

src/request.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* All spec algorithm step numbers are based on https://fetch.spec.whatwg.org/commit-snapshots/ae716822cb3a61843226cd090eefc6589446c1d2/.
88
*/
99

10-
import Headers from './headers.js';
10+
import Headers, { exportNodeCompatibleHeaders } from './headers.js';
1111
import Body, { clone, extractContentType, getTotalBytes } from './body';
1212

1313
const { format: format_url, parse: parse_url } = require('url');
@@ -200,7 +200,7 @@ export function getNodeRequestOptions(request) {
200200

201201
return Object.assign({}, parsedURL, {
202202
method: request.method,
203-
headers: headers.raw(),
203+
headers: exportNodeCompatibleHeaders(headers),
204204
agent: request.agent
205205
});
206206
}

test/test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,20 @@ describe('node-fetch', () => {
208208
});
209209
});
210210

211+
it('should accept custom HoSt header', function() {
212+
const url = `${base}inspect`;
213+
const opts = {
214+
headers: {
215+
HoSt: 'example.com'
216+
}
217+
};
218+
return fetch(url, opts).then(res => {
219+
return res.json();
220+
}).then(res => {
221+
expect(res.headers['host']).to.equal('example.com');
222+
});
223+
});
224+
211225
it('should follow redirect code 301', function() {
212226
const url = `${base}redirect/301`;
213227
return fetch(url).then(res => {

0 commit comments

Comments
 (0)