Skip to content

Commit e34e9cf

Browse files
committed
fix method type string mapping
1 parent 6f5e140 commit e34e9cf

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ target_include_directories(server
4343
$<INSTALL_INTERFACE:lib/mochios/include>
4444
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lib/mochios/include>
4545
)
46+
47+
target_compile_options(server PRIVATE -Wno-return-type)

example/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ int main(int argc, char **argv) {
119119
res.sendFiles(files, "assets.zip");
120120
});
121121

122+
app.post("/print", [](Request &req, Response &res) {
123+
logger::info(req.body.dumps(2));
124+
res.status(STATUS_CODE::OK).send("Successfully printed!");
125+
});
126+
122127
// Starting the server
123128
app.listen(port, []() {
124129
logger::success("Server is running on port " + std::to_string(port));

include/expresso/core/server.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class Server : public Router {
2121
size_t maxConnections;
2222
struct sockaddr_in address;
2323

24+
mochios::enums::method getMethodFromString(const std::string& method) noexcept(false);
25+
2426
void setupMiddlewares();
2527
void acceptConnections();
2628
void handleConnection(int clientSocket);

src/core/server.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ void expresso::core::Server::listen(int port, std::function<void()> callback) {
5555
return;
5656
}
5757

58+
mochios::enums::method expresso::core::Server::getMethodFromString(const std::string &method) noexcept(false) {
59+
if (method == "GET") return mochios::enums::method::GET;
60+
else if (method == "POST") return mochios::enums::method::POST;
61+
else if (method == "PUT") return mochios::enums::method::PUT;
62+
else if (method == "PATCH") return mochios::enums::method::PATCH;
63+
else if (method == "DELETE") return mochios::enums::method::DELETE;
64+
else if (method == "OPTIONS") return mochios::enums::method::OPTIONS;
65+
else if (method == "HEAD") return mochios::enums::method::HEAD;
66+
else logger::error("Unsupported HTTP method: " + method,
67+
"expresso::core::Server::getMethodFromString(std::string &method) noexcept(false)");
68+
}
69+
5870
void expresso::core::Server::setupMiddlewares() {
5971
this->use(std::make_unique<expresso::middleware::Version>());
6072
this->use(std::make_unique<expresso::middleware::Date>());
@@ -136,17 +148,8 @@ expresso::core::Server::makeRequest(std::string &request) noexcept(false) {
136148

137149
std::vector<std::string> parts = brewtils::string::split(line, " ");
138150
const std::string method = brewtils::string::upper(parts[0]);
139-
std::set<std::string>::const_iterator methodIter =
140-
mochios::enums::methods.find(method);
141-
if (methodIter == mochios::enums::methods.end()) {
142-
logger::error("Unsupported HTTP method: " + method,
143-
"expresso::core::Server::makeRequest(std::string &request) "
144-
"noexcept(false)");
145-
}
146-
147151
expresso::messages::Request req(parts[1]);
148-
req.method = static_cast<mochios::enums::method>(
149-
std::distance(mochios::enums::methods.begin(), methodIter) - 1);
152+
req.method = this->getMethodFromString(method);
150153
req.httpVersion = parts[2];
151154
if (req.httpVersion.substr(0, 5) != "HTTP/") {
152155
logger::error("Invalid HTTP version: " + req.httpVersion,

0 commit comments

Comments
 (0)