Created
July 29, 2011 23:24
-
-
Save bnoordhuis/1114958 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
http = require('http'); | |
fs = require('fs'); | |
FRONTEND_PORT = 8000; | |
BACKEND_PORT = 8001; | |
//CONTENT_LENGTH = 2147483648; // 2 GB | |
CONTENT_LENGTH = 2097152; // 2 MB | |
function log_events(prefix, obj) { | |
var fun = obj.emit; | |
obj.emit = function(what, b, c, d, e) { | |
if (what !== 'newListener') { | |
console.error(prefix, arguments); | |
} | |
return fun.call(obj, what, b, c, d, e); | |
}; | |
} | |
function log_events(prefix, obj) {} | |
function frontend(req, res) { | |
res.writeHead(200, { | |
'Content-Length': '' + CONTENT_LENGTH, | |
'Content-Type': 'application/octet-stream', | |
'Connection': 'close' | |
}); | |
res._send(''); // force flush | |
var options = { | |
method: 'GET', | |
host: '127.0.0.1', | |
port: BACKEND_PORT, | |
path: '/' | |
}; | |
var client_req = http.request(options, function(stream) { | |
stream.pipe(res); | |
}); | |
log_events('frontend req', req); | |
log_events('frontend res', res); | |
log_events('frontend client req', client_req); | |
client_req.end(); | |
} | |
function backend(req, res) { | |
res.writeHead(200, { | |
'Content-Length': '' + CONTENT_LENGTH, | |
'Content-Type': 'application/octet-stream', | |
'Connection': 'close' | |
}); | |
res._send(''); // force flush | |
var stream = fs.createReadStream('/dev/zero', { | |
start: 0, | |
end: CONTENT_LENGTH | |
}); | |
log_events('backend req', req); | |
log_events('backend res', res); | |
log_events('backend stream', stream); | |
stream.pipe(res); | |
} | |
http.createServer(frontend).listen(FRONTEND_PORT); | |
http.createServer(backend).listen(BACKEND_PORT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment