ServiceHandler.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Net.Mime;
  5. using System.Reflection;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Emby.Server.Implementations.HttpServer;
  9. using MediaBrowser.Model.Services;
  10. using Microsoft.AspNetCore.Http;
  11. using Microsoft.Extensions.Logging;
  12. namespace Emby.Server.Implementations.Services
  13. {
  14. public class ServiceHandler
  15. {
  16. private RestPath _restPath;
  17. private string _responseContentType;
  18. internal ServiceHandler(RestPath restPath, string responseContentType)
  19. {
  20. _restPath = restPath;
  21. _responseContentType = responseContentType;
  22. }
  23. protected static Task<object> CreateContentTypeRequest(HttpListenerHost host, IRequest httpReq, Type requestType, string contentType)
  24. {
  25. if (!string.IsNullOrEmpty(contentType) && httpReq.ContentLength > 0)
  26. {
  27. var deserializer = RequestHelper.GetRequestReader(host, contentType);
  28. if (deserializer != null)
  29. {
  30. return deserializer.Invoke(requestType, httpReq.InputStream);
  31. }
  32. }
  33. return Task.FromResult(host.CreateInstance(requestType));
  34. }
  35. public static string GetSanitizedPathInfo(string pathInfo, out string contentType)
  36. {
  37. contentType = null;
  38. var pos = pathInfo.LastIndexOf('.');
  39. if (pos != -1)
  40. {
  41. var format = pathInfo.AsSpan().Slice(pos + 1);
  42. contentType = GetFormatContentType(format);
  43. if (contentType != null)
  44. {
  45. pathInfo = pathInfo.Substring(0, pos);
  46. }
  47. }
  48. return pathInfo;
  49. }
  50. private static string GetFormatContentType(ReadOnlySpan<char> format)
  51. {
  52. if (format.Equals("json", StringComparison.Ordinal))
  53. {
  54. return MediaTypeNames.Application.Json;
  55. }
  56. else if (format.Equals("xml", StringComparison.Ordinal))
  57. {
  58. return MediaTypeNames.Application.Xml;
  59. }
  60. return null;
  61. }
  62. public async Task ProcessRequestAsync(HttpListenerHost httpHost, IRequest httpReq, HttpResponse httpRes, ILogger logger, CancellationToken cancellationToken)
  63. {
  64. httpReq.Items["__route"] = _restPath;
  65. if (_responseContentType != null)
  66. {
  67. httpReq.ResponseContentType = _responseContentType;
  68. }
  69. var request = await CreateRequest(httpHost, httpReq, _restPath, logger).ConfigureAwait(false);
  70. httpHost.ApplyRequestFilters(httpReq, httpRes, request);
  71. var response = await httpHost.ServiceController.Execute(httpHost, request, httpReq).ConfigureAwait(false);
  72. // Apply response filters
  73. foreach (var responseFilter in httpHost.ResponseFilters)
  74. {
  75. responseFilter(httpReq, httpRes, response);
  76. }
  77. await ResponseHelper.WriteToResponse(httpRes, httpReq, response, cancellationToken).ConfigureAwait(false);
  78. }
  79. public static async Task<object> CreateRequest(HttpListenerHost host, IRequest httpReq, RestPath restPath, ILogger logger)
  80. {
  81. var requestType = restPath.RequestType;
  82. if (RequireqRequestStream(requestType))
  83. {
  84. // Used by IRequiresRequestStream
  85. var requestParams = GetRequestParams(httpReq.Response.HttpContext.Request);
  86. var request = ServiceHandler.CreateRequest(httpReq, restPath, requestParams, host.CreateInstance(requestType));
  87. var rawReq = (IRequiresRequestStream)request;
  88. rawReq.RequestStream = httpReq.InputStream;
  89. return rawReq;
  90. }
  91. else
  92. {
  93. var requestParams = GetFlattenedRequestParams(httpReq.Response.HttpContext.Request);
  94. var requestDto = await CreateContentTypeRequest(host, httpReq, restPath.RequestType, httpReq.ContentType).ConfigureAwait(false);
  95. return CreateRequest(httpReq, restPath, requestParams, requestDto);
  96. }
  97. }
  98. public static bool RequireqRequestStream(Type requestType)
  99. {
  100. var requiresRequestStreamTypeInfo = typeof(IRequiresRequestStream).GetTypeInfo();
  101. return requiresRequestStreamTypeInfo.IsAssignableFrom(requestType.GetTypeInfo());
  102. }
  103. public static object CreateRequest(IRequest httpReq, RestPath restPath, Dictionary<string, string> requestParams, object requestDto)
  104. {
  105. var pathInfo = !restPath.IsWildCardPath
  106. ? GetSanitizedPathInfo(httpReq.PathInfo, out _)
  107. : httpReq.PathInfo;
  108. return restPath.CreateRequest(pathInfo, requestParams, requestDto);
  109. }
  110. /// <summary>
  111. /// Duplicate Params are given a unique key by appending a #1 suffix
  112. /// </summary>
  113. private static Dictionary<string, string> GetRequestParams(HttpRequest request)
  114. {
  115. var map = new Dictionary<string, string>();
  116. foreach (var pair in request.Query)
  117. {
  118. var values = pair.Value;
  119. if (values.Count == 1)
  120. {
  121. map[pair.Key] = values[0];
  122. }
  123. else
  124. {
  125. for (var i = 0; i < values.Count; i++)
  126. {
  127. map[pair.Key + (i == 0 ? string.Empty : "#" + i)] = values[i];
  128. }
  129. }
  130. }
  131. if ((IsMethod(request.Method, "POST") || IsMethod(request.Method, "PUT"))
  132. && request.HasFormContentType)
  133. {
  134. foreach (var pair in request.Form)
  135. {
  136. var values = pair.Value;
  137. if (values.Count == 1)
  138. {
  139. map[pair.Key] = values[0];
  140. }
  141. else
  142. {
  143. for (var i = 0; i < values.Count; i++)
  144. {
  145. map[pair.Key + (i == 0 ? string.Empty : "#" + i)] = values[i];
  146. }
  147. }
  148. }
  149. }
  150. return map;
  151. }
  152. private static bool IsMethod(string method, string expected)
  153. => string.Equals(method, expected, StringComparison.OrdinalIgnoreCase);
  154. /// <summary>
  155. /// Duplicate params have their values joined together in a comma-delimited string.
  156. /// </summary>
  157. private static Dictionary<string, string> GetFlattenedRequestParams(HttpRequest request)
  158. {
  159. var map = new Dictionary<string, string>();
  160. foreach (var pair in request.Query)
  161. {
  162. map[pair.Key] = pair.Value;
  163. }
  164. if ((IsMethod(request.Method, "POST") || IsMethod(request.Method, "PUT"))
  165. && request.HasFormContentType)
  166. {
  167. foreach (var pair in request.Form)
  168. {
  169. map[pair.Key] = pair.Value;
  170. }
  171. }
  172. return map;
  173. }
  174. }
  175. }