RestHandler.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.Serialization;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Model.Services;
  6. namespace ServiceStack.Host
  7. {
  8. public class RestHandler
  9. {
  10. public string RequestName { get; set; }
  11. public async Task<object> HandleResponseAsync(object response)
  12. {
  13. var taskResponse = response as Task;
  14. if (taskResponse == null)
  15. {
  16. return response;
  17. }
  18. await taskResponse.ConfigureAwait(false);
  19. var taskResult = ServiceStackHost.Instance.GetTaskResult(taskResponse, RequestName);
  20. var taskResults = taskResult as Task[];
  21. if (taskResults == null)
  22. {
  23. var subTask = taskResult as Task;
  24. if (subTask != null)
  25. taskResult = ServiceStackHost.Instance.GetTaskResult(subTask, RequestName);
  26. return taskResult;
  27. }
  28. if (taskResults.Length == 0)
  29. {
  30. return new object[] { };
  31. }
  32. var firstResponse = ServiceStackHost.Instance.GetTaskResult(taskResults[0], RequestName);
  33. var batchedResponses = firstResponse != null
  34. ? (object[])Array.CreateInstance(firstResponse.GetType(), taskResults.Length)
  35. : new object[taskResults.Length];
  36. batchedResponses[0] = firstResponse;
  37. for (var i = 1; i < taskResults.Length; i++)
  38. {
  39. batchedResponses[i] = ServiceStackHost.Instance.GetTaskResult(taskResults[i], RequestName);
  40. }
  41. return batchedResponses;
  42. }
  43. protected static object CreateContentTypeRequest(IRequest httpReq, Type requestType, string contentType)
  44. {
  45. if (!string.IsNullOrEmpty(contentType) && httpReq.ContentLength > 0)
  46. {
  47. var deserializer = ContentTypes.Instance.GetStreamDeserializer(contentType);
  48. if (deserializer != null)
  49. {
  50. return deserializer(requestType, httpReq.InputStream);
  51. }
  52. }
  53. return ServiceStackHost.Instance.CreateInstance(requestType); //Return an empty DTO, even for empty request bodies
  54. }
  55. protected static object GetCustomRequestFromBinder(IRequest httpReq, Type requestType)
  56. {
  57. Func<IRequest, object> requestFactoryFn;
  58. ServiceStackHost.Instance.ServiceController.RequestTypeFactoryMap.TryGetValue(
  59. requestType, out requestFactoryFn);
  60. return requestFactoryFn != null ? requestFactoryFn(httpReq) : null;
  61. }
  62. public static RestPath FindMatchingRestPath(string httpMethod, string pathInfo, out string contentType)
  63. {
  64. pathInfo = GetSanitizedPathInfo(pathInfo, out contentType);
  65. return ServiceStackHost.Instance.ServiceController.GetRestPathForRequest(httpMethod, pathInfo);
  66. }
  67. public static string GetSanitizedPathInfo(string pathInfo, out string contentType)
  68. {
  69. contentType = null;
  70. var pos = pathInfo.LastIndexOf('.');
  71. if (pos >= 0)
  72. {
  73. var format = pathInfo.Substring(pos + 1);
  74. contentType = GetFormatContentType(format);
  75. if (contentType != null)
  76. {
  77. pathInfo = pathInfo.Substring(0, pos);
  78. }
  79. }
  80. return pathInfo;
  81. }
  82. private static string GetFormatContentType(string format)
  83. {
  84. //built-in formats
  85. if (format == "json")
  86. return "application/json";
  87. if (format == "xml")
  88. return "application/xml";
  89. return null;
  90. }
  91. public RestPath GetRestPath(string httpMethod, string pathInfo)
  92. {
  93. if (this.RestPath == null)
  94. {
  95. string contentType;
  96. this.RestPath = FindMatchingRestPath(httpMethod, pathInfo, out contentType);
  97. if (contentType != null)
  98. ResponseContentType = contentType;
  99. }
  100. return this.RestPath;
  101. }
  102. public RestPath RestPath { get; set; }
  103. // Set from SSHHF.GetHandlerForPathInfo()
  104. public string ResponseContentType { get; set; }
  105. public async Task ProcessRequestAsync(IRequest httpReq, IResponse httpRes, string operationName)
  106. {
  107. var appHost = ServiceStackHost.Instance;
  108. var restPath = GetRestPath(httpReq.Verb, httpReq.PathInfo);
  109. if (restPath == null)
  110. {
  111. throw new NotSupportedException("No RestPath found for: " + httpReq.Verb + " " + httpReq.PathInfo);
  112. }
  113. httpReq.SetRoute(restPath);
  114. if (ResponseContentType != null)
  115. httpReq.ResponseContentType = ResponseContentType;
  116. var request = httpReq.Dto = CreateRequest(httpReq, restPath);
  117. if (appHost.ApplyRequestFilters(httpReq, httpRes, request))
  118. return;
  119. var rawResponse = await ServiceStackHost.Instance.ServiceController.Execute(request, httpReq).ConfigureAwait(false);
  120. if (httpRes.IsClosed)
  121. return;
  122. var response = await HandleResponseAsync(rawResponse).ConfigureAwait(false);
  123. if (appHost.ApplyResponseFilters(httpReq, httpRes, response))
  124. return;
  125. await httpRes.WriteToResponse(httpReq, response).ConfigureAwait(false);
  126. }
  127. public static object CreateRequest(IRequest httpReq, RestPath restPath)
  128. {
  129. var dtoFromBinder = GetCustomRequestFromBinder(httpReq, restPath.RequestType);
  130. if (dtoFromBinder != null)
  131. return dtoFromBinder;
  132. var requestParams = httpReq.GetFlattenedRequestParams();
  133. return CreateRequest(httpReq, restPath, requestParams);
  134. }
  135. public static object CreateRequest(IRequest httpReq, RestPath restPath, Dictionary<string, string> requestParams)
  136. {
  137. var requestDto = CreateContentTypeRequest(httpReq, restPath.RequestType, httpReq.ContentType);
  138. return CreateRequest(httpReq, restPath, requestParams, requestDto);
  139. }
  140. public static object CreateRequest(IRequest httpReq, RestPath restPath, Dictionary<string, string> requestParams, object requestDto)
  141. {
  142. string contentType;
  143. var pathInfo = !restPath.IsWildCardPath
  144. ? GetSanitizedPathInfo(httpReq.PathInfo, out contentType)
  145. : httpReq.PathInfo;
  146. return restPath.CreateRequest(pathInfo, requestParams, requestDto);
  147. }
  148. /// <summary>
  149. /// Used in Unit tests
  150. /// </summary>
  151. /// <returns></returns>
  152. public object CreateRequest(IRequest httpReq, string operationName)
  153. {
  154. if (this.RestPath == null)
  155. throw new ArgumentNullException("No RestPath found");
  156. return CreateRequest(httpReq, this.RestPath);
  157. }
  158. }
  159. }