From 6f946f5eea989efd992d134e32138d34a78fe74d Mon Sep 17 00:00:00 2001 From: Sergey Lyubka <valenok@gmail.com> Date: Fri, 10 May 2013 13:41:48 +0100 Subject: [PATCH] websocket code fix, https://github.com/valenok/mongoose/pull/146 --- examples/websocket_html_root/index.html | 12 ++++---- mongoose.c | 40 +++++++++++++++---------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/examples/websocket_html_root/index.html b/examples/websocket_html_root/index.html index f6f7eb3ff..8c5f765fc 100644 --- a/examples/websocket_html_root/index.html +++ b/examples/websocket_html_root/index.html @@ -1,6 +1,6 @@ -<!DOCTYPE html> -<meta charset="utf-8" /> -<title>WebSocket Test</title> +<!DOCTYPE html> +<meta charset="utf-8" /> +<title>WebSocket Test</title> <script language="javascript" type="text/javascript"> var writeToScreen = function(message) { @@ -29,9 +29,9 @@ writeToScreen('<span style="color: red; ">ERROR: </span> ' + ev.data); }; }; -</script> +</script> <style> div {font: small Verdana; } </style> -<h2>Mongoose WebSocket Test</h2> +<h2>Mongoose WebSocket Test</h2> <div style="width: 400px; color: #aaa; padding: 1em; "> This page code creates websocket to the URI "/foo", @@ -40,5 +40,5 @@ receiving the "exit" message. </div> -<div id="output"></div> +<div id="output"></div> </html> diff --git a/mongoose.c b/mongoose.c index 03cc922a5..fec574847 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1509,6 +1509,26 @@ static int pull(FILE *fp, struct mg_connection *conn, char *buf, int len) { return conn->ctx->stop_flag ? -1 : nread; } +static int pull_all(FILE *fp, struct mg_connection *conn, char *buf, int len) { + int n, nread = 0; + + while (len > 0) { + n = pull(fp, conn, buf + nread, len); + if (n < 0) { + nread = n; // Propagate the error + break; + } else if (n == 0) { + break; // No more data to read + } else { + conn->consumed_content += n; + nread += n; + len -= n; + } + } + + return nread; +} + int mg_read(struct mg_connection *conn, void *buf, size_t len) { int n, buffered_len, nread; const char *body; @@ -1536,20 +1556,8 @@ int mg_read(struct mg_connection *conn, void *buf, size_t len) { } // We have returned all buffered data. Read new data from the remote socket. - while (len > 0) { - n = pull(NULL, conn, (char *) buf, (int) len); - if (n < 0) { - nread = n; // Propagate the error - break; - } else if (n == 0) { - break; // No more data to read - } else { - buf = (char *) buf + n; - conn->consumed_content += n; - nread += n; - len -= n; - } - } + n = pull_all(NULL, conn, (char *) buf, (int) len); + nread = n >= 0 ? nread + n : n; } return nread; } @@ -3841,8 +3849,8 @@ static void read_websocket(struct mg_connection *conn) { len = body_len - header_len; memcpy(data, buf + header_len, len); // TODO: handle pull error - pull(NULL, conn, data + len, data_len - len); - conn->data_len = 0; + pull_all(NULL, conn, data + len, data_len - len); + conn->data_len = conn->request_len; } else { len = data_len + header_len; memcpy(data, buf + header_len, data_len); -- GitLab