diff --git a/example/main.cpp b/example/main.cpp index 913f3f5..eacb9c7 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -121,7 +121,11 @@ int main(int argc, char **argv) { app.post("/print", [](Request &req, Response &res) { logger::info(req.body.dumps(2)); - res.status(STATUS_CODE::OK).send("Successfully printed!"); + json::object responseJson; + responseJson["data"] = req.body; + responseJson["timestamp"] = brewtils::date::getCurrentUTC(); + responseJson["printed"] = true; + res.status(STATUS_CODE::OK).json(responseJson); }); // Starting the server diff --git a/include/expresso/core/server.h b/include/expresso/core/server.h index 6b48998..b40cd18 100644 --- a/include/expresso/core/server.h +++ b/include/expresso/core/server.h @@ -26,7 +26,7 @@ class Server : public Router { void setupMiddlewares(); void acceptConnections(); - void handleConnection(int clientSocket); + void handleConnection(int clientSocket) noexcept(false); expresso::messages::Request makeRequest(std::string& request) noexcept(false); nexus::pool threadPool; diff --git a/src/core/server.cpp b/src/core/server.cpp index afcda5a..578955a 100644 --- a/src/core/server.cpp +++ b/src/core/server.cpp @@ -93,31 +93,49 @@ void expresso::core::Server::acceptConnections() { return; } -void expresso::core::Server::handleConnection(int clientSocket) { +void expresso::core::Server::handleConnection( + int clientSocket) noexcept(false) { constexpr size_t bufferSize = 4096; std::vector charRequest; charRequest.resize(bufferSize, '\0'); - size_t totalBytesRead = 0; - size_t bytesRead; - - do { - bytesRead = brewtils::sys::recv( - clientSocket, charRequest.data() + totalBytesRead, bufferSize - 1, 0); - if (bytesRead == static_cast(-1)) { - logger::error( - "Failed to receive data from client!", - "void expresso::core::Server::handleConnection(int clientSocket)"); + + while (true) { + // sanity check + if (charRequest.size() - totalBytesRead < bufferSize) { + charRequest.resize(charRequest.size() + bufferSize, '\0'); + } + + ssize_t bytesRead = brewtils::sys::recv( + clientSocket, + charRequest.data() + totalBytesRead, + bufferSize, + 0); + + // miraculous happening + if (bytesRead < 0) { close(clientSocket); + logger::error("Failed to receive data from client!", + "void expresso::core::Server::handleConnection(int clientSocket) noexcept(false)"); return; } + // if client closed connection + if (bytesRead == 0) { + break; + } + totalBytesRead += bytesRead; - charRequest.resize(totalBytesRead + bufferSize, '\0'); - } while (bytesRead > 0 && charRequest[totalBytesRead - 1] != '\n'); + + // if end of headers reached + if (std::string(charRequest.data(), totalBytesRead).find("\r\n\r\n") != + std::string::npos) { + break; + } + } charRequest.resize(totalBytesRead); - std::string request(charRequest.data()); + std::string request(charRequest.data(), totalBytesRead); if (totalBytesRead == 0 || request.empty()) { close(clientSocket); return; @@ -255,7 +273,7 @@ expresso::core::Server::makeRequest(std::string& request) noexcept(false) { key = brewtils::string::split(data[1], "\r\n")[0]; key = key.substr(0, key.size() - 1); value = brewtils::string::split(data[1], "\r\n\r\n")[1]; - value = value.substr(0, value.size() - 3); + value = brewtils::string::trim(value.substr(0, value.size() - 3)); req.body[key] = json::object(value); } }