Skip to content

Commit 2166c65

Browse files
committed
fix #947 add support for compressed responses and basic auth on the alternative http connection
1 parent 7e504eb commit 2166c65

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

src/Connections/Elasticsearch.Net.Connection.HttpClient/HttpClientConnection.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Net;
44
using System.Net.Http;
55
using System.Net.Http.Headers;
6+
using System.Text;
67
using System.Threading.Tasks;
78
using Elasticsearch.Net.Connection.Configuration;
89

@@ -37,10 +38,14 @@ public HttpClientConnection(IConnectionConfigurationValues settings, HttpClientH
3738

3839
var innerHandler = handler ?? new WebRequestHandler();
3940

41+
if (settings.EnableCompressedResponses)
42+
innerHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
43+
4044
if (innerHandler.SupportsProxy && !string.IsNullOrWhiteSpace(_settings.ProxyAddress))
4145
{
4246
innerHandler.Proxy = new WebProxy(_settings.ProxyAddress)
4347
{
48+
4449
Credentials = new NetworkCredential(_settings.ProxyUsername, _settings.ProxyPassword),
4550
};
4651

@@ -51,6 +56,7 @@ public HttpClientConnection(IConnectionConfigurationValues settings, HttpClientH
5156
{
5257
Timeout = TimeSpan.FromMilliseconds(_settings.Timeout)
5358
};
59+
5460
}
5561

5662
/// <summary>
@@ -115,24 +121,22 @@ public async Task<ElasticsearchResponse<Stream>> DoRequest(HttpMethod method, Ur
115121
try
116122
{
117123
var request = new HttpRequestMessage(method, uri);
124+
if (requestSpecificConfig != null && !string.IsNullOrWhiteSpace(requestSpecificConfig.ContentType))
125+
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(requestSpecificConfig.ContentType));
126+
else if (!string.IsNullOrWhiteSpace(DefaultContentType))
127+
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(DefaultContentType));
128+
129+
if (!string.IsNullOrWhiteSpace(DefaultContentType))
130+
request.Content.Headers.ContentType = new MediaTypeHeaderValue(DefaultContentType);
131+
132+
if (!string.IsNullOrEmpty(uri.UserInfo))
133+
{
134+
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(uri.UserInfo)));
135+
}
118136

119137
if (method != HttpMethod.Get && method != HttpMethod.Head && data != null && data.Length > 0)
120138
{
121139
request.Content = new ByteArrayContent(data);
122-
123-
if (requestSpecificConfig != null && !string.IsNullOrWhiteSpace(requestSpecificConfig.ContentType))
124-
{
125-
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(requestSpecificConfig.ContentType));
126-
}
127-
else if (!string.IsNullOrWhiteSpace(DefaultContentType))
128-
{
129-
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(DefaultContentType));
130-
}
131-
132-
if (!string.IsNullOrWhiteSpace(DefaultContentType))
133-
{
134-
request.Content.Headers.ContentType = new MediaTypeHeaderValue(DefaultContentType);
135-
}
136140
}
137141

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

src/Elasticsearch.Net/Connection/HttpConnection.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private static void ThreadTimeoutCallback(object state, bool timedOut)
141141
protected virtual HttpWebRequest CreateHttpWebRequest(Uri uri, string method, byte[] data, IRequestConfiguration requestSpecificConfig)
142142
{
143143
var myReq = this.CreateWebRequest(uri, method, data, requestSpecificConfig);
144-
this.SetBasicAuthorizationIfNeeded(myReq);
144+
this.SetBasicAuthorizationIfNeeded(uri, myReq);
145145
this.SetProxyIfNeeded(myReq);
146146
return myReq;
147147
}
@@ -164,15 +164,12 @@ private void SetProxyIfNeeded(HttpWebRequest myReq)
164164
}
165165
}
166166

167-
private void SetBasicAuthorizationIfNeeded(HttpWebRequest myReq)
167+
private void SetBasicAuthorizationIfNeeded(Uri uri, HttpWebRequest myReq)
168168
{
169-
//TODO figure out a way to cache this;
170-
171-
//if (this._ConnectionSettings.UriSpecifiedBasicAuth)
172-
//{
173-
myReq.Headers["Authorization"] =
174-
"Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(myReq.RequestUri.UserInfo));
175-
//}
169+
if (!uri.UserInfo.IsNullOrEmpty())
170+
{
171+
myReq.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(myReq.RequestUri.UserInfo));
172+
}
176173
}
177174

178175
protected virtual HttpWebRequest CreateWebRequest(Uri uri, string method, byte[] data, IRequestConfiguration requestSpecificConfig)

src/Tests/Elasticsearch.Net.Tests.Unit/Elasticsearch.Net.Tests.Unit.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<Compile Include="Connection\NoRetryOnServerExceptionTests.cs" />
6565
<Compile Include="Connection\ElasticsearchServerExceptions.cs" />
6666
<Compile Include="Connection\NoRetryTests.cs" />
67+
<Compile Include="Connection\SingleNodeConnectionPoolTests.cs" />
6768
<Compile Include="Stubs\FakeCalls.cs" />
6869
<Compile Include="Stubs\FakeResponse.cs" />
6970
<Compile Include="Connection\SkipDeadNodesTests.cs" />

0 commit comments

Comments
 (0)