Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Sec-WebSocket-Protocol/Version and add check_handshake() #887

Merged
merged 1 commit into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/factory/WFChannel.inl
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,9 @@ public:
{
this->auto_gen_mkey = auto_gen_mkey;
}

private:
bool check_handshake(const protocol::HttpResponse *resp);
};

class ComplexWebSocketOutTask : public ComplexChannelOutTask<protocol::WebSocketFrame>
Expand Down
42 changes: 41 additions & 1 deletion src/factory/WebSocketTaskImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@

#define WS_HTTP_SEC_KEY_K "Sec-WebSocket-Key"
#define WS_HTTP_SEC_KEY_V "dGhlIHNhbXBsZSBub25jZQ=="

#define WS_HTTP_SEC_PROTOCOL_K "Sec-WebSocket-Protocol"
#define WS_HTTP_SEC_PROTOCOL_V "chat"

#define WS_HTTP_SEC_VERSION_K "Sec-WebSocket-Version"
#define WS_HTTP_SEC_VERSION_V "13"

#define WS_HTTP_SEC_ACCEPT_K "Sec-WebSocket-Accept"
#define WS_HTTP_SEC_ACCEPT_V "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="

using namespace protocol;

Expand Down Expand Up @@ -166,6 +173,8 @@ SubTask *ComplexWebSocketOutTask::upgrade()
req->add_header_pair("Upgrade", "websocket");
req->add_header_pair("Connection", "Upgrade");
req->add_header_pair(WS_HTTP_SEC_KEY_K, WS_HTTP_SEC_KEY_V);
req->add_header_pair(WS_HTTP_SEC_PROTOCOL_K, WS_HTTP_SEC_PROTOCOL_V);
req->add_header_pair(WS_HTTP_SEC_VERSION_K, WS_HTTP_SEC_VERSION_V);

if (channel->get_sec_protocol())
req->add_header_pair(WS_HTTP_SEC_PROTOCOL_K, channel->get_sec_protocol());
Expand Down Expand Up @@ -193,7 +202,7 @@ void ComplexWebSocketChannel::handle_in(CommMessageIn *in)
{
HttpResponse *resp = static_cast<HttpResponse *>(in);

if (strcmp(resp->get_status_code(), "101") == 0)
if (this->check_handshake(resp))
this->state = WFT_STATE_SUCCESS;
else
this->state = WFT_STATE_TASK_ERROR;
Expand Down Expand Up @@ -232,3 +241,34 @@ WFWebSocketTask *ComplexWebSocketChannel::new_session()
return task;
}

bool ComplexWebSocketChannel::check_handshake(const HttpResponse *resp)
{
if (strcmp(resp->get_status_code(), "101"))
return false;

std::string name;
std::string value;
HttpHeaderCursor resp_cursor(resp);
int flag = 0;

while (resp_cursor.next(name, value) && flag != 7)
{
if (name.compare("Upgrade") == 0 && value.compare("websocket") == 0)
{
flag |= 1;
}
else if (name.compare("Connection") == 0 &&
value.compare("Upgrade") == 0)
{
flag |= (1 << 1);
}
else if (name.compare(WS_HTTP_SEC_ACCEPT_K) == 0 &&
value.compare(WS_HTTP_SEC_ACCEPT_V) == 0)
{
flag |= (1 << 2);
}
}

return flag == 7;
}