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.

749 lines
27 KiB

3 years ago
11 months ago
3 years ago
2 years ago
11 months ago
2 years ago
11 months ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
2 years ago
11 months ago
3 years ago
2 years ago
11 months ago
2 years ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
11 months ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
11 months ago
3 years ago
11 months ago
11 months ago
9 months ago
11 months ago
3 years ago
11 months ago
3 years ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
3 years ago
11 months ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
11 months ago
3 years ago
11 months ago
11 months ago
3 years ago
11 months ago
11 months ago
3 years ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
2 years ago
3 years ago
11 months ago
2 years ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
3 years ago
3 years ago
3 years ago
11 months ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
11 months ago
3 years ago
11 months ago
11 months ago
3 years ago
11 months ago
11 months ago
3 years ago
2 years ago
3 years ago
  1. using Apewer.Network;
  2. using Apewer.Source;
  3. using System;
  4. using System.Net;
  5. using System.Reflection;
  6. using static Apewer.Web.ApiUtility;
  7. namespace Apewer.Web
  8. {
  9. internal class ApiProcessor
  10. {
  11. private ApiContext _context = null;
  12. internal ApiProcessor(ApiContext context) => _context = context ?? throw new ArgumentNullException(nameof(context));
  13. #region prepare
  14. /// <summary>执行处理程序,返回错误信息。</summary>
  15. public void Run()
  16. {
  17. var url = null as Uri;
  18. var method = HttpMethod.NULL;
  19. var response = null as ApiResponse;
  20. try
  21. {
  22. // 检查执行的前提条件,获取 Method 和 URL。
  23. var check = Check(ref method, ref url);
  24. if (!string.IsNullOrEmpty(check))
  25. {
  26. Logger.Internals.Error(typeof(ApiInvoker), check);
  27. return;
  28. }
  29. // 准备请求模型。
  30. var request = GetRequest(_context.Provider, _context.Options, method, url);
  31. _context.Request = request;
  32. // 准备响应模型。
  33. response = new ApiResponse();
  34. response.Random = request.Random;
  35. response.Application = request.Application;
  36. response.Function = request.Function;
  37. _context.Response = response;
  38. // 调用 API。
  39. Invoke();
  40. }
  41. catch (Exception ex)
  42. {
  43. var message = ex.Message();
  44. Logger.Internals.Error(typeof(ApiInvoker), message);
  45. }
  46. finally
  47. {
  48. // 输出。
  49. if (response != null)
  50. {
  51. try
  52. {
  53. response.Duration = Duration(_context.Beginning);
  54. Output(_context.Provider, _context.Options, response, null, method);
  55. }
  56. catch { }
  57. finally
  58. {
  59. RuntimeUtility.Dispose(response.Model);
  60. }
  61. }
  62. }
  63. }
  64. static string Duration(DateTime beginning)
  65. {
  66. var span = DateTime.Now - beginning;
  67. var ms = span.TotalMilliseconds;
  68. if (ms < 1000) return Math.Round(ms, 0).ToString() + "ms";
  69. if (ms < 10000) return Math.Round(ms / 1000, 2).ToString() + "s";
  70. if (ms < 60000) return Math.Round(ms / 1000, 1).ToString() + "s";
  71. return Math.Round(ms / 1000, 0).ToString() + "s";
  72. }
  73. string Check(ref HttpMethod method, ref Uri url)
  74. {
  75. // 服务程序检查。
  76. var check = _context.Provider.PreInvoke();
  77. if (!string.IsNullOrEmpty(check)) return check;
  78. // URL
  79. url = _context.Provider.GetUrl();
  80. if (url == null) return "URL 无效。";
  81. method = _context.Provider.GetMethod();
  82. if (method == HttpMethod.NULL) return "HTTP 方法无效。";
  83. if (method == HttpMethod.OPTIONS) return null;
  84. // favicon.ico
  85. var lowerPath = TextUtility.AssureStarts(TextUtility.Lower(url.AbsolutePath), "/");
  86. if (!_context.Options.AllowFavIcon)
  87. {
  88. if (lowerPath.StartsWith("/favicon.ico"))
  89. {
  90. Output(_context.Provider, _context.Options, null, null, null);
  91. return "已取消对 favicon.ico 的请求。";
  92. }
  93. }
  94. // robots.txt
  95. if (!_context.Options.AllowRobots)
  96. {
  97. if (lowerPath.StartsWith("/robots.txt"))
  98. {
  99. const string text = "User-agent: *\nDisallow: / \n";
  100. Output(_context.Provider, _context.Options, null, "text/plain", TextUtility.Bytes(text));
  101. return "已取消对 robots.txt 的请求。";
  102. }
  103. }
  104. return null;
  105. }
  106. // 寻找入口。
  107. void Invoke()
  108. {
  109. // OPTIONS
  110. if (_context.Request.Method == HttpMethod.OPTIONS)
  111. {
  112. _context.Response.Model = new ApiTextModel("");
  113. return;
  114. }
  115. // 路由
  116. if (_context.Options.UseRoute)
  117. {
  118. var path = _context?.Request?.Url?.AbsolutePath;
  119. path = path.TrimEnd('/');
  120. var action = _context.Entries.GetAction(path);
  121. if (action != null)
  122. {
  123. _context.ApiAction = action;
  124. Invoke(action);
  125. return;
  126. }
  127. }
  128. // 反射
  129. if (_context.Options.UseReflection)
  130. {
  131. var appName = _context.Request.Application;
  132. var application = _context.Entries.GetApplication(appName);
  133. Invoke(application);
  134. return;
  135. }
  136. // 未匹配到
  137. _context.Response.Duration = Duration(_context.Beginning);
  138. _context.Response.Model = new ApiStatusModel(404);
  139. }
  140. #endregion
  141. #region common
  142. static Type Void = typeof(void);
  143. // 创建控制器实例
  144. static ApiController CreateController(Type type, ApiContext context)
  145. {
  146. var controller = (ApiController)Activator.CreateInstance(type);
  147. ApiUtility.SetContext(controller, context);
  148. return controller;
  149. }
  150. static void Invoke(ApiContext context, MethodInfo method, ApiParameter[] parameters)
  151. {
  152. context.MethodInfo = method;
  153. // 调用。
  154. var parametersValue = ReadParameters(context.Request, parameters);
  155. var controller = context.Controller;
  156. var returnValue = method.Invoke(controller, parametersValue);
  157. // 程序要求停止输出。
  158. var response = context.Response;
  159. if (response.StopReturn) return;
  160. // 已经有了返回模型。
  161. if (response.Model != null) return;
  162. // 没有返回类型。
  163. var returnType = method.ReturnType;
  164. if (returnType == null || returnType.Equals(Void)) return;
  165. // 已明确字符串类型。
  166. if (returnType.Equals(typeof(string)))
  167. {
  168. var textValue = returnValue as string;
  169. var textRenderer = context.Options.TextRenderer;
  170. if (textRenderer != null)
  171. {
  172. textRenderer.Invoke(context, textValue);
  173. return;
  174. }
  175. // 默认视为提示错误
  176. if (!string.IsNullOrEmpty(textValue)) response.Error(textValue);
  177. return;
  178. }
  179. // 已明确 Exception 类型,视为提示错误。
  180. if (returnValue is Exception)
  181. {
  182. ApiUtility.Exception(response, returnValue as Exception);
  183. return;
  184. }
  185. // 已明确 Json 类型。
  186. if (returnValue is Json json)
  187. {
  188. var renderer = context.Options.JsonRenderer;
  189. if (renderer != null)
  190. {
  191. renderer.Invoke(context, json);
  192. return;
  193. }
  194. // 默认设置到 data 属性。
  195. response.Data = json;
  196. return;
  197. }
  198. // 已明确 Model 类型。
  199. if (returnValue is IApiModel model)
  200. {
  201. response.Model = model;
  202. return;
  203. }
  204. // 已明确 Result 类型。
  205. if (returnValue is IActionResult result)
  206. {
  207. response.Model = result;
  208. return;
  209. }
  210. // 类型未知,尝试 ToJson 方法。
  211. if (returnValue is IToJson toJson)
  212. {
  213. var tojson = toJson.ToJson();
  214. var renderer = context.Options.JsonRenderer;
  215. if (renderer != null)
  216. {
  217. renderer.Invoke(context, tojson);
  218. return;
  219. }
  220. response.Data = tojson;
  221. return;
  222. }
  223. // 未知返回类型,尝试使用默认渲染器。
  224. var defaultRenderer = context.Options.DefaultRenderer;
  225. if (defaultRenderer != null) defaultRenderer.Invoke(context, returnValue);
  226. }
  227. #endregion
  228. #region route
  229. // 执行 Action。
  230. void Invoke(ApiAction action)
  231. {
  232. var controller = null as ApiController;
  233. try
  234. {
  235. // 准备控制器。
  236. controller = CreateController(action.Type, _context);
  237. // 准备参数。
  238. var parameters = action.Parameters;
  239. var values = ReadParameters(_context.Request, parameters);
  240. // 调用。
  241. _context.Controller = controller;
  242. Invoke(_context, action.MethodInfo, action.Parameters);
  243. }
  244. catch (Exception ex)
  245. {
  246. if (ex.InnerException != null) ex = ex.InnerException;
  247. ApiUtility.Exception(_context.Response, ex, _context.Options.WithException);
  248. var catcher = _context.Invoker.Catcher;
  249. if (catcher != null)
  250. {
  251. try
  252. {
  253. var apiCatch = new ApiCatch(_context, ex);
  254. catcher.Invoke(apiCatch);
  255. }
  256. catch { }
  257. }
  258. }
  259. finally
  260. {
  261. RuntimeUtility.Dispose(controller);
  262. }
  263. }
  264. #endregion
  265. #region reflection
  266. // 创建控制器。
  267. void Invoke(ApiApplication application)
  268. {
  269. var options = _context.Options;
  270. var entries = _context.Entries;
  271. var request = _context.Request;
  272. var response = _context.Response;
  273. // Application 无效,尝试默认控制器和枚举。
  274. if (application == null)
  275. {
  276. var @default = options.Default;
  277. if (@default == null)
  278. {
  279. // 没有指定默认控制器,尝试枚举。
  280. response.Status = "notfound";
  281. response.Message = "Not Found";
  282. if (options.AllowEnumerate) response.Data = Enumerate(entries.Applications, options);
  283. return;
  284. }
  285. else
  286. {
  287. // 创建默认控制器。
  288. var controller = null as ApiController;
  289. try
  290. {
  291. controller = CreateController(@default, _context);
  292. Invoke(controller, application, null, options, request, response);
  293. }
  294. catch (Exception ex)
  295. {
  296. ApiUtility.Exception(response, ex.InnerException ?? ex);
  297. }
  298. finally
  299. {
  300. RuntimeUtility.Dispose(controller);
  301. }
  302. }
  303. }
  304. else
  305. {
  306. // 创建控制器时候会填充 Controller.Request 属性,可能导致 Request.Function 被篡改,所以在创建之前获取 Function。
  307. var function = application.GetFunction(request.Function);
  308. var controller = null as ApiController;
  309. try
  310. {
  311. controller = CreateController(application.Type, _context);
  312. Invoke(controller, application, function, options, request, response);
  313. }
  314. catch (Exception ex)
  315. {
  316. ApiUtility.Exception(response, ex.InnerException ?? ex);
  317. }
  318. finally
  319. {
  320. RuntimeUtility.Dispose(controller);
  321. }
  322. }
  323. }
  324. // 调用 Function。
  325. void Invoke(ApiController controller, ApiApplication application, ApiFunction function, ApiOptions options, ApiRequest request, ApiResponse response)
  326. {
  327. try
  328. {
  329. // 控制器初始化。
  330. var initializer = ApiUtility.GetInitialier(controller);
  331. var match = initializer == null ? true : initializer.Invoke(controller);
  332. if (!match) return;
  333. if (application.Independent) return;
  334. if (function != null)
  335. {
  336. // 调用 API,获取返回值。
  337. _context.Controller = controller;
  338. Invoke(_context, function.Method, function.Parameters);
  339. }
  340. else
  341. {
  342. // 未匹配到 Function,尝试 Default。
  343. var @default = ApiUtility.GetDefault(controller);
  344. if (@default != null)
  345. {
  346. @default.Invoke(controller);
  347. return;
  348. }
  349. // 没有执行任何 Function,尝试枚举。
  350. response.Status = "notfound";
  351. if (application.Hidden)
  352. {
  353. response.Message = "Not Found";
  354. }
  355. else
  356. {
  357. response.Message = "Not Found";
  358. if (options.AllowEnumerate) response.Data = Enumerate(application.Functions, options);
  359. }
  360. }
  361. }
  362. catch (Exception ex)
  363. {
  364. if (ex.InnerException != null) ex = ex.InnerException;
  365. ApiUtility.Exception(_context.Response, ex, _context.Options.WithException);
  366. var catcher = _context.Invoker.Catcher;
  367. if (catcher != null)
  368. {
  369. try
  370. {
  371. var apiCatch = new ApiCatch(_context, ex);
  372. catcher.Invoke(apiCatch);
  373. }
  374. catch { }
  375. }
  376. }
  377. }
  378. #endregion
  379. #region static
  380. internal static ApiRequest GetRequest(ApiProvider provider, ApiOptions options, HttpMethod method, Uri url)
  381. {
  382. // 创建数据对象。
  383. var request = new ApiRequest();
  384. // Http Method。
  385. request.Method = method;
  386. // 基本信息。
  387. var ip = provider.GetClientIP();
  388. var headers = provider.GetHeaders() ?? new HttpHeaders();
  389. request.Headers = headers;
  390. request.IP = ip;
  391. request.Url = url;
  392. request.Referrer = provider.GetReferrer();
  393. request.Parameters = ApiUtility.Parameters(url.Query);
  394. // Headers。
  395. request.UserAgent = ApiUtility.UserAgent(headers);
  396. request.Cookies = ParseCookies(headers) ?? new CookieCollection();
  397. // 匹配 API。
  398. var application = null as string;
  399. var function = null as string;
  400. var random = null as string;
  401. var ticket = null as string;
  402. var session = null as string;
  403. var page = null as string;
  404. // 解析 POST 请求。
  405. switch (request.Method)
  406. {
  407. case HttpMethod.PATCH:
  408. case HttpMethod.POST:
  409. case HttpMethod.PUT:
  410. var preRead = provider.PreRead();
  411. if (string.IsNullOrEmpty(preRead))
  412. {
  413. var post = null as byte[];
  414. var length = 0L;
  415. var max = options.MaxRequestBody;
  416. if (max == 0) post = new byte[0];
  417. else if (max < 0) post = provider.RequestBody().Read();
  418. else
  419. {
  420. length = provider.GetContentLength();
  421. if (length <= max) post = provider.RequestBody().Read();
  422. }
  423. length = post == null ? 0 : post.Length;
  424. if (length > 1)
  425. {
  426. request.PostData = post;
  427. if (length < 104857600)
  428. {
  429. var text = TextUtility.FromBytes(post);
  430. request.PostText = text;
  431. // 尝试解析 Json,首尾必须是“{}”或“[]”。
  432. var first = post[0];
  433. var last = post[length - 1];
  434. if ((first == 123 && last == 125) || (first == 91 && last == 93))
  435. {
  436. var json = Json.From(text);
  437. if (json != null && json.IsObject)
  438. {
  439. application = json["application"];
  440. function = json["function"];
  441. random = json["random"];
  442. ticket = json["ticket"];
  443. session = json["session"];
  444. page = json["page"];
  445. var data = json.GetProperty("data");
  446. request.PostJson = json;
  447. request.Data = data ?? Json.NewObject();
  448. }
  449. }
  450. // 尝试解析 Form,需要 application/x-www-form-urlencoded
  451. var contentType = headers.GetValue("Content-Type") ?? "";
  452. if (contentType.Contains("urlencoded")) request.Form = ApiUtility.Parameters(text);
  453. }
  454. }
  455. }
  456. break;
  457. }
  458. // 解析 URL 参数。
  459. // URL 参数的优先级应高于 URL 路径,以避免反向代理产生的路径问题。
  460. var urlParameters = ApiUtility.Parameters(request.Url.Query);
  461. if (string.IsNullOrEmpty(application)) application = urlParameters.GetValue("application");
  462. if (string.IsNullOrEmpty(function)) function = urlParameters.GetValue("function");
  463. if (string.IsNullOrEmpty(random)) random = urlParameters.GetValue("random");
  464. if (string.IsNullOrEmpty(ticket)) ticket = urlParameters.GetValue("ticket");
  465. if (string.IsNullOrEmpty(session)) session = urlParameters.GetValue("session");
  466. if (string.IsNullOrEmpty(page)) page = urlParameters.GetValue("page");
  467. // 从 Cookie 中获取 Ticket。
  468. var cookies = request.Cookies;
  469. if (string.IsNullOrEmpty(ticket)) ticket = cookies.GetValue("ticket");
  470. // 最后检查 URL 路径。
  471. var paths = (request.Url.AbsolutePath ?? "").Split('/');
  472. if (string.IsNullOrEmpty(application) && paths.Length >= 2) application = TextUtility.DecodeUrl(paths[1]);
  473. if (string.IsNullOrEmpty(function) && paths.Length >= 3) function = TextUtility.DecodeUrl(paths[2]);
  474. // 修正内容。
  475. application = TextUtility.Trim(application);
  476. function = TextUtility.Trim(function);
  477. random = TextUtility.Trim(random);
  478. ticket = TextUtility.Trim(ticket);
  479. session = TextUtility.Trim(session);
  480. page = TextUtility.Trim(page);
  481. // 设置请求:回传。
  482. request.Application = application;
  483. request.Function = function;
  484. request.Random = random;
  485. // 设置请求:不回传。
  486. request.Ticket = ticket;
  487. request.Session = session;
  488. request.Page = page;
  489. return request;
  490. }
  491. static StringPairs PrepareHeaders(ApiOptions options, ApiResponse response, ApiRequest request = null)
  492. {
  493. var merged = new StringPairs();
  494. if (options != null)
  495. {
  496. // 跨域访问。
  497. if (options.WithAccessControl)
  498. {
  499. merged.Add("Access-Control-Allow-Headers", "Content-Type");
  500. merged.Add("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
  501. merged.Add("Access-Control-Allow-Origin", "*");
  502. var maxage = options.AccessControlMaxAge;
  503. if (maxage > 0) merged.Add("Access-Control-Max-Age", maxage.ToString());
  504. if (request != null && request.Headers != null)
  505. {
  506. var @private = request.Headers.GetValue("Access-Control-Request-Private-Network");
  507. if (NumberUtility.Boolean(@private)) merged.Add("Access-Control-Allow-Private-Network", "true");
  508. }
  509. }
  510. // Content-Type 检查。
  511. if (options.WithContentTypeOptions)
  512. {
  513. merged.Add("X-Content-Type-Options", "nosniff");
  514. }
  515. // 用于客户端,当前页面使用 HTTPS 时,将资源升级为 HTTPS。
  516. if (options.UpgradeHttps)
  517. {
  518. merged.Add("Content-Security-Policy", "upgrade-insecure-requests");
  519. }
  520. // 包含 API 的处理时间。
  521. if (options.WithDuration && response != null)
  522. {
  523. if (response.Duration.NotEmpty()) merged.Add("Duration", response.Duration);
  524. }
  525. }
  526. if (response != null)
  527. {
  528. // Cookies。
  529. var setCookies = SetCookie(response.Cookies);
  530. if (setCookies != null)
  531. {
  532. foreach (var value in setCookies) merged.Add("Set-Cookie", value);
  533. }
  534. // 自定义头。
  535. var headers = response.Headers;
  536. if (headers != null)
  537. {
  538. foreach (var header in headers)
  539. {
  540. var key = TextUtility.Trim(header.Name);
  541. if (string.IsNullOrEmpty(key)) continue;
  542. var value = header.Value;
  543. if (string.IsNullOrEmpty(value)) continue;
  544. merged.Add(key, value);
  545. }
  546. }
  547. }
  548. return merged;
  549. }
  550. internal void Output(ApiProvider provider, ApiOptions options, ApiResponse response, string type, byte[] bytes)
  551. {
  552. var preWrite = provider.PreWrite();
  553. if (!string.IsNullOrEmpty(preWrite)) return;
  554. if (response != null)
  555. {
  556. var responsePreOutput = response.PreOutput;
  557. if (responsePreOutput != null)
  558. {
  559. var @continue = responsePreOutput.Invoke(_context);
  560. if (!@continue) return;
  561. }
  562. }
  563. var invokerPreOutput = _context.Invoker.PreOutput;
  564. if (invokerPreOutput != null)
  565. {
  566. var @continue = invokerPreOutput.Invoke(_context);
  567. if (!@continue) return;
  568. }
  569. var optionsPreOutput = _context.Options.PreOutput;
  570. if (optionsPreOutput != null)
  571. {
  572. var @continue = optionsPreOutput.Invoke(_context);
  573. if (!@continue) return;
  574. }
  575. var headers = PrepareHeaders(options, response);
  576. foreach (var header in headers) provider.SetHeader(header.Key, header.Value);
  577. provider.SetCache(0);
  578. provider.SetContentType(string.IsNullOrEmpty(type) ? "application/octet-stream" : type);
  579. var length = bytes == null ? 0 : bytes.Length;
  580. provider.SetContentLength(length);
  581. if (length > 0) provider.ResponseBody().Write(bytes, 0, bytes.Length);
  582. provider.Sent();
  583. }
  584. internal void Output(ApiProvider provider, ApiOptions options, ApiResponse response, ApiRequest request, HttpMethod method)
  585. {
  586. var preWrite = provider.PreWrite();
  587. if (!string.IsNullOrEmpty(preWrite)) return;
  588. if (response != null)
  589. {
  590. var responsePreOutput = response.PreOutput;
  591. if (responsePreOutput != null)
  592. {
  593. var @continue = responsePreOutput.Invoke(_context);
  594. if (!@continue) return;
  595. }
  596. }
  597. var invokerPreOutput = _context.Invoker.PreOutput;
  598. if (invokerPreOutput != null)
  599. {
  600. var @continue = invokerPreOutput.Invoke(_context);
  601. if (!@continue) return;
  602. }
  603. var optionsPreOutput = _context.Options.PreOutput;
  604. if (optionsPreOutput != null)
  605. {
  606. var @continue = optionsPreOutput.Invoke(_context);
  607. if (!@continue) return;
  608. }
  609. // 设置头。
  610. var headers = PrepareHeaders(options, response, request);
  611. foreach (var header in headers) provider.SetHeader(header.Key, header.Value);
  612. // 自定义模型
  613. var model = response.Model as IApiModel;
  614. var result = response.Model as IActionResult;
  615. if (model != null)
  616. {
  617. try
  618. {
  619. model.Output(_context);
  620. }
  621. catch (Exception ex)
  622. {
  623. Logger.Internals.Exception(ex, model);
  624. }
  625. RuntimeUtility.Dispose(model);
  626. return;
  627. }
  628. else if (result != null)
  629. {
  630. try
  631. {
  632. result.ExecuteResult(_context);
  633. }
  634. catch (Exception ex)
  635. {
  636. Logger.Internals.Exception(ex, result);
  637. }
  638. RuntimeUtility.Dispose(result);
  639. return;
  640. }
  641. var text = ApiUtility.ToJson(response, options);
  642. var bytes = TextUtility.Bytes(text);
  643. provider.SetCache(0);
  644. provider.SetContentType("text/json; charset=utf-8");
  645. provider.SetContentLength(bytes.Length);
  646. var stream = provider.ResponseBody();
  647. if (stream != null && stream.CanWrite) stream.Write(bytes, 0, bytes.Length);
  648. provider.Sent();
  649. }
  650. #endregion
  651. }
  652. }