Ejemplo n.º 1
0
        public static PeakyRouter AllowVerbs(this PeakyRouter router, params string[] verbs)
        {
            if (router == null)
            {
                throw new ArgumentNullException(nameof(router));
            }

            var allowedVerbs = new HashSet <string>(verbs, StringComparer.OrdinalIgnoreCase);

            return(new AnonymousRouter(
                       routeAsync: async context =>
            {
                if (!allowedVerbs.Contains(context.HttpContext.Request.Method))
                {
                    await Task.Yield();
                    context.Handler = async httpContext =>
                    {
                        await Task.Yield();
                        httpContext.Response.StatusCode = 405;
                    };
                }

                await router.RouteAsync(context);
            },
                       pathBase: router.PathBase));
        }
Ejemplo n.º 2
0
        public static PeakyRouter Accept(
            this PeakyRouter router,
            params string[] mediaTypes)
        {
            if (router == null)
            {
                throw new ArgumentNullException(nameof(router));
            }

            var acceptedMediaTypes = mediaTypes.ToMediaTypeCollection();

            return(new AnonymousRouter(
                       routeAsync: async context =>
            {
                var acceptHeader = context.HttpContext.Request.Headers[HeaderNames.Accept];

                if (acceptHeader.IsSubsetOfAny(acceptedMediaTypes))
                {
                    await router.RouteAsync(context);
                }
            },
                       pathBase: router.PathBase));
        }