to es6 classes
This commit is contained in:
		
							
								
								
									
										12
									
								
								net/Http.js
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								net/Http.js
									
									
									
									
									
								
							@@ -7,7 +7,7 @@ var Cookie = cl.load( "botanss.net.components.Cookie" );
 | 
			
		||||
 | 
			
		||||
class CResponse
 | 
			
		||||
{
 | 
			
		||||
	constructor( res )
 | 
			
		||||
	constructor( res, Http )
 | 
			
		||||
	{
 | 
			
		||||
		this.raw = res;
 | 
			
		||||
		this.canExit = true;
 | 
			
		||||
@@ -19,7 +19,7 @@ class CResponse
 | 
			
		||||
		};
 | 
			
		||||
 | 
			
		||||
		this.content = "";
 | 
			
		||||
		this.cookie = new Cookie( "", this );
 | 
			
		||||
		this.cookie = new Cookie( "", Http );
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -45,11 +45,11 @@ class CRequest
 | 
			
		||||
	get isPost() { return this.raw.method == 'POST'; }
 | 
			
		||||
	get remoteAddr() { return this.raw.connection.remoteAddress; }
 | 
			
		||||
 | 
			
		||||
	constructor( req )
 | 
			
		||||
	constructor( req, Http )
 | 
			
		||||
	{
 | 
			
		||||
		this.raw = req;
 | 
			
		||||
		this.uri = require('url').parse( req.url );
 | 
			
		||||
		this.cookie = new Cookie( req.headers.cookie, this )
 | 
			
		||||
		this.cookie = new Cookie( req.headers.cookie, Http )
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -60,8 +60,8 @@ class Http
 | 
			
		||||
		var _self = this;
 | 
			
		||||
 | 
			
		||||
		// Simple Http Model
 | 
			
		||||
		this.response = new CResponse( res );
 | 
			
		||||
		this.request = new CRequest( req );
 | 
			
		||||
		this.response = new CResponse( res, this );
 | 
			
		||||
		this.request = new CRequest( req, this );
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										225
									
								
								net/Router.js
									
									
									
									
									
								
							
							
						
						
									
										225
									
								
								net/Router.js
									
									
									
									
									
								
							@@ -1,12 +1,10 @@
 | 
			
		||||
"use strict";
 | 
			
		||||
 | 
			
		||||
var cl = global.botanLoader;
 | 
			
		||||
var Dragonfly = global.Dragonfly;
 | 
			
		||||
 | 
			
		||||
var util = require( "util" );
 | 
			
		||||
var events = require( "events" );
 | 
			
		||||
 | 
			
		||||
var FatalError = cl.load( "botanss.errors.FatalError" )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
var MaxRedirect = 10;
 | 
			
		||||
 | 
			
		||||
var stripURI = function( url )
 | 
			
		||||
@@ -14,124 +12,133 @@ var stripURI = function( url )
 | 
			
		||||
	return url.replace( /\/+/g, '/' ).replace( /^\//, '' );
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
var RelayPoint = function( uri, internal )
 | 
			
		||||
class RelayPoint
 | 
			
		||||
{
 | 
			
		||||
	this.direct = Boolean(
 | 
			
		||||
		typeof internal !== 'undefined' ? internal : false
 | 
			
		||||
	);
 | 
			
		||||
 | 
			
		||||
	this.value = internal ? uri : stripURI( uri );
 | 
			
		||||
	this.params = Array.prototype.slice.call( arguments, 2 );
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
var Router = function( http )
 | 
			
		||||
{
 | 
			
		||||
	events.EventEmitter.call( this );
 | 
			
		||||
 | 
			
		||||
	this.HTTP = http;
 | 
			
		||||
	this.routes = {};
 | 
			
		||||
	this.routable = true;
 | 
			
		||||
	this.redirect_count = 0;
 | 
			
		||||
 | 
			
		||||
	// Changed when routing
 | 
			
		||||
	this.inputs = [];
 | 
			
		||||
	this.routeObj = {};
 | 
			
		||||
	this.reRoute = false;
 | 
			
		||||
 | 
			
		||||
	this.relaying = new RelayPoint( http.request.raw.url.split( "?" )[0] );
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
util.inherits( Router, events.EventEmitter );
 | 
			
		||||
 | 
			
		||||
Router.prototype.addRoute = function( _name, _route, _action, _status )
 | 
			
		||||
{
 | 
			
		||||
	this.routes[ _name ] = {
 | 
			
		||||
		name: _name
 | 
			
		||||
		, route: _route
 | 
			
		||||
		, action: String( _action )
 | 
			
		||||
		, status: Boolean(
 | 
			
		||||
			typeof _status !== 'undefined' ? _status : true
 | 
			
		||||
		)
 | 
			
		||||
	};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Router.prototype.route = function()
 | 
			
		||||
{
 | 
			
		||||
	if( MaxRedirect < this.redirect_count )
 | 
			
		||||
		throw new FatalError( "Max redirection reached" );
 | 
			
		||||
 | 
			
		||||
	this.redirect_count ++;
 | 
			
		||||
	this.inputs[ this.inputs.length ] = this.relaying;
 | 
			
		||||
 | 
			
		||||
	var currentRoute = this.relaying.value;
 | 
			
		||||
	var r;
 | 
			
		||||
 | 
			
		||||
	// Direct means reroute using route key to match the route
 | 
			
		||||
	if( this.relaying.direct )
 | 
			
		||||
	constructor( uri, internal )
 | 
			
		||||
	{
 | 
			
		||||
		if( r = this.routes[ this.relaying.value ] )
 | 
			
		||||
		{
 | 
			
		||||
			this.routable = false;
 | 
			
		||||
			this.setRoute( r );
 | 
			
		||||
			return r.action;
 | 
			
		||||
		}
 | 
			
		||||
		this.direct = Boolean(
 | 
			
		||||
			typeof internal !== 'undefined' ? internal : false
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
		this.value = internal ? uri : stripURI( uri );
 | 
			
		||||
		this.params = Array.prototype.slice.call( arguments, 2 );
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
var EventEmitter = require( "events" ).EventEmitter;
 | 
			
		||||
 | 
			
		||||
class Router extends EventEmitter
 | 
			
		||||
{
 | 
			
		||||
	constructor ( http )
 | 
			
		||||
	{
 | 
			
		||||
		// Parse RegEx rules one by one
 | 
			
		||||
		for( var i in this.routes )
 | 
			
		||||
		super();
 | 
			
		||||
 | 
			
		||||
		this.HTTP = http;
 | 
			
		||||
		this.routes = {};
 | 
			
		||||
		this.routable = true;
 | 
			
		||||
		this.redirect_count = 0;
 | 
			
		||||
 | 
			
		||||
		// Changed when routing
 | 
			
		||||
		this.inputs = [];
 | 
			
		||||
		this.routeObj = {};
 | 
			
		||||
		this.reRoute = false;
 | 
			
		||||
 | 
			
		||||
		this.relaying = new RelayPoint( http.request.raw.url.split( "?" )[0] );
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	addRoute( _name, _route, _action, _status )
 | 
			
		||||
	{
 | 
			
		||||
		this.routes[ _name ] = {
 | 
			
		||||
			name: _name
 | 
			
		||||
			, route: _route
 | 
			
		||||
			, action: String( _action )
 | 
			
		||||
			, status: Boolean(
 | 
			
		||||
				typeof _status !== 'undefined' ? _status : true
 | 
			
		||||
			)
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	route()
 | 
			
		||||
	{
 | 
			
		||||
		if( MaxRedirect < this.redirect_count )
 | 
			
		||||
			throw new FatalError( "Max redirection reached" );
 | 
			
		||||
 | 
			
		||||
		this.redirect_count ++;
 | 
			
		||||
		this.inputs[ this.inputs.length ] = this.relaying;
 | 
			
		||||
 | 
			
		||||
		var currentRoute = this.relaying.value;
 | 
			
		||||
		var r;
 | 
			
		||||
 | 
			
		||||
		// Direct means reroute using route key to match the route
 | 
			
		||||
		if( this.relaying.direct )
 | 
			
		||||
		{
 | 
			
		||||
			r = this.routes[i];
 | 
			
		||||
			if( r.route.constructor.prototype.exec )
 | 
			
		||||
			if( r = this.routes[ this.relaying.value ] )
 | 
			
		||||
			{
 | 
			
		||||
				var match = r.route.exec( currentRoute );
 | 
			
		||||
				if( match )
 | 
			
		||||
				this.routable = false;
 | 
			
		||||
				this.setRoute( r );
 | 
			
		||||
				return r.action;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			// Parse RegEx rules one by one
 | 
			
		||||
			for( var i in this.routes )
 | 
			
		||||
			{
 | 
			
		||||
				r = this.routes[i];
 | 
			
		||||
				if( r.route.constructor.prototype.exec )
 | 
			
		||||
				{
 | 
			
		||||
					// Set routable to false
 | 
			
		||||
					this.routable = false;
 | 
			
		||||
					this.setRoute( r, match );
 | 
			
		||||
					return r.action;
 | 
			
		||||
					var match = r.route.exec( currentRoute );
 | 
			
		||||
					if( match )
 | 
			
		||||
					{
 | 
			
		||||
						// Set routable to false
 | 
			
		||||
						this.routable = false;
 | 
			
		||||
						this.setRoute( r, match );
 | 
			
		||||
						return r.action;
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		this.setRoute( "*", currentRoute );
 | 
			
		||||
		this.routable = false;
 | 
			
		||||
		return false;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	this.setRoute( "*", currentRoute );
 | 
			
		||||
	this.routable = false;
 | 
			
		||||
	return false;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Set Route
 | 
			
		||||
Router.prototype.setRoute = function( _route, _match )
 | 
			
		||||
{
 | 
			
		||||
	var _self = this;
 | 
			
		||||
	this.routeObj = {
 | 
			
		||||
		route: _route
 | 
			
		||||
		, match: _match
 | 
			
		||||
		, router: _self.router
 | 
			
		||||
		, inputs: _self.inputs
 | 
			
		||||
	// Set Route
 | 
			
		||||
	setRoute( _route, _match )
 | 
			
		||||
	{
 | 
			
		||||
		var _self = this;
 | 
			
		||||
		this.routeObj = {
 | 
			
		||||
			route: _route
 | 
			
		||||
			, match: _match
 | 
			
		||||
			, router: _self.router
 | 
			
		||||
			, inputs: _self.inputs
 | 
			
		||||
 | 
			
		||||
			// Re-route function
 | 
			
		||||
			, reRoute: function( target, direct )
 | 
			
		||||
			{
 | 
			
		||||
				Dragonfly.Debug(
 | 
			
		||||
					"Reroute: " + target
 | 
			
		||||
					+ ( direct ? ", Direct" : "" )
 | 
			
		||||
				);
 | 
			
		||||
				_self.relaying = new RelayPoint( target, direct );
 | 
			
		||||
				_self.routable = true;
 | 
			
		||||
				_self.reRoute = true;
 | 
			
		||||
				_self.emit( "Route" );
 | 
			
		||||
			}
 | 
			
		||||
			, redirect: function( target )
 | 
			
		||||
			{
 | 
			
		||||
				Dragonfly.Debug( "Redirect: " + target );
 | 
			
		||||
				_self.relaying = new RelayPoint( "302", true, target );
 | 
			
		||||
				_self.routable = true;
 | 
			
		||||
				_self.reRoute = true;
 | 
			
		||||
				_self.emit( "Route" );
 | 
			
		||||
			}
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		// Re-route function
 | 
			
		||||
		, reRoute: function( target, direct )
 | 
			
		||||
		{
 | 
			
		||||
			Dragonfly.Debug(
 | 
			
		||||
				"Reroute: " + target
 | 
			
		||||
				+ ( direct ? ", Direct" : "" )
 | 
			
		||||
			);
 | 
			
		||||
			_self.relaying = new RelayPoint( target, direct );
 | 
			
		||||
			_self.routable = true;
 | 
			
		||||
			_self.reRoute = true;
 | 
			
		||||
			_self.emit( "Route" );
 | 
			
		||||
		}
 | 
			
		||||
		, redirect: function( target )
 | 
			
		||||
		{
 | 
			
		||||
			Dragonfly.Debug( "Redirect: " + target );
 | 
			
		||||
			_self.relaying = new RelayPoint( "302", true, target );
 | 
			
		||||
			_self.routable = true;
 | 
			
		||||
			_self.reRoute = true;
 | 
			
		||||
			_self.emit( "Route" );
 | 
			
		||||
		}
 | 
			
		||||
	};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = Router;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										296
									
								
								net/WebFrame.js
									
									
									
									
									
								
							
							
						
						
									
										296
									
								
								net/WebFrame.js
									
									
									
									
									
								
							@@ -1,180 +1,184 @@
 | 
			
		||||
"use strict";
 | 
			
		||||
 | 
			
		||||
var cl = global.botanLoader;
 | 
			
		||||
var Dragonfly = global.Dragonfly;
 | 
			
		||||
var CondStream = cl.load( "botanss.utils.CondStream" );
 | 
			
		||||
 | 
			
		||||
var FatalError = cl.load( "botanss.errors.FatalError" );
 | 
			
		||||
 | 
			
		||||
var Framework = function( garden )
 | 
			
		||||
class WebFrame
 | 
			
		||||
{
 | 
			
		||||
	var _self = this;
 | 
			
		||||
 | 
			
		||||
	this.HTTP = garden;
 | 
			
		||||
	this.result = 0;
 | 
			
		||||
	this.planted = false;
 | 
			
		||||
	this.requestStr = "";
 | 
			
		||||
	this.requestObj = {};
 | 
			
		||||
 | 
			
		||||
	var Router = cl.load( "botanss.net.Router" );
 | 
			
		||||
 | 
			
		||||
	var router = new Router( garden );
 | 
			
		||||
	router.addRoute( "302", false, "302" );
 | 
			
		||||
	router.addRoute( "403", false, "403" );
 | 
			
		||||
	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;
 | 
			
		||||
 | 
			
		||||
	this.handlers = {
 | 
			
		||||
		"403": function()
 | 
			
		||||
		{
 | 
			
		||||
			res.statusCode = 403;
 | 
			
		||||
			_self.result = "403 Forbidden";
 | 
			
		||||
			_self.plantResult();
 | 
			
		||||
		}
 | 
			
		||||
		, "404": function()
 | 
			
		||||
		{
 | 
			
		||||
			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();
 | 
			
		||||
		}
 | 
			
		||||
		, "500": function()
 | 
			
		||||
		{
 | 
			
		||||
			res.statusCode = 500;
 | 
			
		||||
			_self.result = "500 Internal Server Error";
 | 
			
		||||
			_self.plantResult();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Framework.prototype.run = function()
 | 
			
		||||
{
 | 
			
		||||
	var _self = this;
 | 
			
		||||
 | 
			
		||||
	var method = "GET";
 | 
			
		||||
	if( this.HTTP.request.isPost )
 | 
			
		||||
	constructor( Http )
 | 
			
		||||
	{
 | 
			
		||||
		_self.requestStr = new CondStream( "/tmp/", 2048 );
 | 
			
		||||
		var _self = this;
 | 
			
		||||
 | 
			
		||||
		this.HTTP.request.raw.addListener(
 | 
			
		||||
			"data" , ( x ) => _self.requestStr.write( x )
 | 
			
		||||
		);
 | 
			
		||||
		this.HTTP = Http;
 | 
			
		||||
		this.result = 0;
 | 
			
		||||
		this.planted = false;
 | 
			
		||||
		this.requestStr = "";
 | 
			
		||||
		this.requestObj = {};
 | 
			
		||||
 | 
			
		||||
		this.HTTP.request.raw.addListener(
 | 
			
		||||
			"end", () => _self.requestStr.end( () => _self.parseResult() )
 | 
			
		||||
		);
 | 
			
		||||
		var Router = cl.load( "botanss.net.Router" );
 | 
			
		||||
 | 
			
		||||
		method = "POST";
 | 
			
		||||
	}
 | 
			
		||||
		var router = new Router( Http );
 | 
			
		||||
		router.addRoute( "302", false, "302" );
 | 
			
		||||
		router.addRoute( "403", false, "403" );
 | 
			
		||||
		router.addRoute( "404", false, "404" );
 | 
			
		||||
		router.addRoute( "500", false, "500" );
 | 
			
		||||
		router.addListener( "Route", this.__parse.bind( this ) );
 | 
			
		||||
 | 
			
		||||
	var url = this.HTTP.request.raw.url;
 | 
			
		||||
	Dragonfly.Info(
 | 
			
		||||
		( this.HTTP.request.raw.headers[ "x-forwarded-for" ] || this.HTTP.request.remoteAddr ) + " "
 | 
			
		||||
		+ method + ": " + encodeURI( url )
 | 
			
		||||
		+ " - " + this.HTTP.request.raw.headers["user-agent"]
 | 
			
		||||
		, Dragonfly.Visibility.VISIBLE
 | 
			
		||||
	);
 | 
			
		||||
		this.router = router;
 | 
			
		||||
 | 
			
		||||
	if( method == "GET" )
 | 
			
		||||
	{
 | 
			
		||||
		_self.queryStr = url.split( "?" )[1];
 | 
			
		||||
		_self.parseResult();
 | 
			
		||||
	}
 | 
			
		||||
};
 | 
			
		||||
		var res = this.HTTP.response;
 | 
			
		||||
 | 
			
		||||
Framework.prototype.addHandler = function( name, method )
 | 
			
		||||
{
 | 
			
		||||
	this.handlers[ name ] = method.bind( this );
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Framework.prototype.parseResult = function()
 | 
			
		||||
{
 | 
			
		||||
	if( this.router.routable )
 | 
			
		||||
	{
 | 
			
		||||
		var method = this.router.route();
 | 
			
		||||
		if( method )
 | 
			
		||||
		{
 | 
			
		||||
			Dragonfly.Debug( "Call " + method, Dragonfly.Spheres.THERMO );
 | 
			
		||||
 | 
			
		||||
			if( this.handlers[ method ] )
 | 
			
		||||
		this.handlers = {
 | 
			
		||||
			"403": function()
 | 
			
		||||
			{
 | 
			
		||||
				this.handlers[ method ]( this.router.routeObj );
 | 
			
		||||
				return;
 | 
			
		||||
				res.statusCode = 403;
 | 
			
		||||
				_self.result = "403 Forbidden";
 | 
			
		||||
				_self.plantResult();
 | 
			
		||||
			}
 | 
			
		||||
			, "404": function()
 | 
			
		||||
			{
 | 
			
		||||
				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();
 | 
			
		||||
			}
 | 
			
		||||
			, "500": function()
 | 
			
		||||
			{
 | 
			
		||||
				res.statusCode = 500;
 | 
			
		||||
				_self.result = "500 Internal Server Error";
 | 
			
		||||
				_self.plantResult();
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		else if( method === false )
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	run()
 | 
			
		||||
	{
 | 
			
		||||
		var _self = this;
 | 
			
		||||
 | 
			
		||||
		var method = "GET";
 | 
			
		||||
		if( this.HTTP.request.isPost )
 | 
			
		||||
		{
 | 
			
		||||
			Dragonfly.Debug( "No route is defined to handle this URI", Dragonfly.Spheres.THERMO );
 | 
			
		||||
			this.router.routeObj.reRoute( "404", true );
 | 
			
		||||
			return;
 | 
			
		||||
			this.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.__parse() )
 | 
			
		||||
			);
 | 
			
		||||
 | 
			
		||||
			method = "POST";
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		throw new FatalError( "Relay handler \"" + method + "\" is not defined" );
 | 
			
		||||
		var url = this.HTTP.request.raw.url;
 | 
			
		||||
		Dragonfly.Info(
 | 
			
		||||
			( this.HTTP.request.raw.headers[ "x-forwarded-for" ] || this.HTTP.request.remoteAddr ) + " "
 | 
			
		||||
			+ method + ": " + encodeURI( url )
 | 
			
		||||
			+ " - " + this.HTTP.request.raw.headers["user-agent"]
 | 
			
		||||
			, Dragonfly.Visibility.VISIBLE
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
		if( method == "GET" )
 | 
			
		||||
		{
 | 
			
		||||
			this.queryStr = url.split( "?" )[1];
 | 
			
		||||
			this.__parse();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Framework.prototype.plantResult = function()
 | 
			
		||||
{
 | 
			
		||||
	if( this.planted ) return;
 | 
			
		||||
 | 
			
		||||
	this.planted = true;
 | 
			
		||||
	if( this.HTTP )
 | 
			
		||||
	addHandler( name, method )
 | 
			
		||||
	{
 | 
			
		||||
		if( !( this.result instanceof Buffer ) )
 | 
			
		||||
		this.handlers[ name ] = method.bind( this );
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	plantResult()
 | 
			
		||||
	{
 | 
			
		||||
		if( this.planted ) return;
 | 
			
		||||
 | 
			
		||||
		this.planted = true;
 | 
			
		||||
		if( this.HTTP )
 | 
			
		||||
		{
 | 
			
		||||
			this.result = new Buffer( this.result + "" );
 | 
			
		||||
			if( !( this.result instanceof Buffer ) )
 | 
			
		||||
			{
 | 
			
		||||
				this.result = new Buffer( this.result + "" );
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			this.HTTP.response.headers["Content-Length"] = this.result.length;
 | 
			
		||||
			this.HTTP.response.write( this.result );
 | 
			
		||||
			this.HTTP.response.end();
 | 
			
		||||
			Dragonfly.Debug( "Result Planted" );
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		this.HTTP.response.headers["Content-Length"] = this.result.length;
 | 
			
		||||
		this.HTTP.response.write( this.result );
 | 
			
		||||
		this.HTTP.response.end();
 | 
			
		||||
		Dragonfly.Debug( "Result Planted" );
 | 
			
		||||
		// Release resources
 | 
			
		||||
		if( this.requestStr )
 | 
			
		||||
		{
 | 
			
		||||
			this.requestStr.discard();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Release resources
 | 
			
		||||
	if( this.requestStr )
 | 
			
		||||
	// This won't handle path exists
 | 
			
		||||
	// throwing an error is better than handling it
 | 
			
		||||
	// Need to handle it somewhere else
 | 
			
		||||
	plantFile( path, name )
 | 
			
		||||
	{
 | 
			
		||||
		this.requestStr.discard();
 | 
			
		||||
		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 );
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		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() );
 | 
			
		||||
	}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// 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 )
 | 
			
		||||
	__parse()
 | 
			
		||||
	{
 | 
			
		||||
		var p = require( "path" );
 | 
			
		||||
		name = p.basename( path );
 | 
			
		||||
		if( this.router.routable )
 | 
			
		||||
		{
 | 
			
		||||
			var method = this.router.route();
 | 
			
		||||
			if( method )
 | 
			
		||||
			{
 | 
			
		||||
				Dragonfly.Debug( "Call " + method, Dragonfly.Spheres.THERMO );
 | 
			
		||||
 | 
			
		||||
				if( this.handlers[ method ] )
 | 
			
		||||
				{
 | 
			
		||||
					this.handlers[ method ]( this.router.routeObj );
 | 
			
		||||
					return;
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			else if( method === false )
 | 
			
		||||
			{
 | 
			
		||||
				Dragonfly.Debug( "No route is defined to handle this URI", Dragonfly.Spheres.THERMO );
 | 
			
		||||
				this.router.routeObj.reRoute( "404", true );
 | 
			
		||||
				return;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			throw new FatalError( "Relay handler \"" + method + "\" is not defined" );
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
	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() );
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = Framework;
 | 
			
		||||
module.exports = WebFrame;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,43 +1,43 @@
 | 
			
		||||
"use strict";
 | 
			
		||||
 | 
			
		||||
var cl = global.botanLoader;
 | 
			
		||||
 | 
			
		||||
var util = require( "util" );
 | 
			
		||||
 | 
			
		||||
var WebParam = cl.load( "botanss.utils.WebParam" );
 | 
			
		||||
 | 
			
		||||
var Cookie = function( cookieStr, HTTP )
 | 
			
		||||
class Cookie extends WebParam
 | 
			
		||||
{
 | 
			
		||||
	WebParam.call( this, cookieStr );
 | 
			
		||||
	this.HTTP = HTTP;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
util.inherits( Cookie, WebParam );
 | 
			
		||||
 | 
			
		||||
Cookie.prototype.seth = function( name, value )
 | 
			
		||||
{
 | 
			
		||||
	this.set( name, value );
 | 
			
		||||
	this.HTTP.response.headers[ "Set-Cookie" ] = this.toString();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Cookie.prototype.toString = function()
 | 
			
		||||
{
 | 
			
		||||
	var cookieStr = "";
 | 
			
		||||
	var p = "";
 | 
			
		||||
	var e = "";
 | 
			
		||||
	for( var i in this.param )
 | 
			
		||||
	constructor( cookieStr, HTTP )
 | 
			
		||||
	{
 | 
			
		||||
		switch( i.toLowerCase() )
 | 
			
		||||
		{
 | 
			
		||||
			case "path":
 | 
			
		||||
				p = this.param[i];
 | 
			
		||||
				continue;
 | 
			
		||||
			case "expires":
 | 
			
		||||
				e = this.param[i];
 | 
			
		||||
				continue;
 | 
			
		||||
		}
 | 
			
		||||
		cookieStr += i + "=" + this.param[i] + ";";
 | 
			
		||||
		super( cookieStr );
 | 
			
		||||
		this.HTTP = HTTP;
 | 
			
		||||
	}
 | 
			
		||||
	cookieStr += "Path=" + p + ";" + ( e ? ( " Expires=" + e + ";" ) : "" );
 | 
			
		||||
	return cookieStr;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
	seth( name, value )
 | 
			
		||||
	{
 | 
			
		||||
		this.set( name, value );
 | 
			
		||||
		this.HTTP.response.headers[ "Set-Cookie" ] = this.toString();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	toString()
 | 
			
		||||
	{
 | 
			
		||||
		var cookieStr = "";
 | 
			
		||||
		var p = "";
 | 
			
		||||
		var e = "";
 | 
			
		||||
		for( var i in this.param )
 | 
			
		||||
		{
 | 
			
		||||
			switch( i.toLowerCase() )
 | 
			
		||||
			{
 | 
			
		||||
				case "path":
 | 
			
		||||
					p = this.param[i];
 | 
			
		||||
					continue;
 | 
			
		||||
				case "expires":
 | 
			
		||||
					e = this.param[i];
 | 
			
		||||
					continue;
 | 
			
		||||
			}
 | 
			
		||||
			cookieStr += i + "=" + this.param[i] + ";";
 | 
			
		||||
		}
 | 
			
		||||
		cookieStr += "Path=" + p + ";" + ( e ? ( " Expires=" + e + ";" ) : "" );
 | 
			
		||||
		return cookieStr;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = Cookie;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user