Skip to content

Commit a2f4e29

Browse files
committed
Add set_keep_alive_timeout
1 parent b8cf739 commit a2f4e29

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

httplib.h

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5
1717
#endif
1818

19-
#ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND
20-
#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND 0
21-
#endif
22-
2319
#ifndef CPPHTTPLIB_KEEPALIVE_MAX_COUNT
2420
#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 5
2521
#endif
@@ -180,6 +176,7 @@ using socket_t = int;
180176
#include <array>
181177
#include <atomic>
182178
#include <cassert>
179+
#include <cctype>
183180
#include <climits>
184181
#include <condition_variable>
185182
#include <errno.h>
@@ -196,7 +193,6 @@ using socket_t = int;
196193
#include <string>
197194
#include <sys/stat.h>
198195
#include <thread>
199-
#include <cctype>
200196

201197
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
202198
#include <openssl/err.h>
@@ -596,6 +592,7 @@ class Server {
596592
void set_socket_options(SocketOptions socket_options);
597593

598594
void set_keep_alive_max_count(size_t count);
595+
void set_keep_alive_timeout(time_t sec);
599596
void set_read_timeout(time_t sec, time_t usec = 0);
600597
void set_write_timeout(time_t sec, time_t usec = 0);
601598
void set_idle_interval(time_t sec, time_t usec = 0);
@@ -620,6 +617,7 @@ class Server {
620617

621618
std::atomic<socket_t> svr_sock_;
622619
size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT;
620+
time_t keep_alive_timeout_sec_ = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND;
623621
time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND;
624622
time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND;
625623
time_t write_timeout_sec_ = CPPHTTPLIB_WRITE_TIMEOUT_SECOND;
@@ -1740,7 +1738,7 @@ class BufferStream : public Stream {
17401738
size_t position = 0;
17411739
};
17421740

1743-
inline bool keep_alive(socket_t sock) {
1741+
inline bool keep_alive(socket_t sock, time_t keep_alive_timeout_sec) {
17441742
using namespace std::chrono;
17451743
auto start = steady_clock::now();
17461744
while (true) {
@@ -1750,8 +1748,7 @@ inline bool keep_alive(socket_t sock) {
17501748
} else if (val == 0) {
17511749
auto current = steady_clock::now();
17521750
auto duration = duration_cast<milliseconds>(current - start);
1753-
auto timeout = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND * 1000 +
1754-
CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND / 1000;
1751+
auto timeout = keep_alive_timeout_sec * 1000;
17551752
if (duration.count() > timeout) { return false; }
17561753
std::this_thread::sleep_for(std::chrono::milliseconds(1));
17571754
} else {
@@ -1761,13 +1758,13 @@ inline bool keep_alive(socket_t sock) {
17611758
}
17621759

17631760
template <typename T>
1764-
inline bool process_server_socket_core(socket_t sock,
1765-
size_t keep_alive_max_count,
1766-
T callback) {
1761+
inline bool
1762+
process_server_socket_core(socket_t sock, size_t keep_alive_max_count,
1763+
time_t keep_alive_timeout_sec, T callback) {
17671764
assert(keep_alive_max_count > 0);
17681765
auto ret = false;
17691766
auto count = keep_alive_max_count;
1770-
while (count > 0 && keep_alive(sock)) {
1767+
while (count > 0 && keep_alive(sock, keep_alive_timeout_sec)) {
17711768
auto close_connection = count == 1;
17721769
auto connection_closed = false;
17731770
ret = callback(close_connection, connection_closed);
@@ -1778,13 +1775,13 @@ inline bool process_server_socket_core(socket_t sock,
17781775
}
17791776

17801777
template <typename T>
1781-
inline bool process_server_socket(socket_t sock, size_t keep_alive_max_count,
1782-
time_t read_timeout_sec,
1783-
time_t read_timeout_usec,
1784-
time_t write_timeout_sec,
1785-
time_t write_timeout_usec, T callback) {
1778+
inline bool
1779+
process_server_socket(socket_t sock, size_t keep_alive_max_count,
1780+
time_t keep_alive_timeout_sec, time_t read_timeout_sec,
1781+
time_t read_timeout_usec, time_t write_timeout_sec,
1782+
time_t write_timeout_usec, T callback) {
17861783
return process_server_socket_core(
1787-
sock, keep_alive_max_count,
1784+
sock, keep_alive_max_count, keep_alive_timeout_sec,
17881785
[&](bool close_connection, bool connection_closed) {
17891786
SocketStream strm(sock, read_timeout_sec, read_timeout_usec,
17901787
write_timeout_sec, write_timeout_usec);
@@ -2397,9 +2394,7 @@ class brotli_decompressor : public decompressor {
23972394

23982395
if (decoder_r == BROTLI_DECODER_RESULT_ERROR) { return false; }
23992396

2400-
if (!callback(buff.data(), buff.size() - avail_out)) {
2401-
return false;
2402-
}
2397+
if (!callback(buff.data(), buff.size() - avail_out)) { return false; }
24032398
}
24042399

24052400
return decoder_r == BROTLI_DECODER_RESULT_SUCCESS ||
@@ -3896,6 +3891,10 @@ inline void Server::set_keep_alive_max_count(size_t count) {
38963891
keep_alive_max_count_ = count;
38973892
}
38983893

3894+
inline void Server::set_keep_alive_timeout(time_t sec) {
3895+
keep_alive_timeout_sec_ = sec;
3896+
}
3897+
38993898
inline void Server::set_read_timeout(time_t sec, time_t usec) {
39003899
read_timeout_sec_ = sec;
39013900
read_timeout_usec_ = usec;
@@ -3979,10 +3978,11 @@ inline bool Server::write_response(Stream &strm, bool close_connection,
39793978
// Headers
39803979
if (close_connection || req.get_header_value("Connection") == "close") {
39813980
res.set_header("Connection", "close");
3982-
}
3983-
3984-
if (!close_connection && req.get_header_value("Connection") == "Keep-Alive") {
3985-
res.set_header("Connection", "Keep-Alive");
3981+
} else {
3982+
std::stringstream ss;
3983+
ss << "timeout=" << keep_alive_timeout_sec_
3984+
<< ", max=" << keep_alive_max_count_;
3985+
res.set_header("Keep-Alive", ss.str());
39863986
}
39873987

39883988
if (!res.has_header("Content-Type") &&
@@ -4583,8 +4583,8 @@ inline bool Server::is_valid() const { return true; }
45834583

45844584
inline bool Server::process_and_close_socket(socket_t sock) {
45854585
auto ret = detail::process_server_socket(
4586-
sock, keep_alive_max_count_, read_timeout_sec_, read_timeout_usec_,
4587-
write_timeout_sec_, write_timeout_usec_,
4586+
sock, keep_alive_max_count_, keep_alive_timeout_sec_, read_timeout_sec_,
4587+
read_timeout_usec_, write_timeout_sec_, write_timeout_usec_,
45884588
[this](Stream &strm, bool close_connection, bool &connection_closed) {
45894589
return process_request(strm, close_connection, connection_closed,
45904590
nullptr);
@@ -5527,11 +5527,12 @@ inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl,
55275527
template <typename T>
55285528
inline bool
55295529
process_server_socket_ssl(SSL *ssl, socket_t sock, size_t keep_alive_max_count,
5530+
time_t keep_alive_timeout_sec,
55305531
time_t read_timeout_sec, time_t read_timeout_usec,
55315532
time_t write_timeout_sec, time_t write_timeout_usec,
55325533
T callback) {
55335534
return process_server_socket_core(
5534-
sock, keep_alive_max_count,
5535+
sock, keep_alive_max_count, keep_alive_timeout_sec,
55355536
[&](bool close_connection, bool connection_closed) {
55365537
SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec,
55375538
write_timeout_sec, write_timeout_usec);
@@ -5738,8 +5739,9 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) {
57385739

57395740
if (ssl) {
57405741
auto ret = detail::process_server_socket_ssl(
5741-
ssl, sock, keep_alive_max_count_, read_timeout_sec_, read_timeout_usec_,
5742-
write_timeout_sec_, write_timeout_usec_,
5742+
ssl, sock, keep_alive_max_count_, keep_alive_timeout_sec_,
5743+
read_timeout_sec_, read_timeout_usec_, write_timeout_sec_,
5744+
write_timeout_usec_,
57435745
[this, ssl](Stream &strm, bool close_connection,
57445746
bool &connection_closed) {
57455747
return process_request(strm, close_connection, connection_closed,

0 commit comments

Comments
 (0)