Do not include a double slash in URLs when a base URL is not set

pull/2774/head
Mark Monteiro 5 years ago
parent dc5165b97f
commit 3abf870c1e

@ -1236,28 +1236,30 @@ namespace Emby.Server.Implementations
str.CopyTo(span.Slice(1)); str.CopyTo(span.Slice(1));
span[^1] = ']'; span[^1] = ']';
return GetLocalApiUrl(span).ToString(); return GetLocalApiUrl(span);
} }
return GetLocalApiUrl(ipAddress.ToString()).ToString(); return GetLocalApiUrl(ipAddress.ToString());
} }
/// <inheritdoc/> /// <inheritdoc/>
public Uri GetLoopbackHttpApiUrl() public string GetLoopbackHttpApiUrl()
{ {
return GetLocalApiUrl("127.0.0.1", Uri.UriSchemeHttp, HttpPort); return GetLocalApiUrl("127.0.0.1", Uri.UriSchemeHttp, HttpPort);
} }
/// <inheritdoc/> /// <inheritdoc/>
public Uri GetLocalApiUrl(ReadOnlySpan<char> host, string scheme = null, int? port = null) public string GetLocalApiUrl(ReadOnlySpan<char> host, string scheme = null, int? port = null)
{ {
// NOTE: If no BaseUrl is set then UriBuilder appends a trailing slash, but if there is no BaseUrl it does
// not. For consistency, always trim the trailing slash.
return new UriBuilder return new UriBuilder
{ {
Scheme = scheme ?? (ListenWithHttps ? Uri.UriSchemeHttps : Uri.UriSchemeHttp), Scheme = scheme ?? (ListenWithHttps ? Uri.UriSchemeHttps : Uri.UriSchemeHttp),
Host = host.ToString(), Host = host.ToString(),
Port = port ?? (ListenWithHttps ? HttpsPort : HttpPort), Port = port ?? (ListenWithHttps ? HttpsPort : HttpPort),
Path = ServerConfigurationManager.Configuration.BaseUrl Path = ServerConfigurationManager.Configuration.BaseUrl
}.Uri; }.ToString().TrimEnd('/');
} }
public Task<List<IPAddress>> GetLocalIpAddresses(CancellationToken cancellationToken) public Task<List<IPAddress>> GetLocalIpAddresses(CancellationToken cancellationToken)
@ -1333,8 +1335,7 @@ namespace Emby.Server.Implementations
return true; return true;
} }
var apiUrl = GetLocalApiUrl(address); var apiUrl = GetLocalApiUrl(address) + "/system/ping";
apiUrl += "/system/ping";
if (_validAddressResults.TryGetValue(apiUrl, out var cachedResult)) if (_validAddressResults.TryGetValue(apiUrl, out var cachedResult))
{ {

@ -31,18 +31,18 @@ namespace Emby.Server.Implementations.Browser
/// Opens the specified URL in an external browser window. Any exceptions will be logged, but ignored. /// Opens the specified URL in an external browser window. Any exceptions will be logged, but ignored.
/// </summary> /// </summary>
/// <param name="appHost">The application host.</param> /// <param name="appHost">The application host.</param>
/// <param name="url">The URL.</param> /// <param name="relativeUrl">The URL to open, relative to the server base URL.</param>
private static void TryOpenUrl(IServerApplicationHost appHost, string url) private static void TryOpenUrl(IServerApplicationHost appHost, string relativeUrl)
{ {
try try
{ {
Uri baseUrl = appHost.GetLocalApiUrl("localhost"); string baseUrl = appHost.GetLocalApiUrl("localhost");
appHost.LaunchUrl(baseUrl + url); appHost.LaunchUrl(baseUrl + relativeUrl);
} }
catch (Exception ex) catch (Exception ex)
{ {
var logger = appHost.Resolve<ILogger>(); var logger = appHost.Resolve<ILogger>();
logger?.LogError(ex, "Failed to open browser window with URL {URL}", url); logger?.LogError(ex, "Failed to open browser window with URL {URL}", relativeUrl);
} }
} }
} }

@ -76,7 +76,7 @@ namespace MediaBrowser.Controller
/// over HTTP (not HTTPS). /// over HTTP (not HTTPS).
/// </summary> /// </summary>
/// <returns>The API URL.</returns> /// <returns>The API URL.</returns>
public Uri GetLoopbackHttpApiUrl(); string GetLoopbackHttpApiUrl();
/// <summary> /// <summary>
/// Gets a local (LAN) URL that can be used to access the API. HTTPS will be preferred when available. /// Gets a local (LAN) URL that can be used to access the API. HTTPS will be preferred when available.
@ -98,7 +98,7 @@ namespace MediaBrowser.Controller
/// preferring the HTTPS port, if available. /// preferring the HTTPS port, if available.
/// </param> /// </param>
/// <returns>The API URL.</returns> /// <returns>The API URL.</returns>
Uri GetLocalApiUrl(ReadOnlySpan<char> hostname, string scheme = null, int? port = null); string GetLocalApiUrl(ReadOnlySpan<char> hostname, string scheme = null, int? port = null);
/// <summary> /// <summary>
/// Open a URL in an external browser window. /// Open a URL in an external browser window.

Loading…
Cancel
Save