GeezerVison is showing github.com — http: normalize CONNECT request paths · nodejs/node@d266067
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/_http_client.js +18-1Lines changed: 18 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,6 +28,7 @@ const { | 28 | 28 | NumberIsFinite, | 29 | 29 | ObjectAssign, | 30 | 30 | ObjectDefineProperty, | | 31 | + ObjectHasOwn, | 31 | 32 | ObjectKeys, | 32 | 33 | ObjectSetPrototypeOf, | 33 | 34 | ReflectApply, | @@ -331,12 +332,15 @@ function rewriteForProxiedHttp(req, reqOptions, proxyAuthority, userHostHeader, | 331 | 332 | function ClientRequest(input, options, cb) { | 332 | 333 | OutgoingMessage.call(this); | 333 | 334 |
| | 335 | + let pathIsFromURL = false; | 334 | 336 | if (typeof input === 'string') { | 335 | 337 | const urlStr = input; | 336 | 338 | input = urlToHttpOptions(new URL(urlStr)); | | 339 | + pathIsFromURL = true; | 337 | 340 | } else if (isURL(input)) { | 338 | 341 | // url.URL instance | 339 | 342 | input = urlToHttpOptions(input); | | 343 | + pathIsFromURL = true; | 340 | 344 | } else { | 341 | 345 | cb = options; | 342 | 346 | options = input; | @@ -347,6 +351,13 @@ function ClientRequest(input, options, cb) { | 347 | 351 | cb = options; | 348 | 352 | options = input || kEmptyObject; | 349 | 353 | } else { | | 354 | + const hasPathOverride = pathIsFromURL && | | 355 | + options != null && | | 356 | + ObjectHasOwn(options, 'path'); | | 357 | + if (hasPathOverride) { | | 358 | + pathIsFromURL = false; | | 359 | + } | | 360 | + | 350 | 361 | options = ObjectAssign({ __proto__: null }, input, options); | 351 | 362 | } | 352 | 363 |
| @@ -466,7 +477,13 @@ function ClientRequest(input, options, cb) { | 466 | 477 |
| 467 | 478 | this.joinDuplicateHeaders = options.joinDuplicateHeaders; | 468 | 479 |
| 469 | | - this[kPath] = options.path || '/'; | | 480 | + let path = options.path || '/'; | | 481 | + // Strip the leading slash added when the CONNECT target comes from a URL. | | 482 | + if (method === 'CONNECT' && pathIsFromURL && path[0] === '/') { | | 483 | + path = path.slice(1) || '/'; | | 484 | + } | | 485 | + | | 486 | + this[kPath] = path; | 470 | 487 | if (cb) { | 471 | 488 | this.once('response', cb); | 472 | 489 | } | |
Collapse file Copy file name to clipboard +56Lines changed: 56 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +const assert = require('assert'); | | 5 | +const http = require('http'); | | 6 | + | | 7 | +{ | | 8 | + const server = http.createServer(common.mustNotCall()); | | 9 | + | | 10 | + server.on('connect', common.mustCall((req, socket) => { | | 11 | + assert.strictEqual(req.url, 'example.com'); | | 12 | + socket.end('HTTP/1.1 501 Not Implemented\r\n\r\n'); | | 13 | + })); | | 14 | + | | 15 | + server.listen(0, common.mustCall(() => { | | 16 | + const port = server.address().port; | | 17 | + const req = http.request( | | 18 | + new URL(`http://localhost:${port}/example.com`), | | 19 | + { method: 'CONNECT' }, | | 20 | + ); | | 21 | + | | 22 | + req.on('connect', common.mustCall((res, socket) => { | | 23 | + assert.strictEqual(res.statusCode, 501); | | 24 | + socket.destroy(); | | 25 | + server.close(); | | 26 | + })); | | 27 | + | | 28 | + req.end(); | | 29 | + })); | | 30 | +} | | 31 | + | | 32 | +{ | | 33 | + const server = http.createServer(common.mustNotCall()); | | 34 | + | | 35 | + server.on('connect', common.mustCall((req, socket) => { | | 36 | + assert.strictEqual(req.url, '/example.com'); | | 37 | + socket.end('HTTP/1.1 501 Not Implemented\r\n\r\n'); | | 38 | + })); | | 39 | + | | 40 | + server.listen(0, common.mustCall(() => { | | 41 | + const req = http.request({ | | 42 | + host: 'localhost', | | 43 | + port: server.address().port, | | 44 | + method: 'CONNECT', | | 45 | + path: '/example.com', | | 46 | + }); | | 47 | + | | 48 | + req.on('connect', common.mustCall((res, socket) => { | | 49 | + assert.strictEqual(res.statusCode, 501); | | 50 | + socket.destroy(); | | 51 | + server.close(); | | 52 | + })); | | 53 | + | | 54 | + req.end(); | | 55 | + })); | | 56 | +} |
|