-
Notifications
You must be signed in to change notification settings - Fork 30.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: support http proxy for fetch under NODE_USE_ENV_PROXY
- Loading branch information
1 parent
52ac448
commit 18c0d95
Showing
4 changed files
with
102 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const address = process.env.SERVER_ADDRESS; | ||
const response = await fetch(address); | ||
const body = await response.text(); | ||
console.log(body); |
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,22 @@ | ||
const net = require('net'); | ||
|
||
exports.onConnect = function (req, clientSocket, head) { | ||
const [hostname, port] = req.url.split(':'); | ||
|
||
const serverSocket = net.connect(port, hostname, () => { | ||
clientSocket.write( | ||
'HTTP/1.1 200 Connection Established\r\n' + | ||
'Proxy-agent: Node.js-Proxy\r\n' + | ||
'\r\n' | ||
); | ||
serverSocket.write(head); | ||
clientSocket.pipe(serverSocket); | ||
serverSocket.pipe(clientSocket); | ||
}); | ||
|
||
serverSocket.on('error', (err) => { | ||
console.error('Error on CONNECT tunnel:', err.message); | ||
clientSocket.write('HTTP/1.1 500 Connection Error\r\n\r\n'); | ||
clientSocket.end(); | ||
}); | ||
}; |
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,64 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const fixtures = require('../common/fixtures'); | ||
const assert = require('assert'); | ||
const { spawn } = require('child_process'); | ||
const http = require('http'); | ||
const { onConnect } = require('../fixtures/proxy-handler'); | ||
|
||
// Start a server to process the final request. | ||
const server = http.createServer((req, res) => { | ||
res.end('Hello world'); | ||
}); | ||
server.on('error', (err) => { console.log('Server error', err); }); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
// Start a proxy server to tunnel the request. | ||
const proxy = http.createServer(); | ||
// If the request does not go through the proxy server, common.mustCall fails. | ||
proxy.on('connect', common.mustCall((req, clientSocket, head) => { | ||
console.log('Proxying CONNECT', req.url, req.headers); | ||
assert.strictEqual(req.url, `localhost:${server.address().port}`); | ||
onConnect(req, clientSocket, head); | ||
})); | ||
proxy.on('error', (err) => { console.log('Proxy error', err); }); | ||
|
||
proxy.listen(0, common.mustCall(() => { | ||
const proxyAddress = `http://localhost:${proxy.address().port}`; | ||
const serverAddress = `http://localhost:${server.address().port}`; | ||
const child = spawn(process.execPath, | ||
[fixtures.path('fetch-and-log.mjs')], | ||
{ | ||
env: { | ||
...process.env, | ||
HTTP_PROXY: proxyAddress, | ||
NODE_USE_ENV_PROXY: true, | ||
SERVER_ADDRESS: serverAddress, | ||
}, | ||
}); | ||
|
||
const stderr = []; | ||
const stdout = []; | ||
child.stderr.on('data', (chunk) => { | ||
stderr.push(chunk); | ||
}); | ||
child.stdout.on('data', (chunk) => { | ||
stdout.push(chunk); | ||
}); | ||
|
||
child.on('exit', common.mustCall(function(code, signal) { | ||
proxy.close(); | ||
server.close(); | ||
|
||
console.log('--- stderr ---'); | ||
console.log(Buffer.concat(stderr).toString()); | ||
console.log('--- stdout ---'); | ||
const stdoutStr = Buffer.concat(stdout).toString(); | ||
console.log(stdoutStr); | ||
assert.strictEqual(stdoutStr.trim(), 'Hello world'); | ||
assert.strictEqual(code, 0); | ||
assert.strictEqual(signal, null); | ||
})); | ||
})); | ||
})); |