[Spice-devel] [PATCH spice-server 15/23] websocket: Support correctly protocol values

Frediano Ziglio fziglio at redhat.com
Tue Jun 25 16:11:39 UTC 2019


Ignore spaces before "binary" value.
HTTP allows space before and after the value although usually
browsers implementation start the value with a single ASCII space.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 server/websocket.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/server/websocket.c b/server/websocket.c
index 6f1581f9c..96c6fce1f 100644
--- a/server/websocket.c
+++ b/server/websocket.c
@@ -501,15 +501,23 @@ static void websocket_ack_close(void *stream, websocket_write_cb_t write_cb)
 
 static bool websocket_is_start(char *buf)
 {
-    if (strncmp(buf, "GET ", 4) == 0 &&
-            // TODO strip, do not assume a single space
-            find_str(buf, "\nSec-WebSocket-Protocol: binary") &&
-            find_str(buf, "\nSec-WebSocket-Key:") &&
-            g_str_has_suffix(buf, "\r\n\r\n")) {
-        return true;
+    const char *protocol = find_str(buf, "\nSec-WebSocket-Protocol:");
+    const char *key = find_str(buf, "\nSec-WebSocket-Key:");
+
+    if (strncmp(buf, "GET ", 4) != 0 ||
+            protocol == NULL || key == NULL ||
+            !g_str_has_suffix(buf, "\r\n\r\n")) {
+        return false;
     }
 
-    return false;
+    /* check protocol value ignoring spaces before and after */
+    int binary_pos = -1;
+    sscanf(protocol, " binary %n", &binary_pos);
+    if (binary_pos <= 0) {
+        return false;
+    }
+
+    return true;
 }
 
 static void websocket_create_reply(char *buf, char *outbuf)
-- 
2.20.1



More information about the Spice-devel mailing list