HTTP2Response.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 IsCompressed { get; private set; }
  15. public HTTP2Response(HTTPRequest request, bool isFromCache)
  16. : base(request, isFromCache)
  17. {
  18. this.VersionMajor = 2;
  19. this.VersionMinor = 0;
  20. }
  21. internal void AddHeaders(List<KeyValuePair<string, string>> headers)
  22. {
  23. this.ExpectedContentLength = -1;
  24. Dictionary<string, List<string>> newHeaders = this.baseRequest.OnHeadersReceived != null ? new Dictionary<string, List<string>>() : null;
  25. for (int i = 0; i < headers.Count; ++i)
  26. {
  27. KeyValuePair<string, string> header = headers[i];
  28. if (header.Key.Equals(":status", StringComparison.Ordinal))
  29. {
  30. base.StatusCode = int.Parse(header.Value);
  31. base.Message = string.Empty;
  32. }
  33. else
  34. {
  35. if (!this.IsCompressed && header.Key.Equals("content-encoding", StringComparison.OrdinalIgnoreCase))
  36. {
  37. this.IsCompressed = true;
  38. }
  39. else if (base.baseRequest.OnDownloadProgress != null && header.Key.Equals("content-length", StringComparison.OrdinalIgnoreCase))
  40. {
  41. long contentLength;
  42. if (long.TryParse(header.Value, out contentLength))
  43. this.ExpectedContentLength = contentLength;
  44. else
  45. HTTPManager.Logger.Information("HTTP2Response", string.Format("AddHeaders - Can't parse Content-Length as an int: '{0}'", header.Value), this.baseRequest.Context, this.Context);
  46. }
  47. base.AddHeader(header.Key, header.Value);
  48. }
  49. if (newHeaders != null)
  50. {
  51. List<string> values;
  52. if (!newHeaders.TryGetValue(header.Key, out values))
  53. newHeaders.Add(header.Key, values = new List<string>(1));
  54. values.Add(header.Value);
  55. }
  56. }
  57. if (this.ExpectedContentLength == -1 && base.baseRequest.OnDownloadProgress != null)
  58. HTTPManager.Logger.Information("HTTP2Response", "AddHeaders - No Content-Length header found!", this.baseRequest.Context, this.Context);
  59. RequestEventHelper.EnqueueRequestEvent(new RequestEventInfo(this.baseRequest, newHeaders));
  60. }
  61. internal void AddData(Stream stream)
  62. {
  63. if (this.IsCompressed)
  64. {
  65. using (var decoderStream = new Decompression.Zlib.GZipStream(stream, Decompression.Zlib.CompressionMode.Decompress))
  66. {
  67. using (var ms = new BufferPoolMemoryStream((int)stream.Length))
  68. {
  69. var buf = BufferPool.Get(8 * 1024, true);
  70. int byteCount = 0;
  71. while ((byteCount = decoderStream.Read(buf, 0, buf.Length)) > 0)
  72. ms.Write(buf, 0, byteCount);
  73. BufferPool.Release(buf);
  74. base.Data = ms.ToArray();
  75. }
  76. }
  77. }
  78. else
  79. {
  80. base.Data = BufferPool.Get(stream.Length, false);
  81. stream.Read(base.Data, 0, (int)stream.Length);
  82. }
  83. }
  84. bool isPrepared;
  85. private Decompression.GZipDecompressor decompressor;
  86. internal void ProcessData(byte[] payload, int payloadLength)
  87. {
  88. if (!this.isPrepared)
  89. {
  90. this.isPrepared = true;
  91. base.BeginReceiveStreamFragments();
  92. }
  93. if (this.IsCompressed)
  94. {
  95. if (this.decompressor == null)
  96. this.decompressor = new Decompression.GZipDecompressor(0);
  97. var result = this.decompressor.Decompress(payload, 0, payloadLength, true, true);
  98. base.FeedStreamFragment(result.Data, 0, result.Length);
  99. }
  100. else
  101. base.FeedStreamFragment(payload, 0, payloadLength);
  102. }
  103. internal void FinishProcessData()
  104. {
  105. base.FlushRemainingFragmentBuffer();
  106. }
  107. protected override void Dispose(bool disposing)
  108. {
  109. base.Dispose(disposing);
  110. if (disposing)
  111. {
  112. if (this.decompressor != null)
  113. {
  114. this.decompressor.Dispose();
  115. this.decompressor = null;
  116. }
  117. }
  118. }
  119. }
  120. }
  121. #endif