Added an HttpVersionInternal to be used by the higher-stack libraries that need things beyond HttpVersion.Version10/Version11 and/or that have builds targeting versioning of System.Net.Primitives that doesn't have the newly exposed HttpVersion. This can be removed once all of those libraries have all of their builds targeting the updated primitives lib.
Commit migrated from https://github.com/dotnet/corefx/commit/119ae03ddc2ce49511f10b02fb39818aab0aa577
This PR overhauls how logging is accomplished in the System.Net libraries.
There are a few problems with the existing solution:
1. In the original sources, logging was split across a GlobalLog class and a Logging class. Global-related tracing went to GlobalLog, and library-specific tracing went to Logging. When the sources were moved to corefx, this evolved into multiple EventSource-based types, most of which were in Common and were then src-included into each System.Net assembly. This has a significant problem, though, which is that the name/Guid identity of an EventSource can only exist once in a process, and subsequent EventSource instances with the same identity fail. This means that whichever assembly ends up logging something first wins, and all other tracing from other System.Net assemblies ends up being lost.
2. The split also may have made sense when all components shared the same GlobalLog, but now that GlobalLog is included as a unique EventSource instance in each assembly, it's no longer global. This means that we're not only loading multiple EventSources unnecessarily (each has its own costs), but we're also duplicating logging, as some logging is done to one or the other, but some logging is done to both.
3. Due to sharing the Logging class, which evolved into an EventSource-based type, many logging calls had to include which component they came from as an enum value, leading to bloating at the call sites.
4. Additionally, many logging operations included the name of the method in which the calling was being done, which meant both longer call sites as well as stale values as members got renamed over time.
5. Call sites ended up getting fairly long due to lots of string concatenation operations and other manipulations in the logging call. This also then led to most call sites needing to be guarded behind checks for IsEnabled, leading to lots of code at every call site and obscuring the actual information being traced.
6. An additional requirement that got added with the move to corefx was that certain failure conditions should both Debug.Fail and do tracing, leading to some tracing operations taking upwards of 10 lines of code.
All of these issues are addressed by this PR.
- Each System.Net assembly that needs to log now loads a single NetEventSource type for doing logging, each with its own identity.
- That implementation is split across a common, partial NetEventSource.Common.cs file used by all such assemblies, and then a per-assembly partial NetEventSource.AssemblyName.cs file used by the individual assembly. The former file contains most of the logging implementation. The latter files typically contain just the partial class declaration along with the [EventSource] attribute that gives that assembly's NetEventSource its unique identity / name.
- The logging operations in NetEventSource.Common.cs use a variety of relatively-recent language enhancements to improve the logging. Each operation uses a [CallerMemberName] so that the call sites don't need to specify the caller. FormattableString is used so that call sites can use string interpolation while still providing the logging implementation the flexibility to format the individual components in a desired fashion (e.g. IntPtrs are formatted as hex, SafeHandles are rendered in a way that includes their name+hashcode+handle, etc.), along with a mechanism that allows individual libraries to add additional formatting rules. Each operation allows the this reference (or another this-like context object) to be passed in, to simply identify the source of the call (in concert with the caller member name). A debug-only mechanism is included in the tracing to help find potential perf issues due to misuse of the logger. Etc.
- A Fail logging operation is included that both traces and does a Debug.Fail.
With these changes, most call sites are greatly simplified. Some examples:
Before:
```C#
if (GlobalLog.IsEnabled)
{
GlobalLog.AssertFormat("ContextAwareResult#{0}::ContextCopy|Called on completed result.", LoggingHash.HashString(this));
}
Debug.Fail("ContextAwareResult#" + LoggingHash.HashString(this) + "::ContextCopy |Called on completed result.");
```
After:
```C#
NetEventSource.Fail(this, "Called on completed result.");
```
Before:
```C#
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(NetEventSource.ComponentType.Http, this, ".ctor", handler);
```
After:
```C#
NetEventSource.Enter(this, handler);
```
Etc.
In addition to fixing up all of the event sources, I of course also fixed up all usage in order to clean up the call sites. However, this was a rather mechanical change: I did not remove existing logging (other than duplication) nor did I add new logging. At some point, we should review the actual logging being done to determine what's missing and what's not needed. I also did not revise the additional existing events on the sockets and security event sources, but we may want to remove those (or some of those) in favor of just using the shared events.
Commit migrated from https://github.com/dotnet/corefx/commit/92fd5c0814bcc0c03acfa9f3ad857661828b35a6
Ports the .NET Framework implementation to corefx and adds some tests. There is a large portion of the implementation that's internal and not actually reachable from the exposed surface area, but exists to support SmtpClient. I've left that code in so as to enable subsequent porting of System.Net.Mail; at that point, we may want to combine all of this into a System.Net.Mail contract, but for now I've left it in a System.Net.Mime one.
Commit migrated from https://github.com/dotnet/corefx/commit/9594452b79f80f1262c6586ff8f2f5c21fb18ee2
We've standardized on using Unix in most cases where we only cross-
compile once for Unix. This change cleans up mentions of Linux/OSX when
we aren't actually cross-compiling for those OSes.
The remaining projects that still cross-compile for Linux/OSX are:
System.Diagnostics.Process
System.IO.FileSystem.Watcher
System.Net.NetworkInformation
The System.Net.Security functional tests seem to still have a distinct
difference in functional behavior on OSX so I have also left these
configured to cross-compile for Linux/OSX.
Commit migrated from https://github.com/dotnet/corefx/commit/16ee317fa4e194ff7cf61e923caf103909e391f1
Copying between streams represents one of the larger sources of memory allocations and copying in HttpClient. HttpClient has its own stream-to-stream copy implementation, but there are ways we can significantly improve it based on typical usage within HttpClient. This commit does the following:
- Previously it would special case when both the source and destination stream were MemoryStreams, but even in that case it would allocate a temporary buffer and do a Read/Write loop to copy between them, and even then it's relatively rare to copy from a MemoryStream to a MemoryStream. Instead, if the source stream is a MemoryStream and we can get its buffer, we can do a single write to the destination (regardless of what kind of stream it is), avoiding the need for multiple read/writes and for the temporary buffer. If we can't get its buffer but its length is known to be less than or equal to the requested buffer size and it's positioned at the beginning, we can simply ToArray the source and still do a single write to the target.
- If the destination is the special LimitMemoryStream, used for buffering output, and if that LimitMemoryStream was pre-sized based on the response having a Content-Length, then rather than allocating a temporary buffer and repeatedly copying from the source stream into that buffer and copying from the buffer into the memory stream, we can instead give the destination's buffer directly to be read into by the source, avoiding the temporary buffer and the extra copies. This is a very common case.
- If the source does not need to be disposed after the copy, rather than doing our own copy loop, we can instead ask the source to use its CopyToAsync implementation to copy to the destination stream. Worst case this will be exactly what we were previously doing, but best case the source has its own CopyToAsync override that's more efficient.
- When constructing a LimitMemoryStream, if the Content-Length isn't known, we were explicitly passing a 0 capacity to the base, which causes it to allocate a new zero-length array; I've added a ctor to avoid that.
Also avoids some unnecessary array allocations and copies while getting data from buffered content.
Commit migrated from https://github.com/dotnet/corefx/commit/d2502ce05032ae83c5675e2cd4b27ca2fb6706fa
This add a default configuration before the Import of dirs.props
for any projects that don't have Debug as the default configuration.
Removed Configuration and Platform properties set after dirs.props
as they would have no affect and just cause confusion.
Removed OutputType=Library property as it isn't necessary.
Commit migrated from https://github.com/dotnet/corefx/commit/9e1da13cf6d5ff4b37b1867beb3411a2fe61da6a
Adding EventSources to substitute the previous logging model using
TraceSources.
In this PR 4 event sources are added:
-NetEventSource
-SocketsEventSource
-HttpEventSource
-SecurityEventSource
Event methods for NetEventSource, SocketsEventSource and HttpEventSource
are
fully added. SecurityEventSource has only a few of
the new event methods implemented.
Changes in production code are done to match this new implementation.
(Only changes for
events in Sockets and Http are done in this iteration).
Adding missing reference to LoggingHash
Missing Reference in Security csproj
Missing reference in NetworkInformation
Adding a reference and adding a missing new line.
Adding references and readonly modifier.
Adding another missing reference.
Adding some missing blank lines and making readonly the s_log in the
EventSources.
Completing SecurityEventSource
Adding missing all the security events that will substitute PrintInfo
calls.
Doing changes in code to reflect this.
Transforming SecurityEventSource into a Partial class
Some of the events were Windows specific, because of the enums they were
using. Those events are now in a partial class that is included when
those enums are defined.
Moving events to partial SecurityEventSource class
Some more events using Interop.SecurityStatus are moved into the partial
windows specific class.
Also, a missed non event header is added for Associate method in
HttpEventSource.
Deleting Logging reference to instead use LoggingHash
Addressing some of the PR feedback
Adding the Issue number to TODO for localization, and using BlockCopy
instead of a Loop to copy array. Also, correctly passing partialBuffer
instead of Buffer to the DumpArrayAsync method.
Commit migrated from https://github.com/dotnet/corefx/commit/1cd8bf635d4b9a6d8a7d1e25cd320814c993f38b
System.Net.WebHeaderCollection already had a SubstringTrim helper that's used instead of Substring(...).Trim() to avoid first creating the substring and then trimming it, instead just offsetting the startIndex and length appropriately to do the Substring once.
I've factored this helper out into Common, added unit tests, cleaned it up a bit, and used it in several more places in System.Net.
Commit migrated from https://github.com/dotnet/corefx/commit/4e45e641c9d97c3a5d3b6fec4f6ffc33a2405fee
This is a large PR and addresses work item dotnet/corefx#3000. I apologize for not being able to structure it in a series of smaller commits.
The bulk of this PR is switching WinHttpHandler to use the async pattern of calling WinHTTP APIs. This involved a significant restructuring of the code. As part of that, I split up the large WinHttpHandler class into multiple, smaller, helper classes.
While the intent of using async WinHTTP calls is better performance and cancel-ability, this PR has not been fully optimized yet. Due to the schedule of RC1 code complete next week, it's important that this PR get in and get mileage on it in the next few weeks.
There are several TODO items that will be done in later PR's. There is still work being tracked in dotnet/corefx#2165 for various follow-up items. For example, re-conciling the best SafeHandle pattern to use for WinHTTP between WebSockets.Client and WinHttpHandler.
I'm currently using a simple Debug.WriteLine for verbose debug logging which can be turned on/off via environment variable. I plan to switch this to System.Net ETW trace loggers in the next commit and/or PR. But the logging has proved very useful in diagnosing issues.
Please provide feedback. Depending on the feedback, some of it will be marked as follow-up items for future PRs.
Commit migrated from https://github.com/dotnet/corefx/commit/d8e5371a5d0f9f4bb3aa85127cb78adf89dde1fb
This change sets up the layout for having multiple test projects for Http. It follows the current guidlines as well as working within the BUILDTOOLS requirements of having ".Tests.csproj" for the names of the projects.
Commit migrated from https://github.com/dotnet/corefx/commit/e7b68608db14327ec55fc90c0f5aee3ed72fab36