Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions src/WingetCreateCore/Common/PackageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,7 @@ public static class PackageParser
"nullsoft",
};

private static HttpClient httpClient = new()
{
DefaultRequestHeaders =
{
UserAgent = { new ProductInfoHeaderValue("WinGetCreate", Utils.GetEntryAssemblyVersion()) },
},
};
private static HttpClient httpClient = CreateHttpClient();

private enum MachineType
{
Expand Down Expand Up @@ -85,6 +79,33 @@ public static void SetHttpMessageHandler(HttpMessageHandler httpMessageHandler)
{
httpClient.Dispose();
httpClient = httpMessageHandler != null ? new HttpClient(httpMessageHandler) : new HttpClient();
SetDefaultHeaders(httpClient);
}

private static HttpClient CreateHttpClient()
{
var client = new HttpClient();
SetDefaultHeaders(client);
return client;
}

private static void SetDefaultHeaders(HttpClient client)
{
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("WinGetCreate", Utils.GetEntryAssemblyVersion()));

// Add Authorization header from environment variable, if present
string authToken = Environment.GetEnvironmentVariable("WINGET_CREATE_HTTP_CLIENT_AUTH_TOKEN");
if (!string.IsNullOrEmpty(authToken))
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);
}

// Add Accept header from environment variable, if present
string acceptHeader = Environment.GetEnvironmentVariable("WINGET_CREATE_HTTP_CLIENT_ACCEPT_HEADER");
if (!string.IsNullOrEmpty(acceptHeader))
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(acceptHeader));
}
}

/// <summary>
Expand Down