1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #define FORBIDDEN_SYMBOL_ALLOW_ALL
24 
25 #include <curl/curl.h>
26 #include "backends/networking/curl/networkreadstream.h"
27 #include "backends/networking/curl/connectionmanager.h"
28 #include "base/version.h"
29 #include "common/debug.h"
30 
31 namespace Networking {
32 
curlDataCallback(char * d,size_t n,size_t l,void * p)33 size_t NetworkReadStream::curlDataCallback(char *d, size_t n, size_t l, void *p) {
34 	NetworkReadStream *stream = (NetworkReadStream *)p;
35 	if (stream)
36 		return stream->_backingStream.write(d, n * l);
37 	return 0;
38 }
39 
curlReadDataCallback(char * d,size_t n,size_t l,void * p)40 size_t NetworkReadStream::curlReadDataCallback(char *d, size_t n, size_t l, void *p) {
41 	NetworkReadStream *stream = (NetworkReadStream *)p;
42 	if (stream)
43 		return stream->fillWithSendingContents(d, n * l);
44 	return 0;
45 }
46 
curlHeadersCallback(char * d,size_t n,size_t l,void * p)47 size_t NetworkReadStream::curlHeadersCallback(char *d, size_t n, size_t l, void *p) {
48 	NetworkReadStream *stream = (NetworkReadStream *)p;
49 	if (stream)
50 		return stream->addResponseHeaders(d, n * l);
51 	return 0;
52 }
53 
curlProgressCallback(void * p,curl_off_t dltotal,curl_off_t dlnow,curl_off_t ultotal,curl_off_t ulnow)54 static int curlProgressCallback(void *p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
55 	NetworkReadStream *stream = (NetworkReadStream *)p;
56 	if (stream)
57 		stream->setProgress(dlnow, dltotal);
58 	return 0;
59 }
60 
curlProgressCallbackOlder(void * p,double dltotal,double dlnow,double ultotal,double ulnow)61 int NetworkReadStream::curlProgressCallbackOlder(void *p, double dltotal, double dlnow, double ultotal, double ulnow) {
62 	// for libcurl older than 7.32.0 (CURLOPT_PROGRESSFUNCTION)
63 	return curlProgressCallback(p, (curl_off_t)dltotal, (curl_off_t)dlnow, (curl_off_t)ultotal, (curl_off_t)ulnow);
64 }
65 
resetStream()66 void NetworkReadStream::resetStream() {
67 	_eos = _requestComplete = false;
68 	if (!_errorBuffer)
69 		_errorBuffer = (char *)calloc(CURL_ERROR_SIZE, 1);
70 	_sendingContentsBuffer = nullptr;
71 	_sendingContentsSize = _sendingContentsPos = 0;
72 	_progressDownloaded = _progressTotal = 0;
73 	_bufferCopy = nullptr;
74 }
75 
initCurl(const char * url,curl_slist * headersList)76 void NetworkReadStream::initCurl(const char *url, curl_slist *headersList) {
77 	resetStream();
78 
79 	_easy = curl_easy_init();
80 	curl_easy_setopt(_easy, CURLOPT_WRITEFUNCTION, curlDataCallback);
81 	curl_easy_setopt(_easy, CURLOPT_WRITEDATA, this); //so callback can call us
82 	curl_easy_setopt(_easy, CURLOPT_PRIVATE, this); //so ConnectionManager can call us when request is complete
83 	curl_easy_setopt(_easy, CURLOPT_HEADER, 0L);
84 	curl_easy_setopt(_easy, CURLOPT_HEADERDATA, this);
85 	curl_easy_setopt(_easy, CURLOPT_HEADERFUNCTION, curlHeadersCallback);
86 	curl_easy_setopt(_easy, CURLOPT_URL, url);
87 	curl_easy_setopt(_easy, CURLOPT_ERRORBUFFER, _errorBuffer);
88 	curl_easy_setopt(_easy, CURLOPT_VERBOSE, 0L);
89 	curl_easy_setopt(_easy, CURLOPT_FOLLOWLOCATION, 1L); //probably it's OK to have it always on
90 	curl_easy_setopt(_easy, CURLOPT_HTTPHEADER, headersList);
91 	curl_easy_setopt(_easy, CURLOPT_USERAGENT, gScummVMFullVersion);
92 	curl_easy_setopt(_easy, CURLOPT_NOPROGRESS, 0L);
93 	curl_easy_setopt(_easy, CURLOPT_PROGRESSFUNCTION, curlProgressCallbackOlder);
94 	curl_easy_setopt(_easy, CURLOPT_PROGRESSDATA, this);
95 #if defined NINTENDO_SWITCH || defined ANDROID_PLAIN_PORT || defined PSP2
96 	curl_easy_setopt(_easy, CURLOPT_SSL_VERIFYPEER, 0);
97 #endif
98 
99 	const char *caCertPath = ConnMan.getCaCertPath();
100 	if (caCertPath) {
101 		curl_easy_setopt(_easy, CURLOPT_CAINFO, caCertPath);
102 	}
103 
104 #if LIBCURL_VERSION_NUM >= 0x072000
105 	// CURLOPT_XFERINFOFUNCTION introduced in libcurl 7.32.0
106 	// CURLOPT_PROGRESSFUNCTION is used as a backup plan in case older version is used
107 	curl_easy_setopt(_easy, CURLOPT_XFERINFOFUNCTION, curlProgressCallback);
108 	curl_easy_setopt(_easy, CURLOPT_XFERINFODATA, this);
109 #endif
110 
111 #if LIBCURL_VERSION_NUM >= 0x071900
112 	// Added in libcurl 7.25.0
113 	if (_keepAlive) {
114 		curl_easy_setopt(_easy, CURLOPT_TCP_KEEPALIVE, 1L);
115 		curl_easy_setopt(_easy, CURLOPT_TCP_KEEPIDLE, _keepAliveIdle);
116 		curl_easy_setopt(_easy, CURLOPT_TCP_KEEPINTVL, _keepAliveInterval);
117 	}
118 #endif
119 }
120 
reuseCurl(const char * url,curl_slist * headersList)121 bool NetworkReadStream::reuseCurl(const char *url, curl_slist *headersList) {
122 	if (!_keepAlive) {
123 		warning("NetworkReadStream: Can't reuse curl handle (was not setup as keep-alive)");
124 		return false;
125 	}
126 
127 	resetStream();
128 
129 	curl_easy_setopt(_easy, CURLOPT_URL, url);
130 	curl_easy_setopt(_easy, CURLOPT_HTTPHEADER, headersList);
131 	curl_easy_setopt(_easy, CURLOPT_USERAGENT, gScummVMFullVersion); // in case headersList rewrites it
132 
133 	return true;
134 }
135 
setupBufferContents(const byte * buffer,uint32 bufferSize,bool uploading,bool usingPatch,bool post)136 void NetworkReadStream::setupBufferContents(const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post) {
137 	if (uploading) {
138 		curl_easy_setopt(_easy, CURLOPT_UPLOAD, 1L);
139 		curl_easy_setopt(_easy, CURLOPT_READDATA, this);
140 		curl_easy_setopt(_easy, CURLOPT_READFUNCTION, curlReadDataCallback);
141 		_sendingContentsBuffer = buffer;
142 		_sendingContentsSize = bufferSize;
143 	} else if (usingPatch) {
144 		curl_easy_setopt(_easy, CURLOPT_CUSTOMREQUEST, "PATCH");
145 	} else {
146 		if (post || bufferSize != 0) {
147 			curl_easy_setopt(_easy, CURLOPT_POSTFIELDSIZE, bufferSize);
148 #if LIBCURL_VERSION_NUM >= 0x071101
149 			// CURLOPT_COPYPOSTFIELDS available since curl 7.17.1
150 			curl_easy_setopt(_easy, CURLOPT_COPYPOSTFIELDS, buffer);
151 #else
152 			_bufferCopy = (byte*)malloc(bufferSize);
153 			memcpy(_bufferCopy, buffer, bufferSize);
154 			curl_easy_setopt(_easy, CURLOPT_POSTFIELDS, _bufferCopy);
155 #endif
156 		}
157 	}
158 	ConnMan.registerEasyHandle(_easy);
159 }
160 
setupFormMultipart(Common::HashMap<Common::String,Common::String> formFields,Common::HashMap<Common::String,Common::String> formFiles)161 void NetworkReadStream::setupFormMultipart(Common::HashMap<Common::String, Common::String> formFields, Common::HashMap<Common::String, Common::String> formFiles) {
162 	// set POST multipart upload form fields/files
163 	struct curl_httppost *formpost = nullptr;
164 	struct curl_httppost *lastptr = nullptr;
165 
166 	for (Common::HashMap<Common::String, Common::String>::iterator i = formFields.begin(); i != formFields.end(); ++i) {
167 		CURLFORMcode code = curl_formadd(
168 			&formpost,
169 			&lastptr,
170 			CURLFORM_COPYNAME, i->_key.c_str(),
171 			CURLFORM_COPYCONTENTS, i->_value.c_str(),
172 			CURLFORM_END
173 		);
174 
175 		if (code != CURL_FORMADD_OK)
176 			warning("NetworkReadStream: field curl_formadd('%s') failed", i->_key.c_str());
177 	}
178 
179 	for (Common::HashMap<Common::String, Common::String>::iterator i = formFiles.begin(); i != formFiles.end(); ++i) {
180 		CURLFORMcode code = curl_formadd(
181 			&formpost,
182 			&lastptr,
183 			CURLFORM_COPYNAME, i->_key.c_str(),
184 			CURLFORM_FILE, i->_value.c_str(),
185 			CURLFORM_END
186 		);
187 
188 		if (code != CURL_FORMADD_OK)
189 			warning("NetworkReadStream: file curl_formadd('%s') failed", i->_key.c_str());
190 	}
191 
192 	curl_easy_setopt(_easy, CURLOPT_HTTPPOST, formpost);
193 	ConnMan.registerEasyHandle(_easy);
194 }
195 
NetworkReadStream(const char * url,curl_slist * headersList,Common::String postFields,bool uploading,bool usingPatch,bool keepAlive,long keepAliveIdle,long keepAliveInterval)196 NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, Common::String postFields, bool uploading, bool usingPatch, bool keepAlive, long keepAliveIdle, long keepAliveInterval):
197 		_backingStream(DisposeAfterUse::YES), _keepAlive(keepAlive), _keepAliveIdle(keepAliveIdle), _keepAliveInterval(keepAliveInterval), _errorBuffer(nullptr) {
198 	initCurl(url, headersList);
199 	setupBufferContents((const byte *)postFields.c_str(), postFields.size(), uploading, usingPatch, false);
200 }
201 
NetworkReadStream(const char * url,curl_slist * headersList,Common::HashMap<Common::String,Common::String> formFields,Common::HashMap<Common::String,Common::String> formFiles,bool keepAlive,long keepAliveIdle,long keepAliveInterval)202 NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, Common::HashMap<Common::String, Common::String> formFields, Common::HashMap<Common::String, Common::String> formFiles, bool keepAlive, long keepAliveIdle, long keepAliveInterval):
203 		_backingStream(DisposeAfterUse::YES), _keepAlive(keepAlive), _keepAliveIdle(keepAliveIdle), _keepAliveInterval(keepAliveInterval), _errorBuffer(nullptr) {
204 	initCurl(url, headersList);
205 	setupFormMultipart(formFields, formFiles);
206 }
207 
NetworkReadStream(const char * url,curl_slist * headersList,const byte * buffer,uint32 bufferSize,bool uploading,bool usingPatch,bool post,bool keepAlive,long keepAliveIdle,long keepAliveInterval)208 NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post, bool keepAlive, long keepAliveIdle, long keepAliveInterval):
209 		_backingStream(DisposeAfterUse::YES), _keepAlive(keepAlive), _keepAliveIdle(keepAliveIdle), _keepAliveInterval(keepAliveInterval), _errorBuffer(nullptr) {
210 	initCurl(url, headersList);
211 	setupBufferContents(buffer, bufferSize, uploading, usingPatch, post);
212 }
213 
reuse(const char * url,curl_slist * headersList,Common::String postFields,bool uploading,bool usingPatch)214 bool NetworkReadStream::reuse(const char *url, curl_slist *headersList, Common::String postFields, bool uploading, bool usingPatch) {
215 	if (!reuseCurl(url, headersList))
216 		return false;
217 
218 	_backingStream = Common::MemoryReadWriteStream(DisposeAfterUse::YES);
219 	setupBufferContents((const byte *)postFields.c_str(), postFields.size(), uploading, usingPatch, false);
220 	return true;
221 }
222 
reuse(const char * url,curl_slist * headersList,Common::HashMap<Common::String,Common::String> formFields,Common::HashMap<Common::String,Common::String> formFiles)223 bool NetworkReadStream::reuse(const char *url, curl_slist *headersList, Common::HashMap<Common::String, Common::String> formFields, Common::HashMap<Common::String, Common::String> formFiles) {
224 	if (!reuseCurl(url, headersList))
225 		return false;
226 
227 	_backingStream = Common::MemoryReadWriteStream(DisposeAfterUse::YES);
228 	setupFormMultipart(formFields, formFiles);
229 	return true;
230 }
231 
reuse(const char * url,curl_slist * headersList,const byte * buffer,uint32 bufferSize,bool uploading,bool usingPatch,bool post)232 bool NetworkReadStream::reuse(const char *url, curl_slist *headersList, const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post) {
233 	if (!reuseCurl(url, headersList))
234 		return false;
235 
236 	_backingStream = Common::MemoryReadWriteStream(DisposeAfterUse::YES);
237 	setupBufferContents(buffer, bufferSize, uploading, usingPatch, post);
238 	return true;
239 }
240 
~NetworkReadStream()241 NetworkReadStream::~NetworkReadStream() {
242 	if (_easy)
243 		curl_easy_cleanup(_easy);
244 	free(_bufferCopy);
245 	free(_errorBuffer);
246 }
247 
eos() const248 bool NetworkReadStream::eos() const {
249 	return _eos;
250 }
251 
read(void * dataPtr,uint32 dataSize)252 uint32 NetworkReadStream::read(void *dataPtr, uint32 dataSize) {
253 	uint32 actuallyRead = _backingStream.read(dataPtr, dataSize);
254 
255 	if (actuallyRead == 0) {
256 		if (_requestComplete)
257 			_eos = true;
258 		return 0;
259 	}
260 
261 	return actuallyRead;
262 }
263 
finished(uint32 errorCode)264 void NetworkReadStream::finished(uint32 errorCode) {
265 	_requestComplete = true;
266 
267 	char *url = nullptr;
268 	curl_easy_getinfo(_easy, CURLINFO_EFFECTIVE_URL, &url);
269 
270 	if (errorCode == CURLE_OK) {
271 		debug(9, "NetworkReadStream: %s - Request succeeded", url);
272 	} else {
273 		warning("NetworkReadStream: %s - Request failed (%d - %s)", url, errorCode,
274 		        strlen(_errorBuffer) ? _errorBuffer : curl_easy_strerror((CURLcode)errorCode));
275 	}
276 }
277 
httpResponseCode() const278 long NetworkReadStream::httpResponseCode() const {
279 	long responseCode = -1;
280 	if (_easy)
281 		curl_easy_getinfo(_easy, CURLINFO_RESPONSE_CODE, &responseCode);
282 	return responseCode;
283 }
284 
currentLocation() const285 Common::String NetworkReadStream::currentLocation() const {
286 	Common::String result = "";
287 	if (_easy) {
288 		char *pointer;
289 		curl_easy_getinfo(_easy, CURLINFO_EFFECTIVE_URL, &pointer);
290 		result = Common::String(pointer);
291 	}
292 	return result;
293 }
294 
responseHeaders() const295 Common::String NetworkReadStream::responseHeaders() const {
296 	return _responseHeaders;
297 }
298 
responseHeadersMap() const299 Common::HashMap<Common::String, Common::String> NetworkReadStream::responseHeadersMap() const {
300 	// HTTP headers are described at RFC 2616: https://tools.ietf.org/html/rfc2616#section-4.2
301 	// this implementation tries to follow it, but for simplicity it does not support multi-line header values
302 
303 	Common::HashMap<Common::String, Common::String> headers;
304 	Common::String headerName, headerValue, trailingWhitespace;
305 	char c;
306 	bool readingName = true;
307 
308 	for (uint i = 0; i < _responseHeaders.size(); ++i) {
309 		c = _responseHeaders[i];
310 
311 		if (readingName) {
312 			if (c == ' ' || c == '\r' || c == '\n' || c == '\t') {
313 				// header names should not contain any whitespace, this is invalid
314 				// ignore what's been before
315 				headerName = "";
316 				continue;
317 			}
318 			if (c == ':') {
319 				if (!headerName.empty()) {
320 					readingName = false;
321 				}
322 				continue;
323 			}
324 			headerName += c;
325 			continue;
326 		}
327 
328 		// reading value:
329 		if (c == ' ' || c == '\t') {
330 			if (headerValue.empty()) {
331 				// skip leading whitespace
332 				continue;
333 			} else {
334 				// accumulate trailing whitespace
335 				trailingWhitespace += c;
336 				continue;
337 			}
338 		}
339 
340 		if (c == '\r' || c == '\n') {
341 			// not sure if RFC allows empty values, we'll ignore such
342 			if (!headerName.empty() && !headerValue.empty()) {
343 				// add header value
344 				// RFC allows header with the same name to be sent multiple times
345 				// and requires it to be equivalent of just listing all header values separated with comma
346 				// so if header already was met, we'll add new value to the old one
347 				headerName.toLowercase();
348 				if (headers.contains(headerName)) {
349 					headers[headerName] += "," + headerValue;
350 				} else {
351 					headers[headerName] = headerValue;
352 				}
353 			}
354 
355 			headerName = "";
356 			headerValue = "";
357 			trailingWhitespace = "";
358 			readingName = true;
359 			continue;
360 		}
361 
362 		// if we meet non-whitespace character, turns out those "trailing" whitespace characters were not so trailing
363 		headerValue += trailingWhitespace;
364 		trailingWhitespace = "";
365 		headerValue += c;
366 	}
367 
368 	return headers;
369 }
370 
fillWithSendingContents(char * bufferToFill,uint32 maxSize)371 uint32 NetworkReadStream::fillWithSendingContents(char *bufferToFill, uint32 maxSize) {
372 	uint32 sendSize = _sendingContentsSize - _sendingContentsPos;
373 	if (sendSize > maxSize)
374 		sendSize = maxSize;
375 	for (uint32 i = 0; i < sendSize; ++i) {
376 		bufferToFill[i] = _sendingContentsBuffer[_sendingContentsPos + i];
377 	}
378 	_sendingContentsPos += sendSize;
379 	return sendSize;
380 }
381 
addResponseHeaders(char * buffer,uint32 bufferSize)382 uint32 NetworkReadStream::addResponseHeaders(char *buffer, uint32 bufferSize) {
383 	_responseHeaders += Common::String(buffer, bufferSize);
384 	return bufferSize;
385 }
386 
getProgress() const387 double NetworkReadStream::getProgress() const {
388 	if (_progressTotal < 1)
389 		return 0;
390 	return (double)_progressDownloaded / (double)_progressTotal;
391 }
392 
setProgress(uint64 downloaded,uint64 total)393 void NetworkReadStream::setProgress(uint64 downloaded, uint64 total) {
394 	_progressDownloaded = downloaded;
395 	_progressTotal = total;
396 }
397 
398 } // End of namespace Cloud
399