Skip to content

Commit

Permalink
Update default BlazorWebView host address (#24884)
Browse files Browse the repository at this point in the history
Update default host address to 0.0.0.1.
  • Loading branch information
MackinnonBuck authored and rmarinho committed Sep 30, 2024
1 parent 0b84dad commit fa850a7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 40 deletions.
1 change: 1 addition & 0 deletions Microsoft.Maui-dev.sln
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SharedSource", "SharedSource", "{4F2926C8-43AB-4328-A735-D9EAD699F81D}"
ProjectSection(SolutionItems) = preProject
src\BlazorWebView\src\SharedSource\AutoCloseOnReadCompleteStream.cs = src\BlazorWebView\src\SharedSource\AutoCloseOnReadCompleteStream.cs
src\BlazorWebView\src\SharedSource\HostAddressHelper.cs = src\BlazorWebView\src\SharedSource\HostAddressHelper.cs
src\BlazorWebView\src\SharedSource\QueryStringHelper.cs = src\BlazorWebView\src\SharedSource\QueryStringHelper.cs
src\BlazorWebView\src\SharedSource\UrlLoadingEventArgs.cs = src\BlazorWebView\src\SharedSource\UrlLoadingEventArgs.cs
src\BlazorWebView\src\SharedSource\UrlLoadingStrategy.cs = src\BlazorWebView\src\SharedSource\UrlLoadingStrategy.cs
Expand Down
39 changes: 1 addition & 38 deletions src/BlazorWebView/src/Maui/BlazorWebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,7 @@ namespace Microsoft.AspNetCore.Components.WebView.Maui
/// </summary>
public partial class BlazorWebView : View, IBlazorWebView
{
internal static string AppHostAddress { get; } = GetAppHostAddress();

private const string AppHostAddressAlways0000Switch = "BlazorWebView.AppHostAddressAlways0000";

private static bool IsAppHostAddressAlways0000Enabled =>
AppContext.TryGetSwitch(AppHostAddressAlways0000Switch, out var enabled) && enabled;

private static string GetAppHostAddress()
{
if (IsAppHostAddressAlways0000Enabled)
{
return "0.0.0.0";
}
else
{
#if IOS || MACCATALYST
// On iOS/MacCatalyst 18 and higher the 0.0.0.0 address does not work, so we use localhost instead.
// This preserves behavior on older versions of those systems, while defaulting to new behavior on
// the new system.

// Note that pre-release versions of iOS/MacCatalyst have the expected Major/Minor values,
// but the Build, MajorRevision, MinorRevision, and Revision values are all -1, so we need
// to pass in int.MinValue for those values.

if (System.OperatingSystem.IsIOSVersionAtLeast(major: 18, minor: int.MinValue, build: int.MinValue) ||
System.OperatingSystem.IsMacCatalystVersionAtLeast(major: 18, minor: int.MinValue, build: int.MinValue))
{
return "localhost";
}
else
{
return "0.0.0.0";
}
#else
return "0.0.0.0";
#endif
}
}
internal static string AppHostAddress { get; } = HostAddressHelper.GetAppHostAddress();

private readonly JSComponentConfigurationStore _jSComponents = new();

Expand Down
19 changes: 19 additions & 0 deletions src/BlazorWebView/src/SharedSource/HostAddressHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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;

namespace Microsoft.AspNetCore.Components.WebView;

internal static class HostAddressHelper
{
private const string AppHostAddressAlways0000Switch = "BlazorWebView.AppHostAddressAlways0000";

private static bool IsAppHostAddressAlways0000Enabled =>
AppContext.TryGetSwitch(AppHostAddressAlways0000Switch, out var enabled) && enabled;

public static string GetAppHostAddress()
=> IsAppHostAddressAlways0000Enabled
? "0.0.0.0"
: "0.0.0.1";
}
4 changes: 2 additions & 2 deletions src/BlazorWebView/src/SharedSource/WebView2WebViewManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ internal class WebView2WebViewManager : WebViewManager
// Using an IP address means that WebView2 doesn't wait for any DNS resolution,
// making it substantially faster. Note that this isn't real HTTP traffic, since
// we intercept all the requests within this origin.
internal static readonly string AppHostAddress = "0.0.0.0";
internal static readonly string AppHostAddress = HostAddressHelper.GetAppHostAddress();

/// <summary>
/// Gets the application's base URI. Defaults to <c>https://0.0.0.0/</c>
/// Gets the application's base URI. Defaults to <c>https://0.0.0.1/</c>.
/// </summary>
protected static readonly string AppOrigin = $"https://{AppHostAddress}/";

Expand Down

0 comments on commit fa850a7

Please sign in to comment.