Пример #1
0
 /// <summary>
 /// Adds the transform that will append or set the query parameter from a route value.
 /// </summary>
 public static TransformBuilderContext AddQueryRouteValue(this TransformBuilderContext context, string queryKey, string routeValueKey, bool append = true)
 {
     context.RequestTransforms.Add(new QueryParameterRouteTransform(
                                       append ? QueryStringTransformMode.Append : QueryStringTransformMode.Set,
                                       queryKey, routeValueKey));
     return(context);
 }
Пример #2
0
        /// <summary>
        /// Clones the route and adds the transform which will set the request path with the given value.
        /// </summary>
        public static TransformBuilderContext AddPathRouteValues(this TransformBuilderContext context, PathString pattern)
        {
            var binder = context.Services.GetRequiredService <TemplateBinderFactory>();

            context.RequestTransforms.Add(new PathRouteValuesTransform(pattern.Value, binder));
            return(context);
        }
Пример #3
0
        private static void ValidatePathRouteValues(TransformBuilderContext builderContext)
        {
            var requestTransform         = Assert.Single(builderContext.RequestTransforms);
            var pathRouteValuesTransform = Assert.IsType <PathRouteValuesTransform>(requestTransform);

            Assert.Equal("/path#", pathRouteValuesTransform.Template.TemplateText);
        }
Пример #4
0
        private static void ValidatePathRemovePrefix(TransformBuilderContext builderContext)
        {
            var requestTransform    = Assert.Single(builderContext.RequestTransforms);
            var pathStringTransform = Assert.IsType <PathStringTransform>(requestTransform);

            Assert.Equal(PathStringTransform.PathTransformMode.RemovePrefix, pathStringTransform.Mode);
            Assert.Equal("/path#", pathStringTransform.Value.Value);
        }
Пример #5
0
 /// <summary>
 /// Adds the transform which will add the Forwarded header as defined by [RFC 7239](https://tools.ietf.org/html/rfc7239).
 /// </summary>
 public static TransformBuilderContext AddForwarded(this TransformBuilderContext context,
                                                    bool useHost        = true, bool useProto = true, NodeFormat forFormat = NodeFormat.Random,
                                                    NodeFormat byFormat = NodeFormat.Random, bool append = true)
 {
     context.UseDefaultForwarders = false;
     if (byFormat != NodeFormat.None || forFormat != NodeFormat.None || useHost || useProto)
     {
         var random = context.Services.GetRequiredService <IRandomFactory>();
         context.RequestTransforms.Add(new RequestHeaderForwardedTransform(random,
                                                                           forFormat, byFormat, useHost, useProto, append));
     }
     return(context);
 }
Пример #6
0
        /// <summary>
        /// Adds a transform Func that runs on each response for the given route.
        /// </summary>
        public static TransformBuilderContext AddResponseTrailersTransform(this TransformBuilderContext context, Func <ResponseTrailersTransformContext, ValueTask> func)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (func is null)
            {
                throw new ArgumentNullException(nameof(func));
            }

            context.ResponseTrailersTransforms.Add(new ResponseTrailersFuncTransform(func));
            return(context);
        }
Пример #7
0
        private static void ValidateXForwarded(TransformBuilderContext builderContext, bool useFor, bool useHost, bool useProto, bool usePathBase, bool append)
        {
            Assert.False(builderContext.UseDefaultForwarders);

            if (useFor)
            {
                var requestHeaderXForwardedForTransform = Assert.Single(builderContext.RequestTransforms.OfType <RequestHeaderXForwardedForTransform>());
                Assert.Equal("prefix-For", requestHeaderXForwardedForTransform.HeaderName);
                Assert.Equal(append, requestHeaderXForwardedForTransform.Append);
            }
            else
            {
                Assert.Empty(builderContext.RequestTransforms.OfType <RequestHeaderXForwardedForTransform>());
            }

            if (useHost)
            {
                var requestHeaderXForwardedHostTransform = Assert.Single(builderContext.RequestTransforms.OfType <RequestHeaderXForwardedHostTransform>());
                Assert.Equal("prefix-Host", requestHeaderXForwardedHostTransform.HeaderName);
                Assert.Equal(append, requestHeaderXForwardedHostTransform.Append);
            }
            else
            {
                Assert.Empty(builderContext.RequestTransforms.OfType <RequestHeaderXForwardedHostTransform>());
            }

            if (useProto)
            {
                var requestHeaderXForwardedProtoTransform = Assert.Single(builderContext.RequestTransforms.OfType <RequestHeaderXForwardedProtoTransform>());
                Assert.Equal("prefix-Proto", requestHeaderXForwardedProtoTransform.HeaderName);
                Assert.Equal(append, requestHeaderXForwardedProtoTransform.Append);
            }
            else
            {
                Assert.Empty(builderContext.RequestTransforms.OfType <RequestHeaderXForwardedProtoTransform>());
            }

            if (usePathBase)
            {
                var requestHeaderXForwardedPathBaseTransform = Assert.Single(builderContext.RequestTransforms.OfType <RequestHeaderXForwardedPathBaseTransform>());
                Assert.Equal("prefix-PathBase", requestHeaderXForwardedPathBaseTransform.HeaderName);
                Assert.Equal(append, requestHeaderXForwardedPathBaseTransform.Append);
            }
            else
            {
                Assert.Empty(builderContext.RequestTransforms.OfType <RequestHeaderXForwardedPathBaseTransform>());
            }
        }
