Skip to content

Commit 2308c74

Browse files
awwrighttargos
authored andcommitted
http: expose headers on an http.ClientRequest "information" event
1xx intermediate status responses are allowed to have headers; so expose the "httpVersion", "httpVersionMajor", "httpVersionMinor", "headers", "rawHeaders", and "statusMessage" properties on this event. PR-URL: #28459 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 9b2eee1 commit 2308c74

File tree

3 files changed

+84
-4
lines changed

3 files changed

+84
-4
lines changed

doc/api/http.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,18 @@ added: v10.0.0
421421
-->
422422

423423
* `info` {Object}
424+
* `httpVersion` {string}
425+
* `httpVersionMajor` {integer}
426+
* `httpVersionMinor` {integer}
424427
* `statusCode` {integer}
425-
426-
Emitted when the server sends a 1xx response (excluding 101 Upgrade). The
427-
listeners of this event will receive an object containing the status code.
428+
* `statusMessage` {string}
429+
* `headers` {Object}
430+
* `rawHeaders` {string[]}
431+
432+
Emitted when the server sends a 1xx intermediate response (excluding 101
433+
Upgrade). The listeners of this event will receive an object containing the
434+
HTTP version, status code, status message, key-value headers object,
435+
and array with the raw header names followed by their respective values.
428436

429437
```js
430438
const http = require('http');

lib/_http_client.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,15 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
545545
req.emit('continue');
546546
}
547547
// Send information events to all 1xx responses except 101 Upgrade.
548-
req.emit('information', { statusCode: res.statusCode });
548+
req.emit('information', {
549+
statusCode: res.statusCode,
550+
statusMessage: res.statusMessage,
551+
httpVersion: res.httpVersion,
552+
httpVersionMajor: res.httpVersionMajor,
553+
httpVersionMinor: res.httpVersionMinor,
554+
headers: res.headers,
555+
rawHeaders: res.rawHeaders
556+
});
549557

550558
return 1; // Skip body but don't treat as Upgrade.
551559
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const http = require('http');
5+
const Countdown = require('../common/countdown');
6+
7+
const test_res_body = 'other stuff!\n';
8+
const countdown = new Countdown(2, () => server.close());
9+
10+
const server = http.createServer((req, res) => {
11+
console.error('Server sending informational message #1...');
12+
// These function calls may rewritten as necessary
13+
// to call res.writeHead instead
14+
res._writeRaw('HTTP/1.1 102 Processing\r\n');
15+
res._writeRaw('Foo: Bar\r\n');
16+
res._writeRaw('\r\n');
17+
console.error('Server sending full response...');
18+
res.writeHead(200, {
19+
'Content-Type': 'text/plain',
20+
'ABCD': '1'
21+
});
22+
res.end(test_res_body);
23+
});
24+
25+
server.listen(0, function() {
26+
const req = http.request({
27+
port: this.address().port,
28+
path: '/world'
29+
});
30+
req.end();
31+
console.error('Client sending request...');
32+
33+
let body = '';
34+
35+
req.on('information', function(res) {
36+
assert.strictEqual(res.httpVersion, '1.1');
37+
assert.strictEqual(res.httpVersionMajor, 1);
38+
assert.strictEqual(res.httpVersionMinor, 1);
39+
assert.strictEqual(res.statusCode, 102,
40+
`Received ${res.statusCode}, not 102.`);
41+
assert.strictEqual(res.statusMessage, 'Processing',
42+
`Received ${res.statusMessage}, not "Processing".`);
43+
assert.strictEqual(res.headers.foo, 'Bar');
44+
assert.strictEqual(res.rawHeaders[0], 'Foo');
45+
assert.strictEqual(res.rawHeaders[1], 'Bar');
46+
console.error('Client got 102 Processing...');
47+
countdown.dec();
48+
});
49+
50+
req.on('response', function(res) {
51+
// Check that all 102 Processing received before full response received.
52+
assert.strictEqual(countdown.remaining, 1);
53+
assert.strictEqual(res.statusCode, 200,
54+
`Final status code was ${res.statusCode}, not 200.`);
55+
res.setEncoding('utf8');
56+
res.on('data', function(chunk) { body += chunk; });
57+
res.on('end', function() {
58+
console.error('Got full response.');
59+
assert.strictEqual(body, test_res_body);
60+
assert.ok('abcd' in res.headers);
61+
countdown.dec();
62+
});
63+
});
64+
});

0 commit comments

Comments
 (0)