From c4176d232044e8356a8d6389912efa05008d0dc2 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 3 Sep 2017 21:24:20 -0400 Subject: [PATCH] add web socket error handling --- .../ServerManager/WebSocketConnection.cs | 2 +- .../TextEncoding/TextEncoding.cs | 61 ++++++++++++------- MediaBrowser.Api/Images/ImageService.cs | 4 +- MediaBrowser.Api/System/SystemService.cs | 6 ++ MediaBrowser.Model/Text/ITextEncoding.cs | 4 +- 5 files changed, 49 insertions(+), 28 deletions(-) diff --git a/Emby.Server.Implementations/ServerManager/WebSocketConnection.cs b/Emby.Server.Implementations/ServerManager/WebSocketConnection.cs index 4d5192fea..076f50d93 100644 --- a/Emby.Server.Implementations/ServerManager/WebSocketConnection.cs +++ b/Emby.Server.Implementations/ServerManager/WebSocketConnection.cs @@ -136,7 +136,7 @@ namespace Emby.Server.Implementations.ServerManager return; } - var charset = _textEncoding.GetDetectedEncodingName(bytes, null, false); + var charset = _textEncoding.GetDetectedEncodingName(bytes, bytes.Length, null, false); if (string.Equals(charset, "utf-8", StringComparison.OrdinalIgnoreCase)) { diff --git a/Emby.Server.Implementations/TextEncoding/TextEncoding.cs b/Emby.Server.Implementations/TextEncoding/TextEncoding.cs index 1496d6f0f..9eb9be7ea 100644 --- a/Emby.Server.Implementations/TextEncoding/TextEncoding.cs +++ b/Emby.Server.Implementations/TextEncoding/TextEncoding.cs @@ -27,18 +27,33 @@ namespace Emby.Server.Implementations.TextEncoding return Encoding.ASCII; } - private Encoding GetInitialEncoding(byte[] buffer) + private Encoding GetInitialEncoding(byte[] buffer, int count) { - if (buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf) - return Encoding.UTF8; - if (buffer[0] == 0xfe && buffer[1] == 0xff) - return Encoding.Unicode; - if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0xfe && buffer[3] == 0xff) - return Encoding.UTF32; - if (buffer[0] == 0x2b && buffer[1] == 0x2f && buffer[2] == 0x76) - return Encoding.UTF7; + if (count >= 3) + { + if (buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf) + return Encoding.UTF8; + } - var result = new TextEncodingDetect().DetectEncoding(buffer, buffer.Length); + if (count >= 2) + { + if (buffer[0] == 0xfe && buffer[1] == 0xff) + return Encoding.Unicode; + } + + if (count >= 4) + { + if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0xfe && buffer[3] == 0xff) + return Encoding.UTF32; + } + + if (count >= 3) + { + if (buffer[0] == 0x2b && buffer[1] == 0x2f && buffer[2] == 0x76) + return Encoding.UTF7; + } + + var result = new TextEncodingDetect().DetectEncoding(buffer, count); switch (result) { @@ -64,9 +79,11 @@ namespace Emby.Server.Implementations.TextEncoding } private bool _langDetectInitialized; - public string GetDetectedEncodingName(byte[] bytes, string language, bool enableLanguageDetection) + public string GetDetectedEncodingName(byte[] bytes, int count, string language, bool enableLanguageDetection) { - var encoding = GetInitialEncoding(bytes); + var index = 0; + + var encoding = GetInitialEncoding(bytes, count); if (encoding != null && encoding.Equals(Encoding.UTF8)) { @@ -81,7 +98,7 @@ namespace Emby.Server.Implementations.TextEncoding LanguageDetector.Initialize(_json); } - language = DetectLanguage(bytes); + language = DetectLanguage(bytes, index, count); if (!string.IsNullOrWhiteSpace(language)) { @@ -89,7 +106,7 @@ namespace Emby.Server.Implementations.TextEncoding } } - var charset = DetectCharset(bytes, language); + var charset = DetectCharset(bytes, index, count, language); if (!string.IsNullOrWhiteSpace(charset)) { @@ -112,11 +129,11 @@ namespace Emby.Server.Implementations.TextEncoding return null; } - private string DetectLanguage(byte[] bytes) + private string DetectLanguage(byte[] bytes, int index, int count) { try { - return LanguageDetector.DetectLanguage(Encoding.UTF8.GetString(bytes)); + return LanguageDetector.DetectLanguage(Encoding.UTF8.GetString(bytes, index, count)); } catch (NLangDetectException ex) { @@ -124,7 +141,7 @@ namespace Emby.Server.Implementations.TextEncoding try { - return LanguageDetector.DetectLanguage(Encoding.ASCII.GetString(bytes)); + return LanguageDetector.DetectLanguage(Encoding.ASCII.GetString(bytes, index, count)); } catch (NLangDetectException ex) { @@ -132,7 +149,7 @@ namespace Emby.Server.Implementations.TextEncoding try { - return LanguageDetector.DetectLanguage(Encoding.Unicode.GetString(bytes)); + return LanguageDetector.DetectLanguage(Encoding.Unicode.GetString(bytes, index, count)); } catch (NLangDetectException ex) { @@ -163,9 +180,9 @@ namespace Emby.Server.Implementations.TextEncoding } } - public Encoding GetDetectedEncoding(byte[] bytes, string language, bool enableLanguageDetection) + public Encoding GetDetectedEncoding(byte[] bytes, int size, string language, bool enableLanguageDetection) { - var charset = GetDetectedEncodingName(bytes, language, enableLanguageDetection); + var charset = GetDetectedEncodingName(bytes, size, language, enableLanguageDetection); return GetEncodingFromCharset(charset); } @@ -225,10 +242,10 @@ namespace Emby.Server.Implementations.TextEncoding } } - private string DetectCharset(byte[] bytes, string language) + private string DetectCharset(byte[] bytes, int index, int count, string language) { var detector = new CharsetDetector(); - detector.Feed(bytes, 0, bytes.Length); + detector.Feed(bytes, index, count); detector.DataEnd(); var charset = detector.Charset; diff --git a/MediaBrowser.Api/Images/ImageService.cs b/MediaBrowser.Api/Images/ImageService.cs index 309c7195b..69d4a4ab4 100644 --- a/MediaBrowser.Api/Images/ImageService.cs +++ b/MediaBrowser.Api/Images/ImageService.cs @@ -639,9 +639,7 @@ namespace MediaBrowser.Api.Images IsHeadRequest = isHeadRequest, Path = imageResult.Item1, - // Sometimes imagemagick keeps a hold on the file briefly even after it's done writing to it. - // I'd rather do this than add a delay after saving the file - FileShare = FileShareMode.ReadWrite + FileShare = FileShareMode.Read }).ConfigureAwait(false); } diff --git a/MediaBrowser.Api/System/SystemService.cs b/MediaBrowser.Api/System/SystemService.cs index 6df2db26c..37613c1c8 100644 --- a/MediaBrowser.Api/System/SystemService.cs +++ b/MediaBrowser.Api/System/SystemService.cs @@ -148,6 +148,12 @@ namespace MediaBrowser.Api.System var file = _fileSystem.GetFiles(_appPaths.LogDirectoryPath) .First(i => string.Equals(i.Name, request.Name, StringComparison.OrdinalIgnoreCase)); + // For older files, assume fully static + if (file.LastWriteTimeUtc < DateTime.UtcNow.AddHours(-1)) + { + return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShareMode.Read); + } + return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShareMode.ReadWrite); } diff --git a/MediaBrowser.Model/Text/ITextEncoding.cs b/MediaBrowser.Model/Text/ITextEncoding.cs index 96dca0c04..619d90a2b 100644 --- a/MediaBrowser.Model/Text/ITextEncoding.cs +++ b/MediaBrowser.Model/Text/ITextEncoding.cs @@ -7,8 +7,8 @@ namespace MediaBrowser.Model.Text { Encoding GetASCIIEncoding(); - string GetDetectedEncodingName(byte[] bytes, string language, bool enableLanguageDetection); - Encoding GetDetectedEncoding(byte[] bytes, string language, bool enableLanguageDetection); + string GetDetectedEncodingName(byte[] bytes, int size, string language, bool enableLanguageDetection); + Encoding GetDetectedEncoding(byte[] bytes, int size, string language, bool enableLanguageDetection); Encoding GetEncodingFromCharset(string charset); } }