forked from totaljs/framework5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.js
94 lines (74 loc) · 1.8 KB
/
http.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
// Total.js HTTP Server handler
// The MIT License
// Copyright 2023 (c) Peter Širka <petersirka@gmail.com>
'use strict';
exports.listen = function(req, res) {
// req.ip
// req.headers
// req.url
// req.method
// req.on('data', function(chunk))
// req.destroy()
// res.writeHead(status, headers)
// res.pipe();
// res.write();
// res.end()
F.stats.request.request++;
// Not supported
if (req.method === 'HEAD') {
F.stats.request.blocked++;
req.destroy();
return;
}
var ctrl = new F.TController.Controller(req, res);
if (F.paused.length) {
ctrl.fallback(999);
return;
}
if (F.config.$blacklist && F.config.$blacklist.indexOf(ctrl.ip) !== -1) {
F.stats.request.blocked++;
ctrl.fallback(400, 'IP address is blocked');
return;
}
if (F.config.$httpreqlimit) {
if (F.temporary.ddos[ctrl.ip] > F.config.$httpreqlimit) {
F.stats.response.ddos++;
ctrl.fallback(503);
return;
}
if (F.temporary.ddos[ctrl.ip])
F.temporary.ddos[ctrl.ip]++;
else
F.temporary.ddos[ctrl.ip] = 1;
}
if (F.routes.proxies.length && F.TRouting.lookupproxy(ctrl))
return;
if (F.routes.virtual[ctrl.url]) {
F.routes.virtual[ctrl.url](ctrl);
return;
}
// Pending requests
F.temporary.pending.push(ctrl);
if (F.$events.request) {
/*
@Path: Framework
@Event: ON('request', function(ctrl) { ... }); #ctrl {Controller};
The event captures all incoming requests. The next processing can be canceled via the `ctrl.cancel()` method.
*/
F.emit('request', ctrl);
if (ctrl.iscanceled)
return;
}
if (ctrl.headers.origin && (F.def.onCORS || F.config.$cors)) {
if (F.TRouting.lookupcors(ctrl))
ctrl.$route();
} else
ctrl.$route();
// stream.headers
// stream.url
// respond(opt);
// opt.status = 200;
// opt.headers = {};
// opt.stream = '';
// opt.end = '';
};