Skip to content

Commit 856ad81

Browse files
committed
fixed error responses that exceeded 65kb being truncated (sigh WebRequest) causing ElasticsearchServerException to throw a nullreference and .ServerError to always be null
1 parent 502975b commit 856ad81

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/Elasticsearch.Net/Connection/HttpConnection.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ static HttpConnection()
2828
ServicePointManager.UseNagleAlgorithm = false;
2929
ServicePointManager.Expect100Continue = false;
3030
ServicePointManager.DefaultConnectionLimit = 10000;
31+
ServicePointManager.SetTcpKeepAlive(true, 2000, 2000);
32+
33+
//WebException's GetResponse is limitted to 65kb by default.
34+
//Elasticsearch can be alot more chatty then that when dumping exceptions
35+
//On error responses, so lets up the ante.
36+
HttpWebRequest.DefaultMaximumErrorResponseLength = int.MaxValue;
3137
}
3238

3339
public HttpConnection(IConnectionConfigurationValues settings)
@@ -173,6 +179,8 @@ protected virtual HttpWebRequest CreateWebRequest(Uri uri, string method, byte[]
173179
var myReq = (HttpWebRequest)WebRequest.Create(uri);
174180
myReq.Accept = "application/json";
175181
myReq.ContentType = "application/json";
182+
myReq.MaximumResponseHeadersLength = -1;
183+
//myReq.AllowWriteStreamBuffering = false;
176184
if (this.ConnectionSettings.EnableCompressedResponses)
177185
{
178186
myReq.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
@@ -232,6 +240,8 @@ private ElasticsearchResponse<Stream> HandleWebException(byte[] data, WebExcepti
232240
var httpEx = webException.Response as HttpWebResponse;
233241
if (httpEx != null)
234242
{
243+
//StreamReader ms = new StreamReader(httpEx.GetResponseStream());
244+
//var response = ms.ReadToEnd();
235245
cs = WebToElasticsearchResponse(data, httpEx.GetResponseStream(), httpEx, method, path);
236246
return cs;
237247
}

src/Tests/Nest.Tests.Integration/Exceptions/ElasticsearchExceptionTests.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,44 @@ public void ConnectionException_WithThrowingClient_Async()
4848
.ThrowOnElasticsearchServerExceptions());
4949
Assert.Throws<WebException>(async () => await client.RootNodeInfoAsync());
5050
}
51+
52+
[Test]
53+
public async void ServerError_Is_Set_ClientThat_DoesNotThow_AndDoesNotExposeRawResponse_Async()
54+
{
55+
var uri = ElasticsearchConfiguration.CreateBaseUri();
56+
var client = new ElasticClient(new ConnectionSettings(uri).ExposeRawResponse(false));
57+
Assert.DoesNotThrow(async () =>
58+
{
59+
var result = await client.SearchAsync<ElasticsearchProject>(s => s.QueryRaw(@"{ ""badjson"": {} }"));
60+
result.IsValid.Should().BeFalse();
61+
result.ConnectionStatus.HttpStatusCode.Should().Be(400);
62+
var e = result.ServerError;
63+
e.Should().NotBeNull();
64+
e.ExceptionType.Should().Contain("SearchPhaseExecutionException");
65+
});
66+
}
67+
68+
[Test]
69+
public void ServerError_Is_Set_ClientThat_DoesNotThow_AndDoesNotExposeRawResponse()
70+
{
71+
var uri = ElasticsearchConfiguration.CreateBaseUri();
72+
var client = new ElasticClient(new ConnectionSettings(uri).ExposeRawResponse(false));
73+
Assert.DoesNotThrow(() =>
74+
{
75+
var result = client.Search<ElasticsearchProject>(s => s.QueryRaw(@"{ ""badjson"": {} }"));
76+
result.IsValid.Should().BeFalse();
77+
result.ConnectionStatus.HttpStatusCode.Should().Be(400);
78+
var e = result.ServerError;
79+
e.Should().NotBeNull();
80+
e.ExceptionType.Should().Contain("SearchPhaseExecutionException");
81+
});
82+
}
5183

5284
[Test]
5385
public void ServerError_Is_Set_ClientThat_DoesNotThow()
5486
{
5587
var uri = ElasticsearchConfiguration.CreateBaseUri();
56-
var client = new ElasticClient(new ConnectionSettings(uri));
88+
var client = new ElasticClient(new ConnectionSettings(uri).ExposeRawResponse(true));
5789
Assert.DoesNotThrow(() =>
5890
{
5991
var result = client.Search<ElasticsearchProject>(s => s.QueryRaw(@"{ ""badjson"": {} }"));

0 commit comments

Comments
 (0)