summaryrefslogtreecommitdiffstats
path: root/src/uscxml/util/URL.h
blob: f6da30d840cc6701033180487943858af93c66e5 (plain)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
 *  @file
 *  @author     2012-2013 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
 *  @copyright  Simplified BSD
 *
 *  @cond
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the FreeBSD license as published by the FreeBSD
 *  project.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 *  You should have received a copy of the FreeBSD license along with this
 *  program. If not, see <http://www.opensource.org/licenses/bsd-license>.
 *  @endcond
 */

#ifndef URL_H_9DAEGSMV
#define URL_H_9DAEGSMV

#include "uscxml/Common.h"
#include "uscxml/messages/Event.h"

#define DOWNLOAD_IF_NECESSARY if (!_isDownloaded) { download(true); }

#include <string>
#include <sstream>
#include <map>
#include <set>
#include <list>
#include <thread>
#include <condition_variable>
#include <curl/curl.h>
#include <uriparser/Uri.h>

#define USCXML_URI_STRING(obj, field) std::string(obj.field.first, obj.field.afterLast - obj.field.first)

namespace uscxml {

class URL;

class USCXML_API URLMonitor {
public:
	virtual void downloadStarted(const URL& url) {};
	virtual void downloadCompleted(const URL& url) {};
	virtual void downloadFailed(const URL& url, int errorCode) {};
	virtual void headerChunkReceived(const URL& url, const std::string& headerChunk) {};
	virtual void contentChunkReceived(const URL& url, const std::string& contentChunk) {};
};

enum URLRequestType {
	POST,
	GET
};

class USCXML_API URLImpl : public std::enable_shared_from_this<URLImpl> {
public:
	URLImpl(const std::string& url);
	~URLImpl();

	bool isAbsolute() const {
		// see https://sourceforge.net/p/uriparser/bugs/3/
		return _uri.absolutePath || ((_uri.hostText.first != nullptr) && (_uri.pathHead != nullptr));
	}

	std::string scheme() const {
		return USCXML_URI_STRING(_uri, scheme);
	}

	std::string userInfo() const {
		return USCXML_URI_STRING(_uri, userInfo);
	}

	std::string host() const {
		return USCXML_URI_STRING(_uri, hostText);
	}

	std::string port() const {
		return USCXML_URI_STRING(_uri, portText);
	}

	std::string fragment() const {
		return USCXML_URI_STRING(_uri, fragment);
	}

	std::map<std::string, std::string> query() const;
	std::string path() const;
	std::list<std::string> pathComponents() const;

	void normalize();

	static URL resolve(URLImpl* relativeURL, URLImpl* absoluteURL);
	static URL resolveWithCWD(URLImpl* relativeURL);
	static URL refer(URLImpl* absoluteSource, URLImpl* absoluteBase);

	void addMonitor(URLMonitor* monitor)    {
		_monitors.insert(monitor);
	}
	void removeMonitor(URLMonitor* monitor) {
		_monitors.erase(monitor);
	}

	// downloading / uploading
	void addOutHeader(const std::string& key, const std::string& value) {
		_outHeader[key] = value;
	}
	void setOutContent(const std::string& content) {
		_outContent = content;
		_requestType = URLRequestType::POST;
	}
	void setRequestType(URLRequestType requestType) {
		_requestType = requestType;

	}

	const std::map<std::string, std::string> getInHeaderFields() {
		DOWNLOAD_IF_NECESSARY
		return _inHeaders;
	}

	const std::string getInHeaderField(const std::string& key) {
		DOWNLOAD_IF_NECESSARY
		if (_inHeaders.find(key) != _inHeaders.end()) {
			return _inHeaders[key];
		}
		return "";
	}

	const std::string getStatusCode() const {
//        DOWNLOAD_IF_NECESSARY
		return _statusCode;
	}

	const std::string getStatusMessage() const {
//        DOWNLOAD_IF_NECESSARY
		return _statusMsg;
	}

	const std::string getInContent(bool forceReload = false) {
		if (forceReload)
			_isDownloaded = false;
		DOWNLOAD_IF_NECESSARY
		return _rawInContent.str();
	}

	const void download(bool blocking = false);

	operator Data() const;
	operator std::string() const;

protected:
	URLImpl();
	UriUriA _uri;
	std::string _orig;

