Skip to content

Commit

Permalink
Merge pull request #382 from gregpakes/webapi-request-body-with-owin-fix
Browse files Browse the repository at this point in the history
Added support for request body in projects with owin
  • Loading branch information
mduncan26 committed Jun 12, 2018
2 parents f65c798 + 59d0f75 commit 637422b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
3 changes: 1 addition & 2 deletions Mindscape.Raygun4Net.WebApi/RaygunWebApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ private static void AttachInternal(HttpConfiguration config, Func<HttpRequestMes
applicationVersion = applicationVersionFromAttach;
}

var owinAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName.StartsWith("Owin"));
if (owinAssembly == null)
if (RaygunSettings.Settings.IsRawDataIgnored == false)
{
config.MessageHandlers.Add(new RaygunWebApiDelegatingHandler());
}
Expand Down
23 changes: 7 additions & 16 deletions Mindscape.Raygun4Net.WebApi/RaygunWebApiDelegatingHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel;
using System.Linq;
using System.Linq;
using System.Net.Http;
using System.Text;

Expand All @@ -11,21 +10,13 @@ public class RaygunWebApiDelegatingHandler : DelegatingHandler

protected override async System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
var stream = await request.Content.ReadAsStreamAsync();
if (stream != null && stream.CanSeek)
// ReadAsByteArrayAsync is always readable as it calls LoadIntoBufferAsync internally.
var bytes = await request.Content.ReadAsByteArrayAsync();
if (bytes.Length > 0)
{
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);
// Only take first 4096 bytes
var bytesToSend = bytes.Take(4096).ToArray();
request.Properties[RequestBodyKey] = Encoding.UTF8.GetString(bytesToSend);
}

return await base.SendAsync(request, cancellationToken);
Expand Down

0 comments on commit 637422b

Please sign in to comment.