Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry pick 3657 to 1.6 #3682

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ SMTO
sortof
sourceforge
SOURCESDIRECTORY
sourceversion
spamming
SPAPI
Srinivasan
Expand Down
35 changes: 34 additions & 1 deletion src/AppInstallerCommonCore/Downloader.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include "pch.h"
#include "Public/AppInstallerErrors.h"
#include "Public/AppInstallerRuntime.h"
Expand All @@ -17,6 +16,9 @@
using namespace AppInstaller::Runtime;
using namespace AppInstaller::Settings;
using namespace AppInstaller::Filesystem;
using namespace winrt::Windows::Web::Http;
using namespace winrt::Windows::Web::Http::Headers;
using namespace winrt::Windows::Web::Http::Filters;

namespace AppInstaller::Utility
{
Expand Down Expand Up @@ -141,6 +143,37 @@ namespace AppInstaller::Utility
return result;
}

std::map<std::string, std::string> GetHeaders(std::string_view url)
{
AICLI_LOG(Core, Verbose, << "Retrieving headers from url: " << url);

HttpBaseProtocolFilter filter;
filter.CacheControl().ReadBehavior(HttpCacheReadBehavior::MostRecent);

HttpClient client(filter);
client.DefaultRequestHeaders().Connection().Clear();
client.DefaultRequestHeaders().Append(L"Connection", L"close");
client.DefaultRequestHeaders().UserAgent().ParseAdd(Utility::ConvertToUTF16(Runtime::GetDefaultUserAgent().get()));

winrt::Windows::Foundation::Uri uri{ Utility::ConvertToUTF16(url) };
HttpRequestMessage request(HttpMethod::Head(), uri);

HttpResponseMessage response = client.SendRequestAsync(request, HttpCompletionOption::ResponseHeadersRead).get();

THROW_HR_IF(
MAKE_HRESULT(SEVERITY_ERROR, FACILITY_HTTP, response.StatusCode()),
response.StatusCode() != HttpStatusCode::Ok);

std::map<std::string, std::string> result;

for (const auto& header : response.Headers())
{
result.emplace(Utility::ConvertToUTF8(header.Key()), Utility::ConvertToUTF8(header.Value()));
}

return result;
}

std::optional<std::vector<BYTE>> DownloadToStream(
const std::string& url,
std::ostream& dest,
Expand Down
4 changes: 4 additions & 0 deletions src/AppInstallerCommonCore/Public/AppInstallerDownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <wrl/client.h>

#include <filesystem>
#include <map>
#include <optional>
#include <ostream>
#include <string>
Expand Down Expand Up @@ -59,6 +60,9 @@ namespace AppInstaller::Utility
bool computeHash = false,
std::optional<DownloadInfo> info = {});

// Gets the headers for the given URL.
std::map<std::string, std::string> GetHeaders(std::string_view url);

// Determines if the given url is a remote location.
bool IsUrlRemote(std::string_view url);

Expand Down
Loading
Loading