	CURL* getCurlHandle();
	static size_t writeHandler(void *ptr, size_t size, size_t nmemb, void *userdata);
	static size_t headerHandler(void *ptr, size_t size, size_t nmemb, void *userdata);

	void downloadStarted();
	void downloadCompleted();
	void downloadFailed(CURLcode errorCode);

	static void prepareException(ErrorEvent& exception, int errorCode, const std::string& origUri, UriParserStateA* parser);

	CURL* _handle = NULL;
	std::stringstream _rawInContent;
	std::stringstream _rawInHeader;
	std::map<std::string, std::string> _inHeaders;

	std::string _outContent;
	std::map<std::string, std::string> _outHeader;
	URLRequestType _requestType;

	std::string _statusCode;
	std::string _statusMsg;
	bool _isDownloaded = false;
	bool _hasFailed = false;
	std::string _error;

	std::condition_variable_any _condVar;
	std::recursive_mutex _mutex;

	std::set<URLMonitor*> _monitors;

	friend class URLFetcher;
};

class USCXML_API URL {
public:
	PIMPL_OPERATORS(URL);

	URL(const std::string url) : _impl(new URLImpl(url)) {}

	bool isAbsolute() {
		return _impl->isAbsolute();
	}

	std::string scheme() {
		return _impl->scheme();
	}

	std::string userInfo() {
		return _impl->userInfo();
	}

	std::string host() {
		return _impl->host();
	}

	std::string port() {
		return _impl->port();
	}

	std::string fragment() {
		return _impl->fragment();
	}

	std::map<std::string, std::string> query() {
		return _impl->query();
	}

	std::string path() {
		return _impl->path();
	}

	std::list<std::string> pathComponents() {
		return _impl->pathComponents();
	}

	void normalize() {
		return _impl->normalize();
	}

	static URL resolve(URL relativeURL, URL absoluteURL) {
		return URLImpl::resolve(relativeURL._impl.get(), absoluteURL._impl.get());
	}

	static URL resolveWithCWD(URL relativeURL) {
		return URLImpl::resolveWithCWD(relativeURL._impl.get());
	}

	static URL refer(URL absoluteSource, URL absoluteBase) {
		return URLImpl::refer(absoluteSource._impl.get(), absoluteBase._impl.get());
	}

	void addOutHeader(const std::string& key, const std::string& value) {
		return _impl->addOutHeader(key, value);
	}

	void setOutContent(const std::string& content) {
		return _impl->setOutContent(content);
	}
	void setRequestType(URLRequestType requestType) {
		return _impl->setRequestType(requestType);
	}

	const std::map<std::string, std::string> getInHeaderFields() {
		return _impl->getInHeaderFields();
	}

	const std::string getInHeaderField(const std::string& key) {
		return _impl->getInHeaderField(key);
	}

	const std::string getStatusCode() const {
		return _impl->getStatusCode();
	}

	const std::string getStatusMessage() const {
		return _impl->getStatusMessage();
	}

	const std::string getInContent(bool forceReload = false) {
		return _impl->getInContent(forceReload);
	}

	const void download(bool blocking = false) const {
		return _impl->download(blocking);
	}

	void addMonitor(URLMonitor* monitor)    {
		return _impl->addMonitor(monitor);
	}
	void removeMonitor(URLMonitor* monitor) {
		return _impl->removeMonitor(monitor);
	}

	operator Data() const {
		return _impl->operator Data();
	}

	operator std::string() {
		return (*_impl.get());
	}

protected:
	std::shared_ptr<URLImpl> _impl;
	friend class URLFetcher;
};

class USCXML_API URLFetcher {
public:
	static void fetchURL(URL& url);
	static void breakURL(URL& url);

	void start();
	void stop();

protected:
	URLFetcher();
	~URLFetcher();

	static URLFetcher* _instance;
	static URLFetcher* getInstance();

	static void run(void* instance);
	void perform();

	std::thread* _thread;
	std::condition_variable_any _condVar;
	std::recursive_mutex _mutex;
	bool _isStarted;

	std::map<CURL*, URL> _handlesToURLs;
	std::map<CURL*, curl_slist*> _handlesToHeaders;
	CURLM* _multiHandle;
	char* _envProxy;
};

}

#endif /* end of include guard: URL_H_9DAEGSMV */