diff options
| author | Andrew Rabert <6550543+nvllsvm@users.noreply.github.com> | 2019-01-22 18:13:47 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-22 18:13:47 -0500 |
| commit | 28483bdb54be96ae83e0fded097f534d7e26ba1e (patch) | |
| tree | e7f4b92326417ebf55eecdf68a01d2c3b9e660d7 /Emby.Server.Implementations/HttpServer/HttpResultFactory.cs | |
| parent | 920c39454c05e979eabe81877269cd4517a03ccf (diff) | |
| parent | 8106c8393b711a7e1d40487e3caf2b014decbe28 (diff) | |
Merge pull request #651 from jellyfin/release-10.1.0
Release 10.1.0
Diffstat (limited to 'Emby.Server.Implementations/HttpServer/HttpResultFactory.cs')
| -rw-r--r-- | Emby.Server.Implementations/HttpServer/HttpResultFactory.cs | 83 |
1 files changed, 36 insertions, 47 deletions
diff --git a/Emby.Server.Implementations/HttpServer/HttpResultFactory.cs b/Emby.Server.Implementations/HttpServer/HttpResultFactory.cs index 73b2afe64..8b60d61d4 100644 --- a/Emby.Server.Implementations/HttpServer/HttpResultFactory.cs +++ b/Emby.Server.Implementations/HttpServer/HttpResultFactory.cs @@ -1,7 +1,3 @@ -using MediaBrowser.Common.Extensions; -using MediaBrowser.Controller.Net; -using Microsoft.Extensions.Logging; -using MediaBrowser.Model.Serialization; using System; using System.Collections.Generic; using System.Globalization; @@ -13,8 +9,12 @@ using System.Text; using System.Threading.Tasks; using System.Xml; using Emby.Server.Implementations.Services; +using MediaBrowser.Common.Extensions; +using MediaBrowser.Controller.Net; using MediaBrowser.Model.IO; +using MediaBrowser.Model.Serialization; using MediaBrowser.Model.Services; +using Microsoft.Extensions.Logging; using IRequest = MediaBrowser.Model.Services.IRequest; using MimeTypes = MediaBrowser.Model.Net.MimeTypes; @@ -96,8 +96,7 @@ namespace Emby.Server.Implementations.HttpServer responseHeaders = new Dictionary<string, string>(); } - string expires; - if (addCachePrevention && !responseHeaders.TryGetValue("Expires", out expires)) + if (addCachePrevention && !responseHeaders.TryGetValue("Expires", out string expires)) { responseHeaders["Expires"] = "-1"; } @@ -115,7 +114,8 @@ namespace Emby.Server.Implementations.HttpServer string compressionType = null; bool isHeadRequest = false; - if (requestContext != null) { + if (requestContext != null) + { compressionType = GetCompressionType(requestContext, content, contentType); isHeadRequest = string.Equals(requestContext.Verb, "head", StringComparison.OrdinalIgnoreCase); } @@ -142,8 +142,7 @@ namespace Emby.Server.Implementations.HttpServer responseHeaders = new Dictionary<string, string>(); } - string expires; - if (addCachePrevention && !responseHeaders.TryGetValue("Expires", out expires)) + if (addCachePrevention && !responseHeaders.TryGetValue("Expires", out string expires)) { responseHeaders["Expires"] = "-1"; } @@ -187,8 +186,7 @@ namespace Emby.Server.Implementations.HttpServer responseHeaders = new Dictionary<string, string>(); } - string expires; - if (addCachePrevention && !responseHeaders.TryGetValue("Expires", out expires)) + if (addCachePrevention && !responseHeaders.TryGetValue("Expires", out string expires)) { responseHeaders["Expires"] = "-1"; } @@ -207,7 +205,7 @@ namespace Emby.Server.Implementations.HttpServer { if (result == null) { - throw new ArgumentNullException("result"); + throw new ArgumentNullException(nameof(result)); } if (responseHeaders == null) @@ -245,7 +243,7 @@ namespace Emby.Server.Implementations.HttpServer return GetCompressionType(request); } - private string GetCompressionType(IRequest request) + private static string GetCompressionType(IRequest request) { var acceptEncoding = request.Headers["Accept-Encoding"]; @@ -265,7 +263,7 @@ namespace Emby.Server.Implementations.HttpServer } /// <summary> - /// Returns the optimized result for the IRequestContext. + /// Returns the optimized result for the IRequestContext. /// Does not use or store results in any cache. /// </summary> /// <param name="request"></param> @@ -365,7 +363,7 @@ namespace Emby.Server.Implementations.HttpServer return _brotliCompressor.Compress(bytes); } - private byte[] Deflate(byte[] bytes) + private static byte[] Deflate(byte[] bytes) { // In .NET FX incompat-ville, you can't access compressed bytes without closing DeflateStream // Which means we must use MemoryStream since you have to use ToArray() on a closed Stream @@ -379,7 +377,7 @@ namespace Emby.Server.Implementations.HttpServer } } - private byte[] GZip(byte[] buffer) + private static byte[] GZip(byte[] buffer) { using (var ms = new MemoryStream()) using (var zipStream = new GZipStream(ms, CompressionMode.Compress)) @@ -398,7 +396,7 @@ namespace Emby.Server.Implementations.HttpServer : contentType.Split(';')[0].ToLower().Trim(); } - private string SerializeToXmlString(object from) + private static string SerializeToXmlString(object from) { using (var ms = new MemoryStream()) { @@ -412,8 +410,10 @@ namespace Emby.Server.Implementations.HttpServer serializer.WriteObject(xw, from); xw.Flush(); ms.Seek(0, SeekOrigin.Begin); - var reader = new StreamReader(ms); - return reader.ReadToEnd(); + using (var reader = new StreamReader(ms)) + { + return reader.ReadToEnd(); + } } } } @@ -425,7 +425,7 @@ namespace Emby.Server.Implementations.HttpServer { responseHeaders["ETag"] = string.Format("\"{0}\"", cacheKeyString); - var noCache = (requestContext.Headers.Get("Cache-Control") ?? string.Empty).IndexOf("no-cache", StringComparison.OrdinalIgnoreCase) != -1; + bool noCache = (requestContext.Headers.Get("Cache-Control") ?? string.Empty).IndexOf("no-cache", StringComparison.OrdinalIgnoreCase) != -1; if (!noCache) { @@ -453,7 +453,7 @@ namespace Emby.Server.Implementations.HttpServer { if (string.IsNullOrEmpty(path)) { - throw new ArgumentNullException("path"); + throw new ArgumentNullException(nameof(path)); } return GetStaticFileResult(requestContext, new StaticFileResultOptions @@ -463,15 +463,14 @@ namespace Emby.Server.Implementations.HttpServer }); } - public Task<object> GetStaticFileResult(IRequest requestContext, - StaticFileResultOptions options) + public Task<object> GetStaticFileResult(IRequest requestContext, StaticFileResultOptions options) { var path = options.Path; var fileShare = options.FileShare; if (string.IsNullOrEmpty(path)) { - throw new ArgumentNullException("path"); + throw new ArgumentNullException(nameof(path)); } if (fileShare != FileShareMode.Read && fileShare != FileShareMode.ReadWrite) @@ -661,7 +660,7 @@ namespace Emby.Server.Implementations.HttpServer /// <summary> /// Adds the expires header. /// </summary> - private void AddExpiresHeader(IDictionary<string, string> responseHeaders, string cacheKey, TimeSpan? cacheDuration) + private static void AddExpiresHeader(IDictionary<string, string> responseHeaders, string cacheKey, TimeSpan? cacheDuration) { if (cacheDuration.HasValue) { @@ -678,7 +677,7 @@ namespace Emby.Server.Implementations.HttpServer /// </summary> /// <param name="responseHeaders">The responseHeaders.</param> /// <param name="lastDateModified">The last date modified.</param> - private void AddAgeHeader(IDictionary<string, string> responseHeaders, DateTime? lastDateModified) + private static void AddAgeHeader(IDictionary<string, string> responseHeaders, DateTime? lastDateModified) { if (lastDateModified.HasValue) { @@ -699,36 +698,26 @@ namespace Emby.Server.Implementations.HttpServer var ifModifiedSinceHeader = requestContext.Headers.Get("If-Modified-Since"); - if (!string.IsNullOrEmpty(ifModifiedSinceHeader)) + if (!string.IsNullOrEmpty(ifModifiedSinceHeader) + && DateTime.TryParse(ifModifiedSinceHeader, out DateTime ifModifiedSince) + && IsNotModified(ifModifiedSince.ToUniversalTime(), cacheDuration, lastDateModified)) { - DateTime ifModifiedSince; - - if (DateTime.TryParse(ifModifiedSinceHeader, out ifModifiedSince)) - { - if (IsNotModified(ifModifiedSince.ToUniversalTime(), cacheDuration, lastDateModified)) - { - return true; - } - } + return true; } var ifNoneMatchHeader = requestContext.Headers.Get("If-None-Match"); - var hasCacheKey = !cacheKey.Equals(Guid.Empty); + bool hasCacheKey = !cacheKey.Equals(Guid.Empty); // Validate If-None-Match - if ((hasCacheKey || !string.IsNullOrEmpty(ifNoneMatchHeader))) + if ((hasCacheKey && !string.IsNullOrEmpty(ifNoneMatchHeader))) { - Guid ifNoneMatch; - ifNoneMatchHeader = (ifNoneMatchHeader ?? string.Empty).Trim('\"'); - if (Guid.TryParse(ifNoneMatchHeader, out ifNoneMatch)) + if (Guid.TryParse(ifNoneMatchHeader, out var ifNoneMatch) + && cacheKey.Equals(ifNoneMatch)) { - if (hasCacheKey && cacheKey.Equals(ifNoneMatch)) - { - return true; - } + return true; } } @@ -771,7 +760,7 @@ namespace Emby.Server.Implementations.HttpServer /// </summary> /// <param name="date">The date.</param> /// <returns>DateTime.</returns> - private DateTime NormalizeDateForComparison(DateTime date) + private static DateTime NormalizeDateForComparison(DateTime date) { return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind); } @@ -781,7 +770,7 @@ namespace Emby.Server.Implementations.HttpServer /// </summary> /// <param name="hasHeaders">The has options.</param> /// <param name="responseHeaders">The response headers.</param> - private void AddResponseHeaders(IHasHeaders hasHeaders, IEnumerable<KeyValuePair<string, string>> responseHeaders) + private static void AddResponseHeaders(IHasHeaders hasHeaders, IEnumerable<KeyValuePair<string, string>> responseHeaders) { foreach (var item in responseHeaders) { |
