-
Notifications
You must be signed in to change notification settings - Fork 2
/
nscript.js
513 lines (428 loc) · 15.8 KB
/
nscript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*============================================================================*\
Node Dynamic Script Launcher
Callback on
status
header
data (including stderr)
end
error
notes:
Websockets has a differant request structure from staticpage requests
Make a config parameter to exclude stderr
make express view engine compatible
\*============================================================================*/
var fs = require('fs');
var http = require("http");
//var https = require('https');
var url = require("url");
var path = require("path");
var qs = require('querystring');
// Set moduile version
process.versions.Nscript='0.1.1 - Proving a point';
// Define nscript object
var nscript = {};
module.exports = exports = nscript;
/*============================================================================*\
Import configuration variables or if missing, set defaults
\*============================================================================*/
nscript.docRoot = nscript.docRoot || './';
nscript.defaultRootDocument = nscript.defaultRootDocument || '';
nscript.cgiEngine = nscript.cgiEngine || {};
/*
nscript.docRoot = config.docRoot || './';
nscript.defaultRootDocument = config.defaultRootDocument || '';
nscript.cgiEngine = config.cgiEngine || {};
*/
nscript.launcher={};
/*============================================================================*\
Make array of file extentions that are supported with Launchers
to be removed
\*============================================================================*/
nscript.launchers= function (){
var arr=[];
for(var ext in nscript.cgiEngine)
arr.push(ext);
return arr;
}
/*============================================================================*\
Launch dynamic script
- prepare client information for the script to use
- select the launcing method
child.stderr.setEncoding('utf8')
child.stdout.setEncoding('utf8')
\*============================================================================*/
nscript.launch = function(type,request,callback){
//Check that callback are a function
if(!nscript.cgiEngine[type]){
callback('error','NSL has no support for script of type: ' + type);
return;
}
//Compose information on client request
var connection = {};
connection.files={};
try{
// support for express request
if(!!request._parsedUrl){
connection.header = request.headers || '';
connection.pathname = request._parsedUrl.pathname || '';
connection.query = request.query || ''
,
connection.remoteaddress = request.client.remoteAddress || '';
connection.remoteport = request.client.remotePort || '';
connection.url=request.url || '';
connection.method=request.method || '';
connection.httpversion=request.httpVersion || '';
connection.body=request.body || '';
for(var f in request.files){
connection.files[f]={};
connection.files[f].name=request.files[f].name;
connection.files[f].size=request.files[f].size;
connection.files[f].tmp_name=request.files[f].path;
connection.files[f].type=request.files[f].type;
}
// Support for WS websockets
}else if(!!request.socket.upgradeReq){
connection.remoteaddress = request.socket._socket.remoteAddress || '';
connection.remoteport = request.socket._socket.remotePort || '';
connection.url=request.socket.upgradeReq.url || '';
connection.header =request.socket.upgradeReq.headers || '';
connection.httpversion=request.socket.upgradeReq.httpVersion || '';
// event handler support
connection.method='WS';
connection.pathname = request.pathname;
connection.query = request.query;
// Unsupported resuest type
}else{
throw ("Unable to complie client request information");
}
}catch(e){
callback('error',"Unrecognized request type: "+ e);
return;
}
// Add some basic information
connection.docroot=path.resolve(nscript.docRoot);
// Validate path name
// Check/Make pathname a full path from from document root
connection.pathname = path.normalize('/' + connection.pathname);
// Use default script name
if(connection.pathname.length <2)
connection.pathname = nscript.defaultRootDocument;
// Check that script exists
fs.exists(nscript.docRoot + connection.pathname, function(exists){
if (exists) {
// Launch script
if(type == 'php')
nscript.launcher.php(nscript.cgiEngine[type],connection,callback);
else
nscript.launcher.cgi(nscript.cgiEngine[type],connection,callback);
}else{
// Send status Not found
callback('status',"404 " +nscript.docRoot + connection.pathname +" not found");
}
});
}
/*============================================================================*\
Functions for express middle ware
\*============================================================================*/
// Launch function for express middle ware
nscript.expressLaunch = function(request, response, next) {
//Get extention
var ext = path.extname(request._parsedUrl.pathname).substring(1);
var headerSent=false;
// Check that type are supported
if(nscript.launchers().indexOf(ext) > -1){
nscript.launch(ext,request,function(event,data,param){
switch (event){
case 'status':
if(headerSent) break;
response.status(data);
if(logMode>=verbose) console.log("Status: %s",data);
break;
case 'header':
if(headerSent) break;
response.setHeader(data,param);
if(logMode>=verbose) console.log("Header: '%s: %s'",data,param);
break;
case 'data':
headerSent=true;
response.write(data,'binary');
if(logMode>=verbose){
if(data.length<100)
console.log("Data: '%s'",data);
else
console.log("Data: *** length: ",data.length);
}
break;
case 'end':
response.end();
if(logMode>=verbose) console.log("End of request");
break;
case 'error':
if(logMode>=mumble) console.log('Node Scripting launcher error: %s',data);
break;
default:
if(logMode>=verbose) console.log("Unknown event: '%s'",event);
}
});
}else{
next('route');
}
}
/*============================================================================*\
PHP cgi support
* Prepared for fast responce workers
* Fast responce are provided by prespawned workers, waiting for stdin to complete.
* Not yet implemented
Using the command php-cgi
Using the script php_burner.php as a launcher script
request body part and other information are parsed through stdin, to the php
process. body including multipart are interpreted by the server, before parsing
it to the cgi.
Node provides for the uploaded files to be stored. they only need to be renamed
and information passed.
Quirks:
1. php-cgi might send error text formatted in HTML before the headers
Fix: 1. set a default header Content-type: text/html and remove duplicates
2. error messages must be stores until headers are send.
3. location headers must have presedence
2. php-cgi might send a header in one block and the line ending in another
Fix: buffer all headers until end of header section are received
3. the phpinfo() function requests pseudo pages for logo images.
for strange 404 see http://woozle.org/~neale/papers/php-cgi.html
Maximum header section length is hardcoded to about 4K, to prevent unreasonable
memory usages. Are there any reason for a larger buffer size?
Maximum error body length are hardcoded to about 4K too
\*============================================================================*/
nscript.launcher.php = function(cmd,connection,callback) {
// Start child process
var proc = require("child_process").spawn('php-cgi',['php_burner.php']);
proc.stderr.setEncoding('utf8');
proc.stdout.setEncoding('binary');
//Transfer connection request informastion to stdin
proc.stdin.write(JSON.stringify(connection));
proc.stdin.end();
// Get ready for output from the process
function handleOutput(proc,callback){
var buffer='';
var bbuffer='';
var eoh=-1;
var headersSent=false;
var end=false;
var headers={'Content-type':'text/html'}; // Fix 1.1
// Catch output from scrit and send it to client
proc.stdout.on('data', function (data) {
if(end) return;
// Headers. Assume script always send at least one header
if(!headersSent){
// Store headers until a end of header is receiverd (\r\n\r\n) Strip \r
buffer+=data;
// Look for end of header section
eoh=buffer.indexOf('\r\n\r\n');
if(eoh<0) eoh=buffer.indexOf('\n\n');
if(eoh<0 && buffer.length > 4096) eoh=buffer.length;
if(eoh>0){
// Separate headers from body parts
bbuffer+=buffer.substr(eoh+4);
buffer=buffer.substr(0,eoh+1);
// Divide heades lines
var div =-1;
var line=buffer.split('\n');
for(var i in line){
// Split header into key, value pairs
div = line[i].indexOf(":");
if(div>0){
// Fix 1.3 Handle redirect location header
if(line[i].substr(0,div).toLowerCase()=='location'){
callback('status',302);
callback('header','Location',line[i].substr(div+2));
end=true;
callback('end');
return;
}
// remove dublicate headers so that last one counts
headers[ line[i].substr(0,div) ] = line[i].substr(div+2).replace(/\r/g,'');
}
}
// Send headers
for(var i in headers)
callback('header',i,headers[i]);
headersSent=true;
// Send body part if any
if(bbuffer.length>0){
callback('data',bbuffer);
bbuffer='';
}
}
// Body
}else
callback('data',data);
});
// Error. Catch standart error output from script
proc.stderr.on('data', function (data) {
if(end) return;
// Fix 1.2 Store error messages until headers are sent
if(!headersSent){
if(bbuffer.length<4096)
bbuffer+=data.toString();
}else
callback('data',data.toString());
});
proc.stdout.on('close', function () {
if(end) return;
if(!headersSent){
for(var i in headers)
callback('header',i,headers[i]);
headersSent=true;
}
if(bbuffer.length>0)
callback('data',bbuffer);
end=true;
callback('end');
});
}
handleOutput(proc,callback);
}
/*============================================================================*\
Basic CGI launcher
(Use fast CGI in tyhe future)
Parse parameters as arguments and body (POST) on stdin
Maximum header section length is hardcoded to 4K, to prevent unreasonable
memory usages. Are there any reason for a larger buffer size?
Maximum error body length are hardcoded to 4K too
Quirks:
1. Perl dose not have native hearder support. Headers are hand coded and will
be bug riden.
Fix: 1. Preset content_type to trext/html and be very tolerant
Try to guess where the header section ends.
2. End header section if a HTML tag are encountered.
3. End header section when there is no semicolon in a line
\*============================================================================*/
nscript.launcher.cgi = function(cmd,connection,callback) {
var arg=[];
var env={};
// Prepare arguments
// File to execute
arg.push(connection.docroot + connection.pathname);
// Prepare enviroment variables
// Function to convert objectified query string back to its original
env['QUERY_STRING']= (function objToQueryString(obj){
var k = Object.keys(obj);
var s = "";
for(var i=0;i<k.length;i++) {
s += k[i] + "=" + encodeURIComponent(obj[k[i]]);
if (i != k.length -1) s += "&";
}
return s;
})(connection.query);
for(var i in connection.header)
env[i.toUpperCase() ]=connection.header[i];
env['SCRIPT_NAME']=connection.pathname.substring(connection.pathname.lastIndexOf('/')+1);
env['SCRIPT_FILENAME']=connection.docroot + connection.pathname;
env['PATH_INFO']=env['SCRIPT_FILENAME'];
env['DOCUMENT_URI']=connection.pathname;
env['DOCUMENT_ROOT']=connection.docroot;
env['REQUEST_METHOD']=connection.method;
env['REQUEST_URI']=connection.url;
env['REMOTE_ADDR']=connection.remoteaddress;
env['SERVER_SOFTWARE']=process.version;
env['SERVER_PROTOCOL']='HTTP/' + connection.httpversion;
env['GATEWAY_INTERFACE']='Nscript ' + process.versions.Nscript;
/* Unfinished busines
for(var f in request.files){
connection.files[f]={};
connection.files[f].name=request.files[f].name;
connection.files[f].size=request.files[f].size;
connection.files[f].tmp_name=request.files[f].path;
connection.files[f].type=request.files[f].type;
}
*/
// Start child process
var opt={cwd:connection.docroot,env:env};
var proc = require("child_process").spawn(cmd,arg,opt);
// Catch bad command
proc.on('error', function (err) {
console.error('CGI error: %s, shell command: %s', err,cmd);
});
//Transfer connection request informastion to stdin
proc.stdin.write(JSON.stringify(connection));
proc.stdin.end();
// Get ready for output from the process
function handleOutput(proc,callback){
var buffer='';
var bbuffer='';
var eoh=-1;
var headersSent=false;
var end=false;
var headers={'Content-type':'text/html'}; // Fix 1
// Catch output from scrit and send it to client
proc.stdout.on('data', function (data) {
if(end) return;
if(!headersSent){
// Store headers until a end of header is receiverd (\r\n\r\n) Strip \r
buffer+=data.toString().replace(/\r/g,'');
// Look for end of header section
eoh=buffer.indexOf('\n\n');
// Look for a HTML tag
var i=buffer.indexOf('<');
if(i<eoh) eoh=i-2;
if(eoh>0 || buffer.length< 4096){
// Separate headers from body parts
bbuffer+=buffer.substr(eoh+2);
buffer=buffer.substr(0,eoh+1);
// Divide heades lines
var div =-1;
var line=buffer.split('\n');
for(var i in line){
if(!headersSent){
// Split header into key, value pairs
div = line[i].indexOf(":");
if(div>0)
// remove dublicate headers so that last one counts
headers[ line[i].substr(0,div) ] = line[i].substr(div+2);
else{
// Fix 1.3
headersSent=true;
bbuffer+=line[i]+'\n';
}
}else
bbuffer+=line[i]+'\n';
}
// Send headers
for(var i in headers)
callback('header',i,headers[i]);
headersSent=true;
// Send body part if any
if(bbuffer.length>0){
callback('data',bbuffer);
bbuffer='';
}
}
// Body
}else
callback('data',data);
});
// Error. Catch standart error output from script
proc.stderr.on('data', function (data) {
if(end) return;
// Store error messages until headers are sent
if(!headersSent){
if(bbuffer.length<4096)
bbuffer+=data.toString();
}else
callback('data',data.toString());
});
// End request
proc.stdout.on('close', function () {
if(!headersSent){
for(var i in headers)
callback('header',i,headers[i]);
headersSent=true;
}
if(bbuffer.length>0)
callback('data',bbuffer);
end=true;
callback('end');
});
}
handleOutput(proc,callback);
}