2016-11-11 19:55:12 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Threading.Tasks;
|
2017-02-13 01:07:48 +00:00
|
|
|
using Emby.Server.Implementations.HttpServer;
|
2018-12-13 13:18:25 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2016-11-11 19:55:12 +00:00
|
|
|
using MediaBrowser.Model.Services;
|
|
|
|
|
2017-02-13 01:07:48 +00:00
|
|
|
namespace Emby.Server.Implementations.Services
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
public delegate Task<object> InstanceExecFn(IRequest requestContext, object intance, object request);
|
|
|
|
public delegate object ActionInvokerFn(object intance, object request);
|
|
|
|
public delegate void VoidActionInvokerFn(object intance, object request);
|
|
|
|
|
|
|
|
public class ServiceController
|
|
|
|
{
|
2017-02-13 01:07:48 +00:00
|
|
|
public static ServiceController Instance;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2017-08-09 19:56:38 +00:00
|
|
|
public ServiceController()
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2017-02-13 01:07:48 +00:00
|
|
|
Instance = this;
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 19:56:38 +00:00
|
|
|
public void Init(HttpListenerHost appHost, Type[] serviceTypes)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2017-08-09 19:56:38 +00:00
|
|
|
foreach (var serviceType in serviceTypes)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2017-02-13 01:07:48 +00:00
|
|
|
RegisterService(appHost, serviceType);
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 01:07:48 +00:00
|
|
|
public void RegisterService(HttpListenerHost appHost, Type serviceType)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
var processedReqs = new HashSet<Type>();
|
|
|
|
|
|
|
|
var actions = ServiceExecGeneral.Reset(serviceType);
|
|
|
|
|
|
|
|
foreach (var mi in serviceType.GetActions())
|
|
|
|
{
|
|
|
|
var requestType = mi.GetParameters()[0].ParameterType;
|
|
|
|
if (processedReqs.Contains(requestType)) continue;
|
|
|
|
processedReqs.Add(requestType);
|
|
|
|
|
|
|
|
ServiceExecGeneral.CreateServiceRunnersFor(requestType, actions);
|
|
|
|
|
2017-08-19 19:43:35 +00:00
|
|
|
//var returnMarker = GetTypeWithGenericTypeDefinitionOf(requestType, typeof(IReturn<>));
|
|
|
|
//var responseType = returnMarker != null ?
|
|
|
|
// GetGenericArguments(returnMarker)[0]
|
|
|
|
// : mi.ReturnType != typeof(object) && mi.ReturnType != typeof(void) ?
|
|
|
|
// mi.ReturnType
|
|
|
|
// : Type.GetType(requestType.FullName + "Response");
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
RegisterRestPaths(appHost, requestType, serviceType);
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2017-08-19 19:43:35 +00:00
|
|
|
appHost.AddServiceInfo(serviceType, requestType);
|
2017-02-13 02:06:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Type FirstGenericType(Type type)
|
|
|
|
{
|
|
|
|
while (type != null)
|
|
|
|
{
|
|
|
|
if (type.GetTypeInfo().IsGenericType)
|
|
|
|
return type;
|
|
|
|
|
|
|
|
type = type.GetTypeInfo().BaseType;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public readonly RestPath.RestPathMap RestPathMap = new RestPath.RestPathMap();
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public void RegisterRestPaths(HttpListenerHost appHost, Type requestType, Type serviceType)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
var attrs = appHost.GetRouteAttributes(requestType);
|
2017-02-13 02:06:54 +00:00
|
|
|
foreach (RouteAttribute attr in attrs)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var restPath = new RestPath(appHost.CreateInstance, appHost.GetParseFn, requestType, serviceType, attr.Path, attr.Verbs, attr.IsHidden, attr.Summary, attr.Description);
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
RegisterRestPath(restPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static readonly char[] InvalidRouteChars = new[] { '?', '&' };
|
|
|
|
|
|
|
|
public void RegisterRestPath(RestPath restPath)
|
|
|
|
{
|
|
|
|
if (!restPath.Path.StartsWith("/"))
|
2017-02-13 20:54:28 +00:00
|
|
|
throw new ArgumentException(string.Format("Route '{0}' on '{1}' must start with a '/'", restPath.Path, restPath.RequestType.GetMethodName()));
|
2016-11-11 19:55:12 +00:00
|
|
|
if (restPath.Path.IndexOfAny(InvalidRouteChars) != -1)
|
2017-08-31 03:49:38 +00:00
|
|
|
throw new ArgumentException(string.Format("Route '{0}' on '{1}' contains invalid chars. ", restPath.Path, restPath.RequestType.GetMethodName()));
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
List<RestPath> pathsAtFirstMatch;
|
|
|
|
if (!RestPathMap.TryGetValue(restPath.FirstMatchHashKey, out pathsAtFirstMatch))
|
|
|
|
{
|
|
|
|
pathsAtFirstMatch = new List<RestPath>();
|
|
|
|
RestPathMap[restPath.FirstMatchHashKey] = pathsAtFirstMatch;
|
|
|
|
}
|
|
|
|
pathsAtFirstMatch.Add(restPath);
|
|
|
|
}
|
|
|
|
|
2018-12-14 19:17:29 +00:00
|
|
|
public RestPath GetRestPathForRequest(string httpMethod, string pathInfo)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
var matchUsingPathParts = RestPath.GetPathPartsForMatching(pathInfo);
|
|
|
|
|
|
|
|
List<RestPath> firstMatches;
|
|
|
|
|
|
|
|
var yieldedHashMatches = RestPath.GetFirstMatchHashKeys(matchUsingPathParts);
|
|
|
|
foreach (var potentialHashMatch in yieldedHashMatches)
|
|
|
|
{
|
2017-02-12 22:53:14 +00:00
|
|
|
if (!this.RestPathMap.TryGetValue(potentialHashMatch, out firstMatches))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
var bestScore = -1;
|
2018-09-12 17:26:21 +00:00
|
|
|
RestPath bestMatch = null;
|
2016-11-11 19:55:12 +00:00
|
|
|
foreach (var restPath in firstMatches)
|
|
|
|
{
|
2018-12-14 19:17:29 +00:00
|
|
|
var score = restPath.MatchScore(httpMethod, matchUsingPathParts);
|
2018-09-12 17:26:21 +00:00
|
|
|
if (score > bestScore)
|
|
|
|
{
|
|
|
|
bestScore = score;
|
|
|
|
bestMatch = restPath;
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
2017-02-12 22:53:14 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (bestScore > 0 && bestMatch != null)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
return bestMatch;
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var yieldedWildcardMatches = RestPath.GetFirstMatchWildCardHashKeys(matchUsingPathParts);
|
|
|
|
foreach (var potentialHashMatch in yieldedWildcardMatches)
|
|
|
|
{
|
|
|
|
if (!this.RestPathMap.TryGetValue(potentialHashMatch, out firstMatches)) continue;
|
|
|
|
|
|
|
|
var bestScore = -1;
|
2018-09-12 17:26:21 +00:00
|
|
|
RestPath bestMatch = null;
|
2016-11-11 19:55:12 +00:00
|
|
|
foreach (var restPath in firstMatches)
|
|
|
|
{
|
2018-12-14 19:17:29 +00:00
|
|
|
var score = restPath.MatchScore(httpMethod, matchUsingPathParts);
|
2018-09-12 17:26:21 +00:00
|
|
|
if (score > bestScore)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
bestScore = score;
|
|
|
|
bestMatch = restPath;
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
if (bestScore > 0 && bestMatch != null)
|
|
|
|
{
|
|
|
|
return bestMatch;
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-05-26 06:48:54 +00:00
|
|
|
public Task<object> Execute(HttpListenerHost appHost, object requestDto, IRequest req)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
req.Dto = requestDto;
|
|
|
|
var requestType = requestDto.GetType();
|
|
|
|
req.OperationName = requestType.Name;
|
|
|
|
|
2017-02-13 01:07:48 +00:00
|
|
|
var serviceType = appHost.GetServiceTypeByRequest(requestType);
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2017-02-13 01:07:48 +00:00
|
|
|
var service = appHost.CreateInstance(serviceType);
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
//var service = typeFactory.CreateInstance(serviceType);
|
|
|
|
|
|
|
|
var serviceRequiresContext = service as IRequiresRequest;
|
|
|
|
if (serviceRequiresContext != null)
|
|
|
|
{
|
|
|
|
serviceRequiresContext.Request = req;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.Dto == null) // Don't override existing batched DTO[]
|
|
|
|
req.Dto = requestDto;
|
|
|
|
|
|
|
|
//Executes the service and returns the result
|
2017-05-26 06:48:54 +00:00
|
|
|
return ServiceExecGeneral.Execute(serviceType, req, service, requestDto, requestType.GetMethodName());
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 13:18:25 +00:00
|
|
|
}
|