diff --git a/modules/http_server_simple/http_parser.cpp b/modules/http_server_simple/http_parser.cpp index 333f738c7..62b67539b 100644 --- a/modules/http_server_simple/http_parser.cpp +++ b/modules/http_server_simple/http_parser.cpp @@ -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) { diff --git a/modules/http_server_simple/http_parser.h b/modules/http_server_simple/http_parser.h index e15102f46..25c05737f 100644 --- a/modules/http_server_simple/http_parser.h +++ b/modules/http_server_simple/http_parser.h @@ -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 - get_next_request(); + Ref 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 _multipart_form_data; + bool _error; + private: String chr_len_to_str(const char *at, size_t length); diff --git a/modules/http_server_simple/http_server_simple.cpp b/modules/http_server_simple/http_server_simple.cpp index eb8a2d5e0..9e96bfb21 100644 --- a/modules/http_server_simple/http_server_simple.cpp +++ b/modules/http_server_simple/http_server_simple.cpp @@ -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 request, const String &location, const HTTPServerEnums::HTTPStatusCode status_code) {