Added protocol error handling to the http server simple.

This commit is contained in:
Relintai 2023-02-19 10:34:39 +01:00
parent 6b7f56fddb
commit 8b4fc877a0
3 changed files with 34 additions and 9 deletions

View File

@ -29,10 +29,17 @@ bool HTTPParser::is_finished() const {
return !_request.is_valid();
}
bool HTTPParser::has_error() const {
return _error;
}
void HTTPParser::reset() {
_partial_data = "";
_is_ready = false;
_content_type = REQUEST_CONTENT_URLENCODED;
_error = false;
_request.unref();
_requests.clear();
}
//returns the index where processing was ended -> start of the next query if != data_length
@ -78,6 +85,8 @@ HTTPParser::HTTPParser() {
_multipart_parser_settings->on_body_end = _on_multipart_body_end_cb;
_multipart_parser = NULL;
_error = false;
}
HTTPParser::~HTTPParser() {
@ -223,8 +232,8 @@ int HTTPParser::on_message_begin() {
_request->set_method(HTTPServerEnums::HTTP_METHOD_PATCH);
break;
default:
//TODO close the connection
_request->set_method(HTTPServerEnums::HTTP_METHOD_INVALID);
_error = true;
break;
}
@ -296,7 +305,8 @@ int HTTPParser::on_header_value(const char *at, size_t length) {
int bs = s.find("boundary=");
if (bs == -1) {
//Error! boundary must exist TODO set an error variable and close the connection
//Error! boundary must exist
_error = true;
return 0;
}
@ -311,7 +321,8 @@ int HTTPParser::on_header_value(const char *at, size_t length) {
//The CRLF preceeding could also be appended for simpler logic
if (_multipart_boundary.empty()) {
//Error! TODO set an error variable and close the connection
//Error!
_error = true;
}
} else if (s.begins_with("text/plain")) {
@ -335,7 +346,7 @@ int HTTPParser::on_header_value(const char *at, size_t length) {
}
}
//TODO close connection on chunked connection (for now)
_error = true;
return 0;
}
@ -413,7 +424,10 @@ int HTTPParser::on_message_complete() {
process_urlenc_data();
}
_requests.push_back(_request);
if (!_error) {
_requests.push_back(_request);
}
_request.unref();
if (_multipart_parser) {

View File

@ -1,8 +1,8 @@
#ifndef HTTP_PARSER_H
#define HTTP_PARSER_H
#include "core/string/ustring.h"
#include "core/containers/vector.h"
#include "core/string/ustring.h"
#include "core/object/reference.h"
@ -22,12 +22,12 @@ public:
REQUEST_CONTENT_TEXT_PLAIN,
};
Ref<SimpleWebServerRequest>
get_next_request();
Ref<SimpleWebServerRequest> get_next_request();
int get_request_count() const;
bool is_ready() const;
bool is_finished() const;
bool has_error() const;
void reset();
@ -62,6 +62,8 @@ protected:
bool _multipart_form_is_file;
Vector<char> _multipart_form_data;
bool _error;
private:
String chr_len_to_str(const char *at, size_t length);

View File

@ -30,8 +30,8 @@
#include "http_server_simple.h"
#include "modules/web/http/web_server_cookie.h"
#include "http_parser.h"
#include "modules/web/http/web_server_cookie.h"
#include "simple_web_server_request.h"
#include "web_server_simple.h"
@ -86,6 +86,11 @@ void HTTPServerConnection::update() {
if (buffer_start_index >= read) {
break;
}
// Stop processing if a protocol error happened
if (_http_parser->has_error()) {
break;
}
}
}
@ -102,6 +107,10 @@ void HTTPServerConnection::update() {
close();
}
}
if (_http_parser->has_error()) {
close();
}
}
void HTTPServerConnection::send_redirect(Ref<WebServerRequest> request, const String &location, const HTTPServerEnums::HTTPStatusCode status_code) {