Skip to content

Commit

Permalink
fix: use stringcontent for metrics and register so we can set content…
Browse files Browse the repository at this point in the history
… length header and play nice with edge
  • Loading branch information
daveleek committed Dec 7, 2023
1 parent 50d1cf0 commit 8121ba2
Showing 1 changed file with 32 additions and 27 deletions.
59 changes: 32 additions & 27 deletions src/Unleash/Communication/UnleashApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,25 +183,27 @@ public async Task<bool> 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;
}
}
}
}
Expand Down Expand Up @@ -230,25 +232,28 @@ public async Task<bool> 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;
}
}
}
Expand Down

0 comments on commit 8121ba2

Please sign in to comment.