Created
June 23, 2011 18:52
-
-
Save yorickvP/1043282 to your computer and use it in GitHub Desktop.
node.js SCGI server using node-binary
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
/* requires substack/node-binary and dependencies (npm install binary) */ | |
/* also don't forget node.js */ | |
var net = require('net'); | |
var Binary = require('binary'); | |
exports.attach = function attach(server) { | |
if ('number' == typeof server) { | |
var port = server; | |
server = net.createServer(); | |
server.listen(port); | |
} | |
server.on('connection', function connectionHandler(socket) { | |
var headers = {}; | |
Binary.stream(socket) | |
.scan('len', ':') | |
.tap(function(vars) { | |
if (isNaN(vars.len*1)) {socket.end(); return; } | |
var len = vars.len*1; | |
if (len == 0) { socket.end(); return;} | |
this.loop(function(end, vars) { | |
this.scan('key', new Buffer([0])) | |
.scan('value', new Buffer([0])) | |
.tap(function (vars) { | |
headers[vars.key.toString()] = vars.value; | |
len -= vars.key.length + vars.value.length + 2; | |
if (len == 0) end(); | |
}) | |
}); | |
}) | |
.skip(1) // and the data? | |
.buffer('data', function(vars) { | |
var l = 0; | |
if (headers['CONTENT_LENGTH']) l = headers['CONTENT_LENGTH'].toString('ascii')*1; | |
if (isNaN(l)) return 0; | |
return l; | |
}) | |
.tap(function(vars) { | |
server.emit('request', socket, headers, vars.data); | |
}); | |
}); | |
return server; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment