2022-06-27 16:38:35 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* export.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* PANDEMONIUM ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
#include "http_server_simple.h"
|
2022-06-27 16:38:35 +02:00
|
|
|
|
2022-06-30 23:18:45 +02:00
|
|
|
#include "http_parser.h"
|
2022-06-30 23:12:32 +02:00
|
|
|
#include "simple_web_server_request.h"
|
2022-07-01 17:40:47 +02:00
|
|
|
#include "web_server_simple.h"
|
2022-06-30 23:12:32 +02:00
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
void HTTPServerSimple::stop() {
|
|
|
|
server->stop();
|
|
|
|
_clear_client();
|
|
|
|
}
|
2022-06-27 16:38:35 +02:00
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
Error HTTPServerSimple::listen(int p_port, IP_Address p_address, bool p_use_ssl, String p_ssl_key, String p_ssl_cert) {
|
|
|
|
use_ssl = p_use_ssl;
|
|
|
|
if (use_ssl) {
|
|
|
|
Ref<Crypto> crypto = Crypto::create();
|
|
|
|
if (crypto.is_null()) {
|
|
|
|
return ERR_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
if (!p_ssl_key.empty() && !p_ssl_cert.empty()) {
|
2022-06-27 16:38:35 +02:00
|
|
|
key = Ref<CryptoKey>(CryptoKey::create());
|
2022-06-30 16:28:23 +02:00
|
|
|
Error err = key->load(p_ssl_key);
|
|
|
|
ERR_FAIL_COND_V(err != OK, err);
|
2022-06-27 16:38:35 +02:00
|
|
|
cert = Ref<X509Certificate>(X509Certificate::create());
|
2022-06-30 16:28:23 +02:00
|
|
|
err = cert->load(p_ssl_cert);
|
|
|
|
ERR_FAIL_COND_V(err != OK, err);
|
|
|
|
} else {
|
|
|
|
_set_internal_certs(crypto);
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-30 16:28:23 +02:00
|
|
|
return server->listen(p_port, p_address);
|
|
|
|
}
|
2022-06-27 16:38:35 +02:00
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
bool HTTPServerSimple::is_listening() const {
|
|
|
|
return server->is_listening();
|
|
|
|
}
|
2022-06-27 16:38:35 +02:00
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
void HTTPServerSimple::_send_response() {
|
|
|
|
Vector<String> psa = String((char *)req_buf).split("\r\n");
|
|
|
|
int len = psa.size();
|
|
|
|
ERR_FAIL_COND_MSG(len < 4, "Not enough response headers, got: " + itos(len) + ", expected >= 4.");
|
2022-06-27 16:38:35 +02:00
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
Vector<String> req = psa[0].split(" ", false);
|
|
|
|
ERR_FAIL_COND_MSG(req.size() < 2, "Invalid protocol or status code.");
|
2022-06-27 16:38:35 +02:00
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
// Wrong protocol
|
|
|
|
ERR_FAIL_COND_MSG(req[0] != "GET" || req[2] != "HTTP/1.1", "Invalid method or HTTP version.");
|
2022-06-27 16:38:35 +02:00
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
const int query_index = req[1].find_char('?');
|
|
|
|
const String path = (query_index == -1) ? req[1] : req[1].substr(0, query_index);
|
2022-06-27 16:38:35 +02:00
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
const String req_file = path.get_file();
|
|
|
|
const String req_ext = path.get_extension();
|
2022-06-30 17:15:57 +02:00
|
|
|
//const String cache_path = EditorSettings::get_singleton()->get_cache_dir().plus_file("web");
|
|
|
|
//TODO
|
|
|
|
const String cache_path = "./cache/web/";
|
2022-06-30 16:28:23 +02:00
|
|
|
const String filepath = cache_path.plus_file(req_file);
|
2022-06-27 16:38:35 +02:00
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
if (!mimes.has(req_ext) || !FileAccess::exists(filepath)) {
|
|
|
|
String s = "HTTP/1.1 404 Not Found\r\n";
|
2022-06-27 16:38:35 +02:00
|
|
|
s += "Connection: Close\r\n";
|
|
|
|
s += "\r\n";
|
|
|
|
CharString cs = s.utf8();
|
2022-06-30 16:28:23 +02:00
|
|
|
peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const String ctype = mimes[req_ext];
|
|
|
|
|
|
|
|
FileAccess *f = FileAccess::open(filepath, FileAccess::READ);
|
|
|
|
ERR_FAIL_COND(!f);
|
|
|
|
String s = "HTTP/1.1 200 OK\r\n";
|
|
|
|
s += "Connection: Close\r\n";
|
|
|
|
s += "Content-Type: " + ctype + "\r\n";
|
|
|
|
s += "Access-Control-Allow-Origin: *\r\n";
|
|
|
|
s += "Cross-Origin-Opener-Policy: same-origin\r\n";
|
|
|
|
s += "Cross-Origin-Embedder-Policy: require-corp\r\n";
|
|
|
|
s += "Cache-Control: no-store, max-age=0\r\n";
|
|
|
|
s += "\r\n";
|
|
|
|
CharString cs = s.utf8();
|
2022-06-30 18:06:02 +02:00
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
Error err = peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
|
|
|
|
if (err != OK) {
|
2022-06-27 16:38:35 +02:00
|
|
|
memdelete(f);
|
2022-06-30 16:28:23 +02:00
|
|
|
ERR_FAIL();
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
while (true) {
|
|
|
|
uint8_t bytes[4096];
|
|
|
|
uint64_t read = f->get_buffer(bytes, 4096);
|
|
|
|
if (read == 0) {
|
|
|
|
break;
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
2022-06-30 16:28:23 +02:00
|
|
|
err = peer->put_data(bytes, read);
|
2022-06-27 16:38:35 +02:00
|
|
|
if (err != OK) {
|
2022-06-30 16:28:23 +02:00
|
|
|
memdelete(f);
|
|
|
|
ERR_FAIL();
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-30 18:06:02 +02:00
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
memdelete(f);
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
void HTTPServerSimple::poll() {
|
|
|
|
if (!server->is_listening()) {
|
|
|
|
return;
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
2022-06-30 16:28:23 +02:00
|
|
|
if (tcp.is_null()) {
|
|
|
|
if (!server->is_connection_available()) {
|
|
|
|
return;
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
2022-06-30 16:28:23 +02:00
|
|
|
tcp = server->take_connection();
|
|
|
|
peer = tcp;
|
|
|
|
time = OS::get_singleton()->get_ticks_usec();
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
2022-06-30 16:28:23 +02:00
|
|
|
if (OS::get_singleton()->get_ticks_usec() - time > 1000000) {
|
|
|
|
_clear_client();
|
|
|
|
return;
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
2022-06-30 16:28:23 +02:00
|
|
|
if (tcp->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
|
|
|
|
return;
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
if (use_ssl) {
|
|
|
|
if (ssl.is_null()) {
|
|
|
|
ssl = Ref<StreamPeerSSL>(StreamPeerSSL::create());
|
|
|
|
peer = ssl;
|
|
|
|
ssl->set_blocking_handshake_enabled(false);
|
|
|
|
if (ssl->accept_stream(tcp, key, cert) != OK) {
|
|
|
|
_clear_client();
|
|
|
|
return;
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-30 16:28:23 +02:00
|
|
|
ssl->poll();
|
|
|
|
if (ssl->get_status() == StreamPeerSSL::STATUS_HANDSHAKING) {
|
|
|
|
// Still handshaking, keep waiting.
|
|
|
|
return;
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
2022-06-30 16:28:23 +02:00
|
|
|
if (ssl->get_status() != StreamPeerSSL::STATUS_CONNECTED) {
|
|
|
|
_clear_client();
|
|
|
|
return;
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 23:12:32 +02:00
|
|
|
//char *r = (char *)req_buf;
|
|
|
|
//int l = req_pos - 1;
|
|
|
|
//if (l > 3 && r[l] == '\n' && r[l - 1] == '\r' && r[l - 2] == '\n' && r[l - 3] == '\r') {
|
|
|
|
// _send_response();
|
|
|
|
// _clear_client();
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
|
|
|
int read = 0;
|
|
|
|
Error err = peer->get_partial_data(req_buf, 4096, read);
|
|
|
|
|
|
|
|
if (err != OK) {
|
|
|
|
// Got an error
|
|
|
|
_clear_client();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-01 17:40:47 +02:00
|
|
|
if (read > 0) {
|
|
|
|
int buffer_start_index = 0;
|
|
|
|
while (true) {
|
|
|
|
char *rb = reinterpret_cast<char *>(&req_buf[buffer_start_index]);
|
|
|
|
buffer_start_index += _http_parser->read_from_buffer(rb, read);
|
2022-06-30 23:12:32 +02:00
|
|
|
|
2022-07-01 17:40:47 +02:00
|
|
|
if (buffer_start_index >= read) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-30 23:12:32 +02:00
|
|
|
|
2022-07-01 17:40:47 +02:00
|
|
|
if (_http_parser->get_request_count() > 0) {
|
|
|
|
Ref<SimpleWebServerRequest> request = _http_parser->get_next_request();
|
2022-06-30 23:12:32 +02:00
|
|
|
|
2022-07-01 17:40:47 +02:00
|
|
|
request->_server = this;
|
2022-07-01 18:50:42 +02:00
|
|
|
request->setup_url_stack();
|
2022-06-27 16:38:35 +02:00
|
|
|
|
2022-07-01 17:40:47 +02:00
|
|
|
_web_server->server_handle_request(request);
|
2022-07-01 18:25:45 +02:00
|
|
|
|
|
|
|
if (_http_parser->get_request_count() == 0 && _http_parser->is_finished()) {
|
|
|
|
_clear_client();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPServerSimple::send_redirect(const String &location, const HTTPServerEnums::HTTPStatusCode status_code) {
|
|
|
|
//String s = "HTTP/1.1 " + itos(static_cast<int>(status_code)) + " Found\r\n";
|
|
|
|
String s = "HTTP/1.1 302 Found\r\n";
|
|
|
|
s += "Location: " + location + "\r\n";
|
|
|
|
s += "Connection: Close\r\n";
|
|
|
|
s += "\r\n";
|
|
|
|
CharString cs = s.utf8();
|
|
|
|
peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
|
|
|
|
}
|
|
|
|
void HTTPServerSimple::send(const String &body) {
|
2022-07-21 00:54:51 +02:00
|
|
|
String s = "HTTP/1.1 200 OK\r\n";
|
2022-07-01 18:25:45 +02:00
|
|
|
s += "Content-Length: " + itos(body.size()) + "\r\n";
|
|
|
|
s += "Content-type: text/html\r\n";
|
|
|
|
s += "Connection: Close\r\n";
|
|
|
|
s += "\r\n";
|
|
|
|
s += body;
|
|
|
|
|
|
|
|
CharString cs = s.utf8();
|
|
|
|
peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
|
|
|
|
}
|
|
|
|
void HTTPServerSimple::send_file(const String &p_file_path) {
|
|
|
|
if (!FileAccess::exists(p_file_path)) {
|
|
|
|
String s = "HTTP/1.1 404 Not Found\r\n";
|
|
|
|
s += "Connection: Close\r\n";
|
|
|
|
s += "\r\n";
|
|
|
|
CharString cs = s.utf8();
|
|
|
|
peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String ctype;
|
|
|
|
String req_ext = p_file_path.get_extension();
|
|
|
|
|
|
|
|
if (!mimes.has(req_ext)) {
|
2022-07-01 22:17:35 +02:00
|
|
|
ctype = mimes[req_ext];
|
2022-07-01 18:25:45 +02:00
|
|
|
} else {
|
|
|
|
ctype = "text/plain";
|
|
|
|
}
|
|
|
|
|
|
|
|
FileAccess *f = FileAccess::open(p_file_path, FileAccess::READ);
|
|
|
|
ERR_FAIL_COND(!f);
|
|
|
|
String s = "HTTP/1.1 200 OK\r\n";
|
|
|
|
s += "Connection: Close\r\n";
|
|
|
|
s += "Content-Type: " + ctype + "\r\n";
|
|
|
|
s += "Access-Control-Allow-Origin: *\r\n";
|
|
|
|
s += "Cross-Origin-Opener-Policy: same-origin\r\n";
|
|
|
|
s += "Cross-Origin-Embedder-Policy: require-corp\r\n";
|
|
|
|
s += "Cache-Control: no-store, max-age=0\r\n";
|
|
|
|
s += "\r\n";
|
|
|
|
CharString cs = s.utf8();
|
|
|
|
|
|
|
|
Error err = peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
|
|
|
|
if (err != OK) {
|
|
|
|
memdelete(f);
|
|
|
|
ERR_FAIL();
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
uint8_t bytes[4096];
|
|
|
|
uint64_t read = f->get_buffer(bytes, 4096);
|
|
|
|
if (read == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
err = peer->put_data(bytes, read);
|
|
|
|
if (err != OK) {
|
|
|
|
memdelete(f);
|
|
|
|
ERR_FAIL();
|
|
|
|
}
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
2022-07-01 18:25:45 +02:00
|
|
|
|
|
|
|
memdelete(f);
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
HTTPServerSimple::HTTPServerSimple() {
|
2022-07-01 17:40:47 +02:00
|
|
|
_web_server = nullptr;
|
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
mimes["html"] = "text/html";
|
|
|
|
mimes["js"] = "application/javascript";
|
|
|
|
mimes["json"] = "application/json";
|
|
|
|
mimes["pck"] = "application/octet-stream";
|
|
|
|
mimes["png"] = "image/png";
|
|
|
|
mimes["svg"] = "image/svg";
|
|
|
|
mimes["wasm"] = "application/wasm";
|
2022-07-01 17:40:47 +02:00
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
server.instance();
|
2022-06-30 23:12:32 +02:00
|
|
|
_http_parser.instance();
|
2022-06-30 16:28:23 +02:00
|
|
|
stop();
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
|
|
|
|
2022-06-30 23:18:45 +02:00
|
|
|
HTTPServerSimple::~HTTPServerSimple() {
|
|
|
|
}
|
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
void HTTPServerSimple::_clear_client() {
|
|
|
|
peer = Ref<StreamPeer>();
|
|
|
|
ssl = Ref<StreamPeerSSL>();
|
|
|
|
tcp = Ref<StreamPeerTCP>();
|
|
|
|
memset(req_buf, 0, sizeof(req_buf));
|
|
|
|
time = 0;
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
|
|
|
|
2022-06-30 16:28:23 +02:00
|
|
|
void HTTPServerSimple::_set_internal_certs(Ref<Crypto> p_crypto) {
|
2022-06-30 17:15:57 +02:00
|
|
|
//const String cache_path = EditorSettings::get_singleton()->get_cache_dir();
|
|
|
|
//TODO
|
|
|
|
const String cache_path = "./cache/web/";
|
2022-06-30 16:28:23 +02:00
|
|
|
const String key_path = cache_path.plus_file("html5_server.key");
|
|
|
|
const String crt_path = cache_path.plus_file("html5_server.crt");
|
|
|
|
bool regen = !FileAccess::exists(key_path) || !FileAccess::exists(crt_path);
|
|
|
|
if (!regen) {
|
|
|
|
key = Ref<CryptoKey>(CryptoKey::create());
|
|
|
|
cert = Ref<X509Certificate>(X509Certificate::create());
|
|
|
|
if (key->load(key_path) != OK || cert->load(crt_path) != OK) {
|
|
|
|
regen = true;
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-30 16:28:23 +02:00
|
|
|
if (regen) {
|
|
|
|
key = p_crypto->generate_rsa(2048);
|
|
|
|
key->save(key_path);
|
|
|
|
cert = p_crypto->generate_self_signed_certificate(key, "CN=pandemonium-debug.local,O=A Game Dev,C=XXA", "20140101000000", "20340101000000");
|
|
|
|
cert->save(crt_path);
|
2022-06-27 16:38:35 +02:00
|
|
|
}
|
|
|
|
}
|