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
1 change: 1 addition & 0 deletions include/Server/MultiSocketWebserver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MultiSocketWebserver {
void _acceptConnection(int server_fd);
bool _handleClientData(int client_fd);
[[nodiscard]] bool isServerFd(int fd) const;
static void _setSocketTimeouts(int socketFd, size_t timeoutSec);

public:
explicit MultiSocketWebserver(std::vector<std::vector<ServerConfig>> servers_config);
Expand Down
8 changes: 2 additions & 6 deletions src/Server/ClientConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,15 +449,9 @@ void ClientConnection::sendResponse() {
_bytesSendToClient = 0;
}

LOG_INFO(_log("Sending response with status code: " + std::to_string(_response.getStatus())));
LOG_TRACE(_log("Response: \n" + _response.toString()));
const size_t remainingBytes = _response.toString().size() - _bytesSendToClient;
const size_t bytesToSend = std::min(SIZE_BYTES_TO_SEND_BACK, remainingBytes);

LOG_DEBUG(_log("Preparing to send chunk. Total size: " + std::to_string(_response.toString().size()) +
", Bytes sent so far: " + std::to_string(_bytesSendToClient) +
", Chunk size: " + std::to_string(bytesToSend)));

if (!_sendDataToClient(_response.toString(), _bytesSendToClient, bytesToSend)) {
LOG_ERROR(_log("Failed to send chunk. Bytes sent so far: " + std::to_string(_bytesSendToClient)));
return;
Expand All @@ -467,6 +461,8 @@ void ClientConnection::sendResponse() {
", Total bytes sent: " + std::to_string(_bytesSendToClient)));

if (_bytesSendToClient == _response.toString().size()) {
LOG_INFO(_log("Sending response with status code: " + std::to_string(_response.getStatus())));
LOG_TRACE(_log("Response: \n" + _response.toString()));
if (_response.getHeader("Connection") == "keep-alive") {
LOG_INFO(_log("Connection is keep-alive"));
_status = Status::HEADER;
Expand Down
26 changes: 20 additions & 6 deletions src/Server/MultiSocketWebserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,12 @@ void MultiSocketWebserver::_acceptConnection(const int server_fd) {
return;
}

timeval tv{};
tv.tv_sec = 5;
tv.tv_usec = 0;
setsockopt(clientFd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
auto server_configs = _sockets.at(server_fd)->getConfig();

_setSocketTimeouts(clientFd, 5);

try {
_clients.emplace(clientFd,
std::make_unique<ClientConnection>(clientFd, clientAddr, _sockets.at(server_fd)->getConfig()));
_clients.emplace(clientFd, std::make_unique<ClientConnection>(clientFd, clientAddr, server_configs));
_polls.addFd(clientFd);
LOG_INFO("Accepted connection from " + std::string(my_inet_ntoa(clientAddr.sin_addr)) + " on socket " +
std::to_string(clientFd));
Expand Down Expand Up @@ -170,3 +168,19 @@ bool MultiSocketWebserver::_handleClientWrite(int fd) {

return true;
}

void MultiSocketWebserver::_setSocketTimeouts(const int socketFd, const size_t timeoutSec) {
timeval tv{};
tv.tv_sec = timeoutSec;
tv.tv_usec = 0;

// Set receive timeout
if (setsockopt(socketFd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
LOG_ERROR("Error setting receive timeout: " + std::string(strerror(errno)));
}

// Set send timeout
if (setsockopt(socketFd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
LOG_ERROR("Error setting send timeout: " + std::string(strerror(errno)));
}
}
Loading