-
Notifications
You must be signed in to change notification settings - Fork 84
#55 add IIS rewrite map support with tests #168
Changes from all commits
30b4b75
2adedfc
7d0ad0f
d428eb9
5faa822
224665a
24f2ad7
87c900a
c2b536d
162114e
bfae468
1ed4bc1
883a1b2
a88996d
2ccb462
7a8ac6b
9eb1b74
43fbf3b
7d1cc11
461d587
ad5b72c
435fed7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,4 +66,4 @@ public static RewriteOptions AddIISUrlRewrite(this RewriteOptions options, TextR | |
return options; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 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; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite | ||
{ | ||
public class IISRewriteMap | ||
{ | ||
private readonly Dictionary<string, string> _map = new Dictionary<string, string>(); | ||
|
||
public IISRewriteMap(string name) | ||
{ | ||
if (string.IsNullOrEmpty(name)) | ||
{ | ||
throw new ArgumentException(nameof(name)); | ||
} | ||
Name = name; | ||
} | ||
|
||
public string Name { get; } | ||
|
||
public string this[string key] | ||
{ | ||
get | ||
{ | ||
string value; | ||
return _map.TryGetValue(key, out value) ? value : null; | ||
} | ||
set | ||
{ | ||
if (string.IsNullOrEmpty(key)) | ||
{ | ||
throw new ArgumentException(nameof(key)); | ||
} | ||
if (string.IsNullOrEmpty(value)) | ||
{ | ||
throw new ArgumentException(nameof(value)); | ||
} | ||
_map[key] = value; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// 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; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite | ||
{ | ||
public class IISRewriteMapCollection : IEnumerable<IISRewriteMap> | ||
{ | ||
private readonly Dictionary<string, IISRewriteMap> _rewriteMaps = new Dictionary<string, IISRewriteMap>(); | ||
|
||
public void Add(IISRewriteMap rewriteMap) | ||
{ | ||
if (rewriteMap != null) | ||
{ | ||
_rewriteMaps[rewriteMap.Name] = rewriteMap; | ||
} | ||
} | ||
|
||
public int Count => _rewriteMaps.Count; | ||
|
||
public IISRewriteMap this[string key] | ||
{ | ||
get | ||
{ | ||
IISRewriteMap value; | ||
return _rewriteMaps.TryGetValue(key, out value) ? value : null; | ||
} | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return _rewriteMaps.Values.GetEnumerator(); | ||
} | ||
|
||
public IEnumerator<IISRewriteMap> GetEnumerator() | ||
{ | ||
return _rewriteMaps.Values.GetEnumerator(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// 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; | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
|
||
namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite | ||
{ | ||
public static class RewriteMapParser | ||
{ | ||
public static IISRewriteMapCollection Parse(XElement xmlRoot) | ||
{ | ||
if (xmlRoot == null) | ||
{ | ||
throw new ArgumentNullException(nameof(xmlRoot)); | ||
} | ||
|
||
var mapsElement = xmlRoot.Descendants(RewriteTags.RewriteMaps).SingleOrDefault(); | ||
if (mapsElement == null) | ||
{ | ||
return null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. Should we throw here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so. It's valid not to have a rewrite map. Am I missing your point? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's what I was talking about. It just seems like and odd scenario There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i could move the test for the element outside of the parser and never end up in that scenario but this seems to be the preferred style throughout the project. i definitely don't think it should throw, though. is the structure what's objectionable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it should stay here. If its a valid scenario it can stay |
||
} | ||
|
||
var rewriteMaps = new IISRewriteMapCollection(); | ||
foreach (var mapElement in mapsElement.Elements(RewriteTags.RewriteMap)) | ||
{ | ||
var map = new IISRewriteMap(mapElement.Attribute(RewriteTags.Name)?.Value); | ||
foreach (var addElement in mapElement.Elements(RewriteTags.Add)) | ||
{ | ||
map[addElement.Attribute(RewriteTags.Key).Value.ToLowerInvariant()] = addElement.Attribute(RewriteTags.Value).Value; | ||
} | ||
rewriteMaps.Add(map); | ||
} | ||
|
||
return rewriteMaps; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// 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 Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite; | ||
|
||
namespace Microsoft.AspNetCore.Rewrite.Internal.PatternSegments | ||
{ | ||
public class RewriteMapSegment : PatternSegment | ||
{ | ||
private readonly IISRewriteMap _rewriteMap; | ||
private readonly Pattern _pattern; | ||
|
||
public RewriteMapSegment(IISRewriteMap rewriteMap, Pattern pattern) | ||
{ | ||
_rewriteMap = rewriteMap; | ||
_pattern = pattern; | ||
} | ||
|
||
public override string Evaluate(RewriteContext context, BackReferenceCollection ruleBackReferences, BackReferenceCollection conditionBackReferences) | ||
{ | ||
var key = _pattern.Evaluate(context, ruleBackReferences, conditionBackReferences).ToLowerInvariant(); | ||
return _rewriteMap[key]; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copyright and license header needed here