Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 5a0c988

Browse files
committed
Add url support to ResolvePaths
1 parent e311eaa commit 5a0c988

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/ServiceStack.Text/PathUtils.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@ public static string ResolvePaths(this string path)
137137
if (path == null || path.IndexOfAny("./", "/.") == -1)
138138
return path;
139139

140-
var parts = path.Split('/').ToList();
140+
var schemePos = path.IndexOf("://", StringComparison.Ordinal);
141+
var prefix = schemePos >= 0
142+
? path.Substring(0, schemePos + 3)
143+
: "";
144+
145+
var parts = path.Substring(prefix.Length).Split('/').ToList();
141146
var combinedPaths = new List<string>();
142147
foreach (var part in parts)
143148
{
@@ -151,12 +156,12 @@ public static string ResolvePaths(this string path)
151156
}
152157

153158
var resolvedPath = string.Join("/", combinedPaths);
154-
if (path[0] == '/')
159+
if (path[0] == '/' && prefix.Length == 0)
155160
resolvedPath = '/' + resolvedPath;
156161

157-
return path[path.Length - 1] == '/'
158-
? resolvedPath + '/'
159-
: resolvedPath;
162+
return path[path.Length - 1] == '/' && resolvedPath.Length > 0
163+
? prefix + resolvedPath + '/'
164+
: prefix + resolvedPath;
160165
}
161166

162167
public static string[] ToStrings(object[] thesePaths)

tests/ServiceStack.Text.Tests/CombinePathTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,22 @@ public void Can_resolve_paths()
4949
Assert.That("a/../../b".ResolvePaths(), Is.EqualTo("../b"));
5050
}
5151

52+
[Test]
53+
public void Can_resolve_paths_with_urls()
54+
{
55+
Assert.That("http://example.org/a/b/../".ResolvePaths(), Is.EqualTo("http://example.org/a/"));
56+
Assert.That("http://example.org/a/b/..".ResolvePaths(), Is.EqualTo("http://example.org/a"));
57+
58+
Assert.That("http://example.org/a/../b".ResolvePaths(), Is.EqualTo("http://example.org/b"));
59+
Assert.That("http://example.org/a/../b/./c".ResolvePaths(), Is.EqualTo("http://example.org/b/c"));
60+
Assert.That("http://example.org/a/b/c/d/../..".ResolvePaths(), Is.EqualTo("http://example.org/a/b"));
61+
Assert.That("http://example.org/a/b/../../c/d".ResolvePaths(), Is.EqualTo("http://example.org/c/d"));
62+
63+
Assert.That("http://example.org/a/..".ResolvePaths(), Is.EqualTo("http://example.org"));
64+
Assert.That("http://example.org/a/../..".ResolvePaths(), Is.EqualTo("http://"));
65+
Assert.That("http://example.org/a/../../".ResolvePaths(), Is.EqualTo("http://"));
66+
Assert.That("http://example.org/a/../../b".ResolvePaths(), Is.EqualTo("http://b"));
67+
}
68+
5269
}
5370
}

0 commit comments

Comments
 (0)