@@ -66,6 +66,24 @@ request body.
6666Note that when this event is emitted and handled, the ` request ` event will
6767not be emitted.
6868
69+ ### Event: 'connect'
70+
71+ ` function (request, socket, head) { } `
72+
73+ Emitted each time a client requests a http CONNECT method. If this event isn't
74+ listened for, then clients requesting a CONNECT method will have their
75+ connections closed.
76+
77+ * ` request ` is the arguments for the http request, as it is in the request
78+ event.
79+ * ` socket ` is the network socket between the server and client.
80+ * ` head ` is an instance of Buffer, the first packet of the tunneling stream,
81+ this may be empty.
82+
83+ After this event is emitted, the request's socket will not have a ` data `
84+ event listener, meaning you will need to bind to it in order to handle data
85+ sent to the server on that socket.
86+
6987### Event: 'upgrade'
7088
7189` function (request, socket, head) { } `
@@ -74,9 +92,11 @@ Emitted each time a client requests a http upgrade. If this event isn't
7492listened for, then clients requesting an upgrade will have their connections
7593closed.
7694
77- * ` request ` is the arguments for the http request, as it is in the request event.
95+ * ` request ` is the arguments for the http request, as it is in the request
96+ event.
7897* ` socket ` is the network socket between the server and client.
79- * ` head ` is an instance of Buffer, the first packet of the upgraded stream, this may be empty.
98+ * ` head ` is an instance of Buffer, the first packet of the upgraded stream,
99+ this may be empty.
80100
81101After this event is emitted, the request's socket will not have a ` data `
82102event listener, meaning you will need to bind to it in order to handle data
@@ -593,6 +613,69 @@ Options:
593613
594614Emitted after a socket is assigned to this request.
595615
616+ ### Event: 'connect'
617+
618+ ` function (response, socket, head) { } `
619+
620+ Emitted each time a server responds to a request with a CONNECT method. If this
621+ event isn't being listened for, clients receiving a CONNECT method will have
622+ their connections closed.
623+
624+ A client server pair that show you how to listen for the ` connect ` event.
625+
626+ var http = require('http');
627+ var net = require('net');
628+ var url = require('url');
629+
630+ // Create an HTTP tunneling proxy
631+ var proxy = http.createServer(function (req, res) {
632+ res.writeHead(200, {'Content-Type': 'text/plain'});
633+ res.end('okay');
634+ });
635+ proxy.on('connect', function(req, cltSocket, head) {
636+ // connect to an origin server
637+ var srvUrl = url.parse('http://' + req.url);
638+ var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, function() {
639+ cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
640+ 'Proxy-agent: Node-Proxy\r\n' +
641+ '\r\n');
642+ srvSocket.write(head);
643+ srvSocket.pipe(cltSocket);
644+ cltSocket.pipe(srvSocket);
645+ });
646+ });
647+
648+ // now that proxy is running
649+ proxy.listen(1337, '127.0.0.1', function() {
650+
651+ // make a request to a tunneling proxy
652+ var options = {
653+ port: 1337,
654+ host: '127.0.0.1',
655+ method: 'CONNECT',
656+ path: 'www.google.com:80'
657+ };
658+
659+ var req = http.request(options);
660+ req.end();
661+
662+ req.on('connect', function(res, socket, head) {
663+ console.log('got connected!');
664+
665+ // make a request over an HTTP tunnel
666+ socket.write('GET / HTTP/1.1\r\n' +
667+ 'Host: www.google.com:80\r\n' +
668+ 'Connection: close\r\n' +
669+ '\r\n');
670+ socket.on('data', function(chunk) {
671+ console.log(chunk.toString());
672+ });
673+ socket.on('end', function() {
674+ proxy.close();
675+ });
676+ });
677+ });
678+
596679### Event: 'upgrade'
597680
598681` function (response, socket, head) { } `
@@ -601,25 +684,22 @@ Emitted each time a server responds to a request with an upgrade. If this
601684event isn't being listened for, clients receiving an upgrade header will have
602685their connections closed.
603686
604- A client server pair that show you how to listen for the ` upgrade ` event using ` http.getAgent ` :
687+ A client server pair that show you how to listen for the ` upgrade ` event.
605688
606689 var http = require('http');
607- var net = require('net');
608690
609691 // Create an HTTP server
610692 var srv = http.createServer(function (req, res) {
611693 res.writeHead(200, {'Content-Type': 'text/plain'});
612694 res.end('okay');
613695 });
614- srv.on('upgrade', function(req, socket, upgradeHead ) {
696+ srv.on('upgrade', function(req, socket, head ) {
615697 socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
616698 'Upgrade: WebSocket\r\n' +
617699 'Connection: Upgrade\r\n' +
618- '\r\n\r\n ');
700+ '\r\n');
619701
620- socket.ondata = function(data, start, end) {
621- socket.write(data.toString('utf8', start, end), 'utf8'); // echo back
622- };
702+ socket.pipe(socket); // echo back
623703 });
624704
625705 // now that server is running
0 commit comments