-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http2: add req and res options to server creation
Add optional Http2ServerRequest and Http2ServerResponse options to createServer and createSecureServer. Allows custom req & res classes that extend the default ones to be used without overriding the prototype. Backport-PR-URL: #20456 PR-URL: #15560 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
- Loading branch information
Showing
5 changed files
with
95 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
|
||
class MyServerRequest extends h2.Http2ServerRequest { | ||
getUserAgent() { | ||
return this.headers['user-agent'] || 'unknown'; | ||
} | ||
} | ||
|
||
const server = h2.createServer({ | ||
Http2ServerRequest: MyServerRequest | ||
}, (req, res) => { | ||
assert.strictEqual(req.getUserAgent(), 'node-test'); | ||
|
||
res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
res.end(); | ||
}); | ||
server.listen(0); | ||
|
||
server.on('listening', common.mustCall(() => { | ||
|
||
const client = h2.connect(`http://localhost:${server.address().port}`); | ||
const req = client.request({ | ||
':path': '/', | ||
'User-Agent': 'node-test' | ||
}); | ||
|
||
req.on('response', common.mustCall()); | ||
|
||
req.resume(); | ||
req.on('end', common.mustCall(() => { | ||
server.close(); | ||
client.destroy(); | ||
})); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
const h2 = require('http2'); | ||
|
||
class MyServerResponse extends h2.Http2ServerResponse { | ||
status(code) { | ||
return this.writeHead(code, { 'Content-Type': 'text/plain' }); | ||
} | ||
} | ||
|
||
const server = h2.createServer({ | ||
Http2ServerResponse: MyServerResponse | ||
}, (req, res) => { | ||
res.status(200); | ||
res.end(); | ||
}); | ||
server.listen(0); | ||
|
||
server.on('listening', common.mustCall(() => { | ||
|
||
const client = h2.connect(`http://localhost:${server.address().port}`); | ||
const req = client.request({ ':path': '/' }); | ||
|
||
req.on('response', common.mustCall()); | ||
|
||
req.resume(); | ||
req.on('end', common.mustCall(() => { | ||
server.close(); | ||
client.destroy(); | ||
})); | ||
})); |