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.

86 lines
2.3 KiB

11 months ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
11 months ago
3 years ago
  1. using Apewer.Network;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Net;
  8. using System.Security.Cryptography.X509Certificates;
  9. using System.Text;
  10. namespace Apewer.Web
  11. {
  12. /// <summary>请求。</summary>
  13. public sealed class MiniRequest : IToJson
  14. {
  15. /// <summary>上下文。</summary>
  16. public MiniContext Context { get; private set; }
  17. #region connection
  18. internal bool Http11 = false;
  19. /// <summary>要求保持连接。</summary>
  20. public bool KeepAlive { get; internal set; }
  21. #endregion
  22. #region headers
  23. HttpHeaders _headers = new HttpHeaders();
  24. /// <summary>头部。</summary>
  25. public HttpHeaders Headers { get => _headers; }
  26. /// <summary>统一资源定位。</summary>
  27. public Uri Url { get; internal set; }
  28. /// <summary>请求方法。</summary>
  29. public string Method { get; internal set; }
  30. /// <summary>URL 路径。</summary>
  31. public string Path { get; internal set; }
  32. /// <summary>HTTP 协议版本。</summary>
  33. public string Version { get; internal set; }
  34. /// <summary>客户端可接受 Brotli 压缩。</summary>
  35. public bool Brotli { get; internal set; }
  36. /// <summary>客户端可接受 Gzip 压缩。</summary>
  37. public bool Gzip { get; internal set; }
  38. /// <summary>内容长度,单位:字节。</summary>
  39. public long ContentLength { get => _headers.GetValue("Content-Length").Int64(); }
  40. #endregion
  41. #region
  42. /// <summary>请求体的流。</summary>
  43. public Stream Body { get => Context.Connection.GetRequestStream(); }
  44. #endregion
  45. /// <summary></summary>
  46. internal MiniRequest(MiniContext context)
  47. {
  48. Context = context;
  49. }
  50. /// <summary>专用的序列化方法。</summary>
  51. public Json ToJson()
  52. {
  53. var json = new Json();
  54. json.SetProperty("Method", Method);
  55. json.SetProperty("Path", Path);
  56. json.SetProperty("Url", Url?.OriginalString);
  57. json.SetProperty("Headers", Json.From(Headers));
  58. return json;
  59. }
  60. }
  61. }