GeezerVison is showing github.com — http: add CONNECT method handling for default Host header with proxy · nodejs/node@eacfbd0
File tree
Expand 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 +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -526,7 +526,9 @@ function ClientRequest(input, options, cb) { |
526 | 526 | const userHostHeader = this.getHeader('host'); |
527 | 527 |
|
528 | 528 | if (host && !this.getHeader('host') && setHost) { |
529 | | - this.setHeader('Host', hostHeaderFromOptions); |
| 529 | + const hostHeader = method === 'CONNECT' && options.path ? |
| 530 | + String(this.path) : hostHeaderFromOptions; |
| 531 | + this.setHeader('Host', hostHeader); |
530 | 532 | } |
531 | 533 |
|
532 | 534 | if (options.auth && !this.getHeader('Authorization')) { |
|
Collapse file
Copy file name to clipboard +37Lines changed: 37 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 | +const net = require('net'); |
| 7 | + |
| 8 | +const target = 'target.example.com:443'; |
| 9 | + |
| 10 | +const server = net.createServer(common.mustCall((socket) => { |
| 11 | + socket.once('data', common.mustCall((data) => { |
| 12 | + const rawRequest = data.toString(); |
| 13 | + const requestLines = rawRequest.split('\r\n'); |
| 14 | + |
| 15 | + assert.strictEqual(requestLines[0], `CONNECT ${target} HTTP/1.1`); |
| 16 | + assert(requestLines.includes(`Host: ${target}`)); |
| 17 | + |
| 18 | + socket.end('HTTP/1.1 200 Connection established\r\n\r\n'); |
| 19 | + })); |
| 20 | +})); |
| 21 | + |
| 22 | +server.listen(0, common.localhostIPv4, common.mustCall(() => { |
| 23 | + const req = http.request({ |
| 24 | + host: common.localhostIPv4, |
| 25 | + port: server.address().port, |
| 26 | + method: 'CONNECT', |
| 27 | + path: target, |
| 28 | + }, common.mustNotCall()); |
| 29 | + |
| 30 | + req.on('connect', common.mustCall((res, socket) => { |
| 31 | + assert.strictEqual(res.statusCode, 200); |
| 32 | + socket.destroy(); |
| 33 | + server.close(); |
| 34 | + })); |
| 35 | + |
| 36 | + req.end(); |
| 37 | +})); |
Collapse file
Copy file name to clipboardExpand all lines: test/parallel/test-tls-over-http-tunnel.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -61,7 +61,7 @@ const proxy = net.createServer(common.mustCall((clientSocket) => { |
61 | 61 | `CONNECT localhost:${server.address().port} ` + |
62 | 62 | 'HTTP/1.1\r\n' + |
63 | 63 | 'Proxy-Connections: keep-alive\r\n' + |
64 | | - `Host: localhost:${proxy.address().port}\r\n` + |
| 64 | + `Host: localhost:${server.address().port}\r\n` + |
65 | 65 | 'Connection: keep-alive\r\n\r\n'); |
66 | 66 |
|
67 | 67 | console.log('PROXY: got CONNECT request'); |
|