Skip to content

Commit

Permalink
Allow for any websocket protocol containing mqtt
Browse files Browse the repository at this point in the history
I've see various variations, like mqtt and mqttv31. The 3.1.1 specs are
clear what it should be, but the 3.1 specs aren't. So, allowing
anything with mqtt in it.
  • Loading branch information
halfgaar committed Jul 3, 2022
1 parent 1ca1bdf commit c24becf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
5 changes: 3 additions & 2 deletions iowrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ ssize_t IoWrapper::readWebsocketAndOrSsl(int fd, void *buf, size_t nbytes, IoWra
{
std::string websocketKey;
int websocketVersion;
if (parseHttpHeader(websocketPendingBytes, websocketKey, websocketVersion))
std::string subprotocol;
if (parseHttpHeader(websocketPendingBytes, websocketKey, websocketVersion, subprotocol))
{
if (websocketKey.empty())
throw BadHttpRequest("No websocket key specified.");
Expand All @@ -380,7 +381,7 @@ ssize_t IoWrapper::readWebsocketAndOrSsl(int fd, void *buf, size_t nbytes, IoWra

const std::string acceptString = generateWebsocketAcceptString(websocketKey);

std::string answer = generateWebsocketAnswer(acceptString);
std::string answer = generateWebsocketAnswer(acceptString, subprotocol);
parentClient->writeText(answer);
websocketState = WebsocketState::Upgrading;
websocketPendingBytes.reset();
Expand Down
11 changes: 7 additions & 4 deletions utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ bool isPowerOfTwo(int n)
return (n != 0) && (n & (n - 1)) == 0;
}

bool parseHttpHeader(CirBuf &buf, std::string &websocket_key, int &websocket_version)
bool parseHttpHeader(CirBuf &buf, std::string &websocket_key, int &websocket_version, std::string &subprotocol)
{
const std::string s(buf.tailPtr(), buf.usedBytes());
std::istringstream is(s);
Expand Down Expand Up @@ -397,8 +397,11 @@ bool parseHttpHeader(CirBuf &buf, std::string &websocket_key, int &websocket_ver
websocket_key = value;
else if (name == "sec-websocket-version")
websocket_version = stoi(value);
else if (name == "sec-websocket-protocol" && value_lower == "mqtt")
else if (name == "sec-websocket-protocol" && strContains(value_lower, "mqtt"))
{
subprotocol = value;
subprotocol_seen = true;
}
}

if (doubleEmptyLine)
Expand Down Expand Up @@ -489,14 +492,14 @@ std::string generateBadHttpRequestReponse(const std::string &msg)
return oss.str();
}

std::string generateWebsocketAnswer(const std::string &acceptString)
std::string generateWebsocketAnswer(const std::string &acceptString, const std::string &subprotocol)
{
std::ostringstream oss;
oss << "HTTP/1.1 101 Switching Protocols\r\n";
oss << "Upgrade: websocket\r\n";
oss << "Connection: Upgrade\r\n";
oss << "Sec-WebSocket-Accept: " << acceptString << "\r\n";
oss << "Sec-WebSocket-Protocol: mqtt\r\n";
oss << "Sec-WebSocket-Protocol: " << subprotocol << "\r\n";
oss << "\r\n";
oss.flush();
return oss.str();
Expand Down
4 changes: 2 additions & 2 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ std::string str_tolower(std::string s);
bool stringTruthiness(const std::string &val);
bool isPowerOfTwo(int val);

bool parseHttpHeader(CirBuf &buf, std::string &websocket_key, int &websocket_version);
bool parseHttpHeader(CirBuf &buf, std::string &websocket_key, int &websocket_version, std::string &subprotocol);

std::vector<char> base64Decode(const std::string &s);
std::string base64Encode(const unsigned char *input, const int length);
std::string generateWebsocketAcceptString(const std::string &websocketKey);

std::string generateInvalidWebsocketVersionHttpHeaders(const int wantedVersion);
std::string generateBadHttpRequestReponse(const std::string &msg);
std::string generateWebsocketAnswer(const std::string &acceptString);
std::string generateWebsocketAnswer(const std::string &acceptString, const std::string &subprotocol);

void testSsl(const std::string &fullchain, const std::string &privkey);

Expand Down

0 comments on commit c24becf

Please sign in to comment.