RestHandler.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 subTask = taskResult as Task;
  21. if (subTask != null)
  22. {
  23. taskResult = ServiceStackHost.Instance.GetTaskResult(subTask, RequestName);
  24. }
  25. return taskResult;
  26. }
  27. protected static object CreateContentTypeRequest(IRequest httpReq, Type requestType, string contentType)
  28. {
  29. if (!string.IsNullOrEmpty(contentType) && httpReq.ContentLength > 0)
  30. {
  31. var deserializer = ContentTypes.Instance.GetStreamDeserializer(contentType);
  32. if (deserializer != null)
  33. {
  34. return deserializer(requestType, httpReq.InputStream);
  35. }
  36. }
  37. return ServiceStackHost.Instance.CreateInstance(requestType); //Return an empty DTO, even for empty request bodies
  38. }
  39. protected static object GetCustomRequestFromBinder(IRequest httpReq, Type requestType)
  40. {
  41. Func<IRequest, object> requestFactoryFn;
  42. ServiceStackHost.Instance.ServiceController.RequestTypeFactoryMap.TryGetValue(
  43. requestType, out requestFactoryFn);
  44. return requestFactoryFn != null ? requestFactoryFn(httpReq) : null;
  45. }
  46. public static RestPath FindMatchingRestPath(string httpMethod, string pathInfo, out string contentType)
  47. {
  48. pathInfo = GetSanitizedPathInfo(pathInfo, out contentType);
  49. return ServiceStackHost.Instance.ServiceController.GetRestPathForRequest(httpMethod, pathInfo);
  50. }
  51. public static string GetSanitizedPathInfo(string pathInfo, out string contentType)
  52. {
  53. contentType = null;
  54. var pos = pathInfo.LastIndexOf('.');
  55. if (pos >= 0)
  56. {
  57. var format = pathInfo.Substring(pos + 1);
  58. contentType = GetFormatContentType(format);
  59. if (contentType != null)
  60. {
  61. pathInfo = pathInfo.Substring(0, pos);
  62. }
  63. }
  64. return pathInfo;
  65. }
  66. private static string GetFormatContentType(string format)
  67. {
  68. //built-in formats
  69. if (format == "json")
  70. return "application/json";
  71. if (format == "xml")
  72. return "application/xml";
  73. return null;
  74. }
  75. public RestPath GetRestPath(string httpMethod, string pathInfo)
  76. {
  77. if (this.RestPath == null)
  78. {
  79. string contentType;
  80. this.RestPath = FindMatchingRestPath(httpMethod, pathInfo, out contentType);
  81. if (contentType != null)
  82. ResponseContentType = contentType;
  83. }
  84. return this.RestPath;
  85. }
  86. public RestPath RestPath { get; set; }
  87. // Set from SSHHF.GetHandlerForPathInfo()
  88. public string ResponseContentType { get; set; }
  89. public async Task ProcessRequestAsync(IRequest httpReq, IResponse httpRes, string operationName)
  90. {
  91. var appHost = ServiceStackHost.Instance;
  92. var restPath = GetRestPath(httpReq.Verb, httpReq.PathInfo);
  93. if (restPath == null)
  94. {
  95. throw new NotSupportedException("No RestPath found for: " + httpReq.Verb + " " + httpReq.PathInfo);
  96. }
  97. httpReq.SetRoute(restPath);
  98. if (ResponseContentType != null)
  99. httpReq.ResponseContentType = ResponseContentType;
  100. var request = httpReq.Dto = CreateRequest(httpReq, restPath);
  101. appHost.ApplyRequestFilters(httpReq, httpRes, request);
  102. var rawResponse = await ServiceStackHost.Instance.ServiceController.Execute(request, httpReq).ConfigureAwait(false);
  103. var response = await HandleResponseAsync(rawResponse).ConfigureAwait(false);
  104. appHost.ApplyResponseFilters(httpReq, httpRes, response);
  105. await httpRes.WriteToResponse(httpReq, response).ConfigureAwait(false);
  106. }
  107. public static object CreateRequest(IRequest httpReq, RestPath restPath)
  108. {
  109. var dtoFromBinder = GetCustomRequestFromBinder(httpReq, restPath.RequestType);
  110. if (dtoFromBinder != null)
  111. return dtoFromBinder;
  112. var requestParams = httpReq.GetFlattenedRequestParams();
  113. return CreateRequest(httpReq, restPath, requestParams);
  114. }
  115. public static object CreateRequest(IRequest httpReq, RestPath restPath, Dictionary<string, string> requestParams)
  116. {
  117. var requestDto = CreateContentTypeRequest(httpReq, restPath.RequestType, httpReq.ContentType);
  118. return CreateRequest(httpReq, restPath, requestParams, requestDto);
  119. }
  120. public static object CreateRequest(IRequest httpReq, RestPath restPath, Dictionary<string, string> requestParams, object requestDto)
  121. {
  122. string contentType;
  123. var pathInfo = !restPath.IsWildCardPath
  124. ? GetSanitizedPathInfo(httpReq.PathInfo, out contentType)
  125. : httpReq.PathInfo;
  126. return restPath.CreateRequest(pathInfo, requestParams, requestDto);
  127. }
  128. /// <summary>
  129. /// Used in Unit tests
  130. /// </summary>
  131. /// <returns></returns>
  132. public object CreateRequest(IRequest httpReq, string operationName)
  133. {
  134. if (this.RestPath == null)
  135. throw new ArgumentNullException("No RestPath found");
  136. return CreateRequest(httpReq, this.RestPath);
  137. }
  138. }
  139. }