Exemplo n.º 1
0
        /// <summary>Processes the HTTP request routing.</summary>
        /// <param name="context">The context.</param>
        /// <param name="routes">The routes.</param>
        /// <param name="resolver">The resolver.</param>
        /// <param name="defaultRequestConfig">The default request configuration.</param>
        /// <returns></returns>
        internal static async Task <bool> ProcessHttpRequestRouting(this ApiRequestContext context,
                                                                    IApiRoutingTable routes,
                                                                    IUriRouteResolver resolver,
                                                                    IDeepSleepRequestConfiguration defaultRequestConfig)
        {
            if (!context.RequestAborted.IsCancellationRequested)
            {
                if (routes != null && resolver != null)
                {
                    context.Routing.Route = await context.GetRoutingItem(resolver, routes, defaultRequestConfig).ConfigureAwait(false);

                    context.Routing.Template = await context.GetRoutingTemplate(resolver, routes).ConfigureAwait(false);
                }

                context.Configuration = MergeConfigurations(
                    serviceProvider: context?.RequestServices,
                    defaultConfig: defaultRequestConfig,
                    endpointConfig: context.Routing?.Route?.Configuration);

                if (context.Routing.Route?.Location?.MethodInfo != null)
                {
                    var attributes = context.Routing.Route.Location.MethodInfo.GetCustomAttributes();

                    // Find any attribute request pipeline components
                    // and add the to the final configuration of the request.
                    attributes
                    .Where(a => a as IRequestPipelineComponent != null)
                    .Select(a => a as IRequestPipelineComponent)
                    .ToList()
                    .ForEach(p => context.Configuration.PipelineComponents.Add(p));

                    // Find any attribute validator components
                    // and add the to the final configuration of the request.
                    attributes
                    .Where(a => a as IEndpointValidatorComponent != null)
                    .Select(a => a as IEndpointValidatorComponent)
                    .ToList()
                    .ForEach(v => context.Configuration.Validators.Add(v));

                    // Find any authentication components
                    // and add the to the final configuration of the request.
                    var authenticationComponents = attributes
                                                   .Where(a => a as IAuthenticationComponent != null)
                                                   .Select(a => a as IAuthenticationComponent)
                                                   .ToList();

                    if (authenticationComponents.Count > 0)
                    {
                        context.Configuration.AuthenticationProviders = authenticationComponents;
                    }

                    // Find any authorization components
                    // and add the to the final configuration of the request.
                    var authorizationComponents = attributes
                                                  .Where(a => a as IAuthorizationComponent != null)
                                                  .Select(a => a as IAuthorizationComponent)
                                                  .ToList();

                    if (authorizationComponents.Count > 0)
                    {
                        context.Configuration.AuthorizationProviders = authorizationComponents;
                    }
                }


                if (context.Configuration?.RequestValidation?.MaxRequestLength != null && context.ConfigureMaxRequestLength != null)
                {
                    try
                    {
                        context.ConfigureMaxRequestLength(context.Configuration.RequestValidation.MaxRequestLength.Value);
                    }
                    catch { }
                }

                return(true);
            }

            return(false);
        }