4040#define CPPHTTPLIB_WRITE_TIMEOUT_USECOND 0
4141#endif
4242
43+ #ifndef CPPHTTPLIB_IDLE_INTERVAL_SECOND
44+ #define CPPHTTPLIB_IDLE_INTERVAL_SECOND 0
45+ #endif
46+
47+ #ifndef CPPHTTPLIB_IDLE_INTERVAL_USECOND
48+ #define CPPHTTPLIB_IDLE_INTERVAL_USECOND 100000
49+ #endif
50+
4351#ifndef CPPHTTPLIB_REQUEST_URI_MAX_LENGTH
4452#define CPPHTTPLIB_REQUEST_URI_MAX_LENGTH 8192
4553#endif
@@ -518,6 +526,7 @@ class Server {
518526 void set_keep_alive_max_count (size_t count);
519527 void set_read_timeout (time_t sec, time_t usec);
520528 void set_write_timeout (time_t sec, time_t usec);
529+ void set_idle_interval (time_t sec, time_t usec);
521530 void set_payload_max_length (size_t length);
522531
523532 bool bind_to_port (const char *host, int port, int socket_flags = 0 );
@@ -536,12 +545,14 @@ class Server {
536545 bool &connection_close,
537546 const std::function<void (Request &)> &setup_request);
538547
539- size_t keep_alive_max_count_;
540- time_t read_timeout_sec_;
541- time_t read_timeout_usec_;
542- time_t write_timeout_sec_;
543- time_t write_timeout_usec_;
544- size_t payload_max_length_;
548+ size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT;
549+ time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND;
550+ time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND;
551+ time_t write_timeout_sec_ = CPPHTTPLIB_WRITE_TIMEOUT_SECOND;
552+ time_t write_timeout_usec_ = CPPHTTPLIB_WRITE_TIMEOUT_USECOND;
553+ time_t idle_interval_sec_ = CPPHTTPLIB_IDLE_INTERVAL_SECOND;
554+ time_t idle_interval_usec_ = CPPHTTPLIB_IDLE_INTERVAL_USECOND;
555+ size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH;
545556
546557private:
547558 using Handlers = std::vector<std::pair<std::regex, Handler>>;
@@ -3459,14 +3470,7 @@ inline const std::string &BufferStream::get_buffer() const { return buffer; }
34593470} // namespace detail
34603471
34613472// HTTP server implementation
3462- inline Server::Server ()
3463- : keep_alive_max_count_(CPPHTTPLIB_KEEPALIVE_MAX_COUNT),
3464- read_timeout_sec_(CPPHTTPLIB_READ_TIMEOUT_SECOND),
3465- read_timeout_usec_(CPPHTTPLIB_READ_TIMEOUT_USECOND),
3466- write_timeout_sec_(CPPHTTPLIB_WRITE_TIMEOUT_SECOND),
3467- write_timeout_usec_(CPPHTTPLIB_WRITE_TIMEOUT_USECOND),
3468- payload_max_length_(CPPHTTPLIB_PAYLOAD_MAX_LENGTH), is_running_(false ),
3469- svr_sock_(INVALID_SOCKET) {
3473+ inline Server::Server () : is_running_(false ), svr_sock_(INVALID_SOCKET) {
34703474#ifndef _WIN32
34713475 signal (SIGPIPE, SIG_IGN);
34723476#endif
@@ -3592,6 +3596,11 @@ inline void Server::set_write_timeout(time_t sec, time_t usec) {
35923596 write_timeout_usec_ = usec;
35933597}
35943598
3599+ inline void Server::set_idle_interval (time_t sec, time_t usec) {
3600+ idle_interval_sec_ = sec;
3601+ idle_interval_usec_ = usec;
3602+ }
3603+
35953604inline void Server::set_payload_max_length (size_t length) {
35963605 payload_max_length_ = length;
35973606}
@@ -3964,17 +3973,14 @@ inline bool Server::listen_internal() {
39643973 {
39653974 std::unique_ptr<TaskQueue> task_queue (new_task_queue ());
39663975
3967- for (;;) {
3968- if (svr_sock_ == INVALID_SOCKET) {
3969- // The server socket was closed by 'stop' method.
3970- break ;
3971- }
3972-
3973- auto val = detail::select_read (svr_sock_, 0 , 0 );
3974-
3975- if (val == 0 ) { // Timeout
3976- task_queue->on_idle ();
3977- continue ;
3976+ while (svr_sock_ != INVALID_SOCKET) {
3977+ if (idle_interval_sec_ > 0 || idle_interval_usec_ > 0 ) {
3978+ auto val = detail::select_read (svr_sock_, idle_interval_sec_,
3979+ idle_interval_usec_);
3980+ if (val == 0 ) { // Timeout
3981+ task_queue->on_idle ();
3982+ continue ;
3983+ }
39783984 }
39793985
39803986 socket_t sock = accept (svr_sock_, nullptr , nullptr );
0 commit comments