Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
Made SentryRequest.HttpContext static so it's only initialized once p…
Browse files Browse the repository at this point in the history
…er thread.
  • Loading branch information
asbjornu committed Nov 20, 2014
1 parent 278f890 commit 2976eee
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
56 changes: 32 additions & 24 deletions src/app/SharpRaven/Data/SentryRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,33 @@ namespace SharpRaven.Data
/// </summary>
public class SentryRequest
{
private readonly dynamic httpContext;


internal SentryRequest()
{
// NOTE: We're using dynamic to not require a reference to System.Web.
this.httpContext = GetHttpContext();
GetHttpContext();

if (!HasHttpContext)
return;

Url = this.httpContext.Request.Url.ToString();
Method = this.httpContext.Request.HttpMethod;
Url = HttpContext.Request.Url.ToString();
Method = HttpContext.Request.HttpMethod;
Environment = Convert(x => x.Request.ServerVariables);
Headers = Convert(x => x.Request.Headers);
Cookies = Convert(x => x.Request.Cookies);
Data = Convert(x => x.Request.Form);
QueryString = this.httpContext.Request.QueryString.ToString();
QueryString = HttpContext.Request.QueryString.ToString();
}


/// <summary>
/// Gets or sets the HTTP context.
/// </summary>
/// <value>
/// The HTTP context.
/// </value>
internal static dynamic HttpContext { get; set; }


/// <summary>
/// Gets or sets the cookies.
/// </summary>
Expand Down Expand Up @@ -130,9 +136,9 @@ internal SentryRequest()
public string Url { get; set; }

[JsonIgnore]
private bool HasHttpContext
private static bool HasHttpContext
{
get { return this.httpContext != null; }
get { return HttpContext != null; }
}


Expand All @@ -145,7 +151,7 @@ private bool HasHttpContext
public static SentryRequest GetRequest()
{
var request = new SentryRequest();
return request.HasHttpContext ? request : null;
return HasHttpContext ? request : null;
}


Expand All @@ -160,56 +166,58 @@ public SentryUser GetUser()
if (!HasHttpContext)
return null;

return new SentryUser(this.httpContext.User as IPrincipal)
return new SentryUser(HttpContext.User as IPrincipal)
{
IpAddress = this.httpContext.Request.UserHostAddress
IpAddress = HttpContext.Request.UserHostAddress
};
}


private static dynamic GetHttpContext()
private static void GetHttpContext()
{
if (HasHttpContext)
return;

try
{
var systemWeb = AppDomain.CurrentDomain
.GetAssemblies()
.FirstOrDefault(assembly => assembly.FullName.StartsWith("System.Web,"));

if (systemWeb == null)
return null;
if (HasHttpContext || systemWeb == null)
return;

var httpContextType = systemWeb.GetExportedTypes()
.FirstOrDefault(type => type.Name == "HttpContext");

if (httpContextType == null)
return null;
if (HasHttpContext || httpContextType == null)
return;

var currentHttpContextProperty = httpContextType.GetProperty("Current",
BindingFlags.Static | BindingFlags.Public);

if (currentHttpContextProperty == null)
return null;
if (HasHttpContext || currentHttpContextProperty == null)
return;

return currentHttpContextProperty.GetValue(null, null);
HttpContext = currentHttpContextProperty.GetValue(null, null);
}
catch (Exception exception)
{
Console.WriteLine("An error occurred while retrieving the HTTP context: {0}", exception);
return null;
}
}


private IDictionary<string, string> Convert(Func<dynamic, NameObjectCollectionBase> collectionGetter)
private static IDictionary<string, string> Convert(Func<dynamic, NameObjectCollectionBase> collectionGetter)
{
if (this.httpContext == null)
if (!HasHttpContext)
return null;

IDictionary<string, string> dictionary = new Dictionary<string, string>();

try
{
var collection = collectionGetter.Invoke(this.httpContext);
var collection = collectionGetter.Invoke(HttpContext);
var keys = Enumerable.ToArray(collection.AllKeys);

foreach (object key in keys)
Expand Down
10 changes: 9 additions & 1 deletion src/tests/SharpRaven.UnitTests/Data/SentryRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ namespace SharpRaven.UnitTests.Data
[TestFixture]
public class SentryRequestTests
{
[TearDown]
public void TearDown()
{
// Set the HTTP Context to null before so tests don't bleed data into each other. @asbjornu
SentryRequest.HttpContext = null;
}


private static void SimulateHttpRequest(Action<SentryRequest> test)
{
using (var simulator = new HttpSimulator())
Expand Down Expand Up @@ -86,7 +94,7 @@ public void GetRequest_WithHttpContext_RequestHasFormVariables()
{
Assert.That(request.Data, Is.TypeOf<Dictionary<string, string>>());

var data = (Dictionary<string, string>)request.Data;
var data = (Dictionary<string, string>) request.Data;

Assert.That(data, Has.Count.EqualTo(1));
Assert.That(data["Form1"], Is.EqualTo("Value1"));
Expand Down

0 comments on commit 2976eee

Please sign in to comment.