Skip to content

Commit 264785f

Browse files
committed
fix #953 gziped responses caused the HttpClient connection to not read the response stream, also added missing Accept-Encoding headers
1 parent dc4dbe7 commit 264785f

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

Diff for: src/Connections/Elasticsearch.Net.Connection.HttpClient/HttpClientConnection.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ public HttpClientConnection(IConnectionConfigurationValues settings, HttpClientH
3838

3939
var innerHandler = handler ?? new WebRequestHandler();
4040

41-
if (settings.EnableCompressedResponses)
42-
innerHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
43-
4441
if (innerHandler.SupportsProxy && !string.IsNullOrWhiteSpace(_settings.ProxyAddress))
4542
{
4643
innerHandler.Proxy = new WebProxy(_settings.ProxyAddress)
@@ -56,6 +53,12 @@ public HttpClientConnection(IConnectionConfigurationValues settings, HttpClientH
5653
{
5754
Timeout = TimeSpan.FromMilliseconds(_settings.Timeout)
5855
};
56+
if (settings.EnableCompressedResponses && innerHandler.SupportsAutomaticDecompression)
57+
{
58+
innerHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
59+
Client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
60+
Client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
61+
}
5962

6063
}
6164

@@ -126,7 +129,6 @@ public async Task<ElasticsearchResponse<Stream>> DoRequest(HttpMethod method, Ur
126129
else if (!string.IsNullOrWhiteSpace(DefaultContentType))
127130
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(DefaultContentType));
128131

129-
130132
if (!string.IsNullOrEmpty(uri.UserInfo))
131133
{
132134
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(uri.UserInfo)));
@@ -141,7 +143,7 @@ public async Task<ElasticsearchResponse<Stream>> DoRequest(HttpMethod method, Ur
141143

142144
var response = await Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
143145

144-
if (method == HttpMethod.Head || response.Content == null || !response.Content.Headers.ContentLength.HasValue || response.Content.Headers.ContentLength.Value <= 0)
146+
if (method == HttpMethod.Head || response.Content == null)
145147
{
146148
return ElasticsearchResponse<Stream>.Create(_settings, (int)response.StatusCode, method.ToString().ToLowerInvariant(), uri.ToString(), data);
147149
}

Diff for: src/Tests/Nest.Tests.Integration/ElasticsearchConfiguration.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static Uri CreateBaseUri(int? port = null)
3232
{
3333
var host = Host;
3434
if (port != 9500 && Process.GetProcessesByName("fiddler").HasAny())
35-
host = "localhost.fiddler";
35+
host = "ipv4.fiddler";
3636

3737
var uri = new UriBuilder("http", host, port.GetValueOrDefault(9200)).Uri;
3838
return uri;

Diff for: src/Tests/Nest.Tests.Integration/Nest.Tests.Integration.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
</Reference>
6767
<Reference Include="System" />
6868
<Reference Include="System.Core" />
69+
<Reference Include="System.Net.Http" />
6970
<Reference Include="System.Runtime.Serialization" />
7071
<Reference Include="System.Xml.Linq" />
7172
<Reference Include="System.Data.DataSetExtensions" />
@@ -175,6 +176,7 @@
175176
<Compile Include="Indices\StatsTests.cs" />
176177
<Compile Include="Core\AsyncTests.cs" />
177178
<Compile Include="Mapping\MappingVisitorTests.cs" />
179+
<Compile Include="Reproduce\Reproduce953Tests.cs" />
178180
<Compile Include="Reproduce\Reproduce945Tests.cs" />
179181
<Compile Include="Reproduce\Reproduce769Tests.cs" />
180182
<Compile Include="Search\Filter\AndOrNotFilterTests.cs" />
@@ -249,6 +251,10 @@
249251
<Compile Include="Warmers\WarmersTests.cs" />
250252
</ItemGroup>
251253
<ItemGroup>
254+
<ProjectReference Include="..\..\Connections\Elasticsearch.Net.Connection.HttpClient\Elasticsearch.Net.Connection.HttpClient.csproj">
255+
<Project>{a69322fd-b874-44ef-abe0-63f4a7b5593e}</Project>
256+
<Name>Elasticsearch.Net.Connection.HttpClient</Name>
257+
</ProjectReference>
252258
<ProjectReference Include="..\..\Elasticsearch.Net\Elasticsearch.Net.csproj">
253259
<Project>{e97ccf40-0ba6-43fe-9f2d-58d454134088}</Project>
254260
<Name>Elasticsearch.Net</Name>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Text;
2+
using Elasticsearch.Net.Connection;
3+
using Elasticsearch.Net.ConnectionPool;
4+
using FluentAssertions;
5+
using Nest.Tests.Integration;
6+
using Nest.Tests.MockData;
7+
using Nest.Tests.MockData.Domain;
8+
using NUnit.Framework;
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Linq;
12+
13+
namespace Nest.Tests.Integration.Reproduce
14+
{
15+
[TestFixture]
16+
public class Reproduce953Tests
17+
{
18+
[Test]
19+
public void Calling_Refresh_UsingHttpClientConnection_DoesNotThrow()
20+
{
21+
22+
var settings = ElasticsearchConfiguration.Settings()
23+
.EnableCompressedResponses(true);
24+
var connection = new HttpClientConnection(settings);
25+
var client = new ElasticClient(settings, connection: connection);
26+
27+
Assert.DoesNotThrow(()=> client.Refresh());
28+
Assert.DoesNotThrow(()=> client.Get<ElasticsearchProject>(NestTestData.Data.First().Id));
29+
Assert.DoesNotThrow(()=> client.Ping());
30+
31+
}
32+
33+
34+
}
35+
}

0 commit comments

Comments
 (0)