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.

1065 lines
40 KiB

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
4 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
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
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(StringPairs 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(StringPairs headers)
  146. {
  147. if (headers != null)
  148. {
  149. var value = headers.GetValue("x-forwarded-for", true);
  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(StringPairs headers)
  302. {
  303. var cookies = new CookieCollection();
  304. if (headers == null) return cookies;
  305. var hvs = headers.GetValues("cookie", true, false);
  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>设置控制器属性。</summary>
  375. public static void SetProperties(ApiController controller, ApiRequest request, ApiResponse response, ApiOptions options)
  376. {
  377. if (controller == null) return;
  378. controller.Request = request;
  379. controller.Response = response;
  380. controller._options = options;
  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>获取选项。</summary>
  387. public static ApiOptions GetOptions(this ApiController controller) => controller == null ? null : controller._options;
  388. /// <summary>以 POST 转移请求到其它 URL。</summary>
  389. private static string Transfer(ApiController controller, string url, string application = null, string function = null)
  390. {
  391. if (controller == null || controller.Request == null || controller.Response == null) return "ApiControllser 无效。";
  392. if (url.IsEmpty()) return "ApiController 无效。";
  393. var s = Json.NewObject();
  394. s.SetProperty("random", TextUtility.Key());
  395. s.SetProperty("application", application.IsEmpty() ? controller.Request.Application : application);
  396. s.SetProperty("function", function.IsEmpty() ? controller.Request.Function : function);
  397. s.SetProperty("data", controller.Request.Data);
  398. s.SetProperty("session", controller.Request.Session);
  399. s.SetProperty("ticket", controller.Request.Ticket);
  400. s.SetProperty("page", controller.Request.Page);
  401. var c = new HttpClient();
  402. c.Url = url;
  403. c.Method = HttpMethod.POST;
  404. // TODO 设置 Request 的 Cookies。
  405. c.RequestData = s.ToString().Bytes();
  406. var e = c.Send();
  407. if (e != null) return e.Message;
  408. // TODO 解析 Response 的 Cookies。
  409. var r = Json.From(c.ResponseData.Text());
  410. if (r == null || !r.Available)
  411. {
  412. controller.Response.Error("请求失败。");
  413. return "请求失败。";
  414. }
  415. if (r["status"] != "ok")
  416. {
  417. controller.Response.Error(r["message"]);
  418. return r["message"];
  419. }
  420. controller.Response.Data.Reset(r.GetProperty("data"));
  421. return null;
  422. }
  423. /// <summary>创建指定类型的控制器,并引用当前控制器的 Request 和 Response。</summary>
  424. public static T Create<T>(this ApiController current) where T : ApiController, new()
  425. {
  426. var controller = new T();
  427. if (current != null)
  428. {
  429. controller.Request = current.Request;
  430. controller.Response = current.Response;
  431. }
  432. return controller;
  433. }
  434. /// <summary>使用默认控制器处理请求。</summary>
  435. public static void UseDefault(this ApiController current)
  436. {
  437. if (current == null) return;
  438. var options = GetOptions(current);
  439. if (options == null) return;
  440. var type = options.Default;
  441. if (type == null) return;
  442. var controller = null as ApiController;
  443. try
  444. {
  445. controller = (ApiController)Activator.CreateInstance(type);
  446. SetProperties(controller, current.Request, current.Response, options);
  447. }
  448. catch
  449. {
  450. return;
  451. }
  452. controller.GetInitialier()?.Invoke(controller);
  453. }
  454. #endregion
  455. #region ApiRequest
  456. /// <summary>获取 URL 路径段,不存在的段为 NULL 值。可要求解码。</summary>
  457. public static string Segmental(this ApiRequest request, int index = 3, bool decode = false)
  458. {
  459. if (request == null) return null;
  460. if (request._segmentals == null) request._segmentals = Segmentals(request.Url);
  461. return Segmental(request._segmentals, index, decode);
  462. }
  463. /// <summary>获取参数,指定可能的参数名,默认将修剪参数值。</summary>
  464. /// <remarks>当从 URL 中获取参数时将解码。</remarks>
  465. public static string Parameter(this ApiRequest request, params string[] names) => Parameter(request, true, names);
  466. /// <summary>获取参数,指定可能的参数名,可要求修剪参数值。</summary>
  467. /// <remarks>当从 URL 中获取参数时将解码。</remarks>
  468. public static string Parameter(ApiRequest request, bool trim, params string[] names)
  469. {
  470. if (request == null) return null;
  471. if (names == null || names.Length < 1) return null;
  472. var dedupNames = new List<string>(names.Length);
  473. var lowerNames = new List<string>(names.Length);
  474. foreach (var name in names)
  475. {
  476. if (string.IsNullOrEmpty(name)) continue;
  477. if (dedupNames.Contains(name)) continue;
  478. else dedupNames.Add(name);
  479. var lower = TextUtility.Lower(name);
  480. if (lowerNames.Contains(lower)) continue;
  481. else lowerNames.Add(lower);
  482. }
  483. var matched = false;
  484. // POST 优先。
  485. var data = request.Data;
  486. if (data != null && data.IsObject)
  487. {
  488. var properties = data.GetProperties();
  489. if (properties != null)
  490. {
  491. // Json 区分大小写,先全字匹配。
  492. foreach (var property in properties)
  493. {
  494. if (!property.IsProperty) continue;
  495. var name = property.Name;
  496. if (!dedupNames.Contains(name)) continue;
  497. var value = property.Value;
  498. if (value == null) continue;
  499. matched = true;
  500. var text = value.ToString();
  501. if (trim) text = TextUtility.Trim(text);
  502. if (!string.IsNullOrEmpty(text)) return text;
  503. }
  504. // 以小写模糊匹配。
  505. foreach (var property in properties)
  506. {
  507. if (!property.IsProperty) continue;
  508. var name = TextUtility.Lower(property.Name);
  509. if (!lowerNames.Contains(name)) continue;
  510. var value = property.Value;
  511. if (value == null) continue;
  512. matched = true;
  513. var text = value.ToString();
  514. if (trim) text = TextUtility.Trim(text);
  515. if (!string.IsNullOrEmpty(text)) return text;
  516. }
  517. }
  518. }
  519. // 从已解析的 Get 参数中搜索。
  520. if (request.Parameters != null)
  521. {
  522. var value = Parameter(request.Parameters, trim, names);
  523. if (!string.IsNullOrEmpty(value)) return value;
  524. if (value != null) matched = true;
  525. }
  526. return matched ? "" : null;
  527. }
  528. #endregion
  529. #region ApiResponse
  530. internal static ApiModel Model(ApiResponse response, ApiModel model)
  531. {
  532. if (response == null && model == null) return null;
  533. response.Model = model;
  534. return model;
  535. }
  536. /// <summary>设置响应。</summary>
  537. public static string Respond(ApiResponse response, Json data, bool lower = true)
  538. {
  539. if (response == null) return "Response 对象无效。";
  540. if (data != null)
  541. {
  542. if (lower) data = Json.Lower(data);
  543. response.Data = data;
  544. }
  545. return null;
  546. }
  547. /// <summary>设置响应,当发生错误时设置响应。返回错误信息。</summary>
  548. public static string Respond(ApiResponse response, IList list, bool lower = true, int depth = -1, bool force = false)
  549. {
  550. if (response == null) return "Response 对象无效。";
  551. if (list == null)
  552. {
  553. var error = "列表对象无效。";
  554. response.Error(error);
  555. return error;
  556. }
  557. var json = Json.From(list, lower, depth, force);
  558. if (json == null || !json.Available)
  559. {
  560. var error = "列表无法序列化。";
  561. response.Error(error);
  562. return error;
  563. }
  564. if (response.Data == null) response.Data = Json.NewObject();
  565. response.Data.SetProperty("count", list.Count);
  566. response.Data.SetProperty("list", Json.From(list, lower, depth, force));
  567. return null;
  568. }
  569. /// <summary>设置响应,当发生错误时设置响应。返回错误信息。</summary>
  570. public static string Respond(ApiResponse response, IRecord record, bool lower = true)
  571. {
  572. if (response == null) return "Response 对象无效。";
  573. if (record == null)
  574. {
  575. var error = "记录无效。";
  576. response.Error(error);
  577. return error;
  578. }
  579. var json = Json.From(record, lower);
  580. if (json == null || !json.Available)
  581. {
  582. var error = "记录无法序列化。";
  583. response.Error(error);
  584. return error;
  585. }
  586. if (response.Data == null) response.Data = Json.NewObject();
  587. response.Data.Reset(json);
  588. return null;
  589. }
  590. /// <summary>设置 status 为 error,并设置 message 的内容。</summary>
  591. public static void Error(ApiResponse response, string message = "未知错误。")
  592. {
  593. if (response == null) return;
  594. response.Model = null;
  595. response.Status = "error";
  596. response.Message = message ?? TextUtility.Empty;
  597. }
  598. /// <summary>设置 status 为 exception,并设置 message 的内容。</summary>
  599. public static void Exception(ApiResponse response, Exception exception, bool setData = true)
  600. {
  601. if (response == null) return;
  602. response.Model = null;
  603. response.Status = "exception";
  604. if (exception == null) return;
  605. try
  606. {
  607. if (setData)
  608. {
  609. var json = ToJson(exception);
  610. response.Message = json["message"];
  611. response.Data = json;
  612. }
  613. else
  614. {
  615. response.Message = exception.Message();
  616. }
  617. }
  618. catch { }
  619. }
  620. private static Json ToJson(Exception exception, bool withInner = true)
  621. {
  622. if (exception == null) return ToJson(new NullReferenceException());
  623. var json = Json.NewObject();
  624. json.SetProperty("type", exception.GetType().FullName);
  625. json.SetProperty("message", exception.Message);
  626. var st = exception.StackTrace;
  627. if (!string.IsNullOrEmpty(st))
  628. {
  629. var stack = Json.NewArray();
  630. var split = st.Split('\r', '\n');
  631. foreach (var line in split)
  632. {
  633. var trim = TextUtility.Trim(line);
  634. if (string.IsNullOrEmpty(trim)) continue;
  635. stack.AddItem(trim);
  636. }
  637. json.SetProperty("stack", stack);
  638. }
  639. if (withInner)
  640. {
  641. var iex = exception.InnerException;
  642. if (iex != null) json.SetProperty("inner", ToJson(exception, false));
  643. }
  644. return json;
  645. }
  646. /// <summary>停止 Invoker 对返回值的处理。</summary>
  647. public static void StopReturn(ApiResponse response)
  648. {
  649. if (response == null) return;
  650. response.StopReturn = true;
  651. }
  652. /// <summary>生成 Response 的 Json 实例。</summary>
  653. public static string ToJson(this ApiResponse response, ApiOptions options = null)
  654. {
  655. if (response == null) return "{}";
  656. if (string.IsNullOrEmpty(response.Status)) response.Status = "ok";
  657. var json = Json.NewObject();
  658. if (options == null) options = new ApiOptions();
  659. // 执行时间。
  660. if (options.WithClock)
  661. {
  662. json.SetProperty("clock", ClockUtility.Lucid(DateTime.Now));
  663. }
  664. // 持续时间。
  665. if (options.WithDuration)
  666. {
  667. if (response.Duration.NotEmpty()) json.SetProperty("duration", response.Duration);
  668. }
  669. // 随机值。
  670. var random = response.Random;
  671. if (!string.IsNullOrEmpty(random)) json.SetProperty("random", random);
  672. // 调用。
  673. if (options.WithTarget)
  674. {
  675. json.SetProperty("application", response.Application);
  676. json.SetProperty("function", response.Function);
  677. }
  678. // 状态。
  679. json.SetProperty("status", (TextUtility.IsBlank(response.Status) ? TextUtility.Empty : response.Status.ToLower()));
  680. if (!string.IsNullOrEmpty(response.Message)) json.SetProperty("message", response.Message);
  681. // 用户数据。
  682. if (response.Message == "exception" && !options.WithException) json.SetProperty("data");
  683. else json.SetProperty("data", response.Data);
  684. var indented = response.Indented || options.JsonIndent;
  685. var text = json.ToString(indented);
  686. return text;
  687. }
  688. #endregion
  689. #region ApiModel
  690. /// <summary>初始化 ApiMode 的属性。</summary>
  691. public static void Initialize(ApiModel model, ApiRequest request, ApiResponse response, ApiOptions options, ApiProvider provider)
  692. {
  693. if (model == null) return;
  694. model._request = request;
  695. model._response = response;
  696. model._options = options;
  697. model._provider = provider;
  698. }
  699. #endregion
  700. #region Application & Path
  701. /// <summary>MapPath。</summary>
  702. public static string MapPath(params string[] names)
  703. {
  704. var list = new List<string>();
  705. if (names != null)
  706. {
  707. var invalid = StorageUtility.InvalidPathChars;
  708. foreach (var name in names)
  709. {
  710. if (string.IsNullOrEmpty(name)) continue;
  711. var split = name.Split('/', '\\');
  712. foreach (var s in split)
  713. {
  714. var sb = new StringBuilder();
  715. foreach (var c in s)
  716. {
  717. if (Array.IndexOf(invalid, "c") < 0) sb.Append(c);
  718. }
  719. var t = sb.ToString();
  720. if (!string.IsNullOrEmpty(t)) list.Add(t);
  721. }
  722. }
  723. }
  724. return list.Count > 1 ? StorageUtility.CombinePath(list.ToArray()) : "";
  725. }
  726. #endregion
  727. #region ApiFunction Parameters
  728. internal static object[] ReadParameters(ApiRequest request, ApiFunction function)
  729. {
  730. if (request == null || function == null) return null;
  731. return ReadParameters(request, function.Parameters);
  732. }
  733. /// <summary>为带有形参的 Function 准备实参。</summary>
  734. /// <param name="request">API 请求模型。</param>
  735. /// <param name="parameters">Function 的参数信息。</param>
  736. /// <returns>实参。</returns>
  737. public static object[] ReadParameters(ApiRequest request, ParameterInfo[] parameters)
  738. {
  739. if (request == null || parameters == null || parameters.Length < 1) return null;
  740. var pis = parameters;
  741. if (pis == null) return null;
  742. var count = pis.Length;
  743. if (count < 1) return null;
  744. // 当 Function 仅有一个参数时,尝试生成模型。
  745. if (count == 1)
  746. {
  747. var pin = pis[0].Name;
  748. var pit = pis[0].ParameterType;
  749. // POST
  750. if (request.Method == HttpMethod.POST)
  751. {
  752. // string
  753. if (pit.Equals(typeof(string))) return new object[] { request.Parameters.GetValue(pin, true) };
  754. // json
  755. if (pit.Equals(typeof(Json))) return new object[] { request.PostJson };
  756. #if !NET20
  757. // dynamic
  758. if (pit.Equals(typeof(object)))
  759. {
  760. try
  761. {
  762. // var expando = new System.Dynamic.ExpandoObject();
  763. // var dict = expando as IDictionary<string, object>;
  764. var expando = new ObjectSet(false, true);
  765. var dict = expando.Origin();
  766. if (request.Form != null)
  767. {
  768. foreach (var kvp in request.Form)
  769. {
  770. if (dict.ContainsKey(kvp.Key)) continue;
  771. dict.Add(kvp.Key, kvp.Value);
  772. }
  773. }
  774. else if (request.PostJson)
  775. {
  776. var jprops = request.PostJson.GetProperties();
  777. foreach (var jprop in jprops)
  778. {
  779. var name = jprop.Name;
  780. if (dict.ContainsKey(name)) continue;
  781. var value = jprop.Value;
  782. if (value != null)
  783. {
  784. }
  785. dict.Add(name, value);
  786. }
  787. }
  788. return new object[] { expando };
  789. }
  790. catch { }
  791. }
  792. #endif
  793. // class
  794. if (pit.IsClass)
  795. {
  796. try
  797. {
  798. var entity = Activator.CreateInstance(pit);
  799. var setted = false;
  800. if (!setted) setted = ReadParameter(request.Data, entity);
  801. if (!setted) setted = ReadParameter(request.PostJson, entity);
  802. return new object[] { entity };
  803. }
  804. catch { }
  805. }
  806. if (request.PostJson) return new object[] { request.PostJson };
  807. if (request.Form != null) return new object[] { request.Form };
  808. if (request.PostText.NotEmpty()) return new object[] { request.PostText };
  809. return new object[] { request.PostData };
  810. }
  811. else
  812. {
  813. // string
  814. if (pit.Equals(typeof(string))) return new object[] { request.Parameters.GetValue(pin, true) };
  815. // json
  816. if (pit.Equals(typeof(Json))) return new object[] { Json.From(request.Parameters.GetValue(pin, true)) };
  817. }
  818. }
  819. var ps = new object[count];
  820. for (var i = 0; i < count; i++)
  821. {
  822. var name = pis[i].Name;
  823. var type = pis[i].ParameterType;
  824. var text = Parameter(request, name);
  825. ps[i] = ReadParameter(text, type);
  826. }
  827. return ps;
  828. }
  829. static bool ReadParameter(Json json, object entity)
  830. {
  831. if (!json) return false;
  832. if (json.IsObject)
  833. {
  834. var properties = json.GetProperties();
  835. if (properties.Length < 1) return false;
  836. Json.Object(entity, json, true, null, true);
  837. return true;
  838. }
  839. if (json.IsArray)
  840. {
  841. var items = json.GetItems();
  842. if (items.Length < 1) return false;
  843. Json.Object(entity, json, true, null, true);
  844. return true;
  845. }
  846. return false;
  847. }
  848. static object ReadParameter(string text, Type type)
  849. {
  850. if (type.Equals(typeof(object)) || type.Equals(typeof(string))) return text;
  851. if (type.Equals(typeof(bool))) return NumberUtility.Boolean(text);
  852. if (type.Equals(typeof(float))) return NumberUtility.Single(text);
  853. if (type.Equals(typeof(double))) return NumberUtility.Double(text);
  854. if (type.Equals(typeof(decimal))) return NumberUtility.Decimal(text);
  855. if (type.Equals(typeof(byte))) return NumberUtility.Byte(text);
  856. if (type.Equals(typeof(sbyte))) return NumberUtility.SByte(text);
  857. if (type.Equals(typeof(short))) return NumberUtility.Int16(text);
  858. if (type.Equals(typeof(ushort))) return NumberUtility.UInt16(text);
  859. if (type.Equals(typeof(int))) return NumberUtility.Int32(text);
  860. if (type.Equals(typeof(uint))) return NumberUtility.UInt32(text);
  861. if (type.Equals(typeof(long))) return NumberUtility.Int64(text);
  862. if (type.Equals(typeof(ulong))) return NumberUtility.UInt64(text);
  863. if (type.Equals(typeof(byte[]))) return TextUtility.FromBase64(text);
  864. if (type.Equals(typeof(Json))) return Json.From(text);
  865. return type.IsValueType ? Activator.CreateInstance(type) : null;
  866. }
  867. #endregion
  868. #region Enumerate Entries
  869. internal static Json Enumerate(IEnumerable<ApiApplication> applications, ApiOptions options)
  870. {
  871. var list = Json.NewArray();
  872. var count = 0;
  873. if (applications != null)
  874. {
  875. foreach (var app in applications)
  876. {
  877. if (app == null) continue;
  878. if (app.Hidden) continue;
  879. list.AddItem(app.ToJson(options));
  880. count = count + 1;
  881. }
  882. }
  883. var json = Json.NewObject();
  884. json.SetProperty("count", count);
  885. json.SetProperty("list", list);
  886. return json;
  887. }
  888. internal static Json Enumerate(IEnumerable<ApiFunction> functions, ApiOptions options)
  889. {
  890. var count = 0;
  891. var list = Json.NewArray();
  892. if (functions != null)
  893. {
  894. foreach (var func in functions)
  895. {
  896. if (func == null) continue;
  897. if (func.Hidden) continue;
  898. list.AddItem(func.ToJson(options));
  899. count = count + 1;
  900. }
  901. }
  902. var json = Json.NewObject();
  903. json.SetProperty("count", count);
  904. json.SetProperty("list", list);
  905. return json;
  906. }
  907. #endregion
  908. }
  909. }