aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Services/ServiceHandler.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Emby.Server.Implementations/Services/ServiceHandler.cs')
-rw-r--r--Emby.Server.Implementations/Services/ServiceHandler.cs107
1 files changed, 38 insertions, 69 deletions
diff --git a/Emby.Server.Implementations/Services/ServiceHandler.cs b/Emby.Server.Implementations/Services/ServiceHandler.cs
index d32fce1c7..934560de3 100644
--- a/Emby.Server.Implementations/Services/ServiceHandler.cs
+++ b/Emby.Server.Implementations/Services/ServiceHandler.cs
@@ -5,20 +5,21 @@ using System.Threading;
using System.Threading.Tasks;
using Emby.Server.Implementations.HttpServer;
using MediaBrowser.Model.Services;
+using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace Emby.Server.Implementations.Services
{
public class ServiceHandler
{
- public RestPath RestPath { get; }
+ private RestPath _restPath;
- public string ResponseContentType { get; }
+ private string _responseContentType;
internal ServiceHandler(RestPath restPath, string responseContentType)
{
- RestPath = restPath;
- ResponseContentType = responseContentType;
+ _restPath = restPath;
+ _responseContentType = responseContentType;
}
protected static Task<object> CreateContentTypeRequest(HttpListenerHost host, IRequest httpReq, Type requestType, string contentType)
@@ -54,7 +55,7 @@ namespace Emby.Server.Implementations.Services
private static string GetFormatContentType(string format)
{
- //built-in formats
+ // built-in formats
switch (format)
{
case "json": return "application/json";
@@ -63,16 +64,16 @@ namespace Emby.Server.Implementations.Services
}
}
- public async Task ProcessRequestAsync(HttpListenerHost httpHost, IRequest httpReq, IResponse httpRes, ILogger logger, CancellationToken cancellationToken)
+ public async Task ProcessRequestAsync(HttpListenerHost httpHost, IRequest httpReq, HttpResponse httpRes, ILogger logger, CancellationToken cancellationToken)
{
- httpReq.Items["__route"] = RestPath;
+ httpReq.Items["__route"] = _restPath;
- if (ResponseContentType != null)
+ if (_responseContentType != null)
{
- httpReq.ResponseContentType = ResponseContentType;
+ httpReq.ResponseContentType = _responseContentType;
}
- var request = httpReq.Dto = await CreateRequest(httpHost, httpReq, RestPath, logger).ConfigureAwait(false);
+ var request = await CreateRequest(httpHost, httpReq, _restPath, logger).ConfigureAwait(false);
httpHost.ApplyRequestFilters(httpReq, httpRes, request);
@@ -94,7 +95,7 @@ namespace Emby.Server.Implementations.Services
if (RequireqRequestStream(requestType))
{
// Used by IRequiresRequestStream
- var requestParams = await GetRequestParams(httpReq).ConfigureAwait(false);
+ var requestParams = GetRequestParams(httpReq.Response.HttpContext.Request);
var request = ServiceHandler.CreateRequest(httpReq, restPath, requestParams, host.CreateInstance(requestType));
var rawReq = (IRequiresRequestStream)request;
@@ -103,7 +104,7 @@ namespace Emby.Server.Implementations.Services
}
else
{
- var requestParams = await GetFlattenedRequestParams(httpReq).ConfigureAwait(false);
+ var requestParams = GetFlattenedRequestParams(httpReq.Response.HttpContext.Request);
var requestDto = await CreateContentTypeRequest(host, httpReq, restPath.RequestType, httpReq.ContentType).ConfigureAwait(false);
@@ -121,7 +122,7 @@ namespace Emby.Server.Implementations.Services
public static object CreateRequest(IRequest httpReq, RestPath restPath, Dictionary<string, string> requestParams, object requestDto)
{
var pathInfo = !restPath.IsWildCardPath
- ? GetSanitizedPathInfo(httpReq.PathInfo, out string contentType)
+ ? GetSanitizedPathInfo(httpReq.PathInfo, out _)
: httpReq.PathInfo;
return restPath.CreateRequest(pathInfo, requestParams, requestDto);
@@ -130,56 +131,41 @@ namespace Emby.Server.Implementations.Services
/// <summary>
/// Duplicate Params are given a unique key by appending a #1 suffix
/// </summary>
- private static async Task<Dictionary<string, string>> GetRequestParams(IRequest request)
+ private static Dictionary<string, string> GetRequestParams(HttpRequest request)
{
var map = new Dictionary<string, string>();
- foreach (var name in request.QueryString.Keys)
+ foreach (var pair in request.Query)
{
- if (name == null)
- {
- // thank you ASP.NET
- continue;
- }
-
- var values = request.QueryString[name];
+ var values = pair.Value;
if (values.Count == 1)
{
- map[name] = values[0];
+ map[pair.Key] = values[0];
}
else
{
for (var i = 0; i < values.Count; i++)
{
- map[name + (i == 0 ? "" : "#" + i)] = values[i];
+ map[pair.Key + (i == 0 ? string.Empty : "#" + i)] = values[i];
}
}
}
- if ((IsMethod(request.Verb, "POST") || IsMethod(request.Verb, "PUT")))
+ if ((IsMethod(request.Method, "POST") || IsMethod(request.Method, "PUT"))
+ && request.HasFormContentType)
{
- var formData = await request.GetFormData().ConfigureAwait(false);
- if (formData != null)
+ foreach (var pair in request.Form)
{
- foreach (var name in formData.Keys)
+ var values = pair.Value;
+ if (values.Count == 1)
{
- if (name == null)
- {
- // thank you ASP.NET
- continue;
- }
-
- var values = formData.GetValues(name);
- if (values.Count == 1)
- {
- map[name] = values[0];
- }
- else
+ map[pair.Key] = values[0];
+ }
+ else
+ {
+ for (var i = 0; i < values.Count; i++)
{
- for (var i = 0; i < values.Count; i++)
- {
- map[name + (i == 0 ? "" : "#" + i)] = values[i];
- }
+ map[pair.Key + (i == 0 ? string.Empty : "#" + i)] = values[i];
}
}
}
@@ -189,43 +175,26 @@ namespace Emby.Server.Implementations.Services
}
private static bool IsMethod(string method, string expected)
- {
- return string.Equals(method, expected, StringComparison.OrdinalIgnoreCase);
- }
+ => string.Equals(method, expected, StringComparison.OrdinalIgnoreCase);
/// <summary>
/// Duplicate params have their values joined together in a comma-delimited string
/// </summary>
- private static async Task<Dictionary<string, string>> GetFlattenedRequestParams(IRequest request)
+ private static Dictionary<string, string> GetFlattenedRequestParams(HttpRequest request)
{
var map = new Dictionary<string, string>();
- foreach (var name in request.QueryString.Keys)
+ foreach (var pair in request.Query)
{
- if (name == null)
- {
- // thank you ASP.NET
- continue;
- }
-
- map[name] = request.QueryString[name];
+ map[pair.Key] = pair.Value;
}
- if ((IsMethod(request.Verb, "POST") || IsMethod(request.Verb, "PUT")))
+ if ((IsMethod(request.Method, "POST") || IsMethod(request.Method, "PUT"))
+ && request.HasFormContentType)
{
- var formData = await request.GetFormData().ConfigureAwait(false);
- if (formData != null)
+ foreach (var pair in request.Form)
{
- foreach (var name in formData.Keys)
- {
- if (name == null)
- {
- // thank you ASP.NET
- continue;
- }
-
- map[name] = formData[name];
- }
+ map[pair.Key] = pair.Value;
}
}