Add GetLoopbackHttpApiUrl() helper method to replace forceHttps functionality

Also refactor to use return a Uri instead of a string and use UriBuilder under the hood
This commit is contained in:
Mark Monteiro 2020-05-10 14:36:11 -04:00
parent 15fd4812f0
commit 43c22a5822
6 changed files with 38 additions and 23 deletions

View File

@ -1229,28 +1229,28 @@ namespace Emby.Server.Implementations
str.CopyTo(span.Slice(1)); str.CopyTo(span.Slice(1));
span[^1] = ']'; span[^1] = ']';
return GetLocalApiUrl(span); return GetLocalApiUrl(span).ToString();
} }
return GetLocalApiUrl(ipAddress.ToString()); return GetLocalApiUrl(ipAddress.ToString()).ToString();
} }
/// <inheritdoc/> /// <inheritdoc/>
public string GetLocalApiUrl(ReadOnlySpan<char> host) public Uri GetLoopbackHttpApiUrl()
{ {
var url = new StringBuilder(64); return GetLocalApiUrl("127.0.0.1", Uri.UriSchemeHttp, HttpPort);
url.Append(ListenWithHttps ? "https://" : "http://")
.Append(host)
.Append(':')
.Append(ListenWithHttps ? HttpsPort : HttpPort);
string baseUrl = ServerConfigurationManager.Configuration.BaseUrl;
if (baseUrl.Length != 0)
{
url.Append(baseUrl);
} }
return url.ToString(); /// <inheritdoc/>
public Uri GetLocalApiUrl(ReadOnlySpan<char> host, string scheme = null, int? port = null)
{
return new UriBuilder
{
Scheme = scheme ?? (ListenWithHttps ? Uri.UriSchemeHttps : Uri.UriSchemeHttp),
Host = host.ToString(),
Port = port ?? (ListenWithHttps ? HttpsPort : HttpPort),
Path = ServerConfigurationManager.Configuration.BaseUrl
}.Uri;
} }
public Task<List<IPAddress>> GetLocalIpAddresses(CancellationToken cancellationToken) public Task<List<IPAddress>> GetLocalIpAddresses(CancellationToken cancellationToken)

View File

@ -36,7 +36,7 @@ namespace Emby.Server.Implementations.Browser
{ {
try try
{ {
string baseUrl = appHost.GetLocalApiUrl("localhost"); Uri baseUrl = appHost.GetLocalApiUrl("localhost");
appHost.LaunchUrl(baseUrl + url); appHost.LaunchUrl(baseUrl + url);
} }
catch (Exception ex) catch (Exception ex)

View File

@ -1059,7 +1059,7 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
{ {
var stream = new MediaSourceInfo var stream = new MediaSourceInfo
{ {
EncoderPath = _appHost.GetLocalApiUrl("127.0.0.1") + "/LiveTv/LiveRecordings/" + info.Id + "/stream", EncoderPath = _appHost.GetLoopbackHttpApiUrl() + "/LiveTv/LiveRecordings/" + info.Id + "/stream",
EncoderProtocol = MediaProtocol.Http, EncoderProtocol = MediaProtocol.Http,
Path = info.Path, Path = info.Path,
Protocol = MediaProtocol.File, Protocol = MediaProtocol.File,

View File

@ -121,7 +121,7 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
//OpenedMediaSource.Path = tempFile; //OpenedMediaSource.Path = tempFile;
//OpenedMediaSource.ReadAtNativeFramerate = true; //OpenedMediaSource.ReadAtNativeFramerate = true;
MediaSource.Path = _appHost.GetLocalApiUrl("127.0.0.1") + "/LiveTv/LiveStreamFiles/" + UniqueId + "/stream.ts"; MediaSource.Path = _appHost.GetLoopbackHttpApiUrl() + "/LiveTv/LiveStreamFiles/" + UniqueId + "/stream.ts";
MediaSource.Protocol = MediaProtocol.Http; MediaSource.Protocol = MediaProtocol.Http;
//OpenedMediaSource.SupportsDirectPlay = false; //OpenedMediaSource.SupportsDirectPlay = false;
//OpenedMediaSource.SupportsDirectStream = true; //OpenedMediaSource.SupportsDirectStream = true;

View File

@ -106,7 +106,7 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts
//OpenedMediaSource.Path = tempFile; //OpenedMediaSource.Path = tempFile;
//OpenedMediaSource.ReadAtNativeFramerate = true; //OpenedMediaSource.ReadAtNativeFramerate = true;
MediaSource.Path = _appHost.GetLocalApiUrl("127.0.0.1") + "/LiveTv/LiveStreamFiles/" + UniqueId + "/stream.ts"; MediaSource.Path = _appHost.GetLoopbackHttpApiUrl() + "/LiveTv/LiveStreamFiles/" + UniqueId + "/stream.ts";
MediaSource.Protocol = MediaProtocol.Http; MediaSource.Protocol = MediaProtocol.Http;
//OpenedMediaSource.Path = TempFilePath; //OpenedMediaSource.Path = TempFilePath;

View File

@ -65,26 +65,41 @@ namespace MediaBrowser.Controller
/// <summary> /// <summary>
/// Gets a local (LAN) URL that can be used to access the API. The hostname used is the first valid configured /// Gets a local (LAN) URL that can be used to access the API. The hostname used is the first valid configured
/// IP address that can be found via <see cref="GetLocalIpAddresses"/>. /// IP address that can be found via <see cref="GetLocalIpAddresses"/>. HTTPS will be preferred when available.
/// </summary> /// </summary>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the task.</param> /// <param name="cancellationToken">A cancellation token that can be used to cancel the task.</param>
/// <returns>The server URL.</returns> /// <returns>The server URL.</returns>
Task<string> GetLocalApiUrl(CancellationToken cancellationToken); Task<string> GetLocalApiUrl(CancellationToken cancellationToken);
/// <summary> /// <summary>
/// Gets a local (LAN) URL that can be used to access the API. /// Gets a local (LAN) URL that can be used to access the API using the loop-back IP address (127.0.0.1)
/// over HTTP (not HTTPS).
/// </summary> /// </summary>
/// <param name="hostname">The hostname to use in the URL.</param>
/// <returns>The API URL.</returns> /// <returns>The API URL.</returns>
string GetLocalApiUrl(ReadOnlySpan<char> hostname); public Uri GetLoopbackHttpApiUrl();
/// <summary> /// <summary>
/// Gets a local (LAN) URL that can be used to access the API. /// Gets a local (LAN) URL that can be used to access the API. HTTPS will be preferred when available.
/// </summary> /// </summary>
/// <param name="address">The IP address to use as the hostname in the URL.</param> /// <param name="address">The IP address to use as the hostname in the URL.</param>
/// <returns>The API URL.</returns> /// <returns>The API URL.</returns>
string GetLocalApiUrl(IPAddress address); string GetLocalApiUrl(IPAddress address);
/// <summary>
/// Gets a local (LAN) URL that can be used to access the API.
/// </summary>
/// <param name="hostname">The hostname to use in the URL.</param>
/// <param name="scheme">
/// The scheme to use for the URL. If null, the scheme will be selected automatically,
/// preferring HTTPS, if available.
/// </param>
/// <param name="port">
/// The port to use for the URL. If null, the port will be selected automatically,
/// preferring the HTTPS port, if available.
/// </param>
/// <returns>The API URL.</returns>
Uri 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.
/// </summary> /// </summary>