HTTP2Response.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #if (!UNITY_WEBGL || UNITY_EDITOR) && !BESTHTTP_DISABLE_ALTERNATE_SSL && !BESTHTTP_DISABLE_HTTP2
  2. using BestHTTP.Core;
  3. using BestHTTP.Extensions;
  4. using BestHTTP.PlatformSupport.Memory;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. namespace BestHTTP.Connections.HTTP2
  9. {
  10. public sealed class HTTP2Response : HTTPResponse
  11. {
  12. // For progress report
  13. public long ExpectedContentLength { get; private set; }
  14. public bool HasContentEncoding { get => !string.IsNullOrEmpty(this.contentEncoding); }
  15. private string contentEncoding = null;
  16. bool isPrepared;
  17. private Decompression.IDecompressor decompressor;
  18. public HTTP2Response(HTTPRequest request, bool isFromCache)
  19. : base(request, isFromCache)
  20. {
  21. this.VersionMajor = 2;
  22. this.VersionMinor = 0;
  23. }
  24. internal void AddHeaders(List<KeyValuePair<string, string>> headers)
  25. {
  26. this.ExpectedContentLength = -1;
  27. Dictionary<string, List<string>> newHeaders = this.baseRequest.OnHeadersReceived != null ? new Dictionary<string, List<string>>() : null;
  28. for (int i = 0; i < headers.Count; ++i)
  29. {
  30. KeyValuePair<string, string> header = headers[i];
  31. if (header.Key.Equals(":status", StringComparison.Ordinal))
  32. {
  33. base.StatusCode = int.Parse(header.Value);
  34. base.Message = string.Empty;
  35. }
  36. else
  37. {
  38. if (!this.HasContentEncoding && header.Key.Equals("content-encoding", StringComparison.OrdinalIgnoreCase))
  39. {
  40. this.contentEncoding = header.Value;
  41. }
  42. else if (base.baseRequest.OnDownloadProgress != null && header.Key.Equals("content-length", StringComparison.OrdinalIgnoreCase))
  43. {
  44. long contentLength;
  45. if (long.TryParse(header.Value, out contentLength))
  46. this.ExpectedContentLength = contentLength;
  47. else
  48. HTTPManager.Logger.Information("HTTP2Response", string.Format("AddHeaders - Can't parse Content-Length as an int: '{0}'", header.Value), this.baseRequest.Context, this.Context);
  49. }
  50. base.AddHeader(header.Key, header.Value);
  51. }
  52. if (newHeaders != null)
  53. {
  54. List<string> values;
  55. if (!newHeaders.TryGetValue(header.Key, out values))
  56. newHeaders.Add(header.Key, values = new List<string>(1));
  57. values.Add(header.Value);
  58. }
  59. }
  60. if (this.ExpectedContentLength == -1 && base.baseRequest.OnDownloadProgress != null)
  61. HTTPManager.Logger.Information("HTTP2Response", "AddHeaders - No Content-Length header found!", this.baseRequest.Context, this.Context);
  62. RequestEventHelper.EnqueueRequestEvent(new RequestEventInfo(this.baseRequest, newHeaders));
  63. }
  64. internal void AddData(Stream stream)
  65. {
  66. if (this.HasContentEncoding)
  67. {
  68. Stream decoderStream = Decompression.DecompressorFactory.GetDecoderStream(stream, this.contentEncoding);
  69. if (decoderStream == null)
  70. {
  71. base.Data = new byte[stream.Length];
  72. stream.Read(base.Data, 0, (int)stream.Length);
  73. }
  74. else
  75. {
  76. using (var ms = new BufferPoolMemoryStream((int)stream.Length))
  77. {
  78. var buf = BufferPool.Get(HTTPResponse.MinReadBufferSize, true);
  79. int byteCount = 0;
  80. while ((byteCount = decoderStream.Read(buf, 0, buf.Length)) > 0)
  81. ms.Write(buf, 0, byteCount);
  82. BufferPool.Release(buf);
  83. base.Data = ms.ToArray();
  84. }
  85. decoderStream.Dispose();
  86. }
  87. }
  88. else
  89. {
  90. base.Data = new byte[stream.Length];
  91. stream.Read(base.Data, 0, (int)stream.Length);
  92. }
  93. }
  94. internal void ProcessData(byte[] payload, int payloadLength)
  95. {
  96. if (!this.isPrepared)
  97. {
  98. this.isPrepared = true;
  99. base.BeginReceiveStreamFragments();
  100. }
  101. if (this.HasContentEncoding)
  102. {
  103. if (this.decompressor == null)
  104. this.decompressor = Decompression.DecompressorFactory.GetDecompressor(this.contentEncoding, this.Context);
  105. var result = this.decompressor.Decompress(payload, 0, payloadLength, true, true);
  106. base.FeedStreamFragment(result.Data, 0, result.Length);
  107. }
  108. else
  109. base.FeedStreamFragment(payload, 0, payloadLength);
  110. }
  111. internal void FinishProcessData()
  112. {
  113. base.FlushRemainingFragmentBuffer();
  114. }
  115. protected override void Dispose(bool disposing)
  116. {
  117. base.Dispose(disposing);
  118. if (disposing)
  119. {
  120. if (this.decompressor != null)
  121. {
  122. this.decompressor.Dispose();
  123. this.decompressor = null;
  124. }
  125. }
  126. }
  127. }
  128. }
  129. #endif