ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

curlrequest.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #define FORBIDDEN_SYMBOL_ALLOW_ALL
00024 
00025 #include <curl/curl.h>
00026 #include "backends/networking/curl/curlrequest.h"
00027 #include "backends/networking/curl/connectionmanager.h"
00028 #include "backends/networking/curl/networkreadstream.h"
00029 #include "common/textconsole.h"
00030 
00031 namespace Networking {
00032 
00033 CurlRequest::CurlRequest(DataCallback cb, ErrorCallback ecb, Common::String url):
00034     Request(cb, ecb), _url(url), _stream(nullptr), _headersList(nullptr), _bytesBuffer(nullptr),
00035     _bytesBufferSize(0), _uploading(false), _usingPatch(false) {}
00036 
00037 CurlRequest::~CurlRequest() {
00038     delete _stream;
00039     delete _bytesBuffer;
00040 }
00041 
00042 NetworkReadStream *CurlRequest::makeStream() {
00043     if (_bytesBuffer)
00044         return new NetworkReadStream(_url.c_str(), _headersList, _bytesBuffer, _bytesBufferSize, _uploading, _usingPatch, true);
00045     if (!_formFields.empty() || !_formFiles.empty())
00046         return new NetworkReadStream(_url.c_str(), _headersList, _formFields, _formFiles);
00047     return new NetworkReadStream(_url.c_str(), _headersList, _postFields, _uploading, _usingPatch);
00048 }
00049 
00050 void CurlRequest::handle() {
00051     if (!_stream) _stream = makeStream();
00052 
00053     if (_stream && _stream->eos()) {
00054         if (_stream->httpResponseCode() != 200) {
00055             warning("CurlRequest: HTTP response code is not 200 OK (it's %ld)", _stream->httpResponseCode());
00056             ErrorResponse error(this, false, true, "", _stream->httpResponseCode());
00057             finishError(error);
00058             return;
00059         }
00060 
00061         finishSuccess(); //note that this Request doesn't call its callback on success (that's because it has nothing to return)
00062     }
00063 }
00064 
00065 void CurlRequest::restart() {
00066     if (_stream)
00067         delete _stream;
00068     _stream = nullptr;
00069     //with no stream available next handle() will create another one
00070 }
00071 
00072 Common::String CurlRequest::date() const {
00073     if (_stream) {
00074         Common::String headers = _stream->responseHeaders();
00075         const char *cstr = headers.c_str();
00076         const char *position = strstr(cstr, "Date: ");
00077 
00078         if (position) {
00079             Common::String result = "";
00080             char c;
00081             for (const char *i = position + 6; c = *i, c != 0; ++i) {
00082                 if (c == '\n' || c == '\r')
00083                     break;
00084                 result += c;
00085             }
00086             return result;
00087         }
00088     }
00089     return "";
00090 }
00091 
00092 void CurlRequest::setHeaders(Common::Array<Common::String> &headers) {
00093     curl_slist_free_all(_headersList);
00094     _headersList = nullptr;
00095     for (uint32 i = 0; i < headers.size(); ++i)
00096         addHeader(headers[i]);
00097 }
00098 
00099 void CurlRequest::addHeader(Common::String header) {
00100     _headersList = curl_slist_append(_headersList, header.c_str());
00101 }
00102 
00103 void CurlRequest::addPostField(Common::String keyValuePair) {
00104     if (_bytesBuffer)
00105         warning("CurlRequest: added POST fields would be ignored, because there is buffer present");
00106 
00107     if (!_formFields.empty() || !_formFiles.empty())
00108         warning("CurlRequest: added POST fields would be ignored, because there are form fields/files present");
00109 
00110     if (_postFields == "")
00111         _postFields = keyValuePair;
00112     else
00113         _postFields += "&" + keyValuePair;
00114 }
00115 
00116 void CurlRequest::addFormField(Common::String name, Common::String value) {
00117     if (_bytesBuffer)
00118         warning("CurlRequest: added POST form fields would be ignored, because there is buffer present");
00119 
00120     if (_formFields.contains(name))
00121         warning("CurlRequest: form field '%s' already had a value", name.c_str());
00122 
00123     _formFields[name] = value;
00124 }
00125 
00126 void CurlRequest::addFormFile(Common::String name, Common::String filename) {
00127     if (_bytesBuffer)
00128         warning("CurlRequest: added POST form files would be ignored, because there is buffer present");
00129 
00130     if (_formFields.contains(name))
00131         warning("CurlRequest: form file field '%s' already had a value", name.c_str());
00132 
00133     _formFiles[name] = filename;
00134 }
00135 
00136 void CurlRequest::setBuffer(byte *buffer, uint32 size) {
00137     if (_postFields != "")
00138         warning("CurlRequest: added POST fields would be ignored, because buffer added");
00139 
00140     if (_bytesBuffer)
00141         delete _bytesBuffer;
00142 
00143     _bytesBuffer = buffer;
00144     _bytesBufferSize = size;
00145 }
00146 
00147 void CurlRequest::usePut() { _uploading = true; }
00148 
00149 void CurlRequest::usePatch() { _usingPatch = true; }
00150 
00151 NetworkReadStreamResponse CurlRequest::execute() {
00152     if (!_stream) {
00153         _stream = makeStream();
00154         ConnMan.addRequest(this);
00155     }
00156 
00157     return NetworkReadStreamResponse(this, _stream);
00158 }
00159 
00160 const NetworkReadStream *CurlRequest::getNetworkReadStream() const { return _stream; }
00161 
00162 } // End of namespace Networking


Generated on Sat May 18 2019 05:00:59 for ResidualVM by doxygen 1.7.1
curved edge   curved edge