[spice-server,15/23] websocket: Support correctly protocol values

Submitted by Frediano Ziglio on June 25, 2019, 4:11 p.m.

Details

Message ID 20190625161147.25211-16-fziglio@redhat.com
State Accepted
Commit 2ba66599c1404a16e998de83d0c88d867c69838d
Headers show
Series "WebSocket support" ( rev: 1 ) in Spice

Not browsing as part of any series.

Commit Message

Frediano Ziglio June 25, 2019, 4:11 p.m.
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@redhat.com>
---
 server/websocket.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

Patch hide | download patch | download mbox

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)