Permalink
Browse files

working implementation had to change to a none-streaming approach sin…

…ce appjs router response does not seem to offer a method to support streaming. Moved to send all data in the end event so that we can upgrade code later on.
  • Loading branch information...
Simon Horton
Simon Horton committed Nov 14, 2012
1 parent a762e28 commit 6851ee2eae380be5aa573bbc747b0a35b57b7750
Showing with 35 additions and 23 deletions.
  1. +34 −22 index.js
  2. +1 −1 package.json
View
@@ -29,27 +29,34 @@ var spawn = require('child_process').spawn
function router(params) {
//create cgi environment variables.
var reqEnv = {};
- for(var keys = Object.keys(process.env), l = keys.length; l; --l) {
+ /*for(var keys = Object.keys(process.env), l = keys.length; l; --l) {
reqEnv[ keys[l-1] ] = process.env[ keys[l-1] ];
- }
+ }*/
for(var keys = Object.keys(params.env), l = keys.length; l; --l) {
reqEnv[ keys[l-1] ] = params.env[ keys[l-1] ];
}
-
+ function trim(str) {
+ return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
+ }
+
var cgiRouter = function router(request, response, next){
+ //return next();
var url = request.pathname;
if (request.pathname == '/') {
- url = params.directoryIndex;
+ //url = params.directoryIndex;
}
if (path.extname(url) == params.ext) {
- var exec = require('child_process').exec;
- var p = path.resolve(reqEnv['DOCUMENT_ROOT']+url);
+ var exec = require('child_process').exec;
+ var p = path.resolve(reqEnv['DOCUMENT_ROOT']+url);
//set environment variables for this request
reqEnv['SCRIPT_NAME'] = url;
reqEnv['PATH_INFO'] = '';//not supported a the moment.
reqEnv['PATH_TRANSLATED'] = reqEnv['DOCUMENT_ROOT']+url;
- reqEnv['QUERY_STRING'] = req.uri.query || '';
+ reqEnv['QUERY_STRING'] = '';
+ for(var p in request.params) {
+ reqEnv['QUERY_STRING'] += p+"="+encodeURIComponent(request.params[p])+"&";
+ }
reqEnv['REQUEST_METHOD'] = request.method;
//add request headers, "User-Agent" -> "HTTP_USER_AGENT"
@@ -67,47 +74,52 @@ function router(params) {
reqEnv['AUTH_TYPE'] = request.headers.authorization.split(' ')[0];
}
//user defined fn can alter the env for each request
- if (params.envFn) reqEnv = params.envFn(reqEnv);
-
- var cmd = params.bin+' "'+p+'"';
+ //if (params.envFn) reqEnv = params.envFn(reqEnv);
+
+ var cmd = params.bin;
if (params.debug) {
console.log("request:"+url);
- console.log("running:"+cmd);
}
- //we could merge params.env with process.env
+ //response.setHeader('Transfer-Encoding', 'chunked');
var cgi = spawn(params.bin, [], {
- 'env': params.env
+ 'env': reqEnv
});
//request body is just sent directly to stdin of CGI for it to handle.
request.pipe(cgi.stdin);
+ if (params.sterr) {
+ cgi.stderr.on('data',params.sterr);
+ }
var headersSent = false;
- cgi.stout.on('data',function(data) {
+ var allData = "";
+ cgi.stdout.on('data',function(data) {
if (headersSent) {
//stream data to browser as soon as it is available.
+ console.log(data.toString());
response.write(data);
} else {
- var lines = data.toString().split("\n");
+
+ var lines = data.toString().split("\r\n");
//set headers until you get a blank line...
for(var l=0;l<lines.length;l++) {
if (lines[l] == "") {
response.writeHead(200);
headersSent = true;
- //send rest asap
- response.write(lines.slice(l+1).join('\n'));
+ //Seems like AppJS response does not support streaming.
+ //response.write(lines.slice(l+1).join('\n'));
+ allData += lines.slice(l+1).join('\r\n');
+ break;
} else {
//set header
var header = lines[l].split(":");
response.setHeader(header[0], header[1]||'');
}
}
}
+
});
- cgi.stout.on('end',function() {
- response.end();
+ cgi.stdout.on('end',function() {
+ response.end(allData);
});
- if (params.sterr) {
- cgi.sterr.on('data',params.sterr);
- }
} else {
next();
}
View
@@ -1,6 +1,6 @@
{
"name": "appjs-cgi",
- "version": "0.1.1",
+ "version": "0.1.3",
"description": "Router that spawns cgi scripts",
"main": "index.js",
"repository": {

0 comments on commit 6851ee2

Please sign in to comment.