Skip to content

Commit

Permalink
Make non-file URL support optional in XmlUrlResolver
Browse files Browse the repository at this point in the history
This adds [removable feature annotation](dotnet/designs#42) to `XmlDownloadManager`.

If the user specifies that they would like to remove support for this at publish/native compilation time, the linker/compiler is going to replace the method body of `CreateWebRequestOrThrowIfRemoved` with a throwing method body. The exception message is going to inform the user that the feature has been removed because they opted into the removal.

Contributes to dotnet/corefx#30597. Saves 1.2 MB of the size of the Windows UWP People app. This is a size on disk regression that came with NetStandard 2.0.


Commit migrated from dotnet/corefx@5898cdd
  • Loading branch information
MichalStrehovsky committed Jul 31, 2018
1 parent d004dbe commit 857e618
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@
<Compile Include="$(CommonPath)\System\LocalAppContext.cs" />
<Compile Include="System\Xml\Core\LocalAppContextSwitches.cs" />
<Compile Include="$(CommonPath)\System\CSharpHelpers.cs" />
<Compile Include="$(CommonPath)\System\Runtime\CompilerServices\RemovableFeatureAttribute.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsWindows)'=='true'">
<Compile Include="System\Xml\Xsl\Runtime\XmlCollation.Windows.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace System.Xml
using System.Collections;
using System.Net;
using System.Net.Cache;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Net.Http;

Expand All @@ -33,6 +34,27 @@ internal Stream GetStream(Uri uri, ICredentials credentials, IWebProxy proxy,

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())
{
var result = new MemoryStream();
respStream.CopyTo(result);
result.Position = 0;
return result;
}
}

// This method is marked Removable because WebRequest has a lot of dependencies that will bloat
// self-contained distributions of .NET Apps.
// 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.
[RemovableFeature("System.Xml.XmlUrlResolver.NonFileUrlSupport")]
private static WebRequest CreateWebRequestOrThrowIfRemoved(Uri uri, ICredentials credentials, IWebProxy proxy,
RequestCachePolicy cachePolicy)
{
WebRequest req = WebRequest.Create(uri);
if (credentials != null)
Expand All @@ -48,14 +70,7 @@ private Stream GetNonFileStream(Uri uri, ICredentials credentials, IWebProxy pro
req.CachePolicy = cachePolicy;
}

using (WebResponse resp = req.GetResponse())
using (Stream respStream = resp.GetResponseStream())
{
var result = new MemoryStream();
respStream.CopyTo(result);
result.Position = 0;
return result;
}
return req;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,7 @@ internal Task<Stream> GetStreamAsync(Uri uri, ICredentials credentials, IWebProx
private async Task<Stream> GetNonFileStreamAsync(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;
}
if (cachePolicy != null)
{
req.CachePolicy = cachePolicy;
}
WebRequest req = CreateWebRequestOrThrowIfRemoved(uri, credentials, proxy, cachePolicy);

using (WebResponse resp = await req.GetResponseAsync().ConfigureAwait(false))
using (Stream respStream = resp.GetResponseStream())
Expand Down

0 comments on commit 857e618

Please sign in to comment.