From 8d9428ebdc463dcaa02cfa8daf91ab480f2ace6a Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Mon, 14 Jan 2019 18:28:29 +0100 Subject: Ensure DB exists --- Jellyfin.Server/Program.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'Jellyfin.Server/Program.cs') diff --git a/Jellyfin.Server/Program.cs b/Jellyfin.Server/Program.cs index 0510548b5..bdf8c4e94 100644 --- a/Jellyfin.Server/Program.cs +++ b/Jellyfin.Server/Program.cs @@ -99,7 +99,7 @@ namespace Jellyfin.Server new SystemEvents(), new NetworkManager(_loggerFactory, environmentInfo))) { - appHost.Init(); + await appHost.Init(); appHost.ImageProcessor.ImageEncoder = GetImageEncoder(fileSystem, appPaths, appHost.LocalizationManager); @@ -108,7 +108,6 @@ namespace Jellyfin.Server await appHost.RunStartupTasks(); // TODO: read input for a stop command - try { // Block main thread until shutdown @@ -167,7 +166,6 @@ namespace Jellyfin.Server { Directory.CreateDirectory(programDataPath); } - string configDir = Environment.GetEnvironmentVariable("JELLYFIN_CONFIG_DIR"); if (string.IsNullOrEmpty(configDir)) { -- cgit v1.2.3 From 905a253ff5ce2eeee9806b7a4581506468b2fb55 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Wed, 23 Jan 2019 19:09:34 +0100 Subject: Suffix async methods with Async --- .../Activity/ActivityLogEntryPoint.cs | 93 +++++++++++----------- .../Activity/ActivityManager.cs | 2 +- Emby.Server.Implementations/ApplicationHost.cs | 10 +-- Jellyfin.Server/Program.cs | 2 +- MediaBrowser.Api/Library/LibraryService.cs | 2 +- MediaBrowser.Common/IApplicationHost.cs | 2 +- MediaBrowser.Model/Activity/IActivityManager.cs | 2 +- 7 files changed, 57 insertions(+), 56 deletions(-) (limited to 'Jellyfin.Server/Program.cs') diff --git a/Emby.Server.Implementations/Activity/ActivityLogEntryPoint.cs b/Emby.Server.Implementations/Activity/ActivityLogEntryPoint.cs index a8e8f815a..aef72c697 100644 --- a/Emby.Server.Implementations/Activity/ActivityLogEntryPoint.cs +++ b/Emby.Server.Implementations/Activity/ActivityLogEntryPoint.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading.Tasks; using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Plugins; using MediaBrowser.Common.Updates; @@ -92,18 +93,18 @@ namespace Emby.Server.Implementations.Activity _appHost.ApplicationUpdated += _appHost_ApplicationUpdated; } - void _deviceManager_CameraImageUploaded(object sender, GenericEventArgs e) + async void _deviceManager_CameraImageUploaded(object sender, GenericEventArgs e) { - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("CameraImageUploadedFrom"), e.Argument.Device.Name), Type = NotificationType.CameraImageUploaded.ToString() }); } - void _userManager_UserLockedOut(object sender, GenericEventArgs e) + async void _userManager_UserLockedOut(object sender, GenericEventArgs e) { - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("UserLockedOutWithName"), e.Argument.Name), Type = NotificationType.UserLockedOut.ToString(), @@ -111,9 +112,9 @@ namespace Emby.Server.Implementations.Activity }); } - void _subManager_SubtitleDownloadFailure(object sender, SubtitleDownloadFailureEventArgs e) + async void _subManager_SubtitleDownloadFailure(object sender, SubtitleDownloadFailureEventArgs e) { - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("SubtitleDownloadFailureFromForItem"), e.Provider, Notifications.Notifications.GetItemName(e.Item)), Type = "SubtitleDownloadFailure", @@ -122,7 +123,7 @@ namespace Emby.Server.Implementations.Activity }); } - void _sessionManager_PlaybackStopped(object sender, PlaybackStopEventArgs e) + async void _sessionManager_PlaybackStopped(object sender, PlaybackStopEventArgs e) { var item = e.MediaInfo; @@ -145,7 +146,7 @@ namespace Emby.Server.Implementations.Activity var user = e.Users.First(); - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("UserStoppedPlayingItemWithValues"), user.Name, GetItemName(item), e.DeviceName), Type = GetPlaybackStoppedNotificationType(item.MediaType), @@ -153,7 +154,7 @@ namespace Emby.Server.Implementations.Activity }); } - void _sessionManager_PlaybackStart(object sender, PlaybackProgressEventArgs e) + async void _sessionManager_PlaybackStart(object sender, PlaybackProgressEventArgs e) { var item = e.MediaInfo; @@ -176,7 +177,7 @@ namespace Emby.Server.Implementations.Activity var user = e.Users.First(); - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("UserStartedPlayingItemWithValues"), user.Name, GetItemName(item), e.DeviceName), Type = GetPlaybackNotificationType(item.MediaType), @@ -237,7 +238,7 @@ namespace Emby.Server.Implementations.Activity return null; } - void _sessionManager_SessionEnded(object sender, SessionEventArgs e) + async void _sessionManager_SessionEnded(object sender, SessionEventArgs e) { string name; var session = e.SessionInfo; @@ -254,7 +255,7 @@ namespace Emby.Server.Implementations.Activity name = string.Format(_localization.GetLocalizedString("UserOfflineFromDevice"), session.UserName, session.DeviceName); } - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = name, Type = "SessionEnded", @@ -263,11 +264,11 @@ namespace Emby.Server.Implementations.Activity }); } - void _sessionManager_AuthenticationSucceeded(object sender, GenericEventArgs e) + async void _sessionManager_AuthenticationSucceeded(object sender, GenericEventArgs e) { var user = e.Argument.User; - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("AuthenticationSucceededWithUserName"), user.Name), Type = "AuthenticationSucceeded", @@ -276,9 +277,9 @@ namespace Emby.Server.Implementations.Activity }); } - void _sessionManager_AuthenticationFailed(object sender, GenericEventArgs e) + async void _sessionManager_AuthenticationFailed(object sender, GenericEventArgs e) { - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("FailedLoginAttemptWithUserName"), e.Argument.Username), Type = "AuthenticationFailed", @@ -287,9 +288,9 @@ namespace Emby.Server.Implementations.Activity }); } - void _appHost_ApplicationUpdated(object sender, GenericEventArgs e) + async void _appHost_ApplicationUpdated(object sender, GenericEventArgs e) { - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("MessageApplicationUpdatedTo"), e.Argument.versionStr), Type = NotificationType.ApplicationUpdateInstalled.ToString(), @@ -297,27 +298,27 @@ namespace Emby.Server.Implementations.Activity }); } - void _config_NamedConfigurationUpdated(object sender, ConfigurationUpdateEventArgs e) + async void _config_NamedConfigurationUpdated(object sender, ConfigurationUpdateEventArgs e) { - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("MessageNamedServerConfigurationUpdatedWithValue"), e.Key), Type = "NamedConfigurationUpdated" }); } - void _config_ConfigurationUpdated(object sender, EventArgs e) + async void _config_ConfigurationUpdated(object sender, EventArgs e) { - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = _localization.GetLocalizedString("MessageServerConfigurationUpdated"), Type = "ServerConfigurationUpdated" }); } - void _userManager_UserPolicyUpdated(object sender, GenericEventArgs e) + async void _userManager_UserPolicyUpdated(object sender, GenericEventArgs e) { - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("UserPolicyUpdatedWithName"), e.Argument.Name), Type = "UserPolicyUpdated", @@ -325,18 +326,18 @@ namespace Emby.Server.Implementations.Activity }); } - void _userManager_UserDeleted(object sender, GenericEventArgs e) + async void _userManager_UserDeleted(object sender, GenericEventArgs e) { - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("UserDeletedWithName"), e.Argument.Name), Type = "UserDeleted" }); } - void _userManager_UserPasswordChanged(object sender, GenericEventArgs e) + async void _userManager_UserPasswordChanged(object sender, GenericEventArgs e) { - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("UserPasswordChangedWithName"), e.Argument.Name), Type = "UserPasswordChanged", @@ -344,9 +345,9 @@ namespace Emby.Server.Implementations.Activity }); } - void _userManager_UserCreated(object sender, GenericEventArgs e) + async void _userManager_UserCreated(object sender, GenericEventArgs e) { - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("UserCreatedWithName"), e.Argument.Name), Type = "UserCreated", @@ -354,9 +355,9 @@ namespace Emby.Server.Implementations.Activity }); } - void _subManager_SubtitlesDownloaded(object sender, SubtitleDownloadEventArgs e) + async void _subManager_SubtitlesDownloaded(object sender, SubtitleDownloadEventArgs e) { - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("SubtitlesDownloadedForItem"), Notifications.Notifications.GetItemName(e.Item)), Type = "SubtitlesDownloaded", @@ -365,7 +366,7 @@ namespace Emby.Server.Implementations.Activity }); } - void _sessionManager_SessionStarted(object sender, SessionEventArgs e) + async void _sessionManager_SessionStarted(object sender, SessionEventArgs e) { string name; var session = e.SessionInfo; @@ -382,7 +383,7 @@ namespace Emby.Server.Implementations.Activity name = string.Format(_localization.GetLocalizedString("UserOnlineFromDevice"), session.UserName, session.DeviceName); } - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = name, Type = "SessionStarted", @@ -391,9 +392,9 @@ namespace Emby.Server.Implementations.Activity }); } - void _installationManager_PluginUpdated(object sender, GenericEventArgs> e) + async void _installationManager_PluginUpdated(object sender, GenericEventArgs> e) { - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("PluginUpdatedWithName"), e.Argument.Item1.Name), Type = NotificationType.PluginUpdateInstalled.ToString(), @@ -402,18 +403,18 @@ namespace Emby.Server.Implementations.Activity }); } - void _installationManager_PluginUninstalled(object sender, GenericEventArgs e) + async void _installationManager_PluginUninstalled(object sender, GenericEventArgs e) { - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("PluginUninstalledWithName"), e.Argument.Name), Type = NotificationType.PluginUninstalled.ToString() }); } - void _installationManager_PluginInstalled(object sender, GenericEventArgs e) + async void _installationManager_PluginInstalled(object sender, GenericEventArgs e) { - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("PluginInstalledWithName"), e.Argument.name), Type = NotificationType.PluginInstalled.ToString(), @@ -421,11 +422,11 @@ namespace Emby.Server.Implementations.Activity }); } - void _installationManager_PackageInstallationFailed(object sender, InstallationFailedEventArgs e) + async void _installationManager_PackageInstallationFailed(object sender, InstallationFailedEventArgs e) { var installationInfo = e.InstallationInfo; - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("NameInstallFailed"), installationInfo.Name), Type = NotificationType.InstallationFailed.ToString(), @@ -434,7 +435,7 @@ namespace Emby.Server.Implementations.Activity }); } - void _taskManager_TaskCompleted(object sender, TaskCompletionEventArgs e) + async void _taskManager_TaskCompleted(object sender, TaskCompletionEventArgs e) { var result = e.Result; var task = e.Task; @@ -461,7 +462,7 @@ namespace Emby.Server.Implementations.Activity vals.Add(e.Result.LongErrorMessage); } - CreateLogEntry(new ActivityLogEntry + await CreateLogEntry(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("ScheduledTaskFailedWithName"), task.Name), Type = NotificationType.TaskFailed.ToString(), @@ -472,11 +473,11 @@ namespace Emby.Server.Implementations.Activity } } - private void CreateLogEntry(ActivityLogEntry entry) + private async Task CreateLogEntry(ActivityLogEntry entry) { try { - _activityManager.Create(entry); + await _activityManager.CreateAsync(entry); } catch { diff --git a/Emby.Server.Implementations/Activity/ActivityManager.cs b/Emby.Server.Implementations/Activity/ActivityManager.cs index e504bc4d7..8fcacb002 100644 --- a/Emby.Server.Implementations/Activity/ActivityManager.cs +++ b/Emby.Server.Implementations/Activity/ActivityManager.cs @@ -27,7 +27,7 @@ namespace Emby.Server.Implementations.Activity _userManager = userManager; } - public async Task Create(ActivityLogEntry entry) + public async Task CreateAsync(ActivityLogEntry entry) { entry.Date = DateTime.UtcNow; diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs index 3562074fe..f5a4f1581 100644 --- a/Emby.Server.Implementations/ApplicationHost.cs +++ b/Emby.Server.Implementations/ApplicationHost.cs @@ -709,7 +709,7 @@ namespace Emby.Server.Implementations } } - public async Task Init() + public async Task InitAsync() { HttpPort = ServerConfigurationManager.Configuration.HttpServerPortNumber; HttpsPort = ServerConfigurationManager.Configuration.HttpsPortNumber; @@ -739,7 +739,7 @@ namespace Emby.Server.Implementations SetHttpLimit(); - await RegisterResources(); + await RegisterResourcesAsync(); FindParts(); } @@ -754,7 +754,7 @@ namespace Emby.Server.Implementations /// /// Registers resources that classes will depend on /// - protected async Task RegisterResources() + protected async Task RegisterResourcesAsync() { RegisterSingleInstance(ConfigurationManager); RegisterSingleInstance(this); @@ -931,7 +931,7 @@ namespace Emby.Server.Implementations EncodingManager = new MediaEncoder.EncodingManager(FileSystemManager, LoggerFactory, MediaEncoder, ChapterManager, LibraryManager); RegisterSingleInstance(EncodingManager); - var activityLogRepo = await GetActivityLogRepository(); + var activityLogRepo = await GetActivityLogRepositoryAsync(); RegisterSingleInstance(activityLogRepo); RegisterSingleInstance(new ActivityManager(LoggerFactory, activityLogRepo, UserManager)); @@ -1146,7 +1146,7 @@ namespace Emby.Server.Implementations return repo; } - private async Task GetActivityLogRepository() + private async Task GetActivityLogRepositoryAsync() { var repo = new ActivityRepository(ServerConfigurationManager.ApplicationPaths.DataPath); diff --git a/Jellyfin.Server/Program.cs b/Jellyfin.Server/Program.cs index bdf8c4e94..2f14d1c52 100644 --- a/Jellyfin.Server/Program.cs +++ b/Jellyfin.Server/Program.cs @@ -99,7 +99,7 @@ namespace Jellyfin.Server new SystemEvents(), new NetworkManager(_loggerFactory, environmentInfo))) { - await appHost.Init(); + await appHost.InitAsync(); appHost.ImageProcessor.ImageEncoder = GetImageEncoder(fileSystem, appPaths, appHost.LocalizationManager); diff --git a/MediaBrowser.Api/Library/LibraryService.cs b/MediaBrowser.Api/Library/LibraryService.cs index 12d807a7e..d3009295f 100644 --- a/MediaBrowser.Api/Library/LibraryService.cs +++ b/MediaBrowser.Api/Library/LibraryService.cs @@ -839,7 +839,7 @@ namespace MediaBrowser.Api.Library { try { - _activityManager.Create(new ActivityLogEntry + _activityManager.CreateAsync(new ActivityLogEntry { Name = string.Format(_localization.GetLocalizedString("UserDownloadingItemWithValues"), user.Name, item.Name), Type = "UserDownloadingContent", diff --git a/MediaBrowser.Common/IApplicationHost.cs b/MediaBrowser.Common/IApplicationHost.cs index f32eec6ce..9dc625e35 100644 --- a/MediaBrowser.Common/IApplicationHost.cs +++ b/MediaBrowser.Common/IApplicationHost.cs @@ -131,7 +131,7 @@ namespace MediaBrowser.Common /// /// Inits this instance. /// - Task Init(); + Task InitAsync(); /// /// Creates the instance. diff --git a/MediaBrowser.Model/Activity/IActivityManager.cs b/MediaBrowser.Model/Activity/IActivityManager.cs index 3cfd94695..bbd7b6f2d 100644 --- a/MediaBrowser.Model/Activity/IActivityManager.cs +++ b/MediaBrowser.Model/Activity/IActivityManager.cs @@ -9,7 +9,7 @@ namespace MediaBrowser.Model.Activity { event EventHandler> EntryCreated; - Task Create(ActivityLogEntry entry); + Task CreateAsync(ActivityLogEntry entry); IEnumerable GetActivityLogEntries(DateTime? minDate, bool? hasUserId, int? x, int? y); } -- cgit v1.2.3 From 8191efb90cb94727ee8a7626d2226d1d1451f047 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Wed, 23 Jan 2019 20:08:50 +0100 Subject: Fix some analyzer warnings Some changes: * Don't omit braces * Fix culture sensitive string compare * Define accessibility functions I restricted myself to these 5 files, for now :p --- Jellyfin.Server/Program.cs | 10 +- Jellyfin.Server/SocketSharp/RequestMono.cs | 168 ++++++++++++++++++--- .../SocketSharp/WebSocketSharpListener.cs | 10 +- .../SocketSharp/WebSocketSharpRequest.cs | 64 ++++++-- 4 files changed, 202 insertions(+), 50 deletions(-) (limited to 'Jellyfin.Server/Program.cs') diff --git a/Jellyfin.Server/Program.cs b/Jellyfin.Server/Program.cs index 0510548b5..c196a3f23 100644 --- a/Jellyfin.Server/Program.cs +++ b/Jellyfin.Server/Program.cs @@ -224,7 +224,7 @@ namespace Jellyfin.Server .GetManifestResourceStream("Jellyfin.Server.Resources.Configuration.logging.json")) using (Stream fstr = File.Open(configPath, FileMode.CreateNew)) { - await rscstr.CopyToAsync(fstr); + await rscstr.CopyToAsync(fstr).ConfigureAwait(false); } } var configuration = new ConfigurationBuilder() @@ -334,11 +334,9 @@ namespace Jellyfin.Server } else { - commandLineArgsString = string.Join(" ", - Environment.GetCommandLineArgs() - .Skip(1) - .Select(NormalizeCommandLineArgument) - ); + commandLineArgsString = string.Join( + " ", + Environment.GetCommandLineArgs().Skip(1).Select(NormalizeCommandLineArgument)); } _logger.LogInformation("Executable: {0}", module); diff --git a/Jellyfin.Server/SocketSharp/RequestMono.cs b/Jellyfin.Server/SocketSharp/RequestMono.cs index 45cb323d2..017690062 100644 --- a/Jellyfin.Server/SocketSharp/RequestMono.cs +++ b/Jellyfin.Server/SocketSharp/RequestMono.cs @@ -15,19 +15,27 @@ namespace Jellyfin.SocketSharp { int ap = header.IndexOf(attr); if (ap == -1) + { return null; + } ap += attr.Length; if (ap >= header.Length) + { return null; + } char ending = header[ap]; if (ending != '"') + { ending = ' '; + } int end = header.IndexOf(ending, ap + 1); if (end == -1) + { return ending == '"' ? null : header.Substring(ap); + } return header.Substring(ap + 1, end - ap - 1); } @@ -36,7 +44,9 @@ namespace Jellyfin.SocketSharp { string boundary = GetParameter(ContentType, "; boundary="); if (boundary == null) + { return; + } using (var requestStream = InputStream) { @@ -124,7 +134,9 @@ namespace Jellyfin.SocketSharp { string v = "\"" + value + "\""; if (v.Length > 20) + { v = v.Substring(0, 16) + "...\""; + } string msg = string.Format("A potentially dangerous Request.{0} value was " + "detected from the client ({1}={2}).", name, key, v); @@ -135,21 +147,23 @@ namespace Jellyfin.SocketSharp static void ValidateNameValueCollection(string name, QueryParamCollection coll) { if (coll == null) + { return; + } foreach (var pair in coll) { var key = pair.Name; var val = pair.Value; if (val != null && val.Length > 0 && IsInvalidString(val)) + { ThrowValidationException(name, key, val); + } } } internal static bool IsInvalidString(string val) - { - return IsInvalidString(val, out var validationFailureIndex); - } + => IsInvalidString(val, out var validationFailureIndex); internal static bool IsInvalidString(string val, out int validationFailureIndex) { @@ -157,7 +171,9 @@ namespace Jellyfin.SocketSharp int len = val.Length; if (len < 2) + { return false; + } char current = val[0]; for (int idx = 1; idx < len; idx++) @@ -195,10 +211,15 @@ namespace Jellyfin.SocketSharp bool IsContentType(string ct, bool starts_with) { - if (ct == null || ContentType == null) return false; + if (ct == null || ContentType == null) + { + return false; + } if (starts_with) + { return StrUtils.StartsWith(ContentType, ct, true); + } return string.Equals(ContentType, ct, StringComparison.OrdinalIgnoreCase); } @@ -231,7 +252,9 @@ namespace Jellyfin.SocketSharp break; } else + { value.Append((char)c); + } } if (c == -1) { @@ -240,22 +263,26 @@ namespace Jellyfin.SocketSharp } } else if (c == '&') + { AddRawKeyValue(form, key, value); + } else + { key.Append((char)c); + } } if (c == -1) + { AddRawKeyValue(form, key, value); + } } } } } - void AddRawKeyValue(WebROCollection form, StringBuilder key, StringBuilder value) + static void AddRawKeyValue(WebROCollection form, StringBuilder key, StringBuilder value) { - string decodedKey = WebUtility.UrlDecode(key.ToString()); - form.Add(decodedKey, - WebUtility.UrlDecode(value.ToString())); + form.Add(WebUtility.UrlDecode(key.ToString()), WebUtility.UrlDecode(value.ToString())); key.Length = 0; value.Length = 0; @@ -271,7 +298,9 @@ namespace Jellyfin.SocketSharp foreach (var pair in this) { if (result.Length > 0) + { result.Append('&'); + } var key = pair.Name; if (key != null && key.Length > 0) @@ -314,33 +343,52 @@ namespace Jellyfin.SocketSharp public override int Read(byte[] buffer, int dest_offset, int count) { if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); + } if (dest_offset < 0) + { throw new ArgumentOutOfRangeException(nameof(dest_offset), "< 0"); + } if (count < 0) + { throw new ArgumentOutOfRangeException(nameof(count), "< 0"); + } int len = buffer.Length; if (dest_offset > len) + { throw new ArgumentException("destination offset is beyond array size"); + } + // reordered to avoid possible integer overflow if (dest_offset > len - count) + { throw new ArgumentException("Reading would overrun buffer"); + } if (count > end - position) + { count = (int)(end - position); + } if (count <= 0) + { return 0; + } s.Position = position; int result = s.Read(buffer, dest_offset, count); if (result > 0) + { position += result; + } else + { position = end; + } return result; } @@ -348,14 +396,20 @@ namespace Jellyfin.SocketSharp public override int ReadByte() { if (position >= end) + { return -1; + } s.Position = position; int result = s.ReadByte(); if (result < 0) + { position = end; + } else + { position++; + } return result; } @@ -380,7 +434,9 @@ namespace Jellyfin.SocketSharp long virt = real - offset; if (virt < 0 || virt > Length) + { throw new ArgumentException(); + } position = s.Seek(real, SeekOrigin.Begin); return position; @@ -410,7 +466,9 @@ namespace Jellyfin.SocketSharp set { if (value > Length) - throw new ArgumentOutOfRangeException(); + { + throw new ArgumentOutOfRangeException(nameof(value)); + } position = Seek(value, SeekOrigin.Begin); } @@ -438,7 +496,7 @@ namespace Jellyfin.SocketSharp public static readonly CultureInfo InvariantCulture = CultureInfo.InvariantCulture; } - internal sealed class StrUtils + internal static class StrUtils { public static bool StartsWith(string str1, string str2, bool ignore_case) { @@ -455,11 +513,15 @@ namespace Jellyfin.SocketSharp { int l2 = str2.Length; if (l2 == 0) + { return true; + } int l1 = str1.Length; if (l2 > l1) + { return false; + } var comparison = ignore_case ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; return str1.IndexOf(str2, comparison) == str1.Length - str2.Length - 1; @@ -493,7 +555,7 @@ namespace Jellyfin.SocketSharp Encoding encoding; StringBuilder sb; - const byte HYPHEN = (byte)'-', LF = (byte)'\n', CR = (byte)'\r'; + const byte LF = (byte)'\n', CR = (byte)'\r'; // See RFC 2046 // In the case of multipart entities, in which one or more different @@ -520,7 +582,7 @@ namespace Jellyfin.SocketSharp sb = new StringBuilder(); } - string ReadLine() + private string ReadLine() { // CRLF or LF are ok as line endings. bool got_cr = false; @@ -543,58 +605,86 @@ namespace Jellyfin.SocketSharp } if (got_cr) + { sb.Length--; + } return sb.ToString(); } - static string GetContentDispositionAttribute(string l, string name) + private static string GetContentDispositionAttribute(string l, string name) { int idx = l.IndexOf(name + "=\""); if (idx < 0) + { return null; + } + int begin = idx + name.Length + "=\"".Length; int end = l.IndexOf('"', begin); if (end < 0) + { return null; + } + if (begin == end) - return ""; + { + return string.Empty; + } + return l.Substring(begin, end - begin); } - string GetContentDispositionAttributeWithEncoding(string l, string name) + private string GetContentDispositionAttributeWithEncoding(string l, string name) { int idx = l.IndexOf(name + "=\""); if (idx < 0) + { return null; + } + int begin = idx + name.Length + "=\"".Length; int end = l.IndexOf('"', begin); if (end < 0) + { return null; + } + if (begin == end) - return ""; + { + return string.Empty; + } string temp = l.Substring(begin, end - begin); byte[] source = new byte[temp.Length]; for (int i = temp.Length - 1; i >= 0; i--) + { source[i] = (byte)temp[i]; + } return encoding.GetString(source, 0, source.Length); } - bool ReadBoundary() + private bool ReadBoundary() { try { string line = ReadLine(); - while (line == "") + while (line == string.Empty) + { line = ReadLine(); + } + if (line[0] != '-' || line[1] != '-') + { return false; + } if (!StrUtils.EndsWith(line, boundary, false)) + { return true; + } } catch { @@ -603,25 +693,31 @@ namespace Jellyfin.SocketSharp return false; } - string ReadHeaders() + private string ReadHeaders() { string s = ReadLine(); - if (s == "") + if (s.Length == 0) + { return null; + } return s; } - bool CompareBytes(byte[] orig, byte[] other) + private static bool CompareBytes(byte[] orig, byte[] other) { for (int i = orig.Length - 1; i >= 0; i--) + { if (orig[i] != other[i]) + { return false; + } + } return true; } - long MoveToNextBoundary() + private long MoveToNextBoundary() { long retval = 0; bool got_cr = false; @@ -631,13 +727,18 @@ namespace Jellyfin.SocketSharp while (true) { if (c == -1) + { return -1; + } if (state == 0 && c == LF) { retval = data.Position - 1; if (got_cr) + { retval--; + } + state = 1; c = data.ReadByte(); } @@ -650,7 +751,9 @@ namespace Jellyfin.SocketSharp { c = data.ReadByte(); if (c == -1) + { return -1; + } if (c != '-') { @@ -662,7 +765,9 @@ namespace Jellyfin.SocketSharp int nread = data.Read(buffer, 0, buffer.Length); int bl = buffer.Length; if (nread != bl) + { return -1; + } if (!CompareBytes(boundary_bytes, buffer)) { @@ -673,6 +778,7 @@ namespace Jellyfin.SocketSharp data.Position++; got_cr = false; } + c = data.ReadByte(); continue; } @@ -690,12 +796,16 @@ namespace Jellyfin.SocketSharp data.Position++; got_cr = false; } + c = data.ReadByte(); continue; } data.Position = retval + 2; if (got_cr) + { data.Position++; + } + break; } else @@ -711,7 +821,9 @@ namespace Jellyfin.SocketSharp public Element ReadNextElement() { if (at_eof || ReadBoundary()) + { return null; + } var elem = new Element(); string header; @@ -734,19 +846,27 @@ namespace Jellyfin.SocketSharp elem.Start = start; long pos = MoveToNextBoundary(); if (pos == -1) + { return null; + } elem.Length = pos - start; return elem; } - static string StripPath(string path) + private static string StripPath(string path) { if (path == null || path.Length == 0) + { return path; + } - if (path.IndexOf(":\\") != 1 && !path.StartsWith("\\\\")) + if (path.IndexOf(":\\", StringComparison.Ordinal) != 1 + && !path.StartsWith("\\\\", StringComparison.Ordinal)) + { return path; + } + return path.Substring(path.LastIndexOf('\\') + 1); } } diff --git a/Jellyfin.Server/SocketSharp/WebSocketSharpListener.cs b/Jellyfin.Server/SocketSharp/WebSocketSharpListener.cs index ef75644d7..c7f9f01b5 100644 --- a/Jellyfin.Server/SocketSharp/WebSocketSharpListener.cs +++ b/Jellyfin.Server/SocketSharp/WebSocketSharpListener.cs @@ -83,15 +83,15 @@ namespace Jellyfin.SocketSharp private void ProcessContext(HttpListenerContext context) { - //InitTask(context, _disposeCancellationToken); - Task.Run(() => InitTask(context, _disposeCancellationToken)); + var _ = Task.Run(async () => await InitTask(context, _disposeCancellationToken)); } - private void LogRequest(ILogger logger, HttpListenerRequest request) + private static void LogRequest(ILogger logger, HttpListenerRequest request) { var url = request.Url.ToString(); - logger.LogInformation("{0} {1}. UserAgent: {2}", request.IsWebSocketRequest ? "WS" : "HTTP " + request.HttpMethod, url, request.UserAgent ?? string.Empty); + logger.LogInformation("{0} {1}. UserAgent: {2}", + request.IsWebSocketRequest ? "WS" : "HTTP " + request.HttpMethod, url, request.UserAgent ?? string.Empty); } private Task InitTask(HttpListenerContext context, CancellationToken cancellationToken) @@ -196,7 +196,7 @@ namespace Jellyfin.SocketSharp { try { - ctx.Response.StatusCode = 200; + ctx.Response.StatusCode = statusCode; ctx.Response.Close(); } catch (ObjectDisposedException) diff --git a/Jellyfin.Server/SocketSharp/WebSocketSharpRequest.cs b/Jellyfin.Server/SocketSharp/WebSocketSharpRequest.cs index e38468388..97550e686 100644 --- a/Jellyfin.Server/SocketSharp/WebSocketSharpRequest.cs +++ b/Jellyfin.Server/SocketSharp/WebSocketSharpRequest.cs @@ -242,7 +242,6 @@ namespace Jellyfin.SocketSharp return request.ContentType.StartsWith(contentType, StringComparison.OrdinalIgnoreCase); } - public const string Xml = "application/xml"; private static string GetQueryStringContentType(IRequest httpReq) { var format = httpReq.QueryString["format"]; @@ -250,22 +249,40 @@ namespace Jellyfin.SocketSharp { const int formatMaxLength = 4; var pi = httpReq.PathInfo; - if (pi == null || pi.Length <= formatMaxLength) return null; - if (pi[0] == '/') pi = pi.Substring(1); + if (pi == null || pi.Length <= formatMaxLength) + { + return null; + } + if (pi[0] == '/') + { + pi = pi.Substring(1); + } format = LeftPart(pi, '/'); - if (format.Length > formatMaxLength) return null; + if (format.Length > formatMaxLength) + { + return null; + } } format = LeftPart(format, '.').ToLower(); - if (format.Contains("json")) return "application/json"; - if (format.Contains("xml")) return Xml; + if (format.Contains("json", StringComparison.OrdinalIgnoreCase)) + { + return "application/json"; + } + if (format.Contains("xml", StringComparison.OrdinalIgnoreCase)) + { + return "application/xml"; + } return null; } public static string LeftPart(string strVal, char needle) { - if (strVal == null) return null; + if (strVal == null) + { + return null; + } var pos = strVal.IndexOf(needle); return pos == -1 ? strVal @@ -283,14 +300,14 @@ namespace Jellyfin.SocketSharp { var mode = HandlerFactoryPath; - var pos = request.RawUrl.IndexOf("?"); + var pos = request.RawUrl.IndexOf("?", StringComparison.Ordinal); if (pos != -1) { var path = request.RawUrl.Substring(0, pos); this.pathInfo = GetPathInfo( path, mode, - mode ?? ""); + mode ?? string.Empty); } else { @@ -307,18 +324,27 @@ namespace Jellyfin.SocketSharp private static string GetPathInfo(string fullPath, string mode, string appPath) { var pathInfo = ResolvePathInfoFromMappedPath(fullPath, mode); - if (!string.IsNullOrEmpty(pathInfo)) return pathInfo; + if (!string.IsNullOrEmpty(pathInfo)) + { + return pathInfo; + } //Wildcard mode relies on this to work out the handlerPath pathInfo = ResolvePathInfoFromMappedPath(fullPath, appPath); - if (!string.IsNullOrEmpty(pathInfo)) return pathInfo; + if (!string.IsNullOrEmpty(pathInfo)) + { + return pathInfo; + } return fullPath; } private static string ResolvePathInfoFromMappedPath(string fullPath, string mappedPathRoot) { - if (mappedPathRoot == null) return null; + if (mappedPathRoot == null) + { + return null; + } var sbPathInfo = new StringBuilder(); var fullPathParts = fullPath.Split('/'); @@ -345,7 +371,10 @@ namespace Jellyfin.SocketSharp } } } - if (!pathRootFound) return null; + if (!pathRootFound) + { + return null; + } var path = sbPathInfo.ToString(); return path.Length > 1 ? path.TrimEnd('/') : "/"; @@ -400,7 +429,10 @@ namespace Jellyfin.SocketSharp public static Encoding GetEncoding(string contentTypeHeader) { var param = GetParameter(contentTypeHeader, "charset="); - if (param == null) return null; + if (param == null) + { + return null; + } try { return Encoding.GetEncoding(param); @@ -423,7 +455,9 @@ namespace Jellyfin.SocketSharp if (httpFiles == null) { if (files == null) - return httpFiles = new IHttpFile[0]; + { + return httpFiles = Array.Empty(); + } httpFiles = new IHttpFile[files.Count]; var i = 0; -- cgit v1.2.3