public void TestRouteTableSimple() { var t = new RouteTable(); t.AddRoute("/user/{id}", "ID"); t.AddRoute("/user/{id}/action", "ACTION"); t.AddRoute("/user/{id}/action/{level}", "LEVEL"); t.AddRoute("", "DEFAULT"); var rdefault = t.Find("/not/a/route"); Assert.AreNotEqual(null, rdefault, "Match a default route"); var rid = t.Find("/user/person"); var raction = t.Find("/user/person/action"); var rlevel = t.Find("/user/person/action/verbose"); Assert.AreNotEqual(null, rid, "Match the id route"); Assert.AreNotEqual(null, raction, "Match the action route"); Assert.AreNotEqual(null, rlevel, "Match the level route"); Assert.AreEqual("ID", rid.Route.Data, $"Id route is id ({rid.Route.Data})"); Assert.AreEqual("ACTION", raction.Route.Data, $"Action route is id ({raction.Route.Data})"); Assert.AreEqual("LEVEL", rlevel.Route.Data, $"Level route is id ({rlevel.Route.Data})"); Assert.AreEqual("person", rid.Values["id"], $"Id route id is person"); Assert.AreEqual(1, raction.Values.Count, $"action matched 1"); Assert.AreEqual("person", rlevel.Values["id"], $"Level id is person"); Assert.AreEqual("verbose", rlevel.Values["level"], $"Level level is verbose"); Assert.AreEqual(2, rlevel.Values.Count, $"action route matched 2"); }
internal static RouteTable BuildRouteTable(RouterMetadata metadata, Blueprint map) { logger.Debug($"Building routing table for {metadata.Name}."); var table = new RouteTable(); var directlyConnectedSites = map.Sites.Where(s => s.Routers.Any(r => r.Name == metadata.Name)).ToList(); var endpointCardinality = map.Sites.SelectMany(s => s.Endpoints, (site, endpoint) => new { site, endpoint }).GroupBy(x => x.endpoint).ToDictionary(g => g.Key, g => g.Count() > 1); foreach (var iface in metadata.Interfaces) { var site = FindSiteConnectedToInterface(map, metadata.Name, iface); foreach (var endpoint in site.Endpoints) { if (endpointCardinality[endpoint]) { table.AddRoute((i, d) => i != iface && d.Endpoint == endpoint && d.Site == site.Name, $"Interface <> {iface} AND Endpoint = {endpoint} AND Site = {site.Name}", null, iface); } else { table.AddRoute((i, d) => i != iface && d.Endpoint == endpoint, $"Interface <> {iface} AND Endpoint = {endpoint}", null, iface); } } var nearbyGateways = site.Routers.Where(r => r.Name != metadata.Name); foreach (var gateway in nearbyGateways) { var destinations = FindDestinations(gateway, metadata, map, new List <Site>(directlyConnectedSites)).ToArray(); foreach (var destination in destinations) { if (endpointCardinality[destination.Endpoint]) { table.AddRoute((i, d) => i != gateway.Interface && d.Endpoint == destination.Endpoint && d.Site == destination.Site, $"Interface <> {gateway.Interface} AND Endpoint = {destination.Endpoint} AND Site = {destination.Site}", gateway.Name, iface); } else { table.AddRoute((i, d) => i != gateway.Interface && d.Endpoint == destination.Endpoint, $"Interface <> {gateway.Interface} AND Endpoint = {destination.Endpoint}", gateway.Name, iface); } } } } return(table); }
public static RouteTable InitializeRouteTable() { RouteTable routeTable = new RouteTable(); routeTable.AddRoute("get", "restricted", new RouteEntry() { RouteProvider = (continuation, context) => { throw new ApplicationException("You can't do that."); } }); return(routeTable); }
/// <summary> /// Add a route with session expiration checking. /// </summary> public static void AddExpirableRoute(this RouteTable routeTable, string verb, string path, Func <WorkflowContinuation <ContextWrapper>, ContextWrapper, Session, PathParams, WorkflowState> routeHandler) { routeTable.AddRoute(verb, path, new RouteEntry() { SessionExpirationHandler = (continuation, context, session, parms) => { /* Your expiration check */ return(WorkflowState.Continue); }, RouteHandler = routeHandler, }); }
public static RouteTable InitializeRouteTable() { RouteTable routeTable = new RouteTable(websitePath); // Test parameterized URL routeTable.AddRoute("get", "param/{p1}/subpage/{p2}", new RouteEntry() { RouteHandler = (continuation, wrapper, session, parms) => { wrapper.SetPendingResponse("<p>p1 = " + parms["p1"] + "</p><p>p2 = " + parms["p2"] + "</p>"); return(WorkflowState.Continue); } }); // Example usage where we re-use the expirable session and authorization checks. // routeTable.AddExpirableRoute("get", "somepath", myRouteHandler); // routeTable.AddExpirableAuthorizedRoute("get", "someotherpath", myRouteHandler); // Test session expired and authorization flags routeTable.AddRoute("get", "testsession", new RouteEntry() { SessionExpirationHandler = (continuation, wrapper, session, parms) => { if (session.Expired) { // Redirect instead of throwing an exception. wrapper.Context.Redirect(@"ErrorPages\expiredSession"); return(WorkflowState.Abort); } else { return(WorkflowState.Continue); } }, AuthorizationHandler = (continuation, wrapper, session, parms) => { if (!session.Authorized) { // Redirect instead of throwing an exception. wrapper.Context.Redirect(@"ErrorPages\notAuthorized"); return(WorkflowState.Abort); } else { return(WorkflowState.Continue); } }, RouteHandler = (continuation, wrapper, session, parms) => { wrapper.SetPendingResponse("<p>Looking good!</p>"); return(WorkflowState.Continue); } }); // Set the session expired and authorization flags for testing purposes. routeTable.AddRoute("get", "SetState", new RouteEntry() { RouteHandler = (continuation, wrapper, session, pathParams) => { Dictionary <string, string> parms = wrapper.Context.GetUrlParameters(); session.Expired = GetBooleanState(parms, "Expired", false); session.Authorized = GetBooleanState(parms, "Authorized", false); wrapper.SetPendingResponse( "<p>Expired has been set to " + session.Expired + "</p>" + "<p>Authorized has been set to " + session.Authorized + "</p>"); return(WorkflowState.Continue); } }); // Test a form post routeTable.AddRoute("post", "login", "application/x-www-form-urlencoded", new RouteEntry() { RouteHandler = (continuation, wrapper, session, pathParams) => { string data = new StreamReader(wrapper.Context.Request.InputStream, wrapper.Context.Request.ContentEncoding).ReadToEnd(); wrapper.Context.Redirect("Welcome"); // As a redirect, we don't want any downstream processing. return(WorkflowState.Done); } }); // Test a form post with JSON content routeTable.AddRoute("post", "login", "application/json; charset=UTF-8", new RouteEntry() { RouteHandler = (continuation, wrapper, session, pathParams) => { string data = new StreamReader(wrapper.Context.Request.InputStream, wrapper.Context.Request.ContentEncoding).ReadToEnd(); wrapper.Context.RespondWith("Welcome!"); // As an AJAX response, we don't want any downstream processing. return(WorkflowState.Done); } }); routeTable.AddRoute("get", "loadtests", new RouteEntry() { RouteHandler = (continuation, wrapper, session, pathParams) => { long nanosecondsPerTick = (1000L * 1000L * 1000L) / System.Diagnostics.Stopwatch.Frequency; if (Server.Samples == 0) { wrapper.SetPendingResponse("<p>No samples!</p>"); } else { long avgTime = Server.CumulativeTime * nanosecondsPerTick / Server.Samples; string info = String.Format("<p>{0} responses, avg. response time = {1}ns</p><p>Resetting sample info.</p>", Server.Samples, avgTime.ToString("N0")); Server.CumulativeTime = 0; Server.Samples = 0; wrapper.SetPendingResponse(info); } return(WorkflowState.Continue); } }); routeTable.AddRoute("get", "sayhi", new RouteEntry() { RouteHandler = (continuation, wrapper, session, pathParams) => { wrapper.SetPendingResponse("<p>hello</p>"); return(WorkflowState.Continue); } }); return(routeTable); }
/// <summary> /// Adds a generic rule to forward all traffic from <paramref name="incomingInterface"/> to <paramref name="outgoingInterface"/> optionally providing the gateway. /// </summary> public static void AddForwardRoute(this RouteTable routeTable, string incomingInterface, string outgoingInterface, string gateway = null) { routeTable.AddRoute((i, d) => i == incomingInterface, $"Interface = {incomingInterface}", gateway, outgoingInterface); }
public static RouteTable InitializeRouteTable() { RouteTable routeTable = new RouteTable(); routeTable.AddRoute("get", "restricted", new RouteEntry() { RouteProvider = (continuation, context, session) => { throw new ApplicationException("You can't do that."); } }); routeTable.AddRoute("get", "testsession", new RouteEntry() { SessionExpirationProvider = (continuation, context, session) => { if (session.Expired) { throw new ApplicationException("Session has expired!"); } else { return(WorkflowState.Continue); } }, AuthorizationProvider = (continuation, context, session) => { if (!session.Authorized) { throw new ApplicationException("Not authorized!"); } else { return(WorkflowState.Continue); } }, RouteProvider = (continuation, context, session) => { context.RespondWith("<p>Looking good!</p>"); return(WorkflowState.Done); } }); routeTable.AddRoute("get", "SetState", new RouteEntry() { RouteProvider = (continuation, context, session) => { Dictionary <string, string> parms = context.GetUrlParameters(); session.Expired = GetBooleanState(parms, "Expired", false); session.Authorized = GetBooleanState(parms, "Authorized", false); context.RespondWith( "<p>Expired has been set to " + session.Expired + "</p>" + "<p>Authorized has been set to " + session.Authorized + "</p>"); return(WorkflowState.Done); } }); return(routeTable); }