Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ target_include_directories(server
$<INSTALL_INTERFACE:lib/mochios/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lib/mochios/include>
)

target_compile_options(server PRIVATE -Wno-return-type)
5 changes: 5 additions & 0 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 2 additions & 0 deletions include/expresso/core/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 13 additions & 10 deletions src/core/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ void expresso::core::Server::listen(int port, std::function<void()> 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<expresso::middleware::Version>());
this->use(std::make_unique<expresso::middleware::Date>());
Expand Down Expand Up @@ -136,17 +148,8 @@ expresso::core::Server::makeRequest(std::string &request) noexcept(false) {

std::vector<std::string> parts = brewtils::string::split(line, " ");
const std::string method = brewtils::string::upper(parts[0]);
std::set<std::string>::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<mochios::enums::method>(
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,
Expand Down