// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http;
namespace Microsoft.AspNetCore.Rewrite.Internal.ModRewrite
{
///
/// Contains a sequence of pattern segments, which on obtaining the context, will create the appropriate
/// test string and condition for rules and conditions.
///
public class Pattern
{
private IReadOnlyList Segments { get; }
///
/// Creates a new Pattern
///
/// List of pattern segments which will be applied.
public Pattern(IReadOnlyList segments)
{
Segments = segments;
}
///
/// Creates the appropriate test string from the Httpcontext and Segments.
///
///
///
///
///
public string GetPattern(HttpContext context, Match ruleMatch, Match prevCondition)
{
var res = new StringBuilder();
foreach (var segment in Segments)
{
// TODO handle case when segment.Variable is 0 in rule and condition
switch (segment.Type)
{
case SegmentType.Literal:
res.Append(segment.Variable);
break;
case SegmentType.ServerParameter:
res.Append(ServerVariables.Resolve(segment.Variable, context));
break;
case SegmentType.RuleParameter:
var ruleParam = ruleMatch.Groups[segment.Variable];
if (ruleParam != null)
{
res.Append(ruleParam);
}
break;
case SegmentType.ConditionParameter:
var condParam = prevCondition.Groups[segment.Variable];
if (condParam != null)
{
res.Append(condParam);
}
break;
}
}
return res.ToString();
}
}
}