From ef6b90b8e6e6c317fcda85a392c79324f91250db Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 25 Oct 2016 15:02:04 -0400 Subject: make controller project portable --- .../HttpServer/AsyncStreamWriter.cs | 59 ---------------- .../HttpServer/HttpListenerHost.cs | 27 +++++--- .../HttpServer/HttpResultFactory.cs | 59 ++++++++-------- .../HttpServer/IHttpListener.cs | 2 +- .../HttpServer/RangeRequestWriter.cs | 81 +++------------------- .../HttpServer/ResponseFilter.cs | 21 +++--- .../HttpServer/Security/AuthorizationContext.cs | 4 +- .../HttpServer/Security/SessionAuthProvider.cs | 35 ---------- .../HttpServer/Security/SessionContext.cs | 6 +- .../HttpServer/SocketSharp/HttpUtility.cs | 11 +-- .../HttpServer/SocketSharp/RequestMono.cs | 22 +++--- .../SocketSharp/WebSocketSharpListener.cs | 17 +++-- .../SocketSharp/WebSocketSharpRequest.cs | 40 +++++++---- .../SocketSharp/WebSocketSharpResponse.cs | 47 +++++++------ .../HttpServer/StreamWriter.cs | 58 +++------------- .../HttpServer/SwaggerService.cs | 2 +- 16 files changed, 165 insertions(+), 326 deletions(-) delete mode 100644 MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriter.cs delete mode 100644 MediaBrowser.Server.Implementations/HttpServer/Security/SessionAuthProvider.cs (limited to 'MediaBrowser.Server.Implementations/HttpServer') diff --git a/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriter.cs b/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriter.cs deleted file mode 100644 index e44b0c6af..000000000 --- a/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriter.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Threading.Tasks; -using ServiceStack; -using ServiceStack.Web; -using MediaBrowser.Controller.Net; - -namespace MediaBrowser.Server.Implementations.HttpServer -{ - public class AsyncStreamWriter : IStreamWriter, IAsyncStreamWriter, IHasOptions - { - /// - /// Gets or sets the source stream. - /// - /// The source stream. - private IAsyncStreamSource _source; - - public Action OnComplete { get; set; } - public Action OnError { get; set; } - - /// - /// Initializes a new instance of the class. - /// - public AsyncStreamWriter(IAsyncStreamSource source) - { - _source = source; - } - - public IDictionary Options - { - get - { - var hasOptions = _source as IHasOptions; - if (hasOptions != null) - { - return hasOptions.Options; - } - - return new Dictionary(StringComparer.OrdinalIgnoreCase); - } - } - - /// - /// Writes to. - /// - /// The response stream. - public void WriteTo(Stream responseStream) - { - var task = _source.WriteToAsync(responseStream); - Task.WaitAll(task); - } - - public async Task WriteToAsync(Stream responseStream) - { - await _source.WriteToAsync(responseStream).ConfigureAwait(false); - } - } -} diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs index 21522a9da..999634a92 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -6,10 +6,8 @@ using MediaBrowser.Controller.Net; using MediaBrowser.Model.Logging; using MediaBrowser.Server.Implementations.HttpServer.SocketSharp; using ServiceStack; -using ServiceStack.Api.Swagger; using ServiceStack.Host; using ServiceStack.Host.Handlers; -using ServiceStack.Host.HttpListener; using ServiceStack.Logging; using ServiceStack.Web; using System; @@ -24,6 +22,8 @@ using MediaBrowser.Common.Net; using MediaBrowser.Common.Security; using MediaBrowser.Model.Extensions; using MediaBrowser.Model.IO; +using MediaBrowser.Model.Services; +using ServiceStack.Api.Swagger; namespace MediaBrowser.Server.Implementations.HttpServer { @@ -100,7 +100,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer container.Adapter = _containerAdapter; - Plugins.RemoveAll(x => x is NativeTypesFeature); Plugins.Add(new SwaggerFeature()); Plugins.Add(new CorsFeature(allowedHeaders: "Content-Type, Authorization, Range, X-MediaBrowser-Token, X-Emby-Authorization")); @@ -171,7 +170,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// private void StartListener() { - HostContext.Config.HandlerFactoryPath = ListenerRequest.GetHandlerPathIfAny(UrlPrefixes.First()); + HostContext.Config.HandlerFactoryPath = GetHandlerPathIfAny(UrlPrefixes.First()); _listener = GetListener(); @@ -183,6 +182,18 @@ namespace MediaBrowser.Server.Implementations.HttpServer _listener.Start(UrlPrefixes); } + public static string GetHandlerPathIfAny(string listenerUrl) + { + if (listenerUrl == null) return null; + var pos = listenerUrl.IndexOf("://", StringComparison.OrdinalIgnoreCase); + if (pos == -1) return null; + var startHostUrl = listenerUrl.Substring(pos + "://".Length); + var endPos = startHostUrl.IndexOf('/'); + if (endPos == -1) return null; + var endHostUrl = startHostUrl.Substring(endPos + 1); + return string.IsNullOrEmpty(endHostUrl) ? null : endHostUrl.TrimEnd('/'); + } + private IHttpListener GetListener() { return new WebSocketSharpListener(_logger, CertificatePath, _memoryStreamProvider); @@ -569,28 +580,28 @@ namespace MediaBrowser.Server.Implementations.HttpServer base.Init(); } - public override RouteAttribute[] GetRouteAttributes(Type requestType) + public override Model.Services.RouteAttribute[] GetRouteAttributes(Type requestType) { var routes = base.GetRouteAttributes(requestType).ToList(); var clone = routes.ToList(); foreach (var route in clone) { - routes.Add(new RouteAttribute(NormalizeEmbyRoutePath(route.Path), route.Verbs) + routes.Add(new Model.Services.RouteAttribute(NormalizeEmbyRoutePath(route.Path), route.Verbs) { Notes = route.Notes, Priority = route.Priority, Summary = route.Summary }); - routes.Add(new RouteAttribute(NormalizeRoutePath(route.Path), route.Verbs) + routes.Add(new Model.Services.RouteAttribute(NormalizeRoutePath(route.Path), route.Verbs) { Notes = route.Notes, Priority = route.Priority, Summary = route.Summary }); - routes.Add(new RouteAttribute(DoubleNormalizeEmbyRoutePath(route.Path), route.Verbs) + routes.Add(new Model.Services.RouteAttribute(DoubleNormalizeEmbyRoutePath(route.Path), route.Verbs) { Notes = route.Notes, Priority = route.Priority, diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs index 10d6f7493..de41481cc 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs @@ -2,8 +2,6 @@ using MediaBrowser.Controller.Net; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Serialization; -using ServiceStack; -using ServiceStack.Web; using System; using System.Collections.Generic; using System.Globalization; @@ -11,7 +9,13 @@ using System.IO; using System.Net; using System.Text; using System.Threading.Tasks; -using CommonIO; +using MediaBrowser.Common.IO; +using MediaBrowser.Controller.IO; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Services; +using ServiceStack; +using ServiceStack.Web; +using IRequest = MediaBrowser.Model.Services.IRequest; using MimeTypes = MediaBrowser.Model.Net.MimeTypes; namespace MediaBrowser.Server.Implementations.HttpServer @@ -59,10 +63,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// The content. /// Type of the content. /// The response headers. - /// IHasOptions. - private IHasOptions GetHttpResult(object content, string contentType, IDictionary responseHeaders = null) + /// IHasHeaders. + private IHasHeaders GetHttpResult(object content, string contentType, IDictionary responseHeaders = null) { - IHasOptions result; + IHasHeaders result; var stream = content as Stream; @@ -140,11 +144,11 @@ namespace MediaBrowser.Server.Implementations.HttpServer } // Apply headers - var hasOptions = optimizedResult as IHasOptions; + var hasHeaders = optimizedResult as IHasHeaders; - if (hasOptions != null) + if (hasHeaders != null) { - AddResponseHeaders(hasOptions, responseHeaders); + AddResponseHeaders(hasHeaders, responseHeaders); } return optimizedResult; @@ -237,15 +241,15 @@ namespace MediaBrowser.Server.Implementations.HttpServer result = factoryFn(); // Apply caching headers - var hasOptions = result as IHasOptions; + var hasHeaders = result as IHasHeaders; - if (hasOptions != null) + if (hasHeaders != null) { - AddResponseHeaders(hasOptions, responseHeaders); - return hasOptions; + AddResponseHeaders(hasHeaders, responseHeaders); + return hasHeaders; } - IHasOptions httpResult; + IHasHeaders httpResult; var stream = result as Stream; @@ -298,7 +302,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer public Task GetStaticFileResult(IRequest requestContext, string path, - FileShare fileShare = FileShare.Read) + FileShareMode fileShare = FileShareMode.Read) { if (string.IsNullOrEmpty(path)) { @@ -323,7 +327,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer throw new ArgumentNullException("path"); } - if (fileShare != FileShare.Read && fileShare != FileShare.ReadWrite) + if (fileShare != FileShareMode.Read && fileShare != FileShareMode.ReadWrite) { throw new ArgumentException("FileShare must be either Read or ReadWrite"); } @@ -352,9 +356,9 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// The path. /// The file share. /// Stream. - private Stream GetFileStream(string path, FileShare fileShare) + private Stream GetFileStream(string path, FileShareMode fileShare) { - return _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, fileShare); + return _fileSystem.GetFileStream(path, FileOpenMode.Open, FileAccessMode.Read, fileShare); } public Task GetStaticResult(IRequest requestContext, @@ -404,10 +408,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer } var compress = ShouldCompressResponse(requestContext, contentType); - var hasOptions = await GetStaticResult(requestContext, options, compress).ConfigureAwait(false); - AddResponseHeaders(hasOptions, options.ResponseHeaders); + var hasHeaders = await GetStaticResult(requestContext, options, compress).ConfigureAwait(false); + AddResponseHeaders(hasHeaders, options.ResponseHeaders); - return hasOptions; + return hasHeaders; } /// @@ -461,7 +465,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// private static readonly CultureInfo UsCulture = new CultureInfo("en-US"); - private async Task GetStaticResult(IRequest requestContext, StaticResultOptions options, bool compress) + private async Task GetStaticResult(IRequest requestContext, StaticResultOptions options, bool compress) { var isHeadRequest = options.IsHeadRequest; var factoryFn = options.ContentFactory; @@ -673,19 +677,14 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// /// Adds the response headers. /// - /// The has options. + /// The has options. /// The response headers. - private void AddResponseHeaders(IHasOptions hasOptions, IEnumerable> responseHeaders) + private void AddResponseHeaders(IHasHeaders hasHeaders, IEnumerable> responseHeaders) { foreach (var item in responseHeaders) { - hasOptions.Options[item.Key] = item.Value; + hasHeaders.Headers[item.Key] = item.Value; } } - - public object GetAsyncStreamWriter(IAsyncStreamSource streamSource) - { - return new AsyncStreamWriter(streamSource); - } } } \ No newline at end of file diff --git a/MediaBrowser.Server.Implementations/HttpServer/IHttpListener.cs b/MediaBrowser.Server.Implementations/HttpServer/IHttpListener.cs index dc315601f..7db935d43 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/IHttpListener.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/IHttpListener.cs @@ -1,8 +1,8 @@ using MediaBrowser.Controller.Net; -using ServiceStack.Web; using System; using System.Collections.Generic; using System.Threading.Tasks; +using MediaBrowser.Model.Services; namespace MediaBrowser.Server.Implementations.HttpServer { diff --git a/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs b/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs index 4b94095f5..7d4cd3b4d 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs @@ -5,12 +5,13 @@ using System.Collections.Generic; using System.Globalization; using System.IO; using System.Net; +using System.Threading; using System.Threading.Tasks; -using ServiceStack; +using MediaBrowser.Model.Services; namespace MediaBrowser.Server.Implementations.HttpServer { - public class RangeRequestWriter : IStreamWriter, IAsyncStreamWriter, IHttpResult + public class RangeRequestWriter : IAsyncStreamWriter, IHttpResult { /// /// Gets or sets the source stream. @@ -47,20 +48,11 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// Additional HTTP Headers /// /// The headers. - public Dictionary Headers + public IDictionary Headers { get { return _options; } } - /// - /// Gets the options. - /// - /// The options. - public IDictionary Options - { - get { return Headers; } - } - /// /// Initializes a new instance of the class. /// @@ -81,8 +73,8 @@ namespace MediaBrowser.Server.Implementations.HttpServer this._logger = logger; ContentType = contentType; - Options["Content-Type"] = contentType; - Options["Accept-Ranges"] = "bytes"; + Headers["Content-Type"] = contentType; + Headers["Accept-Ranges"] = "bytes"; StatusCode = HttpStatusCode.PartialContent; Cookies = new List(); @@ -112,8 +104,8 @@ namespace MediaBrowser.Server.Implementations.HttpServer RangeLength = 1 + RangeEnd - RangeStart; // Content-Length is the length of what we're serving, not the original content - Options["Content-Length"] = RangeLength.ToString(UsCulture); - Options["Content-Range"] = string.Format("bytes {0}-{1}/{2}", RangeStart, RangeEnd, TotalContentLength); + Headers["Content-Length"] = RangeLength.ToString(UsCulture); + Headers["Content-Range"] = string.Format("bytes {0}-{1}/{2}", RangeStart, RangeEnd, TotalContentLength); if (RangeStart > 0) { @@ -164,62 +156,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer } } - /// - /// Writes to. - /// - /// The response stream. - public void WriteTo(Stream responseStream) - { - try - { - // Headers only - if (IsHeadRequest) - { - return; - } - - using (var source = SourceStream) - { - // If the requested range is "0-", we can optimize by just doing a stream copy - if (RangeEnd >= TotalContentLength - 1) - { - source.CopyTo(responseStream, BufferSize); - } - else - { - CopyToInternal(source, responseStream, RangeLength); - } - } - } - finally - { - if (OnComplete != null) - { - OnComplete(); - } - } - } - - private void CopyToInternal(Stream source, Stream destination, long copyLength) - { - var array = new byte[BufferSize]; - int count; - while ((count = source.Read(array, 0, array.Length)) != 0) - { - var bytesToCopy = Math.Min(count, copyLength); - - destination.Write(array, 0, Convert.ToInt32(bytesToCopy)); - - copyLength -= bytesToCopy; - - if (copyLength <= 0) - { - break; - } - } - } - - public async Task WriteToAsync(Stream responseStream) + public async Task WriteToAsync(Stream responseStream, CancellationToken cancellationToken) { try { diff --git a/MediaBrowser.Server.Implementations/HttpServer/ResponseFilter.cs b/MediaBrowser.Server.Implementations/HttpServer/ResponseFilter.cs index ee05702f4..f5a11ae1f 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/ResponseFilter.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/ResponseFilter.cs @@ -5,6 +5,7 @@ using System; using System.Globalization; using System.Net; using System.Text; +using MediaBrowser.Model.Services; namespace MediaBrowser.Server.Implementations.HttpServer { @@ -46,21 +47,21 @@ namespace MediaBrowser.Server.Implementations.HttpServer var vary = "Accept-Encoding"; - var hasOptions = dto as IHasOptions; + var hasHeaders = dto as IHasHeaders; var sharpResponse = res as WebSocketSharpResponse; - if (hasOptions != null) + if (hasHeaders != null) { - if (!hasOptions.Options.ContainsKey("Server")) + if (!hasHeaders.Headers.ContainsKey("Server")) { - hasOptions.Options["Server"] = "Mono-HTTPAPI/1.1, UPnP/1.0 DLNADOC/1.50"; - //hasOptions.Options["Server"] = "Mono-HTTPAPI/1.1"; + hasHeaders.Headers["Server"] = "Mono-HTTPAPI/1.1, UPnP/1.0 DLNADOC/1.50"; + //hasHeaders.Headers["Server"] = "Mono-HTTPAPI/1.1"; } // Content length has to be explicitly set on on HttpListenerResponse or it won't be happy string contentLength; - if (hasOptions.Options.TryGetValue("Content-Length", out contentLength) && !string.IsNullOrEmpty(contentLength)) + if (hasHeaders.Headers.TryGetValue("Content-Length", out contentLength) && !string.IsNullOrEmpty(contentLength)) { var length = long.Parse(contentLength, UsCulture); @@ -85,13 +86,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer } } - string hasOptionsVary; - if (hasOptions.Options.TryGetValue("Vary", out hasOptionsVary)) + string hasHeadersVary; + if (hasHeaders.Headers.TryGetValue("Vary", out hasHeadersVary)) { - vary = hasOptionsVary; + vary = hasHeadersVary; } - hasOptions.Options["Vary"] = vary; + hasHeaders.Headers["Vary"] = vary; } //res.KeepAlive = false; diff --git a/MediaBrowser.Server.Implementations/HttpServer/Security/AuthorizationContext.cs b/MediaBrowser.Server.Implementations/HttpServer/Security/AuthorizationContext.cs index bc3e7b163..edbb5e512 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/Security/AuthorizationContext.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/Security/AuthorizationContext.cs @@ -1,10 +1,10 @@ using MediaBrowser.Controller.Connect; using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Security; -using ServiceStack.Web; using System; using System.Collections.Generic; using System.Linq; +using MediaBrowser.Model.Services; namespace MediaBrowser.Server.Implementations.HttpServer.Security { @@ -21,7 +21,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security public AuthorizationInfo GetAuthorizationInfo(object requestContext) { - var req = new ServiceStackServiceRequest((IRequest)requestContext); + var req = new ServiceRequest((IRequest)requestContext); return GetAuthorizationInfo(req); } diff --git a/MediaBrowser.Server.Implementations/HttpServer/Security/SessionAuthProvider.cs b/MediaBrowser.Server.Implementations/HttpServer/Security/SessionAuthProvider.cs deleted file mode 100644 index 7c3173101..000000000 --- a/MediaBrowser.Server.Implementations/HttpServer/Security/SessionAuthProvider.cs +++ /dev/null @@ -1,35 +0,0 @@ -using MediaBrowser.Controller.Net; -using ServiceStack; -using ServiceStack.Auth; - -namespace MediaBrowser.Server.Implementations.HttpServer.Security -{ - public class SessionAuthProvider : CredentialsAuthProvider - { - private readonly ISessionContext _sessionContext; - - public SessionAuthProvider(ISessionContext sessionContext) - { - _sessionContext = sessionContext; - } - - public override bool TryAuthenticate(IServiceBase authService, string userName, string password) - { - return true; - } - - public override bool IsAuthorized(IAuthSession session, IAuthTokens tokens, Authenticate request = null) - { - return true; - } - - protected override void SaveUserAuth(IServiceBase authService, IAuthSession session, IAuthRepository authRepo, IAuthTokens tokens) - { - } - - public override object Authenticate(IServiceBase authService, IAuthSession session, Authenticate request) - { - return base.Authenticate(authService, session, request); - } - } -} diff --git a/MediaBrowser.Server.Implementations/HttpServer/Security/SessionContext.cs b/MediaBrowser.Server.Implementations/HttpServer/Security/SessionContext.cs index a498d32fa..f51ca55a8 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/Security/SessionContext.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/Security/SessionContext.cs @@ -3,8 +3,8 @@ using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Security; using MediaBrowser.Controller.Session; -using ServiceStack.Web; using System.Threading.Tasks; +using MediaBrowser.Model.Services; namespace MediaBrowser.Server.Implementations.HttpServer.Security { @@ -47,7 +47,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security public Task GetSession(object requestContext) { - var req = new ServiceStackServiceRequest((IRequest)requestContext); + var req = new ServiceRequest((IRequest)requestContext); return GetSession(req); } @@ -60,7 +60,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security public Task GetUser(object requestContext) { - var req = new ServiceStackServiceRequest((IRequest)requestContext); + var req = new ServiceRequest((IRequest)requestContext); return GetUser(req); } } diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/HttpUtility.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/HttpUtility.cs index 3ef48d13a..49d6bceb4 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/HttpUtility.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/HttpUtility.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Text; +using MediaBrowser.Model.Services; namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp { @@ -857,28 +858,28 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp return output.ToString(); } - public static NameValueCollection ParseQueryString(string query) + public static QueryParamCollection ParseQueryString(string query) { return ParseQueryString(query, Encoding.UTF8); } - public static NameValueCollection ParseQueryString(string query, Encoding encoding) + public static QueryParamCollection ParseQueryString(string query, Encoding encoding) { if (query == null) throw new ArgumentNullException("query"); if (encoding == null) throw new ArgumentNullException("encoding"); if (query.Length == 0 || (query.Length == 1 && query[0] == '?')) - return new NameValueCollection(); + return new QueryParamCollection(); if (query[0] == '?') query = query.Substring(1); - NameValueCollection result = new HttpQSCollection(); + QueryParamCollection result = new QueryParamCollection(); ParseQueryString(query, encoding, result); return result; } - internal static void ParseQueryString(string query, Encoding encoding, NameValueCollection result) + internal static void ParseQueryString(string query, Encoding encoding, QueryParamCollection result) { if (query.Length == 0) return; diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/RequestMono.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/RequestMono.cs index d20dd7ec0..13ae48cff 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/RequestMono.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/RequestMono.cs @@ -5,8 +5,8 @@ using System.IO; using System.Text; using System.Threading.Tasks; using System.Web; +using MediaBrowser.Model.Services; using ServiceStack; -using ServiceStack.Web; namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp { @@ -83,7 +83,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp } } - public NameValueCollection Form + public QueryParamCollection Form { get { @@ -155,14 +155,15 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp throw new HttpRequestValidationException(msg); } - static void ValidateNameValueCollection(string name, NameValueCollection coll) + static void ValidateNameValueCollection(string name, QueryParamCollection coll) { if (coll == null) return; - foreach (string key in coll.Keys) + foreach (var pair in coll) { - string val = coll[key]; + var key = pair.Name; + var val = pair.Value; if (val != null && val.Length > 0 && IsInvalidString(val)) ThrowValidationException(name, key, val); } @@ -348,7 +349,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp } } } - class WebROCollection : NameValueCollection + class WebROCollection : QueryParamCollection { bool got_id; int id; @@ -369,28 +370,29 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp } public void Protect() { - IsReadOnly = true; + //IsReadOnly = true; } public void Unprotect() { - IsReadOnly = false; + //IsReadOnly = false; } public override string ToString() { StringBuilder result = new StringBuilder(); - foreach (string key in AllKeys) + foreach (var pair in this) { if (result.Length > 0) result.Append('&'); + var key = pair.Name; if (key != null && key.Length > 0) { result.Append(key); result.Append('='); } - result.Append(Get(key)); + result.Append(pair.Value); } return result.ToString(); diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs index 5509eb627..37bd00602 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs @@ -2,8 +2,6 @@ using MediaBrowser.Controller.Net; using MediaBrowser.Model.Logging; using MediaBrowser.Server.Implementations.Logging; -using ServiceStack; -using ServiceStack.Web; using SocketHttpListener.Net; using System; using System.Collections.Generic; @@ -11,6 +9,8 @@ using System.Linq; using System.Threading.Tasks; using MediaBrowser.Common.IO; using MediaBrowser.Model.IO; +using MediaBrowser.Model.Services; +using ServiceStack; namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp { @@ -102,12 +102,19 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp { var endpoint = ctx.Request.RemoteEndPoint.ToString(); var url = ctx.Request.RawUrl; - var queryString = new NameValueCollection(ctx.Request.QueryString ?? new NameValueCollection()); + var queryString = ctx.Request.QueryString ?? new NameValueCollection(); + + var queryParamCollection = new QueryParamCollection(); + + foreach (var key in queryString.AllKeys) + { + queryParamCollection[key] = queryString[key]; + } var connectingArgs = new WebSocketConnectingEventArgs { Url = url, - QueryString = queryString, + QueryString = queryParamCollection, Endpoint = endpoint }; @@ -127,7 +134,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp WebSocketConnected(new WebSocketConnectEventArgs { Url = url, - QueryString = queryString, + QueryString = queryParamCollection, WebSocket = new SharpWebSocket(webSocketContext.WebSocket, _logger), Endpoint = endpoint }); diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs index 59e0b2a9b..2546519f4 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs @@ -1,15 +1,21 @@ using System; using System.Collections.Generic; +using System.Collections.Specialized; using System.IO; using System.Text; using Funq; using MediaBrowser.Common.IO; using MediaBrowser.Model.IO; using MediaBrowser.Model.Logging; +using MediaBrowser.Model.Services; using ServiceStack; using ServiceStack.Host; using ServiceStack.Web; using SocketHttpListener.Net; +using IHttpFile = MediaBrowser.Model.Services.IHttpFile; +using IHttpRequest = MediaBrowser.Model.Services.IHttpRequest; +using IHttpResponse = MediaBrowser.Model.Services.IHttpResponse; +using IResponse = MediaBrowser.Model.Services.IResponse; namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp { @@ -27,8 +33,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp _memoryStreamProvider = memoryStreamProvider; this.request = httpContext.Request; this.response = new WebSocketSharpResponse(logger, httpContext.Response, this); - - this.RequestPreferences = new RequestPreferences(this); } public HttpListenerRequest HttpRequest @@ -53,8 +57,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp public RequestAttributes RequestAttributes { get; set; } - public IRequestPreferences RequestPreferences { get; private set; } - public T TryResolve() { if (typeof(T) == typeof(IHttpRequest)) @@ -324,22 +326,34 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp get { return request.UserAgent; } } - private NameValueCollectionWrapper headers; - public INameValueCollection Headers + private QueryParamCollection headers; + public QueryParamCollection Headers + { + get { return headers ?? (headers = ToQueryParams(request.Headers)); } + } + + private QueryParamCollection queryString; + public QueryParamCollection QueryString { - get { return headers ?? (headers = new NameValueCollectionWrapper(request.Headers)); } + get { return queryString ?? (queryString = MyHttpUtility.ParseQueryString(request.Url.Query)); } } - private NameValueCollectionWrapper queryString; - public INameValueCollection QueryString + private QueryParamCollection formData; + public QueryParamCollection FormData { - get { return queryString ?? (queryString = new NameValueCollectionWrapper(MyHttpUtility.ParseQueryString(request.Url.Query))); } + get { return formData ?? (formData = this.Form); } } - private NameValueCollectionWrapper formData; - public INameValueCollection FormData + private QueryParamCollection ToQueryParams(NameValueCollection collection) { - get { return formData ?? (formData = new NameValueCollectionWrapper(this.Form)); } + var result = new QueryParamCollection(); + + foreach (var key in collection.AllKeys) + { + result[key] = collection[key]; + } + + return result; } public bool IsLocal diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs index a58645ec5..3aae6c9ca 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs @@ -5,20 +5,21 @@ using System.Net; using MediaBrowser.Model.Logging; using ServiceStack; using ServiceStack.Host; -using ServiceStack.Web; using HttpListenerResponse = SocketHttpListener.Net.HttpListenerResponse; +using IHttpResponse = MediaBrowser.Model.Services.IHttpResponse; +using IRequest = MediaBrowser.Model.Services.IRequest; namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp { public class WebSocketSharpResponse : IHttpResponse { private readonly ILogger _logger; - private readonly HttpListenerResponse response; + private readonly HttpListenerResponse _response; public WebSocketSharpResponse(ILogger logger, HttpListenerResponse response, IRequest request) { _logger = logger; - this.response = response; + this._response = response; Items = new Dictionary(); Request = request; } @@ -28,28 +29,28 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp public Dictionary Items { get; private set; } public object OriginalResponse { - get { return response; } + get { return _response; } } public int StatusCode { - get { return this.response.StatusCode; } - set { this.response.StatusCode = value; } + get { return this._response.StatusCode; } + set { this._response.StatusCode = value; } } public string StatusDescription { - get { return this.response.StatusDescription; } - set { this.response.StatusDescription = value; } + get { return this._response.StatusDescription; } + set { this._response.StatusDescription = value; } } public string ContentType { - get { return response.ContentType; } - set { response.ContentType = value; } + get { return _response.ContentType; } + set { _response.ContentType = value; } } - public ICookies Cookies { get; set; } + //public ICookies Cookies { get; set; } public void AddHeader(string name, string value) { @@ -59,22 +60,22 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp return; } - response.AddHeader(name, value); + _response.AddHeader(name, value); } public string GetHeader(string name) { - return response.Headers[name]; + return _response.Headers[name]; } public void Redirect(string url) { - response.Redirect(url); + _response.Redirect(url); } public Stream OutputStream { - get { return response.OutputStream; } + get { return _response.OutputStream; } } public object Dto { get; set; } @@ -82,9 +83,9 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp public void Write(string text) { var bOutput = System.Text.Encoding.UTF8.GetBytes(text); - response.ContentLength64 = bOutput.Length; + _response.ContentLength64 = bOutput.Length; - var outputStream = response.OutputStream; + var outputStream = _response.OutputStream; outputStream.Write(bOutput, 0, bOutput.Length); Close(); } @@ -97,7 +98,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp try { - this.response.CloseOutputStream(_logger); + this._response.CloseOutputStream(_logger); } catch (Exception ex) { @@ -113,7 +114,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp public void Flush() { - response.OutputStream.Flush(); + _response.OutputStream.Flush(); } public bool IsClosed @@ -127,19 +128,19 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp //you can happily set the Content-Length header in Asp.Net //but HttpListener will complain if you do - you have to set ContentLength64 on the response. //workaround: HttpListener throws "The parameter is incorrect" exceptions when we try to set the Content-Length header - response.ContentLength64 = contentLength; + _response.ContentLength64 = contentLength; } public void SetCookie(Cookie cookie) { var cookieStr = cookie.AsHeaderValue(); - response.Headers.Add(HttpHeaders.SetCookie, cookieStr); + _response.Headers.Add(HttpHeaders.SetCookie, cookieStr); } public bool SendChunked { - get { return response.SendChunked; } - set { response.SendChunked = value; } + get { return _response.SendChunked; } + set { _response.SendChunked = value; } } public bool KeepAlive { get; set; } diff --git a/MediaBrowser.Server.Implementations/HttpServer/StreamWriter.cs b/MediaBrowser.Server.Implementations/HttpServer/StreamWriter.cs index 5f122fb96..60d0d7c41 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/StreamWriter.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/StreamWriter.cs @@ -1,19 +1,19 @@ using MediaBrowser.Model.Logging; -using ServiceStack.Web; using System; using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Threading; using System.Threading.Tasks; using MediaBrowser.Common.IO; -using ServiceStack; +using MediaBrowser.Model.Services; namespace MediaBrowser.Server.Implementations.HttpServer { /// /// Class StreamWriter /// - public class StreamWriter : IStreamWriter, IAsyncStreamWriter, IHasOptions + public class StreamWriter : IAsyncStreamWriter, IHasHeaders { private ILogger Logger { get; set; } @@ -33,7 +33,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// Gets the options. /// /// The options. - public IDictionary Options + public IDictionary Headers { get { return _options; } } @@ -58,11 +58,11 @@ namespace MediaBrowser.Server.Implementations.HttpServer SourceStream = source; Logger = logger; - Options["Content-Type"] = contentType; + Headers["Content-Type"] = contentType; if (source.CanSeek) { - Options["Content-Length"] = source.Length.ToString(UsCulture); + Headers["Content-Length"] = source.Length.ToString(UsCulture); } } @@ -83,54 +83,14 @@ namespace MediaBrowser.Server.Implementations.HttpServer _bytes = source; Logger = logger; - Options["Content-Type"] = contentType; + Headers["Content-Type"] = contentType; - Options["Content-Length"] = source.Length.ToString(UsCulture); + Headers["Content-Length"] = source.Length.ToString(UsCulture); } private const int BufferSize = 81920; - /// - /// Writes to. - /// - /// The response stream. - public void WriteTo(Stream responseStream) - { - try - { - if (_bytes != null) - { - responseStream.Write(_bytes, 0, _bytes.Length); - } - else - { - using (var src = SourceStream) - { - src.CopyTo(responseStream, BufferSize); - } - } - } - catch (Exception ex) - { - Logger.ErrorException("Error streaming data", ex); - - if (OnError != null) - { - OnError(); - } - - throw; - } - finally - { - if (OnComplete != null) - { - OnComplete(); - } - } - } - - public async Task WriteToAsync(Stream responseStream) + public async Task WriteToAsync(Stream responseStream, CancellationToken cancellationToken) { try { diff --git a/MediaBrowser.Server.Implementations/HttpServer/SwaggerService.cs b/MediaBrowser.Server.Implementations/HttpServer/SwaggerService.cs index d91f316d6..7d4d0a968 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SwaggerService.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SwaggerService.cs @@ -1,7 +1,7 @@ using MediaBrowser.Controller; using MediaBrowser.Controller.Net; -using ServiceStack.Web; using System.IO; +using MediaBrowser.Model.Services; namespace MediaBrowser.Server.Implementations.HttpServer { -- cgit v1.2.3 From 4b51233cc8faeea344661a2a3427579e534d8ea4 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 26 Oct 2016 02:01:42 -0400 Subject: update plugin interfaces --- MediaBrowser.Api/BaseApiService.cs | 2 +- .../MediaBrowser.Controller.csproj | 1 - MediaBrowser.Controller/Net/IHttpServer.cs | 3 +- MediaBrowser.Controller/Net/IRestfulService.cs | 12 ----- MediaBrowser.Controller/Net/LoggedAttribute.cs | 40 +++++--------- MediaBrowser.Model/MediaBrowser.Model.csproj | 4 ++ MediaBrowser.Model/Plugins/IHasWebPages.cs | 9 ++++ MediaBrowser.Model/Plugins/PluginPageInfo.cs | 9 ++++ MediaBrowser.Model/Reflection/IAssemblyInfo.cs | 10 ++++ MediaBrowser.Model/Services/IRequestFilter.cs | 8 +++ .../MediaBrowser.Providers.csproj | 3 -- .../HttpServer/HttpListenerHost.cs | 17 ++++-- .../HttpServer/ServerFactory.cs | 11 ++-- .../HttpServer/SwaggerService.cs | 2 +- .../MediaBrowser.Server.Implementations.csproj | 1 + .../Reflection/AssemblyInfo.cs | 14 +++++ .../ApplicationHost.cs | 6 ++- .../Api/ConfigurationPageInfo.cs | 12 ++++- MediaBrowser.WebDashboard/Api/DashboardService.cs | 62 ++++++++++++++++++++-- MediaBrowser.WebDashboard/ServerEntryPoint.cs | 5 +- Nuget/MediaBrowser.Common.Internal.nuspec | 4 +- Nuget/MediaBrowser.Common.nuspec | 2 +- Nuget/MediaBrowser.Server.Core.nuspec | 5 +- 23 files changed, 173 insertions(+), 69 deletions(-) delete mode 100644 MediaBrowser.Controller/Net/IRestfulService.cs create mode 100644 MediaBrowser.Model/Plugins/IHasWebPages.cs create mode 100644 MediaBrowser.Model/Plugins/PluginPageInfo.cs create mode 100644 MediaBrowser.Model/Reflection/IAssemblyInfo.cs create mode 100644 MediaBrowser.Model/Services/IRequestFilter.cs create mode 100644 MediaBrowser.Server.Implementations/Reflection/AssemblyInfo.cs (limited to 'MediaBrowser.Server.Implementations/HttpServer') diff --git a/MediaBrowser.Api/BaseApiService.cs b/MediaBrowser.Api/BaseApiService.cs index 0ef06804d..4810d4e9c 100644 --- a/MediaBrowser.Api/BaseApiService.cs +++ b/MediaBrowser.Api/BaseApiService.cs @@ -17,7 +17,7 @@ namespace MediaBrowser.Api /// /// Class BaseApiService /// - public class BaseApiService : IHasResultFactory, IRestfulService, IHasSession + public class BaseApiService : IHasResultFactory, IService, IHasSession { /// /// Gets or sets the logger. diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index 7467fc39e..cf20f7bbb 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -208,7 +208,6 @@ - diff --git a/MediaBrowser.Controller/Net/IHttpServer.cs b/MediaBrowser.Controller/Net/IHttpServer.cs index 97c5dd31b..8c503c199 100644 --- a/MediaBrowser.Controller/Net/IHttpServer.cs +++ b/MediaBrowser.Controller/Net/IHttpServer.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using MediaBrowser.Model.Services; namespace MediaBrowser.Controller.Net { @@ -46,7 +47,7 @@ namespace MediaBrowser.Controller.Net /// /// Inits this instance. /// - void Init(IEnumerable services); + void Init(IEnumerable services); /// /// If set, all requests will respond with this message diff --git a/MediaBrowser.Controller/Net/IRestfulService.cs b/MediaBrowser.Controller/Net/IRestfulService.cs deleted file mode 100644 index 01eefe792..000000000 --- a/MediaBrowser.Controller/Net/IRestfulService.cs +++ /dev/null @@ -1,12 +0,0 @@ -using MediaBrowser.Model.Services; - -namespace MediaBrowser.Controller.Net -{ - /// - /// Interface IRestfulService - /// - [Logged] - public interface IRestfulService : IService - { - } -} diff --git a/MediaBrowser.Controller/Net/LoggedAttribute.cs b/MediaBrowser.Controller/Net/LoggedAttribute.cs index c5b9d1cba..6a2a5e2e3 100644 --- a/MediaBrowser.Controller/Net/LoggedAttribute.cs +++ b/MediaBrowser.Controller/Net/LoggedAttribute.cs @@ -7,12 +7,20 @@ using MediaBrowser.Model.Services; namespace MediaBrowser.Controller.Net { - public class LoggedAttribute : Attribute, IHasRequestFilter + public class LoggedAttribute : IRequestFilter { - public ILogger Logger { get; set; } - public IUserManager UserManager { get; set; } - public ISessionManager SessionManager { get; set; } - public IAuthorizationContext AuthorizationContext { get; set; } + public LoggedAttribute(ILogger logger, IUserManager userManager, ISessionManager sessionManager, IAuthorizationContext authorizationContext) + { + Logger = logger; + UserManager = userManager; + SessionManager = sessionManager; + AuthorizationContext = authorizationContext; + } + + public ILogger Logger { get; private set; } + public IUserManager UserManager { get; private set; } + public ISessionManager SessionManager { get; private set; } + public IAuthorizationContext AuthorizationContext { get; private set; } /// /// The request filter is executed before the service. @@ -20,7 +28,7 @@ namespace MediaBrowser.Controller.Net /// The http request wrapper /// The http response wrapper /// The request DTO - public void RequestFilter(IRequest request, IResponse response, object requestDto) + public void Filter(IRequest request, IResponse response, object requestDto) { var serviceRequest = new ServiceRequest(request); @@ -51,25 +59,5 @@ namespace MediaBrowser.Controller.Net } } } - - /// - /// A new shallow copy of this filter is used on every request. - /// - /// IHasRequestFilter. - public IHasRequestFilter Copy() - { - return this; - } - - /// - /// Order in which Request Filters are executed. - /// <0 Executed before global request filters - /// >0 Executed after global request filters - /// - /// The priority. - public int Priority - { - get { return 0; } - } } } diff --git a/MediaBrowser.Model/MediaBrowser.Model.csproj b/MediaBrowser.Model/MediaBrowser.Model.csproj index 37269473e..dca74298e 100644 --- a/MediaBrowser.Model/MediaBrowser.Model.csproj +++ b/MediaBrowser.Model/MediaBrowser.Model.csproj @@ -167,6 +167,9 @@ + + + @@ -174,6 +177,7 @@ + diff --git a/MediaBrowser.Model/Plugins/IHasWebPages.cs b/MediaBrowser.Model/Plugins/IHasWebPages.cs new file mode 100644 index 000000000..0745c3c60 --- /dev/null +++ b/MediaBrowser.Model/Plugins/IHasWebPages.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace MediaBrowser.Model.Plugins +{ + public interface IHasWebPages + { + IEnumerable GetPages(); + } +} diff --git a/MediaBrowser.Model/Plugins/PluginPageInfo.cs b/MediaBrowser.Model/Plugins/PluginPageInfo.cs new file mode 100644 index 000000000..4b91e0791 --- /dev/null +++ b/MediaBrowser.Model/Plugins/PluginPageInfo.cs @@ -0,0 +1,9 @@ +namespace MediaBrowser.Model.Plugins +{ + public class PluginPageInfo + { + public string Name { get; set; } + + public string EmbeddedResourcePath { get; set; } + } +} diff --git a/MediaBrowser.Model/Reflection/IAssemblyInfo.cs b/MediaBrowser.Model/Reflection/IAssemblyInfo.cs new file mode 100644 index 000000000..1c65985cb --- /dev/null +++ b/MediaBrowser.Model/Reflection/IAssemblyInfo.cs @@ -0,0 +1,10 @@ +using System; +using System.IO; + +namespace MediaBrowser.Model.Reflection +{ + public interface IAssemblyInfo + { + Stream GetManifestResourceStream(Type type, string resource); + } +} diff --git a/MediaBrowser.Model/Services/IRequestFilter.cs b/MediaBrowser.Model/Services/IRequestFilter.cs new file mode 100644 index 000000000..7f6db2e4d --- /dev/null +++ b/MediaBrowser.Model/Services/IRequestFilter.cs @@ -0,0 +1,8 @@ + +namespace MediaBrowser.Model.Services +{ + public interface IRequestFilter + { + void Filter(IRequest request, IResponse response, object requestDto); + } +} diff --git a/MediaBrowser.Providers/MediaBrowser.Providers.csproj b/MediaBrowser.Providers/MediaBrowser.Providers.csproj index d1954a527..3fcdbf1c7 100644 --- a/MediaBrowser.Providers/MediaBrowser.Providers.csproj +++ b/MediaBrowser.Providers/MediaBrowser.Providers.csproj @@ -53,10 +53,7 @@ - - - False diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs index 999634a92..71704f8e2 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -17,9 +17,9 @@ using System.Linq; using System.Reflection; using System.Threading; using System.Threading.Tasks; -using MediaBrowser.Common.IO; using MediaBrowser.Common.Net; using MediaBrowser.Common.Security; +using MediaBrowser.Controller; using MediaBrowser.Model.Extensions; using MediaBrowser.Model.IO; using MediaBrowser.Model.Services; @@ -34,7 +34,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer private readonly ILogger _logger; public IEnumerable UrlPrefixes { get; private set; } - private readonly List _restServices = new List(); + private readonly List _restServices = new List(); private IHttpListener _listener; @@ -49,13 +49,16 @@ namespace MediaBrowser.Server.Implementations.HttpServer private readonly INetworkManager _networkManager; private readonly IMemoryStreamProvider _memoryStreamProvider; - public HttpListenerHost(IApplicationHost applicationHost, + private readonly IServerApplicationHost _appHost; + + public HttpListenerHost(IServerApplicationHost applicationHost, ILogManager logManager, IServerConfigurationManager config, string serviceName, string defaultRedirectPath, INetworkManager networkManager, IMemoryStreamProvider memoryStreamProvider, params Assembly[] assembliesWithServices) : base(serviceName, assembliesWithServices) { + _appHost = applicationHost; DefaultRedirectPath = defaultRedirectPath; _networkManager = networkManager; _memoryStreamProvider = memoryStreamProvider; @@ -116,6 +119,12 @@ namespace MediaBrowser.Server.Implementations.HttpServer // } //}); + var requestFilters = _appHost.GetExports().ToList(); + foreach (var filter in requestFilters) + { + HostContext.GlobalRequestFilters.Add(filter.Filter); + } + HostContext.GlobalResponseFilters.Add(new ResponseFilter(_logger).FilterResponse); } @@ -569,7 +578,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// Adds the rest handlers. /// /// The services. - public void Init(IEnumerable services) + public void Init(IEnumerable services) { _restServices.AddRange(services); diff --git a/MediaBrowser.Server.Implementations/HttpServer/ServerFactory.cs b/MediaBrowser.Server.Implementations/HttpServer/ServerFactory.cs index b2cbf2387..4dff2d5a3 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/ServerFactory.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/ServerFactory.cs @@ -1,6 +1,5 @@ -using MediaBrowser.Common; -using MediaBrowser.Common.IO; -using MediaBrowser.Common.Net; +using MediaBrowser.Common.Net; +using MediaBrowser.Controller; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Net; using MediaBrowser.Model.IO; @@ -18,17 +17,17 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// Creates the server. /// /// IHttpServer. - public static IHttpServer CreateServer(IApplicationHost applicationHost, + public static IHttpServer CreateServer(IServerApplicationHost applicationHost, ILogManager logManager, IServerConfigurationManager config, - INetworkManager _networkmanager, + INetworkManager networkmanager, IMemoryStreamProvider streamProvider, string serverName, string defaultRedirectpath) { LogManager.LogFactory = new ServerLogFactory(logManager); - return new HttpListenerHost(applicationHost, logManager, config, serverName, defaultRedirectpath, _networkmanager, streamProvider); + return new HttpListenerHost(applicationHost, logManager, config, serverName, defaultRedirectpath, networkmanager, streamProvider); } } } diff --git a/MediaBrowser.Server.Implementations/HttpServer/SwaggerService.cs b/MediaBrowser.Server.Implementations/HttpServer/SwaggerService.cs index 7d4d0a968..54ee5fbb2 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SwaggerService.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SwaggerService.cs @@ -5,7 +5,7 @@ using MediaBrowser.Model.Services; namespace MediaBrowser.Server.Implementations.HttpServer { - public class SwaggerService : IHasResultFactory, IRestfulService + public class SwaggerService : IHasResultFactory, IService { private readonly IServerApplicationPaths _appPaths; diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj index fd978f3d6..23d373ac4 100644 --- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj +++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj @@ -270,6 +270,7 @@ + diff --git a/MediaBrowser.Server.Implementations/Reflection/AssemblyInfo.cs b/MediaBrowser.Server.Implementations/Reflection/AssemblyInfo.cs new file mode 100644 index 000000000..ec25e7951 --- /dev/null +++ b/MediaBrowser.Server.Implementations/Reflection/AssemblyInfo.cs @@ -0,0 +1,14 @@ +using System; +using System.IO; +using MediaBrowser.Model.Reflection; + +namespace MediaBrowser.Server.Implementations.Reflection +{ + public class AssemblyInfo : IAssemblyInfo + { + public Stream GetManifestResourceStream(Type type, string resource) + { + return type.Assembly.GetManifestResourceStream(resource); + } + } +} diff --git a/MediaBrowser.Server.Startup.Common/ApplicationHost.cs b/MediaBrowser.Server.Startup.Common/ApplicationHost.cs index 4372dc283..f8294a24d 100644 --- a/MediaBrowser.Server.Startup.Common/ApplicationHost.cs +++ b/MediaBrowser.Server.Startup.Common/ApplicationHost.cs @@ -111,9 +111,12 @@ using MediaBrowser.Model.Activity; using MediaBrowser.Model.Globalization; using MediaBrowser.Model.Net; using MediaBrowser.Model.News; +using MediaBrowser.Model.Reflection; using MediaBrowser.Model.Serialization; +using MediaBrowser.Model.Services; using MediaBrowser.Model.Social; using MediaBrowser.Model.Xml; +using MediaBrowser.Server.Implementations.Reflection; using MediaBrowser.Server.Implementations.Xml; namespace MediaBrowser.Server.Startup.Common @@ -634,6 +637,7 @@ namespace MediaBrowser.Server.Startup.Common RegisterSingleInstance(() => new BdInfoExaminer()); RegisterSingleInstance(new XmlReaderSettingsFactory()); + RegisterSingleInstance(new AssemblyInfo()); UserDataManager = new UserDataManager(LogManager, ServerConfigurationManager); RegisterSingleInstance(UserDataManager); @@ -985,7 +989,7 @@ namespace MediaBrowser.Server.Startup.Common base.FindParts(); - HttpServer.Init(GetExports(false)); + HttpServer.Init(GetExports(false)); ServerManager.AddWebSocketListeners(GetExports(false)); diff --git a/MediaBrowser.WebDashboard/Api/ConfigurationPageInfo.cs b/MediaBrowser.WebDashboard/Api/ConfigurationPageInfo.cs index 16aa14cb7..33289e76c 100644 --- a/MediaBrowser.WebDashboard/Api/ConfigurationPageInfo.cs +++ b/MediaBrowser.WebDashboard/Api/ConfigurationPageInfo.cs @@ -1,4 +1,6 @@ -using MediaBrowser.Controller.Plugins; +using MediaBrowser.Common.Plugins; +using MediaBrowser.Controller.Plugins; +using MediaBrowser.Model.Plugins; namespace MediaBrowser.WebDashboard.Api { @@ -30,5 +32,13 @@ namespace MediaBrowser.WebDashboard.Api // Don't use "N" because it needs to match Plugin.Id PluginId = page.Plugin.Id.ToString(); } + + public ConfigurationPageInfo(IPlugin plugin, PluginPageInfo page) + { + Name = page.Name; + + // Don't use "N" because it needs to match Plugin.Id + PluginId = plugin.Id.ToString(); + } } } diff --git a/MediaBrowser.WebDashboard/Api/DashboardService.cs b/MediaBrowser.WebDashboard/Api/DashboardService.cs index 5af580340..6803ad306 100644 --- a/MediaBrowser.WebDashboard/Api/DashboardService.cs +++ b/MediaBrowser.WebDashboard/Api/DashboardService.cs @@ -12,8 +12,11 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; +using MediaBrowser.Common.Plugins; using MediaBrowser.Model.IO; using MediaBrowser.Model.Globalization; +using MediaBrowser.Model.Plugins; +using MediaBrowser.Model.Reflection; using MediaBrowser.Model.Services; namespace MediaBrowser.WebDashboard.Api @@ -76,7 +79,7 @@ namespace MediaBrowser.WebDashboard.Api /// /// Class DashboardService /// - public class DashboardService : IRestfulService, IHasResultFactory + public class DashboardService : IService, IHasResultFactory { /// /// Gets or sets the logger. @@ -109,6 +112,7 @@ namespace MediaBrowser.WebDashboard.Api private readonly IFileSystem _fileSystem; private readonly ILocalizationManager _localization; private readonly IJsonSerializer _jsonSerializer; + private readonly IAssemblyInfo _assemblyInfo; /// /// Initializes a new instance of the class. @@ -116,13 +120,14 @@ namespace MediaBrowser.WebDashboard.Api /// The app host. /// The server configuration manager. /// The file system. - public DashboardService(IServerApplicationHost appHost, IServerConfigurationManager serverConfigurationManager, IFileSystem fileSystem, ILocalizationManager localization, IJsonSerializer jsonSerializer) + public DashboardService(IServerApplicationHost appHost, IServerConfigurationManager serverConfigurationManager, IFileSystem fileSystem, ILocalizationManager localization, IJsonSerializer jsonSerializer, IAssemblyInfo assemblyInfo) { _appHost = appHost; _serverConfigurationManager = serverConfigurationManager; _fileSystem = fileSystem; _localization = localization; _jsonSerializer = jsonSerializer; + _assemblyInfo = assemblyInfo; } /// @@ -132,9 +137,32 @@ namespace MediaBrowser.WebDashboard.Api /// System.Object. public Task Get(GetDashboardConfigurationPage request) { - var page = ServerEntryPoint.Instance.PluginConfigurationPages.First(p => p.Name.Equals(request.Name, StringComparison.OrdinalIgnoreCase)); + IPlugin plugin = null; + Stream stream = null; - return ResultFactory.GetStaticResult(Request, page.Plugin.Version.ToString().GetMD5(), null, null, MimeTypes.GetMimeType("page.html"), () => GetPackageCreator().ModifyHtml("dummy.html", page.GetHtmlStream(), null, _appHost.ApplicationVersion.ToString(), null, false)); + var page = ServerEntryPoint.Instance.PluginConfigurationPages.FirstOrDefault(p => string.Equals(p.Name, request.Name, StringComparison.OrdinalIgnoreCase)); + if (page != null) + { + plugin = page.Plugin; + stream = page.GetHtmlStream(); + } + + if (plugin == null) + { + var altPage = GetPluginPages().FirstOrDefault(p => string.Equals(p.Item1.Name, request.Name, StringComparison.OrdinalIgnoreCase)); + if (altPage != null) + { + plugin = altPage.Item2; + stream = _assemblyInfo.GetManifestResourceStream(plugin.GetType(), altPage.Item1.EmbeddedResourcePath); + } + } + + if (plugin != null && stream != null) + { + return ResultFactory.GetStaticResult(Request, plugin.Version.ToString().GetMD5(), null, null, MimeTypes.GetMimeType("page.html"), () => GetPackageCreator().ModifyHtml("dummy.html", stream, null, _appHost.ApplicationVersion.ToString(), null, false)); + } + + throw new ResourceNotFoundException(); } /// @@ -162,7 +190,7 @@ namespace MediaBrowser.WebDashboard.Api if (request.PageType.HasValue) { - pages = pages.Where(p => p.ConfigurationPageType == request.PageType.Value); + pages = pages.Where(p => p.ConfigurationPageType == request.PageType.Value).ToList(); } // Don't allow a failing plugin to fail them all @@ -182,9 +210,33 @@ namespace MediaBrowser.WebDashboard.Api .Where(i => i != null) .ToList(); + configPages.AddRange(_appHost.Plugins.SelectMany(GetConfigPages)); + return ResultFactory.GetOptimizedResult(Request, configPages); } + private IEnumerable> GetPluginPages() + { + return _appHost.Plugins.SelectMany(GetPluginPages); + } + + private IEnumerable> GetPluginPages(IPlugin plugin) + { + var hasConfig = plugin as IHasWebPages; + + if (hasConfig == null) + { + return new List>(); + } + + return hasConfig.GetPages().Select(i => new Tuple(i, plugin)); + } + + private IEnumerable GetConfigPages(IPlugin plugin) + { + return GetPluginPages(plugin).Select(i => new ConfigurationPageInfo(plugin, i.Item1)); + } + public object Get(GetRobotsTxt request) { return Get(new GetDashboardResource diff --git a/MediaBrowser.WebDashboard/ServerEntryPoint.cs b/MediaBrowser.WebDashboard/ServerEntryPoint.cs index 690c07d8f..b939e4107 100644 --- a/MediaBrowser.WebDashboard/ServerEntryPoint.cs +++ b/MediaBrowser.WebDashboard/ServerEntryPoint.cs @@ -1,6 +1,7 @@ using MediaBrowser.Common; using MediaBrowser.Controller.Plugins; using System.Collections.Generic; +using System.Linq; namespace MediaBrowser.WebDashboard { @@ -10,7 +11,7 @@ namespace MediaBrowser.WebDashboard /// Gets the list of plugin configuration pages /// /// The configuration pages. - public IEnumerable PluginConfigurationPages { get; private set; } + public List PluginConfigurationPages { get; private set; } private readonly IApplicationHost _appHost; @@ -24,7 +25,7 @@ namespace MediaBrowser.WebDashboard public void Run() { - PluginConfigurationPages = _appHost.GetExports(); + PluginConfigurationPages = _appHost.GetExports().ToList(); } public void Dispose() diff --git a/Nuget/MediaBrowser.Common.Internal.nuspec b/Nuget/MediaBrowser.Common.Internal.nuspec index 57b18c338..292c80a7c 100644 --- a/Nuget/MediaBrowser.Common.Internal.nuspec +++ b/Nuget/MediaBrowser.Common.Internal.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Common.Internal - 3.0.676 + 3.0.680 Emby.Common.Internal Luke ebr,Luke,scottisafool @@ -12,7 +12,7 @@ Contains common components shared by Emby Theater and Emby Server. Not intended for plugin developer consumption. Copyright © Emby 2013 - + diff --git a/Nuget/MediaBrowser.Common.nuspec b/Nuget/MediaBrowser.Common.nuspec index 05c9c4afc..111cb0a2a 100644 --- a/Nuget/MediaBrowser.Common.nuspec +++ b/Nuget/MediaBrowser.Common.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Common - 3.0.676 + 3.0.680 Emby.Common Emby Team ebr,Luke,scottisafool diff --git a/Nuget/MediaBrowser.Server.Core.nuspec b/Nuget/MediaBrowser.Server.Core.nuspec index 67607af50..fec8b7722 100644 --- a/Nuget/MediaBrowser.Server.Core.nuspec +++ b/Nuget/MediaBrowser.Server.Core.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Server.Core - 3.0.676 + 3.0.680 Emby.Server.Core Emby Team ebr,Luke,scottisafool @@ -12,10 +12,11 @@ Contains core components required to build plugins for Emby Server. Copyright © Emby 2013 - + + \ No newline at end of file -- cgit v1.2.3