Audacity 3.2.0
CurlResponse.h
Go to the documentation of this file.
1/*!********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file CurlResponse.h
6 @brief Declare an implementation of IResponse using libcurl.
7
8 Dmitry Vedenko
9 **********************************************************************/
10
11#pragma once
12
13#include <deque>
14#include <mutex>
15#include <cstdint>
16
17#include "../IResponse.h"
18
19#include "../HeadersList.h"
20#include "../CookiesList.h"
21#include "../Request.h"
22
23#include "CurlHandleManager.h"
24
25namespace audacity
26{
27namespace network_manager
28{
29
30class MultipartData;
31
32class RequestPayloadStream;
33using RequestPayloadStreamPtr = std::shared_ptr<RequestPayloadStream>;
34
35class CurlResponse final : public IResponse
36{
37public:
39 RequestVerb verb, const Request& request,
40 CurlHandleManager* handleManager) noexcept;
41
42 bool isFinished() const noexcept override;
43 unsigned getHTTPCode() const noexcept override;
44
45 NetworkError getError() const noexcept override;
46 std::string getErrorString() const override;
47
48 bool headersReceived() const noexcept override;
49
50 bool hasHeader(const std::string& headerName) const noexcept override;
51 std::string getHeader(const std::string& headerName) const override;
52
53 const HeadersList& getHeaders() const noexcept override;
54 const CookiesList& getCookies() const noexcept override;
55
56 const Request& getRequest() const noexcept override;
57
58 std::string getURL() const override;
59
60 void abort() noexcept override;
61
62 void setOnDataReceivedCallback(RequestCallback callback) override;
63
64 void setRequestFinishedCallback(RequestCallback callback) override;
65
67 virtual void setDownloadProgressCallback(ProgressCallback callback) override;
69 virtual void setUploadProgressCallback(ProgressCallback callback) override;
70
71 uint64_t getBytesAvailable() const noexcept override;
72 uint64_t readData(void* buffer, uint64_t maxBytesCount) override;
73
75 void setForm(std::unique_ptr<MultipartData> form);
76
77 void perform();
78
79private:
81
84
86
87 mutable std::mutex mCallbackMutex;
92
93 mutable std::mutex mHeadersMutex;
96
97 mutable std::mutex mDataBufferMutex;
98 std::deque<uint8_t> mDataBuffer;
99
100 mutable std::recursive_mutex mStatusMutex;
101
103 std::string mErrorString;
104
105 unsigned mHttpCode { 0 };
106
108 std::unique_ptr<MultipartData> mForm;
109
110 bool mHeadersReceived { false };
111 bool mRequestFinished { false };
112 bool mAbortRequested { false };
113
114 static size_t WriteCallback(const uint8_t* ptr, size_t size, size_t nmemb, CurlResponse* userdata) noexcept;
115 static size_t HeaderCallback(const char* buffer, size_t size, size_t nitems, CurlResponse* userdata) noexcept;
116 static int CurlProgressCallback(CurlResponse* clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) noexcept;
117};
118
119}
120}
Declare a class responsible for reuse of CURL handles.
virtual void setUploadProgressCallback(ProgressCallback callback) override
Set the upload progress callback.
void setForm(std::unique_ptr< MultipartData > form)
virtual void setDownloadProgressCallback(ProgressCallback callback) override
Set the download progress callback.
static size_t WriteCallback(const uint8_t *ptr, size_t size, size_t nmemb, CurlResponse *userdata) noexcept
uint64_t getBytesAvailable() const noexcept override
const CookiesList & getCookies() const noexcept override
std::unique_ptr< MultipartData > mForm
Definition: CurlResponse.h:108
NetworkError getError() const noexcept override
void setPayload(RequestPayloadStreamPtr payload)
const Request & getRequest() const noexcept override
static int CurlProgressCallback(CurlResponse *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) noexcept
bool isFinished() const noexcept override
void setOnDataReceivedCallback(RequestCallback callback) override
unsigned getHTTPCode() const noexcept override
CurlResponse(RequestVerb verb, const Request &request, CurlHandleManager *handleManager) noexcept
std::string getErrorString() const override
bool hasHeader(const std::string &headerName) const noexcept override
std::string getHeader(const std::string &headerName) const override
const HeadersList & getHeaders() const noexcept override
static size_t HeaderCallback(const char *buffer, size_t size, size_t nitems, CurlResponse *userdata) noexcept
CurlHandleManager::Handle * mCurrentHandle
Definition: CurlResponse.h:85
void setRequestFinishedCallback(RequestCallback callback) override
uint64_t readData(void *buffer, uint64_t maxBytesCount) override
bool headersReceived() const noexcept override
std::string getURL() const 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
std::shared_ptr< RequestPayloadStream > RequestPayloadStreamPtr
Definition: CurlResponse.h:33