Exemplo n.º 1
0
 public override Task OnActivateAsync()
 {
     this._id       = HttpId.Parse(this.GetPrimaryKeyString());
     this._logger   = this._serviceProvider.GetRequiredService <ILoggerFactory>().CreateLogger(this._id.FunctionType);
     this._function = this._serviceProvider.GetRequiredServiceByName <IHttpFunction>(this._id.FunctionType);
     return(Task.CompletedTask);
 }
Exemplo n.º 2
0
        public static HttpMethodAttribute[] GetHttpMethodAttributes(this IHttpFunction function)
        {
            var methodInfo     = function.GetType().GetMethod("HandleAsync");
            var httpAttributes = methodInfo.GetCustomAttributes(typeof(HttpMethodAttribute), false);

            return(httpAttributes.Cast <HttpMethodAttribute>().ToArray());
        }
        public async Task InvokeAsync(HttpContext context, IHttpFunction function)
        {
            // execute function
            var result = await function.HandleAsync(context.Request);

            // write result
            var actionContext = new ActionContext(context, context.GetRouteData(), new ActionDescriptor());

            await result.ExecuteResultAsync(actionContext);
        }
 public HttpRequestHandler(ILoggerFactory loggerFactory
                           , IHttpFunction httpFunction
                           , IOptions <HttpFunctionOptions> optionsAccessor
                           , IRouteMatcher internalRouteMatcher
                           )
 {
     log          = loggerFactory.CreateLogger <HttpRequestHandler>();
     function     = httpFunction;
     options      = optionsAccessor.Value;
     routeMatcher = internalRouteMatcher;
 }
        public Task InvokeAsync(HttpContext context, IHttpFunction function)
        {
            if (!options.SkipAuth)
            {
                // enforce authorization when required
                var authAttributes = function.GetAuthorizeAttributes();

                if (function.GetAuthorizeAttributes().Any() && !context.User.Identity.IsAuthenticated)
                {
                    context.Response.StatusCode = 401;

                    return(context.Response.WriteAsync("Unauthorized"));
                }
            }

            return(next(context));
        }
        public Task InvokeAsync(HttpContext context, IHttpFunction function)
        {
            var httpAttributes = function.GetHttpMethodAttributes();

            if (httpAttributes.Any())
            {
                var httpAttribute = httpAttributes.GetMethod(context.Request.Method);

                if (httpAttribute == null)
                {
                    // method not allowed
                    context.Response.StatusCode = 405;

                    return(context.Response.WriteAsync("Method Not Allowed"));
                }

                if (!string.IsNullOrEmpty(httpAttribute.Template))
                {
                    // match route template
                    if (!httpAttribute.MatchRouteTemplate(context, routeMatcher))
                    {
                        // path doesn't match route template
                        return(WriteNotFoundAsync(context));
                    }
                }
                else if (!options.IgnoreRoutingRules && (context.Request.Path != "/"))
                {
                    // attribute template is null. reject custom path unless allowed by options
                    return(WriteNotFoundAsync(context));
                }
            }
            else if (!options.IgnoreRoutingRules && (context.Request.Path != "/"))
            {
                // if there are no http modifiers
                // we reject a custom path unless overriden by options
                return(WriteNotFoundAsync(context));
            }

            return(next(context));
        }
Exemplo n.º 7
0
        public static AuthorizeAttribute[] GetAuthorizeAttributes(this IHttpFunction function)
        {
            var authorizeAttributes = function.GetType().GetCustomAttributes(typeof(AuthorizeAttribute), false);

            return(authorizeAttributes.Cast <AuthorizeAttribute>().ToArray());
        }