mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
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 https://github.com/nodejs/node/issues/28673 Fixes: https://github.com/nodejs/node/issues/28673 PR-URL: https://github.com/nodejs/node/pull/36505 Reviewed-By: Robert Nagy <ronagy@icloud.com>
This commit is contained in:
1 parent
a5ffdaee1c
commit
70ee7dce62
6 files changed
+65
No files matched your search
@@ -1584,6 +1584,15 @@ Removes a header that's queued for implicit sending.
|
||||
response.removeHeader('Content-Encoding');
|
||||
```
|
||||
|
||||
### `response.req`
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* {http.IncomingMessage}
|
||||
|
||||
A reference to the original HTTP `request` object.
|
||||
|
||||
### `response.sendDate`
|
||||
<!-- YAML
|
||||
added: v0.7.5
|
||||
|
||||
@@ -3437,6 +3437,15 @@ Removes a header that has been queued for implicit sending.
|
||||
response.removeHeader('Content-Encoding');
|
||||
```
|
||||
|
||||
### `response.req`
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* {http2.Http2ServerRequest}
|
||||
|
||||
A reference to the original HTTP2 `request` object.
|
||||
|
||||
#### `response.sendDate`
|
||||
<!-- YAML
|
||||
added: v8.4.0
|
||||
|
||||
@@ -181,6 +181,7 @@ function ServerResponse(req) {
|
||||
|
||||
if (req.method === 'HEAD') this._hasBody = false;
|
||||
|
||||
this.req = req;
|
||||
this.sendDate = true;
|
||||
this._sent100 = false;
|
||||
this._expect_continue = false;
|
||||
|
||||
@@ -529,6 +529,10 @@ class Http2ServerResponse extends Stream {
|
||||
return this[kStream].headersSent;
|
||||
}
|
||||
|
||||
get req() {
|
||||
return this[kStream][kRequest];
|
||||
}
|
||||
|
||||
get sendDate() {
|
||||
return this[kState].sendDate;
|
||||
}
|
||||
|
||||
@@ -49,6 +49,8 @@ const server = http.createServer(function(req, res) {
|
||||
res.id = request_number;
|
||||
req.id = request_number++;
|
||||
|
||||
assert.strictEqual(res.req, req);
|
||||
|
||||
if (req.id === 0) {
|
||||
assert.strictEqual(req.method, 'GET');
|
||||
assert.strictEqual(url.parse(req.url).pathname, '/hello');
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
if (!common.hasCrypto)
|
||||
common.skip('missing crypto');
|
||||
const assert = require('assert');
|
||||
const h2 = require('http2');
|
||||
|
||||
// Http2ServerResponse should expose convenience properties
|
||||
|
||||
const server = h2.createServer();
|
||||
server.listen(0, common.mustCall(function() {
|
||||
const port = server.address().port;
|
||||
server.once('request', common.mustCall(function(request, response) {
|
||||
assert.strictEqual(response.req, request);
|
||||
|
||||
response.on('finish', common.mustCall(function() {
|
||||
process.nextTick(() => {
|
||||
server.close();
|
||||
});
|
||||
}));
|
||||
response.end();
|
||||
}));
|
||||
|
||||
const url = `http://localhost:${port}`;
|
||||
const client = h2.connect(url, common.mustCall(function() {
|
||||
const headers = {
|
||||
':path': '/foobar',
|
||||
':method': 'GET',
|
||||
':scheme': 'http',
|
||||
':authority': `localhost:${port}`
|
||||
};
|
||||
const request = client.request(headers);
|
||||
request.on('end', common.mustCall(function() {
|
||||
client.close();
|
||||
}));
|
||||
request.end();
|
||||
request.resume();
|
||||
}));
|
||||
}));
|
||||
Reference in new issue
Block a user