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.

85 lines
2.3 KiB

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