From 8121ba2e3e370cfdd704584c3bbd516faf91d898 Mon Sep 17 00:00:00 2001 From: David Leek Date: Thu, 7 Dec 2023 09:03:59 +0100 Subject: [PATCH] fix: use stringcontent for metrics and register so we can set content length header and play nice with edge --- src/Unleash/Communication/UnleashApiClient.cs | 59 ++++++++++--------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/src/Unleash/Communication/UnleashApiClient.cs b/src/Unleash/Communication/UnleashApiClient.cs index 59082a96..abe0a63d 100644 --- a/src/Unleash/Communication/UnleashApiClient.cs +++ b/src/Unleash/Communication/UnleashApiClient.cs @@ -183,25 +183,27 @@ public async Task RegisterClient(ClientRegistration registration, Cancella var memoryStream = new MemoryStream(); jsonSerializer.Serialize(memoryStream, registration); - const int bufferSize = 1024 * 4; - using (var request = new HttpRequestMessage(HttpMethod.Post, requestUri)) { - request.Content = new StreamContent(memoryStream, bufferSize); - request.Content.Headers.AddContentTypeJson(); - - SetRequestHeaders(request, clientRequestHeaders); - - using (var response = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false)) + using (var streamReader = new StreamReader(memoryStream)) { - if (response.IsSuccessStatusCode) - return true; + var str_content = streamReader.ReadToEnd(); + var content = new StringContent(str_content, System.Text.Encoding.UTF8, "application/json"); + request.Content = content; + request.Content.Headers.Add(@"Content-Length", str_content.Length.ToString()); + SetRequestHeaders(request, clientRequestHeaders); + + using (var response = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false)) + { + if (response.IsSuccessStatusCode) + return true; - var error = await response.Content.ReadAsStringAsync().ConfigureAwait(false); - Logger.Trace($"UNLEASH: Error {response.StatusCode} from request '{requestUri}' in '{nameof(UnleashApiClient)}': " + error); - eventConfig?.RaiseError(new ErrorEvent() { Resource = requestUri, ErrorType = ErrorType.Client, StatusCode = response.StatusCode }); + var error = await response.Content.ReadAsStringAsync().ConfigureAwait(false); + Logger.Trace($"UNLEASH: Error {response.StatusCode} from request '{requestUri}' in '{nameof(UnleashApiClient)}': " + error); + eventConfig?.RaiseError(new ErrorEvent() { Resource = requestUri, ErrorType = ErrorType.Client, StatusCode = response.StatusCode }); - return false; + return false; + } } } } @@ -230,25 +232,28 @@ public async Task SendMetrics(ThreadSafeMetricsBucket metrics, Cancellatio }); } - const int bufferSize = 1024 * 4; - using (var request = new HttpRequestMessage(HttpMethod.Post, requestUri)) { - request.Content = new StreamContent(memoryStream, bufferSize); - request.Content.Headers.AddContentTypeJson(); + using (var streamReader = new StreamReader(memoryStream)) + { + var str_content = streamReader.ReadToEnd(); + var content = new StringContent(str_content, System.Text.Encoding.UTF8, "application/json"); + request.Content = content; + request.Content.Headers.Add(@"Content-Length", str_content.Length.ToString()); - SetRequestHeaders(request, clientRequestHeaders); + SetRequestHeaders(request, clientRequestHeaders); - using (var response = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false)) - { - if (response.IsSuccessStatusCode || response.StatusCode == HttpStatusCode.NotModified) + using (var response = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false)) { - HandleMetricsSuccessResponse(response); - return true; + if (response.IsSuccessStatusCode || response.StatusCode == HttpStatusCode.NotModified) + { + HandleMetricsSuccessResponse(response); + return true; + } + + await HandleMetricsErrorResponse(response, requestUri); + return false; } - - await HandleMetricsErrorResponse(response, requestUri); - return false; } } }