Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/System.Net.Http/src/System.Net.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
<Compile Include="System\Net\Http\ByteArrayContent.cs" />
<Compile Include="System\Net\Http\ClientCertificateOption.cs" />
<Compile Include="System\Net\Http\DelegatingHandler.cs" />
<Compile Include="System\Net\Http\DiagnosticsHandler.cs" />
<Compile Include="System\Net\Http\DiagnosticsHandlerLoggingStrings.cs"/>
<Compile Include="System\Net\Http\FormUrlEncodedContent.cs" />
<Compile Include="System\Net\Http\HttpClient.cs" />
<Compile Include="System\Net\Http\HttpCompletionOption.cs" />
Expand Down Expand Up @@ -119,8 +121,6 @@
</ItemGroup>
<!-- Common, except for Windows net46 build -->
<ItemGroup Condition="'$(TargetGroup)' != 'net46' And '$(TargetGroup)' != 'netfx'">
<Compile Include="System\Net\Http\DiagnosticsHandler.cs" />
<Compile Include="System\Net\Http\DiagnosticsHandlerLoggingStrings.cs"/>
<Compile Include="$(CommonPath)\System\Net\Mail\DomainLiteralReader.cs">
<Link>Common\System\Net\Mail\DomainLiteralReader.cs</Link>
</Compile>
Expand Down Expand Up @@ -179,6 +179,7 @@
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime" />
<ProjectReference Include="..\..\System.Diagnostics.DiagnosticSource\src\System.Diagnostics.DiagnosticSource.csproj" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not do ProjectReferences here, there should be just a Reference.

</ItemGroup>
<!-- Unix -->
<PropertyGroup Condition=" '$(TargetsUnix)' == 'true' ">
Expand Down
23 changes: 14 additions & 9 deletions src/System.Net.Http/src/System/Net/Http/DiagnosticsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ namespace System.Net.Http
/// <summary>
/// DiagnosticHandler notifies DiagnosticSource subscribers about outgoing Http requests
/// </summary>
internal sealed class DiagnosticsHandler : DelegatingHandler
internal sealed class DiagnosticsHandler
{
/// <summary>
/// DiagnosticHandler constructor
/// </summary>
/// <param name="innerHandler">Inner handler: Windows or Unix implementation of HttpMessageHandler.
/// Note that DiagnosticHandler is the latest in the pipeline </param>
public DiagnosticsHandler(HttpMessageHandler innerHandler) : base(innerHandler)
/// <param name="sendAsync">referene to HttpClientHandler.SendAsync (Windows\Linux\Win Desktop)</param>
public DiagnosticsHandler(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> sendAsync)
{
_sendAsync = sendAsync;
}

internal static bool IsEnabled()
Expand All @@ -30,7 +30,7 @@ internal static bool IsEnabled()
return s_diagnosticListener.IsEnabled();
}

protected internal override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
internal async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
//HttpClientHandler is responsible to call DiagnosticsHandler.IsEnabled() before forwarding request here.
Expand All @@ -55,6 +55,7 @@ protected internal override async Task<HttpResponseMessage> SendAsync(HttpReques
activity.Start();
}
}
#if !NET46
//if Activity events are disabled, try to write System.Net.Http.Request event (deprecated)
else if (s_diagnosticListener.IsEnabled(DiagnosticsHandlerLoggingStrings.RequestWriteNameDeprecated))
{
Expand All @@ -69,7 +70,7 @@ protected internal override async Task<HttpResponseMessage> SendAsync(HttpReques
}
);
}

#endif
// If we are on at all, we propagate any activity information.
Activity currentActivity = Activity.Current;
if (currentActivity != null)
Expand All @@ -92,7 +93,7 @@ protected internal override async Task<HttpResponseMessage> SendAsync(HttpReques
}
}

