From 242c945400d19ca002d203dc87708ca3d009efa7 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Fri, 21 Feb 2020 14:05:38 -0800 Subject: [PATCH] (client + server) Fix #155 / http header parser should treat the space(s) after the : delimiter as optional. Fixing this bug made us discover that websocket sub-protocols are not properly serialiazed, but start with a , --- docs/CHANGELOG.md | 4 ++++ ixwebsocket/IXWebSocket.cpp | 12 +++++++++++- ixwebsocket/IXWebSocketHttpHeaders.cpp | 15 +++++++++++++-- ixwebsocket/IXWebSocketVersion.h | 2 +- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 6440f469..7642dba6 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog All changes to this project will be documented in this file. +## [8.1.3] - 2020-02-21 + +(client + server) Fix #155 / http header parser should treat the space(s) after the : delimiter as optional. Fixing this bug made us discover that websocket sub-protocols are not properly serialiazed, but start with a , + ## [8.1.2] - 2020-02-18 (WebSocketServer) add option to disable deflate compression, exposed with the -x option to ws echo_server diff --git a/ixwebsocket/IXWebSocket.cpp b/ixwebsocket/IXWebSocket.cpp index 945e1a09..91eba49a 100644 --- a/ixwebsocket/IXWebSocket.cpp +++ b/ixwebsocket/IXWebSocket.cpp @@ -191,9 +191,19 @@ namespace ix auto subProtocols = getSubProtocols(); if (!subProtocols.empty()) { + // + // Sub Protocol strings are comma separated. + // Python code to do that is: + // >>> ','.join(['json', 'msgpack']) + // 'json,msgpack' + // + int i = 0; for (auto subProtocol : subProtocols) { - subProtocolsHeader += ","; + if (i++ != 0) + { + subProtocolsHeader += ","; + } subProtocolsHeader += subProtocol; } headers["Sec-WebSocket-Protocol"] = subProtocolsHeader; diff --git a/ixwebsocket/IXWebSocketHttpHeaders.cpp b/ixwebsocket/IXWebSocketHttpHeaders.cpp index 53c928e2..4bc90972 100644 --- a/ixwebsocket/IXWebSocketHttpHeaders.cpp +++ b/ixwebsocket/IXWebSocketHttpHeaders.cpp @@ -66,12 +66,23 @@ namespace ix { line[i] = '\0'; std::string lineStr(line); - // colon is ':', colon+1 is ' ', colon+2 is the start of the value. + // colon is ':', usually colon+1 is ' ', and colon+2 is the start of the value. + // some webservers do not put a space after the colon character, so + // the start of the value might be farther than colon+2. + // The spec says that space after the : should be discarded. // i is end of string (\0), i-colon is length of string minus key; // subtract 1 for '\0', 1 for '\n', 1 for '\r', // 1 for the ' ' after the ':', and total is -4 + // since we use an std::string later on and don't account for '\0', + // plus the optional first space, total is -2 + int start = colon + 1; + while (lineStr[start] == ' ') + { + start++; + } + std::string name(lineStr.substr(0, colon)); - std::string value(lineStr.substr(colon + 2, i - colon - 4)); + std::string value(lineStr.substr(start, lineStr.size() - start - 2)); headers[name] = value; } diff --git a/ixwebsocket/IXWebSocketVersion.h b/ixwebsocket/IXWebSocketVersion.h index af24d5e7..ee48964b 100644 --- a/ixwebsocket/IXWebSocketVersion.h +++ b/ixwebsocket/IXWebSocketVersion.h @@ -6,4 +6,4 @@ #pragma once -#define IX_WEBSOCKET_VERSION "8.1.2" +#define IX_WEBSOCKET_VERSION "8.1.3"