Skip to content

Commit a4a308e

Browse files
authored
Merge pull request #68 from ndw/fix-http
Fix the bug where UriUtils.GetStream() hides http errors
2 parents bda7bb4 + 4acc423 commit a4a308e

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

XmlResolver/XmlResolver/Org/XmlResolver/Utils/UriUtils.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Reflection;
99
using System.Runtime.InteropServices;
1010
using System.Text;
11+
using System.Web;
1112

1213
namespace Org.XmlResolver.Utils {
1314
/// <summary>
@@ -78,7 +79,7 @@ public static Uri Resolve(Uri baseURI, Uri uri) {
7879
/// <returns>The current directory as a URI.</returns>
7980
public static Uri Cwd() {
8081
return new Uri(new Uri("file://"),
81-
System.IO.Directory.GetCurrentDirectory() + "/");
82+
Directory.GetCurrentDirectory() + "/");
8283
}
8384

8485
/// <summary>
@@ -310,10 +311,13 @@ public static Stream GetStream(string uri) {
310311
/// <c>pack:</c>, and <c>data:</c> URIs are supported.</para>
311312
/// <para>The assembly is only relevant for <c>pack:</c> scheme URIs.</para>
312313
/// <para>In addition to <c>ArgumentException</c>, any exceptions raised attempting to open the
313-
/// resource will also be thrown.</para>
314+
/// resource will also be thrown. In particular, <c>FileNotFoundException</c> or similar exceptions for unreadable
315+
/// file: URIs and <c>HttpRequestException</c> for http(s) resources that return an error code other than 200.
316+
/// (The underlying Http machinery follows redirects, so this shouldn't apply to 3xx error codes under
317+
/// most circumstances.)</para>
314318
/// <param name="uri">The URI.</param>
315319
/// <param name="asm">The relevant assembly.</param>
316-
/// <returns>The stream, or null if the stram could not be opened.</returns>
320+
/// <returns>The stream, or null if the stream could not be opened.</returns>
317321
/// <exception cref="ArgumentException">If the URI is not absolute or has an unsupported scheme.</exception>
318322
public static Stream GetStream(string uri, Assembly asm) {
319323
if (uri.StartsWith("file:/")) {
@@ -331,7 +335,7 @@ public static Stream GetStream(string uri, Assembly asm) {
331335
if (uri.StartsWith("data:")) {
332336
return _getDataStream(uri);
333337
}
334-
338+
335339
throw new ArgumentException("Unexpected URI scheme: " + uri);
336340
}
337341

@@ -364,6 +368,11 @@ private static Stream _getFileStream(string uri) {
364368
private static Stream _getHttpStream(string uri) {
365369
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, uri);
366370
HttpResponseMessage resp = httpClient.Send(req);
371+
372+
if (resp.StatusCode != HttpStatusCode.OK)
373+
{
374+
throw new HttpRequestException("Failed to read resource", null, resp.StatusCode);
375+
}
367376
return resp.Content.ReadAsStream();
368377
}
369378

@@ -460,7 +469,7 @@ private static Stream _getPackFileStream(string uri) {
460469
filestr = filestr.Replace("%3A", ":");
461470
filestr = filestr.Replace(",", "/");
462471

463-
Uri fileuri = UriUtils.NewUri(filestr);
472+
Uri fileuri = NewUri(filestr);
464473
filestr = fileuri.AbsolutePath;
465474

466475
// Assume this is a zip file...
@@ -486,7 +495,7 @@ private static Stream _getDataStream(string uri) {
486495
String data = path.Substring(pos + 1);
487496
if (mediatype.EndsWith(";base64")) {
488497
// Base64 decode it
489-
var base64bytes = System.Convert.FromBase64String(data);
498+
var base64bytes = Convert.FromBase64String(data);
490499
inputStream = new MemoryStream(base64bytes);
491500
}
492501
else {
@@ -501,7 +510,7 @@ private static Stream _getDataStream(string uri) {
501510
}
502511
}
503512

504-
data = System.Web.HttpUtility.UrlDecode(data, System.Text.Encoding.GetEncoding(charset));
513+
data = HttpUtility.UrlDecode(data, Encoding.GetEncoding(charset));
505514
inputStream = new MemoryStream(Encoding.UTF8.GetBytes(data));
506515
}
507516

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ if (!hasProperty("nugetSource")) {
6464
tasks.register("dotnetBuild") {
6565
inputs.files fileTree(dir: "${projectDir}/XmlResolver/XmlResolver")
6666
outputs.file "${projectDir}/XmlResolver/XmlResolver/bin/Release/net5.0/XmlResolver.dll"
67-
outputs.file "${projectDir}/XmlResolver/XmlResolver/bin/Release/XmlResolver.{$resolverVersion}.nupkg"
67+
outputs.file "${projectDir}/XmlResolver/XmlResolver/bin/Release/XmlResolver.${resolverVersion}.nupkg"
6868

6969
doLast {
7070
exec {

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
resolverVersion=2.0.0
1+
resolverVersion=2.1.0

0 commit comments

Comments
 (0)