* UriTemplate.cs, UriTemplateMatch.cs : add support for wildcard.
* UriTemplateTest.cs : add test for wildcard.
svn path=/trunk/mcs/; revision=141410
+2008-09-07 Atsushi Enomoto <atsushi@ximian.com>
+
+ * UriTemplate.cs, UriTemplateMatch.cs : add support for wildcard.
+
2008-09-07 Atsushi Enomoto <atsushi@ximian.com>
* UriTemplate.cs : implement IsEquivalentTo().
// Match
+ static readonly char [] slashSep = {'/'};
+
public UriTemplateMatch Match (Uri baseAddress, Uri candidate)
{
CheckBaseAddress (baseAddress);
m.RequestUri = candidate;
var vc = m.BoundVariables;
- string cp = baseAddress.MakeRelativeUri(candidate).ToString();
+ string cp = baseAddress.MakeRelativeUri(candidate).ToString ();
if (IgnoreTrailingSlash && cp [cp.Length - 1] == '/')
cp = cp.Substring (0, cp.Length - 1);
int tEnd = template.IndexOf ('?');
if (tEnd < 0)
tEnd = template.Length;
-
- if ((cp.Length - c) != (tEnd - i) ||
+ bool wild = (template [tEnd - 1] == '*');
+ if (wild)
+ tEnd--;
+ if (!wild && (cp.Length - c) != (tEnd - i) ||
String.CompareOrdinal (cp, c, template, i, tEnd - i) != 0)
return null; // suffix doesn't match
-
+ if (wild) {
+ c += tEnd - i;
+ foreach (var pe in cp.Substring (c).Split (slashSep, StringSplitOptions.RemoveEmptyEntries))
+ m.WildcardPathSegments.Add (pe);
+ }
if (candidate.Query.Length == 0)
return m;
ReadOnlyCollection<string> ParsePathTemplate (string template, int index, int end)
{
+ int widx = template.IndexOf ('*', index, end);
+ if (widx >= 0 && widx != end - 1)
+ throw new FormatException (String.Format ("Wildcard in UriTemplate is valid only if it is placed at the last part of the path: '{0}'", template));
List<string> list = null;
int prevEnd = -2;
for (int i = index; i <= end; ) {
NameValueCollection nvc, query_params;
object data;
UriTemplate template;
- Collection<string> path_segments;
+ Collection<string> path_segments, wildcard;
public Uri BaseUri {
get { return base_uri; }
set { template = value; }
}
- [MonoTODO]
public Collection<string> WildcardPathSegments {
- get { throw new NotImplementedException (); }
+ get {
+ if (wildcard == null)
+ wildcard = new Collection<string> ();
+ return wildcard;
+ }
}
}
}
+2009-09-07 Atsushi Enomoto <atsushi@ximian.com>
+
+ * UriTemplateTest.cs : add test for wildcard.
+
2009-09-07 Atsushi Enomoto <atsushi@ximian.com>
* UriTemplateEquivalenceComparerTest.cs : new test.
new UriTemplate ("http://localhost:8080/{foo}/{");
}
+ [Test]
+ [ExpectedException (typeof (FormatException))]
+ public void ConstructorBrokenTemplate3 ()
+ {
+ new UriTemplate ("http://localhost:8080/{foo}/*/baz");
+ }
+
[Test]
public void ToString ()
{
Assert.AreEqual ("vv", m.QueryParameters ["p1"], "#5");
}
+ [Test]
+ public void MatchWildcard ()
+ {
+ var t = new UriTemplate ("/hoge/*?p1={foo}");
+ var m = t.Match (new Uri ("http://localhost"), new Uri ("http://localhost/hoge/ppp/qqq?p1=v1"));
+ Assert.IsNotNull (m, "#0");
+ Assert.IsNotNull (m.QueryParameters, "#1.0");
+ Assert.AreEqual ("v1", m.QueryParameters ["p1"], "#1");
+ Assert.IsNotNull (m.WildcardPathSegments, "#2.0");
+ Assert.AreEqual (2, m.WildcardPathSegments.Count, "#2");
+ Assert.AreEqual ("ppp", m.WildcardPathSegments [0], "#3");
+ Assert.AreEqual ("qqq", m.WildcardPathSegments [1], "#4");
+ }
+
[Test]
public void IgnoreTrailingSlash ()
{