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
6 changes: 5 additions & 1 deletion example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion include/expresso/core/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
48 changes: 33 additions & 15 deletions src/core/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> 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<ssize_t>(-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;
Expand Down Expand Up @@ -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);
}
}
Expand Down