editor_http_server.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #include "editor_http_server.h"
  31. void EditorHTTPServer::_server_thread_poll(void *data) {
  32. EditorHTTPServer *web_server = static_cast<EditorHTTPServer *>(data);
  33. while (!web_server->server_quit.is_set()) {
  34. OS::get_singleton()->delay_usec(6900);
  35. {
  36. MutexLock lock(web_server->server_lock);
  37. web_server->_poll();
  38. }
  39. }
  40. }
  41. void EditorHTTPServer::_clear_client() {
  42. peer = Ref<StreamPeer>();
  43. tls = Ref<StreamPeerTLS>();
  44. tcp = Ref<StreamPeerTCP>();
  45. memset(req_buf, 0, sizeof(req_buf));
  46. time = 0;
  47. req_pos = 0;
  48. }
  49. void EditorHTTPServer::_set_internal_certs(Ref<Crypto> p_crypto) {
  50. const String cache_path = EditorPaths::get_singleton()->get_cache_dir();
  51. const String key_path = cache_path.path_join("html5_server.key");
  52. const String crt_path = cache_path.path_join("html5_server.crt");
  53. bool regen = !FileAccess::exists(key_path) || !FileAccess::exists(crt_path);
  54. if (!regen) {
  55. key = Ref<CryptoKey>(CryptoKey::create());
  56. cert = Ref<X509Certificate>(X509Certificate::create());
  57. if (key->load(key_path) != OK || cert->load(crt_path) != OK) {
  58. regen = true;
  59. }
  60. }
  61. if (regen) {
  62. key = p_crypto->generate_rsa(2048);
  63. key->save(key_path);
  64. cert = p_crypto->generate_self_signed_certificate(key, "CN=godot-debug.local,O=A Game Dev,C=XXA", "20140101000000", "20340101000000");
  65. cert->save(crt_path);
  66. }
  67. }
  68. void EditorHTTPServer::_send_response() {
  69. Vector<String> psa = String((char *)req_buf).split("\r\n");
  70. int len = psa.size();
  71. ERR_FAIL_COND_MSG(len < 4, "Not enough response headers, got: " + itos(len) + ", expected >= 4.");
  72. Vector<String> req = psa[0].split(" ", false);
  73. ERR_FAIL_COND_MSG(req.size() < 2, "Invalid protocol or status code.");
  74. // Wrong protocol
  75. ERR_FAIL_COND_MSG(req[0] != "GET" || req[2] != "HTTP/1.1", "Invalid method or HTTP version.");
  76. const int query_index = req[1].find_char('?');
  77. const String path = (query_index == -1) ? req[1] : req[1].substr(0, query_index);
  78. const String req_file = path.get_file();
  79. const String req_ext = path.get_extension();
  80. const String cache_path = EditorPaths::get_singleton()->get_temp_dir().path_join("web");
  81. const String filepath = cache_path.path_join(req_file);
  82. if (!mimes.has(req_ext) || !FileAccess::exists(filepath)) {
  83. String s = "HTTP/1.1 404 Not Found\r\n";
  84. s += "Connection: Close\r\n";
  85. s += "\r\n";
  86. CharString cs = s.utf8();
  87. peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
  88. return;
  89. }
  90. const String ctype = mimes[req_ext];
  91. Ref<FileAccess> f = FileAccess::open(filepath, FileAccess::READ);
  92. ERR_FAIL_COND(f.is_null());
  93. String s = "HTTP/1.1 200 OK\r\n";
  94. s += "Connection: Close\r\n";
  95. s += "Content-Type: " + ctype + "\r\n";
  96. s += "Access-Control-Allow-Origin: *\r\n";
  97. s += "Cross-Origin-Opener-Policy: same-origin\r\n";
  98. s += "Cross-Origin-Embedder-Policy: require-corp\r\n";
  99. s += "Cache-Control: no-store, max-age=0\r\n";
  100. s += "\r\n";
  101. CharString cs = s.utf8();
  102. Error err = peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
  103. if (err != OK) {
  104. ERR_FAIL();
  105. }
  106. while (true) {
  107. uint8_t bytes[4096];
  108. uint64_t read = f->get_buffer(bytes, 4096);
  109. if (read == 0) {
  110. break;
  111. }
  112. err = peer->put_data(bytes, read);
  113. if (err != OK) {
  114. ERR_FAIL();
  115. }
  116. }
  117. }
  118. void EditorHTTPServer::_poll() {
  119. if (!server->is_listening()) {
  120. return;
  121. }
  122. if (tcp.is_null()) {
  123. if (!server->is_connection_available()) {
  124. return;
  125. }
  126. tcp = server->take_connection();
  127. peer = tcp;
  128. time = OS::get_singleton()->get_ticks_usec();
  129. }
  130. if (OS::get_singleton()->get_ticks_usec() - time > 1000000) {
  131. _clear_client();
  132. return;
  133. }
  134. if (tcp->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
  135. return;
  136. }
  137. if (use_tls) {
  138. if (tls.is_null()) {
  139. tls = Ref<StreamPeerTLS>(StreamPeerTLS::create());
  140. peer = tls;
  141. if (tls->accept_stream(tcp, TLSOptions::server(key, cert)) != OK) {
  142. _clear_client();
  143. return;
  144. }
  145. }
  146. tls->poll();
  147. if (tls->get_status() == StreamPeerTLS::STATUS_HANDSHAKING) {
  148. // Still handshaking, keep waiting.
  149. return;
  150. }
  151. if (tls->get_status() != StreamPeerTLS::STATUS_CONNECTED) {
  152. _clear_client();
  153. return;
  154. }
  155. }
  156. while (true) {
  157. char *r = (char *)req_buf;
  158. int l = req_pos - 1;
  159. if (l > 3 && r[l] == '\n' && r[l - 1] == '\r' && r[l - 2] == '\n' && r[l - 3] == '\r') {
  160. _send_response();
  161. _clear_client();
  162. return;
  163. }
  164. int read = 0;
  165. ERR_FAIL_COND(req_pos >= 4096);
  166. Error err = peer->get_partial_data(&req_buf[req_pos], 1, read);
  167. if (err != OK) {
  168. // Got an error
  169. _clear_client();
  170. return;
  171. } else if (read != 1) {
  172. // Busy, wait next poll
  173. return;
  174. }
  175. req_pos += read;
  176. }
  177. }
  178. void EditorHTTPServer::stop() {
  179. server_quit.set();
  180. if (server_thread.is_started()) {
  181. server_thread.wait_to_finish();
  182. }
  183. if (server.is_valid()) {
  184. server->stop();
  185. }
  186. _clear_client();
  187. }
  188. Error EditorHTTPServer::listen(int p_port, IPAddress p_address, bool p_use_tls, String p_tls_key, String p_tls_cert) {
  189. MutexLock lock(server_lock);
  190. if (server->is_listening()) {
  191. return ERR_ALREADY_IN_USE;
  192. }
  193. use_tls = p_use_tls;
  194. if (use_tls) {
  195. Ref<Crypto> crypto = Crypto::create();
  196. if (crypto.is_null()) {
  197. return ERR_UNAVAILABLE;
  198. }
  199. if (!p_tls_key.is_empty() && !p_tls_cert.is_empty()) {
  200. key = Ref<CryptoKey>(CryptoKey::create());
  201. Error err = key->load(p_tls_key);
  202. ERR_FAIL_COND_V(err != OK, err);
  203. cert = Ref<X509Certificate>(X509Certificate::create());
  204. err = cert->load(p_tls_cert);
  205. ERR_FAIL_COND_V(err != OK, err);
  206. } else {
  207. _set_internal_certs(crypto);
  208. }
  209. }
  210. Error err = server->listen(p_port, p_address);
  211. if (err == OK) {
  212. server_quit.clear();
  213. server_thread.start(_server_thread_poll, this);
  214. }
  215. return err;
  216. }
  217. bool EditorHTTPServer::is_listening() const {
  218. MutexLock lock(server_lock);
  219. return server->is_listening();
  220. }
  221. EditorHTTPServer::EditorHTTPServer() {
  222. mimes["html"] = "text/html";
  223. mimes["js"] = "application/javascript";
  224. mimes["json"] = "application/json";
  225. mimes["pck"] = "application/octet-stream";
  226. mimes["png"] = "image/png";
  227. mimes["svg"] = "image/svg";
  228. mimes["wasm"] = "application/wasm";
  229. server.instantiate();
  230. stop();
  231. }
  232. EditorHTTPServer::~EditorHTTPServer() {
  233. stop();
  234. }