Skip to content

Commit d0074d0

Browse files
committed
Initial commit. Product version 1.0.0.
1 parent 35f40b2 commit d0074d0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+6541
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<PropertyGroup>
11+
<NoWarn>1701;1702;CS8981</NoWarn>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="HtmlAgilityPack" Version="1.11.57" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<None Include="ManualCheck\**">
20+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
21+
</None>
22+
<Compile Remove="ManualCheck\**" />
23+
<Content Remove="ManualCheck\**" />
24+
<EmbeddedResource Remove="ManualCheck\**" />
25+
</ItemGroup>
26+
27+
<ItemGroup>
28+
<None Include="ExtraLicenses\**">
29+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
30+
</None>
31+
<Compile Remove="ExtraLicenses\**" />
32+
<Content Remove="ExtraLicenses\**" />
33+
<EmbeddedResource Remove="ExtraLicenses\**" />
34+
</ItemGroup>
35+
36+
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
37+
<Exec Command="xcopy /E /Y /d &quot;$(ProjectDir)ExtraLicenses\&quot; &quot;$(TargetDir)ExtraLicenses\&quot;" />
38+
</Target>
39+
40+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.7.34221.43
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Crossbill.LicenseNoticeAggregator", "Crossbill.LicenseNoticeAggregator.csproj", "{063B25B0-7095-4834-8147-79B4D7DCE8A4}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{063B25B0-7095-4834-8147-79B4D7DCE8A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{063B25B0-7095-4834-8147-79B4D7DCE8A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{063B25B0-7095-4834-8147-79B4D7DCE8A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{063B25B0-7095-4834-8147-79B4D7DCE8A4}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {C097D1BA-6B7F-4E1C-A2A3-CD505FB24E52}
24+
EndGlobalSection
25+
EndGlobal

Downloader.cs

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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

Comments
 (0)