Implemented sending responses. (In an extremely simple way for now.)

This commit is contained in:
Relintai 2022-07-01 18:25:45 +02:00
parent 24e120142e
commit a7b705ebea
5 changed files with 100 additions and 0 deletions

View File

@ -23,6 +23,10 @@ bool HTTPParser::is_ready() const {
return _is_ready; return _is_ready;
} }
bool HTTPParser::is_finished() const {
return !_request.is_valid();
}
void HTTPParser::reset() { void HTTPParser::reset() {
_partial_data = ""; _partial_data = "";
_is_ready = false; _is_ready = false;

View File

@ -17,6 +17,7 @@ public:
int get_request_count() const; int get_request_count() const;
bool is_ready() const; bool is_ready() const;
bool is_finished() const;
void reset(); void reset();

View File

@ -205,9 +205,86 @@ void HTTPServerSimple::poll() {
request->_server = this; request->_server = this;
_web_server->server_handle_request(request); _web_server->server_handle_request(request);
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) {
String s = "HTTP/1.1 302 Found\r\n";
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)) {
const String ctype = mimes[req_ext];
} 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();
}
}
memdelete(f);
}
HTTPServerSimple::HTTPServerSimple() { HTTPServerSimple::HTTPServerSimple() {
_web_server = nullptr; _web_server = nullptr;

View File

@ -37,6 +37,8 @@
#include "core/project_settings.h" #include "core/project_settings.h"
#include "../http/http_server_enums.h"
class HTTPParser; class HTTPParser;
class WebServerSimple; class WebServerSimple;
@ -49,6 +51,10 @@ public:
void _send_response(); void _send_response();
void poll(); void poll();
void send_redirect(const String &location, const HTTPServerEnums::HTTPStatusCode status_code);
void send(const String &body);
void send_file(const String &p_file_path);
HTTPServerSimple(); HTTPServerSimple();
~HTTPServerSimple(); ~HTTPServerSimple();

View File

@ -10,6 +10,7 @@
#include "../http/web_node.h" #include "../http/web_node.h"
#include "../http/web_permission.h" #include "../http/web_permission.h"
#include "http_server_simple.h"
String SimpleWebServerRequest::get_cookie(const String &key) { String SimpleWebServerRequest::get_cookie(const String &key) {
return ""; return "";
@ -49,9 +50,16 @@ String SimpleWebServerRequest::get_parameter(const String &key) const {
} }
void SimpleWebServerRequest::send_redirect(const String &location, const HTTPServerEnums::HTTPStatusCode status_code) { void SimpleWebServerRequest::send_redirect(const String &location, const HTTPServerEnums::HTTPStatusCode status_code) {
ERR_FAIL_COND(!_server);
_server->send_redirect(location, status_code);
} }
void SimpleWebServerRequest::send() { void SimpleWebServerRequest::send() {
ERR_FAIL_COND(!_server);
_server->send(compiled_body);
// if (connection_closed) { // if (connection_closed) {
// SimpleWebServerRequestPool::return_request(this); // SimpleWebServerRequestPool::return_request(this);
// return; // return;
@ -61,6 +69,10 @@ void SimpleWebServerRequest::send() {
} }
void SimpleWebServerRequest::send_file(const String &p_file_path) { void SimpleWebServerRequest::send_file(const String &p_file_path) {
ERR_FAIL_COND(!_server);
_server->send_file(p_file_path);
// SimpleWebServerRequestPool::return_request(this); // SimpleWebServerRequestPool::return_request(this);
} }