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 "backends/networking/curl/curlrequest.h"
26 #include "backends/networking/curl/connectionmanager.h"
27 #include "backends/networking/curl/networkreadstream.h"
28 #include "common/textconsole.h"
29 #include <curl/curl.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, "", _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::String headers = _stream->responseHeaders();
75 		const char *cstr = headers.c_str();
76 		const char *position = strstr(cstr, "Date: ");
77 
78 		if (position) {
79 			Common::String result = "";
80 			char c;
81 			for (const char *i = position + 6; c = *i, c != 0; ++i) {
82 				if (c == '\n' || c == '\r')
83 					break;
84 				result += c;
85 			}
86 			return result;
87 		}
88 	}
89 	return "";
90 }
91 
setHeaders(Common::Array<Common::String> & headers)92 void CurlRequest::setHeaders(Common::Array<Common::String> &headers) {
93 	curl_slist_free_all(_headersList);
94 	_headersList = nullptr;
95 	for (uint32 i = 0; i < headers.size(); ++i)
96 		addHeader(headers[i]);
97 }
98 
addHeader(Common::String header)99 void CurlRequest::addHeader(Common::String header) {
100 	_headersList = curl_slist_append(_headersList, header.c_str());
101 }
102 
addPostField(Common::String keyValuePair)103 void CurlRequest::addPostField(Common::String keyValuePair) {
104 	if (_bytesBuffer)
105 		warning("CurlRequest: added POST fields would be ignored, because there is buffer present");
106 
107 	if (!_formFields.empty() || !_formFiles.empty())
108 		warning("CurlRequest: added POST fields would be ignored, because there are form fields/files present");
109 
110 	if (_postFields == "")
111 		_postFields = keyValuePair;
112 	else
113 		_postFields += "&" + keyValuePair;
114 }
115 
addFormField(Common::String name,Common::String value)116 void CurlRequest::addFormField(Common::String name, Common::String value) {
117 	if (_bytesBuffer)
118 		warning("CurlRequest: added POST form fields would be ignored, because there is buffer present");
119 
120 	if (_formFields.contains(name))
121 		warning("CurlRequest: form field '%s' already had a value", name.c_str());
122 
123 	_formFields[name] = value;
124 }
125 
addFormFile(Common::String name,Common::String filename)126 void CurlRequest::addFormFile(Common::String name, Common::String filename) {
127 	if (_bytesBuffer)
128 		warning("CurlRequest: added POST form files would be ignored, because there is buffer present");
129 
130 	if (_formFields.contains(name))
131 		warning("CurlRequest: form file field '%s' already had a value", name.c_str());
132 
133 	_formFiles[name] = filename;
134 }
135 
setBuffer(byte * buffer,uint32 size)136 void CurlRequest::setBuffer(byte *buffer, uint32 size) {
137 	if (_postFields != "")
138 		warning("CurlRequest: added POST fields would be ignored, because buffer added");
139 
140 	if (_bytesBuffer)
141 		delete _bytesBuffer;
142 
143 	_bytesBuffer = buffer;
144 	_bytesBufferSize = size;
145 }
146 
usePut()147 void CurlRequest::usePut() { _uploading = true; }
148 
usePatch()149 void CurlRequest::usePatch() { _usingPatch = true; }
150 
execute()151 NetworkReadStreamResponse CurlRequest::execute() {
152 	if (!_stream) {
153 		_stream = makeStream();
154 		ConnMan.addRequest(this);
155 	}
156 
157 	return NetworkReadStreamResponse(this, _stream);
158 }
159 
getNetworkReadStream() const160 const NetworkReadStream *CurlRequest::getNetworkReadStream() const { return _stream; }
161 
162 } // End of namespace Networking
163