public override async Task ProcessRequestAsync(HttpContext context) { context.ThrowIfNull("context"); var request = new HttpRequestWrapper(context.Request); var response = new HttpResponseWrapper(context.Response); if (_antiCsrfCookieManager != null && _antiCsrfNonceValidator != null && _antiCsrfResponseGenerator != null) { if (!String.IsNullOrEmpty(context.Request.ContentType)) { try { var contentType = new ContentType(context.Request.ContentType); if (String.Equals(contentType.MediaType, "application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase) || String.Equals(contentType.MediaType, "multipart/form-data", StringComparison.OrdinalIgnoreCase)) { ValidationResult validationResult = await _antiCsrfNonceValidator.ValidateAsync(request); ResponseResult responseResult = await _antiCsrfResponseGenerator.GetResponseAsync(validationResult); if (responseResult.ResultType == ResponseResultType.ResponseGenerated) { await ProcessResponseAsync(context, responseResult.Response, null); return; } } } catch (FormatException) { } } await _antiCsrfCookieManager.ConfigureCookieAsync(request, response); } { IEnumerable<RouteMatchResult> routeMatchResults = await GetRouteMatchResultsAsync(request); IEnumerable<Task<ResponseGenerators.ResponseResult>> responseResultTasks = _responseGenerators.Select(arg => arg.GetResponseAsync(new HttpContextWrapper(context), routeMatchResults)); foreach (Task<ResponseGenerators.ResponseResult> responseResultTask in responseResultTasks) { ResponseGenerators.ResponseResult responseResult = await responseResultTask; if (responseResult.ResultType == ResponseGenerators.ResponseResultType.ResponseGenerated) { await ProcessResponseAsync(context, await responseResult.Response, responseResult.CacheKey); return; } } } }
public void ProcessRequest(HttpContext context) { context.ThrowIfNull("context"); var request = new HttpRequestWrapper(context.Request); var response = new HttpResponseWrapper(context.Response); // ReSharper disable ImplicitlyCapturedClosure RouteMatchResult[] routeMatchResults = _routes.Select(arg => new RouteMatchResult(arg, arg.MatchesRequest(request))).ToArray(); // ReSharper restore ImplicitlyCapturedClosure ResponseResult responseResult = _responseGenerators .Select(arg => arg.GetResponse(request, routeMatchResults)) .FirstOrDefault(arg => arg.ResultType != ResponseResultType.ResponseNotGenerated); if (responseResult == null) { throw new ApplicationException("No response was generated."); } ProcessResponse(request, response, responseResult.Response, responseResult.CacheKey); }
public override async Task ProcessRequestAsync(HttpContext context) { context.ThrowIfNull("context"); var contextWrapper = new HttpContextWrapper(context); var requestWrapper = new HttpRequestWrapper(context.Request); var responseWrapper = new HttpResponseWrapper(context.Response); ExceptionDispatchInfo exceptionDispatchInfo = null; try { foreach (IRequestValidator requestValidator in _requestValidators) { ValidateResult validateResult = await requestValidator.Validate(requestWrapper, responseWrapper); if (validateResult.ResultType == ValidateResultType.RequestValidated) { continue; } await ProcessResponseAsync(context, validateResult.Response, null); return; } IEnumerable<RouteMatchResult> routeMatchResults = _routes.Select(arg => new RouteMatchResult(arg, arg.MatchesRequest(requestWrapper))); IEnumerable<Task<ResponseResult>> responseResultTasks = _responseGenerators.Select(arg => arg.GetResponseAsync(contextWrapper, routeMatchResults)); foreach (Task<ResponseResult> responseResultTask in responseResultTasks) { ResponseResult responseResult = await responseResultTask; if (responseResult.ResultType != ResponseResultType.ResponseGenerated) { continue; } await ProcessResponseAsync(context, await responseResult.Response, responseResult.CacheKey); return; } } catch (Exception exception) { exceptionDispatchInfo = ExceptionDispatchInfo.Capture(exception); } if (exceptionDispatchInfo != null) { foreach (IErrorHandler errorHandler in _errorHandlers) { if ((await errorHandler.HandleAsync(contextWrapper, exceptionDispatchInfo.SourceException)).ResultType != HandleResultType.Handled) { continue; } exceptionDispatchInfo = null; break; } } if (exceptionDispatchInfo != null) { exceptionDispatchInfo.Throw(); } }