diff options
author | Matt Jordan <mjordan@digium.com> | 2015-05-17 20:36:41 -0500 |
---|---|---|
committer | Matt Jordan <mjordan@digium.com> | 2015-05-22 11:12:03 -0500 |
commit | 5ac65ddfb46f715490b4eccbef57dc6f424e9bc2 (patch) | |
tree | 986bf038659867abd94189051637ddb5f180dcff /res/ari | |
parent | 9d8a462356a938eea82e8424242d89a682495b57 (diff) |
res/ari: Register Stasis application on WebSocket attempt
Prior to this patch, when a WebSocket connection is made, ARI would not
be informed of the connection until after the WebSocket layer had
accepted the connection. This created a brief race condition where the
ARI client would be notified that it was connected, a channel would be
sent into the Stasis dialplan application, but ARI would not yet have
registered the Stasis application presented in the HTTP request that
established the WebSocket.
This patch resolves this issue by doing the following:
* When a WebSocket attempt is made, a callback is made into the ARI
application layer, which verifies and registers the apps presented in
the HTTP request. Because we do not yet have a WebSocket, we cannot
have an event session for the corresponding applications. Some
defensive checks were thus added to make the application objects
tolerant to a NULL event session.
* When a WebSocket connection is made, the registered application is
updated with the newly created event session that wraps the WebSocket
connection.
ASTERISK-24988 #close
Reported by: Joshua Colp
Change-Id: Ia5dc60dc2b6bee76cd5aff0f69dd53b36e83f636
Diffstat (limited to 'res/ari')
-rw-r--r-- | res/ari/resource_events.c | 39 | ||||
-rw-r--r-- | res/ari/resource_events.h | 15 |
2 files changed, 52 insertions, 2 deletions
diff --git a/res/ari/resource_events.c b/res/ari/resource_events.c index d159741c2..09bcafc2d 100644 --- a/res/ari/resource_events.c +++ b/res/ari/resource_events.c @@ -119,6 +119,10 @@ static void app_handler(void *data, const char *app_name, const char *msg_application = S_OR( ast_json_string_get(ast_json_object_get(message, "application")), ""); + + if (!session) { + return; + } /* Determine if we've been replaced */ if (strcmp(msg_type, "ApplicationReplaced") == 0 && @@ -168,7 +172,40 @@ static int session_register_app(struct event_session *session, return 0; } -void ast_ari_websocket_events_event_websocket(struct ast_ari_websocket_session *ws_session, +int ast_ari_websocket_events_event_websocket_attempted(struct ast_tcptls_session_instance *ser, + struct ast_variable *headers, + struct ast_ari_events_event_websocket_args *args) +{ + int res = 0; + size_t i, j; + + ast_debug(3, "/events WebSocket attempted\n"); + + if (args->app_count == 0) { + ast_http_error(ser, 400, "Bad Request", "Missing param 'app'"); + return -1; + } + + for (i = 0; i < args->app_count; ++i) { + if (ast_strlen_zero(args->app[i])) { + res = -1; + break; + } + + res |= stasis_app_register(args->app[i], app_handler, NULL); + } + + if (res) { + for (j = 0; j < i; ++j) { + stasis_app_unregister(args->app[j]); + } + ast_http_error(ser, 400, "Bad Request", "Invalid application provided in param 'app'."); + } + + return res; +} + +void ast_ari_websocket_events_event_websocket_established(struct ast_ari_websocket_session *ws_session, struct ast_variable *headers, struct ast_ari_events_event_websocket_args *args) { diff --git a/res/ari/resource_events.h b/res/ari/resource_events.h index 646cf9bfc..2b631819b 100644 --- a/res/ari/resource_events.h +++ b/res/ari/resource_events.h @@ -48,6 +48,19 @@ struct ast_ari_events_event_websocket_args { /*! Parsing context for app. */ char *app_parse; }; + +/*! + * \brief WebSocket connection for events. + * + * \param ser HTTP TCP/TLS Server Session + * \param headers HTTP headers + * \param args Swagger parameters + * + * \retval 0 success + * \retval non-zero error + */ +int ast_ari_websocket_events_event_websocket_attempted(struct ast_tcptls_session_instance *ser, struct ast_variable *headers, struct ast_ari_events_event_websocket_args *args); + /*! * \brief WebSocket connection for events. * @@ -55,7 +68,7 @@ struct ast_ari_events_event_websocket_args { * \param headers HTTP headers. * \param args Swagger parameters. */ -void ast_ari_websocket_events_event_websocket(struct ast_ari_websocket_session *session, struct ast_variable *headers, struct ast_ari_events_event_websocket_args *args); +void ast_ari_websocket_events_event_websocket_established(struct ast_ari_websocket_session *session, struct ast_variable *headers, struct ast_ari_events_event_websocket_args *args); /*! Argument struct for ast_ari_events_user_event() */ struct ast_ari_events_user_event_args { /*! Event name */ |