From 07fa4b48eaec102fec055cc88338ac79cf63e971 Mon Sep 17 00:00:00 2001 From: Evan Smith Date: Tue, 26 Jul 2016 11:05:17 -0400 Subject: [PATCH 1/4] Make websocket headers case insensitive Per Websocket/HTTP spec, updated to allow case-insensitivity in websocket/HTTP headers. --- src/WebSocketClient.cpp | 44 +++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/src/WebSocketClient.cpp b/src/WebSocketClient.cpp index 9d14d93..1485e5d 100644 --- a/src/WebSocketClient.cpp +++ b/src/WebSocketClient.cpp @@ -91,6 +91,27 @@ bool WebSocketClient::analyzeRequest() { socket_client->print(CRLF); socket_client->print(F("Sec-WebSocket-Version: 13\r\n")); +#ifdef DEBUGGING + Serial.println("Printing websocket upgrade headers"); + Serial.print(F("GET ")); + Serial.print(path); + if (issocketio) { + socket_client->print(F("socket.io/?EIO=3&transport=websocket&sid=")); + socket_client->print(sid); + } + Serial.print(F(" HTTP/1.1\r\n")); + Serial.print(F("Upgrade: websocket\r\n")); + Serial.print(F("Connection: Upgrade\r\n")); + Serial.print(F("Sec-WebSocket-Key: ")); + Serial.print(key); + Serial.print(CRLF); + Serial.print(F("Sec-WebSocket-Protocol: ")); + Serial.print(protocol); + Serial.print(CRLF); + Serial.print(F("Sec-WebSocket-Version: 13\r\n")); +#endif + + } else { #ifdef DEBUGGING @@ -123,17 +144,28 @@ bool WebSocketClient::analyzeRequest() { temp += (char)bite; if ((char)bite == '\n') { + String tempLC = temp; + tempLC.toLowerCase(); // uses case-insensitive header string for parsing to ensure to catch response from servers using different upper/lowercase variants of headers #ifdef DEBUGGING Serial.print("Got Header: " + temp); #endif - if (!foundupgrade && temp.startsWith("Upgrade: websocket")) { + if (!foundupgrade && tempLC.startsWith("upgrade: websocket")) { foundupgrade = true; - } else if (temp.startsWith("Sec-WebSocket-Accept: ")) { + } else if (tempLC.startsWith("sec-websocket-accept: ")) { serverKey = temp.substring(22,temp.length() - 2); // Don't save last CR+LF - } else if (!foundsid && temp.startsWith("Set-Cookie: ")) { - foundsid = true; - String tempsid = temp.substring(temp.indexOf("=") + 1, temp.length() - 2); // Don't save last CR+LF - strcpy(sid, tempsid.c_str()); + } else if (!foundsid && tempLC.startsWith("set-cookie: ")) { + foundsid = true; + if (temp.indexOf(";") == -1){ // looks for ";" in cookie header, which indicates more than one cookie value + String tempsid = temp.substring(temp.indexOf("=") + 1, temp.length() - 2); // Don't save last CR+LF + } + else { + String tempsid = temp.substring(temp.indexOf("=") + 1, temp.indexOf(";")); // assumes sid is first cookie value, discards all other values + } + strcpy(sid, tempsid.c_str()); + #ifdef DEBUGGING + Serial.println("Parsing Set-Cookie..."); + Serial.println("tempsid: " + tempsid); + #endif } temp = ""; } From 8c7a1279c883633238f445f8cc299c75bc152fed Mon Sep 17 00:00:00 2001 From: Evan Smith Date: Tue, 26 Jul 2016 11:11:54 -0400 Subject: [PATCH 2/4] Fix tempsid declare bug --- src/WebSocketClient.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/WebSocketClient.cpp b/src/WebSocketClient.cpp index 1485e5d..8a51e92 100644 --- a/src/WebSocketClient.cpp +++ b/src/WebSocketClient.cpp @@ -155,11 +155,12 @@ bool WebSocketClient::analyzeRequest() { serverKey = temp.substring(22,temp.length() - 2); // Don't save last CR+LF } else if (!foundsid && tempLC.startsWith("set-cookie: ")) { foundsid = true; + String tempsid; if (temp.indexOf(";") == -1){ // looks for ";" in cookie header, which indicates more than one cookie value - String tempsid = temp.substring(temp.indexOf("=") + 1, temp.length() - 2); // Don't save last CR+LF + tempsid = temp.substring(temp.indexOf("=") + 1, temp.length() - 2); // Don't save last CR+LF } else { - String tempsid = temp.substring(temp.indexOf("=") + 1, temp.indexOf(";")); // assumes sid is first cookie value, discards all other values + tempsid = temp.substring(temp.indexOf("=") + 1, temp.indexOf(";")); // assumes sid is first cookie value, discards all other values } strcpy(sid, tempsid.c_str()); #ifdef DEBUGGING From b3c07c07731ceb0b51969d06ae5f80b3c4538834 Mon Sep 17 00:00:00 2001 From: Evan Smith Date: Mon, 4 Dec 2017 16:46:55 -0500 Subject: [PATCH 3/4] committing random change --- src/WebSocketClient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WebSocketClient.cpp b/src/WebSocketClient.cpp index 8a51e92..4029304 100644 --- a/src/WebSocketClient.cpp +++ b/src/WebSocketClient.cpp @@ -1,4 +1,4 @@ -//#define DEBUGGING +#define DEBUGGING #include "global.h" #include "WebSocketClient.h" From a2a116ae48ed61b41590a1d62b5b005a3e128117 Mon Sep 17 00:00:00 2001 From: Evan Smith Date: Wed, 29 Aug 2018 21:35:22 -0400 Subject: [PATCH 4/4] Comment out debugging flag --- src/WebSocketClient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WebSocketClient.cpp b/src/WebSocketClient.cpp index 4029304..8a51e92 100644 --- a/src/WebSocketClient.cpp +++ b/src/WebSocketClient.cpp @@ -1,4 +1,4 @@ -#define DEBUGGING +//#define DEBUGGING #include "global.h" #include "WebSocketClient.h"