Implement sending keep alive connection type if a connection has more than one request for HTTPServerSimple. Also more togglable debugging.

This commit is contained in:
Relintai 2023-02-19 15:11:29 +01:00
parent 0e651dd4fd
commit e5f31bde7d
2 changed files with 76 additions and 4 deletions

View File

@ -37,6 +37,9 @@
#include "simple_web_server_request.h" #include "simple_web_server_request.h"
#include "web_server_simple.h" #include "web_server_simple.h"
#define CONNECTION_OPEN_CLOSE_DEBUG 0
#define CONNECTION_RESPOSE_DEBUG 0
void HTTPServerConnection::update() { void HTTPServerConnection::update() {
ERR_FAIL_COND(closed()); ERR_FAIL_COND(closed());
@ -59,11 +62,14 @@ void HTTPServerConnection::update() {
return; return;
} }
} }
ssl->poll(); ssl->poll();
if (ssl->get_status() == StreamPeerSSL::STATUS_HANDSHAKING) { if (ssl->get_status() == StreamPeerSSL::STATUS_HANDSHAKING) {
// Still handshaking, keep waiting. // Still handshaking, keep waiting.
return; return;
} }
if (ssl->get_status() != StreamPeerSSL::STATUS_CONNECTED) { if (ssl->get_status() != StreamPeerSSL::STATUS_CONNECTED) {
close(); close();
return; return;
@ -119,7 +125,12 @@ void HTTPServerConnection::send_redirect(Ref<WebServerRequest> request, const St
//String s = "HTTP/1.1 " + itos(static_cast<int>(status_code)) + " Found\r\n"; //String s = "HTTP/1.1 " + itos(static_cast<int>(status_code)) + " Found\r\n";
String s = "HTTP/1.1 " + HTTPServerEnums::get_status_code_header_string(status_code) + "\r\n"; String s = "HTTP/1.1 " + HTTPServerEnums::get_status_code_header_string(status_code) + "\r\n";
s += "Location: " + location + "\r\n"; s += "Location: " + location + "\r\n";
s += "Connection: Close\r\n";
if (has_more_messages()) {
s += "Connection: keep-alive\r\n";
} else {
s += "Connection: close\r\n";
}
for (int i = 0; i < request->response_get_cookie_count(); ++i) { for (int i = 0; i < request->response_get_cookie_count(); ++i) {
Ref<WebServerCookie> cookie = request->response_get_cookie(i); Ref<WebServerCookie> cookie = request->response_get_cookie(i);
@ -134,6 +145,11 @@ void HTTPServerConnection::send_redirect(Ref<WebServerRequest> request, const St
} }
s += "\r\n"; s += "\r\n";
#if CONNECTION_RESPOSE_DEBUG
ERR_PRINT(s);
#endif
CharString cs = s.utf8(); CharString cs = s.utf8();
peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1); peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
} }
@ -144,7 +160,12 @@ void HTTPServerConnection::send(Ref<WebServerRequest> request) {
String s = "HTTP/1.1 " + HTTPServerEnums::get_status_code_header_string(request->get_status_code()) + "\r\n"; String s = "HTTP/1.1 " + HTTPServerEnums::get_status_code_header_string(request->get_status_code()) + "\r\n";
s += "Content-Length: " + itos(body.size()) + "\r\n"; s += "Content-Length: " + itos(body.size()) + "\r\n";
s += "Content-type: text/html\r\n"; s += "Content-type: text/html\r\n";
s += "Connection: Close\r\n";
if (has_more_messages()) {
s += "Connection: keep-alive\r\n";
} else {
s += "Connection: close\r\n";
}
for (int i = 0; i < request->response_get_cookie_count(); ++i) { for (int i = 0; i < request->response_get_cookie_count(); ++i) {
Ref<WebServerCookie> cookie = request->response_get_cookie(i); Ref<WebServerCookie> cookie = request->response_get_cookie(i);
@ -161,13 +182,22 @@ void HTTPServerConnection::send(Ref<WebServerRequest> request) {
s += "\r\n"; s += "\r\n";
s += body; s += body;
#if CONNECTION_RESPOSE_DEBUG
ERR_PRINT(s);
#endif
CharString cs = s.utf8(); CharString cs = s.utf8();
peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1); peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
} }
void HTTPServerConnection::send_file(Ref<WebServerRequest> request, const String &p_file_path) { void HTTPServerConnection::send_file(Ref<WebServerRequest> request, const String &p_file_path) {
if (!FileAccess::exists(p_file_path)) { if (!FileAccess::exists(p_file_path)) {
String s = "HTTP/1.1 404 Not Found\r\n"; String s = "HTTP/1.1 404 Not Found\r\n";
s += "Connection: Close\r\n";
if (has_more_messages()) {
s += "Connection: keep-alive\r\n";
} else {
s += "Connection: close\r\n";
}
for (int i = 0; i < request->response_get_cookie_count(); ++i) { for (int i = 0; i < request->response_get_cookie_count(); ++i) {
Ref<WebServerCookie> cookie = request->response_get_cookie(i); Ref<WebServerCookie> cookie = request->response_get_cookie(i);
@ -182,6 +212,11 @@ void HTTPServerConnection::send_file(Ref<WebServerRequest> request, const String
} }
s += "\r\n"; s += "\r\n";
#if CONNECTION_RESPOSE_DEBUG
ERR_PRINT(s);
#endif
CharString cs = s.utf8(); CharString cs = s.utf8();
peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1); peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
return; return;
@ -199,7 +234,13 @@ void HTTPServerConnection::send_file(Ref<WebServerRequest> request, const String
FileAccess *f = FileAccess::open(p_file_path, FileAccess::READ); FileAccess *f = FileAccess::open(p_file_path, FileAccess::READ);
ERR_FAIL_COND(!f); ERR_FAIL_COND(!f);
String s = "HTTP/1.1 200 OK\r\n"; String s = "HTTP/1.1 200 OK\r\n";
s += "Connection: Close\r\n";
if (has_more_messages()) {
s += "Connection: keep-alive\r\n";
} else {
s += "Connection: close\r\n";
}
s += "Content-Type: " + ctype + "\r\n"; s += "Content-Type: " + ctype + "\r\n";
for (int i = 0; i < request->response_get_cookie_count(); ++i) { for (int i = 0; i < request->response_get_cookie_count(); ++i) {
@ -219,6 +260,11 @@ void HTTPServerConnection::send_file(Ref<WebServerRequest> request, const String
s += "Cross-Origin-Embedder-Policy: require-corp\r\n"; s += "Cross-Origin-Embedder-Policy: require-corp\r\n";
s += "Cache-Control: no-store, max-age=0\r\n"; s += "Cache-Control: no-store, max-age=0\r\n";
s += "\r\n"; s += "\r\n";
#if CONNECTION_RESPOSE_DEBUG
ERR_PRINT(s);
#endif
CharString cs = s.utf8(); CharString cs = s.utf8();
Error err = peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1); Error err = peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
@ -244,6 +290,10 @@ void HTTPServerConnection::send_file(Ref<WebServerRequest> request, const String
} }
void HTTPServerConnection::close() { void HTTPServerConnection::close() {
#if CONNECTION_OPEN_CLOSE_DEBUG
ERR_PRINT("CONN CLOSE");
#endif
tcp.unref(); tcp.unref();
ssl.unref(); ssl.unref();
peer.unref(); peer.unref();
@ -254,6 +304,22 @@ bool HTTPServerConnection::closed() {
return _closed; return _closed;
} }
bool HTTPServerConnection::has_more_messages() {
if (_closed) {
return false;
}
if (_http_parser->has_error()) {
return false;
}
if (_http_parser->get_request_count() == 0 && _http_parser->is_finished()) {
return false;
}
return true;
}
HTTPServerConnection::HTTPServerConnection() { HTTPServerConnection::HTTPServerConnection() {
_web_server = nullptr; _web_server = nullptr;
_http_server = nullptr; _http_server = nullptr;
@ -335,6 +401,10 @@ void HTTPServerSimple::poll() {
ERR_CONTINUE(!tcp.is_valid()); ERR_CONTINUE(!tcp.is_valid());
#if CONNECTION_OPEN_CLOSE_DEBUG
ERR_PRINT("NEW CONN");
#endif
Ref<HTTPServerConnection> connection; Ref<HTTPServerConnection> connection;
connection.instance(); connection.instance();

View File

@ -61,6 +61,8 @@ public:
void close(); void close();
bool closed(); bool closed();
bool has_more_messages();
HTTPServerConnection(); HTTPServerConnection();
~HTTPServerConnection(); ~HTTPServerConnection();