winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

HttpRandomAccessStream.cpp (2970B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 
      4 #include "pch.h"
      5 #include "HttpRandomAccessStream.h"
      6 #include "Public/AppInstallerDownloader.h"
      7 
      8 using namespace winrt::Windows::Foundation;
      9 using namespace winrt::Windows::Storage::Streams;
     10 
     11 // Note: the HttpRandomAccessStream is passed to the AppxPackaging COM API
     12 // All exceptions thrown across dll boundaries should be WinRT exception not custom exceptions.
     13 // The HRESULTs will be mapped to UI error code by the appropriate component
     14 namespace AppInstaller::Utility::HttpStream
     15 {
     16     IAsyncOperation<IRandomAccessStream> HttpRandomAccessStream::InitializeAsync(const Uri& uri)
     17     {
     18         auto strong_this{ get_strong() };
     19 
     20         try
     21         {
     22             strong_this->m_httpHelper = co_await HttpClientWrapper::CreateAsync(uri);
     23             strong_this->m_size = strong_this->m_httpHelper->GetFullFileSize();
     24             strong_this->m_httpLocalCache = std::make_unique<HttpLocalCache>();
     25         }
     26         catch (const ServiceUnavailableException& e)
     27         {
     28             strong_this->m_retryAfter = e.RetryAfter();
     29             throw;
     30         }
     31 
     32         co_return strong_this.as<IRandomAccessStream>();
     33     }
     34 
     35     uint64_t HttpRandomAccessStream::Size() const
     36     {
     37         return m_size;
     38     }
     39 
     40     void HttpRandomAccessStream::Size(uint64_t value)
     41     {
     42         UNREFERENCED_PARAMETER(value);
     43         THROW_HR(E_NOTIMPL);
     44     }
     45 
     46     uint64_t HttpRandomAccessStream::Position() const
     47     {
     48         return m_requestedPosition;
     49     }
     50 
     51     bool HttpRandomAccessStream::CanRead() const
     52     {
     53         return true;
     54     }
     55 
     56     bool HttpRandomAccessStream::CanWrite() const
     57     {
     58         return false;
     59     }
     60 
     61     IInputStream HttpRandomAccessStream::GetInputStreamAt(uint64_t position) const
     62     {
     63         UNREFERENCED_PARAMETER(position);
     64         THROW_HR(E_NOTIMPL);
     65     }
     66 
     67     IOutputStream HttpRandomAccessStream::GetOutputStreamAt(uint64_t position) const
     68     {
     69         UNREFERENCED_PARAMETER(position);
     70         THROW_HR(E_NOTIMPL);
     71     }
     72 
     73     IRandomAccessStream HttpRandomAccessStream::CloneStream() const
     74     {
     75         THROW_HR(E_NOTIMPL);
     76     }
     77 
     78     void HttpRandomAccessStream::Seek(uint64_t position)
     79     {
     80         m_requestedPosition = position;
     81     }
     82 
     83     IAsyncOperationWithProgress<IBuffer, uint32_t> HttpRandomAccessStream::ReadAsync(
     84         IBuffer buffer,
     85         uint32_t count,
     86         InputStreamOptions options)
     87     {
     88         IBuffer result = co_await m_httpLocalCache->ReadFromCacheAndDownloadIfNecessaryAsync(
     89             m_requestedPosition,
     90             count,
     91             m_httpHelper.get(),
     92             options);
     93         winrt::check_hresult(ULong64Add(m_requestedPosition, result.Length(), &m_requestedPosition));
     94 
     95         co_return result;
     96     }
     97 
     98     std::chrono::seconds HttpRandomAccessStream::RetryAfter() const
     99     {
    100         return m_retryAfter;
    101     }
    102 }