Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/Elasticsearch.Net/Connection/HttpConnection-CoreFx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public class HttpConnection : IConnection
protected readonly ConcurrentDictionary<int, HttpClient> Clients = new ConcurrentDictionary<int, HttpClient>();

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 =>
Expand All @@ -64,8 +64,6 @@ private HttpClient GetClient(RequestData requestData)

public virtual ElasticsearchResponse<TReturn> Request<TReturn>(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);

Expand All @@ -78,10 +76,11 @@ public virtual ElasticsearchResponse<TReturn> Request<TReturn>(RequestData reque
responseMessage = client.SendAsync(requestMessage).GetAwaiter().GetResult();
requestData.MadeItToResponse = true;
builder.StatusCode = (int) responseMessage.StatusCode;
IEnumerable<string> 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
Expand All @@ -107,8 +106,6 @@ public virtual ElasticsearchResponse<TReturn> Request<TReturn>(RequestData reque
public virtual async Task<ElasticsearchResponse<TReturn>> RequestAsync<TReturn>(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);

Expand All @@ -121,10 +118,11 @@ public virtual async Task<ElasticsearchResponse<TReturn>> RequestAsync<TReturn>(
responseMessage = await client.SendAsync(requestMessage, cancellationToken).ConfigureAwait(false);
requestData.MadeItToResponse = true;
builder.StatusCode = (int) responseMessage.StatusCode;
IEnumerable<string> 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
Expand Down Expand Up @@ -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]);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Elasticsearch.Net/Responses/ElasticsearchResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public class ElasticsearchResponse<T> : IApiCallDetails

public IEnumerable<string> DeprecationWarnings { get; internal set; } = Enumerable.Empty<string>();

public string ResponseMimeType { get; internal set; }

internal bool AllowAllStatusCodes { get; }

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Elasticsearch.Net/Transport/Pipeline/ResponseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ResponseBuilder<TReturn>
public Exception Exception { get; set; }
public int? StatusCode { get; set; }
public Stream Stream { get; set; }
public string ResponseMimeType { get; set; } = RequestData.MimeType;

public IEnumerable<string> DeprecationWarnings { get; set; }

Expand Down Expand Up @@ -61,6 +62,7 @@ private ElasticsearchResponse<TReturn> Initialize(int? statusCode, Exception exc
response.HttpMethod = this._requestData.Method;
response.OriginalException = exception;
response.DeprecationWarnings = this.DeprecationWarnings ?? Enumerable.Empty<string>();
response.ResponseMimeType = this.ResponseMimeType;
return response;
}

Expand Down