Audacity 3.2.0
CurlResponseFactory.cpp
Go to the documentation of this file.
1/*!********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file CurlResponseFactory.cpp
6 @brief Define an implementation of IResponseFactory using libcurl.
7
8 Dmitry Vedenko
9 **********************************************************************/
10#include "CurlResponseFactory.h"
11
12#include "CurlResponse.h"
13#include "MultipartData.h"
14#include "RequestPayload.h"
15
16#include "ThreadPool/ThreadPool.h"
17
18namespace audacity
19{
20namespace network_manager
21{
22namespace
23{
24class StubResponse final : public IResponse
25{
26public:
27 explicit StubResponse(const Request& request)
28 : mRequest { request }
29 {
30 }
31
32 bool isFinished() const noexcept override
33 {
34 return true;
35 }
36
37 unsigned getHTTPCode() const noexcept override
38 {
39 return 0;
40 }
41
42 NetworkError getError() const noexcept override
43 {
45 }
46
47 std::string getErrorString() const override
48 {
49 return {};
50 }
51
52 bool headersReceived() const noexcept override
53 {
54 return false;
55 }
56
57 bool hasHeader(const std::string& headerName) const noexcept override
58 {
59 return false;
60 }
61
62 std::string getHeader(const std::string& headerName) const override
63 {
64 return {};
65 }
66
67 const HeadersList& getHeaders() const noexcept override
68 {
69 static HeadersList empty;
70 return empty;
71 }
72
73 const CookiesList& getCookies() const noexcept override
74 {
75 static CookiesList empty;
76 return empty;
77 }
78
79 const Request& getRequest() const noexcept override
80 {
81 return mRequest;
82 }
83
84 std::string getURL() const override
85 {
86 return {};
87 }
88
89 void abort() noexcept override
90 {
91 }
92
94 {
95 }
96
98 {
99 if (callback)
100 callback(this);
101 }
102
104 {
105 }
106
108 {
109 }
110
111 uint64_t getBytesAvailable() const noexcept override
112 {
113 return 0;
114 }
115
116 uint64_t readData(void*, uint64_t) override
117 {
118 return 0;
119 }
120
121private:
123};
124} // namespace
125
126constexpr decltype(std::thread::hardware_concurrency ()) MIN_CURL_THREADS = 6;
127
129 : mThreadPool (std::make_unique<ThreadPool>(
130 std::max (
132 std::thread::hardware_concurrency ()
133 )))
134{
135
136}
137
139{
140}
141
142void CurlResponseFactory::setProxy (const std::string& proxy)
143{
144 mHandleManager->setProxy (proxy);
145}
146
148{
149 return performRequest(verb, request, RequestPayloadStreamPtr {});
150}
151
153 RequestVerb verb, const Request& request,
154 RequestPayloadStreamPtr payloadStream)
155{
156 if (!mThreadPool)
157 return std::make_shared<StubResponse>(request);
158
159 auto response =
160 std::make_shared<CurlResponse>(verb, request, mHandleManager.get());
161
162 mThreadPool->enqueue(
163 [response, payloadStream = std::move(payloadStream)]()
164 {
165 if (payloadStream)
166 response->setPayload(payloadStream);
167
168 response->perform();
169 });
170
171 return response;
172}
173
175 RequestVerb verb, const Request& request,
176 std::unique_ptr<MultipartData> form)
177{
178 if (!mThreadPool)
179 return std::make_shared<StubResponse>(request);
180
181 std::shared_ptr<CurlResponse> response =
182 std::make_shared<CurlResponse>(verb, request, mHandleManager.get());
183
184
185 mThreadPool->enqueue(
186 [response, rawForm = form.release()]() mutable
187 {
188 if (rawForm != nullptr && !rawForm->IsEmpty())
189 response->setForm(std::unique_ptr<MultipartData>(rawForm));
190
191 response->perform();
192 });
193
194 return response;
195}
196
198{
199 mThreadPool.reset ();
200 mHandleManager.reset ();
201}
202
203}
204}
Declare an implementation of IResponse using libcurl.
Declare an implementation of IResponseFactory using libcurl.
Declare a class for constructing HTTP requests.
ResponsePtr performRequest(RequestVerb verb, const Request &request) override
std::unique_ptr< CurlHandleManager > mHandleManager
void setProxy(const std::string &proxy) override
Interface, that provides access to the data from the HTTP response.
Definition: IResponse.h:113
std::function< void(int64_t current, int64_t expected)> ProgressCallback
Definition: IResponse.h:118
std::function< void(IResponse *)> RequestCallback
Definition: IResponse.h:115
void setUploadProgressCallback(ProgressCallback) override
Set the upload progress callback.
void setDownloadProgressCallback(ProgressCallback) override
Set the download progress callback.
std::shared_ptr< IResponse > ResponsePtr
std::shared_ptr< RequestPayloadStream > RequestPayloadStreamPtr
Definition: CurlResponse.h:33
constexpr decltype(std::thread::hardware_concurrency()) MIN_CURL_THREADS
STL namespace.