Skip to content
This repository has been archived by the owner on Feb 14, 2022. It is now read-only.

Commit

Permalink
Support for fragment signs ('#') in URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
DecaTec committed Oct 31, 2017
1 parent dbd7783 commit f450b90
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 28 deletions.
6 changes: 3 additions & 3 deletions DecaTec.WebDav/DecaTec.WebDav.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard1.1</TargetFramework>
<PackageId>PortableWebDavLibrary</PackageId>
<Version>1.1.5.0</Version>
<Version>1.1.6.0</Version>
<Authors>DecaTec</Authors>
<Company>DecaTec</Company>
<Product>PortableWebDavLibrary</Product>
Expand All @@ -16,8 +16,8 @@
<PackageTags>WebDAV, portable, NETStandard, NETCore, UWP, dotnet, portable-webdav-library, Xamarin, Mono, multiplatform</PackageTags>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageReleaseNotes>Full documentation with changelog is available at https://decatec.de/ext/PortableWebDAVLibrary/Doc</PackageReleaseNotes>
<AssemblyVersion>1.1.5.0</AssemblyVersion>
<FileVersion>1.1.4.0</FileVersion>
<AssemblyVersion>1.1.6.0</AssemblyVersion>
<FileVersion>1.1.6.0</FileVersion>
</PropertyGroup>

<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
Expand Down
25 changes: 23 additions & 2 deletions DecaTec.WebDav/Tools/UriHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public static class UriHelper
/// <remarks>This method should be used instead of the Uri constructor with UriKind.RelativeOrAbsolute.</remarks>
public static Uri CreateUriFromUrl(string url)
{
if(!string.IsNullOrEmpty(url))
url = EscapeFragment(url);

var uri = new Uri(url, UriKind.RelativeOrAbsolute);
return CreateRelativeUriWhenSchemeIsFile(uri);
}
Expand Down Expand Up @@ -600,8 +603,9 @@ public static string EscapePathSegments(string urlToEscape)
/// As an example, calling EscapePathSegments with the <see cref="Uri"/> "http://domain.com/sub [path]" will return "http://domain.com/sub%20%5Bpath3%5D".</remarks>
public static string EscapePathSegments(Uri uriToEscape)
{
uriToEscape = new Uri(EscapeFragment(uriToEscape.OriginalString));
var pathAndQuery = uriToEscape.LocalPath;
var splitted = pathAndQuery.Split(new char[]{'/'}, StringSplitOptions.RemoveEmptyEntries);
var splitted = pathAndQuery.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

for (int i = 0; i < splitted.Length; i++)
{
Expand All @@ -613,10 +617,27 @@ public static string EscapePathSegments(Uri uriToEscape)
Path = string.Join("/", splitted)
};

return builder.Uri.AbsoluteUri;
return builder.Uri.AbsoluteUri;
}

#endregion EscapeFolders

#region EscapeFragment

/// <summary>
/// Escapes the number sign ('#') which is a special character in a URL (fragment).
/// </summary>
/// <param name="urlToEscape">The URL to be escaped.</param>
/// <returns>The URL specified which escaped fragment signs.</returns>
public static string EscapeFragment(string urlToEscape)
{
if (string.IsNullOrEmpty(urlToEscape))
return null;

return urlToEscape.Replace("#", "%23");
}

#endregion EscapeFragment
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@
</para>
</listItem>

<listItem>
<para>
<link xlink:href="99a9e6e4-4d50-4ffd-acf5-b622749f22fe" />
</para>
</listItem>

</list>

</content>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<topic id="99a9e6e4-4d50-4ffd-acf5-b622749f22fe" revisionNumber="1">
<developerConceptualDocument
xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5"
xmlns:xlink="http://www.w3.org/1999/xlink">
<introduction></introduction>

<section>
<title>Changes in release v1.1.6.0</title>
<content>

<list class="bullet">

<listItem>
<para>
The library now supports number signs ('#') in URLs which is usually a special character in URLs (fragment).
</para>
</listItem>

</list>

</content>
</section>

<relatedTopics>
<link xlink:href="55c4b378-829f-44e2-af56-20a587c2d24b" />
</relatedTopics>
</developerConceptualDocument>
</topic>
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,15 @@
<HelpKeyword index="K" term="version, 1.1.4.0" />
</HelpKeywords>
</Topic>
<Topic id="ebd1e4d0-6e9b-4377-bb8f-2c1427947641" visible="True" isSelected="true" title="Version 1.1.5.0" linkText="Version 1.1.5.0">
<Topic id="ebd1e4d0-6e9b-4377-bb8f-2c1427947641" visible="True" title="Version 1.1.5.0" linkText="Version 1.1.5.0">
<HelpKeywords>
<HelpKeyword index="K" term="version, 1.1.5.0" />
</HelpKeywords>
</Topic>
<Topic id="99a9e6e4-4d50-4ffd-acf5-b622749f22fe" visible="True" isSelected="true" title="Version 1.1.6.0" linkText="Version 1.1.6.0">
<HelpKeywords>
<HelpKeyword index="K" term="version, 1.1.6.0" />
</HelpKeywords>
</Topic>
</Topic>
</Topics>
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
<None Include="Content\VersionHistory\v1.1.3.0.aml" />
<None Include="Content\VersionHistory\v1.1.4.0.aml" />
<None Include="Content\VersionHistory\v1.1.5.0.aml" />
<None Include="Content\VersionHistory\v1.1.6.0.aml" />
<None Include="Content\VersionHistory\VersionHistory.aml" />
<None Include="Content\Welcome.aml" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,24 +221,24 @@ public async Task UIT_WebDavSession_Copy()
}

