Ejemplo n.º 1
0
        public async Task InvokeAsync(PipelineContext context, PipelineDelegate next)
        {
            await next.Invoke();

            if (context.GetFirst(out HttpResponseMessage httpResponseMessage))
            {
                var code = (int)httpResponseMessage.StatusCode;
                if ((Sanitize500s && Between(code, 500, 599)) ||
                    (Sanitize400s && Between(code, 400, 499)) ||
                    (Sanitize300s && Between(code, 300, 399)) ||
                    SanitizeResponseCodes.Contains(code))
                {
                    if (SaveOriginalContent)
                    {
                        var originalResponse = new ResponseSanitizerContent();
                        originalResponse.Copy(httpResponseMessage);
                        context.Add(originalResponse);
                    }

                    HttpResponseBuilder.HttpResponseMessage(httpResponseMessage)
                    .WithNoContent()
                    .WithReasonPhrase(string.Empty)
                    .If(ReturnErrorReference,
                        resp => resp.WithStringContent($"{{\"errorReference\":\"{context.Id}\"}}"))
                    .If(ReplacementResponseCode.HasValue,
                        resp => resp.WithStatusCode(ReplacementResponseCode.Value));
                }
            }
        }
Ejemplo n.º 2
0
        public async Task InvokeAsync(PipelineContext context, PipelineDelegate next)
        {
            if (context.GetFirst(out HttpRequestMessage httpRequestMessage))
            {
                var redirectUri = FindMatchingRedirect(httpRequestMessage.RequestUri);
                if (redirectUri != null)
                {
                    var redirected = new Redirected(
                        httpRequestMessage, redirectUri);
                    context.Add(redirected);

                    httpRequestMessage.RequestUri = redirectUri;
                }
                else
                {
                    if (Return404IfNotFound)
                    {
                        var response = HttpResponseBuilder
                                       .HttpResponseMessage()
                                       .WithStatusCode(404)
                                       .WithContent(new StringContent(
                                                        "{\"RouteRedirect\": \"No matching route\"}",
                                                        Encoding.ASCII,
                                                        "appliation/json"))
                                       .Build();
                        context.Add(response);

                        return;
                    }
                }

                await next.Invoke();
            }
            else
            {
                throw new PipelineDependencyException <HttpRequestMessage>(this);
            }
        }
Ejemplo n.º 3
0
        public async Task InvokeAsync(PipelineContext context, PipelineDelegate next)
        {
            if (context.GetFirst(out HttpRequestMessage httpRequestMessage))
            {
                if (RefusedMethods.Any(x => x == httpRequestMessage.Method))
                {
                    var response = HttpResponseBuilder
                                   .HttpResponseMessage()
                                   .WithReasonPhrase("Method refused")
                                   .WithStatusCode(403);

                    context.Add(response);
                }
                else
                {
                    await next.Invoke();
                }
            }
            else
            {
                throw new PipelineDependencyException <HttpRequestMessage>(this);
            }
        }
Ejemplo n.º 4
0
        public async Task InvokeAsync(PipelineContext context, PipelineDelegate next)
        {
            if (context.GetFirst(out HttpRequestMessage httpRequestMessage))
            {
                if (httpRequestMessage.Content?.Headers.ContentLength > MaxRequestSize)
                {
                    var response = HttpResponseBuilder
                                   .HttpResponseMessage()
                                   .WithReasonPhrase("Content too large")
                                   .WithStatusCode(500);

                    context.Add(response);
                }
                else
                {
                    await next.Invoke();
                }
            }
            else
            {
                throw new PipelineDependencyException <HttpRequestMessage>(this);
            }
        }