mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-18 14:39:45 +02:00
Add support for GZip and Deflate (#439)
* gzip - wip * wip * tests * fix gzip and deflate * CheckIfShouldKillVBCSCompiler * DisableRequestBodyDecompressing
This commit is contained in:
49
src/WireMock.Net/Util/CompressionUtils.cs
Normal file
49
src/WireMock.Net/Util/CompressionUtils.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
|
||||
namespace WireMock.Util
|
||||
{
|
||||
internal static class CompressionUtils
|
||||
{
|
||||
public static byte[] Compress(string contentEncoding, byte[] data)
|
||||
{
|
||||
using (var compressedStream = new MemoryStream())
|
||||
using (var zipStream = Create(contentEncoding, compressedStream, CompressionMode.Compress))
|
||||
{
|
||||
zipStream.Write(data, 0, data.Length);
|
||||
|
||||
#if !NETSTANDARD1_3
|
||||
zipStream.Close();
|
||||
#endif
|
||||
return compressedStream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] Decompress(string contentEncoding, byte[] data)
|
||||
{
|
||||
using (var compressedStream = new MemoryStream(data))
|
||||
using (var zipStream = Create(contentEncoding, compressedStream, CompressionMode.Decompress))
|
||||
using (var resultStream = new MemoryStream())
|
||||
{
|
||||
zipStream.CopyTo(resultStream);
|
||||
return resultStream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
private static Stream Create(string contentEncoding, Stream stream, CompressionMode mode)
|
||||
{
|
||||
switch (contentEncoding)
|
||||
{
|
||||
case "gzip":
|
||||
return new GZipStream(stream, mode);
|
||||
|
||||
case "deflate":
|
||||
return new DeflateStream(stream, mode);
|
||||
|
||||
default:
|
||||
throw new NotSupportedException($"ContentEncoding '{contentEncoding}' is not supported.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user