Skip to content
Draft
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
21 changes: 8 additions & 13 deletions src/AppInstallerCommonCore/HttpStream/HttpClientWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using namespace winrt::Windows::Web::Http::Filters;
// The HRESULTs will be mapped to UI error code by the appropriate component
namespace AppInstaller::Utility::HttpStream
{
std::future<std::shared_ptr<HttpClientWrapper>> HttpClientWrapper::CreateAsync(const Uri& uri)
std::shared_ptr<HttpClientWrapper> HttpClientWrapper::Create(const Uri& uri)
{
// TODO: Use proxy info. HttpClient does not support using a custom proxy, only using the system-wide one.
std::shared_ptr<HttpClientWrapper> instance = std::make_shared<HttpClientWrapper>();
Expand All @@ -37,13 +37,11 @@ namespace AppInstaller::Utility::HttpStream
instance->m_httpClient.DefaultRequestHeaders().Append(L"Connection", L"Keep-Alive");
instance->m_httpClient.DefaultRequestHeaders().UserAgent().ParseAdd(Utility::ConvertToUTF16(Runtime::GetDefaultUserAgent().get()));

co_await instance->PopulateInfoAsync();

co_return instance;
return instance;
}

// this function will issue a HEAD request to determine the size of the file and the redirect URI
std::future<void> HttpClientWrapper::PopulateInfoAsync()
IAsyncAction HttpClientWrapper::PopulateInfoAsync()
{
HttpRequestMessage request(HttpMethod::Head(), m_requestUri);

Expand Down Expand Up @@ -93,7 +91,7 @@ namespace AppInstaller::Utility::HttpStream
#pragma warning( disable : 4714) // HRESULT_FROM_WIN32 marked as forceinline not inlined
#endif

std::future<IBuffer> HttpClientWrapper::SendHttpRequestAsync(
IAsyncOperation<IBuffer> HttpClientWrapper::SendHttpRequestAsync(
_In_ ULONG64 startPosition,
_In_ UINT32 requestedSizeInBytes)
{
Expand Down Expand Up @@ -177,14 +175,11 @@ namespace AppInstaller::Utility::HttpStream
#pragma warning( pop )
#endif

std::future<IBuffer> HttpClientWrapper::DownloadRangeAsync(
IAsyncOperation<IBuffer> HttpClientWrapper::DownloadRangeAsync(
const ULONG64 startPosition,
const UINT32 requestedSizeInBytes,
const InputStreamOptions& options)
const InputStreamOptions&)
{
std::vector<byte> byteArray(requestedSizeInBytes);
IBuffer buffer = CryptographicBuffer::CreateFromByteArray(byteArray);

co_return co_await SendHttpRequestAsync(startPosition, requestedSizeInBytes);
return SendHttpRequestAsync(startPosition, requestedSizeInBytes);
}
}
}
11 changes: 6 additions & 5 deletions src/AppInstallerCommonCore/HttpStream/HttpClientWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ namespace AppInstaller::Utility::HttpStream
class HttpClientWrapper
{
public:
static std::future<std::shared_ptr<HttpClientWrapper>> CreateAsync(const winrt::Windows::Foundation::Uri& uri);
// Create returns an uninitialized wrapper. Callers must co_await PopulateInfoAsync() before use.
static std::shared_ptr<HttpClientWrapper> Create(const winrt::Windows::Foundation::Uri& uri);

std::future<winrt::Windows::Storage::Streams::IBuffer> DownloadRangeAsync(
winrt::Windows::Foundation::IAsyncAction PopulateInfoAsync();

winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::Streams::IBuffer> DownloadRangeAsync(
const ULONG64 startPosition,
const UINT32 requestedSizeInBytes,
const winrt::Windows::Storage::Streams::InputStreamOptions& options);
Expand Down Expand Up @@ -42,9 +45,7 @@ namespace AppInstaller::Utility::HttpStream
std::wstring m_etagHeader;
std::wstring m_lastModifiedHeader;

std::future<void> PopulateInfoAsync();

std::future<winrt::Windows::Storage::Streams::IBuffer> SendHttpRequestAsync(
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::Streams::IBuffer> SendHttpRequestAsync(
_In_ ULONG64 startPosition,
_In_ UINT32 requestedSizeInBytes);
};
Expand Down
6 changes: 3 additions & 3 deletions src/AppInstallerCommonCore/HttpStream/HttpLocalCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using namespace winrt::Windows::Security::Cryptography;
// The HRESULTs will be mapped to UI error code by the appropriate component
namespace AppInstaller::Utility::HttpStream
{
std::future<IBuffer> HttpLocalCache::ReadFromCacheAndDownloadIfNecessaryAsync(
winrt::Windows::Foundation::IAsyncOperation<IBuffer> HttpLocalCache::ReadFromCacheAndDownloadIfNecessaryAsync(
const ULONG64 requestedPosition,
const UINT32 requestedSize,
HttpClientWrapper* httpClientWrapper,
Expand Down Expand Up @@ -145,7 +145,7 @@ namespace AppInstaller::Utility::HttpStream

// Downloads a chunk of the file, saves it to the cache, and returns the corresponding buffer
// If the requested size is 0, this method returns an empty buffer without making HTTP calls
std::future<void> HttpLocalCache::DownloadAndSaveToCacheAsync(
winrt::Windows::Foundation::IAsyncAction HttpLocalCache::DownloadAndSaveToCacheAsync(
const std::vector<ULONG64> unsatisfiablePages,
HttpClientWrapper* httpClientWrapper,
InputStreamOptions httpInputStreamOptions)
Expand Down Expand Up @@ -243,4 +243,4 @@ namespace AppInstaller::Utility::HttpStream
writer.WriteBuffer(buffer2);
return writer.DetachBuffer();
}
}
}
5 changes: 3 additions & 2 deletions src/AppInstallerCommonCore/HttpStream/HttpLocalCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once

#include <winrt/Windows.Foundation.h>
#include "HttpClientWrapper.h"

namespace AppInstaller::Utility::HttpStream
Expand All @@ -23,7 +24,7 @@ namespace AppInstaller::Utility::HttpStream

// Returns a buffer matching the requested range by reading the parts of the range that are cached
// and downloading the rest using the provided httpClientWrapper object
std::future<winrt::Windows::Storage::Streams::IBuffer> ReadFromCacheAndDownloadIfNecessaryAsync(
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::Streams::IBuffer> ReadFromCacheAndDownloadIfNecessaryAsync(
const ULONG64 requestedPosition,
const UINT32 requestedSize,
HttpClientWrapper* httpClientWrapper,
Expand All @@ -47,7 +48,7 @@ namespace AppInstaller::Utility::HttpStream

void VacateStaleEntriesFromCache();

std::future<void> DownloadAndSaveToCacheAsync(
winrt::Windows::Foundation::IAsyncAction DownloadAndSaveToCacheAsync(
const std::vector<ULONG64> unsatisfiablePages,
HttpClientWrapper* httpClientWrapper,
const winrt::Windows::Storage::Streams::InputStreamOptions httpInputStreamOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ namespace AppInstaller::Utility::HttpStream

try
{
strong_this->m_httpHelper = co_await HttpClientWrapper::CreateAsync(uri);
auto httpHelper = HttpClientWrapper::Create(uri);
co_await httpHelper->PopulateInfoAsync();
strong_this->m_httpHelper = std::move(httpHelper);
strong_this->m_size = strong_this->m_httpHelper->GetFullFileSize();
strong_this->m_httpLocalCache = std::make_unique<HttpLocalCache>();
}
Expand Down
4 changes: 4 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<PlatformToolset Condition="'$(VisualStudioVersion)' == '17.0'">v143</PlatformToolset>
<PlatformToolset Condition="'$(VisualStudioVersion)' == '18.0'">v145</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<!-- Disable /await from CppWinRT. Can be removed when https://github.com/microsoft/cppwinrt/pull/1521 comes through (looks like it will be 3.0+). -->
<CppWinRTEnableLegacyCoroutines>false</CppWinRTEnableLegacyCoroutines>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(WinGetMacros)' != ''">
<ClCompile>
Expand All @@ -26,6 +28,8 @@
<ItemDefinitionGroup>
<ClCompile>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<!-- Enable /await:strict. Can be removed when https://github.com/microsoft/cppwinrt/pull/1521 comes through (looks like it will be 3.0+). -->
<AdditionalOptions>%(AdditionalOptions) /await:strict</AdditionalOptions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)'=='ReleaseStatic'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ namespace ConfigurationShim

if (IsConfigurationAvailable())
{
return;
co_return;
}

auto strong_this{ get_strong() };
Expand Down
Loading