Minor bug fixes. Added 302 redirect

This commit is contained in:
斟酌 鵬兄 2015-04-15 18:38:44 +08:00
parent 1b361227e7
commit 21dfcaca0e
3 changed files with 57 additions and 20 deletions

View File

@ -1,11 +1,20 @@
var encodeCookie = function( cookie )
{
var cookieStr = "";
var p = "";
for( var i in cookie )
{
if( i.toLowerCase() == "path" )
{
p = cookie[i];
continue;
}
cookieStr += i + "=" + encodeURI( cookie[i] ) + ";";
}
// Path at tail
cookieStr += "path=" + p + ";";
return cookieStr;
};

View File

@ -19,6 +19,7 @@ var RelayPoint = function( uri, internal )
);
this.value = internal ? uri : stripURI( uri );
this.params = Array.prototype.slice.call( arguments, 2 );
};
var Router = function( http )
@ -63,6 +64,7 @@ Router.prototype.route = function()
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 ] )
@ -74,6 +76,7 @@ Router.prototype.route = function()
}
else
{
// Parse RegEx rules one by one
for( var i in this.routes )
{
r = this.routes[i];
@ -99,11 +102,12 @@ Router.prototype.route = function()
// Set Route
Router.prototype.setRoute = function( _route, _match )
{
var _self = this;
this.routeObj = {
route: _route
, match: _match
, router: this.router
, inputs: this.inputs
, router: _self.router
, inputs: _self.inputs
// Re-route function
, reRoute: function( target, direct )
@ -112,11 +116,19 @@ Router.prototype.setRoute = function( _route, _match )
"Reroute: " + target
+ ( direct ? ", Direct" : "" )
);
this.relaying = new RelayPoint( target, direct );
this.routable = true;
this.reRoute = true;
this.emit( "Route" );
}.bind( this )
_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" );
}
};
};

View File

@ -3,6 +3,8 @@ var FatalError = require( '../errors/FatalError.js' );
var Framework = function( garden )
{
var _self = this;
this.HTTP = garden;
this.result = 0;
this.planted = false;
@ -10,24 +12,38 @@ var Framework = function( garden )
this.requestObj = {};
var Router = require( "./Router" );
this.router = new Router( garden );
this.router.addRoute( "404", false, "404" );
this.router.addRoute( "500", false, "500" );
this.router.addListener( "Route", this.parseResult.bind( this ) );
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;
this.handlers = {
"404": function()
{
this.HTTP.response.statusCode = 404;
this.result = "404 Not Found";
this.plantResult();
}.bind( this )
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()
{
this.HTTP.response.statusCode = 500;
this.result = "500 Internal Server Error";
this.plantResult();
}.bind( this )
res.statusCode = 500;
_self.result = "500 Internal Server Error";
_self.plantResult();
}
}
};
@ -46,7 +62,7 @@ Framework.prototype.run = function()
{
_self.parseResult();
});
method = "POST";
}