ScummVM API documentation
networkreadstream-curl.h
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 modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (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, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef BACKENDS_NETWORKING_HTTP_CURL_NETWORKREADSTREAMCURL_H
23 #define BACKENDS_NETWORKING_HTTP_CURL_NETWORKREADSTREAMCURL_H
24 
25 #define FORBIDDEN_SYMBOL_ALLOW_ALL
26 
27 #include "backends/networking/http/networkreadstream.h"
28 #include "common/hash-str.h"
29 #include "common/hashmap.h"
30 #include "common/memstream.h"
31 #include "common/path.h"
32 #include "common/str.h"
33 #include "common/stream.h"
34 
35 #include <curl/curl.h>
36 
37 namespace Networking {
38 
40 private:
41  Common::MemoryReadWriteStream _backingStream;
42  CURL *_easy;
43  struct curl_slist *_headersSlist;
44  char *_errorBuffer;
45  CURLcode _errorCode;
46  byte *_bufferCopy; // To use with old curl version where CURLOPT_COPYPOSTFIELDS is not available
47  uint64 _progressDownloaded, _progressTotal;
48  bool _requestComplete;
49  Common::String _responseHeaders;
50 
51  const byte *_sendingContentsBuffer;
52  uint32 _sendingContentsSize;
53  uint32 _sendingContentsPos;
54 
55  void initCurl(const char *url, RequestHeaders *headersList);
56  bool reuseCurl(const char *url, RequestHeaders *headersList);
57  static struct curl_slist *requestHeadersToSlist(const RequestHeaders *headersList);
58 
59  static size_t curlDataCallback(char *d, size_t n, size_t l, void *p);
60  static size_t curlReadDataCallback(char *d, size_t n, size_t l, void *p);
61  static size_t curlHeadersCallback(char *d, size_t n, size_t l, void *p);
62  static int curlProgressCallbackOlder(void *p, double dltotal, double dlnow, double ultotal, double ulnow);
63 
64  // CURL-specific methods
65  CURL *getEasyHandle() const { return _easy; }
66  void resetStream();
67  void setupBufferContents(const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post);
68  void setupFormMultipart(const Common::HashMap<Common::String, Common::String> &formFields, const Common::HashMap<Common::String, Common::Path> &formFiles);
69 
70 public:
71  NetworkReadStreamCurl(const char *url, RequestHeaders *headersList, const Common::String &postFields, bool uploading, bool usingPatch, bool keepAlive, long keepAliveIdle, long keepAliveInterval);
72 
73  NetworkReadStreamCurl(const char *url, RequestHeaders *headersList, const Common::HashMap<Common::String, Common::String> &formFields, const Common::HashMap<Common::String, Common::Path> &formFiles, bool keepAlive, long keepAliveIdle, long keepAliveInterval);
74 
75  NetworkReadStreamCurl(const char *url, RequestHeaders *headersList, const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post, bool keepAlive, long keepAliveIdle, long keepAliveInterval);
76 
78  void finished(CURLcode errorCode);
79 
81  bool reuse(const char *url, RequestHeaders *headersList, const Common::String &postFields, bool uploading = false, bool usingPatch = false) override;
83  bool reuse(
84  const char *url, RequestHeaders *headersList,
86  const Common::HashMap<Common::String, Common::Path> &formFiles) override;
88  bool reuse(const char *url, RequestHeaders *headersList, const byte *buffer, uint32 bufferSize, bool uploading = false, bool usingPatch = false, bool post = true) override;
89 
91  void setProgress(uint64 downloaded, uint64 total);
92  double getProgress() const override;
93  uint32 read(void *dataPtr, uint32 dataSize) override;
94 
95  long httpResponseCode() const override;
96  Common::String currentLocation() const override;
104 
105  bool hasError() const override;
106  const char *getError() const override;
107 };
108 
109 } // End of namespace Networking
110 
111 #endif
Definition: str.h:59
Definition: scummvmcloud.h:32
bool reuse(const char *url, RequestHeaders *headersList, const Common::String &postFields, bool uploading=false, bool usingPatch=false) override
uint32 read(void *dataPtr, uint32 dataSize) override
void setProgress(uint64 downloaded, uint64 total)
long httpResponseCode() const override
Definition: networkreadstream-curl.h:39
Common::HashMap< Common::String, Common::String > responseHeadersMap() const override
Definition: memstream.h:283
Definition: networkreadstream.h:37
double getProgress() const override
Common::String currentLocation() const override