8000 Allow running gzip-ped script from URL · dotnet-script/dotnet-script@aea2f7b · GitHub
[go: up one dir, main page]

Skip to content

Commit aea2f7b

Browse files
committed
Allow running gzip-ped script from URL
1 parent 7a798a5 commit aea2f7b

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/Dotnet.Script.Core/ScriptDownloader.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.IO;
3+
using System.IO.Compression;
34
using System.Net.Http;
4-
using System.Net.Mime;
55
using System.Threading.Tasks;
66

77
namespace Dotnet.Script.Core
@@ -10,7 +10,6 @@ public class ScriptDownloader
1010
{
1111
public async Task<string> Download(string uri)
1212
{
13-
const string plainTextMediaType = "text/plain";
1413
using (HttpClient client = new HttpClient())
1514
{
1615
using (HttpResponseMessage response = await client.GetAsync(uri))
@@ -19,14 +18,22 @@ public async Task<string> Download(string uri)
1918

2019
using (HttpContent content = response.Content)
2120
{
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)
2523
{
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");
2736
}
28-
29-
throw new NotSupportedException($"The media type '{mediaType}' is not supported when executing a script over http/https");
3037
}
3138
}
3239
}

0 commit comments

Comments
 (0)
0