aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/HttpServer/HttpResultFactory.cs
diff options
context:
space:
mode:
authorLogicalPhallacy <44458166+LogicalPhallacy@users.noreply.github.com>2019-01-23 00:31:35 -0800
committerGitHub <noreply@github.com>2019-01-23 00:31:35 -0800
commit404bd04cbc17dc8c8bf4a5c9aa3ca9c5cd85aa68 (patch)
tree3d267c6ceef9439a034c113095e10e4d619e7c70 /Emby.Server.Implementations/HttpServer/HttpResultFactory.cs
parent8ff89fdc0c30f595a171ffc550f907ef22b6212a (diff)
parente05e002b8bb4d13eb2b80b56a0aad8903ddb701e (diff)
Merge pull request #8 from jellyfin/master
rebase to latest master
Diffstat (limited to 'Emby.Server.Implementations/HttpServer/HttpResultFactory.cs')
-rw-r--r--Emby.Server.Implementations/HttpServer/HttpResultFactory.cs83
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)
{