-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
HTTP/1 createServer() options to pass custom Request and Response classes #15752
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,7 +65,8 @@ See [`http.Server#keepAliveTimeout`][]. | |
<!-- YAML | ||
added: v0.3.4 | ||
--> | ||
- `options` {Object} Accepts `options` from [`tls.createServer()`][] and [`tls.createSecureContext()`][]. | ||
- `options` {Object} Accepts `options` from [`tls.createServer()`][], | ||
[`tls.createSecureContext()`][] and [`http.createServer()`][]. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add a link at the bottom for |
||
- `requestListener` {Function} A listener to be added to the `request` event. | ||
|
||
Example: | ||
|
@@ -258,6 +259,7 @@ const req = https.request(options, (res) => { | |
[`http.Server#setTimeout()`]: http.html#http_server_settimeout_msecs_callback | ||
[`http.Server#timeout`]: http.html#http_server_timeout | ||
[`http.Server`]: http.html#http_class_http_server | ||
[`http.createServer()`]: http.html#httpcreateserveroptions-requestlistener | ||
[`http.close()`]: http.html#http_server_close_callback | ||
[`http.get()`]: http.html#http_http_get_options_callback | ||
[`http.request()`]: http.html#http_http_request_options_callback | ||
|
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
41 changes: 41 additions & 0 deletions
41
test/parallel/test-http-server-options-incoming-message.js
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,41 @@ | ||
'use strict'; | ||
|
||
/** | ||
* This test covers http.Server({ IncomingMessage }) option: | ||
* With IncomingMessage option the server should use | ||
* the new class for creating req Object instead of the default | ||
* http.IncomingMessage. | ||
*/ | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
class MyIncomingMessage extends http.IncomingMessage { | ||
getUserAgent() { | ||
return this.headers['user-agent'] || 'unknown'; | ||
} | ||
} | ||
|
||
const server = http.Server({ | ||
IncomingMessage: MyIncomingMessage | ||
}, common.mustCall(function(req, res) { | ||
assert.strictEqual(req.getUserAgent(), 'node-test'); | ||
res.statusCode = 200; | ||
res.end(); | ||
})); | ||
server.listen(); | ||
|
||
server.on('listening', function makeRequest() { | ||
http.get({ | ||
port: this.address().port, | ||
headers: { | ||
'User-Agent': 'node-test' | ||
} | ||
}, (res) => { | ||
assert.strictEqual(res.statusCode, 200); | ||
res.on('end', () => { | ||
server.close(); | ||
}); | ||
res.resume(); | ||
}); | ||
}); |
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,35 @@ | ||
'use strict'; | ||
|
||
/** | ||
* This test covers http.Server({ ServerResponse }) option: | ||
* With ServerResponse option the server should use | ||
* the new class for creating res Object instead of the default | ||
* http.ServerResponse. | ||
*/ | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
class MyServerResponse extends http.ServerResponse { | ||
status(code) { | ||
return this.writeHead(code, { 'Content-Type': 'text/plain' }); | ||
} | ||
} | ||
|
||
const server = http.Server({ | ||
ServerResponse: MyServerResponse | ||
}, common.mustCall(function(req, res) { | ||
res.status(200); | ||
res.end(); | ||
})); | ||
server.listen(); | ||
|
||
server.on('listening', function makeRequest() { | ||
http.get({ port: this.address().port }, (res) => { | ||
assert.strictEqual(res.statusCode, 200); | ||
res.on('end', () => { | ||
server.close(); | ||
}); | ||
res.resume(); | ||
}); | ||
}); |
90 changes: 90 additions & 0 deletions
90
test/parallel/test-http2-https-fallback-http-server-options.js
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,90 @@ | ||
// Flags: --expose-http2 | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const fixtures = require('../common/fixtures'); | ||
|
||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const assert = require('assert'); | ||
const url = require('url'); | ||
const tls = require('tls'); | ||
const http2 = require('http2'); | ||
const https = require('https'); | ||
const http = require('http'); | ||
|
||
const key = fixtures.readKey('agent8-key.pem'); | ||
const cert = fixtures.readKey('agent8-cert.pem'); | ||
const ca = fixtures.readKey('fake-startcom-root-cert.pem'); | ||
|
||
function onRequest(request, response) { | ||
const { socket: { alpnProtocol } } = request.httpVersion === '2.0' ? | ||
request.stream.session : request; | ||
response.status(200); | ||
response.end(JSON.stringify({ | ||
alpnProtocol, | ||
httpVersion: request.httpVersion, | ||
userAgent: request.getUserAgent() | ||
})); | ||
} | ||
|
||
class MyIncomingMessage extends http.IncomingMessage { | ||
getUserAgent() { | ||
return this.headers['user-agent'] || 'unknown'; | ||
} | ||
} | ||
|
||
class MyServerResponse extends http.ServerResponse { | ||
status(code) { | ||
return this.writeHead(code, { 'Content-Type': 'application/json' }); | ||
} | ||
} | ||
|
||
// HTTP/2 & HTTP/1.1 server | ||
{ | ||
const server = http2.createSecureServer( | ||
{ | ||
cert, | ||
key, allowHTTP1: true, | ||
Http1IncomingMessage: MyIncomingMessage, | ||
Http1ServerResponse: MyServerResponse | ||
}, | ||
common.mustCall(onRequest, 1) | ||
); | ||
|
||
server.listen(0); | ||
|
||
server.on('listening', common.mustCall(() => { | ||
const { port } = server.address(); | ||
const origin = `https://localhost:${port}`; | ||
|
||
// HTTP/1.1 client | ||
https.get( | ||
Object.assign(url.parse(origin), { | ||
secureContext: tls.createSecureContext({ ca }), | ||
headers: { 'User-Agent': 'node-test' } | ||
}), | ||
common.mustCall((response) => { | ||
assert.strictEqual(response.statusCode, 200); | ||
assert.strictEqual(response.statusMessage, 'OK'); | ||
assert.strictEqual( | ||
response.headers['content-type'], | ||
'application/json' | ||
); | ||
|
||
response.setEncoding('utf8'); | ||
let raw = ''; | ||
response.on('data', (chunk) => { raw += chunk; }); | ||
response.on('end', common.mustCall(() => { | ||
const { alpnProtocol, httpVersion, userAgent } = JSON.parse(raw); | ||
assert.strictEqual(alpnProtocol, false); | ||
assert.strictEqual(httpVersion, '1.1'); | ||
assert.strictEqual(userAgent, 'node-test'); | ||
|
||
server.close(); | ||
})); | ||
}) | ||
); | ||
})); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add the fact that the
options
argument is supported now to the changelog? i.e.(This could also be done when landing, I guess, so I’m not removing the
ready
label.)