The Free Software Media System - Server Backend & API
add web socket error handling
Luke Pulverentic4176d232044
diff --git a/Emby.Server.Implementations/ServerManager/WebSocketConnection.cs b/Emby.Server.Implementations/ServerManager/WebSocketConnection.csindex 4d5192feaf..076f50d937 100644--- a/Emby.Server.Implementations/ServerManager/WebSocketConnection.cs+++ b/Emby.Server.Implementations/ServerManager/WebSocketConnection.cs@@ -136,7 +136,7 @@ namespace Emby.Server.Implementations.ServerManagerreturn;}- 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.csindex 1496d6f0f5..9eb9be7eaa 100644--- a/Emby.Server.Implementations/TextEncoding/TextEncoding.cs+++ b/Emby.Server.Implementations/TextEncoding/TextEncoding.cs@@ -27,18 +27,33 @@ namespace Emby.Server.Implementations.TextEncodingreturn 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;+ }++ 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;+ }- var result = new TextEncodingDetect().DetectEncoding(buffer, buffer.Length);+ 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.TextEncodingLanguageDetector.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.TextEncodingreturn 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.TextEncodingtry{- 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.TextEncodingtry{- 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.csindex 309c7195ba..69d4a4ab45 100644--- a/MediaBrowser.Api/Images/ImageService.cs+++ b/MediaBrowser.Api/Images/ImageService.cs@@ -639,9 +639,7 @@ namespace MediaBrowser.Api.ImagesIsHeadRequest = 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.csindex 6df2db26c7..37613c1c81 100644--- a/MediaBrowser.Api/System/SystemService.cs+++ b/MediaBrowser.Api/System/SystemService.cs@@ -148,6 +148,12 @@ namespace MediaBrowser.Api.Systemvar 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.csindex 96dca0c045..619d90a2b9 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);}}