Esempio n. 1
0
        public void CheckServerVariableFeatureHasPrecedenceWhenEnabled(string variable, string expected, int uriMatchPart)
        {
            // Arrange and Act
            var testParserContext = new ParserContext("test");
            var serverVar         = ServerVariables.FindServerVariable(variable, testParserContext, (UriMatchPart)uriMatchPart, false);
            var httpContext       = CreateTestHttpContext();

            httpContext.Features.Set <IServerVariablesFeature>(new TestServerVariablesFeature(new Dictionary <string, string>
            {
                ["CONTENT_LENGTH"]   = "20",
                ["CONTENT_TYPE"]     = "text/xml",
                ["HTTP_ACCEPT"]      = "other-accept",
                ["HTTP_COOKIE"]      = "other-cookie",
                ["HTTP_HOST"]        = "otherexample.com",
                ["HTTP_REFERER"]     = "other-referer",
                ["HTTP_USER_AGENT"]  = "other-useragent",
                ["HTTP_CONNECTION"]  = "other-connection",
                ["HTTP_URL"]         = "http://otherexample.com/other-foo?bar=2",
                ["QUERY_STRING"]     = "bar=2",
                ["REQUEST_FILENAME"] = "/other-foo",
                ["REQUEST_URI"]      = "/other-foo",
                ["REQUEST_METHOD"]   = "POST"
            }));

            var rewriteContext = CreateTestRewriteContext(httpContext);
            var lookup         = serverVar.Evaluate(rewriteContext, CreateTestRuleMatch().BackReferences, CreateTestCondMatch().BackReferences);

            // Assert
            Assert.Equal(expected, lookup);
        }
Esempio n. 2
0
        public void CheckServerVariableParsingAndApplication(string variable, string expected, int uriMatchPart)
        {
            // Arrange and Act
            var testParserContext = new ParserContext("test");
            var serverVar         = ServerVariables.FindServerVariable(variable, testParserContext, (UriMatchPart)uriMatchPart, true);
            var lookup            = serverVar.Evaluate(CreateTestRewriteContext(), CreateTestRuleMatch().BackReferences, CreateTestCondMatch().BackReferences);

            // Assert
            Assert.Equal(expected, lookup);
        }
Esempio n. 3
0
        private void EmptyQueryStringCheck()
        {
            var context        = new DefaultHttpContext();
            var rewriteContext = new RewriteContext {
                HttpContext = context
            };
            var testParserContext = new ParserContext("test");
            var serverVar         = ServerVariables.FindServerVariable("QUERY_STRING", testParserContext, UriMatchPart.Path, true);
            var lookup            = serverVar.Evaluate(rewriteContext, CreateTestRuleMatch().BackReferences, CreateTestCondMatch().BackReferences);

            Assert.Equal(string.Empty, lookup);
        }
Esempio n. 4
0
    /// <summary>
    /// Obtains the condition parameter, which could either be a condition variable or a
    /// server variable. Assumes the current character is immediately after the '%'.
    /// context, on return will be on the last character of variable captured, such that after
    /// Next() is called, it will be on the character immediately after the condition parameter.
    /// </summary>
    /// <param name="context">The ParserContext</param>
    /// <param name="results">The List of results which the new condition parameter will be added.</param>
    /// <returns>true </returns>
    private static void ParseConditionParameter(ParserContext context, IList <PatternSegment> results)
    {
        // Parse { }
        if (context.Current == OpenBrace)
        {
            // Start of a server variable
            if (!context.Next())
            {
                // Dangling {
                throw new FormatException(Resources.FormatError_InputParserMissingCloseBrace(context.Index));
            }
            context.Mark();
            while (context.Current != CloseBrace)
            {
                if (!context.Next())
                {
                    throw new FormatException(Resources.FormatError_InputParserMissingCloseBrace(context.Index));
                }
                else if (context.Current == Colon)
                {
                    // Have a segmented look up Ex: HTTP:xxxx
                    // Most of these we can't handle
                    throw new NotImplementedException("Segmented Lookups no implemented");
                }
            }

            // Need to verify server variable captured exists
            var rawServerVariable = context.Capture() !;
            results.Add(ServerVariables.FindServerVariable(rawServerVariable, context));
        }
        else if (context.Current >= '0' && context.Current <= '9')
        {
            // means we have a segmented lookup
            // store information in the testString result to know what to look up.
            context.Mark();
            context.Next();
            var rawConditionParameter = context.Capture() !;

            // Once we leave this method, the while loop will call next again. Because
            // capture is exclusive, we need to go one past the end index, capture, and then go back.
            context.Back();
            var parsedIndex = int.Parse(rawConditionParameter, CultureInfo.InvariantCulture);
            results.Add(new ConditionMatchSegment(parsedIndex));
        }
        else
        {
            // illegal escape of a character
            throw new FormatException(Resources.FormatError_InputParserInvalidInteger(context.Template, context.Index));
        }
    }