Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
GH-368: Allow for unicode in host names and escape query (#393)
Browse files Browse the repository at this point in the history
* Allow for unicode in host names and escape query

Previously we were passing the the url as is to each platform and letting it deal with how to parse into the platform specific url.

This handles unicode domain names (IDN Mapping aka PunyCode) for the domain, but also reconstructs the url based on the parsed System.Uri from the original string, and so the PathAndQuery will be the % escaped version which will account for unicode characters in it as well.

Finally, on iOS and Android we are using AbsoluteUri to end up with the fully IDN mapped and % encoded URL to pass to the system URL types.

* Extract Escape Uri logic into its own method

More testable.

* Add uri escaping tests

* Improve uri escape tests

* Update default iOS DeviceTests target

Also don’t allow picking a sim that’s unavailable (this was a bug since we were checking for contains `available` which of course `unavailable` also contains!)
  • Loading branch information
Redth authored and jamesmontemagno committed Jul 19, 2018
1 parent fc0c502 commit 4f9966a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
3 changes: 2 additions & 1 deletion DeviceTests/build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
var TARGET = Argument("target", "Default");

var IOS_SIM_NAME = EnvironmentVariable("IOS_SIM_NAME") ?? "iPhone X";
var IOS_SIM_RUNTIME = EnvironmentVariable("IOS_SIM_RUNTIME") ?? "iOS 11.3";
var IOS_SIM_RUNTIME = EnvironmentVariable("IOS_SIM_RUNTIME") ?? "iOS 11.4";
var IOS_PROJ = "./DeviceTests.iOS/DeviceTests.iOS.csproj";
var IOS_BUNDLE_ID = "com.xamarin.essentials.devicetests";
var IOS_IPA_PATH = "./DeviceTests.iOS/bin/iPhoneSimulator/Release/Xamarin.EssentialsDeviceTestsiOS.app";
Expand Down Expand Up @@ -108,6 +108,7 @@ Task ("test-ios-emu")
// Look for a matching simulator on the system
var sim = ListAppleSimulators ()
.First (s => (s.Availability.Contains("available") || s.Availability.Contains("booted"))
&& !s.Availability.Contains("unavailable")
&& s.Name == IOS_SIM_NAME && s.Runtime == IOS_SIM_RUNTIME);

// Boot the simulator
Expand Down
15 changes: 15 additions & 0 deletions Tests/Browser_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,20 @@ public async Task Open_Uri_NetStandard() =>
[Fact]
public async Task Open_Uri_Launch_NetStandard() =>
await Assert.ThrowsAsync<NotImplementedInReferenceAssemblyException>(() => Browser.OpenAsync(new Uri("http://xamarin.com"), BrowserLaunchType.SystemPreferred));

[Theory]
[InlineData("https://xamarin.com", "https://xamarin.com")]
[InlineData("http://xamarin.com", "http://xamarin.com")]
[InlineData("https://xamariñ.com", "https://xn--xamari-1wa.com")]
[InlineData("http://xamariñ.com", "http://xn--xamari-1wa.com")]
[InlineData("https://xamariñ.com/?test=xamariñ", "https://xn--xamari-1wa.com/?test=xamari%C3%B1")]
[InlineData("http://xamariñ.com/?test=xamariñ", "http://xn--xamari-1wa.com/?test=xamari%C3%B1")]
[InlineData("http://xamariñ.com/?test=xamariñ xamariñ", "http://xn--xamari-1wa.com/?test=xamari%C3%B1%20xamari%C3%B1")]
public void Escape_Uri(string uri, string escaped)
{
var escapedUri = Browser.EscapeUri(new Uri(uri));

Assert.Equal(escaped, escapedUri.AbsoluteUri.TrimEnd('/'));
}
}
}
2 changes: 1 addition & 1 deletion Xamarin.Essentials/Browser/Browser.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static Task PlatformOpenAsync(Uri uri, BrowserLaunchType launchType)
if (uri == null)
throw new ArgumentNullException(nameof(uri));

var nativeUri = AndroidUri.Parse(uri.OriginalString);
var nativeUri = AndroidUri.Parse(uri.AbsoluteUri);

switch (launchType)
{
Expand Down
2 changes: 1 addition & 1 deletion Xamarin.Essentials/Browser/Browser.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static Task PlatformOpenAsync(Uri uri, BrowserLaunchType launchType)
if (uri == null)
throw new ArgumentNullException(nameof(uri));

var nativeUrl = new NSUrl(uri.OriginalString);
var nativeUrl = new NSUrl(uri.AbsoluteUri);

switch (launchType)
{
Expand Down
12 changes: 11 additions & 1 deletion Xamarin.Essentials/Browser/Browser.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@ public static Task OpenAsync(Uri uri) =>
OpenAsync(uri, BrowserLaunchType.SystemPreferred);

public static Task OpenAsync(Uri uri, BrowserLaunchType launchType) =>
PlatformOpenAsync(uri, launchType);
PlatformOpenAsync(EscapeUri(uri), launchType);

internal static Uri EscapeUri(Uri uri)
{
#if NETSTANDARD1_0
return uri;
#else
var idn = new System.Globalization.IdnMapping();
return new Uri(uri.Scheme + "://" + idn.GetAscii(uri.DnsSafeHost) + uri.PathAndQuery);
#endif
}
}

public enum BrowserLaunchType
Expand Down

0 comments on commit 4f9966a

Please sign in to comment.