Created
May 23, 2022 20:03
-
-
Save RenatoExpert/6bdb90360d85f4735e242a4487502462 to your computer and use it in GitHub Desktop.
extracted from https://nodejs.org/api/http.html
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
/* | |
Event: 'connect'# | |
Added in: v0.7.0 | |
response <http.IncomingMessage> | |
socket <stream.Duplex> | |
head <Buffer> | |
Emitted each time a server responds to a request with a CONNECT method. If this event is not being listened for, clients receiving a CONNECT method will have their connections closed. | |
This event is guaranteed to be passed an instance of the <net.Socket> class, a subclass of <stream.Duplex>, unless the user specifies a socket type other than <net.Socket>. | |
A client and server pair demonstrating how to listen for the 'connect' event: | |
/* | |
const http = require('node:http'); | |
const net = require('node:net'); | |
const { URL } = require('node:url'); | |
// Create an HTTP tunneling proxy | |
const proxy = http.createServer((req, res) => { | |
res.writeHead(200, { 'Content-Type': 'text/plain' }); | |
res.end('okay'); | |
}); | |
proxy.on('connect', (req, clientSocket, head) => { | |
// Connect to an origin server | |
const { port, hostname } = new URL(`http://${req.url}`); | |
const serverSocket = net.connect(port || 80, hostname, () => { | |
clientSocket.write('HTTP/1.1 200 Connection Established\r\n' + | |
'Proxy-agent: Node.js-Proxy\r\n' + | |
'\r\n'); | |
serverSocket.write(head); | |
serverSocket.pipe(clientSocket); | |
clientSocket.pipe(serverSocket); | |
}); | |
}); | |
// Now that proxy is running | |
proxy.listen(1337, '127.0.0.1', () => { | |
// Make a request to a tunneling proxy | |
const options = { | |
port: 1337, | |
host: '127.0.0.1', | |
method: 'CONNECT', | |
path: 'www.google.com:80' | |
}; | |
const req = http.request(options); | |
req.end(); | |
req.on('connect', (res, socket, head) => { | |
console.log('got connected!'); | |
// Make a request over an HTTP tunnel | |
socket.write('GET / HTTP/1.1\r\n' + | |
'Host: www.google.com:80\r\n' + | |
'Connection: close\r\n' + | |
'\r\n'); | |
socket.on('data', (chunk) => { | |
console.log(chunk.toString()); | |
}); | |
socket.on('end', () => { | |
proxy.close(); | |
}); | |
}); | |
}); | |
/* | |
Event: 'continue'# | |
Added in: v0.3.2 | |
Emitted when the server sends a '100 Continue' HTTP response, usually because the request contained 'Expect: 100-continue'. This is an instruction that the client should send the request body. | |
Event: 'finish'# | |
Added in: v0.3.6 | |
Emitted when the request has been sent. More specifically, this event is emitted when the last segment of the response headers and body have been handed off to the operating system for transmission over the network. It does not imply that the server has received anything yet. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment