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/curlrequest.h"
27 #include "backends/networking/curl/connectionmanager.h"
28 #include "backends/networking/curl/networkreadstream.h"
29 #include "common/textconsole.h"
30 
31 namespace Networking {
32 
CurlRequest(DataCallback cb,ErrorCallback ecb,Common::String url)33 CurlRequest::CurlRequest(DataCallback cb, ErrorCallback ecb, Common::String url):
34 	Request(cb, ecb), _url(url), _stream(nullptr), _headersList(nullptr), _bytesBuffer(nullptr),
35 	_bytesBufferSize(0), _uploading(false), _usingPatch(false) {}
36 
~CurlRequest()37 CurlRequest::~CurlRequest() {
38 	delete _stream;
39 	delete _bytesBuffer;
40 }
41 
makeStream()42 NetworkReadStream *CurlRequest::makeStream() {
43 	if (_bytesBuffer)
44 		return new NetworkReadStream(_url.c_str(), _headersList, _bytesBuffer, _bytesBufferSize, _uploading, _usingPatch, true);
45 	if (!_formFields.empty() || !_formFiles.empty())
46 		return new NetworkReadStream(_url.c_str(), _headersList, _formFields, _formFiles);
47 	return new NetworkReadStream(_url.c_str(), _headersList, _postFields, _uploading, _usingPatch);
48 }
49 
handle()50 void CurlRequest::handle() {
51 	if (!_stream) _stream = makeStream();
52 
53 	if (_stream && _stream->eos()) {
54 		if (_stream->httpResponseCode() != 200) {
55 			warning("CurlRequest: HTTP response code is not 200 OK (it's %ld)", _stream->httpResponseCode());
56 			ErrorResponse error(this, false, true, "HTTP response code is not 200 OK", _stream->httpResponseCode());
57 			finishError(error);
58 			return;
59 		}
60 
61 		finishSuccess(); //note that this Request doesn't call its callback on success (that's because it has nothing to return)
62 	}
63 }
64 
restart()65 void CurlRequest::restart() {
66 	if (_stream)
67 		delete _stream;
68 	_stream = nullptr;
69 	//with no stream available next handle() will create another one
70 }
71 
date() const72 Common::String CurlRequest::date() const {
73 	if (_stream) {
74 		Common::HashMap<Common::String, Common::String> headers = _stream->responseHeadersMap();
75 		if (headers.contains("date"))
76 			return headers["date"];
77 	}
78 	return "";
79 }
80 
setHeaders(Common::Array<Common::String> & headers)81 void CurlRequest::setHeaders(Common::Array<Common::String> &headers) {
82 	curl_slist_free_all(_headersList);
83 	_headersList = nullptr;
84 	for (uint32 i = 0; i < headers.size(); ++i)
85 		addHeader(headers[i]);
86 }
87 
addHeader(Common::String header)88 void CurlRequest::addHeader(Common::String header) {
89 	_headersList = curl_slist_append(_headersList, header.c_str());
90 }
91 
addPostField(Common::String keyValuePair)92 void CurlRequest::addPostField(Common::String keyValuePair) {
93 	if (_bytesBuffer)
94 		warning("CurlRequest: added POST fields would be ignored, because there is buffer present");
95 
96 	if (!_formFields.empty() || !_formFiles.empty())
97 		warning("CurlRequest: added POST fields would be ignored, because there are form fields/files present");
98 
99 	if (_postFields == "")
100 		_postFields = keyValuePair;
101 	else
102 		_postFields += "&" + keyValuePair;
103 }
104 
addFormField(Common::String name,Common::String value)105 void CurlRequest::addFormField(Common::String name, Common::String value) {
106 	if (_bytesBuffer)
107 		warning("CurlRequest: added POST form fields would be ignored, because there is buffer present");
108 
109 	if (_formFields.contains(name))
110 		warning("CurlRequest: form field '%s' already had a value", name.c_str());
111 
112 	_formFields[name] = value;
113 }
114 
addFormFile(Common::String name,Common::String filename)115 void CurlRequest::addFormFile(Common::String name, Common::String filename) {
116 	if (_bytesBuffer)
117 		warning("CurlRequest: added POST form files would be ignored, because there is buffer present");
118 
119 	if (_formFields.contains(name))
120 		warning("CurlRequest: form file field '%s' already had a value", name.c_str());
121 
122 	_formFiles[name] = filename;
123 }
124 
setBuffer(byte * buffer,uint32 size)125 void CurlRequest::setBuffer(byte *buffer, uint32 size) {
126 	if (_postFields != "")
127 		warning("CurlRequest: added POST fields would be ignored, because buffer added");
128 
129 	if (_bytesBuffer)
130 		delete _bytesBuffer;
131 
132 	_bytesBuffer = buffer;
133 	_bytesBufferSize = size;
134 }
135 
usePut()136 void CurlRequest::usePut() { _uploading = true; }
137 
usePatch()138 void CurlRequest::usePatch() { _usingPatch = true; }
139 
execute()140 NetworkReadStreamResponse CurlRequest::execute() {
141 	if (!_stream) {
142 		_stream = makeStream();
143 		ConnMan.addRequest(this);
144 	}
145 
146 	return NetworkReadStreamResponse(this, _stream);
147 }
148 
getNetworkReadStream() const149 const NetworkReadStream *CurlRequest::getNetworkReadStream() const { return _stream; }
150 
151 } // End of namespace Networking
152