From 1446ec38d4ff9fca2f570df1f5706c19613999a3 Mon Sep 17 00:00:00 2001 From: Greg Pakes Date: Mon, 7 May 2018 17:32:49 +1000 Subject: [PATCH 1/4] Added support for request body in projects with owin --- .../RaygunWebApiClient.cs | 10 +++------- .../RaygunWebApiDelegatingHandler.cs | 20 ++++++------------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/Mindscape.Raygun4Net.WebApi/RaygunWebApiClient.cs b/Mindscape.Raygun4Net.WebApi/RaygunWebApiClient.cs index 4d9362cf..bf396c54 100644 --- a/Mindscape.Raygun4Net.WebApi/RaygunWebApiClient.cs +++ b/Mindscape.Raygun4Net.WebApi/RaygunWebApiClient.cs @@ -132,13 +132,9 @@ private static void AttachInternal(HttpConfiguration config, Func a.FullName.StartsWith("Owin")); - if (owinAssembly == null) - { - config.MessageHandlers.Add(new RaygunWebApiDelegatingHandler()); - } - + + config.MessageHandlers.Add(new RaygunWebApiDelegatingHandler()); + var clientCreator = new RaygunWebApiClientProvider(generateRaygunClientWithMessage, applicationVersion); config.Services.Add(typeof(IExceptionLogger), new RaygunWebApiExceptionLogger(clientCreator)); diff --git a/Mindscape.Raygun4Net.WebApi/RaygunWebApiDelegatingHandler.cs b/Mindscape.Raygun4Net.WebApi/RaygunWebApiDelegatingHandler.cs index 6e4f9378..a889c2a6 100644 --- a/Mindscape.Raygun4Net.WebApi/RaygunWebApiDelegatingHandler.cs +++ b/Mindscape.Raygun4Net.WebApi/RaygunWebApiDelegatingHandler.cs @@ -1,4 +1,6 @@ using System.ComponentModel; +using System.Globalization; +using System.IO; using System.Linq; using System.Net.Http; using System.Text; @@ -11,21 +13,11 @@ public class RaygunWebApiDelegatingHandler : DelegatingHandler protected override async System.Threading.Tasks.Task SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { - var stream = await request.Content.ReadAsStreamAsync(); - if (stream != null && stream.CanSeek) + // ReadAsStringAsync is always readable as it calls LoadIntoBufferAsync internally. + var body = await request.Content.ReadAsStringAsync(); + if (!string.IsNullOrEmpty(body)) { - var lengthToRead = (int)(stream.Length < 4096 ? stream.Length : 4096); - var buffer = new byte[lengthToRead]; - - await stream.ReadAsync(buffer, 0, lengthToRead, cancellationToken); - - var body = Encoding.UTF8.GetString(buffer); - if (!string.IsNullOrEmpty(body)) - { - request.Properties[RequestBodyKey] = body; - } - - stream.Seek(0, System.IO.SeekOrigin.Begin); + request.Properties[RequestBodyKey] = body; } return await base.SendAsync(request, cancellationToken); From 78520b5a8e346e7ff3a23f8cf64700d70b60d761 Mon Sep 17 00:00:00 2001 From: Greg Pakes Date: Mon, 7 May 2018 17:38:05 +1000 Subject: [PATCH 2/4] Removed unused references --- .../RaygunWebApiDelegatingHandler.cs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/Mindscape.Raygun4Net.WebApi/RaygunWebApiDelegatingHandler.cs b/Mindscape.Raygun4Net.WebApi/RaygunWebApiDelegatingHandler.cs index a889c2a6..f5255c87 100644 --- a/Mindscape.Raygun4Net.WebApi/RaygunWebApiDelegatingHandler.cs +++ b/Mindscape.Raygun4Net.WebApi/RaygunWebApiDelegatingHandler.cs @@ -1,13 +1,8 @@ -using System.ComponentModel; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Net.Http; -using System.Text; +using System.Net.Http; namespace Mindscape.Raygun4Net.WebApi { - public class RaygunWebApiDelegatingHandler : DelegatingHandler + public class RaygunWebApiDelegatingHandler : DelegatingHandler { public const string RequestBodyKey = "Raygun.RequestBody"; From 077371a95bd9c9d377a4805017d009c19a0e9b1a Mon Sep 17 00:00:00 2001 From: Greg Pakes Date: Mon, 7 May 2018 18:19:02 +1000 Subject: [PATCH 3/4] Reintroduced the 4096 byte limit --- .../RaygunWebApiDelegatingHandler.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Mindscape.Raygun4Net.WebApi/RaygunWebApiDelegatingHandler.cs b/Mindscape.Raygun4Net.WebApi/RaygunWebApiDelegatingHandler.cs index f5255c87..cfd38bd6 100644 --- a/Mindscape.Raygun4Net.WebApi/RaygunWebApiDelegatingHandler.cs +++ b/Mindscape.Raygun4Net.WebApi/RaygunWebApiDelegatingHandler.cs @@ -1,18 +1,22 @@ -using System.Net.Http; +using System.Linq; +using System.Net.Http; +using System.Text; namespace Mindscape.Raygun4Net.WebApi { - public class RaygunWebApiDelegatingHandler : DelegatingHandler + public class RaygunWebApiDelegatingHandler : DelegatingHandler { public const string RequestBodyKey = "Raygun.RequestBody"; protected override async System.Threading.Tasks.Task SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { - // ReadAsStringAsync is always readable as it calls LoadIntoBufferAsync internally. - var body = await request.Content.ReadAsStringAsync(); - if (!string.IsNullOrEmpty(body)) + // ReadAsByteArrayAsync is always readable as it calls LoadIntoBufferAsync internally. + var bytes = await request.Content.ReadAsByteArrayAsync(); + if (bytes.Length > 0) { - request.Properties[RequestBodyKey] = body; + // Only take first 4096 bytes + var bytesToSend = bytes.Take(4096).ToArray(); + request.Properties[RequestBodyKey] = Encoding.UTF8.GetString(bytesToSend); } return await base.SendAsync(request, cancellationToken); From 59d0f75adc1871a7062b44029986a3412a893f24 Mon Sep 17 00:00:00 2001 From: Greg Pakes Date: Tue, 8 May 2018 09:35:28 +1000 Subject: [PATCH 4/4] Respect the IsRawDataIgnored setting --- Mindscape.Raygun4Net.WebApi/RaygunWebApiClient.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Mindscape.Raygun4Net.WebApi/RaygunWebApiClient.cs b/Mindscape.Raygun4Net.WebApi/RaygunWebApiClient.cs index bf396c54..0aa3820a 100644 --- a/Mindscape.Raygun4Net.WebApi/RaygunWebApiClient.cs +++ b/Mindscape.Raygun4Net.WebApi/RaygunWebApiClient.cs @@ -132,9 +132,12 @@ private static void AttachInternal(HttpConfiguration config, Func