22#if !defined(A9010DA64FD34372A24071A29FA59EEC)
23#define A9010DA64FD34372A24071A29FA59EEC
25#if defined(HAVE_LIBCURL)
29#include <unordered_map>
33#include <miktex/Core/Session>
34#include <miktex/Trace/Trace>
35#include <miktex/Trace/TraceStream>
37#include "WebSession.h"
40MPM_INTERNAL_BEGIN_NAMESPACE;
43 public std::enable_shared_from_this<CurlWebSession>,
47 CurlWebSession(IProgressNotify_* callback);
50 ~CurlWebSession()
override;
53 std::unique_ptr<WebFile> OpenUrl(
const std::string& url)
override
55 return OpenUrl(url, {});
59 std::unique_ptr<WebFile> OpenUrl(
const std::string& url,
const std::unordered_map<std::string, std::string>& formData)
override;
62 void Dispose()
override;
71 void ReadInformationals();
74 std::string GetCurlErrorString(CURLMcode code)
const
76#if LIBCURL_VERSION_NUM >= 0x70c00
77 if (curlVersionInfo->version_num >= 0x70c00)
79 return curl_multi_strerror(code);
82 std::string str = T_(
"The cURL multi interface returned an error code: ");
83 str += std::to_string(code);
88 void FatalCurlError(CURLMcode code)
const
90 MIKTEX_FATAL_ERROR(GetCurlErrorString(code));
94 void ExpectOK(CURLMcode code)
const
103 CURL* pCurl =
nullptr;
106 CURLM* pCurlm =
nullptr;
109 int runningHandles = -1;
114 return runningHandles == 0;
118 CURL* GetEasyHandle()
const throw ()
124 CURLM* GetMultiHandle()
const throw ()
130 std::string GetCurlErrorString(CURLcode code)
const
132#if LIBCURL_VERSION_NUM >= 0x70c00
133 if (curlVersionInfo->version_num >= 0x70c00)
135 return curl_easy_strerror(code);
140 std::string str = T_(
"The cURL easy interface returned an error code: ");
141 str += std::to_string(code);
147 void FatalCurlError(CURLcode code,
const char* effectiveUrl)
const;
150 void ExpectOK(CURLcode code,
const char* effectiveUrl)
const
152 if (code != CURLE_OK)
154 FatalCurlError(code, effectiveUrl);
159 template<
class T>
void SetOption(CURLoption option, T val)
const
161 CURLcode code = curl_easy_setopt(pCurl, option, val);
162 if (code != CURLE_OK)
164 MIKTEX_FATAL_ERROR_2(GetCurlErrorString(code),
"option", std::to_string(option));
169 std::string UrlEncode(
const std::string& s)
const
171 char* encoded = curl_easy_escape(pCurl, s.c_str(),
static_cast<int>(s.length()));
172 std::string result = encoded;
178 void SetCustomHeaders(
const std::unordered_map<std::string, std::string>& headers)
override;
184 static int ProgressCallback(
void* pv,
double dltotal,
double dlnow,
double ultotal,
double ulnow);
187 static int DebugCallback(CURL* pCurl, curl_infotype infoType,
char* pData, std::size_t sizeData,
void* pv);
190 std::string proxyPort;
193 std::string userPassword;
196 struct curl_slist* headers =
nullptr;
199 curl_version_info_data* curlVersionInfo =
nullptr;
202 std::unique_ptr<MiKTeX::Trace::TraceStream> trace_curl;
205 std::unique_ptr<MiKTeX::Trace::TraceStream> trace_mpm;
208 std::shared_ptr<MiKTeX::Core::Session> session = MIKTEX_SESSION();
211MPM_INTERNAL_END_NAMESPACE;