Skip to content

Commit

Permalink
Skip HTTP/2 headers when making a HTTP/1 request (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry authored Nov 10, 2023
1 parent cd66366 commit 1bec004
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
17 changes: 17 additions & 0 deletions source/auto.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,23 @@ module.exports = async (input, options, callback) => {
options.agent = agent.http;
}

// If we're sending HTTP/1.1, handle any explicitly set H2 headers in the options:
if (options.headers) {
// :authority is equivalent to the http/1.1 host header
if (options.headers[':authority']) {
if (!options.headers.host) {
options.headers.host = options.headers[':authority'];
}

delete options.headers[':authority'];
}

// All other H2 headers are represented in the request-line, separate to headers
delete options.headers[':method'];
delete options.headers[':scheme'];
delete options.headers[':path'];
}

return delayAsyncDestroy(http.request(options, callback));
};

Expand Down
20 changes: 20 additions & 0 deletions test/auto.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ test('https', async t => {
t.is(data, 'http/1.1');
});

test('https with http2 headers', async t => {
const request = await http2.auto({
protocol: 'https:',
hostname: 'localhost',
port: h2s.address().port,
ALPNProtocols: ['http/1.1'],
headers: {
':method': 'GET',
':scheme': 'https',
':authority': `localhost:${h2s.address().port}`,
':path': '/'
}
});
request.end();

const response = await pEvent(request, 'response');
const data = await getStream(response);
t.is(data, 'http/1.1');
});

test('https agent', async t => {
const agent = new https.Agent();

Expand Down

0 comments on commit 1bec004

Please sign in to comment.