diff options
Diffstat (limited to 'MediaBrowser.Server.Implementations/HttpServer')
4 files changed, 39 insertions, 13 deletions
diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs index 62a43751d..94715b490 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -76,7 +76,8 @@ namespace MediaBrowser.Server.Implementations.HttpServer {typeof (InvalidOperationException), 422}, {typeof (ResourceNotFoundException), 404}, {typeof (FileNotFoundException), 404}, - {typeof (DirectoryNotFoundException), 404} + {typeof (DirectoryNotFoundException), 404}, + {typeof (Implementations.Security.AuthenticationException), 401} }; HostConfig.Instance.DebugMode = true; @@ -157,7 +158,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer HostContext.Config.HandlerFactoryPath = ListenerRequest.GetHandlerPathIfAny(UrlPrefixes.First()); _listener = NativeWebSocket.IsSupported - ? _listener = new HttpListenerServer(_logger, _threadPoolManager) + ? _listener = new WebSocketSharpListener(_logger, _threadPoolManager) : _listener = new WebSocketSharpListener(_logger, _threadPoolManager); _listener.WebSocketHandler = WebSocketHandler; diff --git a/MediaBrowser.Server.Implementations/HttpServer/NativeWebSocket.cs b/MediaBrowser.Server.Implementations/HttpServer/NativeWebSocket.cs index f89cdac47..c7669fecb 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/NativeWebSocket.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/NativeWebSocket.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Common.Events; +using System.Text; +using MediaBrowser.Common.Events; using MediaBrowser.Common.Net; using MediaBrowser.Model.Logging; using System; @@ -36,7 +37,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// <param name="socket">The socket.</param> /// <param name="logger">The logger.</param> /// <exception cref="System.ArgumentNullException">socket</exception> - public NativeWebSocket(System.Net.WebSockets.WebSocket socket, ILogger logger) + public NativeWebSocket(WebSocket socket, ILogger logger) { if (socket == null) { @@ -155,6 +156,22 @@ namespace MediaBrowser.Server.Implementations.HttpServer return WebSocket.SendAsync(new ArraySegment<byte>(bytes), nativeType, true, linkedTokenSource.Token); } + public Task SendAsync(byte[] bytes, bool endOfMessage, CancellationToken cancellationToken) + { + var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _cancellationTokenSource.Token); + + return WebSocket.SendAsync(new ArraySegment<byte>(bytes), System.Net.WebSockets.WebSocketMessageType.Binary, true, linkedTokenSource.Token); + } + + public Task SendAsync(string text, bool endOfMessage, CancellationToken cancellationToken) + { + var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _cancellationTokenSource.Token); + + var bytes = Encoding.UTF8.GetBytes(text); + + return WebSocket.SendAsync(new ArraySegment<byte>(bytes), System.Net.WebSockets.WebSocketMessageType.Text, true, linkedTokenSource.Token); + } + /// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> diff --git a/MediaBrowser.Server.Implementations/HttpServer/Security/AuthService.cs b/MediaBrowser.Server.Implementations/HttpServer/Security/AuthService.cs index 1933cc716..ebb5ab741 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/Security/AuthService.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/Security/AuthService.cs @@ -74,7 +74,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security if (user != null && user.Configuration.IsDisabled) { - throw new UnauthorizedAccessException("User account has been disabled."); + throw new AuthenticationException("User account has been disabled."); } if (!string.IsNullOrWhiteSpace(auth.DeviceId) && diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SharpWebSocket.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SharpWebSocket.cs index 412789240..7ff3a1247 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SharpWebSocket.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SharpWebSocket.cs @@ -96,22 +96,30 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp /// Sends the async. /// </summary> /// <param name="bytes">The bytes.</param> - /// <param name="type">The type.</param> /// <param name="endOfMessage">if set to <c>true</c> [end of message].</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task.</returns> - public Task SendAsync(byte[] bytes, WebSocketMessageType type, bool endOfMessage, CancellationToken cancellationToken) + public Task SendAsync(byte[] bytes, bool endOfMessage, CancellationToken cancellationToken) { - System.Net.WebSockets.WebSocketMessageType nativeType; + var completionSource = new TaskCompletionSource<bool>(); - if (!Enum.TryParse(type.ToString(), true, out nativeType)) - { - _logger.Warn("Unrecognized WebSocketMessageType: {0}", type.ToString()); - } + WebSocket.SendAsync(bytes, res => completionSource.TrySetResult(true)); + + return completionSource.Task; + } + /// <summary> + /// Sends the asynchronous. + /// </summary> + /// <param name="text">The text.</param> + /// <param name="endOfMessage">if set to <c>true</c> [end of message].</param> + /// <param name="cancellationToken">The cancellation token.</param> + /// <returns>Task.</returns> + public Task SendAsync(string text, bool endOfMessage, CancellationToken cancellationToken) + { var completionSource = new TaskCompletionSource<bool>(); - WebSocket.SendAsync(Encoding.UTF8.GetString(bytes), res => completionSource.TrySetResult(true)); + WebSocket.SendAsync(text, res => completionSource.TrySetResult(true)); return completionSource.Task; } |
