Skip to content

Commit

Permalink
Better urlencoded check for WebApi
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Fauchelle committed May 15, 2015
1 parent fa5054d commit fd9bbea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;

Expand All @@ -25,16 +26,25 @@ public static RaygunRequestMessage Build(HttpRequestMessage request, RaygunReque
message.Form = ToDictionary(request.GetQueryNameValuePairs(), options.IsFormFieldIgnored);
message.QueryString = ToDictionary(request.GetQueryNameValuePairs(), s => false);

SetHeaders(message, request, options.IsHeaderIgnored);

if (!options.IsRawDataIgnored)
{
object body;
if (request.Properties.TryGetValue(RaygunWebApiDelegatingHandler.RequestBodyKey, out body))
string contentType = null;
if (message.Headers != null && message.Headers.Contains("Content-Type"))
{
message.RawData = body.ToString();
contentType = (string)message.Headers["Content-Type"];
}
}

SetHeaders(message, request, options.IsHeaderIgnored);
if (contentType == null || CultureInfo.InvariantCulture.CompareInfo.IndexOf(contentType, "application/x-www-form-urlencoded", CompareOptions.IgnoreCase) < 0)
{
object body;
if (request.Properties.TryGetValue(RaygunWebApiDelegatingHandler.RequestBodyKey, out body))
{
message.RawData = body.ToString();
}
}
}

return message;
}
Expand Down Expand Up @@ -99,7 +109,7 @@ private static void SetHeaders(RaygunRequestMessage message, HttpRequestMessage
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine("Error retrieving Headers and RawData {0}", ex.Message);
System.Diagnostics.Trace.WriteLine("Error retrieving Headers: {0}", ex.Message);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions Mindscape.Raygun4Net/Builders/RaygunRequestMessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ public static RaygunRequestMessage Build(HttpRequest request, RaygunRequestMessa
{
try
{
// Don't send the raw request data at all if the content-type is urlencoded
var contentType = request.Headers["Content-Type"];
if (contentType != "text/html" && (contentType == null || CultureInfo.InvariantCulture.CompareInfo.IndexOf(contentType, "application/x-www-form-urlencoded", CompareOptions.IgnoreCase) < 0) && request.RequestType != "GET")
{
int length = 4096;
request.InputStream.Seek(0, SeekOrigin.Begin);
string temp = new StreamReader(request.InputStream).ReadToEnd();

// If we made it this far, strip out any values that have been marked as ignored form fields
Dictionary<string, string> ignored = GetIgnoredFormValues(request.Form, options.IsFormFieldIgnored);
temp = StripIgnoredFormData(temp, ignored);

Expand Down

0 comments on commit fd9bbea

Please sign in to comment.