public override async Task <HttpResponse> Invoke(HttpRequest request)
        {
            var ext = GetExtension(request.Url);

            if (ext == string.Empty)
            {
                return(await Next?.Invoke(request));
            }

            if (!EnableFileTypes.Keys.Any(x => x.ToLower().Equals(ext)))
            {
                return(await Next?.Invoke(request));
            }

            var fileinfo = fileProvider.GetFileInfo(request.Url);

            if (!fileinfo.Exists)
            {
                return(await HttpResponseHelper.CreateNotFoundResponseAsync());
            }

            using (var stream = fileinfo.CreateReadStream())
            {
                var res = new HttpResponse
                {
                    ContentType = EnableFileTypes[ext],
                };
                await res.WriteBodyAsync(stream);

                res.Headers.Add("Cache-Control", DefaultCacheStrategy);
                return(res);
            }
        }
Beispiel #2
0
        public override async Task <HttpResponse> Invoke(HttpRequest httpRequest)
        {
            try
            {
                var context = new ActionExecuteContext
                {
                    HttpRequest = httpRequest
                };

                var(controller, methodInfo, parameter) = route.Route(httpRequest);
                if (controller == null)
                {
                    return(await HttpResponseHelper.CreateNotFoundResponseAsync());
                }

                context.Controller = controller;
                context.Action     = methodInfo;
                ((ControllerBase)controller).Request = httpRequest;

                var filterList = GetFilters(controller, methodInfo);
                var stack      = new Stack <IFilter>();
                for (var i = 0; i < filterList.Count; i++)
                {
                    var filter = filterList[i];
                    await filter.OnActionExecutingAsync(context);

                    if (context.Final)
                    {
                        return(context.HttpResponse);
                    }
                    stack.Push(filter);
                }

                await controller.OnActionExecutingAsync(context);

                if (context.Final)
                {
                    return(context.HttpResponse);
                }

                var parameters = new List <object>();
                if (parameter != null)
                {
                    parameters.Add(parameter);
                }

                if (methodInfo.ReturnType.IsGenericType) //Task<IActionResult>
                {
                    var actionResult = await(methodInfo.Invoke(controller, parameters.ToArray()) as Task <IActionResult>);
                    context.HttpResponse = await actionResult.ExecuteResultAsync();
                }
                else
                {
                    var actionResult = methodInfo.Invoke(controller, parameters.ToArray()) as IActionResult;
                    context.HttpResponse = await actionResult.ExecuteResultAsync();
                }

                context.HttpResponse.Cookies.AddRange(controller.ResponseCookie);

                await controller.OnActionExecutedAsync(context);

                if (context.Final)
                {
                    return(context.HttpResponse);
                }

                while (stack.Count != 0)
                {
                    var filter = stack.Pop();
                    await filter.OnActionExecutedAsync(context);

                    if (context.Final)
                    {
                        return(context.HttpResponse);
                    }
                }
                return(context.HttpResponse);
            }
            catch (Exception e)
            {
                return(await HttpResponseHelper.CreateDefaultErrorResponseAsync(e));
            }
        }