diff --git a/CMakeLists.txt b/CMakeLists.txt index 903d197..ea1b9d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,3 +43,5 @@ target_include_directories(server $ $ ) + +target_compile_options(server PRIVATE -Wno-return-type) diff --git a/example/main.cpp b/example/main.cpp index 7b94e01..913f3f5 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -119,6 +119,11 @@ int main(int argc, char **argv) { res.sendFiles(files, "assets.zip"); }); + app.post("/print", [](Request &req, Response &res) { + logger::info(req.body.dumps(2)); + res.status(STATUS_CODE::OK).send("Successfully printed!"); + }); + // Starting the server app.listen(port, []() { logger::success("Server is running on port " + std::to_string(port)); diff --git a/include/expresso/core/server.h b/include/expresso/core/server.h index 039f368..bfce4a5 100644 --- a/include/expresso/core/server.h +++ b/include/expresso/core/server.h @@ -21,6 +21,8 @@ class Server : public Router { size_t maxConnections; struct sockaddr_in address; + mochios::enums::method getMethodFromString(const std::string& method) noexcept(false); + void setupMiddlewares(); void acceptConnections(); void handleConnection(int clientSocket); diff --git a/src/core/server.cpp b/src/core/server.cpp index e492164..6eb7f71 100644 --- a/src/core/server.cpp +++ b/src/core/server.cpp @@ -55,6 +55,18 @@ void expresso::core::Server::listen(int port, std::function callback) { return; } +mochios::enums::method expresso::core::Server::getMethodFromString(const std::string &method) noexcept(false) { + if (method == "GET") return mochios::enums::method::GET; + else if (method == "POST") return mochios::enums::method::POST; + else if (method == "PUT") return mochios::enums::method::PUT; + else if (method == "PATCH") return mochios::enums::method::PATCH; + else if (method == "DELETE") return mochios::enums::method::DELETE; + else if (method == "OPTIONS") return mochios::enums::method::OPTIONS; + else if (method == "HEAD") return mochios::enums::method::HEAD; + else logger::error("Unsupported HTTP method: " + method, + "expresso::core::Server::getMethodFromString(std::string &method) noexcept(false)"); +} + void expresso::core::Server::setupMiddlewares() { this->use(std::make_unique()); this->use(std::make_unique()); @@ -136,17 +148,8 @@ expresso::core::Server::makeRequest(std::string &request) noexcept(false) { std::vector parts = brewtils::string::split(line, " "); const std::string method = brewtils::string::upper(parts[0]); - std::set::const_iterator methodIter = - mochios::enums::methods.find(method); - if (methodIter == mochios::enums::methods.end()) { - logger::error("Unsupported HTTP method: " + method, - "expresso::core::Server::makeRequest(std::string &request) " - "noexcept(false)"); - } - expresso::messages::Request req(parts[1]); - req.method = static_cast( - std::distance(mochios::enums::methods.begin(), methodIter) - 1); + req.method = this->getMethodFromString(method); req.httpVersion = parts[2]; if (req.httpVersion.substr(0, 5) != "HTTP/") { logger::error("Invalid HTTP version: " + req.httpVersion,