Skip to content

Commit 70ee7dc

Browse files
ianstormtaylorruyadorno
authored andcommitted
http: attach request as res.req
This change makes it possible for userland http-related modules to pave over edge cases that require referencing the original request when handling a response--making a "Lodash for HTTP" library possible. More information and research in #28673 Fixes: #28673 PR-URL: #36505 Reviewed-By: Robert Nagy <ronagy@icloud.com>
1 parent a5ffdae commit 70ee7dc

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed

doc/api/http.md

+9
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,15 @@ Removes a header that's queued for implicit sending.
15841584
response.removeHeader('Content-Encoding');
15851585
```
15861586

1587+
### `response.req`
1588+
<!-- YAML
1589+
added: REPLACEME
1590+
-->
1591+
1592+
* {http.IncomingMessage}
1593+
1594+
A reference to the original HTTP `request` object.
1595+
15871596
### `response.sendDate`
15881597
<!-- YAML
15891598
added: v0.7.5

doc/api/http2.md

+9
Original file line numberDiff line numberDiff line change
@@ -3437,6 +3437,15 @@ Removes a header that has been queued for implicit sending.
34373437
response.removeHeader('Content-Encoding');
34383438
```
34393439

3440+
### `response.req`
3441+
<!-- YAML
3442+
added: REPLACEME
3443+
-->
3444+
3445+
* {http2.Http2ServerRequest}
3446+
3447+
A reference to the original HTTP2 `request` object.
3448+
34403449
#### `response.sendDate`
34413450
<!-- YAML
34423451
added: v8.4.0

lib/_http_server.js

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ function ServerResponse(req) {
181181

182182
if (req.method === 'HEAD') this._hasBody = false;
183183

184+
this.req = req;
184185
this.sendDate = true;
185186
this._sent100 = false;
186187
this._expect_continue = false;

lib/internal/http2/compat.js

+4
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,10 @@ class Http2ServerResponse extends Stream {
529529
return this[kStream].headersSent;
530530
}
531531

532+
get req() {
533+
return this[kStream][kRequest];
534+
}
535+
532536
get sendDate() {
533537
return this[kState].sendDate;
534538
}

test/parallel/test-http-server.js

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ const server = http.createServer(function(req, res) {
4949
res.id = request_number;
5050
req.id = request_number++;
5151

52+
assert.strictEqual(res.req, req);
53+
5254
if (req.id === 0) {
5355
assert.strictEqual(req.method, 'GET');
5456
assert.strictEqual(url.parse(req.url).pathname, '/hello');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
const assert = require('assert');
7+
const h2 = require('http2');
8+
9+
// Http2ServerResponse should expose convenience properties
10+
11+
const server = h2.createServer();
12+
server.listen(0, common.mustCall(function() {
13+
const port = server.address().port;
14+
server.once('request', common.mustCall(function(request, response) {
15+
assert.strictEqual(response.req, request);
16+
17+
response.on('finish', common.mustCall(function() {
18+
process.nextTick(() => {
19+
server.close();
20+
});
21+
}));
22+
response.end();
23+
}));
24+
25+
const url = `http://localhost:${port}`;
26+
const client = h2.connect(url, common.mustCall(function() {
27+
const headers = {
28+
':path': '/foobar',
29+
':method': 'GET',
30+
':scheme': 'http',
31+
':authority': `localhost:${port}`
32+
};
33+
const request = client.request(headers);
34+
request.on('end', common.mustCall(function() {
35+
client.close();
36+
}));
37+
request.end();
38+
request.resume();
39+
}));
40+
}));

0 commit comments

Comments
 (0)