total5
Version:
Total.js framework v5
87 lines (68 loc) • 1.73 kB
JavaScript
// Total.js HTTP Server handler
// The MIT License
// Copyright 2023 (c) Peter Širka <petersirka@gmail.com>
;
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++;
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 = '';
};