You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1070 lines
40 KiB

4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
9 months ago
4 years ago
9 months ago
4 years ago
9 months ago
4 years ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
9 months ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
9 months ago
4 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
2 years ago
4 years ago
9 months ago
4 years ago
9 months ago
4 years ago
9 months ago
4 years ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
9 months ago
3 years ago
4 years ago
  1. using Apewer.Network;
  2. using Apewer.Source;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Net;
  10. using System.Reflection;
  11. using System.Text;
  12. namespace Apewer.Web
  13. {
  14. /// <summary></summary>
  15. public static class ApiUtility
  16. {
  17. #region Text
  18. /// <summary>修剪 IP 地址,去除无效的部分。</summary>
  19. public static string TrimIP(string text)
  20. {
  21. var trimmed = TextUtility.Trim(text);
  22. if (string.IsNullOrEmpty(trimmed)) return "";
  23. var ip = TextUtility.Trim(trimmed.Split(':')[0]);
  24. return NetworkUtility.IsIP(ip) ? ip : "";
  25. }
  26. /// <summary>按 &amp; 拆分多个参数。</summary>
  27. public static StringPairs Parameters(string query, bool decode = true)
  28. {
  29. var input = query;
  30. var list = new StringPairs();
  31. if (!string.IsNullOrEmpty(input))
  32. {
  33. if (input[0] == '?') input = input.Substring(1);
  34. var args = input.Split('&');
  35. foreach (var arg in args)
  36. {
  37. var equals = arg.IndexOf("=");
  38. var left = equals < 0 ? arg : arg.Substring(0, equals);
  39. var right = equals < 0 ? "" : arg.Substring(equals + 1);
  40. if (decode)
  41. {
  42. left = TextUtility.DecodeUrl(left);
  43. right = TextUtility.DecodeUrl(right);
  44. }
  45. list.Add(left, right);
  46. }
  47. }
  48. return list;
  49. }
  50. /// <summary>获取参数并解码,可要求修剪参数值。</summary>
  51. public static string Parameter(string encoded, bool trim, params string[] names)
  52. {
  53. if (string.IsNullOrEmpty(encoded)) return null;
  54. if (names == null || names.Length < 1) return null;
  55. // Names 转为小写,加强适配。
  56. var lowerNames = new List<string>(names.Length);
  57. foreach (var name in names)
  58. {
  59. var lower = TextUtility.Lower(name);
  60. if (string.IsNullOrEmpty(lower)) continue;
  61. lowerNames.Add(lower);
  62. }
  63. if (lowerNames.Count < 1) return null;
  64. // 分参数对比。
  65. var parameters = Parameters(encoded);
  66. var matched = false;
  67. foreach (var parameter in parameters)
  68. {
  69. var left = parameter.Key;
  70. var right = parameter.Value;
  71. if (trim) right = TextUtility.Lower(right);
  72. var lowerLeft = TextUtility.Lower(left);
  73. if (lowerNames.Contains(right))
  74. {
  75. matched = true;
  76. if (!string.IsNullOrEmpty(right)) return right;
  77. }
  78. }
  79. return matched ? "" : null;
  80. }
  81. /// <summary>获取参数,可要求修剪参数值。</summary>
  82. public static string Parameter(StringPairs parameters, bool trim, params string[] names)
  83. {
  84. if (parameters == null || parameters.Count < 1) return null;
  85. if (names == null || names.Length < 1) return null;
  86. var lowerNames = new List<string>(names.Length);
  87. foreach (var name in names)
  88. {
  89. var lower = TextUtility.Lower(name);
  90. if (string.IsNullOrEmpty(lower)) continue;
  91. lowerNames.Add(lower);
  92. }
  93. foreach (var parameter in parameters)
  94. {
  95. var lowerKey = TextUtility.Lower(parameter.Key);
  96. if (lowerNames.Contains(lowerKey))
  97. {
  98. var value = parameter.Value;
  99. if (trim) value = TextUtility.Trim(value);
  100. if (!string.IsNullOrEmpty(value)) return value;
  101. }
  102. }
  103. return null;
  104. }
  105. /// <summary>获取 User Agent。</summary>
  106. public static string UserAgent(HttpHeaders headers) => headers == null ? null : headers.GetValue("user-agent");
  107. // 从 Uri 对象中解析路径片段。
  108. private static string[] Segmentals(Uri url)
  109. {
  110. if (url == null) return null;
  111. if (string.IsNullOrEmpty(url.AbsolutePath)) return null;
  112. var segmentals = url.AbsolutePath.Split('/');
  113. return segmentals;
  114. }
  115. // 获取已经解析的路径片段。
  116. private static string Segmental(string[] segmentals, int index = 3, bool decode = false)
  117. {
  118. if (segmentals == null || segmentals.Length < 1) return null;
  119. if (index < 1 || index >= segmentals.Length) return null;
  120. var segmental = segmentals[index];
  121. if (decode) segmental = TextUtility.DecodeUrl(segmental);
  122. return segmental;
  123. }
  124. #endregion
  125. #region Headers
  126. /// <summary></summary>
  127. public static HttpMethod Method(string method)
  128. {
  129. if (!string.IsNullOrEmpty(method))
  130. {
  131. var upper = TextUtility.Upper(method);
  132. if (upper.Contains("OPTIONS")) return HttpMethod.OPTIONS;
  133. else if (upper.Contains("POST")) return HttpMethod.POST;
  134. else if (upper.Contains("GET")) return HttpMethod.GET;
  135. else if (upper.Contains("CONNECT")) return HttpMethod.CONNECT;
  136. else if (upper.Contains("DELETE")) return HttpMethod.DELETE;
  137. else if (upper.Contains("HEAD")) return HttpMethod.HEAD;
  138. else if (upper.Contains("PATCH")) return HttpMethod.PATCH;
  139. else if (upper.Contains("PUT")) return HttpMethod.PUT;
  140. else if (upper.Contains("TRACE")) return HttpMethod.TRACE;
  141. }
  142. return HttpMethod.NULL;
  143. }
  144. /// <summary>获取 X-Forwarded-For,不存在时返回 NULL 值。</summary>
  145. public static string[] GetForwardedIP(HttpHeaders headers)
  146. {
  147. if (headers != null)
  148. {
  149. var value = headers.GetValue("x-forwarded-for");
  150. if (!string.IsNullOrEmpty(value))
  151. {
  152. var fips = new List<string>();
  153. var split = value.Split(',', ' ');
  154. foreach (var para in split)
  155. {
  156. var fip = TrimIP(para);
  157. if (!string.IsNullOrEmpty(fip)) fips.Add(fip);
  158. }
  159. return fips.ToArray();
  160. }
  161. }
  162. return new string[0];
  163. }
  164. #endregion
  165. #region Cookies
  166. // https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Set-Cookie
  167. // <cookie-name> 可以是除了控制字符 (CTLs)、空格 (spaces) 或制表符 (tab)之外的任何 US-ASCII 字符。
  168. // 同时不能包含以下分隔字符: ( ) < > @ , ; : \ " / [ ] ? = { }.
  169. // <cookie-value> 是可选的,如果存在的话,那么需要包含在双引号里面。
  170. // 支持除了控制字符(CTLs)、空格(whitespace)、双引号(double quotes)、逗号(comma)、分号(semicolon)以及反斜线(backslash)之外的任意 US-ASCII 字符。
  171. // 关于编码:许多应用会对 cookie 值按照URL编码(URL encoding)规则进行编码,但是按照 RFC 规范,这不是必须的。
  172. // 不过满足规范中对于 <cookie-value> 所允许使用的字符的要求是有用的。
  173. const string CookieChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&*+-<>^_`|~";
  174. /// <summary>修剪 Cookie 的 Name 文本。</summary>
  175. public static string CookieName(string name)
  176. {
  177. if (name.IsEmpty()) return null;
  178. var t = TextUtility.Restrict(name, CookieChars);
  179. while (t.StartsWith("$")) t = t.Substring(1);
  180. return t;
  181. }
  182. /// <summary>修剪 Cookie 的 Value 文本。</summary>
  183. public static string CookieValue(string value)
  184. {
  185. if (value.IsEmpty()) return value;
  186. var t = TextUtility.Restrict(value, CookieChars);
  187. if (t != value)
  188. {
  189. t = TextUtility.EncodeUrl(value);
  190. t = TextUtility.Restrict(t, CookieChars);
  191. }
  192. return t;
  193. }
  194. static string CookieExpires(DateTime dt)
  195. {
  196. // Expires=Wed, 21 Oct 2015 07:28:00 GMT;
  197. var utc = dt.ToUniversalTime();
  198. var sb = new StringBuilder();
  199. var week = utc.DayOfWeek.ToString().Substring(0, 3);
  200. sb.Append(week);
  201. sb.Append(", ");
  202. var day = utc.Day;
  203. if (day < 10) sb.Append("0");
  204. sb.Append(day);
  205. sb.Append(" ");
  206. var month = null as string;
  207. switch (utc.Month)
  208. {
  209. case 1: month = "Jan"; break;
  210. case 2: month = "Feb"; break;
  211. case 3: month = "Mar"; break;
  212. case 4: month = "Apr"; break;
  213. case 5: month = "May"; break;
  214. case 6: month = "Jun"; break;
  215. case 7: month = "Jul"; break;
  216. case 8: month = "Aug"; break;
  217. case 9: month = "Sep"; break;
  218. case 10: month = "Oct"; break;
  219. case 11: month = "Nov"; break;
  220. case 12: month = "Dec"; break;
  221. }
  222. sb.Append(month);
  223. sb.Append(" ");
  224. var year = utc.Year;
  225. if (year < 1000) sb.Append("0");
  226. if (year < 100) sb.Append("0");
  227. if (year < 10) sb.Append("0");
  228. sb.Append(year);
  229. sb.Append(" ");
  230. var time = ClockUtility.Lucid(utc, false, true, true, false);
  231. sb.Append(time);
  232. sb.Append(" GMT");
  233. return sb.ToString();
  234. }
  235. /// <summary>获取 Cookies 中第一个匹配到的值。</summary>
  236. public static string GetValue(this CookieCollection cookies, string name)
  237. {
  238. if (cookies == null || name.IsEmpty()) return null;
  239. var count = cookies.Count;
  240. for (var i = 0; i < count; i++)
  241. {
  242. var cookie = cookies[i];
  243. var n = cookie.Name;
  244. if (name == n) return cookie.Value;
  245. }
  246. var lower = TextUtility.Lower(name);
  247. for (var i = 0; i < count; i++)
  248. {
  249. var cookie = cookies[i];
  250. var n = TextUtility.Lower(cookie.Name);
  251. if (lower == n) return cookie.Value;
  252. }
  253. return null;
  254. }
  255. /// <summary>添加 Cookie 到集合。</summary>
  256. /// <param name="cookies">Cookie 集合。</param>
  257. /// <param name="name">名称。</param>
  258. /// <param name="value">值。</param>
  259. /// <param name="path">应用的路径。</param>
  260. /// <exception cref="CookieException"></exception>
  261. public static void Add(this CookieCollection cookies, string name, string value, string path = "/") => Add(cookies, name, value, path, null);
  262. /// <summary>添加 Cookie 到集合。</summary>
  263. /// <param name="cookies">Cookie 集合。</param>
  264. /// <param name="name">名称。</param>
  265. /// <param name="value">值。</param>
  266. /// <param name="path">应用的路径。</param>
  267. /// <param name="expires">过期时间。</param>
  268. /// <exception cref="CookieException"></exception>
  269. public static void Add(this CookieCollection cookies, string name, string value, DateTime expires, string path = "/") => Add(cookies, name, value, path, new Class<DateTime>(expires));
  270. /// <summary>添加 Cookie 到集合。</summary>
  271. /// <param name="cookies">Cookie 集合。</param>
  272. /// <param name="name">名称。</param>
  273. /// <param name="value">值。</param>
  274. /// <param name="path">应用的路径。</param>
  275. /// <param name="expires">过期时间。</param>
  276. /// <exception cref="CookieException"></exception>
  277. public static void Add(this CookieCollection cookies, string name, string value, string path, DateTime expires) => Add(cookies, name, value, path, new Class<DateTime>(expires));
  278. static void Add(this CookieCollection cookies, string name, string value, string path, Class<DateTime> expires)
  279. {
  280. if (cookies == null) return;
  281. name = CookieName(name);
  282. value = CookieName(value) ?? "";
  283. if (string.IsNullOrEmpty(name)) return;
  284. var cookie = new Cookie(name, value);
  285. cookie.Path = string.IsNullOrEmpty(path) ? "/" : path;
  286. if (expires != null) cookie.Expires = expires.Value;
  287. cookies.Add(cookie);
  288. }
  289. /// <summary>遍历 Cookie 集合。</summary>
  290. public static void ForEach(this CookieCollection cookies, Action<Cookie> action)
  291. {
  292. if (cookies == null || action == null) return;
  293. var count = cookies.Count;
  294. for (var i = 0; i < count; i++)
  295. {
  296. var cookie = cookies[i];
  297. if (cookie == null) continue;
  298. action.Invoke(cookie);
  299. }
  300. }
  301. internal static CookieCollection ParseCookies(HttpHeaders headers)
  302. {
  303. var cookies = new CookieCollection();
  304. if (headers == null) return cookies;
  305. var hvs = headers.GetValues("Cookie");
  306. foreach (var hv in hvs)
  307. {
  308. if (string.IsNullOrEmpty(hv)) continue;
  309. var lines = hv.Split(';');
  310. foreach (var line in lines)
  311. {
  312. var e = line.IndexOf("=");
  313. if (e < 0) continue;
  314. var name = TextUtility.Left(line, e, true);
  315. var value = TextUtility.Right(line, line.Length - e - 1, true);
  316. if (string.IsNullOrEmpty(name)) continue;
  317. if (string.IsNullOrEmpty(value)) continue;
  318. cookies.Add(name, value);
  319. }
  320. }
  321. return cookies;
  322. }
  323. /// <summary>生成 Set-Cookie 的值。</summary>
  324. public static string SetCookie(Cookie cookie)
  325. {
  326. if (cookie == null) return null;
  327. var n = CookieName(cookie.Name);
  328. if (n.IsEmpty()) return null;
  329. var v = CookieValue(cookie.Value);
  330. if (v == null) v = "";
  331. var sb = new StringBuilder();
  332. sb.Append(n);
  333. sb.Append("=");
  334. sb.Append(v);
  335. var expires = cookie.Expires;
  336. if (expires > DateTime.MinValue)
  337. {
  338. sb.Append("; Expires=");
  339. sb.Append(CookieExpires(expires));
  340. }
  341. var domain = cookie.Domain;
  342. if (!string.IsNullOrEmpty(domain))
  343. {
  344. sb.Append("; Domain=");
  345. sb.Append(domain);
  346. }
  347. var path = cookie.Path;
  348. sb.Append("; Path=");
  349. sb.Append(string.IsNullOrEmpty(path) ? "/" : path);
  350. var secure = cookie.Secure;
  351. if (secure)
  352. {
  353. sb.Append("; Secure");
  354. }
  355. return sb.ToString();
  356. }
  357. /// <summary>生成 Set-Cookie 的值。</summary>
  358. public static string[] SetCookie(CookieCollection cookies)
  359. {
  360. if (cookies == null) return null;
  361. var count = cookies.Count;
  362. var lines = new List<string>(count);
  363. for (var i = 0; i < count; i++)
  364. {
  365. var cookie = cookies[i];
  366. var value = SetCookie(cookie);
  367. if (value.IsEmpty()) continue;
  368. lines.Add(value);
  369. }
  370. return lines.ToArray();
  371. }
  372. #endregion
  373. #region ApiController
  374. /// <summary>设置控制器的 <see cref="ApiController.Context" /> 属性。</summary>
  375. /// <exception cref="ArgumentNullException" />
  376. public static void SetContext(ApiController controller, ApiContext context)
  377. {
  378. if (controller == null) throw new ArgumentNullException(nameof(controller));
  379. if (context == null) throw new ArgumentNullException(nameof(context));
  380. controller._context = context;
  381. }
  382. /// <summary>获取由控制器构造函数指定的初始化程序。</summary>
  383. public static Func<ApiController, bool> GetInitialier(this ApiController controller) => controller == null ? null : controller._func;
  384. /// <summary>获取由控制器构造函数指定的默认程序。</summary>
  385. public static Action<ApiController> GetDefault(this ApiController controller) => controller == null ? null : controller._default;
  386. /// <summary>以 POST 转移请求到其它 URL。</summary>
  387. private static string Transfer(ApiController controller, string url, string application = null, string function = null)
  388. {
  389. if (controller == null || controller.Request == null || controller.Response == null) return "ApiControllser 无效。";
  390. if (url.IsEmpty()) return "ApiController 无效。";
  391. var s = Json.NewObject();
  392. s.SetProperty("random", TextUtility.Key());
  393. s.SetProperty("application", application.IsEmpty() ? controller.Request.Application : application);
  394. s.SetProperty("function", function.IsEmpty() ? controller.Request.Function : function);
  395. s.SetProperty("data", controller.Request.Data);
  396. s.SetProperty("session", controller.Request.Session);
  397. s.SetProperty("ticket", controller.Request.Ticket);
  398. s.SetProperty("page", controller.Request.Page);
  399. var c = new HttpClient();
  400. c.Url = url;
  401. c.Method = HttpMethod.POST;
  402. // TODO 设置 Request 的 Cookies。
  403. c.RequestData = s.ToString().Bytes();
  404. var e = c.Send();
  405. if (e != null) return e.Message;
  406. // TODO 解析 Response 的 Cookies。
  407. var r = Json.From(c.ResponseData.Text());
  408. if (r == null || !r.Available)
  409. {
  410. controller.Response.Error("请求失败。");
  411. return "请求失败。";
  412. }
  413. if (r["status"] != "ok")
  414. {
  415. controller.Response.Error(r["message"]);
  416. return r["message"];
  417. }
  418. controller.Response.Data.Reset(r.GetProperty("data"));
  419. return null;
  420. }
  421. /// <summary>创建指定类型的控制器,并引用参照控制器的 Context。</summary>
  422. /// <exception cref="ArgumentNullException" />
  423. public static T Create<T>(this ApiController reference) where T : ApiController, new()
  424. {
  425. if (reference == null) throw new ArgumentNullException(nameof(reference));
  426. var controller = new T();
  427. controller._context = reference.Context;
  428. return controller;
  429. }
  430. /// <summary>使用默认控制器处理请求。</summary>
  431. public static void UseDefault(this ApiController current)
  432. {
  433. if (current == null) return;
  434. var options = current._context?.Options;
  435. if (options == null) return;
  436. var type = options.Default;
  437. if (type == null) return;
  438. var controller = null as ApiController;
  439. try
  440. {
  441. controller = (ApiController)Activator.CreateInstance(type);
  442. SetContext(controller, current.Context);
  443. }
  444. catch
  445. {
  446. return;
  447. }
  448. controller.GetInitialier()?.Invoke(controller);
  449. }
  450. #endregion
  451. #region ApiContext
  452. /// <summary>作为纯文本输出。</summary>
  453. /// <remarks>Content-Type: text/plain</remarks>
  454. public static void Text(ApiContext context, string text)
  455. {
  456. if (context == null) return;
  457. Model(context.Response, new ApiTextModel(text, "text/plain"));
  458. }
  459. /// <summary>设置 status 为 error,并设置 message 的内容。</summary>
  460. public static void Error(ApiContext context, string text)
  461. {
  462. if (context == null) return;
  463. Error(context.Response, text);
  464. }
  465. #endregion
  466. #region ApiRequest
  467. /// <summary>获取 URL 路径段,不存在的段为 NULL 值。可要求解码。</summary>
  468. public static string Segmental(this ApiRequest request, int index = 3, bool decode = false)
  469. {
  470. if (request == null) return null;
  471. if (request._segmentals == null) request._segmentals = Segmentals(request.Url);
  472. return Segmental(request._segmentals, index, decode);
  473. }
  474. /// <summary>获取参数,指定可能的参数名,默认将修剪参数值。</summary>
  475. /// <remarks>当从 URL 中获取参数时将解码。</remarks>
  476. public static string Parameter(this ApiRequest request, params string[] names) => Parameter(request, true, names);
  477. /// <summary>获取参数,指定可能的参数名,可要求修剪参数值。</summary>
  478. /// <remarks>当从 URL 中获取参数时将解码。</remarks>
  479. public static string Parameter(ApiRequest request, bool trim, params string[] names)
  480. {
  481. if (request == null) return null;
  482. if (names == null || names.Length < 1) return null;
  483. var dedupNames = new List<string>(names.Length);
  484. var lowerNames = new List<string>(names.Length);
  485. foreach (var name in names)
  486. {
  487. if (string.IsNullOrEmpty(name)) continue;
  488. if (dedupNames.Contains(name)) continue;
  489. else dedupNames.Add(name);
  490. var lower = TextUtility.Lower(name);
  491. if (lowerNames.Contains(lower)) continue;
  492. else lowerNames.Add(lower);
  493. }
  494. var matched = false;
  495. // POST 优先。
  496. var data = request.Data;
  497. if (data != null && data.IsObject)
  498. {
  499. var properties = data.GetProperties();
  500. if (properties != null)
  501. {
  502. // Json 区分大小写,先全字匹配。
  503. foreach (var property in properties)
  504. {
  505. if (!property.IsProperty) continue;
  506. var name = property.Name;
  507. if (!dedupNames.Contains(name)) continue;
  508. var value = property.Value;
  509. if (value == null) continue;
  510. matched = true;
  511. var text = value.ToString();
  512. if (trim) text = TextUtility.Trim(text);
  513. if (!string.IsNullOrEmpty(text)) return text;
  514. }
  515. // 以小写模糊匹配。
  516. foreach (var property in properties)
  517. {
  518. if (!property.IsProperty) continue;
  519. var name = TextUtility.Lower(property.Name);
  520. if (!lowerNames.Contains(name)) continue;
  521. var value = property.Value;
  522. if (value == null) continue;
  523. matched = true;
  524. var text = value.ToString();
  525. if (trim) text = TextUtility.Trim(text);
  526. if (!string.IsNullOrEmpty(text)) return text;
  527. }
  528. }
  529. }
  530. // 从已解析的 Get 参数中搜索。
  531. if (request.Parameters != null)
  532. {
  533. var value = Parameter(request.Parameters, trim, names);
  534. if (!string.IsNullOrEmpty(value)) return value;
  535. if (value != null) matched = true;
  536. }
  537. return matched ? "" : null;
  538. }
  539. #endregion
  540. #region ApiResponse
  541. internal static ApiModel Model(ApiResponse response, ApiModel model)
  542. {
  543. if (response == null && model == null) return null;
  544. response.Model = model;
  545. return model;
  546. }
  547. /// <summary>设置响应。</summary>
  548. public static string Respond(ApiResponse response, Json data, bool lower = true)
  549. {
  550. if (response == null) return "Response 对象无效。";
  551. if (data != null)
  552. {
  553. if (lower) data = Json.Lower(data);
  554. response.Data = data;
  555. }
  556. return null;
  557. }
  558. /// <summary>设置响应,当发生错误时设置响应。返回错误信息。</summary>
  559. public static string Respond(ApiResponse response, IList list, bool lower = true, int depth = -1, bool force = false)
  560. {
  561. if (response == null) return "Response 对象无效。";
  562. if (list == null)
  563. {
  564. var error = "列表对象无效。";
  565. response.Error(error);
  566. return error;
  567. }
  568. var json = Json.From(list, lower, depth, force);
  569. if (json == null || !json.Available)
  570. {
  571. var error = "列表无法序列化。";
  572. response.Error(error);
  573. return error;
  574. }
  575. if (response.Data == null) response.Data = Json.NewObject();
  576. response.Data.SetProperty("count", list.Count);
  577. response.Data.SetProperty("list", Json.From(list, lower, depth, force));
  578. return null;
  579. }
  580. /// <summary>设置响应,当发生错误时设置响应。返回错误信息。</summary>
  581. public static string Respond(ApiResponse response, IRecord record, bool lower = true)
  582. {
  583. if (response == null) return "Response 对象无效。";
  584. if (record == null)
  585. {
  586. var error = "记录无效。";
  587. response.Error(error);
  588. return error;
  589. }
  590. var json = Json.From(record, lower);
  591. if (json == null || !json.Available)
  592. {
  593. var error = "记录无法序列化。";
  594. response.Error(error);
  595. return error;
  596. }
  597. if (response.Data == null) response.Data = Json.NewObject();
  598. response.Data.Reset(json);
  599. return null;
  600. }
  601. /// <summary>设置 status 为 error,并设置 message 的内容。</summary>
  602. public static void Error(ApiResponse response, string message = "未知错误。")
  603. {
  604. if (response == null) return;
  605. response.Model = null;
  606. response.Status = "error";
  607. response.Message = message ?? TextUtility.Empty;
  608. }
  609. /// <summary>设置 status 为 exception,并设置 message 的内容。</summary>
  610. public static void Exception(ApiResponse response, Exception exception, bool setData = true)
  611. {
  612. if (response == null) return;
  613. response.Model = null;
  614. response.Status = "exception";
  615. if (exception != null)
  616. {
  617. try
  618. {
  619. if (setData)
  620. {
  621. var json = ToJson(exception);
  622. response.Message = json["message"];
  623. response.Data = json;
  624. }
  625. else
  626. {
  627. response.Message = exception.Message();
  628. }
  629. }
  630. catch { }
  631. }
  632. }
  633. private static Json ToJson(Exception exception, bool withInner = true)
  634. {
  635. if (exception == null) return ToJson(new NullReferenceException());
  636. var json = Json.NewObject();
  637. json.SetProperty("type", exception.GetType().FullName);
  638. json.SetProperty("message", exception.Message);
  639. var st = exception.StackTrace;
  640. if (!string.IsNullOrEmpty(st))
  641. {
  642. var stack = Json.NewArray();
  643. var split = st.Split('\r', '\n');
  644. foreach (var line in split)
  645. {
  646. var trim = TextUtility.Trim(line);
  647. if (string.IsNullOrEmpty(trim)) continue;
  648. stack.AddItem(trim);
  649. }
  650. json.SetProperty("stack", stack);
  651. }
  652. if (withInner)
  653. {
  654. var iex = exception.InnerException;
  655. if (iex != null) json.SetProperty("inner", ToJson(exception, false));
  656. }
  657. return json;
  658. }
  659. /// <summary>停止 Invoker 对返回值的处理。</summary>
  660. public static void StopReturn(ApiResponse response)
  661. {
  662. if (response == null) return;
  663. response.StopReturn = true;
  664. }
  665. /// <summary>生成 Response 的 Json 实例。</summary>
  666. public static string ToJson(this ApiResponse response, ApiOptions options = null)
  667. {
  668. if (response == null) return "{}";
  669. if (string.IsNullOrEmpty(response.Status)) response.Status = "ok";
  670. var json = Json.NewObject();
  671. if (options == null) options = new ApiOptions();
  672. // 执行时间。
  673. if (options.WithClock)
  674. {
  675. json.SetProperty("clock", ClockUtility.Lucid(DateTime.Now));
  676. }
  677. // 持续时间。
  678. if (options.WithDuration)
  679. {
  680. if (response.Duration.NotEmpty()) json.SetProperty("duration", response.Duration);
  681. }
  682. // 随机值。
  683. var random = response.Random;
  684. if (!string.IsNullOrEmpty(random)) json.SetProperty("random", random);
  685. // 调用。
  686. if (options.WithTarget)
  687. {
  688. json.SetProperty("application", response.Application);
  689. json.SetProperty("function", response.Function);
  690. }
  691. // 状态。
  692. json.SetProperty("status", (TextUtility.IsBlank(response.Status) ? TextUtility.Empty : response.Status.ToLower()));
  693. if (!string.IsNullOrEmpty(response.Message)) json.SetProperty("message", response.Message);
  694. // 用户数据。
  695. if (response.Message == "exception" && !options.WithException) json.SetProperty("data");
  696. else json.SetProperty("data", response.Data);
  697. var indented = response.Indented || options.JsonIndent;
  698. var text = json.ToString(indented);
  699. return text;
  700. }
  701. #endregion
  702. #region ApiResult
  703. /// <summary>对 HTTP 结果设置文件名。</summary>
  704. /// <param name="result">结果。</param>
  705. /// <param name="name">文件名(未编码)。</param>
  706. public static void SetAttachemnt(this HeadResult result, string name)
  707. {
  708. if (result == null) throw new ArgumentNullException(nameof(result));
  709. if (name.IsEmpty()) throw new ArgumentNullException(nameof(name));
  710. var encoded = TextUtility.EncodeUrl(name);
  711. result.Headers.Add("Content-Disposition", $"attachment; filename={encoded}");
  712. }
  713. #endregion
  714. #region ApiFunction Parameters
  715. internal static object[] ReadParameters(ApiRequest request, ApiFunction function)
  716. {
  717. if (request == null || function == null) return null;
  718. return ReadParameters(request, function.Parameters);
  719. }
  720. /// <summary>为带有形参的 Function 准备实参。</summary>
  721. /// <param name="request">API 请求模型。</param>
  722. /// <param name="parameters">Function 的参数信息。</param>
  723. /// <returns>实参。</returns>
  724. public static object[] ReadParameters(ApiRequest request, ParameterInfo[] parameters)
  725. {
  726. if (request == null || parameters == null || parameters.Length < 1) return null;
  727. var apiParameters = parameters.Map(x => ApiParameter.Parse(x) ?? throw new Exception($"参数【{x.Name}】无效。"));
  728. return ReadParameters(request, apiParameters);
  729. }
  730. /// <summary>为带有形参的 Function 准备实参。</summary>
  731. /// <param name="request">API 请求模型。</param>
  732. /// <param name="parameters">Function 的参数信息。</param>
  733. /// <returns>实参。</returns>
  734. public static object[] ReadParameters(ApiRequest request, ApiParameter[] parameters)
  735. {
  736. if (request == null || parameters == null || parameters.Length < 1) return null;
  737. if (parameters == null) return null;
  738. var count = parameters.Length;
  739. if (count < 1) return null;
  740. // 当 Function 仅有一个参数时,尝试生成模型。
  741. if (count == 1 && parameters[0] != null)
  742. {
  743. var parameterName = parameters[0].Name;
  744. var parameterType = parameters[0].Type;
  745. // POST
  746. if (request.Method == HttpMethod.POST)
  747. {
  748. // string
  749. if (parameterType.Equals(typeof(string))) return new object[] { request.Parameters.GetValue(parameterName, true) };
  750. // json
  751. if (parameterType.Equals(typeof(Json))) return new object[] { request.PostJson };
  752. #if !NET20
  753. // dynamic
  754. if (parameterType.Equals(typeof(object)))
  755. {
  756. try
  757. {
  758. // var expando = new System.Dynamic.ExpandoObject();
  759. // var dict = expando as IDictionary<string, object>;
  760. var expando = new ObjectSet(false, true);
  761. var dict = expando.Origin();
  762. if (request.Form != null)
  763. {
  764. foreach (var kvp in request.Form)
  765. {
  766. if (dict.ContainsKey(kvp.Key)) continue;
  767. dict.Add(kvp.Key, kvp.Value);
  768. }
  769. }
  770. else if (request.PostJson)
  771. {
  772. var jprops = request.PostJson.GetProperties();
  773. foreach (var jprop in jprops)
  774. {
  775. var name = jprop.Name;
  776. if (dict.ContainsKey(name)) continue;
  777. var value = jprop.Value;
  778. if (value != null)
  779. {
  780. }
  781. dict.Add(name, value);
  782. }
  783. }
  784. return new object[] { expando };
  785. }
  786. catch { }
  787. }
  788. #endif
  789. // class
  790. if (parameterType.IsClass)
  791. {
  792. try
  793. {
  794. var entity = ReadParameter(request.Data, parameterType);
  795. if (entity == null) entity = ReadParameter(request.PostJson, parameterType);
  796. if (entity != null) return new object[] { entity.Value };
  797. }
  798. catch { }
  799. }
  800. if (request.PostJson) return new object[] { request.PostJson };
  801. if (request.Form != null) return new object[] { request.Form };
  802. if (request.PostText.NotEmpty()) return new object[] { request.PostText };
  803. return new object[] { request.PostData };
  804. }
  805. else
  806. {
  807. // string
  808. if (parameterType.Equals(typeof(string))) return new object[] { request.Parameters.GetValue(parameterName, true) };
  809. // json
  810. if (parameterType.Equals(typeof(Json))) return new object[] { Json.From(request.Parameters.GetValue(parameterName, true)) };
  811. }
  812. }
  813. var values = new object[count];
  814. for (var i = 0; i < count; i++)
  815. {
  816. if (parameters[i] != null)
  817. {
  818. var name = parameters[i].Name;
  819. var type = parameters[i].Type;
  820. var text = Parameter(request, name);
  821. values[i] = ReadParameter(text, type);
  822. }
  823. }
  824. return values;
  825. }
  826. static Class<object> ReadParameter(Json json, Type type)
  827. {
  828. if (json)
  829. {
  830. if (json.IsObject)
  831. {
  832. var properties = json.GetProperties();
  833. if (properties.Length > 0)
  834. {
  835. var entity = Json.Object(type, json, true, null, true);
  836. return new Class<object>(entity);
  837. }
  838. }
  839. if (json.IsArray)
  840. {
  841. var items = json.GetItems();
  842. if (items.Length > 0)
  843. {
  844. var entity = Json.Object(type, json, true, null, true);
  845. return new Class<object>(entity);
  846. }
  847. }
  848. }
  849. return null;
  850. }
  851. static object ReadParameter(string text, Type type)
  852. {
  853. if (type.Equals(typeof(object)) || type.Equals(typeof(string))) return text;
  854. if (type.Equals(typeof(bool))) return NumberUtility.Boolean(text);
  855. if (type.Equals(typeof(float))) return NumberUtility.Single(text);
  856. if (type.Equals(typeof(double))) return NumberUtility.Double(text);
  857. if (type.Equals(typeof(decimal))) return NumberUtility.Decimal(text);
  858. if (type.Equals(typeof(byte))) return NumberUtility.Byte(text);
  859. if (type.Equals(typeof(sbyte))) return NumberUtility.SByte(text);
  860. if (type.Equals(typeof(short))) return NumberUtility.Int16(text);
  861. if (type.Equals(typeof(ushort))) return NumberUtility.UInt16(text);
  862. if (type.Equals(typeof(int))) return NumberUtility.Int32(text);
  863. if (type.Equals(typeof(uint))) return NumberUtility.UInt32(text);
  864. if (type.Equals(typeof(long))) return NumberUtility.Int64(text);
  865. if (type.Equals(typeof(ulong))) return NumberUtility.UInt64(text);
  866. if (type.Equals(typeof(byte[]))) return TextUtility.FromBase64(text);
  867. if (type.Equals(typeof(Json))) return Json.From(text);
  868. return type.IsValueType ? Activator.CreateInstance(type) : null;
  869. }
  870. #endregion
  871. #region Enumerate Entries
  872. internal static Json Enumerate(IEnumerable<ApiApplication> applications, ApiOptions options)
  873. {
  874. var list = Json.NewArray();
  875. var count = 0;
  876. if (applications != null)
  877. {
  878. foreach (var app in applications)
  879. {
  880. if (app == null) continue;
  881. if (app.Hidden) continue;
  882. list.AddItem(app.ToJson(options));
  883. count = count + 1;
  884. }
  885. }
  886. var json = Json.NewObject();
  887. json.SetProperty("count", count);
  888. json.SetProperty("applications", list);
  889. return json;
  890. }
  891. internal static Json Enumerate(IEnumerable<ApiFunction> functions, ApiOptions options)
  892. {
  893. var count = 0;
  894. var list = Json.NewArray();
  895. if (functions != null)
  896. {
  897. foreach (var func in functions)
  898. {
  899. if (func == null) continue;
  900. if (func.Hidden) continue;
  901. list.AddItem(func.ToJson(options));
  902. count = count + 1;
  903. }
  904. }
  905. var json = Json.NewObject();
  906. json.SetProperty("count", count);
  907. json.SetProperty("functions", list);
  908. return json;
  909. }
  910. #endregion
  911. }
  912. }