Ejemplo n.º 1
0
        public static FluentHost Handles(this FluentHost host, string route, Verb verb, Func <HttpListenerContext, IHandlerResponse> handler, CorsConfig corsConfig = null)
        {
            host.Handlers.Add(new RouteHandler(route, verb, handler, corsConfig));

            if (corsConfig == null)
            {
                return(host);
            }

            // TODO: Tidy this up.
            var preflightHandler = new RouteHandler(route,
                                                    Verb.Options,
                                                    context =>
            {
                var origin = context.Request.Headers["Origin"].ToLowerInvariant();
                var allow  = corsConfig.Origins.Any(p => p.ToLowerInvariant() == origin) || corsConfig.Origins.Any(p => p == "*");
                return(new HandlerResponse
                {
                    Code = allow ? 200 : 400,
                    ContentLength = 0,
                    ContentType = "text/plain", Encoding = Encoding.UTF8,
                    Stream = new MemoryStream(new byte[] { })
                });
            }, corsConfig);

            host.Handlers.Add(preflightHandler);

            return(host);
        }
Ejemplo n.º 2
0
 public static FluentHost AddHandler(this FluentHost host, IRouteHandler handler)
 {
     host.Handlers.Add(handler);
     return(host);
 }