Skip to content

Commit

Permalink
Fix handling of WS handshake error response
Browse files Browse the repository at this point in the history
Check response code, make sure it's 101.
Pass http_message to the client to keep it appraised.
This represents a slight change in the API -
in case of an error MG_EV_WEBSOCKET_HANDSHAKE_DONE will now be delivered where previosuly connection would just hang.
Clients that do not examine the argument may for a moment think handshake has succeeded but in fact connection will be closed immediately.

CL: mg: Fix handling of WS handshake error response

PUBLISHED_FROM=645a43d9e5bee216e54411f85827c9b974e9a7d1
  • Loading branch information
Deomid Ryabkov authored and cesantabot committed Feb 14, 2019
1 parent e485579 commit d236e0f
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/mg_rpc_channel_ws.c → src/mgos_rpc_channel_ws.c
Expand Up @@ -37,9 +37,6 @@ struct mg_rpc_channel_ws_data {

static void mg_rpc_ws_handler(struct mg_connection *nc, int ev, void *ev_data,
void *user_data) {
#if !MG_ENABLE_CALLBACK_USERDATA
void *user_data = nc->user_data;
#endif
struct mg_rpc_channel *ch = (struct mg_rpc_channel *) user_data;
if (ch == NULL) {
nc->flags |= MG_F_CLOSE_IMMEDIATELY;
Expand All @@ -50,9 +47,13 @@ static void mg_rpc_ws_handler(struct mg_connection *nc, int ev, void *ev_data,
if (chd == NULL) return;
switch (ev) {
case MG_EV_WEBSOCKET_HANDSHAKE_DONE: {
LOG(LL_INFO, ("%p WS HANDSHAKE DONE", ch));
chd->is_open = true;
ch->ev_handler(ch, MG_RPC_CHANNEL_OPEN, NULL);
struct http_message *hm = (struct http_message *) ev_data;
LOG((hm->resp_code == 101 ? LL_INFO : LL_ERROR),
("%p WS handshake resp %d", ch, hm->resp_code));
if (hm->resp_code == 101) {
chd->is_open = true;
ch->ev_handler(ch, MG_RPC_CHANNEL_OPEN, NULL);
}
break;
}
case MG_EV_WEBSOCKET_FRAME: {
Expand Down

0 comments on commit d236e0f

Please sign in to comment.