1
1
using System ;
2
2
using System . IO ;
3
+ using System . IO . Compression ;
3
4
using System . Net ;
4
5
using System . Net . Http ;
5
- using System . Net . Mime ;
6
6
using System . Threading . Tasks ;
7
7
8
8
namespace Dotnet . Script . Core
@@ -11,7 +11,6 @@ public class ScriptDownloader
11
11
{
12
12
public async Task < string > Download ( string uri )
13
13
{
14
- const string plainTextMediaType = "text/plain" ;
15
14
using ( HttpClient client = new HttpClient ( new HttpClientHandler
16
15
{
17
16
// Avoid Deflate due to bugs. For more info, see:
@@ -25,14 +24,22 @@ public async Task<string> Download(string uri)
25
24
26
25
using ( HttpContent content = response . Content )
27
26
{
28
- string mediaType = content . Headers . ContentType . MediaType ;
29
-
30
- if ( string . IsNullOrWhiteSpace ( mediaType ) || mediaType . Equals ( plainTextMediaType , StringComparison . InvariantCultureIgnoreCase ) )
27
+ var mediaType = content . Headers . ContentType . MediaType ? . ToLowerInvariant ( ) . Trim ( ) ;
28
+ switch ( mediaType )
31
29
{
32
- return await content . ReadAsStringAsync ( ) ;
30
+ case null :
31
+ case "" :
32
+ case "text/plain" :
33
+ return await content . ReadAsStringAsync ( ) ;
34
+ case "application/gzip" :
35
+ case "application/x-gzip" :
36
+ using ( var stream = await content . ReadAsStreamAsync ( ) )
37
+ using ( var gzip = new GZipStream ( stream , CompressionMode . Decompress ) )
38
+ using ( var reader = new StreamReader ( gzip ) )
39
+ return await reader . ReadToEndAsync ( ) ;
40
+ default :
41
+ throw new NotSupportedException ( $ "The media type '{ mediaType } ' is not supported when executing a script over http/https") ;
33
42
}
34
-
35
- throw new NotSupportedException ( $ "The media type '{ mediaType } ' is not supported when executing a script over http/https") ;
36
43
}
37
44
}
38
45
}
0 commit comments