From acf72a610915ddf6ebfc38a5654393f283971a84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Amiaux?= Date: Wed, 9 Jan 2013 17:05:29 +0100 Subject: [PATCH] Add request_timeout option --- mongoose.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/mongoose.c b/mongoose.c index e883ba09..a9c4ade2 100644 --- a/mongoose.c +++ b/mongoose.c @@ -449,7 +449,7 @@ enum { ACCESS_LOG_FILE, ENABLE_DIRECTORY_LISTING, ERROR_LOG_FILE, GLOBAL_PASSWORDS_FILE, INDEX_FILES, ENABLE_KEEP_ALIVE, ACCESS_CONTROL_LIST, EXTRA_MIME_TYPES, LISTENING_PORTS, DOCUMENT_ROOT, SSL_CERTIFICATE, - NUM_THREADS, RUN_AS_USER, REWRITE, HIDE_FILES, + NUM_THREADS, RUN_AS_USER, REWRITE, HIDE_FILES, REQUEST_TIMEOUT, NUM_OPTIONS }; @@ -477,6 +477,7 @@ static const char *config_options[] = { "u", "run_as_user", NULL, "w", "url_rewrite_patterns", NULL, "x", "hide_files_patterns", NULL, + "z", "request_timeout", NULL, NULL }; #define ENTRIES_PER_CONFIG_OPTION 3 @@ -4314,6 +4315,23 @@ static int parse_port_string(const struct vec *vec, struct socket *so) { return 1; } +static int set_timeout(struct mg_context *ctx, SOCKET sock) { +#ifndef _WIN32 + struct timeval timeout; + if( !ctx->config[REQUEST_TIMEOUT] ) + return 0; + timeout.tv_sec = 0; + timeout.tv_usec = atoi(ctx->config[REQUEST_TIMEOUT]) * 1000; +#else + DWORD timeout; + if( !ctx->config[REQUEST_TIMEOUT] ) + return 0; + timeout = atoi(ctx->config[REQUEST_TIMEOUT]); +#endif + return setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, sizeof(timeout)) + || setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (void *)&timeout, sizeof(timeout)); +} + static int set_ports_option(struct mg_context *ctx) { const char *list = ctx->config[LISTENING_PORTS]; int on = 1, success = 1; @@ -4332,6 +4350,7 @@ static int set_ports_option(struct mg_context *ctx) { success = 0; } else if ((sock = socket(so.lsa.sa.sa_family, SOCK_STREAM, 6)) == INVALID_SOCKET || + set_timeout(ctx, sock) || // On Windows, SO_REUSEADDR is recommended only for // broadcast UDP sockets setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *) &on,