forked from RPCS3/discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecompressedContent.cs
41 lines (35 loc) · 1.15 KB
/
DecompressedContent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace CompatApiClient.Compression;
public class DecompressedContent : HttpContent
{
private readonly HttpContent content;
private readonly ICompressor compressor;
public DecompressedContent(HttpContent content, ICompressor compressor)
{
this.content = content;
this.compressor = compressor;
RemoveHeaders();
}
protected override bool TryComputeLength(out long length)
{
length = -1;
return false;
}
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context)
{
var contentStream = await content.ReadAsStreamAsync().ConfigureAwait(false);
var decompressedLength = await compressor.DecompressAsync(contentStream, stream).ConfigureAwait(false);
Headers.ContentLength = decompressedLength;
}
private void RemoveHeaders()
{
foreach (var (key, value) in content.Headers)
Headers.TryAddWithoutValidation(key, value);
Headers.ContentEncoding.Clear();
Headers.ContentLength = null;
}
}