in src/Elastic.Transport/Components/TransportClient/HttpRequestInvoker.cs [462:498]
private static async Task SetContentAsync(HttpRequestMessage message, BoundConfiguration boundConfiguration, PostData postData, CancellationToken cancellationToken)
{
if (boundConfiguration.TransferEncodingChunked)
message.Content = new BoundConfigurationContent(boundConfiguration, cancellationToken);
else
{
var stream = boundConfiguration.MemoryStreamFactory.Create();
if (boundConfiguration.HttpCompression)
{
using var zipStream = new GZipStream(stream, CompressionMode.Compress, true);
await postData.WriteAsync(zipStream, boundConfiguration.ConnectionSettings, boundConfiguration.DisableDirectStreaming, cancellationToken).ConfigureAwait(false);
}
else
await postData.WriteAsync(stream, boundConfiguration.ConnectionSettings, boundConfiguration.DisableDirectStreaming, cancellationToken).ConfigureAwait(false);
// the written bytes are uncompressed, so can only be used when http compression isn't used
if (boundConfiguration.DisableDirectStreaming && !boundConfiguration.HttpCompression)
{
message.Content = new ByteArrayContent(postData.WrittenBytes);
#if DOTNETCORE_2_1_OR_HIGHER
await stream.DisposeAsync().ConfigureAwait(false);
#else
stream.Dispose();
#endif
}
else
{
stream.Position = 0;
message.Content = new StreamContent(stream);
}
if (boundConfiguration.HttpCompression)
message.Content.Headers.ContentEncoding.Add("gzip");
message.Content.Headers.TryAddWithoutValidation("Content-Type", boundConfiguration.ContentType);
}
}