ScummVM API documentation
networkreadstream-emscripten.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_EMSCRIPTEN_NETWORKREADSTREAMEMSCRIPTEN_H
23#define BACKENDS_NETWORKING_HTTP_EMSCRIPTEN_NETWORKREADSTREAMEMSCRIPTEN_H
24
25#ifdef EMSCRIPTEN
26
27#include "backends/networking/http/networkreadstream.h"
28
29extern "C" {
30// External JS functions for HTTP fetching
31extern void NetworkReadStreamEmscripten_init(void);
32extern int NetworkReadStreamEmscripten_fetch(const char *method, const char *url,
33 char **requestHeaders,
34 const char *requestData, size_t requestDataSize,
35 char **formFields = nullptr,
36 char **formFiles = nullptr);
37extern void NetworkReadStreamEmscripten_close(int fetchId);
38
39// Getter functions for fetch properties
40extern char *NetworkReadStreamEmscripten_getDataPtr(int fetchId);
41extern char *NetworkReadStreamEmscripten_getErr(int fetchId);
42extern uint32 NetworkReadStreamEmscripten_getNumBytes(int fetchId);
43extern char **NetworkReadStreamEmscripten_responseHeadersArray(int fetchId);
44extern unsigned short NetworkReadStreamEmscripten_status(int fetchId);
45extern uint32 NetworkReadStreamEmscripten_totalBytes(int fetchId);
46extern bool NetworkReadStreamEmscripten_completed(int fetchId);
47extern bool NetworkReadStreamEmscripten_hasError(int fetchId);
48}
49
50namespace Networking {
51
52class NetworkReadStreamEmscripten : public NetworkReadStream {
53private:
54 int _fetchId; // Fetch ID instead of pointer
55 Common::String _url;
56 RequestHeaders *_headersList;
57 uint32 _readPos; // Current read position in the JS buffer
58
59 // Helper methods
60 static char **buildHeadersArray(const RequestHeaders *headersList);
61 static char **buildFormFieldsArray(const Common::HashMap<Common::String, Common::String> &formFields);
62 static char **buildFormFilesArray(const Common::HashMap<Common::String, Common::Path> &formFiles);
63 static void cleanupStringArray(char **array);
64
65 void resetStream();
66 void setupBufferContents(const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post);
67 void setupFormMultipart(const Common::HashMap<Common::String, Common::String> &formFields, const Common::HashMap<Common::String, Common::Path> &formFiles);
68
69public:
70 NetworkReadStreamEmscripten(const char *url, RequestHeaders *headersList, const Common::String &postFields, bool uploading, bool usingPatch, bool keepAlive, long keepAliveIdle, long keepAliveInterval);
71
72 NetworkReadStreamEmscripten(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);
73
74 NetworkReadStreamEmscripten(const char *url, RequestHeaders *headersList, const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post, bool keepAlive, long keepAliveIdle, long keepAliveInterval);
75
76 ~NetworkReadStreamEmscripten() override;
77 void initFetch();
78
79 // NetworkReadStream interface
80 bool reuse(const char *url, RequestHeaders *headersList, const Common::String &postFields, bool uploading = false, bool usingPatch = false) override { return false; } // no reuse for Emscripten
81 bool reuse(const char *url, RequestHeaders *headersList, const Common::HashMap<Common::String, Common::String> &formFields, const Common::HashMap<Common::String, Common::Path> &formFiles) override { return false; } // no reuse for Emscripten
82 bool reuse(const char *url, RequestHeaders *headersList, const byte *buffer, uint32 bufferSize, bool uploading = false, bool usingPatch = false, bool post = false) override { return false; } // no reuse for Emscripten
83
84 bool hasError() const override;
85 const char *getError() const override;
86 double getProgress() const override;
87 long httpResponseCode() const override;
88 Common::String currentLocation() const override;
89 Common::HashMap<Common::String, Common::String> responseHeadersMap() const override;
90
91 uint32 read(void *dataPtr, uint32 dataSize) override;
92 bool eos() const override;
93};
94
95} // End of namespace Networking
96
97#endif // EMSCRIPTEN
98
99#endif
Definition: str.h:59