From 2a79ae0a6e4deb4f5d1eb83a377d26ff542c88ca Mon Sep 17 00:00:00 2001 From: Joshua Boniface Date: Tue, 8 Oct 2019 15:18:34 -0400 Subject: [PATCH 1/4] Normalize baseUrl behaviour Fully normalizes the baseUrl behaviour to better match how this sort of feature works in other programs. 1. The baseUrl is always appended to paths, even the built-in `/emby` and `/mediabrowser` paths. 2. The baseUrl is set statically at class instance creation, to ensure it persists through changes until the next restart. 3. Configuration is normalized using a function when set, to ensure it's in a standard `/mypath` format with leading `/`. 4. Cleans up the conditionals around default redirects. For sanity after changing the URL, it will match *any* path that doesn't match the current baseUrl and redirect it back to the main page (with baseUrl). 5. Adds a second method, NormalizeUrlPath, to avoid lots of `+ "/" +` string manipulations which are unclean - we should always have a leading slash. 6. Sets the default baseUrl to an empty string to avoid unexpected behaviour, though this would be worked-around automatically. 7. Adds some debug logs whenever a URL is normalized, to help track down issues with this code (if any arise). --- .../HttpServer/HttpListenerHost.cs | 73 +++++++++---------- .../Configuration/ServerConfiguration.cs | 34 ++++++++- 2 files changed, 66 insertions(+), 41 deletions(-) diff --git a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs index d60f5c0556..36753a9a12 100644 --- a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -39,6 +39,7 @@ namespace Emby.Server.Implementations.HttpServer private readonly IHttpListener _socketListener; private readonly Func> _funcParseFn; private readonly string _defaultRedirectPath; + private readonly string _baseUrlPrefix; private readonly Dictionary ServiceOperationsMap = new Dictionary(); private IWebSocketListener[] _webSocketListeners = Array.Empty(); private readonly List _webSocketConnections = new List(); @@ -58,6 +59,7 @@ namespace Emby.Server.Implementations.HttpServer _logger = logger; _config = config; _defaultRedirectPath = configuration["HttpListenerHost:DefaultRedirectPath"]; + _baseUrlPrefix = _config.Configuration.BaseUrl; _networkManager = networkManager; _jsonSerializer = jsonSerializer; _xmlSerializer = xmlSerializer; @@ -87,6 +89,20 @@ namespace Emby.Server.Implementations.HttpServer return _appHost.CreateInstance(type); } + private string NormalizeUrlPath(string path) + { + if (path.StartsWith("/")) + { + // If the path begins with a leading slash, just return it as-is + return path; + } + else + { + // If the path does not begin with a leading slash, append one for consistency + return "/" + path; + } + } + /// /// Applies the request filters. Returns whether or not the request has been handled /// and no more processing should be done. @@ -471,22 +487,15 @@ namespace Emby.Server.Implementations.HttpServer urlToLog = GetUrlToLog(urlString); - if (string.Equals(localPath, "/" + _config.Configuration.BaseUrl + "/", StringComparison.OrdinalIgnoreCase) - || string.Equals(localPath, "/" + _config.Configuration.BaseUrl, StringComparison.OrdinalIgnoreCase)) + if (string.Equals(localPath, _baseUrlPrefix + "/", StringComparison.OrdinalIgnoreCase) + || string.Equals(localPath, _baseUrlPrefix, StringComparison.OrdinalIgnoreCase) + || string.Equals(localPath, "/", StringComparison.OrdinalIgnoreCase) + || string.IsNullOrEmpty(localPath) + || !localPath.StartsWith(_baseUrlPrefix)) { - httpRes.Redirect("/" + _config.Configuration.BaseUrl + "/" + _defaultRedirectPath); - return; - } - - if (string.Equals(localPath, "/", StringComparison.OrdinalIgnoreCase)) - { - httpRes.Redirect(_defaultRedirectPath); - return; - } - - if (string.IsNullOrEmpty(localPath)) - { - httpRes.Redirect("/" + _defaultRedirectPath); + // Always redirect back to the default path if the base prefix is invalid or missing + _logger.LogDebug("Normalizing a URL at {0}", localPath); + httpRes.Redirect(_baseUrlPrefix + "/" + _defaultRedirectPath); return; } @@ -596,7 +605,7 @@ namespace Emby.Server.Implementations.HttpServer foreach (var route in clone) { - routes.Add(new RouteAttribute(NormalizeCustomRoutePath(_config.Configuration.BaseUrl, route.Path), route.Verbs) + routes.Add(new RouteAttribute(NormalizeCustomRoutePath(route.Path), route.Verbs) { Notes = route.Notes, Priority = route.Priority, @@ -651,36 +660,22 @@ namespace Emby.Server.Implementations.HttpServer return _socketListener.ProcessWebSocketRequest(context); } - // this method was left for compatibility with third party clients - private static string NormalizeEmbyRoutePath(string path) + private string NormalizeEmbyRoutePath(string path) { - if (path.StartsWith("/", StringComparison.OrdinalIgnoreCase)) - { - return "/emby" + path; - } - - return "emby/" + path; + _logger.LogDebug("Normalizing /emby route"); + return _baseUrlPrefix + "/emby" + NormalizeUrlPath(path); } - // this method was left for compatibility with third party clients - private static string NormalizeMediaBrowserRoutePath(string path) + private string NormalizeMediaBrowserRoutePath(string path) { - if (path.StartsWith("/", StringComparison.OrdinalIgnoreCase)) - { - return "/mediabrowser" + path; - } - - return "mediabrowser/" + path; + _logger.LogDebug("Normalizing /mediabrowser route"); + return _baseUrlPrefix + "/mediabrowser" + NormalizeUrlPath(path); } - private static string NormalizeCustomRoutePath(string baseUrl, string path) + private string NormalizeCustomRoutePath(string path) { - if (path.StartsWith("/", StringComparison.OrdinalIgnoreCase)) - { - return "/" + baseUrl + path; - } - - return baseUrl + "/" + path; + _logger.LogDebug("Normalizing custom route {0}", path); + return _baseUrlPrefix + NormalizeUrlPath(path); } /// diff --git a/MediaBrowser.Model/Configuration/ServerConfiguration.cs b/MediaBrowser.Model/Configuration/ServerConfiguration.cs index 24e7714037..93993bec1a 100644 --- a/MediaBrowser.Model/Configuration/ServerConfiguration.cs +++ b/MediaBrowser.Model/Configuration/ServerConfiguration.cs @@ -10,6 +10,7 @@ namespace MediaBrowser.Model.Configuration { public const int DefaultHttpPort = 8096; public const int DefaultHttpsPort = 8920; + private string _baseUrl; /// /// Gets or sets a value indicating whether [enable u pn p]. @@ -162,7 +163,36 @@ namespace MediaBrowser.Model.Configuration public bool SkipDeserializationForBasicTypes { get; set; } public string ServerName { get; set; } - public string BaseUrl { get; set; } + public string BaseUrl + { + get => _baseUrl; + set + { + _baseUrl = value; + // Normalize the start of the string + if (string.IsNullOrWhiteSpace(_baseUrl)) + { + // If baseUrl is empty, set an empty prefix string + _baseUrl = string.Empty; + } + else if (!_baseUrl.StartsWith("/")) + { + // If baseUrl was not configured with a leading slash, append one for consistency + _baseUrl = "/" + _baseUrl; + } + else + { + // If baseUrl was configured with a leading slash, just return it as-is + _baseUrl = _baseUrl; + } + // Normalize the end of the string + if (_baseUrl.EndsWith("/")) + { + // If baseUrl was configured with a trailing slash, remove it for consistency + _baseUrl = _baseUrl.Remove(_baseUrl.Length - 1); + } + } + } public string UICulture { get; set; } @@ -243,7 +273,7 @@ namespace MediaBrowser.Model.Configuration SortRemoveCharacters = new[] { ",", "&", "-", "{", "}", "'" }; SortRemoveWords = new[] { "the", "a", "an" }; - BaseUrl = "jellyfin"; + BaseUrl = string.Empty; UICulture = "en-US"; MetadataOptions = new[] From b96079fee6c8cf8919eb1f531bdfaac3db3fb2b8 Mon Sep 17 00:00:00 2001 From: Joshua Boniface Date: Wed, 9 Oct 2019 09:22:55 -0400 Subject: [PATCH 2/4] Make NormalizeUrlPath static --- Emby.Server.Implementations/HttpServer/HttpListenerHost.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs index 36753a9a12..ecd35a1a84 100644 --- a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -89,7 +89,7 @@ namespace Emby.Server.Implementations.HttpServer return _appHost.CreateInstance(type); } - private string NormalizeUrlPath(string path) + private static string NormalizeUrlPath(string path) { if (path.StartsWith("/")) { From b10e06ff459476042cff0eec5ebea3c304e6d1f5 Mon Sep 17 00:00:00 2001 From: Joshua Boniface Date: Wed, 9 Oct 2019 10:36:40 -0400 Subject: [PATCH 3/4] Fix spacing issues --- MediaBrowser.Model/Configuration/ServerConfiguration.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/MediaBrowser.Model/Configuration/ServerConfiguration.cs b/MediaBrowser.Model/Configuration/ServerConfiguration.cs index 93993bec1a..b0a96aa037 100644 --- a/MediaBrowser.Model/Configuration/ServerConfiguration.cs +++ b/MediaBrowser.Model/Configuration/ServerConfiguration.cs @@ -169,7 +169,8 @@ namespace MediaBrowser.Model.Configuration set { _baseUrl = value; - // Normalize the start of the string + + // Normalize the start of the string if (string.IsNullOrWhiteSpace(_baseUrl)) { // If baseUrl is empty, set an empty prefix string @@ -185,6 +186,7 @@ namespace MediaBrowser.Model.Configuration // If baseUrl was configured with a leading slash, just return it as-is _baseUrl = _baseUrl; } + // Normalize the end of the string if (_baseUrl.EndsWith("/")) { From 345a14ff5543256a4fa5c6a55f76d77334972c67 Mon Sep 17 00:00:00 2001 From: Joshua Boniface Date: Wed, 9 Oct 2019 10:52:51 -0400 Subject: [PATCH 4/4] Use value instead of assigning baseUrl first --- .../Configuration/ServerConfiguration.cs | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/MediaBrowser.Model/Configuration/ServerConfiguration.cs b/MediaBrowser.Model/Configuration/ServerConfiguration.cs index b0a96aa037..b8abe49e3e 100644 --- a/MediaBrowser.Model/Configuration/ServerConfiguration.cs +++ b/MediaBrowser.Model/Configuration/ServerConfiguration.cs @@ -168,31 +168,26 @@ namespace MediaBrowser.Model.Configuration get => _baseUrl; set { - _baseUrl = value; - // Normalize the start of the string - if (string.IsNullOrWhiteSpace(_baseUrl)) + if (string.IsNullOrWhiteSpace(value)) { // If baseUrl is empty, set an empty prefix string - _baseUrl = string.Empty; + value = string.Empty; } - else if (!_baseUrl.StartsWith("/")) + else if (!value.StartsWith("/")) { // If baseUrl was not configured with a leading slash, append one for consistency - _baseUrl = "/" + _baseUrl; - } - else - { - // If baseUrl was configured with a leading slash, just return it as-is - _baseUrl = _baseUrl; + value = "/" + value; } // Normalize the end of the string - if (_baseUrl.EndsWith("/")) + if (value.EndsWith("/")) { // If baseUrl was configured with a trailing slash, remove it for consistency - _baseUrl = _baseUrl.Remove(_baseUrl.Length - 1); + value = value.Remove(value.Length - 1); } + + _baseUrl = value; } }