Browse Source

select Sec-WebSocket-Protocol

ithewei 2 years ago
parent
commit
3101e743c2
2 changed files with 13 additions and 3 deletions
  1. 8 0
      http/server/HttpServer.cpp
  2. 5 3
      http/wsdef.h

+ 8 - 0
http/server/HttpServer.cpp

@@ -125,6 +125,14 @@ static void on_recv(hio_t* io, void* _buf, int readbytes) {
                 ws_encode_key(iter_key->second.c_str(), ws_accept);
                 resp->headers[SEC_WEBSOCKET_ACCEPT] = ws_accept;
             }
+            auto iter_protocol = req->headers.find(SEC_WEBSOCKET_PROTOCOL);
+            if (iter_protocol != req->headers.end()) {
+                hv::StringList subprotocols = hv::split(iter_protocol->second, ',');
+                if (subprotocols.size() > 0) {
+                    hlogw("%s: %s => just select first protocol %s", SEC_WEBSOCKET_PROTOCOL, iter_protocol->second.c_str(), subprotocols[0].c_str());
+                    resp->headers[SEC_WEBSOCKET_PROTOCOL] = subprotocols[0];
+                }
+            }
             upgrade_protocol = HttpHandler::WEBSOCKET;
             // NOTE: SwitchWebSocket after send handshake response
         }

+ 5 - 3
http/wsdef.h

@@ -6,9 +6,11 @@
 #include <stdbool.h>
 #include <stdlib.h> // import rand
 
-#define SEC_WEBSOCKET_VERSION   "Sec-WebSocket-Version"
-#define SEC_WEBSOCKET_KEY       "Sec-WebSocket-Key"
-#define SEC_WEBSOCKET_ACCEPT    "Sec-WebSocket-Accept"
+#define SEC_WEBSOCKET_VERSION       "Sec-WebSocket-Version"
+#define SEC_WEBSOCKET_KEY           "Sec-WebSocket-Key"
+#define SEC_WEBSOCKET_ACCEPT        "Sec-WebSocket-Accept"
+#define SEC_WEBSOCKET_PROTOCOL      "Sec-WebSocket-Protocol"
+#define SEC_WEBSOCKET_EXTENSIONS    "Sec-WebSocket-Extensions"
 
 #define WS_SERVER_MIN_FRAME_SIZE    2
 // 1000 1001 0000 0000