diff --git a/src/Elasticsearch.Net/Connection/HttpConnection-CoreFx.cs b/src/Elasticsearch.Net/Connection/HttpConnection-CoreFx.cs index 86e79c7690b..715fe783468 100644 --- a/src/Elasticsearch.Net/Connection/HttpConnection-CoreFx.cs +++ b/src/Elasticsearch.Net/Connection/HttpConnection-CoreFx.cs @@ -36,14 +36,14 @@ public class HttpConnection : IConnection protected readonly ConcurrentDictionary Clients = new ConcurrentDictionary(); private static readonly string CanNotUseStreamResponsesWithCurlHandler = - "Using Stream as TReturn does not work as expected on .NET core linux, because we can no longer guarantee this works it will be removed from the client in our 6.0 release" + "Using Stream as TReturn does not work as expected on .NET core linux. " + + "Because we can no longer guarantee this works it will be removed from the client in our 6.0 release" ; private HttpClient GetClient(RequestData requestData) { var key = GetClientKey(requestData); - HttpClient client; - if (this.Clients.TryGetValue(key, out client)) return client; + if (this.Clients.TryGetValue(key, out var client)) return client; lock (_lock) { client = this.Clients.GetOrAdd(key, h => @@ -64,8 +64,6 @@ private HttpClient GetClient(RequestData requestData) public virtual ElasticsearchResponse Request(RequestData requestData) where TReturn : class { - //TODO remove Stream response support in 6.0, closing the stream is sufficient on desktop/mono - //but not on .NET core on linux HttpClient which proxies to curl. if (typeof(TReturn) == typeof(Stream) && ConnectionConfiguration.IsCurlHandler) throw new Exception(CanNotUseStreamResponsesWithCurlHandler); @@ -78,10 +76,11 @@ public virtual ElasticsearchResponse Request(RequestData reque responseMessage = client.SendAsync(requestMessage).GetAwaiter().GetResult(); requestData.MadeItToResponse = true; builder.StatusCode = (int) responseMessage.StatusCode; - IEnumerable warnings; - if (responseMessage.Headers.TryGetValues("Warning", out warnings)) + if (responseMessage.Headers.TryGetValues("Warning", out var warnings)) builder.DeprecationWarnings = warnings; + builder.ResponseMimeType = responseMessage.Content.Headers.ContentType?.MediaType; + if (responseMessage.Content != null) builder.Stream = responseMessage.Content.ReadAsStreamAsync().GetAwaiter().GetResult(); // https://github.com/elastic/elasticsearch-net/issues/2311 @@ -107,8 +106,6 @@ public virtual ElasticsearchResponse Request(RequestData reque public virtual async Task> RequestAsync(RequestData requestData, CancellationToken cancellationToken) where TReturn : class { - //TODO remove Stream response support in 6.0, closing the stream is sufficient on desktop/mono - //but not on .NET core on linux HttpClient which proxies to curl. if (typeof(TReturn) == typeof(Stream) && ConnectionConfiguration.IsCurlHandler) throw new Exception(CanNotUseStreamResponsesWithCurlHandler); @@ -121,10 +118,11 @@ public virtual async Task> RequestAsync( responseMessage = await client.SendAsync(requestMessage, cancellationToken).ConfigureAwait(false); requestData.MadeItToResponse = true; builder.StatusCode = (int) responseMessage.StatusCode; - IEnumerable warnings; - if (responseMessage.Headers.TryGetValues("Warning", out warnings)) + if (responseMessage.Headers.TryGetValues("Warning", out var warnings)) builder.DeprecationWarnings = warnings; + builder.ResponseMimeType = responseMessage.Content.Headers.ContentType?.MediaType; + if (responseMessage.Content != null) builder.Stream = await responseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false); // https://github.com/elastic/elasticsearch-net/issues/2311 @@ -250,7 +248,7 @@ protected virtual HttpRequestMessage CreateRequestMessage(RequestData requestDat else { // Set content in order to set a Content-Type header. - // Content gets diposed so can't be shared instance + // Content gets disposed so can't be shared instance requestMessage.Content = new ByteArrayContent(new byte[0]); } diff --git a/src/Elasticsearch.Net/Responses/ElasticsearchResponse.cs b/src/Elasticsearch.Net/Responses/ElasticsearchResponse.cs index 9236cfece64..66b20c06501 100644 --- a/src/Elasticsearch.Net/Responses/ElasticsearchResponse.cs +++ b/src/Elasticsearch.Net/Responses/ElasticsearchResponse.cs @@ -75,6 +75,8 @@ public class ElasticsearchResponse : IApiCallDetails public IEnumerable DeprecationWarnings { get; internal set; } = Enumerable.Empty(); + public string ResponseMimeType { get; internal set; } + internal bool AllowAllStatusCodes { get; } /// diff --git a/src/Elasticsearch.Net/Transport/Pipeline/ResponseBuilder.cs b/src/Elasticsearch.Net/Transport/Pipeline/ResponseBuilder.cs index 0b10b4c3780..1b6d545b5b1 100644 --- a/src/Elasticsearch.Net/Transport/Pipeline/ResponseBuilder.cs +++ b/src/Elasticsearch.Net/Transport/Pipeline/ResponseBuilder.cs @@ -22,6 +22,7 @@ public class ResponseBuilder public Exception Exception { get; set; } public int? StatusCode { get; set; } public Stream Stream { get; set; } + public string ResponseMimeType { get; set; } = RequestData.MimeType; public IEnumerable DeprecationWarnings { get; set; } @@ -61,6 +62,7 @@ private ElasticsearchResponse Initialize(int? statusCode, Exception exc response.HttpMethod = this._requestData.Method; response.OriginalException = exception; response.DeprecationWarnings = this.DeprecationWarnings ?? Enumerable.Empty(); + response.ResponseMimeType = this.ResponseMimeType; return response; }