Skip to content

Commit

Permalink
Merge pull request #1428 from Badgerati/Issue-1426
Browse files Browse the repository at this point in the history
Migrates Stream functions into the .NET Listener
  • Loading branch information
Badgerati authored Oct 23, 2024
2 parents 79ec468 + c948542 commit 0b08d67
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 312 deletions.
8 changes: 8 additions & 0 deletions src/Listener/PodeCompressionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Pode
{
public enum PodeCompressionType
{
Gzip,
Deflate
}
}
92 changes: 92 additions & 0 deletions src/Listener/PodeHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Runtime.Versioning;
using System.Threading.Tasks;
using System.Threading;
using System.Text;
using System.IO.Compression;

namespace Pode
{
Expand Down Expand Up @@ -212,5 +214,95 @@ public static List<T> Subset<T>(List<T> list, int startIndex, int endIndex)
{
return Subset(list.ToArray(), startIndex, endIndex).ToList<T>();
}

public static byte[] ConvertStreamToBytes(Stream stream)
{
// we need to copy the stream to a memory stream and then return the bytes
using (var memory = new MemoryStream())
{
stream.CopyTo(memory);
return memory.ToArray();
}
}

public static string ConvertBytesToString(byte[] bytes, bool removeNewLines = false)
{
// return empty string if no bytes
if (bytes == default(byte[]) || bytes.Length == 0)
{
return string.Empty;
}

// convert the bytes to a string
var str = Encoding.UTF8.GetString(bytes);

// remove new lines if needed
if (removeNewLines)
{
return str.Trim(NEW_LINE_ARRAY);
}

return str;
}

public static string ReadStreamToEnd(Stream stream, Encoding encoding = default)
{
// return empty string if no stream
if (stream == default(Stream))
{
return string.Empty;
}

// set the encoding if not provided
if (encoding == default(Encoding))
{
encoding = Encoding.UTF8;
}

// read the stream to the end
using (var reader = new StreamReader(stream, encoding))
{
return reader.ReadToEnd();
}
}

// decompress bytes into either a gzip or deflate stream, and return the string
public static string DecompressBytes(byte[] bytes, PodeCompressionType type, Encoding encoding = default)
{
var stream = CompressStream(new MemoryStream(bytes), type, CompressionMode.Decompress);
return ReadStreamToEnd(stream, encoding);
}

// compress bytes into either a gzip or deflate stream, and return the bytes
public static byte[] CompressBytes(byte[] bytes, PodeCompressionType type)
{
var ms = new MemoryStream();

using (var stream = CompressStream(ms, type, CompressionMode.Compress))
{
stream.Write(bytes, 0, bytes.Length);
}

ms.Position = 0;
return ms.ToArray();
}

// compress stream into either a gzip or deflate stream
public static Stream CompressStream(Stream stream, PodeCompressionType type, CompressionMode mode)
{
var leaveOpen = mode == CompressionMode.Compress;

switch (type)
{
case PodeCompressionType.Gzip:
return new GZipStream(stream, mode, leaveOpen);

case PodeCompressionType.Deflate:
return new DeflateStream(stream, mode, leaveOpen);

default:
return stream;
}
}
}
}
9 changes: 1 addition & 8 deletions src/Private/Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1535,14 +1535,7 @@ function ConvertFrom-PodeRequestContent {
else {
# if the request is compressed, attempt to uncompress it
if (![string]::IsNullOrWhiteSpace($TransferEncoding)) {
# create a compressed stream to decompress the req bytes
$ms = [System.IO.MemoryStream]::new()
$ms.Write($Request.RawBody, 0, $Request.RawBody.Length)
$null = $ms.Seek(0, 0)
$stream = Get-PodeCompressionStream -InputStream $ms -Encoding $TransferEncoding -Mode Decompress

# read the decompressed bytes
$Content = Read-PodeStreamToEnd -Stream $stream -Encoding $Request.ContentEncoding
$Content = [PodeHelpers]::DecompressBytes($Request.RawBody, $TransferEncoding, $Request.ContentEncoding)
}
else {
$Content = $Request.Body
Expand Down
Loading

0 comments on commit 0b08d67

Please sign in to comment.