BotanSS/net/WebFrame.js

181 lines
3.8 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-07-23 09:15:24 +00:00
var CondStream = cl.load( "botanss.utils.CondStream" );
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" );
2015-05-28 07:27:26 +00:00
router.addRoute( "403", false, "403" );
2015-04-15 10:38:44 +00:00
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 = {
2015-05-28 07:27:26 +00:00
"403": function()
{
res.statusCode = 403;
_self.result = "403 Forbidden";
_self.plantResult();
}
, "404": function()
2014-10-10 08:38:06 +00:00
{
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 )
{
2015-07-23 09:15:24 +00:00
_self.requestStr = new CondStream( "/tmp/", 2048 );
this.HTTP.request.raw.addListener(
"data" , ( x ) => _self.requestStr.write( x )
);
this.HTTP.request.raw.addListener(
"end", () => _self.requestStr.end( () => _self.parseResult() )
);
2015-04-15 10:38:44 +00:00
2014-10-10 08:38:06 +00:00
method = "POST";
}
2015-05-30 12:07:42 +00:00
var url = this.HTTP.request.raw.url;
2014-10-10 08:38:06 +00:00
Dragonfly.Info(
2015-01-06 07:16:35 +00:00
( this.HTTP.request.raw.headers[ "x-forwarded-for" ] || this.HTTP.request.remoteAddr ) + " "
2015-05-30 12:07:42 +00:00
+ method + ": " + encodeURI( url )
2014-10-10 08:38:06 +00:00
+ " - " + this.HTTP.request.raw.headers["user-agent"]
, Dragonfly.Visibility.VISIBLE
);
2015-05-30 12:07:42 +00:00
if( method == "GET" )
{
_self.queryStr = url.split( "?" )[1];
_self.parseResult();
}
2014-10-10 08:38:06 +00:00
};
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()
{
2015-05-26 09:02:42 +00:00
if( this.planted ) return;
this.planted = true;
if( this.HTTP )
2014-10-10 08:38:06 +00:00
{
2015-05-26 09:02:42 +00:00
if( !( this.result instanceof Buffer ) )
2014-10-10 08:38:06 +00:00
{
2015-05-26 09:02:42 +00:00
this.result = new Buffer( this.result + "" );
2014-10-10 08:38:06 +00:00
}
2015-05-26 09:02:42 +00:00
this.HTTP.response.headers["Content-Length"] = this.result.length;
this.HTTP.response.write( this.result );
this.HTTP.response.end();
2015-08-17 08:30:18 +00:00
Dragonfly.Debug( "Result Planted" );
2015-05-26 09:02:42 +00:00
}
2015-07-28 04:25:10 +00:00
// Release resources
if( this.requestStr )
{
this.requestStr.discard();
}
2015-05-26 09:02:42 +00:00
};
// This won't handle path exists
// throwing an error is better than handling it
// Need to handle it somewhere else
Framework.prototype.plantFile = function( path, name )
{
if( this.planted ) return;
var _self = this;
this.planted = true;
var resp = this.HTTP.response;
if( !name )
{
var p = require( "path" );
name = p.basename( path );
2014-10-10 08:38:06 +00:00
}
2015-05-26 09:02:42 +00:00
resp.headers[ "Content-Disposition" ] = "attachment; filename=\"" + name + "\"";
var fs = require( "fs" );
Dragonfly.Debug( "Stream out: " + path );
var rs = fs.createReadStream( path );
rs.addListener( "data", ( x ) => resp.write( x ) );
rs.addListener( "end", () => resp.end() );
2014-10-10 08:38:06 +00:00
};
module.exports = Framework;