Commit cdb65889 authored by Sergey Lyubka's avatar Sergey Lyubka

Fix to stop misbehaving clients to DoS mongoose

parent eecf24b2
...@@ -1693,7 +1693,7 @@ int mg_get_var(const char *data, size_t data_len, const char *name, ...@@ -1693,7 +1693,7 @@ int mg_get_var(const char *data, size_t data_len, const char *name,
// Decode variable into destination buffer // Decode variable into destination buffer
len = url_decode(p, (size_t)(s - p), dst, dst_len, 1); len = url_decode(p, (size_t)(s - p), dst, dst_len, 1);
// Redirect error code from -1 to -2 (destination buffer too small). // Redirect error code from -1 to -2 (destination buffer too small).
if (len == -1) { if (len == -1) {
len = -2; len = -2;
...@@ -4632,9 +4632,12 @@ static void reset_per_request_attributes(struct mg_connection *conn) { ...@@ -4632,9 +4632,12 @@ static void reset_per_request_attributes(struct mg_connection *conn) {
} }
static void close_socket_gracefully(struct mg_connection *conn) { static void close_socket_gracefully(struct mg_connection *conn) {
#if defined(_WIN32)
char buf[MG_BUF_LEN]; char buf[MG_BUF_LEN];
int n;
#endif
struct linger linger; struct linger linger;
int n, sock = conn->client.sock; int sock = conn->client.sock;
// Set linger option to avoid socket hanging out after close. This prevent // Set linger option to avoid socket hanging out after close. This prevent
// ephemeral port exhaust problem under high QPS. // ephemeral port exhaust problem under high QPS.
...@@ -4646,6 +4649,7 @@ static void close_socket_gracefully(struct mg_connection *conn) { ...@@ -4646,6 +4649,7 @@ static void close_socket_gracefully(struct mg_connection *conn) {
(void) shutdown(sock, SHUT_WR); (void) shutdown(sock, SHUT_WR);
set_non_blocking_mode(sock); set_non_blocking_mode(sock);
#if defined(_WIN32)
// Read and discard pending incoming data. If we do not do that and close the // Read and discard pending incoming data. If we do not do that and close the
// socket, the data in the send buffer may be discarded. This // socket, the data in the send buffer may be discarded. This
// behaviour is seen on Windows, when client keeps sending data // behaviour is seen on Windows, when client keeps sending data
...@@ -4654,6 +4658,7 @@ static void close_socket_gracefully(struct mg_connection *conn) { ...@@ -4654,6 +4658,7 @@ static void close_socket_gracefully(struct mg_connection *conn) {
do { do {
n = pull(NULL, conn, buf, sizeof(buf)); n = pull(NULL, conn, buf, sizeof(buf));
} while (n > 0); } while (n > 0);
#endif
// Now we know that our FIN is ACK-ed, safe to close // Now we know that our FIN is ACK-ed, safe to close
(void) closesocket(sock); (void) closesocket(sock);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment