Guest User

Untitled

a guest
Feb 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. namespace XXX.XXX.XXX.XXX.XXX
  2. {
  3. public class HttpGZipClientHandler : System.Net.Http.HttpClientHandler
  4. {
  5. long time = 0;
  6. private long _downloadedBytesFromServer;
  7. private long _downloadedProcessedBytes;
  8. private long _intendedUploadedBytesToServer;
  9. private long _uploadedBytesToServer;
  10. private long _additionalTimeOverhead = 0;
  11.  
  12. public override bool SupportsAutomaticDecompression { get { return true; } }
  13. public long DownloadedBytesFromServer { get { return _downloadedBytesFromServer; } }
  14. public long DownloadedProcessedBytes { get { return _downloadedProcessedBytes; } }
  15. public long IntendedUploadedBytesToServer { get { return _intendedUploadedBytesToServer; } }
  16. public long UploadedBytesToServer { get { return _uploadedBytesToServer; } }
  17. public long AdditionalTimeOverhead { get { return _additionalTimeOverhead; } }
  18.  
  19. public void ResetStatistics()
  20. {
  21. _downloadedBytesFromServer = 0;
  22. _downloadedProcessedBytes = 0;
  23. _intendedUploadedBytesToServer = 0;
  24. _uploadedBytesToServer = 0;
  25. _additionalTimeOverhead = 0;
  26. }
  27.  
  28. protected override async Task<System.Net.Http.HttpResponseMessage> SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
  29. {
  30.  
  31. //Save content headers before compressing
  32. System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>> savedContentHeaders = new Dictionary<string, IEnumerable<string>>();
  33. foreach (System.Collections.Generic.KeyValuePair<string, System.Collections.Generic.IEnumerable<string>> keyValue in request.Content.Headers)
  34. {
  35. savedContentHeaders.Add(keyValue.Key, keyValue.Value);
  36. }
  37.  
  38. //Compress request content
  39. System.Diagnostics.Stopwatch sp1 = new System.Diagnostics.Stopwatch();
  40. sp1.Start();
  41.  
  42. _intendedUploadedBytesToServer += request.Content.Headers.ContentLength.HasValue ? request.Content.Headers.ContentLength.Value : 0;
  43.  
  44. await request.Content.LoadIntoBufferAsync().ConfigureAwait(false);
  45. request.Content = new HttpGZipContent(await request.Content.ReadAsByteArrayAsync().ConfigureAwait(false), System.IO.Compression.CompressionMode.Compress);
  46.  
  47. byte[] uploadedBytes = await request.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
  48. _uploadedBytesToServer += uploadedBytes.Length;
  49.  
  50. sp1.Stop();
  51. _additionalTimeOverhead += sp1.ElapsedMilliseconds;
  52.  
  53. //Set headers
  54. foreach (System.Collections.Generic.KeyValuePair<string, System.Collections.Generic.IEnumerable<string>> keyValue in savedContentHeaders)
  55. {
  56. request.Content.Headers.Add(keyValue.Key, keyValue.Value);
  57. }
  58.  
  59. request.Headers.AcceptEncoding.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("gzip"));
  60. request.Content.Headers.Add("Content-Encoding", "gzip");
  61.  
  62. //Execute request
  63. System.Net.Http.HttpResponseMessage response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
  64. _downloadedBytesFromServer += response.Content.Headers.ContentLength.HasValue ? response.Content.Headers.ContentLength.Value : 0;
  65.  
  66. //Decompress response content
  67. if (response.Content.Headers.ContentEncoding.Contains("gzip"))
  68. {
  69. System.Diagnostics.Stopwatch sp2 = new System.Diagnostics.Stopwatch();
  70. sp2.Start();
  71.  
  72. await response.Content.LoadIntoBufferAsync().ConfigureAwait(false);
  73. response.Content = new HttpGZipContent(await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false), System.IO.Compression.CompressionMode.Decompress);
  74.  
  75. byte[] processedBytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
  76. _downloadedProcessedBytes += processedBytes.Length;
  77.  
  78. sp2.Stop();
  79. _additionalTimeOverhead += sp2.ElapsedMilliseconds;
  80. }
  81. else
  82. _downloadedProcessedBytes += response.Content.Headers.ContentLength.HasValue ? response.Content.Headers.ContentLength.Value : 0;
  83.  
  84. return response;
  85. }
  86. }
  87.  
  88. internal sealed class HttpGZipContent : System.Net.Http.HttpContent
  89. {
  90. private readonly byte[] _content;
  91. private readonly System.IO.Compression.CompressionMode _compressionMode;
  92.  
  93. public HttpGZipContent(byte[] content, System.IO.Compression.CompressionMode compressionMode)
  94. {
  95. _compressionMode = compressionMode;
  96. _content = content;
  97. }
  98.  
  99. protected override async System.Threading.Tasks.Task SerializeToStreamAsync(System.IO.Stream stream, System.Net.TransportContext context)
  100. {
  101. if (_compressionMode == System.IO.Compression.CompressionMode.Compress)
  102. {
  103. using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(_content.Length))
  104. {
  105. using (System.IO.Compression.GZipStream zipStream = new System.IO.Compression.GZipStream(memoryStream, System.IO.Compression.CompressionMode.Compress))
  106. {
  107. zipStream.Write(_content, 0, _content.Length);
  108. zipStream.Flush();
  109. }
  110.  
  111. byte[] compressed = memoryStream.ToArray();
  112. System.IO.MemoryStream copyStream = new System.IO.MemoryStream(compressed);
  113. await copyStream.CopyToAsync(stream).ConfigureAwait(false);
  114. }
  115. }
  116. else
  117. {
  118. using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(_content, 0, _content.Length))
  119. {
  120. using (System.IO.Compression.GZipStream zipStream = new System.IO.Compression.GZipStream(memoryStream, System.IO.Compression.CompressionMode.Decompress))
  121. {
  122. await zipStream.CopyToAsync(stream).ConfigureAwait(false);
  123. }
  124. }
  125. }
  126. }
  127.  
  128. protected override bool TryComputeLength(out long length)
  129. {
  130. length = _content.Length;
  131. return true;
  132. }
  133.  
  134. protected override void Dispose(bool disposing)
  135. {
  136. base.Dispose(disposing);
  137. }
  138. }
  139. }
Add Comment
Please, Sign In to add comment