| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7a432c1 commit c247cb0
9 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1663,10 +1663,20 @@ A collection of all the standard HTTP response status codes, and the | |||
| 1663 | 1663 | short description of each. For example, `http.STATUS_CODES[404] === 'Not | |
| 1664 | 1664 | Found'`. | |
| 1665 | 1665 | ||
| 1666 | - ## http.createServer([requestListener]) | ||
| 1666 | + ## http.createServer([options][, requestListener]) | ||
| 1667 | 1667 | <!-- YAML | |
| 1668 | 1668 | added: v0.1.13 | |
| 1669 | - --> | ||
| 1669 | + - version: REPLACEME | ||
| 1670 | + pr-url: https://github.com/nodejs/node/pull/15752 | ||
| 1671 | + description: The `options` argument is supported now. | ||
| 1672 | + --> | ||
| 1673 | + - `options` {Object} | ||
| 1674 | + * `IncomingMessage` {http.IncomingMessage} Specifies the IncomingMessage class | ||
| 1675 | + to be used. Useful for extending the original `IncomingMessage`. Defaults | ||
| 1676 | + to: `IncomingMessage` | ||
| 1677 | + * `ServerResponse` {http.ServerResponse} Specifies the ServerResponse class to | ||
| 1678 | + be used. Useful for extending the original `ServerResponse`. Defaults to: | ||
| 1679 | + `ServerResponse` | ||
| 1670 | 1680 | - `requestListener` {Function} | |
| 1671 | 1681 | ||
| 1672 | 1682 | * Returns: {http.Server} | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -65,7 +65,8 @@ See [`http.Server#keepAliveTimeout`][]. | |||
| 65 | 65 | <!-- YAML | |
| 66 | 66 | added: v0.3.4 | |
| 67 | 67 | --> | |
| 68 | - - `options` {Object} Accepts `options` from [`tls.createServer()`][] and [`tls.createSecureContext()`][]. | ||
| 68 | + - `options` {Object} Accepts `options` from [`tls.createServer()`][], | ||
| 69 | + [`tls.createSecureContext()`][] and [`http.createServer()`][]. | ||
| 69 | 70 | - `requestListener` {Function} A listener to be added to the `request` event. | |
| 70 | 71 | ||
| 71 | 72 | Example: | |
@@ -258,6 +259,7 @@ const req = https.request(options, (res) => { | |||
| 258 | 259 | [`http.Server#setTimeout()`]: http.html#http_server_settimeout_msecs_callback | |
| 259 | 260 | [`http.Server#timeout`]: http.html#http_server_timeout | |
| 260 | 261 | [`http.Server`]: http.html#http_class_http_server | |
| 262 | + [`http.createServer()`]: http.html#httpcreateserveroptions-requestlistener | ||
| 261 | 263 | [`http.close()`]: http.html#http_server_close_callback | |
| 262 | 264 | [`http.get()`]: http.html#http_http_get_options_callback | |
| 263 | 265 | [`http.request()`]: http.html#http_http_request_options_callback | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -34,6 +34,7 @@ const { | |||
| 34 | 34 | ||
| 35 | 35 | const debug = require('util').debuglog('http'); | |
| 36 | 36 | ||
| 37 | + const kIncomingMessage = Symbol('IncomingMessage'); | ||
| 37 | 38 | const kOnHeaders = HTTPParser.kOnHeaders | 0; | |
| 38 | 39 | const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0; | |
| 39 | 40 | const kOnBody = HTTPParser.kOnBody | 0; | |
@@ -73,7 +74,11 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method, | |||
| 73 | 74 | parser._url = ''; | |
| 74 | 75 | } | |
| 75 | 76 | ||
| 76 | - parser.incoming = new IncomingMessage(parser.socket); | ||
| 77 | + // Parser is also used by http client | ||
| 78 | + var ParserIncomingMessage = parser.socket && parser.socket.server ? | ||
| 79 | + parser.socket.server[kIncomingMessage] : IncomingMessage; | ||
| 80 | + | ||
| 81 | + parser.incoming = new ParserIncomingMessage(parser.socket); | ||
| 77 | 82 | parser.incoming.httpVersionMajor = versionMajor; | |
| 78 | 83 | parser.incoming.httpVersionMinor = versionMinor; | |
| 79 | 84 | parser.incoming.httpVersion = `${versionMajor}.${versionMinor}`; | |
@@ -300,5 +305,6 @@ module.exports = { | |||
| 300 | 305 | freeParser, | |
| 301 | 306 | httpSocketSetup, | |
| 302 | 307 | methods, | |
| 303 | - parsers | ||
| 308 | + parsers, | ||
| 309 | + kIncomingMessage | ||
| 304 | 310 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -33,6 +33,7 @@ const { | |||
| 33 | 33 | continueExpression, | |
| 34 | 34 | chunkExpression, | |
| 35 | 35 | httpSocketSetup, | |
| 36 | + kIncomingMessage, | ||
| 36 | 37 | _checkInvalidHeaderChar: checkInvalidHeaderChar | |
| 37 | 38 | } = require('_http_common'); | |
| 38 | 39 | const { OutgoingMessage } = require('_http_outgoing'); | |
@@ -41,9 +42,12 @@ const { | |||
| 41 | 42 | defaultTriggerAsyncIdScope, | |
| 42 | 43 | getOrSetAsyncId | |
| 43 | 44 | } = require('internal/async_hooks'); | |
| 45 | + const { IncomingMessage } = require('_http_incoming'); | ||
| 44 | 46 | const errors = require('internal/errors'); | |
| 45 | 47 | const Buffer = require('buffer').Buffer; | |
| 46 | 48 | ||
| 49 | + const kServerResponse = Symbol('ServerResponse'); | ||
| 50 | + | ||
| 47 | 51 | const STATUS_CODES = { | |
| 48 | 52 | 100: 'Continue', | |
| 49 | 53 | 101: 'Switching Protocols', | |
@@ -263,9 +267,19 @@ function writeHead(statusCode, reason, obj) { | |||
| 263 | 267 | // Docs-only deprecated: DEP0063 | |
| 264 | 268 | ServerResponse.prototype.writeHeader = ServerResponse.prototype.writeHead; | |
| 265 | 269 | ||
| 270 | + function Server(options, requestListener) { | ||
| 271 | + if (!(this instanceof Server)) return new Server(options, requestListener); | ||
| 272 | + | ||
| 273 | + if (typeof options === 'function') { | ||
| 274 | + requestListener = options; | ||
| 275 | + options = {}; | ||
| 276 | + } else if (options == null || typeof options === 'object') { | ||
| 277 | + options = util._extend({}, options); | ||
| 278 | + } | ||
| 279 | + | ||
| 280 | + this[kIncomingMessage] = options.IncomingMessage || IncomingMessage; | ||
| 281 | + this[kServerResponse] = options.ServerResponse || ServerResponse; | ||
| 266 | 282 | ||
| 267 | - function Server(requestListener) { | ||
| 268 | - if (!(this instanceof Server)) return new Server(requestListener); | ||
| 269 | 283 | net.Server.call(this, { allowHalfOpen: true }); | |
| 270 | 284 | ||
| 271 | 285 | if (requestListener) { | |
@@ -587,7 +601,7 @@ function parserOnIncoming(server, socket, state, req, keepAlive) { | |||
| 587 | 601 | } | |
| 588 | 602 | } | |
| 589 | 603 | ||
| 590 | - var res = new ServerResponse(req); | ||
| 604 | + var res = new server[kServerResponse](req); | ||
| 591 | 605 | res._onPendingData = updateOutgoingData.bind(undefined, socket, state); | |
| 592 | 606 | ||
| 593 | 607 | res.shouldKeepAlive = keepAlive; | |
@@ -690,5 +704,6 @@ module.exports = { | |||
| 690 | 704 | STATUS_CODES, | |
| 691 | 705 | Server, | |
| 692 | 706 | ServerResponse, | |
| 693 | - _connectionListener: connectionListener | ||
| 707 | + _connectionListener: connectionListener, | ||
| 708 | + kServerResponse | ||
| 694 | 709 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,6 +36,9 @@ const { inherits } = util; | |||
| 36 | 36 | const debug = util.debuglog('https'); | |
| 37 | 37 | const { urlToOptions, searchParamsSymbol } = require('internal/url'); | |
| 38 | 38 | const errors = require('internal/errors'); | |
| 39 | + const { IncomingMessage, ServerResponse } = require('http'); | ||
| 40 | + const { kIncomingMessage } = require('_http_common'); | ||
| 41 | + const { kServerResponse } = require('_http_server'); | ||
| 39 | 42 | ||
| 40 | 43 | function Server(opts, requestListener) { | |
| 41 | 44 | if (!(this instanceof Server)) return new Server(opts, requestListener); | |
@@ -57,6 +60,9 @@ function Server(opts, requestListener) { | |||
| 57 | 60 | opts.ALPNProtocols = ['http/1.1']; | |
| 58 | 61 | } | |
| 59 | 62 | ||
| 63 | + this[kIncomingMessage] = opts.IncomingMessage || IncomingMessage; | ||
| 64 | + this[kServerResponse] = opts.ServerResponse || ServerResponse; | ||
| 65 | + | ||
| 60 | 66 | tls.Server.call(this, opts, _connectionListener); | |
| 61 | 67 | ||
| 62 | 68 | this.httpAllowHalfOpen = false; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,41 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * This test covers http.Server({ IncomingMessage }) option: | ||
| 5 | + * With IncomingMessage option the server should use | ||
| 6 | + * the new class for creating req Object instead of the default | ||
| 7 | + * http.IncomingMessage. | ||
| 8 | + */ | ||
| 9 | + const common = require('../common'); | ||
| 10 | + const assert = require('assert'); | ||
| 11 | + const http = require('http'); | ||
| 12 | + | ||
| 13 | + class MyIncomingMessage extends http.IncomingMessage { | ||
| 14 | + getUserAgent() { | ||
| 15 | + return this.headers['user-agent'] || 'unknown'; | ||
| 16 | + } | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + const server = http.Server({ | ||
| 20 | + IncomingMessage: MyIncomingMessage | ||
| 21 | + }, common.mustCall(function(req, res) { | ||
| 22 | + assert.strictEqual(req.getUserAgent(), 'node-test'); | ||
| 23 | + res.statusCode = 200; | ||
| 24 | + res.end(); | ||
| 25 | + })); | ||
| 26 | + server.listen(); | ||
| 27 | + | ||
| 28 | + server.on('listening', function makeRequest() { | ||
| 29 | + http.get({ | ||
| 30 | + port: this.address().port, | ||
| 31 | + headers: { | ||
| 32 | + 'User-Agent': 'node-test' | ||
| 33 | + } | ||
| 34 | + }, (res) => { | ||
| 35 | + assert.strictEqual(res.statusCode, 200); | ||
| 36 | + res.on('end', () => { | ||
| 37 | + server.close(); | ||
| 38 | + }); | ||
| 39 | + res.resume(); | ||
| 40 | + }); | ||
| 41 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,35 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * This test covers http.Server({ ServerResponse }) option: | ||
| 5 | + * With ServerResponse option the server should use | ||
| 6 | + * the new class for creating res Object instead of the default | ||
| 7 | + * http.ServerResponse. | ||
| 8 | + */ | ||
| 9 | + const common = require('../common'); | ||
| 10 | + const assert = require('assert'); | ||
| 11 | + const http = require('http'); | ||
| 12 | + | ||
| 13 | + class MyServerResponse extends http.ServerResponse { | ||
| 14 | + status(code) { | ||
| 15 | + return this.writeHead(code, { 'Content-Type': 'text/plain' }); | ||
| 16 | + } | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + const server = http.Server({ | ||
| 20 | + ServerResponse: MyServerResponse | ||
| 21 | + }, common.mustCall(function(req, res) { | ||
| 22 | + res.status(200); | ||
| 23 | + res.end(); | ||
| 24 | + })); | ||
| 25 | + server.listen(); | ||
| 26 | + | ||
| 27 | + server.on('listening', function makeRequest() { | ||
| 28 | + http.get({ port: this.address().port }, (res) => { | ||
| 29 | + assert.strictEqual(res.statusCode, 200); | ||
| 30 | + res.on('end', () => { | ||
| 31 | + server.close(); | ||
| 32 | + }); | ||
| 33 | + res.resume(); | ||
| 34 | + }); | ||
| 35 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,51 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * This test covers http.Server({ IncomingMessage }) option: | ||
| 5 | + * With IncomingMessage option the server should use | ||
| 6 | + * the new class for creating req Object instead of the default | ||
| 7 | + * http.IncomingMessage. | ||
| 8 | + */ | ||
| 9 | + const common = require('../common'); | ||
| 10 | + const fixtures = require('../common/fixtures'); | ||
| 11 | + | ||
| 12 | + if (!common.hasCrypto) | ||
| 13 | + common.skip('missing crypto'); | ||
| 14 | + | ||
| 15 | + const assert = require('assert'); | ||
| 16 | + const http = require('http'); | ||
| 17 | + const https = require('https'); | ||
| 18 | + | ||
| 19 | + class MyIncomingMessage extends http.IncomingMessage { | ||
| 20 | + getUserAgent() { | ||
| 21 | + return this.headers['user-agent'] || 'unknown'; | ||
| 22 | + } | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + const server = https.createServer({ | ||
| 26 | + key: fixtures.readKey('agent1-key.pem'), | ||
| 27 | + cert: fixtures.readKey('agent1-cert.pem'), | ||
| 28 | + ca: fixtures.readKey('ca1-cert.pem'), | ||
| 29 | + IncomingMessage: MyIncomingMessage | ||
| 30 | + }, common.mustCall(function(req, res) { | ||
| 31 | + assert.strictEqual(req.getUserAgent(), 'node-test'); | ||
| 32 | + res.statusCode = 200; | ||
| 33 | + res.end(); | ||
| 34 | + })); | ||
| 35 | + server.listen(); | ||
| 36 | + | ||
| 37 | + server.on('listening', function makeRequest() { | ||
| 38 | + https.get({ | ||
| 39 | + port: this.address().port, | ||
| 40 | + rejectUnauthorized: false, | ||
| 41 | + headers: { | ||
| 42 | + 'User-Agent': 'node-test' | ||
| 43 | + } | ||
| 44 | + }, (res) => { | ||
| 45 | + assert.strictEqual(res.statusCode, 200); | ||
| 46 | + res.on('end', () => { | ||
| 47 | + server.close(); | ||
| 48 | + }); | ||
| 49 | + res.resume(); | ||
| 50 | + }); | ||
| 51 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,47 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * This test covers http.Server({ ServerResponse }) option: | ||
| 5 | + * With ServerResponse option the server should use | ||
| 6 | + * the new class for creating res Object instead of the default | ||
| 7 | + * http.ServerResponse. | ||
| 8 | + */ | ||
| 9 | + const common = require('../common'); | ||
| 10 | + const fixtures = require('../common/fixtures'); | ||
| 11 | + | ||
| 12 | + if (!common.hasCrypto) | ||
| 13 | + common.skip('missing crypto'); | ||
| 14 | + | ||
| 15 | + const assert = require('assert'); | ||
| 16 | + const http = require('http'); | ||
| 17 | + const https = require('https'); | ||
| 18 | + | ||
| 19 | + class MyServerResponse extends http.ServerResponse { | ||
| 20 | + status(code) { | ||
| 21 | + return this.writeHead(code, { 'Content-Type': 'text/plain' }); | ||
| 22 | + } | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + const server = https.createServer({ | ||
| 26 | + key: fixtures.readKey('agent1-key.pem'), | ||
| 27 | + cert: fixtures.readKey('agent1-cert.pem'), | ||
| 28 | + ca: fixtures.readKey('ca1-cert.pem'), | ||
| 29 | + ServerResponse: MyServerResponse | ||
| 30 | + }, common.mustCall(function(req, res) { | ||
| 31 | + res.status(200); | ||
| 32 | + res.end(); | ||
| 33 | + })); | ||
| 34 | + server.listen(); | ||
| 35 | + | ||
| 36 | + server.on('listening', function makeRequest() { | ||
| 37 | + https.get({ | ||
| 38 | + port: this.address().port, | ||
| 39 | + rejectUnauthorized: false | ||
| 40 | + }, (res) => { | ||
| 41 | + assert.strictEqual(res.statusCode, 200); | ||
| 42 | + res.on('end', () => { | ||
| 43 | + server.close(); | ||
| 44 | + }); | ||
| 45 | + res.resume(); | ||
| 46 | + }); | ||
| 47 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments