Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rosolko committed Jun 22, 2024
1 parent f7a517f commit 5d66a17
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 40 deletions.
20 changes: 7 additions & 13 deletions WebDriverManager/Clients/ChromeForTestingClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,20 @@ private static TResult GetResultFromHttpTask<TResult>(Task<HttpResponseMessage>
{
var httpTask = Task.Run(() => taskToRun);
httpTask.Wait();
var response = httpTask.Result;
if (response.Content.Headers.Contains("Content-Encoding"))

var encoding = httpTask.Result.Content.Headers?.GetValues("Content-Encoding")?.FirstOrDefault();
if (encoding != null && encoding.Equals("gzip", StringComparison.OrdinalIgnoreCase))
{
string encoding = response.Content.Headers.GetValues("Content-Encoding").FirstOrDefault();
if (string.Equals(encoding, "gzip", StringComparison.OrdinalIgnoreCase))
{
var readBytesTask = Task.Run(() => httpTask.Result.Content.ReadAsByteArrayAsync());
readBytesTask.Wait();
var readBytesTask = Task.Run(() => httpTask.Result.Content.ReadAsByteArrayAsync());
readBytesTask.Wait();

byte[] decompressionData = GzipDecompression.DecompressGzip(readBytesTask.Result);
return JsonConvert.DeserializeObject<TResult>(Encoding.UTF8.GetString(decompressionData));
}
var decompressionData = ArchiveHelper.UnpackGzip(readBytesTask.Result);
return JsonConvert.DeserializeObject<TResult>(Encoding.UTF8.GetString(decompressionData));
}


var readStringTask = Task.Run(() => httpTask.Result.Content.ReadAsStringAsync());
readStringTask.Wait();
return JsonConvert.DeserializeObject<TResult>(readStringTask.Result);


}
}
}
22 changes: 22 additions & 0 deletions WebDriverManager/Helpers/ArchiveHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.IO.Compression;
using System.IO;

namespace WebDriverManager.Helpers
{
internal static class ArchiveHelper
{
public static byte[] UnpackGzip(byte[] compressedData)
{
using (var compressedStream = new MemoryStream(compressedData))
using (var decompressedStream = new MemoryStream())
{
using (var gZipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
{
gZipStream.CopyTo(decompressedStream);
}

return decompressedStream.ToArray();
}
}
}
}
27 changes: 0 additions & 27 deletions WebDriverManager/Helpers/GzipDecompression.cs

This file was deleted.

0 comments on commit 5d66a17

Please sign in to comment.