HttpHeader.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. namespace IFramework.Net.Http
  5. {
  6. public class HttpHeader
  7. {
  8. public string Protocol { get; set; } = "HTTP/1.1";
  9. public HttpOption Option { get; set; }
  10. public string RelativeUri { get; set; } = "/";
  11. public string Host { get; set; }
  12. public string Connection { get; set; }
  13. public string UserAgent { get; set; }
  14. public string Accept { get; set; }
  15. public string CacheControl { get; set; }
  16. public string ContentType { get; set; }
  17. public string AcceptEncoding { get; set; }
  18. public string AcceptLanguage { get; set; }
  19. public string Referer { get; set; }
  20. public string Cookie { get; set; }
  21. public string SecFetchUser { get; set; }
  22. public string SecFetchMode { get; set; }
  23. public string SecFetchSite { get; set; }
  24. public string SecFetchDest { get; set; }
  25. /// <summary>
  26. /// extend reserve
  27. /// </summary>
  28. public string Extensions { get; set; }
  29. /// <summary>
  30. /// do not track
  31. /// </summary>
  32. public string DNT { get; set; }
  33. public long StreamPosition { get; private set; }
  34. public long ContentLength { get; set; }
  35. public HttpHeader(string httpContent)
  36. {
  37. string[] lines = httpContent.Split('\n');
  38. GetHeaderLine(lines[0]);
  39. for(int i = 1; i < lines.Length; ++i)
  40. {
  41. if (lines[i] == string.Empty) break;
  42. GetHeader(lines[i]);
  43. }
  44. }
  45. public HttpHeader(SegmentOffset segment)
  46. {
  47. using (MemoryStream ms = new MemoryStream(segment.buffer, segment.offset, segment.size))
  48. using (StreamReader reader = new StreamReader(ms, Encoding.UTF8))
  49. {
  50. string line = reader.ReadLine();// first line
  51. GetHeaderLine(line);
  52. while ((line=reader.ReadLine()) != null)
  53. {
  54. if (line == string.Empty)
  55. {
  56. ContentLength = ms.Length - ms.Position;
  57. StreamPosition = ms.Position;
  58. break;
  59. }
  60. GetHeader(line);
  61. }
  62. }
  63. }
  64. #if NET_VERSION_4_5
  65. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  66. #endif
  67. protected void GetHeaderLine(string line)
  68. {
  69. var ops = line.Split(' ');
  70. Option = (HttpOption)Enum.Parse(typeof(HttpOption), ops[0]);
  71. RelativeUri = ops[1];
  72. Protocol = ops[2];
  73. }
  74. public string ToHeaderString(HttpStatusCode httpStatusCode)
  75. {
  76. StringBuilder builder = new StringBuilder();
  77. builder.AppendFormat("{0} {1} {2}", Protocol, ((int)httpStatusCode), httpStatusCode.ToString());
  78. builder.AppendLine();
  79. builder.AppendLine("Cache-Control:"+CacheControl);
  80. builder.AppendLine("Connection:"+Connection);
  81. //builder.AppendLine("Content-Encoding:"+ AcceptEncoding);
  82. builder.AppendLine("Content-Type:" + ContentType);
  83. builder.AppendLine("Date:" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"));
  84. if (string.IsNullOrEmpty(Extensions) == false)
  85. builder.AppendLine(Extensions);
  86. builder.AppendLine();
  87. return builder.ToString();
  88. }
  89. private void GetHeader(string line)
  90. {
  91. string[] dics = line.Split(':');
  92. if (dics.Length <= 1) return;
  93. switch (dics[0])
  94. {
  95. case "Host":Host = dics[1]; break;
  96. case "Connection": Connection = dics[1];break;
  97. case "DNT": DNT = dics[1]; break;
  98. case "Accept": Accept = dics[1]; break;
  99. case "Referer": Referer = dics[1]; break;
  100. case "Cookie": Cookie = dics[1]; break;
  101. case "User-Agent": UserAgent = dics[1]; break;
  102. case "Cache-Control": CacheControl = dics[1]; break;
  103. case "Accept-Encoding": AcceptEncoding = dics[1]; break;
  104. case "Accept-Language": AcceptLanguage = dics[1]; break;
  105. case "Sec-Fetch-User": SecFetchUser = dics[1]; break;
  106. case "Sec-Fetch-Mode": SecFetchMode = dics[1]; break;
  107. case "Sec-Fetch-Site": SecFetchSite = dics[1]; break;
  108. case "Sec-Fetch-Dest": SecFetchDest = dics[1]; break;
  109. default: Extensions += dics[1];break;
  110. }
  111. }
  112. }
  113. public enum HttpOption
  114. {
  115. GET,
  116. POST,
  117. PUT,
  118. DELETE,
  119. HEAD,
  120. TRACE,
  121. PATCH,
  122. OPTIONS
  123. }
  124. }