Background and Motivation
There is currently no support for HTTP request compression. Response decompression support has been present and maintained for many years now, but request compression has been oddly absent. Some web servers support request compression and this would be useful when sending large objects over slow connections. Adding opt-in request compression would be useful for programmers looking to optimize their network communication code in certain situations.
Proposed API (updated by @iremyux)
namespace System.Net.Http
{
public enum CompressionMethod
{
GZip = 0x1,
Deflate = 0x2,
Brotli = 0x4,
Zstandard = 0x8
}
public sealed partial class CompressedContent : System.Net.Http.HttpContent
{
public CompressedContent(System.Net.Http.HttpContent content, CompressionMethod compressionMethod) { }
public CompressedContent(System.Net.Http.HttpContent content, CompressionMethod compressionMethod, System.IO.Compression.CompressionLevel compressionLevel) { }
}
}
Usage Examples (updated by @iremyux)
var request = new HttpRequestMessage(RequestMethod, URI);
{
Content = new CompressedContent(json, CompressionMethod.Brotli)
};
var response = await httpClient.SendAsync(request);
var content = new StreamContent(largeFileStream);
request.Content = new CompressedContent(content, CompressionMethod.GZip, CompressionLevel.SmallestSize);
Alternative Designs (updated by @iremyux)
- The underlying compression algorithms offer other parameters beyond
CompressionLevel : a leaveOpen flag which is not meaningful in this case and an algorithm-specific options object ( ZLibCompressionOptions , BrotliCompressionOptions , ZstandardCompressionOptions ). We decided the advanced per-algorithm tuning can be discussed later if there is demand for it.
- We considered numbering
CompressionMethod sequentially. This was rejected in favor of aligning the values with the existing DecompressionMethods flags so the two compression-related enums share a consistent, memorable mapping for the same algorithms.
- A
CompressionHandler (similar to the existing DecompressionHandler) that automatically compresses requests and handles server negotiation was discussed.
Risks
- 415 Unsupported Media Type: Servers that don't support compressed requests will reject them. Users must handle this themselves.
Background and Motivation
There is currently no support for HTTP request compression. Response decompression support has been present and maintained for many years now, but request compression has been oddly absent. Some web servers support request compression and this would be useful when sending large objects over slow connections. Adding opt-in request compression would be useful for programmers looking to optimize their network communication code in certain situations.
Proposed API (updated by @iremyux)
Usage Examples (updated by @iremyux)
Alternative Designs (updated by @iremyux)
CompressionLevel: aleaveOpenflag which is not meaningful in this case and an algorithm-specific options object (ZLibCompressionOptions,BrotliCompressionOptions,ZstandardCompressionOptions). We decided the advanced per-algorithm tuning can be discussed later if there is demand for it.CompressionMethodsequentially. This was rejected in favor of aligning the values with the existingDecompressionMethodsflags so the two compression-related enums share a consistent, memorable mapping for the same algorithms.CompressionHandler(similar to the existingDecompressionHandler) that automatically compresses requests and handles server negotiation was discussed.Risks