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