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
 | 
					class CResponse
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	constructor( res )
 | 
						constructor( res, Http )
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		this.raw = res;
 | 
							this.raw = res;
 | 
				
			||||||
		this.canExit = true;
 | 
							this.canExit = true;
 | 
				
			||||||
@@ -19,7 +19,7 @@ class CResponse
 | 
				
			|||||||
		};
 | 
							};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		this.content = "";
 | 
							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 isPost() { return this.raw.method == 'POST'; }
 | 
				
			||||||
	get remoteAddr() { return this.raw.connection.remoteAddress; }
 | 
						get remoteAddr() { return this.raw.connection.remoteAddress; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	constructor( req )
 | 
						constructor( req, Http )
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		this.raw = req;
 | 
							this.raw = req;
 | 
				
			||||||
		this.uri = require('url').parse( req.url );
 | 
							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;
 | 
							var _self = this;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Simple Http Model
 | 
							// Simple Http Model
 | 
				
			||||||
		this.response = new CResponse( res );
 | 
							this.response = new CResponse( res, this );
 | 
				
			||||||
		this.request = new CRequest( req );
 | 
							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 cl = global.botanLoader;
 | 
				
			||||||
var Dragonfly = global.Dragonfly;
 | 
					var Dragonfly = global.Dragonfly;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var util = require( "util" );
 | 
					 | 
				
			||||||
var events = require( "events" );
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var FatalError = cl.load( "botanss.errors.FatalError" )
 | 
					var FatalError = cl.load( "botanss.errors.FatalError" )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
var MaxRedirect = 10;
 | 
					var MaxRedirect = 10;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var stripURI = function( url )
 | 
					var stripURI = function( url )
 | 
				
			||||||
@@ -14,124 +12,133 @@ var stripURI = function( url )
 | 
				
			|||||||
	return url.replace( /\/+/g, '/' ).replace( /^\//, '' );
 | 
						return url.replace( /\/+/g, '/' ).replace( /^\//, '' );
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var RelayPoint = function( uri, internal )
 | 
					class RelayPoint
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	this.direct = Boolean(
 | 
						constructor( uri, internal )
 | 
				
			||||||
		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 )
 | 
					 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		if( r = this.routes[ this.relaying.value ] )
 | 
							this.direct = Boolean(
 | 
				
			||||||
		{
 | 
								typeof internal !== 'undefined' ? internal : false
 | 
				
			||||||
			this.routable = false;
 | 
							);
 | 
				
			||||||
			this.setRoute( r );
 | 
					
 | 
				
			||||||
			return r.action;
 | 
							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
 | 
							super();
 | 
				
			||||||
		for( var i in this.routes )
 | 
					
 | 
				
			||||||
 | 
							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 = this.routes[ this.relaying.value ] )
 | 
				
			||||||
			if( r.route.constructor.prototype.exec )
 | 
					 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				var match = r.route.exec( currentRoute );
 | 
									this.routable = false;
 | 
				
			||||||
				if( match )
 | 
									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
 | 
										var match = r.route.exec( currentRoute );
 | 
				
			||||||
					this.routable = false;
 | 
										if( match )
 | 
				
			||||||
					this.setRoute( r, match );
 | 
										{
 | 
				
			||||||
					return r.action;
 | 
											// 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
 | 
						// Set Route
 | 
				
			||||||
Router.prototype.setRoute = function( _route, _match )
 | 
						setRoute( _route, _match )
 | 
				
			||||||
{
 | 
						{
 | 
				
			||||||
	var _self = this;
 | 
							var _self = this;
 | 
				
			||||||
	this.routeObj = {
 | 
							this.routeObj = {
 | 
				
			||||||
		route: _route
 | 
								route: _route
 | 
				
			||||||
		, match: _match
 | 
								, match: _match
 | 
				
			||||||
		, router: _self.router
 | 
								, router: _self.router
 | 
				
			||||||
		, inputs: _self.inputs
 | 
								, 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;
 | 
					module.exports = Router;
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										296
									
								
								net/WebFrame.js
									
									
									
									
									
								
							
							
						
						
									
										296
									
								
								net/WebFrame.js
									
									
									
									
									
								
							@@ -1,180 +1,184 @@
 | 
				
			|||||||
 | 
					"use strict";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var cl = global.botanLoader;
 | 
					var cl = global.botanLoader;
 | 
				
			||||||
var Dragonfly = global.Dragonfly;
 | 
					var Dragonfly = global.Dragonfly;
 | 
				
			||||||
var CondStream = cl.load( "botanss.utils.CondStream" );
 | 
					var CondStream = cl.load( "botanss.utils.CondStream" );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var FatalError = cl.load( "botanss.errors.FatalError" );
 | 
					var FatalError = cl.load( "botanss.errors.FatalError" );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var Framework = function( garden )
 | 
					class WebFrame
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	var _self = this;
 | 
						constructor( Http )
 | 
				
			||||||
 | 
					 | 
				
			||||||
	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 )
 | 
					 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		_self.requestStr = new CondStream( "/tmp/", 2048 );
 | 
							var _self = this;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		this.HTTP.request.raw.addListener(
 | 
							this.HTTP = Http;
 | 
				
			||||||
			"data" , ( x ) => _self.requestStr.write( x )
 | 
							this.result = 0;
 | 
				
			||||||
		);
 | 
							this.planted = false;
 | 
				
			||||||
 | 
							this.requestStr = "";
 | 
				
			||||||
 | 
							this.requestObj = {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		this.HTTP.request.raw.addListener(
 | 
							var Router = cl.load( "botanss.net.Router" );
 | 
				
			||||||
			"end", () => _self.requestStr.end( () => _self.parseResult() )
 | 
					 | 
				
			||||||
		);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		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;
 | 
							this.router = router;
 | 
				
			||||||
	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" )
 | 
							var res = this.HTTP.response;
 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		_self.queryStr = url.split( "?" )[1];
 | 
					 | 
				
			||||||
		_self.parseResult();
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
Framework.prototype.addHandler = function( name, method )
 | 
							this.handlers = {
 | 
				
			||||||
{
 | 
								"403": function()
 | 
				
			||||||
	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[ method ]( this.router.routeObj );
 | 
									res.statusCode = 403;
 | 
				
			||||||
				return;
 | 
									_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.requestStr = new CondStream( "/tmp/", 2048 );
 | 
				
			||||||
			this.router.routeObj.reRoute( "404", true );
 | 
					
 | 
				
			||||||
			return;
 | 
								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();
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
};
 | 
						addHandler( name, method )
 | 
				
			||||||
 | 
					 | 
				
			||||||
Framework.prototype.plantResult = function()
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	if( this.planted ) return;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	this.planted = true;
 | 
					 | 
				
			||||||
	if( this.HTTP )
 | 
					 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		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;
 | 
							// Release resources
 | 
				
			||||||
		this.HTTP.response.write( this.result );
 | 
							if( this.requestStr )
 | 
				
			||||||
		this.HTTP.response.end();
 | 
							{
 | 
				
			||||||
		Dragonfly.Debug( "Result Planted" );
 | 
								this.requestStr.discard();
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Release resources
 | 
						// This won't handle path exists
 | 
				
			||||||
	if( this.requestStr )
 | 
						// 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
 | 
						__parse()
 | 
				
			||||||
// 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" );
 | 
							if( this.router.routable )
 | 
				
			||||||
		name = p.basename( path );
 | 
							{
 | 
				
			||||||
 | 
								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 + "\"";
 | 
					module.exports = WebFrame;
 | 
				
			||||||
 | 
					 | 
				
			||||||
	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;
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,43 +1,43 @@
 | 
				
			|||||||
 | 
					"use strict";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var cl = global.botanLoader;
 | 
					var cl = global.botanLoader;
 | 
				
			||||||
 | 
					 | 
				
			||||||
var util = require( "util" );
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var WebParam = cl.load( "botanss.utils.WebParam" );
 | 
					var WebParam = cl.load( "botanss.utils.WebParam" );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var Cookie = function( cookieStr, HTTP )
 | 
					class Cookie extends WebParam
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	WebParam.call( this, cookieStr );
 | 
						constructor( cookieStr, HTTP )
 | 
				
			||||||
	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 )
 | 
					 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		switch( i.toLowerCase() )
 | 
							super( cookieStr );
 | 
				
			||||||
		{
 | 
							this.HTTP = HTTP;
 | 
				
			||||||
			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;
 | 
						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;
 | 
					module.exports = Cookie;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,35 +1,41 @@
 | 
				
			|||||||
var WebParam = function( paramStr )
 | 
					"use strict";
 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	var list = {};
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	paramStr && paramStr.split( ";" ).forEach( function( param )
 | 
					class WebParam
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						constructor( paramStr )
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		var parts = param.split( "=" );
 | 
							var list = {};
 | 
				
			||||||
		list[ parts.shift().trim() ] = decodeURI( parts.join( "=" ) );
 | 
					 | 
				
			||||||
	} );
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	this.param = list;
 | 
							paramStr && paramStr.split( ";" ).forEach( function( param )
 | 
				
			||||||
};
 | 
							{
 | 
				
			||||||
 | 
								var parts = param.split( "=" );
 | 
				
			||||||
 | 
								list[ parts.shift().trim() ] = decodeURI( parts.join( "=" ) );
 | 
				
			||||||
 | 
							} );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
WebParam.prototype.set = function( name, value )
 | 
							this.param = list;
 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	this.param[ name ] = value;
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
WebParam.prototype.get = function( name )
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	return this.param[ name ];
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
WebParam.prototype.toString = function()
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	var paramStr = "";
 | 
					 | 
				
			||||||
	for( var i in param )
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		paramStr += i + "=" + param[i] + ";";
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return paramStr;
 | 
					
 | 
				
			||||||
};
 | 
						set( name, value )
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							this.param[ name ] = value;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						get( name )
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							return this.param[ name ];
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						toString()
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							var paramStr = "";
 | 
				
			||||||
 | 
							for( var i in param )
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								paramStr += i + "=" + param[i] + ";";
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return paramStr;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
WebParam.ExtractHeader = function( hstr )
 | 
					WebParam.ExtractHeader = function( hstr )
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user