-
Notifications
You must be signed in to change notification settings - Fork 1
/
cgi.js
77 lines (61 loc) · 1.59 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
var sys = require('sys'),
http = require('http');
var Request = function() {
this.method = process.env['REQUEST_METHOD'];
this.headers = {
'host': process.env['HTTP_HOST'],
'user-agent': process.env['HTTP_USER_AGENT']
};
this.url = process.env['REQUEST_URI'];
};
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';
}
sys.puts('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];
}
sys.puts(field + ": " + value);
}
};
this.write = function(message) {
if(!body) {
body = true;
sys.puts("");
}
if(message) sys.print(message);
};
this.flush = function() {
};
this.end = function() {
this.write.apply(this, arguments);
};
};
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);
};
exports.Request = Request;
exports.Response = Response;