Audacity 3.2.0
IResponse.h
Go to the documentation of this file.
1/*!********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file IResponse.h
6 @brief Declare an interface for HTTP response.
7
8 Dmitry Vedenko
9 **********************************************************************/
10
11#pragma once
12
13#include <string>
14#include <cstdint>
15#include <vector>
16#include <functional>
17
18#include "NetworkManagerApi.h"
19
21
22namespace audacity
23{
24namespace network_manager
25{
26
27class Request;
28class HeadersList;
29class CookiesList;
30
31enum class NetworkError
32{
33 NoError,
34 BadURL,
35 ConnectionFailed,
39 Timeout,
45 UnknownError,
47};
48
49namespace HttpCode
50{
51enum
52{
53 Continue = 100,
57
58 OK = 200,
59 Created = 201,
60 Accepted = 202,
62 NoContent = 204,
65
68 Found = 302,
69 SeeOther = 303,
73
77 Forbidden = 403,
78 NotFound = 404,
82 Conflict = 409,
83 Gone = 410,
92 Locked = 423,
94 TooEarly = 425,
99
108} // namespace HttpCode
109
110
113{
114public:
115 using RequestCallback = std::function<void (IResponse*)>;
118 using ProgressCallback = std::function<void(int64_t current, int64_t expected)>;
119
120 virtual ~IResponse () = default;
121
122 virtual bool isFinished () const noexcept = 0;
123
124 virtual unsigned getHTTPCode () const noexcept = 0;
125
126 virtual NetworkError getError () const noexcept = 0;
127 virtual std::string getErrorString () const = 0;
128
129 virtual bool headersReceived () const noexcept = 0;
130
131 virtual bool hasHeader (const std::string& headerName) const noexcept = 0;
132 virtual std::string getHeader (const std::string& headerName) const = 0;
133
134 virtual const HeadersList& getHeaders () const noexcept = 0;
135 virtual const CookiesList& getCookies () const noexcept = 0;
136
137 virtual const Request& getRequest () const noexcept = 0;
138 virtual std::string getURL () const = 0;
139
140 virtual void abort () noexcept = 0;
141
142 virtual void setOnDataReceivedCallback (RequestCallback callback) = 0;
143 virtual void setRequestFinishedCallback (RequestCallback callback) = 0;
144
146 virtual void setDownloadProgressCallback(ProgressCallback callback) = 0;
148 virtual void setUploadProgressCallback(ProgressCallback callback) = 0;
149
150 // The total bytes available to read by readData
151 virtual uint64_t getBytesAvailable () const noexcept = 0;
152
153 // Reads at max maxBytesCount into the buffer, returns the actual count of bytes read
154 virtual uint64_t readData (void* buffer, uint64_t maxBytesCount) = 0;
155
156 template<typename RetVal = std::vector<uint8_t>>
157 RetVal readAll ()
158 {
159 RetVal result;
160
161 constexpr uint64_t bufferSize = 4 * 1024;
162 uint8_t buffer[bufferSize];
163
164 while (uint64_t bytesRead = readData (buffer, bufferSize))
165 {
166 using PtrType = typename RetVal::pointer;
167
168 PtrType begin = reinterpret_cast<PtrType>(buffer);
169 PtrType end = reinterpret_cast<PtrType>(buffer + bytesRead);
170
171 result.insert (result.end (), begin, end);
172
173 if (bytesRead < bufferSize)
174 break;
175 }
176
177 return result;
178 }
179
180 void Cancel() override
181 {
182 abort();
183 }
184};
185
186}
187}
Declare macros for the Network Manager library DLL API.
#define NETWORK_MANAGER_API
A class, representing a list of HTTP cookies.
A class, representing a list of HTTP headers.
Class to construct the HTTP request.
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
virtual bool isFinished() const noexcept=0
std::function< void(IResponse *)> RequestCallback
Definition: IResponse.h:115
const char * end(const char *str) noexcept
Definition: StringUtils.h:106
const char * begin(const char *str) noexcept
Definition: StringUtils.h:101
STL namespace.