Skip to content

Commit

Permalink
Improve packaged source updating (#3657) (#3682)
Browse files Browse the repository at this point in the history
This change refactors the index package updating to enable an optimization; using an HTTP header to determine the available version rather than reading the package contents remotely.  If the header is present, containing a valid package version string, it will be used as the available version.  If not, the existing package content examination will be used.

The refactoring was required to do all package inspection steps after downloading the package, rather than before.  This enables the single version value to be sufficient until we decide to update, and then in the very unlikely event of the package not meeting criteria we will delete it after having downloaded it.
  • Loading branch information
JohnMcPMS authored Sep 27, 2023
1 parent 2eb99f9 commit f600c3b
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 107 deletions.
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

0 comments on commit f600c3b

Please sign in to comment.