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.

113 lines
3.5 KiB

3 years ago
8 months ago
3 years ago
8 months ago
3 years ago
8 months ago
3 years ago
8 months ago
3 years ago
8 months ago
3 years ago
  1. using Apewer.Network;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. namespace Apewer.Web
  7. {
  8. /// <summary></summary>
  9. public sealed class MiniProvider : ApiProvider<MiniContext>
  10. {
  11. MiniConnection connection;
  12. MiniContext context;
  13. MiniRequest request;
  14. MiniResponse response;
  15. /// <summary>HttpContext</summary>
  16. public override MiniContext Context { get => context; }
  17. /// <summary>创建服务程序实例。</summary>
  18. /// <exception cref="ArgumentNullException"></exception>
  19. public MiniProvider(MiniContext context)
  20. {
  21. if (context == null) throw new ArgumentNullException(nameof(context));
  22. this.context = context;
  23. connection = context.Connection;
  24. request = context.Request;
  25. response = context.Response;
  26. }
  27. /// <summary>发送完毕后执行。</summary>
  28. public override void Sent() => connection.Close();
  29. /// <summary>结束本次请求和响应。</summary>
  30. public override void End() => connection.Close(true);
  31. /// <summary>释放非托管资源。</summary>
  32. public override void Dispose() => connection.Close(true);
  33. #region request
  34. /// <summary></summary>
  35. public override string GetClientIP() => connection.RemoteEndPoint.Address.ToString();
  36. /// <summary></summary>
  37. public override long GetContentLength() => request.ContentLength;
  38. /// <summary></summary>
  39. public override string GetContentType() => request.Headers.GetValue("Content-Type");
  40. /// <summary></summary>
  41. public override HttpHeaders GetHeaders() => request.Headers;
  42. /// <summary></summary>
  43. public override HttpMethod GetMethod() => NetworkUtility.ParseHttpMethod(request.Method);
  44. /// <summary></summary>
  45. public override string GetReferrer() => request.Headers.GetValue("Referrer");
  46. /// <summary></summary>
  47. public override Uri GetUrl() => request.Url;
  48. /// <summary></summary>
  49. public override Stream RequestBody() => request.Body;
  50. #endregion
  51. #region response
  52. /// <summary></summary>
  53. public override void SetCache(int seconds)
  54. {
  55. if (seconds > 0)
  56. {
  57. SetHeader("Cache-Control", $"public, max-age={seconds}, s-maxage={seconds}");
  58. return;
  59. }
  60. SetHeader("Cache-Control", "no-cache, no-store, must-revalidate");
  61. SetHeader("Pragma", "no-cache");
  62. }
  63. /// <summary></summary>
  64. public override void SetContentLength(long value) => response.ContentLength = value;
  65. /// <summary></summary>
  66. public override void SetContentType(string value) => response.ContentType = value;
  67. /// <summary></summary>
  68. public override string SetHeader(string name, string value) => response.Headers[name] = value;
  69. /// <summary></summary>
  70. public override void SetRedirect(string location)
  71. {
  72. if (string.IsNullOrEmpty(location)) location = "/";
  73. response.Status = 302;
  74. response.Location = location;
  75. response.Close();
  76. }
  77. /// <summary></summary>
  78. public override void SetStatus(int status, int subStatus = 0) => response.Status = status;
  79. /// <summary></summary>
  80. public override Stream ResponseBody() => response.Body;
  81. #endregion
  82. }
  83. }