1 diff --git a/third_party/mongoose/mongoose.c b/third_party/mongoose/mongoose.c
2 --- a/third_party/mongoose/mongoose.c
3 +++ b/third_party/mongoose/mongoose.c
6 #define _XOPEN_SOURCE 600 // For flockfile() on Linux
7 #define _LARGEFILE_SOURCE // Enable 64-bit file offsets
8 +#ifndef __STDC_FORMAT_MACROS
9 #define __STDC_FORMAT_MACROS // <inttypes.h> wants this for C++
10 +#endif // __STDC_FORMAT_MACROS
13 #if defined(__SYMBIAN32__)
17 #if defined(_WIN32) && !defined(__SYMBIAN32__) // Windows specific
21 #define _WIN32_WINNT 0x0400 // To make it link in VS2005
23 +#include <winsock2.h>
26 #define PATH_MAX MAX_PATH
31 -#include <winsock2.h>
32 #define NO_CGI // WinCE has no pipes
35 @@ -849,7 +854,9 @@ static int pthread_cond_init(pthread_cond_t *cv, const void *unused) {
38 static int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mutex) {
39 - HANDLE handles[] = {cv->signal, cv->broadcast};
41 + handles[0] = cv->signal;
42 + handles[1] = cv->broadcast;
44 WaitForMultipleObjects(2, handles, FALSE, INFINITE);
45 return WaitForSingleObject(*mutex, INFINITE) == WAIT_OBJECT_0? 0 : -1;
46 @@ -3424,6 +3431,10 @@ static int set_ports_option(struct mg_context *ctx) {
48 struct socket so, *listener;
50 + struct linger linger;
52 + linger.l_linger = 1;
54 while (success && (list = next_option(list, &vec, NULL)) != NULL) {
55 if (!parse_port_string(&vec, &so)) {
56 cry(fc(ctx), "%s: %.*s: invalid port spec. Expecting list of: %s",
57 @@ -3448,6 +3459,8 @@ static int set_ports_option(struct mg_context *ctx) {
58 // Thanks to Igor Klopov who suggested the patch.
59 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *) &on,
61 + setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &linger,
63 bind(sock, &so.lsa.u.sa, so.lsa.len) != 0 ||
64 listen(sock, 100) != 0) {
66 @@ -3768,15 +3781,8 @@ static void reset_per_request_attributes(struct mg_connection *conn) {
68 static void close_socket_gracefully(SOCKET sock) {
70 - struct linger linger;
73 - // Set linger option to avoid socket hanging out after close. This prevent
74 - // ephemeral port exhaust problem under high QPS.
76 - linger.l_linger = 1;
77 - setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger));
79 // Send FIN to the client
80 (void) shutdown(sock, SHUT_WR);
81 set_non_blocking_mode(sock);
82 @@ -4233,8 +4239,6 @@ struct mg_context *mg_start(mg_callback_t user_callback, void *user_data,
83 // Ignore SIGPIPE signal, so if browser cancels the request, it
84 // won't kill the whole process.
85 (void) signal(SIGPIPE, SIG_IGN);
86 - // Also ignoring SIGCHLD to let the OS to reap zombies properly.
87 - (void) signal(SIGCHLD, SIG_IGN);
90 (void) pthread_mutex_init(&ctx->mutex, NULL);
91 @@ -3812,1 +3812,1 @@ static void close_connection(struct mg_connection *conn) {
94 static void discard_current_request_from_buffer(struct mg_connection *conn) {
96 int buffered_len, body_len;
98 - buffered = conn->buf + conn->request_len;
99 buffered_len = conn->data_len - conn->request_len;
100 assert(buffered_len >= 0);
102 @@ -3867,7 +3865,7 @@ static void handle_proxy_request(struct mg_connection *conn) {
104 conn->peer->client.is_ssl = is_ssl;
108 // Forward client's request to the target
109 mg_printf(conn->peer, "%s %s HTTP/%s\r\n", ri->request_method, ri->uri + len,
111 @@ -2820,6 +2820,8 @@ static void prepare_cgi_environment(struct mg_connection *conn,
112 blk->len = blk->nvars = 0;
115 + memset(&root, 0, sizeof(root));
117 get_document_root(conn, &root);
119 addenv(blk, "SERVER_NAME=%s", conn->ctx->config[AUTHENTICATION_DOMAIN]);
120 @@ -2916,6 +2918,8 @@ static void handle_cgi_request(struct mg_connection *conn, const char *prog) {
124 + memset(&ri, 0, sizeof(ri));
126 prepare_cgi_environment(conn, prog, &blk);
128 // CGI must be executed in its own directory. 'dir' must point to the