winget-cli

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

AppInstallerDownloader.h (4867B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #pragma once
      4 #include <AppInstallerErrors.h>
      5 #include <AppInstallerProgress.h>
      6 
      7 #include <chrono>
      8 #include <filesystem>
      9 #include <map>
     10 #include <optional>
     11 #include <ostream>
     12 #include <string>
     13 #include <string_view>
     14 #include <vector>
     15 
     16 using namespace std::chrono_literals;
     17 
     18 namespace AppInstaller::Utility
     19 {
     20     // The type of data being downloaded; determines what code should
     21     // be used when downloading.
     22     enum class DownloadType
     23     {
     24         Index,
     25         Manifest,
     26         WinGetUtil,
     27         Installer,
     28         InstallerMetadataCollectionInput,
     29         ConfigurationFile,
     30     };
     31 
     32     struct DownloadRequestHeader
     33     {
     34         std::string Name;
     35         std::string Value;
     36         bool IsAuth = false;
     37     };
     38 
     39     // Extra metadata about a download for use by certain downloaders (Delivery Optimization for instance).
     40     // Extra download request headers.
     41     struct DownloadInfo
     42     {
     43         std::string DisplayName;
     44         std::string ContentId;
     45         std::vector<DownloadRequestHeader> RequestHeaders;
     46     };
     47 
     48     // Properties about the downloaded file.
     49     struct DownloadResult
     50     {
     51         std::vector<BYTE> Sha256Hash;
     52         uint64_t SizeInBytes = 0;
     53         std::optional<std::string> ContentType;
     54     };
     55 
     56     // An exception that indicates that a remote service is too busy/unavailable and may contain data on when to try again.
     57     struct ServiceUnavailableException : public wil::ResultException
     58     {
     59         ServiceUnavailableException(std::chrono::seconds retryAfter = 0s) : wil::ResultException(APPINSTALLER_CLI_ERROR_SERVICE_UNAVAILABLE), m_retryAfter(retryAfter) {}
     60 
     61         std::chrono::seconds RetryAfter() const { return m_retryAfter; }
     62 
     63     private:
     64         std::chrono::seconds m_retryAfter;
     65     };
     66 
     67     // Downloads a file from the given URL and places it in the given location.
     68     //   url: The url to be downloaded from. http->https redirection is allowed.
     69     //   dest: The stream to be downloaded to.
     70     //   computeHash: Optional. Indicates if SHA256 hash should be calculated when downloading.
     71     //   downloadInfo: Optional. Currently only used by DO to identify the download.
     72     DownloadResult DownloadToStream(
     73         const std::string& url,
     74         std::ostream& dest,
     75         DownloadType type,
     76         IProgressCallback& progress,
     77         std::optional<DownloadInfo> downloadInfo = {});
     78 
     79     // Downloads a file from the given URL and places it in the given location.
     80     //   url: The url to be downloaded from. http->https redirection is allowed.
     81     //   dest: The path to local file to be downloaded to.
     82     //   computeHash: Optional. Indicates if SHA256 hash should be calculated when downloading.
     83     //   downloadInfo: Optional. Currently only used by DO to identify the download.
     84     DownloadResult Download(
     85         const std::string& url,
     86         const std::filesystem::path& dest,
     87         DownloadType type,
     88         IProgressCallback& progress,
     89         std::optional<DownloadInfo> downloadInfo = {});
     90 
     91     // Gets the headers for the given URL.
     92     std::map<std::string, std::string> GetHeaders(std::string_view url);
     93 
     94     // Determines if the given url is a remote location.
     95     bool IsUrlRemote(std::string_view url);
     96 
     97     // Determines if the given url is secured.
     98     bool IsUrlSecure(std::string_view url);
     99 
    100     // Apply Mark of the web if the target file is on NTFS, otherwise does nothing.
    101     void ApplyMotwIfApplicable(const std::filesystem::path& filePath, URLZONE zone);
    102 
    103     // Remove Mark of the web if the target file is on NTFS, otherwise does nothing.
    104     void RemoveMotwIfApplicable(const std::filesystem::path& filePath);
    105 
    106     // Apply Mark of the web using IAttachmentExecute::Save if the target file is on NTFS, otherwise does nothing.
    107     // This method only does a best effort since Attachment Execution Service may be disabled.
    108     // If IAttachmentExecute::Save is successfully invoked and the scan failed, the failure HRESULT is returned.
    109     // zoneIfScanFailure: URLZONE to apply if IAttachmentExecute::Save scan failed.
    110     HRESULT ApplyMotwUsingIAttachmentExecuteIfApplicable(const std::filesystem::path& filePath, const std::string& source, URLZONE zoneIfScanFailure);
    111 
    112     // Function to read-only create a stream from a uri string (url address or file system path)
    113     ::Microsoft::WRL::ComPtr<IStream> GetReadOnlyStreamFromURI(std::string_view uriStr);
    114 
    115     // Gets the retry after value in terms of a delay in seconds.
    116     std::chrono::seconds GetRetryAfter(const std::wstring& retryAfter);
    117 
    118     // Gets the retry after value in terms of a delay in seconds.
    119     std::chrono::seconds GetRetryAfter(const winrt::Windows::Web::Http::HttpResponseMessage& response);
    120 }