Implementing IRequestIdentifierFeature

Using code from HttpListener codebase to generate trace ids just to be consistent with other code.
This commit is contained in:
Praburaj 2015-03-02 16:52:56 -08:00
parent c08721c7b3
commit ca3259f703
3 changed files with 40 additions and 2 deletions

View File

@ -22,7 +22,6 @@ using System.Net;
using System.Net.WebSockets;
using System.Security.Claims;
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.FeatureModel;
@ -42,7 +41,8 @@ namespace Microsoft.AspNet.Server.WebListener
IHttpRequestLifetimeFeature,
IHttpWebSocketFeature,
IHttpAuthenticationFeature,
IHttpUpgradeFeature
IHttpUpgradeFeature,
IRequestIdentifierFeature
{
private RequestContext _requestContext;
private FeatureCollection _features;
@ -110,6 +110,8 @@ namespace Microsoft.AspNet.Server.WebListener
_features.Add(typeof(IHttpWebSocketFeature), this);
}
_features.Add(typeof(IRequestIdentifierFeature), this);
// TODO:
/*
Server
@ -432,5 +434,13 @@ namespace Microsoft.AspNet.Server.WebListener
get { return _authHandler; }
set { _authHandler = value; }
}
Guid IRequestIdentifierFeature.TraceIdentifier
{
get
{
return _requestContext.TraceIdentifier;
}
}
}
}

View File

@ -132,6 +132,19 @@ namespace Microsoft.Net.Http.Server
}
}
public unsafe Guid TraceIdentifier
{
get
{
// This is the base GUID used by HTTP.SYS for generating the activity ID.
// HTTP.SYS overwrites the first 8 bytes of the base GUID with RequestId to generate ETW activity ID.
var guid = new Guid(0xffcb4c93, 0xa57f, 0x453c, 0xb6, 0x3f, 0x84, 0x71, 0xc, 0x79, 0x67, 0xbb);
*((ulong*)&guid) = Request.RequestId;
return guid;
}
}
/// <summary>
/// The authentication challengest that will be added to the response if the status code is 401.
/// This must be a subset of the AuthenticationSchemes enabled on the server.

View File

@ -63,6 +63,11 @@ namespace Microsoft.AspNet.Server.WebListener
Assert.NotEqual(0, connectionInfo.LocalPort);
Assert.True(connectionInfo.IsLocal);
// Trace identifier
var requestIdentifierFeature = httpContext.GetFeature<IRequestIdentifierFeature>();
Assert.NotNull(requestIdentifierFeature);
Assert.NotNull(requestIdentifierFeature.TraceIdentifier);
// Note: Response keys are validated in the ResponseTests
}
catch (Exception ex)
@ -95,12 +100,17 @@ namespace Microsoft.AspNet.Server.WebListener
{
var requestInfo = httpContext.GetFeature<IHttpRequestFeature>();
var connectionInfo = httpContext.GetFeature<IHttpConnectionFeature>();
var requestIdentifierFeature = httpContext.GetFeature<IRequestIdentifierFeature>();
// Request Keys
Assert.Equal("http", requestInfo.Scheme);
Assert.Equal(expectedPath, requestInfo.Path);
Assert.Equal(expectedPathBase, requestInfo.PathBase);
Assert.Equal(string.Empty, requestInfo.QueryString);
// Trace identifier
Assert.NotNull(requestIdentifierFeature);
Assert.NotNull(requestIdentifierFeature.TraceIdentifier);
}
catch (Exception ex)
{
@ -135,10 +145,15 @@ namespace Microsoft.AspNet.Server.WebListener
{
var httpContext = new DefaultHttpContext((IFeatureCollection)env);
var requestInfo = httpContext.GetFeature<IHttpRequestFeature>();
var requestIdentifierFeature = httpContext.GetFeature<IRequestIdentifierFeature>();
try
{
Assert.Equal(expectedPath, requestInfo.Path);
Assert.Equal(expectedPathBase, requestInfo.PathBase);
// Trace identifier
Assert.NotNull(requestIdentifierFeature);
Assert.NotNull(requestIdentifierFeature.TraceIdentifier);
}
catch (Exception ex)
{