From 3221e837f9758e90b91f0f6760af1c3b67e04c2d Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Sun, 17 Nov 2019 23:05:39 +0100 Subject: * Add support for multi segment base urls * Make baseurl case-insensitive --- MediaBrowser.Api/BaseApiService.cs | 139 +++++++++++++++++++++++++++---------- 1 file changed, 102 insertions(+), 37 deletions(-) (limited to 'MediaBrowser.Api/BaseApiService.cs') diff --git a/MediaBrowser.Api/BaseApiService.cs b/MediaBrowser.Api/BaseApiService.cs index 5f1f6c5b1..41ee314df 100644 --- a/MediaBrowser.Api/BaseApiService.cs +++ b/MediaBrowser.Api/BaseApiService.cs @@ -1,5 +1,7 @@ using System; +using System.IO; using System.Linq; +using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Dto; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Audio; @@ -16,19 +18,35 @@ namespace MediaBrowser.Api /// /// Class BaseApiService /// - public class BaseApiService : IService, IRequiresRequest + public abstract class BaseApiService : IService, IRequiresRequest { + public BaseApiService( + ILogger logger, + IServerConfigurationManager serverConfigurationManager, + IHttpResultFactory httpResultFactory) + { + Logger = logger; + ServerConfigurationManager = serverConfigurationManager; + ResultFactory = httpResultFactory; + } + /// - /// Gets or sets the logger. + /// Gets the logger. /// /// The logger. - public ILogger Logger => ApiEntryPoint.Instance.Logger; + protected ILogger Logger { get; } + + /// + /// Gets or sets the server configuration manager. + /// + /// The server configuration manager. + protected IServerConfigurationManager ServerConfigurationManager { get; } /// - /// Gets or sets the HTTP result factory. + /// Gets the HTTP result factory. /// /// The HTTP result factory. - public IHttpResultFactory ResultFactory => ApiEntryPoint.Instance.ResultFactory; + protected IHttpResultFactory ResultFactory { get; } /// /// Gets or sets the request context. @@ -36,10 +54,7 @@ namespace MediaBrowser.Api /// The request context. public IRequest Request { get; set; } - public string GetHeader(string name) - { - return Request.Headers[name]; - } + public string GetHeader(string name) => Request.Headers[name]; public static string[] SplitValue(string value, char delim) { @@ -292,51 +307,101 @@ namespace MediaBrowser.Api return result; } - protected string GetPathValue(int index) + /// + /// Gets the path segment at the specified index. + /// + /// The index of the path segment. + /// The path segment at the specified index. + /// Path doesn't contain enough segments. + /// Path doesn't start with the base url. + protected internal ReadOnlySpan GetPathValue(int index) { - var pathInfo = Parse(Request.PathInfo); - var first = pathInfo[0]; + static void ThrowIndexOutOfRangeException() + { + throw new IndexOutOfRangeException("Path doesn't contain enough segments."); + } + + static void ThrowInvalidDataException() + { + throw new InvalidDataException("Path doesn't start with the base url."); + } - string baseUrl = ApiEntryPoint.Instance.ConfigurationManager.Configuration.BaseUrl; + ReadOnlySpan path = Request.PathInfo; - // backwards compatibility - if (baseUrl.Length == 0) + // Remove the protocol part from the url + int pos = path.LastIndexOf("://"); + if (pos != -1) { - if (string.Equals(first, "mediabrowser", StringComparison.OrdinalIgnoreCase) - || string.Equals(first, "emby", StringComparison.OrdinalIgnoreCase)) - { - index++; - } + path = path.Slice(pos + 3); } - else if (string.Equals(first, baseUrl.Remove(0, 1))) + + // Remove the query string + pos = path.LastIndexOf('?'); + if (pos != -1) { - index++; - var second = pathInfo[1]; - if (string.Equals(second, "mediabrowser", StringComparison.OrdinalIgnoreCase) - || string.Equals(second, "emby", StringComparison.OrdinalIgnoreCase)) + path = path.Slice(0, pos); + } + + // Remove the domain + pos = path.IndexOf('/'); + if (pos != -1) + { + path = path.Slice(pos); + } + + // Remove base url + string baseUrl = ServerConfigurationManager.Configuration.BaseUrl; + int baseUrlLen = baseUrl.Length; + if (baseUrlLen != 0) + { + if (path.StartsWith(baseUrl, StringComparison.OrdinalIgnoreCase)) { - index++; + path = path.Slice(baseUrlLen); + } + else + { + // The path doesn't start with the base url, + // how did we get here? + ThrowInvalidDataException(); } } - return pathInfo[index]; - } + // Remove leading / + path = path.Slice(1); - private static string[] Parse(string pathUri) - { - var actionParts = pathUri.Split(new[] { "://" }, StringSplitOptions.None); + // Backwards compatibility + const string Emby = "emby/"; + if (path.StartsWith(Emby, StringComparison.OrdinalIgnoreCase)) + { + path = path.Slice(Emby.Length); + } - var pathInfo = actionParts[actionParts.Length - 1]; + const string MediaBrowser = "mediabrowser/"; + if (path.StartsWith(MediaBrowser, StringComparison.OrdinalIgnoreCase)) + { + path = path.Slice(MediaBrowser.Length); + } - var optionsPos = pathInfo.LastIndexOf('?'); - if (optionsPos != -1) + // Skip segments until we are at the right index + for (int i = 0; i < index; i++) { - pathInfo = pathInfo.Substring(0, optionsPos); + pos = path.IndexOf('/'); + if (pos == -1) + { + ThrowIndexOutOfRangeException(); + } + + path = path.Slice(pos + 1); } - var args = pathInfo.Split('/'); + // Remove the rest + pos = path.IndexOf('/'); + if (pos != -1) + { + path = path.Slice(0, pos); + } - return args.Skip(1).ToArray(); + return path; } /// -- cgit v1.2.3 From a6f883345f1941bf99db1667e84c1caf0b370479 Mon Sep 17 00:00:00 2001 From: Bond-009 Date: Thu, 5 Dec 2019 17:44:46 +0100 Subject: Reduce #lines --- Emby.Dlna/Api/DlnaServerService.cs | 8 ++------ MediaBrowser.Api/BaseApiService.cs | 8 ++------ 2 files changed, 4 insertions(+), 12 deletions(-) (limited to 'MediaBrowser.Api/BaseApiService.cs') diff --git a/Emby.Dlna/Api/DlnaServerService.cs b/Emby.Dlna/Api/DlnaServerService.cs index f64c89389..a451bbcf9 100644 --- a/Emby.Dlna/Api/DlnaServerService.cs +++ b/Emby.Dlna/Api/DlnaServerService.cs @@ -216,14 +216,10 @@ namespace Emby.Dlna.Api protected internal ReadOnlySpan GetPathValue(int index) { static void ThrowIndexOutOfRangeException() - { - throw new IndexOutOfRangeException("Path doesn't contain enough segments."); - } + => throw new IndexOutOfRangeException("Path doesn't contain enough segments."); static void ThrowInvalidDataException() - { - throw new InvalidDataException("Path doesn't start with the base url."); - } + => throw new InvalidDataException("Path doesn't start with the base url."); ReadOnlySpan path = Request.PathInfo; diff --git a/MediaBrowser.Api/BaseApiService.cs b/MediaBrowser.Api/BaseApiService.cs index 41ee314df..2b994d279 100644 --- a/MediaBrowser.Api/BaseApiService.cs +++ b/MediaBrowser.Api/BaseApiService.cs @@ -317,14 +317,10 @@ namespace MediaBrowser.Api protected internal ReadOnlySpan GetPathValue(int index) { static void ThrowIndexOutOfRangeException() - { - throw new IndexOutOfRangeException("Path doesn't contain enough segments."); - } + => throw new IndexOutOfRangeException("Path doesn't contain enough segments."); static void ThrowInvalidDataException() - { - throw new InvalidDataException("Path doesn't start with the base url."); - } + => throw new InvalidDataException("Path doesn't start with the base url."); ReadOnlySpan path = Request.PathInfo; -- cgit v1.2.3