public bool Build(TransformBuilderContext context, IReadOnlyDictionary <string, string> transformValues)
        {
            if (transformValues.TryGetValue(RequestHeadersCopyKey, out var copyHeaders))
            {
                TransformHelpers.CheckTooManyParameters(transformValues, expected: 1);
                context.CopyRequestHeaders = bool.Parse(copyHeaders);
            }
            else if (transformValues.TryGetValue(RequestHeaderOriginalHostKey, out var originalHost))
            {
                TransformHelpers.CheckTooManyParameters(transformValues, expected: 1);
                context.UseOriginalHost = bool.Parse(originalHost);
            }
            else if (transformValues.TryGetValue(RequestHeaderKey, out var headerName))
            {
                TransformHelpers.CheckTooManyParameters(transformValues, expected: 2);
                if (transformValues.TryGetValue(SetKey, out var setValue))
                {
                    context.AddRequestHeader(headerName, setValue, append: false);
                }
                else if (transformValues.TryGetValue(AppendKey, out var appendValue))
                {
                    context.AddRequestHeader(headerName, appendValue, append: true);
                }
                else
                {
                    throw new ArgumentException($"Unexpected parameters for RequestHeader: {string.Join(';', transformValues.Keys)}. Expected 'Set' or 'Append'");
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
        public bool Build(TransformBuilderContext context, IReadOnlyDictionary <string, string> transformValues)
        {
            if (transformValues.TryGetValue(PathSetKey, out var pathSet))
            {
                TransformHelpers.CheckTooManyParameters(transformValues, expected: 1);
                var path = MakePathString(pathSet);
                context.AddPathSet(path);
            }
            else if (transformValues.TryGetValue(PathPrefixKey, out var pathPrefix))
            {
                TransformHelpers.CheckTooManyParameters(transformValues, expected: 1);
                var path = MakePathString(pathPrefix);
                context.AddPathPrefix(path);
            }
            else if (transformValues.TryGetValue(PathRemovePrefixKey, out var pathRemovePrefix))
            {
                TransformHelpers.CheckTooManyParameters(transformValues, expected: 1);
                var path = MakePathString(pathRemovePrefix);
                context.AddPathRemovePrefix(path);
            }
            else if (transformValues.TryGetValue(PathPatternKey, out var pathPattern))
            {
                TransformHelpers.CheckTooManyParameters(transformValues, expected: 1);
                var path = MakePathString(pathPattern);
                // We don't use the extension here because we want to avoid doing a DI lookup for the binder every time.
                context.RequestTransforms.Add(new PathRouteValuesTransform(path.Value, _binderFactory));
            }
            else
            {
                return(false);
            }

            return(true);
        }
        public bool Validate(TransformRouteValidationContext context, IReadOnlyDictionary <string, string> transformValues)
        {
            if (transformValues.TryGetValue(RequestHeadersCopyKey, out var copyHeaders))
            {
                TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1);
                if (!bool.TryParse(copyHeaders, out var _))
                {
                    context.Errors.Add(new ArgumentException($"Unexpected value for RequestHeaderCopy: {copyHeaders}. Expected 'true' or 'false'"));
                }
            }
            else if (transformValues.TryGetValue(RequestHeaderOriginalHostKey, out var originalHost))
            {
                TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1);
                if (!bool.TryParse(originalHost, out var _))
                {
                    context.Errors.Add(new ArgumentException($"Unexpected value for RequestHeaderOriginalHost: {originalHost}. Expected 'true' or 'false'"));
                }
            }
            else if (transformValues.TryGetValue(RequestHeaderKey, out var _))
            {
                TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 2);
                if (!transformValues.TryGetValue(SetKey, out var _) && !transformValues.TryGetValue(AppendKey, out var _))
                {
                    context.Errors.Add(new ArgumentException($"Unexpected parameters for RequestHeader: {string.Join(';', transformValues.Keys)}. Expected 'Set' or 'Append'"));
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
        public bool Validate(TransformRouteValidationContext context, IReadOnlyDictionary <string, string> transformValues)
        {
            if (transformValues.TryGetValue(PathSetKey, out var pathSet))
            {
                TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1);
            }
            else if (transformValues.TryGetValue(PathPrefixKey, out var pathPrefix))
            {
                TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1);
            }
            else if (transformValues.TryGetValue(PathRemovePrefixKey, out var pathRemovePrefix))
            {
                TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1);
            }
            else if (transformValues.TryGetValue(PathPatternKey, out var pathPattern))
            {
                TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1);
                // TODO: Validate the pattern format. Does it build?
            }
            else
            {
                return(false);
            }

            return(true);
        }
        public bool Validate(TransformRouteValidationContext context, IReadOnlyDictionary <string, string> transformValues)
        {
            if (transformValues.TryGetValue(QueryValueParameterKey, out var queryValueParameter))
            {
                TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 2);
                if (!transformValues.TryGetValue(AppendKey, out var _) && !transformValues.TryGetValue(SetKey, out var _))
                {
                    context.Errors.Add(new ArgumentException($"Unexpected parameters for QueryValueParameter: {string.Join(';', transformValues.Keys)}. Expected 'Append' or 'Set'."));
                }
            }
            else if (transformValues.TryGetValue(QueryRouteParameterKey, out var queryRouteParameter))
            {
                TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 2);
                if (!transformValues.TryGetValue(AppendKey, out var _) && !transformValues.TryGetValue(SetKey, out var _))
                {
                    context.Errors.Add(new ArgumentException($"Unexpected parameters for QueryRouteParameter: {string.Join(';', transformValues.Keys)}. Expected 'Append' or 'Set'."));
                }
            }
            else if (transformValues.TryGetValue(QueryRemoveParameterKey, out var removeQueryParameter))
            {
                TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1);
            }
            else
            {
                return(false);
            }

            return(true);
        }
        public bool Build(TransformBuilderContext context, IReadOnlyDictionary <string, string> transformValues)
        {
            if (transformValues.TryGetValue(HttpMethodChangeKey, out var fromHttpMethod))
            {
                TransformHelpers.CheckTooManyParameters(transformValues, expected: 2);
                if (transformValues.TryGetValue(SetKey, out var toHttpMethod))
                {
                    context.AddHttpMethodChange(fromHttpMethod, toHttpMethod);
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
        public bool Validate(TransformRouteValidationContext context, IReadOnlyDictionary <string, string> transformValues)
        {
            if (transformValues.TryGetValue(HttpMethodChangeKey, out var _))
            {
                TransformHelpers.CheckTooManyParameters(transformValues, expected: 2);
                if (!transformValues.TryGetValue(SetKey, out var _))
                {
                    context.Errors.Add(new ArgumentException($"Unexpected parameters for HttpMethod: {string.Join(';', transformValues.Keys)}. Expected 'Set'"));
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
        public bool Build(TransformBuilderContext context, IReadOnlyDictionary <string, string> transformValues)
        {
            if (transformValues.TryGetValue(QueryValueParameterKey, out var queryValueParameter))
            {
                TransformHelpers.CheckTooManyParameters(transformValues, expected: 2);
                if (transformValues.TryGetValue(AppendKey, out var appendValue))
                {
                    context.AddQueryValue(queryValueParameter, appendValue, append: true);
                }
                else if (transformValues.TryGetValue(SetKey, out var setValue))
                {
                    context.AddQueryValue(queryValueParameter, setValue, append: false);
                }
                else
                {
                    throw new NotSupportedException(string.Join(";", transformValues.Keys));
                }
            }
            else if (transformValues.TryGetValue(QueryRouteParameterKey, out var queryRouteParameter))
            {
                TransformHelpers.CheckTooManyParameters(transformValues, expected: 2);
                if (transformValues.TryGetValue(AppendKey, out var routeValueKeyAppend))
                {
                    context.AddQueryRouteValue(queryRouteParameter, routeValueKeyAppend, append: true);
                }
                else if (transformValues.TryGetValue(SetKey, out var routeValueKeySet))
                {
                    context.AddQueryRouteValue(queryRouteParameter, routeValueKeySet, append: false);
                }
                else
                {
                    throw new NotSupportedException(string.Join(";", transformValues.Keys));
                }
            }
            else if (transformValues.TryGetValue(QueryRemoveParameterKey, out var removeQueryParameter))
            {
                TransformHelpers.CheckTooManyParameters(transformValues, expected: 1);
                context.AddQueryRemoveKey(removeQueryParameter);
            }
            else
            {
                return(false);
            }

            return(true);
        }
        public bool Build(TransformBuilderContext context, IReadOnlyDictionary <string, string> transformValues)
        {
            if (transformValues.TryGetValue(ResponseHeadersCopyKey, out var copyHeaders))
            {
                TransformHelpers.CheckTooManyParameters(transformValues, expected: 1);
                context.CopyResponseHeaders = bool.Parse(copyHeaders);
            }
            else if (transformValues.TryGetValue(ResponseTrailersCopyKey, out copyHeaders))
            {
                TransformHelpers.CheckTooManyParameters(transformValues, expected: 1);
                context.CopyResponseTrailers = bool.Parse(copyHeaders);
            }
            else if (transformValues.TryGetValue(ResponseHeaderKey, out var responseHeaderName))
            {
                var always = false;
                if (transformValues.TryGetValue(WhenKey, out var whenValue))
                {
                    TransformHelpers.CheckTooManyParameters(transformValues, expected: 3);
                    always = string.Equals(AlwaysValue, whenValue, StringComparison.OrdinalIgnoreCase);
                }
                else
                {
                    TransformHelpers.CheckTooManyParameters(transformValues, expected: 2);
                }

                if (transformValues.TryGetValue(SetKey, out var setValue))
                {
                    context.AddResponseHeader(responseHeaderName, setValue, append: false, always);
                }
                else if (transformValues.TryGetValue(AppendKey, out var appendValue))
                {
                    context.AddResponseHeader(responseHeaderName, appendValue, append: true, always);
                }
                else
                {
                    throw new ArgumentException($"Unexpected parameters for ResponseHeader: {string.Join(';', transformValues.Keys)}. Expected 'Set' or 'Append'");
                }
            }
            else if (transformValues.TryGetValue(ResponseTrailerKey, out var responseTrailerName))
            {
                var always = false;
                if (transformValues.TryGetValue(WhenKey, out var whenValue))
                {
                    TransformHelpers.CheckTooManyParameters(transformValues, expected: 3);
                    always = string.Equals(AlwaysValue, whenValue, StringComparison.OrdinalIgnoreCase);
                }
                else
                {
                    TransformHelpers.CheckTooManyParameters(transformValues, expected: 2);
                }

                if (transformValues.TryGetValue(SetKey, out var setValue))
                {
                    context.AddResponseTrailer(responseTrailerName, setValue, append: false, always);
                }
                else if (transformValues.TryGetValue(AppendKey, out var appendValue))
                {
                    context.AddResponseTrailer(responseTrailerName, appendValue, append: true, always);
                }
                else
                {
                    throw new ArgumentException($"Unexpected parameters for ResponseTrailer: {string.Join(';', transformValues.Keys)}. Expected 'Set' or 'Append'");
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
        public bool Validate(TransformRouteValidationContext context, IReadOnlyDictionary <string, string> transformValues)
        {
            if (transformValues.TryGetValue(ResponseHeadersCopyKey, out var copyHeaders))
            {
                TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1);
                if (!bool.TryParse(copyHeaders, out var _))
                {
                    context.Errors.Add(new ArgumentException($"Unexpected value for ResponseHeadersCopy: {copyHeaders}. Expected 'true' or 'false'"));
                }
            }
            else if (transformValues.TryGetValue(ResponseTrailersCopyKey, out copyHeaders))
            {
                TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1);
                if (!bool.TryParse(copyHeaders, out var _))
                {
                    context.Errors.Add(new ArgumentException($"Unexpected value for ResponseTrailersCopy: {copyHeaders}. Expected 'true' or 'false'"));
                }
            }
            else if (transformValues.TryGetValue(ResponseHeaderKey, out var _))
            {
                if (transformValues.TryGetValue(WhenKey, out var whenValue))
                {
                    TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 3);
                    if (!string.Equals(AlwaysValue, whenValue, StringComparison.OrdinalIgnoreCase) && !string.Equals(SuccessValue, whenValue, StringComparison.OrdinalIgnoreCase))
                    {
                        context.Errors.Add(new ArgumentException($"Unexpected value for ResponseHeader:When: {whenValue}. Expected 'Always' or 'Success'"));
                    }
                }
                else
                {
                    TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 2);
                }

                if (!transformValues.TryGetValue(SetKey, out var _) && !transformValues.TryGetValue(AppendKey, out var _))
                {
                    context.Errors.Add(new ArgumentException($"Unexpected parameters for ResponseHeader: {string.Join(';', transformValues.Keys)}. Expected 'Set' or 'Append'"));
                }
            }
            else if (transformValues.TryGetValue(ResponseTrailerKey, out var _))
            {
                if (transformValues.TryGetValue(WhenKey, out var whenValue))
                {
                    TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 3);
                    if (!string.Equals(AlwaysValue, whenValue, StringComparison.OrdinalIgnoreCase) && !string.Equals(SuccessValue, whenValue, StringComparison.OrdinalIgnoreCase))
                    {
                        context.Errors.Add(new ArgumentException($"Unexpected value for ResponseTrailer:When: {whenValue}. Expected 'Always' or 'Success'"));
                    }
                }
                else
                {
                    TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 2);
                }

                if (!transformValues.TryGetValue(SetKey, out var _) && !transformValues.TryGetValue(AppendKey, out var _))
                {
                    context.Errors.Add(new ArgumentException($"Unexpected parameters for ResponseTrailer: {string.Join(';', transformValues.Keys)}. Expected 'Set' or 'Append'"));
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 11
0
        public bool Validate(TransformRouteValidationContext context, IReadOnlyDictionary <string, string> transformValues)
        {
            if (transformValues.TryGetValue(XForwardedKey, out var xforwardedHeaders))
            {
                var expected = 1;

                if (transformValues.TryGetValue(AppendKey, out var appendValue))
                {
                    expected++;
                    if (!bool.TryParse(appendValue, out var _))
                    {
                        context.Errors.Add(new ArgumentException($"Unexpected value for X-Forwarded:Append: {appendValue}. Expected 'true' or 'false'"));
                    }
                }

                if (transformValues.TryGetValue(PrefixForwardedKey, out var _))
                {
                    expected++;
                }

                TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected);

                // for, host, proto, Prefix
                var tokens = xforwardedHeaders.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var token in tokens)
                {
                    if (!string.Equals(token, ForKey, StringComparison.OrdinalIgnoreCase) &&
                        !string.Equals(token, HostKey, StringComparison.OrdinalIgnoreCase) &&
                        !string.Equals(token, ProtoKey, StringComparison.OrdinalIgnoreCase) &&
                        !string.Equals(token, PrefixKey, StringComparison.OrdinalIgnoreCase))
                    {
                        context.Errors.Add(new ArgumentException($"Unexpected value for X-Forwarded: {token}. Expected 'for', 'host', 'proto', or 'Prefix'"));
                    }
                }
            }
            else if (transformValues.TryGetValue(ForwardedKey, out var forwardedHeader))
            {
                var expected = 1;

                if (transformValues.TryGetValue(AppendKey, out var appendValue))
                {
                    expected++;
                    if (!bool.TryParse(appendValue, out var _))
                    {
                        context.Errors.Add(new ArgumentException($"Unexpected value for Forwarded:Append: {appendValue}. Expected 'true' or 'false'"));
                    }
                }

                var enumValues = "Random,RandomAndPort,Unknown,UnknownAndPort,Ip,IpAndPort";
                if (transformValues.TryGetValue(ForFormatKey, out var forFormat))
                {
                    expected++;
                    if (!Enum.TryParse <NodeFormat>(forFormat, ignoreCase: true, out var _))
                    {
                        context.Errors.Add(new ArgumentException($"Unexpected value for Forwarded:ForFormat: {forFormat}. Expected: {enumValues}"));
                    }
                }

                if (transformValues.TryGetValue(ByFormatKey, out var byFormat))
                {
                    expected++;
                    if (!Enum.TryParse <NodeFormat>(byFormat, ignoreCase: true, out var _))
                    {
                        context.Errors.Add(new ArgumentException($"Unexpected value for Forwarded:ByFormat: {byFormat}. Expected: {enumValues}"));
                    }
                }

                TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected);

                // for, host, proto, by
                var tokens = forwardedHeader.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var token in tokens)
                {
                    if (!string.Equals(token, ByKey, StringComparison.OrdinalIgnoreCase) &&
                        !string.Equals(token, HostKey, StringComparison.OrdinalIgnoreCase) &&
                        !string.Equals(token, ProtoKey, StringComparison.OrdinalIgnoreCase) &&
                        !string.Equals(token, ForKey, StringComparison.OrdinalIgnoreCase))
                    {
                        context.Errors.Add(new ArgumentException($"Unexpected value for X-Forwarded: {token}. Expected 'for', 'host', 'proto', or 'by'"));
                    }
                }
            }
            else if (transformValues.TryGetValue(ClientCertKey, out var clientCertHeader))
            {
                TransformHelpers.TryCheckTooManyParameters(context, transformValues, expected: 1);
            }
            else
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 12
0
        public bool Build(TransformBuilderContext context, IReadOnlyDictionary <string, string> transformValues)
        {
            if (transformValues.TryGetValue(XForwardedKey, out var xforwardedHeaders))
            {
                var expected = 1;

                var append = true;
                if (transformValues.TryGetValue(AppendKey, out var appendValue))
                {
                    expected++;
                    append = bool.Parse(appendValue);
                }

                var prefix = "X-Forwarded-";
                if (transformValues.TryGetValue(PrefixForwardedKey, out var prefixValue))
                {
                    expected++;
                    prefix = prefixValue;
                }

                TransformHelpers.CheckTooManyParameters(transformValues, expected);

                // for, host, proto, Prefix
                var  tokens = xforwardedHeaders.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                bool useFor, useHost, useProto, usePrefix;
                useFor = useHost = useProto = usePrefix = false;

                foreach (var token in tokens)
                {
                    if (string.Equals(token, ForKey, StringComparison.OrdinalIgnoreCase))
                    {
                        useFor = true;
                    }
                    else if (string.Equals(token, HostKey, StringComparison.OrdinalIgnoreCase))
                    {
                        useHost = true;
                    }
                    else if (string.Equals(token, ProtoKey, StringComparison.OrdinalIgnoreCase))
                    {
                        useProto = true;
                    }
                    else if (string.Equals(token, PrefixKey, StringComparison.OrdinalIgnoreCase))
                    {
                        usePrefix = true;
                    }
                    else
                    {
                        throw new ArgumentException($"Unexpected value for X-Forwarded: {token}. Expected 'for', 'host', 'proto', or 'Prefix'");
                    }
                }

                context.AddXForwarded(prefix, useFor, useHost, useProto, usePrefix, append);
            }
            else if (transformValues.TryGetValue(ForwardedKey, out var forwardedHeader))
            {
                var useHost   = false;
                var useProto  = false;
                var useFor    = false;
                var useBy     = false;
                var forFormat = NodeFormat.None;
                var byFormat  = NodeFormat.None;

                // for, host, proto, Prefix
                var tokens = forwardedHeader.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var token in tokens)
                {
                    if (string.Equals(token, ForKey, StringComparison.OrdinalIgnoreCase))
                    {
                        useFor    = true;
                        forFormat = NodeFormat.Random; // RFC Default
                    }
                    else if (string.Equals(token, ByKey, StringComparison.OrdinalIgnoreCase))
                    {
                        useBy    = true;
                        byFormat = NodeFormat.Random; // RFC Default
                    }
                    else if (string.Equals(token, HostKey, StringComparison.OrdinalIgnoreCase))
                    {
                        useHost = true;
                    }
                    else if (string.Equals(token, ProtoKey, StringComparison.OrdinalIgnoreCase))
                    {
                        useProto = true;
                    }
                    else
                    {
                        throw new ArgumentException($"Unexpected value for X-Forwarded: {token}. Expected 'for', 'host', 'proto', or 'by'");
                    }
                }

                var expected = 1;

                var append = true;
                if (transformValues.TryGetValue(AppendKey, out var appendValue))
                {
                    expected++;
                    append = bool.Parse(appendValue);
                }

                if (useFor && transformValues.TryGetValue(ForFormatKey, out var forFormatString))
                {
                    expected++;
                    forFormat = Enum.Parse <NodeFormat>(forFormatString, ignoreCase: true);
                }

                if (useBy && transformValues.TryGetValue(ByFormatKey, out var byFormatString))
                {
                    expected++;
                    byFormat = Enum.Parse <NodeFormat>(byFormatString, ignoreCase: true);
                }

                TransformHelpers.CheckTooManyParameters(transformValues, expected);

                context.UseDefaultForwarders = false;
                if (useBy || useFor || useHost || useProto)
                {
                    // Not using the extension to avoid resolving the random factory each time.
                    context.RequestTransforms.Add(new RequestHeaderForwardedTransform(_randomFactory, forFormat, byFormat, useHost, useProto, append));
                }
            }
            else if (transformValues.TryGetValue(ClientCertKey, out var clientCertHeader))
            {
                TransformHelpers.CheckTooManyParameters(transformValues, expected: 1);
                context.AddClientCertHeader(clientCertHeader);
            }
            else
            {
                return(false);
            }

            return(true);
        }