-
Notifications
You must be signed in to change notification settings - Fork 17
/
nerve.js
129 lines (116 loc) · 3.3 KB
/
nerve.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
var sys = require('sys');
var http = require('http');
require('./http_state');
(function() {
process.mixin(http.ServerResponse.prototype, {
respond: function(response_data) {
var headers = {
'Content-Type': 'text/html',
'Content-Length': (response_data.content && response_data.content.length) || response_data.length || 0,
}
if(this.cookies) headers['Set-Cookie'] = this.cookies.join(', ');
for(name in response_data.headers) headers[name] = response_data.headers[name];
this.sendHeader(response_data.status_code || 200, headers);
this.sendBody(response_data.content || response_data);
this.finish();
}
});
function match_request(matcher, req) {
if(typeof matcher === 'string') {
return (matcher === req.url);
} else if(matcher.constructor === RegExp) {
return req.url.match(matcher);
} else {
return req.url.match(matcher.apply(req));
}
}
function to_regexp(pattern) {
if(pattern.constructor === RegExp) {
return pattern;
} else {
return new RegExp('^' + pattern + '$');
}
}
function get(pattern) {
return function() {
if(this.method !== 'GET') {
return false;
} else {
return to_regexp(pattern);
}
}
};
function post(pattern) {
return function() {
if(this.method !== 'POST') {
return false;
} else {
return to_regexp(pattern);
}
}
};
function put(pattern) {
return function() {
if(this.method !== 'PUT') {
return false;
} else {
return to_regexp(pattern);
}
}
};
function del(pattern) {
return function() {
if(this.method !== 'DELETE') {
return false;
} else {
return to_regexp(pattern);
}
}
};
function create(app, options) {
function request_handler(req, res) {
req.session = req.get_or_create_session(req, res, {duration: options.session_duration || 30*60*1000});
for(var i = 0; i < app.length; i++) {
var matcher = app[i][0], handler = app[i][1], handler_args = [req, res], match = match_request(matcher, req);
if(match) {
try {
if(typeof match.slice === 'function') {
handler_args = handler_args.concat(match.slice(1));
}
handler.apply(null, handler_args);
} catch(e) {
res.respond({content: '<html><head><title>Exception</title></head><body><h1>Exception</h1><pre>' + sys.inspect(e) + '</pre></body></html>', status_code: 501});
}
return;
}
}
res.respond({content: '<html><head><title>Not Found</title></head><body><h1>Not Found</h1></body></html>', status_code: 404});
}
options = options || {};
if(!options.port && !options.ssl_port) options.port = 8000;
if(options.port) {
var server = http.createServer(request_handler);
}
if(options.ssl_port && options.private_key && options.certificate) {
var ssl_server = http.createServer(request_handler);
ssl_server.setSecure('X509_PEM', options.ca_certs, options.crl_list, options.private_key, options.certificate);
}
return {
serve: function() {
if(server) server.listen(options.port, options.host);
if(ssl_server) ssl_server.listen(options.ssl_port, options.host);
return this;
},
close: function() {
if(server) server.close();
if(ssl_server) ssl_server.close();
return this;
}
}
};
exports.get = get;
exports.post = post;
exports.put = put;
exports.del = del;
exports.create = create;
})();