﻿using System;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Threading.Tasks;

namespace Dotnet.Script.Core
{
    public class ScriptDownloader
    {
        public async Task<string> Download(string uri)
        {
            using (HttpClient client = new HttpClient())
            {
                using (HttpResponseMessage response = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead))
                {
                    response.EnsureSuccessStatusCode();

                    using (HttpContent content = response.Content)
                    {
                        var mediaType = content.Headers.ContentType.MediaType?.ToLowerInvariant().Trim();
                        switch (mediaType)
                        {
                            case null:
                            case "":
                            case "text/plain":
                                return await content.ReadAsStringAsync();
                            case "application/gzip":
                            case "application/x-gzip":
                                using (var stream = await content.ReadAsStreamAsync())
                                using (var gzip = new GZipStream(stream, CompressionMode.Decompress))
                                using (var reader = new StreamReader(gzip))
                                    return await reader.ReadToEndAsync();
                            default:
                                throw new NotSupportedException($"The media type '{mediaType}' is not supported when executing a script over http/https");
                        }
                    }
                }
            }
        }
    }
}

﻿using System;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Threading.Tasks;

namespace Dotnet.Script.Core
{
    public class ScriptDownloader
    {
        public async Task<string> Download(string uri)
        {
            using (HttpClient client = new HttpClient())
            {
                using (HttpResponseMessage response = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead))
                {
                    response.EnsureSuccessStatusCode();

                    using (HttpContent content = response.Content)
                    {
                        var mediaType = content.Headers.ContentType?.MediaType?.ToLowerInvariant().Trim();
                        switch (mediaType)
                        {
                            case null:
                            case "":
                            case "text/plain":
                                return await content.ReadAsStringAsync();
                            case "application/gzip":
                            case "application/x-gzip":
                                using (var stream = await content.ReadAsStreamAsync())
                                using (var gzip = new GZipStream(stream, CompressionMode.Decompress))
                                using (var reader = new StreamReader(gzip))
                                    return await reader.ReadToEndAsync();
                            default:
                                throw new NotSupportedException($"The media type '{mediaType}' is not supported when executing a script over http/https");
                        }
                    }
                }
            }
        }
    }
}

Support remote scripts with empty/null Content Type
Support remote scripts with empty/null Content Type
