diff options
-rw-r--r-- | minion/src/gymnasiearbete.cpp | 16 | ||||
-rw-r--r-- | minion/src/http/request.cpp | 42 | ||||
-rw-r--r-- | minion/src/http/request.hpp | 14 | ||||
-rw-r--r-- | minion/src/wifi_module.cpp | 10 | ||||
-rw-r--r-- | minion/src/wifi_module.hpp | 5 |
5 files changed, 63 insertions, 24 deletions
diff --git a/minion/src/gymnasiearbete.cpp b/minion/src/gymnasiearbete.cpp index 47e386e..092250a 100644 --- a/minion/src/gymnasiearbete.cpp +++ b/minion/src/gymnasiearbete.cpp @@ -74,35 +74,33 @@ void setup() void loop() { - const auto *request = wifi_module.read_incoming_request(); + const auto request = wifi_module.read_incoming_request(); - if (request == nullptr) + if (request.connection_id() == -1) { return; } - const auto connection_id = request->connection_id(); + const auto connection_id = request.connection_id(); Serial.print("Connection ID: "); Serial.println(static_cast<unsigned int>(connection_id)); Serial.print("Request method: "); - Serial.println(http_request_method_strs[static_cast<size_t>(request->method())]); + Serial.println(http_request_method_strs[static_cast<size_t>(request.method())]); Serial.print("Request HTTP version: "); - Serial.println(request->http_version()); + Serial.println(request.http_version()); Serial.print("Request path: "); - Serial.println(request->path()); + Serial.println(request.path()); Serial.print("\nData: "); - Serial.println(request->data()); + Serial.println(request.data()); wifi_module.send_response(connection_id, 200U, "hello there!"); wifi_module.close_connection(connection_id); - delete request; - delay(SECOND_IN_MILLIS); } diff --git a/minion/src/http/request.cpp b/minion/src/http/request.cpp index 9a35d37..9baf7cc 100644 --- a/minion/src/http/request.cpp +++ b/minion/src/http/request.cpp @@ -55,7 +55,7 @@ auto str_to_http_request_method(const char *http_request_method_str) -> HTTPRequ } HTTPRequest::HTTPRequest( - size_t connection_id, + int connection_id, HTTPRequestMethod method, char *http_version, // NOLINT(bugprone-easily-swappable-parameters) char *path, @@ -71,12 +71,23 @@ HTTPRequest::HTTPRequest( { } +HTTPRequest::HTTPRequest(HTTPRequest &&other) noexcept + : _connection_id(other._connection_id), + _method(other._method), + _http_version(other._http_version), + _path(other._path), + _data_length(other._data_length), + _data(other._data) +{ + other._data = nullptr; +} + HTTPRequest::~HTTPRequest() noexcept { free(_data); } -auto HTTPRequest::connection_id() const noexcept -> size_t +auto HTTPRequest::connection_id() const noexcept -> int { return _connection_id; } @@ -105,3 +116,30 @@ auto HTTPRequest::data() const noexcept -> const char * { return _data; } + +auto HTTPRequest::operator=(HTTPRequest &&other) noexcept -> HTTPRequest & +{ + if (this != &other) + { + free(_data); + _data = other._data; + other._data = nullptr; + } + + return *this; +} + +HTTPRequest HTTPRequest::create_invalid() noexcept +{ + return HTTPRequest(); +} + +HTTPRequest::HTTPRequest() noexcept + : _connection_id(-1), + _method(HTTPRequestMethod(-1)), + _http_version(""), + _path(""), + _data_length(0), + _data(nullptr) +{ +} diff --git a/minion/src/http/request.hpp b/minion/src/http/request.hpp index 85af722..8524dc6 100644 --- a/minion/src/http/request.hpp +++ b/minion/src/http/request.hpp @@ -25,7 +25,7 @@ class HTTPRequest { public: explicit HTTPRequest( - size_t connection_id, + int connection_id, HTTPRequestMethod method, char *http_version, char *path, @@ -35,11 +35,11 @@ public: HTTPRequest(const HTTPRequest &other) noexcept = delete; - HTTPRequest(HTTPRequest &&other) noexcept = delete; + HTTPRequest(HTTPRequest &&other) noexcept; ~HTTPRequest() noexcept; - auto connection_id() const noexcept -> size_t; + auto connection_id() const noexcept -> int; auto method() const noexcept -> HTTPRequestMethod; @@ -53,13 +53,17 @@ public: auto operator=(const HTTPRequest &other) noexcept -> HTTPRequest & = delete; - auto operator=(HTTPRequest &&other) noexcept -> HTTPRequest & = delete; + auto operator=(HTTPRequest &&other) noexcept -> HTTPRequest &; + + static HTTPRequest create_invalid() noexcept; private: - const size_t _connection_id; + const int _connection_id; const HTTPRequestMethod _method; char *_http_version; char *_path; const int _data_length; char *_data; + + HTTPRequest() noexcept; }; diff --git a/minion/src/wifi_module.cpp b/minion/src/wifi_module.cpp index 2f4e57c..dbcd737 100644 --- a/minion/src/wifi_module.cpp +++ b/minion/src/wifi_module.cpp @@ -228,13 +228,13 @@ auto WiFiModule::get_local_ip(char *local_ip_out) noexcept -> const char * return local_ip_out; } -auto WiFiModule::read_incoming_request() noexcept -> HTTPRequest * +auto WiFiModule::read_incoming_request() noexcept -> HTTPRequest { char request_prefix[] = "+IPD,"; if (get_available() == 0 || !_serial.find(request_prefix)) { - return nullptr; + return HTTPRequest::create_invalid(); } const auto min_available_bytes = 5; @@ -273,15 +273,15 @@ auto WiFiModule::read_incoming_request() noexcept -> HTTPRequest * if (request_data == nullptr) { - return nullptr; + return HTTPRequest::create_invalid(); } strcpy(request_data, ""); _read_bytes(request_data, request_data_length, TIMEOUT_LONG); - return new HTTPRequest( - connection_id, + return HTTPRequest( + static_cast<int>(connection_id), request_method, http_version, request_path, diff --git a/minion/src/wifi_module.hpp b/minion/src/wifi_module.hpp index 447f056..bac5998 100644 --- a/minion/src/wifi_module.hpp +++ b/minion/src/wifi_module.hpp @@ -91,10 +91,9 @@ public: /** * Reads a incoming HTTP request. * - * @returns A pointer to the request. Has to be deleted after use. Is nullptr if - * reading the request failed. + * @returns A request. Has a connection ID of -1 if reading the request failed. */ - auto read_incoming_request() noexcept -> HTTPRequest *; + auto read_incoming_request() noexcept -> HTTPRequest; auto close_connection(size_t connection_id) noexcept -> bool; |