BotanSS/net/WebFrame.js

134 lines
2.7 KiB
JavaScript
Raw Normal View History

2015-04-18 14:14:36 +00:00
var cl = global.botanLoader;
2014-10-10 08:38:06 +00:00
var Dragonfly = global.Dragonfly;
2015-04-18 14:14:36 +00:00
var FatalError = cl.load( "botanss.errors.FatalError" );
2014-10-10 08:38:06 +00:00
var Framework = function( garden )
{
2015-04-15 10:38:44 +00:00
var _self = this;
2014-10-10 08:38:06 +00:00
this.HTTP = garden;
this.result = 0;
this.planted = false;
this.requestStr = "";
this.requestObj = {};
2015-04-18 14:14:36 +00:00
var Router = cl.load( "botanss.net.Router" );
2015-04-15 10:38:44 +00:00
var router = new Router( garden );
router.addRoute( "302", false, "302" );
router.addRoute( "404", false, "404" );
router.addRoute( "500", false, "500" );
router.addListener( "Route", this.parseResult.bind( this ) );
this.router = router;
var res = this.HTTP.response;
2014-10-10 08:38:06 +00:00
this.handlers = {
"404": function()
{
2015-04-15 10:38:44 +00:00
res.statusCode = 404;
_self.result = "404 Not Found";
_self.plantResult();
}
, "302": function()
{
res.statusCode = 302;
res.headers[ "Location" ] = router.relaying.params[0];
_self.result = "";
_self.plantResult();
}
2015-02-12 08:00:45 +00:00
, "500": function()
{
2015-04-15 10:38:44 +00:00
res.statusCode = 500;
_self.result = "500 Internal Server Error";
_self.plantResult();
}
2014-10-10 08:38:06 +00:00
}
};
Framework.prototype.run = function()
{
var _self = this;
var method = "GET";
if( this.HTTP.request.isPost )
{
this.HTTP.request.raw.addListener( "data", function( data )
{
_self.requestStr += data.toString();
})
this.HTTP.request.raw.addListener( "end", function()
{
_self.parseResult();
});
2015-04-15 10:38:44 +00:00
2014-10-10 08:38:06 +00:00
method = "POST";
}
Dragonfly.Info(
2015-01-06 07:16:35 +00:00
( this.HTTP.request.raw.headers[ "x-forwarded-for" ] || this.HTTP.request.remoteAddr ) + " "
2014-10-22 05:14:33 +00:00
+ method + ": " + encodeURI( this.HTTP.request.raw.url )
2014-10-10 08:38:06 +00:00
+ " - " + this.HTTP.request.raw.headers["user-agent"]
, Dragonfly.Visibility.VISIBLE
);
if( method == "GET" ) _self.parseResult();
};
Framework.prototype.addHandler = function( name, method )
{
this.handlers[ name ] = method.bind( this );
};
Framework.prototype.parseResult = function()
{
2014-10-22 10:48:34 +00:00
if( this.router.routable )
2014-10-10 08:38:06 +00:00
{
var method = this.router.route();
if( method )
{
2015-02-12 08:00:45 +00:00
Dragonfly.Debug( "Call " + method, Dragonfly.Spheres.THERMO );
2014-10-10 08:38:06 +00:00
if( this.handlers[ method ] )
{
this.handlers[ method ]( this.router.routeObj );
2014-10-22 10:48:34 +00:00
return;
2014-10-10 08:38:06 +00:00
}
}
else if( method === false )
{
2015-02-12 08:00:45 +00:00
Dragonfly.Debug( "No route is defined to handle this URI", Dragonfly.Spheres.THERMO );
2014-10-22 10:48:34 +00:00
this.router.routeObj.reRoute( "404", true );
return;
}
2014-10-10 08:38:06 +00:00
throw new FatalError( "Relay handler \"" + method + "\" is not defined" );
}
};
Framework.prototype.plantResult = function()
{
if( !this.planted )
{
this.planted = true;
if( this.HTTP )
{
if( !( this.result instanceof Buffer ) )
{
2015-02-17 06:01:26 +00:00
this.result = new Buffer( this.result + "" );
2014-10-10 08:38:06 +00:00
}
this.HTTP.response.headers["Content-Length"] = this.result.length;
this.HTTP.response.write( this.result );
this.HTTP.response.end();
}
}
};
module.exports = Framework;