Пример #8
0
        private static void ValidateForwarded(TransformBuilderContext builderContext, bool useHost, bool useProto,
                                              NodeFormat forFormat, NodeFormat byFormat, bool append)
        {
            Assert.False(builderContext.UseDefaultForwarders);

            if (byFormat != NodeFormat.None || forFormat != NodeFormat.None || useHost || useProto)
            {
                var transform = Assert.Single(builderContext.RequestTransforms);
                var requestHeaderForwardedTransform = Assert.IsType <RequestHeaderForwardedTransform>(transform);
                Assert.Equal(append, requestHeaderForwardedTransform.Append);
                Assert.Equal(useHost, requestHeaderForwardedTransform.HostEnabled);
                Assert.Equal(useProto, requestHeaderForwardedTransform.ProtoEnabled);
                Assert.Equal(byFormat, requestHeaderForwardedTransform.ByFormat);
                Assert.Equal(forFormat, requestHeaderForwardedTransform.ForFormat);
            }
            else
            {
                Assert.Empty(builderContext.RequestTransforms);
            }
        }
Пример #9
0
 /// <summary>
 /// Adds the transform which will add X-Forwarded-* request headers.
 /// </summary>
 public static TransformBuilderContext AddXForwarded(this TransformBuilderContext context, string headerPrefix = "X-Forwarded-",
                                                     bool useFor = true, bool useHost = true, bool useProto = true, bool usePathBase = true, bool append = true)
 {
     context.UseDefaultForwarders = false;
     if (useFor)
     {
         context.RequestTransforms.Add(new RequestHeaderXForwardedForTransform(headerPrefix + ForwardedTransformFactory.ForKey, append));
     }
     if (useHost)
     {
         context.RequestTransforms.Add(new RequestHeaderXForwardedHostTransform(headerPrefix + ForwardedTransformFactory.HostKey, append));
     }
     if (useProto)
     {
         context.RequestTransforms.Add(new RequestHeaderXForwardedProtoTransform(headerPrefix + ForwardedTransformFactory.ProtoKey, append));
     }
     if (usePathBase)
     {
         context.RequestTransforms.Add(new RequestHeaderXForwardedPathBaseTransform(headerPrefix + ForwardedTransformFactory.PathBaseKey, append));
     }
     return(context);
 }
Пример #10
0
 /// <summary>
 /// Adds the transform that will remove the given query key.
 /// </summary>
 public static TransformBuilderContext AddQueryRemoveKey(this TransformBuilderContext context, string queryKey)
 {
     context.RequestTransforms.Add(new QueryParameterRemoveTransform(queryKey));
     return(context);
 }
Пример #11
0
 /// <summary>
 /// Adds the transform which will append or set the request header.
 /// </summary>
 public static TransformBuilderContext AddRequestHeader(this TransformBuilderContext context, string headerName, string value, bool append = true)
 {
     context.RequestTransforms.Add(new RequestHeaderValueTransform(headerName, value, append));
     return(context);
 }
Пример #12
0
 /// <summary>
 /// Adds the transform which will append or set the response trailer.
 /// </summary>
 public static TransformBuilderContext AddResponseTrailer(this TransformBuilderContext context, string headerName, string value, bool append = true, bool always = true)
 {
     context.ResponseTrailersTransforms.Add(new ResponseTrailerValueTransform(headerName, value, append, always));
     return(context);
 }
Пример #13
0
 /// <summary>
 /// Adds the transform which will set the given header with the Base64 encoded client certificate.
 /// </summary>
 public static TransformBuilderContext AddClientCertHeader(this TransformBuilderContext context, string headerName)
 {
     context.RequestTransforms.Add(new RequestHeaderClientCertTransform(headerName));
     return(context);
 }
Пример #14
0
 /// <summary>
 /// Adds the transform that will replace the HTTP method if it matches.
 /// </summary>
 public static TransformBuilderContext AddHttpMethodChange(this TransformBuilderContext context, string fromHttpMethod, string toHttpMethod)
 {
     context.RequestTransforms.Add(new HttpMethodChangeTransform(fromHttpMethod, toHttpMethod));
     return(context);
 }
Пример #15
0
 /// <summary>
 /// Adds the transform which will remove the matching prefix from the request path.
 /// </summary>
 public static TransformBuilderContext AddPathRemovePrefix(this TransformBuilderContext context, PathString prefix)
 {
     context.RequestTransforms.Add(new PathStringTransform(PathStringTransform.PathTransformMode.RemovePrefix, prefix));
     return(context);
 }
Пример #16
0
 /// <summary>
 /// Adds the transform which sets the request path with the given value.
 /// </summary>
 public static TransformBuilderContext AddPathSet(this TransformBuilderContext context, PathString path)
 {
     context.RequestTransforms.Add(new PathStringTransform(PathStringTransform.PathTransformMode.Set, path));
     return(context);
 }