Created
June 14, 2012 13:48
-
-
Save victusfate/2930412 to your computer and use it in GitHub Desktop.
curl node repl (possibly dangerous?)
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
josh@onix:/tmp/http-repl$ curl -sSNT. localhost:8000 | |
Actual repl over http. NOW WITH A LIMITED CONTEXT!! | |
>> help | |
'Exits are North, South and Dennis.' | |
>> .exit | |
Terminal exiting. | |
You'll want to mash ctrl-c. | |
^C | |
josh@onix:/tmp/http-repl$ |
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
var http = require('http'); | |
var repl = require('repl'); | |
var Stream = require('stream'); | |
var vm = require('vm'); | |
var buf0 = new Buffer([0]); | |
var server = http.createServer(function (req, res) { | |
if (!req.headers['user-agent'].match('curl/')) { | |
res.setHeader('content-type', 'text/plain'); | |
return res.end('curl -sSNT. localhost:8000'); | |
} | |
res.setHeader('content-type', 'multipart/octet-stream'); | |
res.write('Actual repl over http. NOW WITH A LIMITED CONTEXT!!\r\n'); | |
var stream = new Stream(); | |
stream.readable = true; | |
stream.writable = true; | |
stream.write = function (data) { | |
res.write(data); | |
}; | |
stream.resume = function () { | |
req.resume(); | |
}; | |
stream.pause = function () { | |
res.pause(); | |
}; | |
stream.destroy = function () { | |
res.write('\r\n'); | |
res.write('Terminal exiting.\r\n'); | |
res.end('You\'ll want to mash ctrl-c.\r\n'); | |
delete stream; | |
}; | |
req.on('data', function (data) { | |
stream.emit('data', data); | |
}); | |
req.on('end', function () { | |
res.end(); | |
}); | |
var remote = repl.start('>> ', stream); | |
remote.context = vm.createContext({ | |
help: 'Exits are North, South and Dennis.' | |
}); | |
var iv = setInterval(function () { | |
res.write(buf0); | |
}, 100); | |
res.on('end', function () { | |
clearInterval(iv); | |
}); | |
}); | |
server.listen(8000, 'localhost', function () { | |
console.log('curl -sSNT. localhost:8000'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment