Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorkoichik <koichik@improvement.jp>2012-01-09 06:51:06 +0400
committerkoichik <koichik@improvement.jp>2012-01-09 06:51:06 +0400
commit08a91acd76cd107dc2f3914f9ea7e277bb85206e (patch)
tree2485f34d557b22cf461cc46cfb58f5b60a2b34c2 /doc
parentc1a63a9e905f90684c3c22eae59cbe12fced661c (diff)
http: better support for CONNECT method.
Introduces 'connect' event on both client (http.ClientRequest) and server (http.Server). Refs: #2259, #2474. Fixes #1576.
Diffstat (limited to 'doc')
-rw-r--r--doc/api/http.markdown98
1 files changed, 89 insertions, 9 deletions
diff --git a/doc/api/http.markdown b/doc/api/http.markdown
index 57f13a48ad2..c9a2dc0dd62 100644
--- a/doc/api/http.markdown
+++ b/doc/api/http.markdown
@@ -66,6 +66,24 @@ request body.
Note that when this event is emitted and handled, the `request` event will
not be emitted.
+### Event: 'connect'
+
+`function (request, socket, head) { }`
+
+Emitted each time a client requests a http CONNECT method. If this event isn't
+listened for, then clients requesting a CONNECT method will have their
+connections closed.
+
+* `request` is the arguments for the http request, as it is in the request
+ event.
+* `socket` is the network socket between the server and client.
+* `head` is an instance of Buffer, the first packet of the tunneling stream,
+ this may be empty.
+
+After this event is emitted, the request's socket will not have a `data`
+event listener, meaning you will need to bind to it in order to handle data
+sent to the server on that socket.
+
### Event: 'upgrade'
`function (request, socket, head) { }`
@@ -74,9 +92,11 @@ Emitted each time a client requests a http upgrade. If this event isn't
listened for, then clients requesting an upgrade will have their connections
closed.
-* `request` is the arguments for the http request, as it is in the request event.
+* `request` is the arguments for the http request, as it is in the request
+ event.
* `socket` is the network socket between the server and client.
-* `head` is an instance of Buffer, the first packet of the upgraded stream, this may be empty.
+* `head` is an instance of Buffer, the first packet of the upgraded stream,
+ this may be empty.
After this event is emitted, the request's socket will not have a `data`
event listener, meaning you will need to bind to it in order to handle data
@@ -593,6 +613,69 @@ Options:
Emitted after a socket is assigned to this request.
+### Event: 'connect'
+
+`function (response, socket, head) { }`
+
+Emitted each time a server responds to a request with a CONNECT method. If this
+event isn't being listened for, clients receiving a CONNECT method will have
+their connections closed.
+
+A client server pair that show you how to listen for the `connect` event.
+
+ var http = require('http');
+ var net = require('net');
+ var url = require('url');
+
+ // Create an HTTP tunneling proxy
+ var proxy = http.createServer(function (req, res) {
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end('okay');
+ });
+ proxy.on('connect', function(req, cltSocket, head) {
+ // connect to an origin server
+ var srvUrl = url.parse('http://' + req.url);
+ var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, function() {
+ cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
+ 'Proxy-agent: Node-Proxy\r\n' +
+ '\r\n');
+ srvSocket.write(head);
+ srvSocket.pipe(cltSocket);
+ cltSocket.pipe(srvSocket);
+ });
+ });
+
+ // now that proxy is running
+ proxy.listen(1337, '127.0.0.1', function() {
+
+ // make a request to a tunneling proxy
+ var options = {
+ port: 1337,
+ host: '127.0.0.1',
+ method: 'CONNECT',
+ path: 'www.google.com:80'
+ };
+
+ var req = http.request(options);
+ req.end();
+
+ req.on('connect', function(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', function(chunk) {
+ console.log(chunk.toString());
+ });
+ socket.on('end', function() {
+ proxy.close();
+ });
+ });
+ });
+
### Event: 'upgrade'
`function (response, socket, head) { }`
@@ -601,25 +684,22 @@ Emitted each time a server responds to a request with an upgrade. If this
event isn't being listened for, clients receiving an upgrade header will have
their connections closed.
-A client server pair that show you how to listen for the `upgrade` event using `http.getAgent`:
+A client server pair that show you how to listen for the `upgrade` event.
var http = require('http');
- var net = require('net');
// Create an HTTP server
var srv = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('okay');
});
- srv.on('upgrade', function(req, socket, upgradeHead) {
+ srv.on('upgrade', function(req, socket, head) {
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
'Upgrade: WebSocket\r\n' +
'Connection: Upgrade\r\n' +
- '\r\n\r\n');
+ '\r\n');
- socket.ondata = function(data, start, end) {
- socket.write(data.toString('utf8', start, end), 'utf8'); // echo back
- };
+ socket.pipe(socket); // echo back
});
// now that server is running