8
8
using System . Reflection ;
9
9
using System . Runtime . InteropServices ;
10
10
using System . Text ;
11
+ using System . Web ;
11
12
12
13
namespace Org . XmlResolver . Utils {
13
14
/// <summary>
@@ -78,7 +79,7 @@ public static Uri Resolve(Uri baseURI, Uri uri) {
78
79
/// <returns>The current directory as a URI.</returns>
79
80
public static Uri Cwd ( ) {
80
81
return new Uri ( new Uri ( "file://" ) ,
81
- System . IO . Directory . GetCurrentDirectory ( ) + "/" ) ;
82
+ Directory . GetCurrentDirectory ( ) + "/" ) ;
82
83
}
83
84
84
85
/// <summary>
@@ -310,10 +311,13 @@ public static Stream GetStream(string uri) {
310
311
/// <c>pack:</c>, and <c>data:</c> URIs are supported.</para>
311
312
/// <para>The assembly is only relevant for <c>pack:</c> scheme URIs.</para>
312
313
/// <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>
314
318
/// <param name="uri">The URI.</param>
315
319
/// <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>
317
321
/// <exception cref="ArgumentException">If the URI is not absolute or has an unsupported scheme.</exception>
318
322
public static Stream GetStream ( string uri , Assembly asm ) {
319
323
if ( uri . StartsWith ( "file:/" ) ) {
@@ -331,7 +335,7 @@ public static Stream GetStream(string uri, Assembly asm) {
331
335
if ( uri . StartsWith ( "data:" ) ) {
332
336
return _getDataStream ( uri ) ;
333
337
}
334
-
338
+
335
339
throw new ArgumentException ( "Unexpected URI scheme: " + uri ) ;
336
340
}
337
341
@@ -364,6 +368,11 @@ private static Stream _getFileStream(string uri) {
364
368
private static Stream _getHttpStream ( string uri ) {
365
369
HttpRequestMessage req = new HttpRequestMessage ( HttpMethod . Get , uri ) ;
366
370
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
+ }
367
376
return resp . Content . ReadAsStream ( ) ;
368
377
}
369
378
@@ -460,7 +469,7 @@ private static Stream _getPackFileStream(string uri) {
460
469
filestr = filestr . Replace ( "%3A" , ":" ) ;
461
470
filestr = filestr . Replace ( "," , "/" ) ;
462
471
463
- Uri fileuri = UriUtils . NewUri ( filestr ) ;
472
+ Uri fileuri = NewUri ( filestr ) ;
464
473
filestr = fileuri . AbsolutePath ;
465
474
466
475
// Assume this is a zip file...
@@ -486,7 +495,7 @@ private static Stream _getDataStream(string uri) {
486
495
String data = path . Substring ( pos + 1 ) ;
487
496
if ( mediatype . EndsWith ( ";base64" ) ) {
488
497
// Base64 decode it
489
- var base64bytes = System . Convert . FromBase64String ( data ) ;
498
+ var base64bytes = Convert . FromBase64String ( data ) ;
490
499
inputStream = new MemoryStream ( base64bytes ) ;
491
500
}
492
501
else {
@@ -501,7 +510,7 @@ private static Stream _getDataStream(string uri) {
501
510
}
502
511
}
503
512
504
- data = System . Web . HttpUtility . UrlDecode ( data , System . Text . Encoding . GetEncoding ( charset ) ) ;
513
+ data = HttpUtility . UrlDecode ( data , Encoding . GetEncoding ( charset ) ) ;
505
514
inputStream = new MemoryStream ( Encoding . UTF8 . GetBytes ( data ) ) ;
506
515
}
507
516
0 commit comments