|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Net.Http.Headers; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +namespace Crossbill.LicenseNoticeAggregator |
| 9 | +{ |
| 10 | + public static class Downloader |
| 11 | + { |
| 12 | + public static MemoryStream DownloadLicense(string apiUrl, bool isIgnoreErrors) |
| 13 | + { |
| 14 | + using (HttpClientHandler handler = new HttpClientHandler()) |
| 15 | + { |
| 16 | + handler.AllowAutoRedirect = true; |
| 17 | + using (HttpClient client = GetClient(handler)) |
| 18 | + { |
| 19 | + // List data response. |
| 20 | + HttpResponseMessage response = client.GetAsync(apiUrl).Result; // Blocking call! |
| 21 | + if (response.IsSuccessStatusCode) |
| 22 | + { |
| 23 | + MemoryStream memory = new MemoryStream(); |
| 24 | + using (var stream = response.Content.ReadAsStreamAsync().Result) |
| 25 | + { |
| 26 | + CopyStream(stream, memory); |
| 27 | + } |
| 28 | + memory.Position = 0; |
| 29 | + return memory; |
| 30 | + } |
| 31 | + else if (isIgnoreErrors) |
| 32 | + { |
| 33 | + return null; |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + var error = GetError(apiUrl, response); |
| 38 | + if (error == null) |
| 39 | + { |
| 40 | + return null; |
| 41 | + } |
| 42 | + throw error; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private static void CopyStream(Stream input, Stream output) |
| 49 | + { |
| 50 | + if (input == null) |
| 51 | + { |
| 52 | + throw new ArgumentNullException(nameof(input)); |
| 53 | + } |
| 54 | + if (output == null) |
| 55 | + { |
| 56 | + throw new ArgumentNullException(nameof(output)); |
| 57 | + } |
| 58 | + |
| 59 | + byte[] buffer = new byte[8 * 1024]; |
| 60 | + int len; |
| 61 | + while ((len = input.Read(buffer, 0, buffer.Length)) > 0) |
| 62 | + { |
| 63 | + output.Write(buffer, 0, len); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private static HttpClient GetClient(HttpClientHandler handler) |
| 68 | + { |
| 69 | + HttpClient client = new HttpClient(handler); |
| 70 | + client.Timeout = TimeSpan.FromMinutes(5); |
| 71 | + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain")); |
| 72 | + return client; |
| 73 | + } |
| 74 | + |
| 75 | + private static Exception FormatError(string message, string url, string response) |
| 76 | + { |
| 77 | + Exception inner = null; |
| 78 | + if (message != null) |
| 79 | + { |
| 80 | + inner = new Exception(message); |
| 81 | + } |
| 82 | + var ex = new Exception("Remote server call failed.", inner); |
| 83 | + if (!String.IsNullOrEmpty(url)) |
| 84 | + { |
| 85 | + ex.Data.Add("RemoteUrl", url); |
| 86 | + } |
| 87 | + if (!String.IsNullOrEmpty(response)) |
| 88 | + { |
| 89 | + ex.Data.Add("Response", response); |
| 90 | + } |
| 91 | + return ex; |
| 92 | + } |
| 93 | + |
| 94 | + private static Exception GetError(string apiUrl, HttpResponseMessage response) |
| 95 | + { |
| 96 | + Exception ex = null; |
| 97 | + if (response != null && response.StatusCode == System.Net.HttpStatusCode.Unauthorized) |
| 98 | + { |
| 99 | + ex = new UnauthorizedAccessException(response.ReasonPhrase); |
| 100 | + } |
| 101 | + else if (response != null && response.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable) |
| 102 | + { |
| 103 | + ex = new Exception(response.ReasonPhrase); |
| 104 | + } |
| 105 | + |
| 106 | + if (ex != null) |
| 107 | + { |
| 108 | + if (!String.IsNullOrEmpty(apiUrl)) |
| 109 | + { |
| 110 | + ex.Data.Add("RemoteUrl", apiUrl); |
| 111 | + } |
| 112 | + throw ex; |
| 113 | + } |
| 114 | + |
| 115 | + string result = null; |
| 116 | + if (response != null && response.Content != null) |
| 117 | + { |
| 118 | + result = response.Content.ReadAsStringAsync().Result; |
| 119 | + } |
| 120 | + |
| 121 | + return FormatError(response.ReasonPhrase, apiUrl, result); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments