Debian Codegraph @

platform/web/export/editor_http_server.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* editor_http_server.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "editor_http_server.h"
32
33 void EditorHTTPServer::_server_thread_poll(void *data) {
34 EditorHTTPServer *web_server = static_cast<EditorHTTPServer *>(data);
35 while (!web_server->server_quit.is_set()) {
36 OS::get_singleton()->delay_usec(6900);
37 {
38 MutexLock lock(web_server->server_lock);
39 web_server->_poll();
40 }
41 }
42 }
43
44 void EditorHTTPServer::_clear_client() {
45 peer = Ref<StreamPeer>();
46 tls = Ref<StreamPeerTLS>();
47 tcp = Ref<StreamPeerTCP>();
48 memset(req_buf, 0, sizeof(req_buf));
49 time = 0;
50 req_pos = 0;
51 }
52
53 void EditorHTTPServer::_set_internal_certs(Ref<Crypto> p_crypto) {
54 const String cache_path = EditorPaths::get_singleton()->get_cache_dir();
55 const String key_path = cache_path.path_join("html5_server.key");
56 const String crt_path = cache_path.path_join("html5_server.crt");
57 bool regen = !FileAccess::exists(key_path) || !FileAccess::exists(crt_path);
58 if (!regen) {
59 key = Ref<CryptoKey>(CryptoKey::create());
60 cert = Ref<X509Certificate>(X509Certificate::create());
61 if (key->load(key_path) != OK || cert->load(crt_path) != OK) {
62 regen = true;
63 }
64 }
65 if (regen) {
66 key = p_crypto->generate_rsa(2048);
67 key->save(key_path);
68 cert = p_crypto->generate_self_signed_certificate(key, "CN=godot-debug.local,O=A Game Dev,C=XXA", "20140101000000", "20340101000000");
69 cert->save(crt_path);
70 }
71 }
72
73 void EditorHTTPServer::_send_response() {
74 Vector<String> psa = String((char *)req_buf).split("\r\n");
75 int len = psa.size();
76 ERR_FAIL_COND_MSG(len < 4, "Not enough response headers, got: " + itos(len) + ", expected >= 4.");
77
78 Vector<String> req = psa[0].split(" ", false);
79 ERR_FAIL_COND_MSG(req.size() < 2, "Invalid protocol or status code.");
80
81 // Wrong protocol
82 ERR_FAIL_COND_MSG(req[0] != "GET" || req[2] != "HTTP/1.1", "Invalid method or HTTP version.");
83
84 const int query_index = req[1].find_char('?');
85 const String path = (query_index == -1) ? req[1] : req[1].substr(0, query_index);
86
87 const String req_file = path.get_file();
88 const String req_ext = path.get_extension();
89 const String cache_path = EditorPaths::get_singleton()->get_temp_dir().path_join("web");
90 const String filepath = cache_path.path_join(req_file);
91
92 if (!mimes.has(req_ext) || !FileAccess::exists(filepath)) {
93 String s = "HTTP/1.1 404 Not Found\r\n";
94 s += "Connection: Close\r\n";
95 s += "\r\n";
96 CharString cs = s.utf8();
97 peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
98 return;
99 }
100 const String ctype = mimes[req_ext];
101
102 Ref<FileAccess> f = FileAccess::open(filepath, FileAccess::READ);
103 ERR_FAIL_COND(f.is_null());
104 String s = "HTTP/1.1 200 OK\r\n";
105 s += "Connection: Close\r\n";
106 s += "Content-Type: " + ctype + "\r\n";
107 s += "Access-Control-Allow-Origin: *\r\n";
108 s += "Cross-Origin-Opener-Policy: same-origin\r\n";
109 s += "Cross-Origin-Embedder-Policy: require-corp\r\n";
110 s += "Cache-Control: no-store, max-age=0\r\n";
111 s += "\r\n";
112 CharString cs = s.utf8();
113 Error err = peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
114 if (err != OK) {
115 ERR_FAIL();
116 }
117
118 while (true) {
119 uint8_t bytes[4096];
120 uint64_t read = f->get_buffer(bytes, 4096);
121 if (read == 0) {
122 break;
123 }
124 err = peer->put_data(bytes, read);
125 if (err != OK) {
126 ERR_FAIL();
127 }
128 }
129 }
130
131 void EditorHTTPServer::_poll() {
132 if (!server->is_listening()) {
133 return;
134 }
135 if (tcp.is_null()) {
136 if (!server->is_connection_available()) {
137 return;
138 }
139 tcp = server->take_connection();
140 peer = tcp;
141 time = OS::get_singleton()->get_ticks_usec();
142 }
143 if (OS::get_singleton()->get_ticks_usec() - time > 1000000) {
144 _clear_client();
145 return;
146 }
147 if (tcp->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
148 return;
149 }
150
151 if (use_tls) {
152 if (tls.is_null()) {
153 tls = Ref<StreamPeerTLS>(StreamPeerTLS::create());
154 peer = tls;
155 if (tls->accept_stream(tcp, TLSOptions::server(key, cert)) != OK) {
156 _clear_client();
157 return;
158 }
159 }
160 tls->poll();
161 if (tls->get_status() == StreamPeerTLS::STATUS_HANDSHAKING) {
162 // Still handshaking, keep waiting.
163 return;
164 }
165 if (tls->get_status() != StreamPeerTLS::STATUS_CONNECTED) {
166 _clear_client();
167 return;
168 }
169 }
170
171 while (true) {
172 char *r = (char *)req_buf;
173 int l = req_pos - 1;
174 if (l > 3 && r[l] == '\n' && r[l - 1] == '\r' && r[l - 2] == '\n' && r[l - 3] == '\r') {
175 _send_response();
176 _clear_client();
177 return;
178 }
179
180 int read = 0;
181 ERR_FAIL_COND(req_pos >= 4096);
182 Error err = peer->get_partial_data(&req_buf[req_pos], 1, read);
183 if (err != OK) {
184 // Got an error
185 _clear_client();
186 return;
187 } else if (read != 1) {
188 // Busy, wait next poll
189 return;
190 }
191 req_pos += read;
192 }
193 }
194
195 void EditorHTTPServer::stop() {
196 server_quit.set();
197 if (server_thread.is_started()) {
198 server_thread.wait_to_finish();
199 }
200 if (server.is_valid()) {
201 server->stop();
202 }
203 _clear_client();
204 }
205
206 Error EditorHTTPServer::listen(int p_port, IPAddress p_address, bool p_use_tls, String p_tls_key, String p_tls_cert) {
207 MutexLock lock(server_lock);
208 if (server->is_listening()) {
209 return ERR_ALREADY_IN_USE;
210 }
211 use_tls = p_use_tls;
212 if (use_tls) {
213 Ref<Crypto> crypto = Crypto::create();
214 if (crypto.is_null()) {
215 return ERR_UNAVAILABLE;
216 }
217 if (!p_tls_key.is_empty() && !p_tls_cert.is_empty()) {
218 key = Ref<CryptoKey>(CryptoKey::create());
219 Error err = key->load(p_tls_key);
220 ERR_FAIL_COND_V(err != OK, err);
221 cert = Ref<X509Certificate>(X509Certificate::create());
222 err = cert->load(p_tls_cert);
223 ERR_FAIL_COND_V(err != OK, err);
224 } else {
225 _set_internal_certs(crypto);
226 }
227 }
228 Error err = server->listen(p_port, p_address);
229 if (err == OK) {
230 server_quit.clear();
231 server_thread.start(_server_thread_poll, this);
232 }
233 return err;
234 }
235
236 bool EditorHTTPServer::is_listening() const {
237 MutexLock lock(server_lock);
238 return server->is_listening();
239 }
240
241 EditorHTTPServer::EditorHTTPServer() {
242 mimes["html"] = "text/html";
243 mimes["js"] = "application/javascript";
244 mimes["json"] = "application/json";
245 mimes["pck"] = "application/octet-stream";
246 mimes["png"] = "image/png";
247 mimes["svg"] = "image/svg";
248 mimes["wasm"] = "application/wasm";
249 server.instantiate();
250 stop();
251 }
252
253 EditorHTTPServer::~EditorHTTPServer() {
254 stop();
255 }