Skip to content

Commit e980fa1

Browse files
Mpdreamzrusscam
authored andcommitted
Fix HTTP compression for HttpConnection HttpClient implementation (#3921)
* GzipStream was wrapping completed task handler not the writable stream * Make sure HttpCompression is enabled randomly when running the integration tests * Remove adding Accept-Encoding twice This commit removes adding Accept-Encoding headers again, when constructing the requestMessage. These headers are added by the default HttpClientHandler implementation, with AutomaticDecompression. There is the possibility that a user overrides CreateHttpClientHandler() and does not set AutomaticDecompression, but does not override CreateRequestMessage(), and is relying on CreateHttpRequestMessage to add the Accept-Encoding headers when using HttpCompression. This commit will break that usage pattern, but I think this is acceptable to do, because we should optimize for the default behaviour i.e. we _could_ check that Accept-Encoding headers have been set when HttpCompression is enabled, inside CreateHttpRequestMessage(), and set them if not, but this is additional overhead to perform on every request. Instead, a user overriding CreateHttpClientHandler() and not setting AutomaticDecompression, and not overriding CreateRequestMessage(), when wishing to use HttpCompression, should set AutomaticDecompression inside their overridden implementation. Fixes #3913
1 parent fef8a00 commit e980fa1

File tree

9 files changed

+15
-12
lines changed

9 files changed

+15
-12
lines changed

src/Elasticsearch.Net/Connection/Content/RequestDataContent.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public RequestDataContent(RequestData requestData)
3737

3838
Task OnStreamAvailable(PostData data, Stream stream, HttpContent content, TransportContext context)
3939
{
40+
if (_requestData.HttpCompression)
41+
stream = new GZipStream(stream, CompressionMode.Compress, false);
4042
using(stream)
4143
data.Write(stream, requestData.ConnectionSettings);
4244
return Task.CompletedTask;
@@ -52,6 +54,8 @@ public RequestDataContent(RequestData requestData, CancellationToken token)
5254

5355
async Task OnStreamAvailable(PostData data, Stream stream, HttpContent content, TransportContext context)
5456
{
57+
if (_requestData.HttpCompression)
58+
stream = new GZipStream(stream, CompressionMode.Compress, false);
5559
using (stream)
5660
await data.WriteAsync(stream, requestData.ConnectionSettings, token).ConfigureAwait(false);
5761
}
@@ -75,8 +79,6 @@ protected override async Task SerializeToStreamAsync(Stream stream, TransportCon
7579

7680
var serializeToStreamTask = new TaskCompletionSource<bool>();
7781

78-
if (_requestData.HttpCompression)
79-
stream = new GZipStream(stream, CompressionMode.Compress, false);
8082
var wrappedStream = new CompleteTaskOnCloseStream(stream, serializeToStreamTask);
8183
await _onStreamAvailable(data, wrappedStream, this, context).ConfigureAwait(false);
8284
await serializeToStreamTask.Task.ConfigureAwait(false);

src/Elasticsearch.Net/Connection/HttpConnection.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,7 @@ protected virtual HttpRequestMessage CreateRequestMessage(RequestData requestDat
241241

242242
if (!requestData.RunAs.IsNullOrEmpty())
243243
requestMessage.Headers.Add(RequestData.RunAsSecurityHeader, requestData.RunAs);
244-
245-
if (requestData.HttpCompression)
246-
{
247-
requestMessage.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
248-
requestMessage.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
249-
}
250-
244+
251245
return requestMessage;
252246
}
253247

src/Tests/Tests.Configuration/EnvironmentConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public EnvironmentConfiguration(YamlConfiguration yamlConfiguration)
2828
{
2929
SourceSerializer = RandomBoolConfig("SOURCESERIALIZER", randomizer),
3030
TypedKeys = RandomBoolConfig("TYPEDKEYS", randomizer),
31+
HttpCompression = RandomBoolConfig("HTTPCOMPRESSION", randomizer),
3132
};
3233
}
3334

src/Tests/Tests.Configuration/TestConfigurationBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,8 @@ public class RandomConfiguration
6666

6767
/// <summary> Randomly enable typed keys on searches (defaults to true) on NEST search requests</summary>
6868
public bool TypedKeys { get; set; }
69+
70+
/// <summary> Randomly enable compression on the http requests</summary>
71+
public bool HttpCompression { get; set; }
6972
}
7073
}

src/Tests/Tests.Configuration/TestConfigurationExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static void DumpConfiguration(this TestConfigurationBase config)
2424
Console.WriteLine($" - Random:");
2525
Console.WriteLine($" \t- {nameof(config.Random.SourceSerializer)}: {config.Random.SourceSerializer}");
2626
Console.WriteLine($" \t- {nameof(config.Random.TypedKeys)}: {config.Random.TypedKeys}");
27+
Console.WriteLine($" \t- {nameof(config.Random.HttpCompression)}: {config.Random.HttpCompression}");
2728
Console.WriteLine(new string('-', 20));
2829
}
2930
}

src/Tests/Tests.Configuration/YamlConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public YamlConfiguration(string configurationFile)
3434
{
3535
SourceSerializer = RandomBool("source_serializer", randomizer),
3636
TypedKeys = RandomBool("typed_keys", randomizer),
37+
HttpCompression = RandomBool("http_compression", randomizer),
3738
};
3839
}
3940

src/Tests/Tests.Configuration/tests.default.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ test_against_already_running_elasticsearch: true
2020

2121
#random_source_serializer: true
2222
#random_old_connection: true
23-
seed: 74337
23+
#random_http_compresssion: true
24+
#seed: 74337
2425

2526
# Can be helpful to speed up tests runs as setting this to true only randomly tests a single overload of the api rather than all 4.
2627
# Can also help keep the noise down in case of test failures

src/Tests/Tests.Core/Client/Settings/TestConnectionSettings.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ public TestConnectionSettings(
4343
private void ApplyTestSettings() =>
4444
RerouteToProxyIfNeeded()
4545
.EnableDebugMode()
46-
//TODO make this random
47-
//.EnableHttpCompression()
46+
.EnableHttpCompression(TestConfiguration.Instance.Random.HttpCompression)
4847
#if DEBUG
4948
.EnableDebugMode()
5049
#endif

src/Tests/Tests.Core/Xunit/NestXunitRunOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ bool runningIntegrations
9696

9797
AppendExplictConfig(nameof(RandomConfiguration.SourceSerializer), sb);
9898
AppendExplictConfig(nameof(RandomConfiguration.TypedKeys), sb);
99+
AppendExplictConfig(nameof(RandomConfiguration.HttpCompression), sb);
99100

100101
if (runningIntegrations)
101102
sb.Append("integrate ")

0 commit comments

Comments
 (0)