Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Remove System.Net.Requests dependency from System.Private.Xml
Browse files Browse the repository at this point in the history
Anything that uses System.Xml ends up implicitly referencing this .dll, which in a trimmed default MVC app is 97K.  The only thing it's used for is as part of XmlResolver to download the specified url.  We can instead remove the usage of WebRequest.Create and replace it with usage of HttpClient, which is already brought in because System.Net.Requests uses it to implement HttpWebRequest.
  • Loading branch information
stephentoub committed Sep 14, 2019
1 parent 001420e commit c4933b4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 74 deletions.
1 change: 0 additions & 1 deletion src/System.Private.Xml/src/System.Private.Xml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,6 @@
<Reference Include="System.Linq.Expressions" />
<Reference Include="System.Memory" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Requests" />
<Reference Include="System.Net.Primitives" />
<Reference Include="System.ObjectModel" />
<Reference Include="System.Reflection.Primitives" />
Expand Down
72 changes: 25 additions & 47 deletions src/System.Private.Xml/src/System/Xml/XmlDownloadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,17 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.IO;
using System.Net;
using System.Net.Cache;
using System.Net.Http;

namespace System.Xml
{
using System;
using System.IO;
using System.Security;
using System.Collections;
using System.Net;
using System.Net.Cache;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Net.Http;

//
// XmlDownloadManager
//
internal partial class XmlDownloadManager
{
internal Stream GetStream(Uri uri, ICredentials credentials, IWebProxy proxy,
RequestCachePolicy cachePolicy)
internal Stream GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
{
if (uri.Scheme == "file")
{
Expand All @@ -32,42 +24,28 @@ internal Stream GetStream(Uri uri, ICredentials credentials, IWebProxy proxy,
}
}

private Stream GetNonFileStream(Uri uri, ICredentials credentials, IWebProxy proxy,
RequestCachePolicy cachePolicy)
private Stream GetNonFileStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
{
WebRequest req = CreateWebRequestOrThrowIfRemoved(uri, credentials, proxy, cachePolicy);

using (WebResponse resp = req.GetResponse())
using (Stream respStream = resp.GetResponseStream())
using (var handler = new SocketsHttpHandler())
using (var client = new HttpClient(handler))
{
var result = new MemoryStream();
respStream.CopyTo(result);
result.Position = 0;
return result;
}
}
if (credentials != null)
{
handler.Credentials = credentials;
}
if (proxy != null)
{
handler.Proxy = proxy;
}

// This code is statically reachable from any place that uses XmlReaderSettings (i.e. every app that
// does something XML related is going to have this in their transitive call graph). People rarely need
// this functionality though.
private static WebRequest CreateWebRequestOrThrowIfRemoved(Uri uri, ICredentials credentials, IWebProxy proxy,
RequestCachePolicy cachePolicy)
{
WebRequest req = WebRequest.Create(uri);
if (credentials != null)
{
req.Credentials = credentials;
}
if (proxy != null)
{
req.Proxy = proxy;
using (Stream respStream = client.GetStreamAsync(uri).GetAwaiter().GetResult())
{
var result = new MemoryStream();
respStream.CopyTo(result);
result.Position = 0;
return result;
}
}
if (cachePolicy != null)
{
req.CachePolicy = cachePolicy;
}

return req;
}
}
}
48 changes: 26 additions & 22 deletions src/System.Private.Xml/src/System/Xml/XmlDownloadManagerAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,18 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.IO;
using System.Net;
using System.Net.Cache;
using System.Net.Http;
using System.Threading.Tasks;

namespace System.Xml
{
using System;
using System.IO;
using System.Security;
using System.Collections;
using System.Net;
using System.Net.Cache;
using System.Runtime.Versioning;
using System.Threading.Tasks;
using System.Net.Http;
//
// XmlDownloadManager
//
internal partial class XmlDownloadManager
{
internal Task<Stream> GetStreamAsync(Uri uri, ICredentials credentials, IWebProxy proxy,
RequestCachePolicy cachePolicy)
internal Task<Stream> GetStreamAsync(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
{
if (uri.Scheme == "file")
{
Expand All @@ -34,15 +28,25 @@ internal Task<Stream> GetStreamAsync(Uri uri, ICredentials credentials, IWebProx
private async Task<Stream> GetNonFileStreamAsync(Uri uri, ICredentials credentials, IWebProxy proxy,
RequestCachePolicy cachePolicy)
{
WebRequest req = CreateWebRequestOrThrowIfRemoved(uri, credentials, proxy, cachePolicy);

using (WebResponse resp = await req.GetResponseAsync().ConfigureAwait(false))
using (Stream respStream = resp.GetResponseStream())
using (var handler = new SocketsHttpHandler())
using (var client = new HttpClient(handler))
{
var result = new MemoryStream();
await respStream.CopyToAsync(result).ConfigureAwait(false);
result.Position = 0;
return result;
if (credentials != null)
{
handler.Credentials = credentials;
}
if (proxy != null)
{
handler.Proxy = proxy;
}

using (Stream respStream = await client.GetStreamAsync(uri).ConfigureAwait(false))
{
var result = new MemoryStream();
respStream.CopyTo(result);
result.Position = 0;
return result;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static void ReadAsyncAfterInitializationWithUriThrows()
{
using (XmlReader reader = XmlReader.Create("http://test.test/test.html", new XmlReaderSettings() { Async = true }))
{
Assert.Throws<System.Net.WebException>(() => reader.ReadAsync().GetAwaiter().GetResult());
Assert.Throws<System.Net.Http.HttpRequestException>(() => reader.ReadAsync().GetAwaiter().GetResult());
}
}

Expand All @@ -83,14 +83,14 @@ public static void ReadAfterInitializationWithUriOnAsyncReaderTrows()
{
using (XmlReader reader = XmlReader.Create("http://test.test/test.html", new XmlReaderSettings() { Async = true }))
{
Assert.Throws<System.Net.WebException>(() => reader.Read());
Assert.Throws<System.Net.Http.HttpRequestException>(() => reader.Read());
}
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsSubsystemForLinux))] // [ActiveIssue(11057)]
public static void InitializationWithUriOnNonAsyncReaderTrows()
{
Assert.Throws<System.Net.WebException>(() => XmlReader.Create("http://test.test/test.html", new XmlReaderSettings() { Async = false }));
Assert.Throws<System.Net.Http.HttpRequestException>(() => XmlReader.Create("http://test.test/test.html", new XmlReaderSettings() { Async = false }));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static void TestResolveInvalidPaths()
[Fact]
public static void TestResolveInvalidPath()
{
Assert.Throws<System.Net.WebException>(() => XmlReader.Create("ftp://host.invalid"));
Assert.ThrowsAny<Exception>(() => XmlReader.Create("ftp://host.invalid"));
}

[Fact]
Expand Down

0 comments on commit c4933b4

Please sign in to comment.