Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose headers and rawHeaders on an http.ClientRequest "information" event #28459

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,18 @@ added: v10.0.0
-->

* `info` {Object}
* `httpVersion` {string}
* `httpVersionMajor` {integer}
* `httpVersionMinor` {integer}
* `statusCode` {integer}

Emitted when the server sends a 1xx response (excluding 101 Upgrade). The
listeners of this event will receive an object containing the status code.
* `statusMessage` {string}
* `headers` {Object}
* `rawHeaders` {string[]}

Emitted when the server sends a 1xx intermediate response (excluding 101
Upgrade). The listeners of this event will receive an object containing the
HTTP version, status code, status message, key-value headers object,
and array with the raw header names followed by their respective values.

```js
const http = require('http');
Expand Down
10 changes: 9 additions & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,15 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
req.emit('continue');
}
// Send information events to all 1xx responses except 101 Upgrade.
req.emit('information', { statusCode: res.statusCode });
req.emit('information', {
statusCode: res.statusCode,
statusMessage: res.statusMessage,
httpVersion: res.httpVersion,
httpVersionMajor: res.httpVersionMajor,
httpVersionMinor: res.httpVersionMinor,
headers: res.headers,
rawHeaders: res.rawHeaders
});
awwright marked this conversation as resolved.
Show resolved Hide resolved

return 1; // Skip body but don't treat as Upgrade.
}
Expand Down
64 changes: 64 additions & 0 deletions test/parallel/test-http-information-headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';
require('../common');
const assert = require('assert');
const http = require('http');
const Countdown = require('../common/countdown');

const test_res_body = 'other stuff!\n';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const test_res_body = 'other stuff!\n';
const responseBody = 'other stuff!\n';

Just a nit only because we prefer camelCase on js but it can be ignored.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is almost an identical copy of another file, the same variable probably there too.

const countdown = new Countdown(2, () => server.close());

const server = http.createServer((req, res) => {
console.error('Server sending informational message #1...');
// These function calls may rewritten as necessary
// to call res.writeHead instead
res._writeRaw('HTTP/1.1 102 Processing\r\n');
res._writeRaw('Foo: Bar\r\n');
res._writeRaw('\r\n');
console.error('Server sending full response...');
res.writeHead(200, {
'Content-Type': 'text/plain',
'ABCD': '1'
});
res.end(test_res_body);
});

server.listen(0, function() {
const req = http.request({
port: this.address().port,
path: '/world'
});
req.end();
console.error('Client sending request...');

let body = '';

req.on('information', function(res) {
assert.strictEqual(res.httpVersion, '1.1');
assert.strictEqual(res.httpVersionMajor, 1);
assert.strictEqual(res.httpVersionMinor, 1);
assert.strictEqual(res.statusCode, 102,
`Received ${res.statusCode}, not 102.`);
assert.strictEqual(res.statusMessage, 'Processing',
`Received ${res.statusMessage}, not "Processing".`);
assert.strictEqual(res.headers.foo, 'Bar');
assert.strictEqual(res.rawHeaders[0], 'Foo');
assert.strictEqual(res.rawHeaders[1], 'Bar');
console.error('Client got 102 Processing...');
countdown.dec();
});

req.on('response', function(res) {
// Check that all 102 Processing received before full response received.
assert.strictEqual(countdown.remaining, 1);
assert.strictEqual(res.statusCode, 200,
`Final status code was ${res.statusCode}, not 200.`);
res.setEncoding('utf8');
res.on('data', function(chunk) { body += chunk; });
res.on('end', function() {
console.error('Got full response.');
assert.strictEqual(body, test_res_body);
assert.ok('abcd' in res.headers);
countdown.dec();
});
});
});