Audacity 3.2.0
CurlHandleManager.h
Go to the documentation of this file.
1/*!********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file CurlHandleManager.h
6 @brief Declare a class responsible for reuse of CURL handles.
7
8 Dmitry Vedenko
9 **********************************************************************/
10
11#pragma once
12
13#include <string>
14#include <chrono>
15#include <vector>
16#include <mutex>
17
18#include <curl/curl.h>
19
20#include "CurlStringList.h"
21#include "../IResponseFactory.h"
22
23#include "../CookiesList.h"
24#include "../HeadersList.h"
25
26namespace audacity
27{
28namespace network_manager
29{
30
32{
33public:
34 class Handle final
35 {
36 friend class CurlHandleManager;
37 Handle (CurlHandleManager* owner, CURL* handle, RequestVerb verb, std::string url) noexcept;
38 public:
39 Handle (Handle&& rhs) noexcept;
40 ~Handle () noexcept;
41
42 Handle& operator = (Handle&& rhs) noexcept;
43
44 template<typename... Args>
45 CURLcode setOption (CURLoption option, Args... value) noexcept
46 {
47 return curl_easy_setopt (mHandle, option, value...);
48 }
49
50 CURLcode setOption (CURLoption option, const std::string& value) noexcept;
51
52 CURLcode appendCookie (const Cookie& cookie) noexcept;
53 CURLcode appendCookies (const CookiesList& cookie) noexcept;
54
55 void appendHeader (const Header& header);
56 void appendHeaders (const HeadersList& headers);
57
58 struct Result final
59 {
60 CURLcode Code;
61 std::string Message;
62 };
63
64 Result perform ();
65
66 void markKeepAlive ();
67
68 bool isHandleFromCache () const noexcept;
69
70 unsigned getHTTPCode () const noexcept;
71
72 void reset () noexcept;
73
74 CURL* getCurlHandle() const noexcept;
75
78 private:
79 CURL* mHandle { nullptr };
81
83 std::string mUrl;
84
86
87 bool mUserAgentSet { false };
88 bool mReuse { false };
89 bool mHandleFromCache { false };
90 };
91
94
95 void setProxy (std::string proxy);
96
97 Handle getHandle (RequestVerb verb, const std::string& url);
98private:
99 using RequestClock = std::chrono::steady_clock;
100 using RequestTimePoint = RequestClock::time_point;
101
102 static constexpr std::chrono::milliseconds KEEP_ALIVE_IDLE { std::chrono::seconds (120) };
103 static constexpr std::chrono::milliseconds KEEP_ALIVE_PROBE { std::chrono::seconds (60) };
104
105 struct CachedHandle final
106 {
108 std::string SchemeAndDomain;
109
110 CURL* Handle { nullptr };
111
113 };
114
115 std::string getUserAgent () const;
116
117 CURL* getCurlHandleFromCache (RequestVerb verb, const std::string& url);
118 void cacheHandle (Handle& handle);
119
120 void cleanupHandlesCache ();
121
122 static std::string GetSchemeAndDomain (const std::string& url);
123
124 std::string mProxy;
125 std::string mUserAgent;
126
128 std::vector<CachedHandle> mHandleCache;
129};
130
131}
132}
Declare a RAII wrapper for the curl_slist.
Handle(CurlHandleManager *owner, CURL *handle, RequestVerb verb, std::string url) noexcept
CURLcode appendCookie(const Cookie &cookie) noexcept
CURLcode appendCookies(const CookiesList &cookie) noexcept
CURLcode setOption(CURLoption option, Args... value) noexcept
static constexpr std::chrono::milliseconds KEEP_ALIVE_PROBE
Handle getHandle(RequestVerb verb, const std::string &url)
CURL * getCurlHandleFromCache(RequestVerb verb, const std::string &url)
static std::string GetSchemeAndDomain(const std::string &url)
static constexpr std::chrono::milliseconds KEEP_ALIVE_IDLE