Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit 6851ee2eae380be5aa573bbc747b0a35b57b7750 1 parent a762e28
authored November 14, 2012

Showing 2 changed files with 35 additions and 23 deletions. Show diff stats Hide diff stats

  1. 56  index.js
  2. 2  package.json
56  index.js
@@ -29,27 +29,34 @@ var spawn = require('child_process').spawn
29 29
 function router(params) {
30 30
 	//create cgi environment variables.
31 31
 	var reqEnv = {};
32  
-	for(var keys = Object.keys(process.env), l = keys.length; l; --l) {
  32
+	/*for(var keys = Object.keys(process.env), l = keys.length; l; --l) {
33 33
 	   reqEnv[ keys[l-1] ] = process.env[ keys[l-1] ];
34  
-	}
  34
+	}*/
35 35
 	for(var keys = Object.keys(params.env), l = keys.length; l; --l)	{
36 36
 	   reqEnv[ keys[l-1] ] = params.env[ keys[l-1] ];
37 37
 	}
38  
-				
  38
+	function trim(str) {
  39
+		return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  40
+	}
  41
+	
39 42
 	var cgiRouter = function router(request, response, next){
  43
+		//return next();
40 44
 		var url = request.pathname;
41 45
 		if (request.pathname == '/') {
42  
-			url = params.directoryIndex;
  46
+			//url = params.directoryIndex;
43 47
 		}
44 48
 		if (path.extname(url) == params.ext) {
45  
-			var exec = require('child_process').exec;
46  
-			var p = path.resolve(reqEnv['DOCUMENT_ROOT']+url);
47 49
 			
  50
+		var exec = require('child_process').exec;
  51
+			var p = path.resolve(reqEnv['DOCUMENT_ROOT']+url);
48 52
 			//set environment variables for this request
49 53
 			reqEnv['SCRIPT_NAME'] = url;
50 54
 			reqEnv['PATH_INFO'] = '';//not supported a the moment.
51 55
 			reqEnv['PATH_TRANSLATED'] = reqEnv['DOCUMENT_ROOT']+url;
52  
-			reqEnv['QUERY_STRING'] = req.uri.query || '';
  56
+			reqEnv['QUERY_STRING'] = '';
  57
+			for(var p in request.params) {
  58
+				reqEnv['QUERY_STRING'] += p+"="+encodeURIComponent(request.params[p])+"&";
  59
+			}
53 60
 			reqEnv['REQUEST_METHOD'] = request.method;
54 61
 			
55 62
 			//add request headers, "User-Agent" -> "HTTP_USER_AGENT"
@@ -67,33 +74,40 @@ function router(params) {
67 74
 				reqEnv['AUTH_TYPE'] = request.headers.authorization.split(' ')[0];
68 75
 			}
69 76
 			//user defined fn can alter the env for each request
70  
-			if (params.envFn) reqEnv = params.envFn(reqEnv);
71  
-						
72  
-			var cmd = params.bin+' "'+p+'"';
  77
+			//if (params.envFn) reqEnv = params.envFn(reqEnv);
  78
+	
  79
+			var cmd = params.bin;
73 80
 			if (params.debug) {
74 81
 				console.log("request:"+url);
75  
-				console.log("running:"+cmd);
76 82
 			}
77  
-			//we could merge params.env with process.env
  83
+			//response.setHeader('Transfer-Encoding', 'chunked');
78 84
 			var cgi = spawn(params.bin, [], {
79  
-			  'env': params.env
  85
+			  'env': reqEnv
80 86
 			});
81 87
 			//request body is just sent directly to stdin of CGI for it to handle.
82 88
 			request.pipe(cgi.stdin);
  89
+			if (params.sterr) {
  90
+				cgi.stderr.on('data',params.sterr);
  91
+			}
83 92
 			var headersSent = false;
84  
-			cgi.stout.on('data',function(data) {
  93
+			var allData = "";
  94
+			cgi.stdout.on('data',function(data) {
85 95
 				if (headersSent) {
86 96
 					//stream data to browser as soon as it is available.
  97
+					console.log(data.toString());
87 98
 					response.write(data);
88 99
 				} else {
89  
-					var lines = data.toString().split("\n");
  100
+					
  101
+					var lines = data.toString().split("\r\n");
90 102
 					//set headers until you get a blank line...
91 103
 					for(var l=0;l<lines.length;l++) {
92 104
 						if (lines[l] == "") {
93 105
 							response.writeHead(200);
94 106
 							headersSent = true;
95  
-							//send rest asap
96  
-							response.write(lines.slice(l+1).join('\n'));
  107
+							//Seems like AppJS response does not support streaming.
  108
+							//response.write(lines.slice(l+1).join('\n'));
  109
+							allData += lines.slice(l+1).join('\r\n');
  110
+							break;
97 111
 						} else {
98 112
 							//set header
99 113
 							var header = lines[l].split(":");
@@ -101,13 +115,11 @@ function router(params) {
101 115
 						}
102 116
 					}
103 117
 				}
  118
+				
104 119
 			});
105  
-			cgi.stout.on('end',function() {
106  
-				response.end();
  120
+			cgi.stdout.on('end',function() {
  121
+				response.end(allData);
107 122
 			});
108  
-			if (params.sterr) {
109  
-				cgi.sterr.on('data',params.sterr);
110  
-			}
111 123
 		} else {
112 124
 			next();
113 125
 		}
2  package.json
... ...
@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "appjs-cgi",
3  
-  "version": "0.1.1",
  3
+  "version": "0.1.3",
4 4
   "description": "Router that spawns cgi scripts",
5 5
   "main": "index.js",
6 6
   "repository": {

0 notes on commit 6851ee2

Please sign in to comment.
Something went wrong with that request. Please try again.