Created
July 12, 2017 15:55
-
-
Save chenminhua/9c70bdf5223ce411559a94746ba803d9 to your computer and use it in GitHub Desktop.
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
var http = require('http'); | |
var net = require('net'); | |
var url = require('url'); | |
function request(cReq, cRes) { | |
var u = url.parse(cReq.url); | |
var options = { | |
hostname : u.hostname, | |
port : u.port || 80, | |
path : u.path, | |
method : cReq.method, | |
headers : cReq.headers | |
}; | |
var pReq = http.request(options, function(pRes) { | |
cRes.writeHead(pRes.statusCode, pRes.headers); | |
pRes.pipe(cRes); | |
}).on('error', function(e) { | |
cRes.end(); | |
}); | |
cReq.pipe(pReq); | |
} | |
function connect(cReq, cSock) { | |
var u = url.parse('http://' + cReq.url); | |
var pSock = net.connect(u.port, u.hostname, function() { | |
cSock.write('HTTP/1.1 200 Connection Established\r\n\r\n'); | |
pSock.pipe(cSock); | |
}).on('error', function(e) { | |
cSock.end(); | |
}); | |
cSock.pipe(pSock); | |
} | |
http.createServer() | |
.on('request', request) | |
.on('connect', connect) | |
.listen(8888, '0.0.0.0'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment