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

networkreadstream.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/networkreadstream.h"
00027 #include "backends/networking/curl/connectionmanager.h"
00028 #include "base/version.h"
00029 #include "common/debug.h"
00030 
00031 namespace Networking {
00032 
00033 size_t NetworkReadStream::curlDataCallback(char *d, size_t n, size_t l, void *p) {
00034     NetworkReadStream *stream = (NetworkReadStream *)p;
00035     if (stream)
00036         return stream->_backingStream.write(d, n * l);
00037     return 0;
00038 }
00039 
00040 size_t NetworkReadStream::curlReadDataCallback(char *d, size_t n, size_t l, void *p) {
00041     NetworkReadStream *stream = (NetworkReadStream *)p;
00042     if (stream)
00043         return stream->fillWithSendingContents(d, n * l);
00044     return 0;
00045 }
00046 
00047 size_t NetworkReadStream::curlHeadersCallback(char *d, size_t n, size_t l, void *p) {
00048     NetworkReadStream *stream = (NetworkReadStream *)p;
00049     if (stream)
00050         return stream->addResponseHeaders(d, n * l);
00051     return 0;
00052 }
00053 
00054 static int curlProgressCallback(void *p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
00055     NetworkReadStream *stream = (NetworkReadStream *)p;
00056     if (stream)
00057         stream->setProgress(dlnow, dltotal);
00058     return 0;
00059 }
00060 
00061 int NetworkReadStream::curlProgressCallbackOlder(void *p, double dltotal, double dlnow, double ultotal, double ulnow) {
00062     // for libcurl older than 7.32.0 (CURLOPT_PROGRESSFUNCTION)
00063     return curlProgressCallback(p, (curl_off_t)dltotal, (curl_off_t)dlnow, (curl_off_t)ultotal, (curl_off_t)ulnow);
00064 }
00065 
00066 void NetworkReadStream::resetStream() {
00067     _eos = _requestComplete = false;
00068     if (!_errorBuffer)
00069         _errorBuffer = (char *)calloc(CURL_ERROR_SIZE, 1);
00070     _sendingContentsBuffer = nullptr;
00071     _sendingContentsSize = _sendingContentsPos = 0;
00072     _progressDownloaded = _progressTotal = 0;
00073     _bufferCopy = nullptr;
00074 }
00075 
00076 void NetworkReadStream::initCurl(const char *url, curl_slist *headersList) {
00077     resetStream();
00078 
00079     _easy = curl_easy_init();
00080     curl_easy_setopt(_easy, CURLOPT_WRITEFUNCTION, curlDataCallback);
00081     curl_easy_setopt(_easy, CURLOPT_WRITEDATA, this); //so callback can call us
00082     curl_easy_setopt(_easy, CURLOPT_PRIVATE, this); //so ConnectionManager can call us when request is complete
00083     curl_easy_setopt(_easy, CURLOPT_HEADER, 0L);
00084     curl_easy_setopt(_easy, CURLOPT_HEADERDATA, this);
00085     curl_easy_setopt(_easy, CURLOPT_HEADERFUNCTION, curlHeadersCallback);
00086     curl_easy_setopt(_easy, CURLOPT_URL, url);
00087     curl_easy_setopt(_easy, CURLOPT_ERRORBUFFER, _errorBuffer);
00088     curl_easy_setopt(_easy, CURLOPT_VERBOSE, 0L);
00089     curl_easy_setopt(_easy, CURLOPT_FOLLOWLOCATION, 1L); //probably it's OK to have it always on
00090     curl_easy_setopt(_easy, CURLOPT_HTTPHEADER, headersList);
00091     curl_easy_setopt(_easy, CURLOPT_USERAGENT, gScummVMFullVersion);
00092     curl_easy_setopt(_easy, CURLOPT_NOPROGRESS, 0L);
00093     curl_easy_setopt(_easy, CURLOPT_PROGRESSFUNCTION, curlProgressCallbackOlder);
00094     curl_easy_setopt(_easy, CURLOPT_PROGRESSDATA, this);
00095 #if defined NINTENDO_SWITCH || defined ANDROID_PLAIN_PORT || defined PSP2
00096     curl_easy_setopt(_easy, CURLOPT_SSL_VERIFYPEER, 0);
00097 #endif
00098 
00099     const char *caCertPath = ConnMan.getCaCertPath();
00100     if (caCertPath) {
00101         curl_easy_setopt(_easy, CURLOPT_CAINFO, caCertPath);
00102     }
00103 
00104 #if LIBCURL_VERSION_NUM >= 0x072000
00105     // CURLOPT_XFERINFOFUNCTION introduced in libcurl 7.32.0
00106     // CURLOPT_PROGRESSFUNCTION is used as a backup plan in case older version is used
00107     curl_easy_setopt(_easy, CURLOPT_XFERINFOFUNCTION, curlProgressCallback);
00108     curl_easy_setopt(_easy, CURLOPT_XFERINFODATA, this);
00109 #endif
00110 
00111 #if LIBCURL_VERSION_NUM >= 0x071900
00112     // Added in libcurl 7.25.0
00113     if (_keepAlive) {
00114         curl_easy_setopt(_easy, CURLOPT_TCP_KEEPALIVE, 1L);
00115         curl_easy_setopt(_easy, CURLOPT_TCP_KEEPIDLE, _keepAliveIdle);
00116         curl_easy_setopt(_easy, CURLOPT_TCP_KEEPINTVL, _keepAliveInterval);
00117     }
00118 #endif
00119 }
00120 
00121 bool NetworkReadStream::reuseCurl(const char *url, curl_slist *headersList) {
00122     if (!_keepAlive) {
00123         warning("NetworkReadStream: Can't reuse curl handle (was not setup as keep-alive)");
00124         return false;
00125     }
00126 
00127     resetStream();
00128 
00129     curl_easy_setopt(_easy, CURLOPT_URL, url);
00130     curl_easy_setopt(_easy, CURLOPT_HTTPHEADER, headersList);
00131     curl_easy_setopt(_easy, CURLOPT_USERAGENT, gScummVMFullVersion); // in case headersList rewrites it
00132 
00133     return true;
00134 }
00135 
00136 void NetworkReadStream::setupBufferContents(const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post) {
00137     if (uploading) {
00138         curl_easy_setopt(_easy, CURLOPT_UPLOAD, 1L);
00139         curl_easy_setopt(_easy, CURLOPT_READDATA, this);
00140         curl_easy_setopt(_easy, CURLOPT_READFUNCTION, curlReadDataCallback);
00141         _sendingContentsBuffer = buffer;
00142         _sendingContentsSize = bufferSize;
00143     } else if (usingPatch) {
00144         curl_easy_setopt(_easy, CURLOPT_CUSTOMREQUEST, "PATCH");
00145     } else {
00146         if (post || bufferSize != 0) {
00147             curl_easy_setopt(_easy, CURLOPT_POSTFIELDSIZE, bufferSize);
00148 #if LIBCURL_VERSION_NUM >= 0x071101
00149             // CURLOPT_COPYPOSTFIELDS available since curl 7.17.1
00150             curl_easy_setopt(_easy, CURLOPT_COPYPOSTFIELDS, buffer);
00151 #else
00152             _bufferCopy = (byte*)malloc(bufferSize);
00153             memcpy(_bufferCopy, buffer, bufferSize);
00154             curl_easy_setopt(_easy, CURLOPT_POSTFIELDS, _bufferCopy);
00155 #endif
00156         }
00157     }
00158     ConnMan.registerEasyHandle(_easy);
00159 }
00160 
00161 void NetworkReadStream::setupFormMultipart(Common::HashMap<Common::String, Common::String> formFields, Common::HashMap<Common::String, Common::String> formFiles) {
00162     // set POST multipart upload form fields/files
00163     struct curl_httppost *formpost = nullptr;
00164     struct curl_httppost *lastptr = nullptr;
00165 
00166     for (Common::HashMap<Common::String, Common::String>::iterator i = formFields.begin(); i != formFields.end(); ++i) {
00167         CURLFORMcode code = curl_formadd(
00168             &formpost,
00169             &lastptr,
00170             CURLFORM_COPYNAME, i->_key.c_str(),
00171             CURLFORM_COPYCONTENTS, i->_value.c_str(),
00172             CURLFORM_END
00173         );
00174 
00175         if (code != CURL_FORMADD_OK)
00176             warning("NetworkReadStream: field curl_formadd('%s') failed", i->_key.c_str());
00177     }
00178 
00179     for (Common::HashMap<Common::String, Common::String>::iterator i = formFiles.begin(); i != formFiles.end(); ++i) {
00180         CURLFORMcode code = curl_formadd(
00181             &formpost,
00182             &lastptr,
00183             CURLFORM_COPYNAME, i->_key.c_str(),
00184             CURLFORM_FILE, i->_value.c_str(),
00185             CURLFORM_END
00186         );
00187 
00188         if (code != CURL_FORMADD_OK)
00189             warning("NetworkReadStream: file curl_formadd('%s') failed", i->_key.c_str());
00190     }
00191 
00192     curl_easy_setopt(_easy, CURLOPT_HTTPPOST, formpost);
00193     ConnMan.registerEasyHandle(_easy);
00194 }
00195 
00196 NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, Common::String postFields, bool uploading, bool usingPatch, bool keepAlive, long keepAliveIdle, long keepAliveInterval):
00197         _backingStream(DisposeAfterUse::YES), _keepAlive(keepAlive), _keepAliveIdle(keepAliveIdle), _keepAliveInterval(keepAliveInterval), _errorBuffer(nullptr) {
00198     initCurl(url, headersList);
00199     setupBufferContents((const byte *)postFields.c_str(), postFields.size(), uploading, usingPatch, false);
00200 }
00201 
00202 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):
00203         _backingStream(DisposeAfterUse::YES), _keepAlive(keepAlive), _keepAliveIdle(keepAliveIdle), _keepAliveInterval(keepAliveInterval), _errorBuffer(nullptr) {
00204     initCurl(url, headersList);
00205     setupFormMultipart(formFields, formFiles);
00206 }
00207 
00208 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):
00209         _backingStream(DisposeAfterUse::YES), _keepAlive(keepAlive), _keepAliveIdle(keepAliveIdle), _keepAliveInterval(keepAliveInterval), _errorBuffer(nullptr) {
00210     initCurl(url, headersList);
00211     setupBufferContents(buffer, bufferSize, uploading, usingPatch, post);
00212 }
00213 
00214 bool NetworkReadStream::reuse(const char *url, curl_slist *headersList, Common::String postFields, bool uploading, bool usingPatch) {
00215     if (!reuseCurl(url, headersList))
00216         return false;
00217 
00218     _backingStream = Common::MemoryReadWriteStream(DisposeAfterUse::YES);
00219     setupBufferContents((const byte *)postFields.c_str(), postFields.size(), uploading, usingPatch, false);
00220     return true;
00221 }
00222 
00223 bool NetworkReadStream::reuse(const char *url, curl_slist *headersList, Common::HashMap<Common::String, Common::String> formFields, Common::HashMap<Common::String, Common::String> formFiles) {
00224     if (!reuseCurl(url, headersList))
00225         return false;
00226 
00227     _backingStream = Common::MemoryReadWriteStream(DisposeAfterUse::YES);
00228     setupFormMultipart(formFields, formFiles);
00229     return true;
00230 }
00231 
00232 bool NetworkReadStream::reuse(const char *url, curl_slist *headersList, const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post) {
00233     if (!reuseCurl(url, headersList))
00234         return false;
00235 
00236     _backingStream = Common::MemoryReadWriteStream(DisposeAfterUse::YES);
00237     setupBufferContents(buffer, bufferSize, uploading, usingPatch, post);
00238     return true;
00239 }
00240 
00241 NetworkReadStream::~NetworkReadStream() {
00242     if (_easy)
00243         curl_easy_cleanup(_easy);
00244     free(_bufferCopy);
00245     free(_errorBuffer);
00246 }
00247 
00248 bool NetworkReadStream::eos() const {
00249     return _eos;
00250 }
00251 
00252 uint32 NetworkReadStream::read(void *dataPtr, uint32 dataSize) {
00253     uint32 actuallyRead = _backingStream.read(dataPtr, dataSize);
00254 
00255     if (actuallyRead == 0) {
00256         if (_requestComplete)
00257             _eos = true;
00258         return 0;
00259     }
00260 
00261     return actuallyRead;
00262 }
00263 
00264 void NetworkReadStream::finished(uint32 errorCode) {
00265     _requestComplete = true;
00266 
00267     char *url = nullptr;
00268     curl_easy_getinfo(_easy, CURLINFO_EFFECTIVE_URL, &url);
00269 
00270     if (errorCode == CURLE_OK) {
00271         debug(9, "NetworkReadStream: %s - Request succeeded", url);
00272     } else {
00273         warning("NetworkReadStream: %s - Request failed (%d - %s)", url, errorCode,
00274                 strlen(_errorBuffer) ? _errorBuffer : curl_easy_strerror((CURLcode)errorCode));
00275     }
00276 }
00277 
00278 long NetworkReadStream::httpResponseCode() const {
00279     long responseCode = -1;
00280     if (_easy)
00281         curl_easy_getinfo(_easy, CURLINFO_RESPONSE_CODE, &responseCode);
00282     return responseCode;
00283 }
00284 
00285 Common::String NetworkReadStream::currentLocation() const {
00286     Common::String result = "";
00287     if (_easy) {
00288         char *pointer;
00289         curl_easy_getinfo(_easy, CURLINFO_EFFECTIVE_URL, &pointer);
00290         result = Common::String(pointer);
00291     }
00292     return result;
00293 }
00294 
00295 Common::String NetworkReadStream::responseHeaders() const {
00296     return _responseHeaders;
00297 }
00298 
00299 Common::HashMap<Common::String, Common::String> NetworkReadStream::responseHeadersMap() const {
00300     // HTTP headers are described at RFC 2616: https://tools.ietf.org/html/rfc2616#section-4.2
00301     // this implementation tries to follow it, but for simplicity it does not support multi-line header values
00302 
00303     Common::HashMap<Common::String, Common::String> headers;
00304     Common::String headerName, headerValue, trailingWhitespace;
00305     char c;
00306     bool readingName = true;
00307 
00308     for (uint i = 0; i < _responseHeaders.size(); ++i) {
00309         c = _responseHeaders[i];
00310 
00311         if (readingName) {
00312             if (c == ' ' || c == '\r' || c == '\n' || c == '\t') {
00313                 // header names should not contain any whitespace, this is invalid
00314                 // ignore what's been before
00315                 headerName = "";
00316                 continue;
00317             }
00318             if (c == ':') {
00319                 if (!headerName.empty()) {
00320                     readingName = false;
00321                 }
00322                 continue;
00323             }
00324             headerName += c;
00325             continue;
00326         }
00327 
00328         // reading value:
00329         if (c == ' ' || c == '\t') {
00330             if (headerValue.empty()) {
00331                 // skip leading whitespace
00332                 continue;
00333             } else {
00334                 // accumulate trailing whitespace
00335                 trailingWhitespace += c;
00336                 continue;
00337             }
00338         }
00339 
00340         if (c == '\r' || c == '\n') {
00341             // not sure if RFC allows empty values, we'll ignore such
00342             if (!headerName.empty() && !headerValue.empty()) {
00343                 // add header value
00344                 // RFC allows header with the same name to be sent multiple times
00345                 // and requires it to be equivalent of just listing all header values separated with comma
00346                 // so if header already was met, we'll add new value to the old one
00347                 headerName.toLowercase();
00348                 if (headers.contains(headerName)) {
00349                     headers[headerName] += "," + headerValue;
00350                 } else {
00351                     headers[headerName] = headerValue;
00352                 }
00353             }
00354 
00355             headerName = "";
00356             headerValue = "";
00357             trailingWhitespace = "";
00358             readingName = true;
00359             continue;
00360         }
00361 
00362         // if we meet non-whitespace character, turns out those "trailing" whitespace characters were not so trailing
00363         headerValue += trailingWhitespace;
00364         trailingWhitespace = "";
00365         headerValue += c;
00366     }
00367 
00368     return headers;
00369 }
00370 
00371 uint32 NetworkReadStream::fillWithSendingContents(char *bufferToFill, uint32 maxSize) {
00372     uint32 sendSize = _sendingContentsSize - _sendingContentsPos;
00373     if (sendSize > maxSize)
00374         sendSize = maxSize;
00375     for (uint32 i = 0; i < sendSize; ++i) {
00376         bufferToFill[i] = _sendingContentsBuffer[_sendingContentsPos + i];
00377     }
00378     _sendingContentsPos += sendSize;
00379     return sendSize;
00380 }
00381 
00382 uint32 NetworkReadStream::addResponseHeaders(char *buffer, uint32 bufferSize) {
00383     _responseHeaders += Common::String(buffer, bufferSize);
00384     return bufferSize;
00385 }
00386 
00387 double NetworkReadStream::getProgress() const {
00388     if (_progressTotal < 1)
00389         return 0;
00390     return (double)_progressDownloaded / (double)_progressTotal;
00391 }
00392 
00393 void NetworkReadStream::setProgress(uint64 downloaded, uint64 total) {
00394     _progressDownloaded = downloaded;
00395     _progressTotal = total;
00396 }
00397 
00398 } // End of namespace Cloud


Generated on Sat May 30 2020 05:00:33 for ResidualVM by doxygen 1.7.1
curved edge   curved edge