ScummVM API documentation
networkreadstream.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_CURL_NETWORKREADSTREAM_H
23 #define BACKENDS_NETWORKING_CURL_NETWORKREADSTREAM_H
24 
25 #include "common/memstream.h"
26 #include "common/path.h"
27 #include "common/stream.h"
28 #include "common/str.h"
29 #include "common/hashmap.h"
30 #include "common/hash-str.h"
31 
32 typedef void CURL;
33 struct curl_slist;
34 
35 namespace Networking {
36 
38  CURL *_easy;
39  Common::MemoryReadWriteStream _backingStream;
40  bool _keepAlive;
41  long _keepAliveIdle, _keepAliveInterval;
42  bool _eos, _requestComplete;
43  char *_errorBuffer;
44  uint32 _errorCode;
45  const byte *_sendingContentsBuffer;
46  uint32 _sendingContentsSize;
47  uint32 _sendingContentsPos;
48  byte *_bufferCopy; // To use with old curl version where CURLOPT_COPYPOSTFIELDS is not available
49  Common::String _responseHeaders;
50  uint64 _progressDownloaded, _progressTotal;
51 
52  void resetStream();
53  void initCurl(const char *url, curl_slist *headersList);
54  bool reuseCurl(const char *url, curl_slist *headersList);
55  void setupBufferContents(const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post);
56  void setupFormMultipart(const Common::HashMap<Common::String, Common::String> &formFields, const Common::HashMap<Common::String, Common::Path> &formFiles);
57 
65  uint32 fillWithSendingContents(char *bufferToFill, uint32 maxSize);
66 
72  uint32 addResponseHeaders(char *buffer, uint32 bufferSize);
73 
74  static size_t curlDataCallback(char *d, size_t n, size_t l, void *p);
75  static size_t curlReadDataCallback(char *d, size_t n, size_t l, void *p);
76  static size_t curlHeadersCallback(char *d, size_t n, size_t l, void *p);
77  static int curlProgressCallbackOlder(void *p, double dltotal, double dlnow, double ultotal, double ulnow);
78 public:
80  NetworkReadStream(const char *url, curl_slist *headersList, const Common::String &postFields, bool uploading = false, bool usingPatch = false, bool keepAlive = false, long keepAliveIdle = 120, long keepAliveInterval = 60);
83  const char *url, curl_slist *headersList,
86  bool keepAlive = false, long keepAliveIdle = 120, long keepAliveInterval = 60);
88  NetworkReadStream(const char *url, curl_slist *headersList, const byte *buffer, uint32 bufferSize, bool uploading = false, bool usingPatch = false, bool post = true, bool keepAlive = false, long keepAliveIdle = 120, long keepAliveInterval = 60);
89  ~NetworkReadStream() override;
90 
92  bool reuse(const char *url, curl_slist *headersList, const Common::String &postFields, bool uploading = false, bool usingPatch = false);
94  bool reuse(
95  const char *url, curl_slist *headersList,
99  bool reuse(const char *url, curl_slist *headersList, const byte *buffer, uint32 bufferSize, bool uploading = false, bool usingPatch = false, bool post = true);
100 
111  bool eos() const override;
112 
125  uint32 read(void *dataPtr, uint32 dataSize) override;
126 
133  void finished(uint32 errorCode);
134 
141  long httpResponseCode() const;
142 
150 
157 
165 
167  double getProgress() const;
168 
170  void setProgress(uint64 downloaded, uint64 total);
171 
172  bool keepAlive() const { return _keepAlive; }
173 
174  bool hasError() const;
175  uint32 getErrorCode() const { return _errorCode; }
176  const char *getError() const;
177 
178 };
179 
180 } // End of namespace Networking
181 
182 #endif
void setProgress(uint64 downloaded, uint64 total)
Common::HashMap< Common::String, Common::String > responseHeadersMap() const
Definition: str.h:59
Common::String currentLocation() const
Definition: scummvmcloud.h:31
bool eos() const override
void finished(uint32 errorCode)
Common::String responseHeaders() const
Definition: memstream.h:283
bool reuse(const char *url, curl_slist *headersList, const Common::String &postFields, bool uploading=false, bool usingPatch=false)
Definition: networkreadstream.h:37
uint32 read(void *dataPtr, uint32 dataSize) override
Definition: stream.h:385
NetworkReadStream(const char *url, curl_slist *headersList, const Common::String &postFields, bool uploading=false, bool usingPatch=false, bool keepAlive=false, long keepAliveIdle=120, long keepAliveInterval=60)