Task<HttpResponseMessage> responseTask = base.SendAsync(request, cancellationToken);
Task<HttpResponseMessage> responseTask = _sendAsync(request, cancellationToken);
try
{
await responseTask.ConfigureAwait(false);
Expand Down Expand Up @@ -128,6 +129,7 @@ protected internal override async Task<HttpResponseMessage> SendAsync(HttpReques
RequestTaskStatus = responseTask.Status
});
}
#if !NET46
//if Activity events are disabled, try to write System.Net.Http.Response event (deprecated)
else if (s_diagnosticListener.IsEnabled(DiagnosticsHandlerLoggingStrings.ResponseWriteNameDeprecated))
{
Expand All @@ -142,15 +144,18 @@ protected internal override async Task<HttpResponseMessage> SendAsync(HttpReques
}
);
}
#endif
}

return responseTask.Result;
}

#region private
#region private

private readonly Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> _sendAsync;
private static readonly DiagnosticListener s_diagnosticListener =
new DiagnosticListener(DiagnosticsHandlerLoggingStrings.DiagnosticListenerName);

#endregion
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ namespace System.Net.Http
internal static class DiagnosticsHandlerLoggingStrings
{
public const string DiagnosticListenerName = "HttpHandlerDiagnosticListener";

#if !NET46
public const string RequestWriteNameDeprecated = "System.Net.Http.Request";
public const string ResponseWriteNameDeprecated = "System.Net.Http.Response";
#endif

public const string ExceptionEventName = "System.Net.Http.Exception";
public const string ActivityName = "System.Net.Http.Activity";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ private Func<
SslPolicyErrors,
bool> _serverCertificateCustomValidationCallback;

private readonly DiagnosticsHandler _diagnosticsPipeline;

#endregion Fields

#region Properties
Expand Down Expand Up @@ -446,6 +448,7 @@ public HttpClientHandler()
_properties = null; // only create collection when required.
_maxConnectionsPerServer = ServicePointManager.DefaultConnectionLimit;
_serverCertificateCustomValidationCallback = null;
_diagnosticsPipeline = new DiagnosticsHandler(this.SendAsyncImpl);
}

protected override void Dispose(bool disposing)
Expand Down Expand Up @@ -753,9 +756,19 @@ private static void SetContentHeaders(HttpWebRequest webRequest, HttpRequestMess
#endregion Message Setup

#region Request Processing

protected internal override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
if (DiagnosticsHandler.IsEnabled())
{
return _diagnosticsPipeline.SendAsync(request, cancellationToken);
}
return SendAsyncImpl(request, cancellationToken);
}

internal Task<HttpResponseMessage> SendAsyncImpl(HttpRequestMessage request,
CancellationToken cancellationToken)
{
if (request == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public IDictionary<String, object> Properties
public HttpClientHandler()
{
_curlHandler = new CurlHandler();
_diagnosticsPipeline = new DiagnosticsHandler(_curlHandler);
_diagnosticsPipeline = new DiagnosticsHandler(_curlHandler.SendAsync);
}

protected override void Dispose(bool disposing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public IDictionary<String, object> Properties
public HttpClientHandler()
{
_winHttpHandler = new WinHttpHandler();
_diagnosticsPipeline = new DiagnosticsHandler(_winHttpHandler);
_diagnosticsPipeline = new DiagnosticsHandler(_winHttpHandler.SendAsync);

// Adjust defaults to match current .NET Desktop HttpClientHandler (based on HWR stack).
AllowAutoRedirect = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class HttpClientHandler : HttpMessageHandler

private readonly RTHttpBaseProtocolFilter rtFilter;
private readonly HttpHandlerToFilter handlerToFilter;
private readonly HttpMessageHandler diagnosticsPipeline;
private readonly DiagnosticsHandler diagnosticsPipeline;

private volatile bool operationStarted;
private volatile bool disposed;
Expand Down Expand Up @@ -422,7 +422,7 @@ public HttpClientHandler()
{
this.rtFilter = new RTHttpBaseProtocolFilter();
this.handlerToFilter = new HttpHandlerToFilter(this.rtFilter);
this.diagnosticsPipeline = new DiagnosticsHandler(handlerToFilter);
this.diagnosticsPipeline = new DiagnosticsHandler(handlerToFilter.SendAsync);

this.clientCertificateOptions = ClientCertificateOption.Manual;

Expand Down