[TestMethod]
public async Task UIT_WebDavSession_Copy_WithBrackets()
public async Task UIT_WebDavSession_Copy_WithSpecialCharacters()
{
using (var session = CreateWebDavSession())
{
session.BaseUri = new Uri(webDavRootFolder);
var created = await session.CreateDirectoryAsync("test [Source]");
var created2 = await session.CreateDirectoryAsync("test [Source]/folderToCopy");
var created3 = await session.CreateDirectoryAsync("test [Destination]");
var items = await session.ListAsync("/test [Source]");
var created = await session.CreateDirectoryAsync("test [Source#]");
var created2 = await session.CreateDirectoryAsync("test [Source#]/folderToCopy");
var created3 = await session.CreateDirectoryAsync("test [Destination#]");
var items = await session.ListAsync("/test [Source#]");
Assert.AreEqual(items.Count, 1);
var copy = await session.CopyAsync("test [Source]", "test [Destination]", true);
var items2 = await session.ListAsync("/test [Source]");
var copy = await session.CopyAsync("test [Source#]", "test [Destination#]", true);
var items2 = await session.ListAsync("/test [Source#]");
Assert.AreEqual(items2.Count, 1);
var items3 = await session.ListAsync("/test [Destination]");
var items3 = await session.ListAsync("/test [Destination#]");
Assert.AreEqual(items2.Count, 1);
Assert.AreEqual("folderToCopy", items3[0].Name);
var delete = await session.DeleteAsync("test [Source]");
var delete2 = await session.DeleteAsync("test [Destination]");
var delete = await session.DeleteAsync("test [Source#]");
var delete2 = await session.DeleteAsync("test [Destination#]");

Assert.IsTrue(created);
Assert.IsTrue(created2);
Expand Down Expand Up @@ -321,24 +321,24 @@ public async Task UIT_WebDavSession_Move()
}

[TestMethod]
public async Task UIT_WebDavSession_Move_WithBracketsDestination()
public async Task UIT_WebDavSession_Move_WithSpecialCharacters()
{
using (var session = CreateWebDavSession())
{
session.BaseUri = new Uri(webDavRootFolder);
var created = await session.CreateDirectoryAsync("test [Source]");
var created2 = await session.CreateDirectoryAsync("test [Source]/folderToMove");
var created3 = await session.CreateDirectoryAsync("test [Destination]");
var items = await session.ListAsync("/test [Source]");
var created = await session.CreateDirectoryAsync("test [Source#]");
var created2 = await session.CreateDirectoryAsync("test [Source#]/folderToMove");
var created3 = await session.CreateDirectoryAsync("test [Destination#]");
var items = await session.ListAsync("/test [Source#]");
Assert.AreEqual(items.Count, 1);
var move = await session.MoveAsync("test [Source]/folderToMove", "test [Destination]/folderToMove", true);
var items2 = await session.ListAsync("/test [Source]");
var move = await session.MoveAsync("test [Source#]/folderToMove", "test [Destination#]/folderToMove", true);
var items2 = await session.ListAsync("/test [Source#]");
Assert.AreEqual(items2.Count, 0);
var items3 = await session.ListAsync("/test [Destination]");
var items3 = await session.ListAsync("/test [Destination#]");
Assert.AreEqual(items3.Count, 1);
Assert.AreEqual("folderToMove", items3[0].Name);
var delete = await session.DeleteAsync("test [Source]");
var delete2 = await session.DeleteAsync("test [Destination]");
var delete = await session.DeleteAsync("test [Source#]");
var delete2 = await session.DeleteAsync("test [Destination#]");

Assert.IsTrue(created);
Assert.IsTrue(created2);
Expand Down
14 changes: 12 additions & 2 deletions UnitTests/DecaTec.WebDav.UnitTest/UnitTestUriHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1676,10 +1676,20 @@ public void UT_UriHelper_EscapeFolders_Uri_DomainOnly()
[TestMethod]
public void UT_UriHelper_EscapeFolders_Uri_WithSpecialCharacters()
{
var url = @"http://google.de/test/test2 [test3]";
var url = @"http://google.de/te#st/test2# [test3]";
var uri = new Uri(url);
var actual = UriHelper.EscapePathSegments(uri);
var expected = @"http://google.de/test/test2%20%5Btest3%5D";
var expected = @"http://google.de/te%23st/test2%23%20%5Btest3%5D";
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void UT_UriHelper_EscapeFolders_Uri_WithPortAndSpecialCharacters()
{
var url = @"http://google.de:8080/te#st/test2# [test3]";
var uri = new Uri(url);
var actual = UriHelper.EscapePathSegments(uri);
var expected = @"http://google.de:8080/te%23st/test2%23%20%5Btest3%5D";
Assert.AreEqual(expected, actual);
}

Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
v1.1.6.0
- The library now supports number signs ('#') in URLs which is usually a special character in URLs (fragment).

v1.1.5.0:
- Bugfix: WebDavClient/WebDavSession methods for copy/move failed when destination Uri contained special characters.
- UriHelper: New method EscapePathSegments which escapes only the path segments of a Uri/URL.
Expand Down

0 comments on commit f450b90

Please sign in to comment.