From: Kevin Harwell Date: Wed, 11 Feb 2015 17:52:35 +0000 (+0000) Subject: res_http_websocket: websocket write timeout fails to fully disconnect X-Git-Tag: certified/13.1-cert3-rc1~49 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8be00450b93c4a1edfb82f884022f431b9c3d457;p=thirdparty%2Fasterisk.git res_http_websocket: websocket write timeout fails to fully disconnect When writing to a websocket if a timeout occurred the underlying socket did not get closed/disconnected. This patch makes sure the websocket gets disconnected on a write timeout. Also a notice is logged stating that the websocket was disconnected. ASTERISK-24701 #close Reported by: Matt Jordan Review: https://reviewboard.asterisk.org/r/4412/ ........ Merged revisions 431669 from http://svn.asterisk.org/svn/asterisk/branches/11 ........ Merged revisions 431670 from http://svn.asterisk.org/svn/asterisk/branches/13 git-svn-id: https://origsvn.digium.com/svn/asterisk/certified/branches/13.1@431697 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- diff --git a/res/ari/ari_websockets.c b/res/ari/ari_websockets.c index ff0a53c4fc..f3b764bf15 100644 --- a/res/ari/ari_websockets.c +++ b/res/ari/ari_websockets.c @@ -100,6 +100,10 @@ struct ast_json *ast_ari_websocket_session_read( { RAII_VAR(struct ast_json *, message, NULL, ast_json_unref); + if (ast_websocket_fd(session->ws_session) < 0) { + return NULL; + } + while (!message) { int res; char *payload; @@ -127,7 +131,7 @@ struct ast_json *ast_ari_websocket_session_read( switch (opcode) { case AST_WEBSOCKET_OPCODE_CLOSE: - ast_debug(1, "WebSocket closed by peer\n"); + ast_debug(1, "WebSocket closed\n"); return NULL; case AST_WEBSOCKET_OPCODE_TEXT: message = ast_json_load_buf(payload, payload_len, NULL); @@ -173,8 +177,12 @@ int ast_ari_websocket_session_write(struct ast_ari_websocket_session *session, } ast_debug(3, "Examining ARI event: \n%s\n", str); - return ast_websocket_write(session->ws_session, - AST_WEBSOCKET_OPCODE_TEXT, str, strlen(str)); + if (ast_websocket_write(session->ws_session, + AST_WEBSOCKET_OPCODE_TEXT, str, strlen(str))) { + ast_log(LOG_NOTICE, "Problem occurred during websocket write, websocket closed\n"); + return -1; + } + return 0; } void ari_handle_websocket(struct ast_websocket_server *ws_server, diff --git a/res/res_http_websocket.c b/res/res_http_websocket.c index 400e5eb1c2..4f42ef04fe 100644 --- a/res/res_http_websocket.c +++ b/res/res_http_websocket.c @@ -307,11 +307,15 @@ int AST_OPTIONAL_API_NAME(ast_websocket_write)(struct ast_websocket *session, en } if (ast_careful_fwrite(session->f, session->fd, frame, header_size, session->timeout)) { ao2_unlock(session); + /* 1011 - server terminating connection due to not being able to fulfill the request */ + ast_websocket_close(session, 1011); return -1; } if (ast_careful_fwrite(session->f, session->fd, payload, actual_length, session->timeout)) { ao2_unlock(session); + /* 1011 - server terminating connection due to not being able to fulfill the request */ + ast_websocket_close(session, 1011); return -1; } fflush(session->f);