ControllerBuilder.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. #if !WASM
  2. using System;
  3. using Microsoft.AspNetCore.Http;
  4. using System.Reflection;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using System.Collections.Generic;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.AspNetCore.Mvc.Routing;
  10. using System.Globalization;
  11. using System.Diagnostics;
  12. using Microsoft.AspNetCore.Routing.Template;
  13. using Microsoft.AspNetCore.Routing;
  14. using System.Threading;
  15. using Microsoft.Extensions.DependencyInjection;
  16. using Microsoft.Extensions.Logging;
  17. using System.IO;
  18. using System.Text.Json;
  19. namespace FastReport.Web.Infrastructure
  20. {
  21. internal static partial class ControllerBuilder
  22. {
  23. private static ControllerExecutor _executor;
  24. private static readonly JsonSerializerOptions _serializerOptions = new() { PropertyNameCaseInsensitive = true };
  25. public static MethodInfo[] GetControllerMethods()
  26. {
  27. return GetControllerMethods(typeof(Controllers.Controllers));
  28. }
  29. internal static MethodInfo[] GetControllerMethods(Type fromType)
  30. {
  31. return fromType.GetMethods(BindingFlags.Public | BindingFlags.Static);
  32. }
  33. public static Delegate BuildMinimalAPIDelegate(MethodInfo method)
  34. {
  35. var methodParams = method.GetParameters().Select(param => param.ParameterType).ToList();
  36. var returnType = method.ReturnType;
  37. Type delegateType;
  38. switch (methodParams.Count + 1) // plus returnType
  39. {
  40. case 1:
  41. if (returnType == typeof(void))
  42. delegateType = typeof(Action);
  43. else
  44. delegateType = typeof(Func<>);
  45. break;
  46. case 2:
  47. delegateType = typeof(Func<,>);
  48. break;
  49. case 3:
  50. delegateType = typeof(Func<,,>);
  51. break;
  52. case 4:
  53. delegateType = typeof(Func<,,,>);
  54. break;
  55. case 5:
  56. delegateType = typeof(Func<,,,,>);
  57. break;
  58. case 6:
  59. delegateType = typeof(Func<,,,,,>);
  60. break;
  61. case 7:
  62. delegateType = typeof(Func<,,,,,,>);
  63. break;
  64. case 8:
  65. delegateType = typeof(Func<,,,,,,,>);
  66. break;
  67. case 9:
  68. delegateType = typeof(Func<,,,,,,,,>);
  69. break;
  70. case 10:
  71. delegateType = typeof(Func<,,,,,,,,,>);
  72. break;
  73. default:
  74. throw new NotImplementedException();
  75. }
  76. if (methodParams.Count > 0)
  77. {
  78. methodParams.Add(returnType);
  79. delegateType = delegateType.MakeGenericType(methodParams.ToArray());
  80. }
  81. return Delegate.CreateDelegate(delegateType, method);
  82. }
  83. public static ControllerExecutor Executor => _executor;
  84. public static void InitializeControllers()
  85. {
  86. var methods = GetControllerMethods();
  87. InitializeControllers(methods);
  88. }
  89. internal static void InitializeControllers(MethodInfo[] methods)
  90. {
  91. var endpoints = BuildEndpoints(methods);
  92. _executor = new ControllerExecutor(endpoints);
  93. }
  94. internal static EndpointInfo[] BuildEndpoints(IReadOnlyCollection<MethodInfo> methods)
  95. {
  96. //var availableServices = GetFastReportServices();
  97. List<EndpointInfo> endpoints = new List<EndpointInfo>();
  98. foreach (var method in methods)
  99. {
  100. var endpoint = BuildEndpoint(method);
  101. endpoints.AddRange(endpoint);
  102. }
  103. return endpoints.ToArray();
  104. }
  105. internal static IEnumerable<EndpointInfo> BuildEndpoint(MethodInfo method)
  106. {
  107. var httpMethodAttribute = method.GetCustomAttribute<HttpMethodAttribute>();
  108. string routeTemplate = httpMethodAttribute?.Template ?? method.GetCustomAttribute<RouteAttribute>()?.Template;
  109. if (routeTemplate == null)
  110. throw new Exception($"Method '{method.Name}' doesn't have a route template. Please, use RouteAttribute or Http[Get|Post|...]Attribute with route template");
  111. var parameters = new List<Func<HttpContext, object>>();
  112. var parameterInfos = method.GetParameters();
  113. for (int i = 0; i < parameterInfos.Length; i++)
  114. {
  115. var parameterInfo = parameterInfos[i];
  116. var paramType = parameterInfo.ParameterType;
  117. // if FromQuery/FromRoute/FromBody etc.
  118. if (TryAttributesSearching(parameters, parameterInfo, paramType))
  119. continue;
  120. // if knownTypes:
  121. if (TryKnownTypesSearching(parameters, parameterInfo.Name, paramType))
  122. continue;
  123. // if exist in serviceProvider
  124. parameters.Add((httpContext) => httpContext.RequestServices.GetService(paramType));
  125. continue;
  126. }
  127. var resultHandler = BuildResultHandler(method.ReturnType);
  128. var templateMatcher = new TemplateMatcher(TemplateParser.Parse(WebUtils.ToUrl(FastReportGlobal.FastReportOptions.RouteBasePath, routeTemplate).TrimStart('/')), new RouteValueDictionary());
  129. if(httpMethodAttribute != null)
  130. foreach(var httpMethod in httpMethodAttribute.HttpMethods)
  131. {
  132. yield return new EndpointInfo(httpMethod,
  133. templateMatcher,
  134. method,
  135. parameters.ToArray(),
  136. resultHandler);
  137. }
  138. else
  139. {
  140. // if HttpMethodAttribute doesn't exists => it's GET
  141. yield return new EndpointInfo("GET",
  142. templateMatcher,
  143. method,
  144. parameters.ToArray(),
  145. resultHandler);
  146. }
  147. }
  148. private static Func<HttpContext, object, Task> BuildResultHandler(Type returnType)
  149. {
  150. PropertyInfo resultProperty;
  151. if(returnType.IsGenericType)
  152. {
  153. var generticType = returnType.GetGenericTypeDefinition();
  154. if (generticType == typeof(Task<>))
  155. {
  156. resultProperty = returnType.GetProperty(nameof(Task<object>.Result));
  157. return async (httpContext, methodResult) =>
  158. {
  159. await (Task)methodResult;
  160. var result = resultProperty.GetValue(methodResult);
  161. await BuilderTemplates.HandleResult(httpContext, result);
  162. };
  163. }
  164. else if (generticType == typeof(ValueTask<>))
  165. {
  166. resultProperty = returnType.GetProperty(nameof(ValueTask<object>.Result));
  167. return async (httpContext, methodResult) =>
  168. {
  169. // TODO: NOT WORKING
  170. await (ValueTask<object>)methodResult;
  171. var result = resultProperty.GetValue(methodResult);
  172. await BuilderTemplates.HandleResult(httpContext, result);
  173. };
  174. }
  175. }
  176. else if (returnType == typeof(Task))
  177. {
  178. return BuilderTemplates.Task_AwaitAndHandleResult;
  179. }
  180. else if (returnType == typeof(ValueTask))
  181. {
  182. return BuilderTemplates.ValueTask_AwaitAndHandleResult;
  183. }
  184. return BuilderTemplates.HandleResult;
  185. }
  186. internal static class BuilderTemplates
  187. {
  188. private static readonly IResult _SuccessResult = Results.Ok();
  189. public static async Task Task_AwaitAndHandleResult(HttpContext httpContext, object methodResult)
  190. {
  191. await(Task)methodResult;
  192. await HandleResult(httpContext, null);
  193. }
  194. public static async Task ValueTask_AwaitAndHandleResult(HttpContext httpContext, object methodResult)
  195. {
  196. await (ValueTask)methodResult;
  197. await HandleResult(httpContext, null);
  198. }
  199. public static Task HandleResult(HttpContext httpContext, object result)
  200. {
  201. if (result == null)
  202. {
  203. return _SuccessResult.ExecuteAsync(httpContext);
  204. }
  205. if (result is IResult iResult)
  206. {
  207. return iResult.ExecuteAsync(httpContext);
  208. }
  209. else
  210. {
  211. Debug.Assert(result.GetType() != typeof(IActionResult));
  212. string content = System.Text.Json.JsonSerializer.Serialize(result);
  213. var contentResult = Results.Content(content, "text/json");
  214. return contentResult.ExecuteAsync(httpContext);
  215. }
  216. }
  217. }
  218. private static bool TryKnownTypesSearching(List<Func<HttpContext, object>> parameters, string paramName, Type paramType)
  219. {
  220. if (paramType == typeof(HttpContext))
  221. {
  222. parameters.Add(static (httpContext) => httpContext);
  223. return true;
  224. }
  225. else if (paramType == typeof(HttpRequest))
  226. {
  227. parameters.Add(static (httpContext) => httpContext.Request);
  228. return true;
  229. }
  230. else if (paramType == typeof(HttpResponse))
  231. {
  232. parameters.Add(static (httpContext) => httpContext.Response);
  233. return true;
  234. }
  235. else if (paramType == typeof(CancellationToken))
  236. {
  237. parameters.Add(static (httpContext) => httpContext.RequestAborted);
  238. return true;
  239. }
  240. else if (paramType == typeof(IServiceProvider))
  241. {
  242. parameters.Add(static (httpContext) => httpContext.RequestServices);
  243. return true;
  244. }
  245. // Special case: we check primitive types
  246. else if (paramType.IsSimpleType())
  247. {
  248. parameters.Add((httpContext) =>
  249. {
  250. var request = httpContext.Request;
  251. if (request.Query.TryGetValue(paramName, out var queryValue))
  252. return ParseToType(queryValue, paramType);
  253. if (request.Headers.TryGetValue(paramName, out var headerValue))
  254. return ParseToType(headerValue, paramType);
  255. if (request.RouteValues.TryGetValue(paramName, out var routeValue))
  256. return ParseToType(routeValue, paramType);
  257. if (request.Form.TryGetValue(paramName, out var formValue))
  258. return ParseToType(formValue, paramType);
  259. return default;
  260. });
  261. return true;
  262. }
  263. return false;
  264. }
  265. private static bool TryAttributesSearching(List<Func<HttpContext, object>> parameters, ParameterInfo parameterInfo, Type paramType)
  266. {
  267. var attributes = parameterInfo.GetCustomAttributes(false);
  268. foreach (var attribute in attributes)
  269. {
  270. Func<HttpContext, object> func;
  271. if (attribute is FromQueryAttribute fromQuery)
  272. {
  273. string name = fromQuery.Name ?? parameterInfo.Name;
  274. if (paramType.IsSimpleType())
  275. func = (httpContext) =>
  276. {
  277. var queryValue = httpContext.Request.Query[name].ToString();
  278. return ParseToType(queryValue, paramType);
  279. };
  280. else
  281. func = (httpContext) =>
  282. {
  283. return DeserializeFromCollection(httpContext.Request.Query, paramType);
  284. };
  285. parameters.Add(func);
  286. return true;
  287. }
  288. else if (attribute is FromBodyAttribute)
  289. {
  290. func = (httpContext) =>
  291. {
  292. using var reader = new StreamReader(httpContext.Request.Body);
  293. var requestBody = reader.ReadToEndAsync().GetAwaiter().GetResult();
  294. return JsonSerializer.Deserialize(requestBody, paramType, _serializerOptions);
  295. };
  296. parameters.Add(func);
  297. return true;
  298. }
  299. else if (attribute is FromHeaderAttribute fromHeader)
  300. {
  301. string name = fromHeader.Name ?? parameterInfo.Name;
  302. if (paramType.IsSimpleType())
  303. func = (httpContext) =>
  304. {
  305. var queryValue = httpContext.Request.Headers[name].ToString();
  306. return ParseToType(queryValue, paramType);
  307. };
  308. else
  309. func = (httpContext) =>
  310. {
  311. return DeserializeFromCollection(httpContext.Request.Headers, paramType);
  312. };
  313. parameters.Add(func);
  314. return true;
  315. }
  316. else if (attribute is FromRouteAttribute fromRoute)
  317. {
  318. string name = fromRoute.Name ?? parameterInfo.Name;
  319. if (paramType.IsSimpleType())
  320. func = (httpContext) =>
  321. {
  322. var queryValue = httpContext.Request.RouteValues[name].ToString();
  323. return ParseToType(queryValue, paramType);
  324. };
  325. else
  326. func = (httpContext) =>
  327. {
  328. return DeserializeFromCollection(httpContext.Request.RouteValues, paramType);
  329. };
  330. parameters.Add(func);
  331. return true;
  332. }
  333. else if (attribute is FromServicesAttribute)
  334. {
  335. parameters.Add((httpContext) =>
  336. {
  337. return httpContext.RequestServices.GetService(paramType);
  338. });
  339. return true;
  340. }
  341. }
  342. return false;
  343. }
  344. private static object DeserializeFromCollection(IDictionary<string, object> dictionary, Type paramType)
  345. {
  346. var result = Activator.CreateInstance(paramType);
  347. var properties = paramType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
  348. foreach (var property in properties)
  349. {
  350. if (dictionary.TryGetValue(property.Name, out var value))
  351. {
  352. var parsedValue = ParseToType(value.ToString(), property.PropertyType);
  353. property.SetValue(result, parsedValue);
  354. }
  355. }
  356. return result;
  357. }
  358. private static object DeserializeFromCollection(IQueryCollection dictionary, Type paramType)
  359. {
  360. var result = Activator.CreateInstance(paramType);
  361. // TODO: test with init-only fields
  362. var properties = paramType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
  363. foreach (var property in properties)
  364. {
  365. if (dictionary.TryGetValue(property.Name, out var value))
  366. {
  367. var parsedValue = ParseToType(value.ToString(), property.PropertyType);
  368. property.SetValue(result, parsedValue);
  369. }
  370. }
  371. return result;
  372. }
  373. private static object DeserializeFromCollection(IHeaderDictionary dictionary, Type paramType)
  374. {
  375. var result = Activator.CreateInstance(paramType);
  376. var properties = paramType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
  377. foreach (var property in properties)
  378. {
  379. if (dictionary.TryGetValue(property.Name, out var value))
  380. {
  381. var parsedValue = ParseToType(value.ToString(), property.PropertyType);
  382. property.SetValue(result, parsedValue);
  383. }
  384. }
  385. return result;
  386. }
  387. private static object ParseToType(string value, Type type)
  388. {
  389. object result;
  390. if (type.IsPrimitive || type == typeof(string))
  391. {
  392. result = Convert.ChangeType(value, type, CultureInfo.InvariantCulture);
  393. }
  394. else // if (type.IsEnum)
  395. {
  396. result = Enum.Parse(type, value);
  397. }
  398. return result;
  399. }
  400. private static object ParseToType(object value, Type type)
  401. {
  402. object result;
  403. if (type.IsPrimitive || type == typeof(string))
  404. {
  405. result = Convert.ChangeType(value, type, CultureInfo.InvariantCulture);
  406. }
  407. else // if (type.IsEnum)
  408. {
  409. result = Enum.Parse(type, value.ToString());
  410. }
  411. return result;
  412. }
  413. [DebuggerDisplay("{Method.Name} [{Parameters.Length}], {HttpMethod} {Route.Template}")]
  414. internal readonly struct EndpointInfo
  415. {
  416. public readonly string HttpMethod;
  417. public readonly TemplateMatcher Route;
  418. public readonly Func<HttpContext, object>[] Parameters;
  419. public readonly MethodInfo Method;
  420. public readonly Func<HttpContext, object, Task> Handler;
  421. public EndpointInfo(string httpMethod, TemplateMatcher route, MethodInfo method,
  422. Func<HttpContext, object>[] parameters,
  423. Func<HttpContext, object, Task> handler)
  424. {
  425. HttpMethod = httpMethod;
  426. Route = route;
  427. Method = method;
  428. Parameters = parameters;
  429. Handler = handler;
  430. }
  431. }
  432. [DebuggerDisplay("{Method.Name} [{Parameters.Length}], {HttpMethod} {Route.Template}")]
  433. internal readonly struct BuiltEndpointInfo
  434. {
  435. public readonly string HttpMethod;
  436. public readonly TemplateMatcher Route;
  437. public readonly Func<HttpContext, Task> Method;
  438. public BuiltEndpointInfo(string httpMethod, TemplateMatcher route, Func<HttpContext, Task> method)
  439. {
  440. HttpMethod = httpMethod;
  441. Route = route;
  442. Method = method;
  443. }
  444. }
  445. //private static Type[] GetFastReportServices()
  446. //{
  447. // var types = typeof(ControllerBuilder).Assembly.GetTypes();
  448. // var services = new List<Type>();
  449. // foreach (var type in types)
  450. // {
  451. // if(type.IsInterface && type.IsPublic)
  452. // {
  453. // services.Add(type);
  454. // }
  455. // }
  456. // return services.ToArray();
  457. //}
  458. }
  459. internal static class TypeExtensions
  460. {
  461. public static bool IsSimpleType(this Type type)
  462. => type.IsPrimitive || type.IsEnum || type == typeof(string);
  463. }
  464. }
  465. #endif