-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgi.js
100 lines (78 loc) · 2.3 KB
/
cgi.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
/*
Run nodejs scripts as CGI scripts (via .htaccess Rewrite rule)
Original code from: https://github.com/puffnfresh/node-cgi
Adjusted to remove warnings and allow processing of request
body (for HTTP POST form submission)
*/
var http = require('http'),
fs = require('fs')
var Request = function() {
this.url = process.env['REQUEST_URI'];
this.method = process.env['REQUEST_METHOD'];
this.headers = {};
for (var k in process.env) {
if (process.env.hasOwnProperty(k) && k.indexOf("HTTP_") == 0) {
this.headers[k.replace(/^HTTP_/, "").toLowerCase()] = process.env[k];
}
}
// FIXME: There are probably more portable ways to do this, but it works on
// our FreeBSD target and I don't care enough to fix it at the moment
this.body = fs.readFileSync(process.stdin.fd).toString();
this.form = this.body.split("&").reduce(function(map, field) {
var parts = field.split("=")
map[parts[0]] = (parts.length > 1) ? decodeURIComponent(parts[1].replace(/\+/g, " ")) : "";
return map
}, {})
};
// FIXME: There has to be a better way to write to stdout directly
function writeline(data) { console.log(data) }
var Response = function() {
var body = false;
this.writeHead = function() {
var status = arguments[0];
var reason = arguments[1];
var headers = arguments[2];
if (typeof reason != 'string') {
headers = reason;
reason = http.STATUS_CODES[arguments[0]] || 'unknown';
}
writeline('Status: ' + status + ' ' + reason);
var field, value;
var keys = Object.keys(headers);
var isArray = (headers instanceof Array);
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
if (isArray) {
field = headers[key][0];
value = headers[key][1];
} else {
field = key;
value = headers[key];
}
writeline(field + ": " + value);
}
};
this.write = function(message) {
if (!body) {
body = true;
writeline("");
}
if (message) writeline(message);
};
this.flush = function() {
};
this.end = function() {
this.write.apply(this, arguments);
};
this.stream = process.stdout;
};
var Server = function(listener, options) {
var request = new Request();
var response = new Response();
this.listen = function() {
listener(request, response);
};
};
exports.createServer = function(listener, options) {
return new Server(listener, options);
};