Skip to content

Commit b3a2beb

Browse files
authored
Merge pull request #135 from coding-cpp/fix/134
2 parents 794d37c + 3d7f276 commit b3a2beb

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

example/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ int main(int argc, char **argv) {
121121

122122
app.post("/print", [](Request &req, Response &res) {
123123
logger::info(req.body.dumps(2));
124-
res.status(STATUS_CODE::OK).send("Successfully printed!");
124+
json::object responseJson;
125+
responseJson["data"] = req.body;
126+
responseJson["timestamp"] = brewtils::date::getCurrentUTC();
127+
responseJson["printed"] = true;
128+
res.status(STATUS_CODE::OK).json(responseJson);
125129
});
126130

127131
// Starting the server

include/expresso/core/server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Server : public Router {
2626

2727
void setupMiddlewares();
2828
void acceptConnections();
29-
void handleConnection(int clientSocket);
29+
void handleConnection(int clientSocket) noexcept(false);
3030

3131
expresso::messages::Request makeRequest(std::string& request) noexcept(false);
3232
nexus::pool threadPool;

src/core/server.cpp

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,31 +93,49 @@ void expresso::core::Server::acceptConnections() {
9393
return;
9494
}
9595

96-
void expresso::core::Server::handleConnection(int clientSocket) {
96+
void expresso::core::Server::handleConnection(
97+
int clientSocket) noexcept(false) {
9798
constexpr size_t bufferSize = 4096;
9899
std::vector<char> charRequest;
99100
charRequest.resize(bufferSize, '\0');
100-
101101
size_t totalBytesRead = 0;
102-
size_t bytesRead;
103-
104-
do {
105-
bytesRead = brewtils::sys::recv(
106-
clientSocket, charRequest.data() + totalBytesRead, bufferSize - 1, 0);
107-
if (bytesRead == static_cast<ssize_t>(-1)) {
108-
logger::error(
109-
"Failed to receive data from client!",
110-
"void expresso::core::Server::handleConnection(int clientSocket)");
102+
103+
while (true) {
104+
// sanity check
105+
if (charRequest.size() - totalBytesRead < bufferSize) {
106+
charRequest.resize(charRequest.size() + bufferSize, '\0');
107+
}
108+
109+
ssize_t bytesRead = brewtils::sys::recv(
110+
clientSocket,
111+
charRequest.data() + totalBytesRead,
112+
bufferSize,
113+
0);
114+
115+
// miraculous happening
116+
if (bytesRead < 0) {
111117
close(clientSocket);
118+
logger::error("Failed to receive data from client!",
119+
"void expresso::core::Server::handleConnection(int clientSocket) noexcept(false)");
112120
return;
113121
}
114122

123+
// if client closed connection
124+
if (bytesRead == 0) {
125+
break;
126+
}
127+
115128
totalBytesRead += bytesRead;
116-
charRequest.resize(totalBytesRead + bufferSize, '\0');
117-
} while (bytesRead > 0 && charRequest[totalBytesRead - 1] != '\n');
129+
130+
// if end of headers reached
131+
if (std::string(charRequest.data(), totalBytesRead).find("\r\n\r\n") !=
132+
std::string::npos) {
133+
break;
134+
}
135+
}
118136

119137
charRequest.resize(totalBytesRead);
120-
std::string request(charRequest.data());
138+
std::string request(charRequest.data(), totalBytesRead);
121139
if (totalBytesRead == 0 || request.empty()) {
122140
close(clientSocket);
123141
return;
@@ -255,7 +273,7 @@ expresso::core::Server::makeRequest(std::string& request) noexcept(false) {
255273
key = brewtils::string::split(data[1], "\r\n")[0];
256274
key = key.substr(0, key.size() - 1);
257275
value = brewtils::string::split(data[1], "\r\n\r\n")[1];
258-
value = value.substr(0, value.size() - 3);
276+
value = brewtils::string::trim(value.substr(0, value.size() - 3));
259277
req.body[key] = json::object(value);
260278
}
261279
}

0 commit comments

Comments
 (0)