forked from openframeworks/openFrameworks
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathofURLFileLoader.h
More file actions
262 lines (220 loc) · 9.38 KB
/
ofURLFileLoader.h
File metadata and controls
262 lines (220 loc) · 9.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#pragma once
#include "ofEvents.h"
// ofBuffer only
#include "ofFileUtils.h"
#include <map>
class ofHttpResponse;
/// \class ofHttpRequest
/// \brief An HTTP GET or POST request.
class ofHttpRequest {
public:
ofHttpRequest();
ofHttpRequest(const std::string & url, const std::string & name, bool saveTo = false, bool autoClose=true, bool verbose=false);
std::string url; ///< request url
std::string name; ///< optional name key for sorting
bool saveTo; ///< save to a file once the request is finished?
bool close; ///< auto close connection at each request - default true
bool verbose; ///< verbose packet logs
std::map<std::string, std::string> headers; ///< HTTP header keys & values
std::string body; ///< POST body data
std::string contentType; ///< POST data mime type
std::function<void(const ofHttpResponse &)> done;
std::function<void(const ofHttpRequest &, float)> progressCallback = nullptr; ///< pass a function for progress of download
size_t timeoutSeconds = 0;
bool headerOnly = false;
/// \return the unique id for this request
int getId() const;
[[deprecated("Use getId().")]]
int getID();
/// HTTP request type
enum Method{
GET, //< request data from a specified resource (via url)
POST, //< submit data to be processed to a specified resource (via url)
PUT
} method;
private:
int id; ///< unique id for this request
static int nextID; ///< global for computing next unique id
};
/// \class ofHttpResponse
/// \brief An HTTP response to a GET or POST request.
class ofHttpResponse {
public:
ofHttpResponse();
ofHttpResponse(const ofHttpRequest & request, const ofBuffer & data, int status, const std::string & error);
ofHttpResponse(const ofHttpRequest & request, int status, const std::string & error);
operator ofBuffer &();
ofHttpRequest request; ///< matching HTTP request for this response
ofBuffer data; ///< response raw data
int status; ///< HTTP response status (200: OK, 404: Not Found, etc)
std::string error; ///< HTTP error string, if any (OK, Not Found, etc)
};
/// \brief Make an HTTP GET request.
///
/// Blocks until a response is returned or the request times out.
///
/// \param url HTTP url to request, ie. "http://somewebsite.com/someapi/someimage.jpg"
/// \return HTTP response
ofHttpResponse ofLoadURL(const std::string & url);
/// \brief Make an asynchronous HTTP GET request.
///
/// Asynchronous requests will not block as they are placed in a queue and run
/// using a background thread. This also means, however, that the response may
/// not be available after the this function returns.
///
/// \param url HTTP url to request, ie. "http://somewebsite.com/someapi/someimage.jpg"
/// \param name optional key to use when sorting requests
/// \return unique id for the active HTTP request
int ofLoadURLAsync(const std::string & url, const std::string & name = ""); // returns id
/// \brief Make an HTTP GET request and save the response data to a file.
///
/// Blocks until a response is returned or the request times out.
///
/// \param url HTTP url to request, ie. "http://somewebsite.com/someapi/someimage.jpg"
/// \param path file path to save to
/// \return HTTP response on success or failure
ofHttpResponse ofSaveURLTo(const std::string & url, const of::filesystem::path & path);
/// \brief Make an asynchronous HTTP request and save the response data to a
/// file.
///
/// Asynchronous requests will not block as they are placed in a queue and run
/// using a background thread. This also means, however, that the response may
/// not be available after the this function returns.
///
/// \param url HTTP url to request, ie. "http://somewebsite.com/someapi/someimage.jpg"
/// \param path file path to save to
/// \return unique id for the active HTTP request
int ofSaveURLAsync(const std::string & url, const of::filesystem::path & path);
/// \brief Remove an active HTTP request from the queue.
/// \param id HTTP request id
void ofRemoveURLRequest(int id);
/// \brief Remove all active HTTP requests from the queue.
void ofRemoveAllURLRequests();
/// \brief Stop & remove all active and waiting HTTP requests.
void ofStopURLLoader();
ofEvent<ofHttpResponse> & ofURLResponseEvent();
template <class T>
void ofRegisterURLNotification(T * obj) {
ofAddListener(ofURLResponseEvent(), obj, &T::urlResponse);
}
template <class T>
void ofUnregisterURLNotification(T * obj) {
ofRemoveListener(ofURLResponseEvent(), obj, &T::urlResponse);
}
class ofBaseURLFileLoader;
/// \class ofURLFileLoader
/// \brief Loads a file from a URL using an HTTP request.
class ofURLFileLoader {
public:
ofURLFileLoader();
/// \brief Make an HTTP request.
///
/// Blocks until a response is returned or the request times out.
///
/// \param url HTTP url to request, ie. "http://somewebsite.com/someapi/someimage.jpg"
/// \return HTTP response on success or failure
ofHttpResponse get(const std::string & url);
/// \brief Make an asynchronous HTTP request.
///
/// Will not block, placed in a queue and run using a background thread.
///
/// \param url HTTP url to request, ie. "http://somewebsite.com/someapi/someimage.jpg"
/// \param name optional key to use when sorting requests
/// \return unique id for the active HTTP request
int getAsync(const std::string & url, const std::string & name = "");
/// \brief Make an HTTP request and save the response data to a file.
///
/// Blocks until a response is returned or the request times out.
///
/// \param url HTTP url to request, ie. "http://somewebsite.com/someapi/someimage.jpg"
/// \param path file path to save to
/// \return HTTP response on success or failure
ofHttpResponse saveTo(const std::string & url, const of::filesystem::path & path);
/// \brief Make an asynchronous HTTP request and save the response data to a file.
///
/// Will not block, placed in a queue and run using a background thread.
///
/// \param url HTTP url to request, ie. "http://somewebsite.com/someapi/someimage.jpg"
/// \param path file path to save to
/// \returns unique id for the active HTTP request
int saveAsync(const std::string & url, const of::filesystem::path & path);
/// \brief Remove an active HTTP request from the queue.
/// \param id HTTP request id
void remove(int id);
/// \brief Clear all active HTTP requests from the queue.
void clear();
/// \brief Stop & remove all active and waiting HTTP requests.
void stop();
/// \brief Low level HTTP request implementation.
///
/// Blocks until a response is returned or the request times out.
///
/// \return HTTP response on success or failure
ofHttpResponse handleRequest(const ofHttpRequest & request);
/// \brief Low level HTTP request asynchronous implementation.
///
/// This is a non-blocking version of handleRequest that will return a
/// response in the urlResponse callback.
///
/// \return unique id of the active HTTP request
int handleRequestAsync(const ofHttpRequest & request);
private:
std::shared_ptr<ofBaseURLFileLoader> impl;
};
/// \class ofBaseURLFileLoader
/// \brief Loads a file from a URL using an HTTP request.
class ofBaseURLFileLoader {
public:
virtual ~ofBaseURLFileLoader() {};
/// \brief Make an HTTP request.
///
/// Blocks until a response is returned or the request times out.
///
/// \param url HTTP url to request, ie. "http://somewebsite.com/someapi/someimage.jpg"
/// \return HTTP response on success or failure
virtual ofHttpResponse get(const std::string & url) = 0;
/// \brief Make an asynchronous HTTP request.
///
/// Will not block, placed in a queue and run using a background thread.
///
/// \param url HTTP url to request, ie. "http://somewebsite.com/someapi/someimage.jpg"
/// \param name optional key to use when sorting requests
/// \return unique id for the active HTTP request
virtual int getAsync(const std::string & url, const std::string & name = "") = 0;
/// \brief Make an HTTP request and save the response data to a file.
///
/// Blocks until a response is returned or the request times out.
///
/// \param url HTTP url to request, ie. "http://somewebsite.com/someapi/someimage.jpg"
/// \param path file path to save to
/// \return HTTP response on success or failure
virtual ofHttpResponse saveTo(const std::string & url, const of::filesystem::path & path) = 0;
/// \brief Make an asynchronous HTTP request and save the response data to a file.
///
/// Will not block, placed in a queue and run using a background thread.
///
/// \param url HTTP url to request, ie. "http://somewebsite.com/someapi/someimage.jpg"
/// \param path file path to save to
/// \return unique id for the active HTTP request
virtual int saveAsync(const std::string & url, const of::filesystem::path & path) = 0;
/// \brief Remove an active HTTP request from the queue.
/// \param id HTTP request id
virtual void remove(int id) = 0;
/// \brief Clear all active HTTP requests from the queue.
virtual void clear() = 0;
/// \brief Stop & remove all active and waiting HTTP requests.
virtual void stop() = 0;
/// \brief Low level HTTP request implementation.
///
/// Blocks until a response is returned or the request times out.
///
/// \return HTTP response on success or failure
virtual ofHttpResponse handleRequest(const ofHttpRequest & request) = 0;
/// \brief Low level HTTP request asynchronous implementation.
///
/// This is a non-blocking version of handleRequest that will return a
/// response in the urlResponse callback.
///
/// \return unique id of the active HTTP request
virtual int handleRequestAsync(const ofHttpRequest & request) = 0;
};