88using System . Reflection ;
99using System . Runtime . InteropServices ;
1010using System . Text ;
11+ using System . Web ;
1112
1213namespace 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
0 commit comments