winget-cli

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

CurlConnectionManager.cpp (1824B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 
      4 #include "CurlConnectionManager.h"
      5 
      6 #include "../ErrorHandling.h"
      7 #include "CurlConnection.h"
      8 
      9 #include <curl/curl.h>
     10 
     11 using namespace SFS;
     12 using namespace SFS::details;
     13 
     14 namespace
     15 {
     16 // Curl recommends checking for expected features in runtime
     17 void CheckCurlFeatures(const ReportingHandler& handler)
     18 {
     19     curl_version_info_data* ver = curl_version_info(CURLVERSION_NOW);
     20     THROW_CODE_IF_NOT_LOG(HttpUnexpected, ver, handler);
     21 
     22     THROW_CODE_IF_NOT_LOG(HttpUnexpected, (ver->features & CURL_VERSION_SSL), handler, "Curl was not built with SSL");
     23     THROW_CODE_IF_NOT_LOG(HttpUnexpected,
     24                           (ver->features & CURL_VERSION_THREADSAFE),
     25                           handler,
     26                           "Curl is not thread safe");
     27 
     28     // For thread safety we need the DNS resolutions to be asynchronous (which happens because of c-ares)
     29     THROW_CODE_IF_NOT_LOG(HttpUnexpected,
     30                           (ver->features & CURL_VERSION_ASYNCHDNS),
     31                           handler,
     32                           "Curl was not built with async DNS resolutions");
     33 }
     34 } // namespace
     35 
     36 CurlConnectionManager::CurlConnectionManager(const ReportingHandler& handler) : ConnectionManager(handler)
     37 {
     38     THROW_CODE_IF_NOT_LOG(HttpUnexpected,
     39                           curl_global_init(CURL_GLOBAL_ALL) == CURLE_OK,
     40                           m_handler,
     41                           "Curl failed to initialize");
     42     CheckCurlFeatures(m_handler);
     43 }
     44 
     45 CurlConnectionManager::~CurlConnectionManager()
     46 {
     47     curl_global_cleanup();
     48 }
     49 
     50 std::unique_ptr<Connection> CurlConnectionManager::MakeConnection(const ConnectionConfig& config)
     51 {
     52     return std::make_unique<CurlConnection>(config, m_handler);
     53 }