Skip to content

Instantly share code, notes, and snippets.

@ncb000gt
Forked from markc/gist:899249
Created April 2, 2011 05:25
Show Gist options
  • Save ncb000gt/899254 to your computer and use it in GitHub Desktop.
Save ncb000gt/899254 to your computer and use it in GitHub Desktop.
function serve_cgi(filename, res, get, post, method, vhost, port, pinfo, get, sname, uri, droot) {
var env = {
CONTENT_LENGTH: post.length,
CONTENT_TYPE: 'application/x-www-form-urlencoded',
DOCUMENT_ROOT: droot,
GATEWAY_INTERFACE: 'CGI/1.1',
HTTP_HOST: vhost,
QUERY_STRING: get,
REDIRECT_STATUS: '200',
REQUEST_METHOD: method,
REQUEST_URI: uri,
SCRIPT_FILENAME: filename,
SCRIPT_NAME: sname,
SERVER_NAME: vhost,
SERVER_PORT: port,
SERVER_PROTOCOL: 'HTTP/1.1',
SERVER_SOFTWARE: 'Node/'+process.version+' (Nodality/20110402)',
}
var chunks = [];
var total = 0;
var headers = {};
var cgi = spawn(filename, [], env);
cgi.stdout.on('data', function (data) {
chunks.push(data);
total += data.length;
});
cgi.on('exit', function (code) {
var buf = new Buffer(length);
var offset = 0;
chunks.forEach(function(chunk) {
chunk.copy(buf, offset, 0);
offset += chunk.length;
});
//chop your headers out here.
//build the headers object up.
res.writeHead(200, headers);
res.write(data, 'binary');
res.end();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment