public virtual SillyProxyResponse Handle(SillyProxyRequest input, ILambdaContext lambdaContext) { try { if (input == null) { throw new SillyException(SillyHttpStatusCode.ServerError, "Request aborted upon delivery."); } OriginalRequest = input; HttpMethod = StringToHttpMethod(input.httpMethod); Path = input.path; ISillyView sillyContent = Dispatch(this); if (sillyContent == null) { throw new SillyException(SillyHttpStatusCode.NotFound, "The path " + input.path + " does not exist."); } SillyProxyResponse response = new SillyProxyResponse(); response.body = sillyContent.Content; return(response); } catch (SillyException sillyEx) { return(buildErrorResponse(sillyEx)); } catch (Exception Ex) { return(buildErrorResponse(SillyHttpStatusCode.ServerError, Ex.Message + "\n" + Ex.StackTrace)); } }
public ISillyView Dispatch(ISillyContext context) { if (context == null || context.HttpMethod == SupportedHttpMethods.Unsupported) { return(null); } string path = context.Path; if (String.IsNullOrEmpty(path) || String.IsNullOrWhiteSpace(path)) { return(null); } List <SillyRoute> availableRoutes = Routes[context.HttpMethod]; if (availableRoutes.Count == 0) { return(null); } string[] pathSegments = path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); foreach (SillyRoute route in availableRoutes) { if (route.Count == 0 || !route.IsValid || pathSegments.Length > route.Count) { continue; } RouteMatchingVisitor visitor = new RouteMatchingVisitor(Controllers); if (visitor.TryMatch(route, pathSegments)) { if (visitor.Controller == null || visitor.Method == null) { continue; } int varCount = 1 + visitor.Vars.Count; object[] vars = new object[varCount]; vars[0] = context; if (visitor.Vars.Count > 0) { int index = 1; foreach (object var in visitor.Vars) { vars[index] = var; } } try { ISillyView view = visitor.Method.Invoke(visitor.Controller, vars) as ISillyView; return(view); } catch (Exception ex) { throw ex.InnerException; } } } return(null); }
public void DispatchTests() { DispatchTester dispatcher = new DispatchTester(); List <string> routes = new List <string>() { "/", "/about", "/content", "/admin", "/admin/dashboard", "/admin/users/123/sexdolle", "/admin/d***o/sex/pistol", "/admin/d***o", "/admin/users/456" }; List <string> invalidRoutes = new List <string>() { "/poop", "/123/annihilation", "/admin/users/123/sexdolle/inyourmouth", "/admin/uppers" }; foreach (string route in routes) { Console.Write("Dispatch Route " + route + " -> "); try { dispatcher.Path = route; dispatcher.HttpMethod = SupportedHttpMethods.Get; ISillyView content = dispatcher.Dispatch(dispatcher); Console.WriteLine((content == null) ? "null" : content.Content); Assert.NotEqual(content, null); } catch (Exception ex) { Console.WriteLine(ex.Message); Assert.True(false); } } foreach (string invalid in invalidRoutes) { Console.Write("Dispatch Invalid Route " + invalid + " -> "); try { dispatcher.Path = invalid; dispatcher.HttpMethod = SupportedHttpMethods.Get; ISillyView content = dispatcher.Dispatch(dispatcher); Console.WriteLine((content == null) ? "null" : content.Content); Assert.Equal(content, null); } catch (Exception ex) { Console.WriteLine(ex.Message); Assert.True(false); } } }