[asterisk-commits] dlee: trunk r393528 - in /trunk: ./ include/asterisk/ res/ res/stasis_http/ r...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Wed Jul 3 11:32:02 CDT 2013
Author: dlee
Date: Wed Jul 3 11:32:00 2013
New Revision: 393528
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=393528
Log:
Shuffle RESTful URL's around.
This patch moves the RESTful URL's around to more appropriate
locations for release.
The /stasis URL's are moved to /ari, since Asterisk REST Interface was
a more appropriate name than Stasis-HTTP. (Most of the code still has
stasis_http references, but they will be cleaned up after there are no
more outstanding branches that would have merge conflicts with such a
change).
A larger change was moving the ARI events WebSocket off of the shared
/ws URL to its permanent home on /ari/events. The Swagger code
generator was extended to handle "upgrade: websocket" and
"websocketProtocol:" attributes on an operation.
The WebSocket module was modified to better handle WebSocket servers
that have a single registered protocol handler. If a client
connections does not specify the Sec-WebSocket-Protocol header, and
the server has a single protocol handler registered, the WebSocket
server will go ahead and accept the client for that subprotocol.
(closes issue ASTERISK-21857)
Review: https://reviewboard.asterisk.org/r/2621/
Added:
trunk/res/stasis_http/ari_websockets.c
- copied, made public unchanged from r393524, team/dlee/private/merges/res/stasis_http/ari_websockets.c
trunk/rest-api-templates/param_parsing.mustache
- copied, made public unchanged from r393524, team/dlee/private/merges/rest-api-templates/param_parsing.mustache
Removed:
trunk/res/res_stasis_websocket.c
Modified:
trunk/configure
trunk/configure.ac
trunk/include/asterisk/autoconfig.h.in
trunk/include/asterisk/http.h
trunk/include/asterisk/stasis_http.h
trunk/include/asterisk/utils.h
trunk/res/Makefile
trunk/res/res_http_websocket.c
trunk/res/res_http_websocket.exports.in
trunk/res/res_stasis_http.c
trunk/res/res_stasis_http.exports.in
trunk/res/res_stasis_http_asterisk.c
trunk/res/res_stasis_http_bridges.c
trunk/res/res_stasis_http_channels.c
trunk/res/res_stasis_http_endpoints.c
trunk/res/res_stasis_http_events.c
trunk/res/res_stasis_http_playback.c
trunk/res/res_stasis_http_recordings.c
trunk/res/res_stasis_http_sounds.c
trunk/res/stasis_http/resource_events.c
trunk/res/stasis_http/resource_events.h
trunk/rest-api-templates/asterisk_processor.py
trunk/rest-api-templates/res_stasis_http_resource.c.mustache
trunk/rest-api-templates/rest_handler.mustache
trunk/rest-api-templates/stasis_http_resource.c.mustache
trunk/rest-api-templates/stasis_http_resource.h.mustache
trunk/rest-api-templates/swagger_model.py
trunk/rest-api/api-docs/events.json
trunk/tests/test_stasis_http.c
trunk/tests/test_utils.c
Modified: trunk/configure.ac
URL: http://svnview.digium.com/svn/asterisk/trunk/configure.ac?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/configure.ac (original)
+++ trunk/configure.ac Wed Jul 3 11:32:00 2013
@@ -388,6 +388,7 @@
AST_EXT_LIB_SETUP([COROSYNC], [Corosync], [cpg])
AST_EXT_LIB_SETUP_OPTIONAL([COROSYNC_CFG_STATE_TRACK], [A callback only in corosync 1.x], [COROSYNC], [cfg])
AST_EXT_LIB_SETUP([CURSES], [curses], [curses])
+AST_EXT_LIB_SETUP([CRYPT], [password and data encryption], [crypt])
AST_EXT_LIB_SETUP([CRYPTO], [OpenSSL Cryptography], [crypto])
AST_EXT_LIB_SETUP_OPTIONAL([OPENSSL_SRTP], [OpenSSL SRTP Extension Support], [CRYPTO], [crypto])
AST_EXT_LIB_SETUP([DAHDI], [DAHDI], [dahdi])
@@ -2148,6 +2149,29 @@
exit 1
fi
+# Find crypt support
+# * -lcrypt on *NIX
+# * in libsystem on OS X
+AST_EXT_LIB_CHECK([LIBCRYPT], [crypt], [crypt], [crypt.h])
+AC_CHECK_FUNC([crypt], [SYSCRYPT=true], [SYSCRYPT=""])
+
+if test "x$LIBCRYPT_LIB" != "x" ; then
+ CRYPT_LIB="$LIBCRYPT_LIB"
+ CRYPT_INCLUDE="$LIBCRYPT_INCLUDE"
+ AC_DEFINE([HAVE_CRYPT], [1], [Define to 1 if you have the `crypt' function.])
+elif test "x$SYSCRYPT" != "x" ; then
+ CRYPT_LIB=""
+ CRYPT_INCLUDE=""
+ AC_DEFINE([HAVE_CRYPT], [1], [Define to 1 if you have the `crypt' function.])
+fi
+
+AC_SUBST(CRYPT_LIB)
+AC_SUBST(CRYPT_INCLUDE)
+
+# Find crypt_r support
+AC_CHECK_LIB([crypt], [crypt_r],
+ [AC_DEFINE([HAVE_CRYPT_R], [1], [Define to 1 if you have the `crypt_r' function.])])
+
AST_EXT_LIB_CHECK([CRYPTO], [crypto], [AES_encrypt], [openssl/aes.h])
if test "$PBX_CRYPTO" = "1";
Modified: trunk/include/asterisk/autoconfig.h.in
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/autoconfig.h.in?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/include/asterisk/autoconfig.h.in (original)
+++ trunk/include/asterisk/autoconfig.h.in Wed Jul 3 11:32:00 2013
@@ -149,8 +149,14 @@
/* Define to 1 if you have the `cosl' function. */
#undef HAVE_COSL
+/* Define to 1 if you have the `crypt' function. */
+#undef HAVE_CRYPT
+
/* Define to 1 if you have the OpenSSL Cryptography library. */
#undef HAVE_CRYPTO
+
+/* Define to 1 if you have the `crypt_r' function. */
+#undef HAVE_CRYPT_R
/* Define to 1 if you have a functional curl library. */
#undef HAVE_CURL
Modified: trunk/include/asterisk/http.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/http.h?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/include/asterisk/http.h (original)
+++ trunk/include/asterisk/http.h Wed Jul 3 11:32:00 2013
@@ -112,6 +112,26 @@
/*! \brief Get cookie from Request headers */
struct ast_variable *ast_http_get_cookies(struct ast_variable *headers);
+/*! \brief HTTP authentication information. */
+struct ast_http_auth {
+ /*! Provided userid. */
+ char *userid;
+ /*! For Basic auth, the provided password. */
+ char *password;
+};
+
+/*!
+ * \brief Get HTTP authentication information from headers.
+ *
+ * The returned object is AO2 managed, so clean up with ao2_cleanup().
+ *
+ * \param headers HTTP request headers.
+ * \return HTTP auth structure.
+ * \return \c NULL if no supported HTTP auth headers present.
+ * \since 12
+ */
+struct ast_http_auth *ast_http_get_auth(struct ast_variable *headers);
+
/*! \brief Register a URI handler */
int ast_http_uri_link(struct ast_http_uri *urihandler);
Modified: trunk/include/asterisk/stasis_http.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/stasis_http.h?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/include/asterisk/stasis_http.h (original)
+++ trunk/include/asterisk/stasis_http.h Wed Jul 3 11:32:00 2013
@@ -53,13 +53,17 @@
struct stasis_rest_handlers {
/*! Path segement to handle */
const char *path_segment;
- /*! If true (non-zero), path_segment is a wildcard, and will match all values.
+ /*! If true (non-zero), path_segment is a wildcard, and will match all
+ * values.
*
- * Value of the segement will be passed into the \a path_vars parameter of the callback.
+ * Value of the segement will be passed into the \a path_vars parameter
+ * of the callback.
*/
int is_wildcard;
/*! Callbacks for all handled HTTP methods. */
stasis_rest_callback callbacks[AST_HTTP_MAX_METHOD];
+ /*! WebSocket server for handling WebSocket upgrades. */
+ struct ast_websocket_server *ws_server;
/*! Number of children in the children array */
size_t num_children;
/*! Handlers for sub-paths */
@@ -78,7 +82,9 @@
* See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html */
int response_code;
/*! Corresponding text for the response code */
- const char *response_text; // Shouldn't http.c handle this?
+ const char *response_text; /* Shouldn't http.c handle this? */
+ /*! Flag to indicate that no further response is needed */
+ int no_response:1;
};
/*!
@@ -104,14 +110,17 @@
* Only call from res_stasis_http and test_stasis_http. Only public to allow
* for unit testing.
*
+ * \param ser TCP/TLS connection.
* \param uri HTTP URI, relative to the API path.
* \param method HTTP method.
* \param get_params HTTP \c GET parameters.
* \param headers HTTP headers.
* \param[out] response RESTful HTTP response.
*/
-void stasis_http_invoke(const char *uri, enum ast_http_method method, struct ast_variable *get_params,
- struct ast_variable *headers, struct stasis_http_response *response);
+void stasis_http_invoke(struct ast_tcptls_session_instance *ser,
+ const char *uri, enum ast_http_method method,
+ struct ast_variable *get_params, struct ast_variable *headers,
+ struct stasis_http_response *response);
/*!
* \internal
@@ -126,14 +135,49 @@
*/
void stasis_http_get_docs(const char *uri, struct ast_variable *headers, struct stasis_http_response *response);
-/*!
- * \internal
- * \brief Stasis WebSocket connection handler
- * \param session WebSocket session.
- * \param parameters HTTP \c GET parameters.
- * \param headers HTTP headers.
- */
-void stasis_websocket_callback(struct ast_websocket *session, struct ast_variable *parameters, struct ast_variable *headers);
+/*! \brief Abstraction for reading/writing JSON to a WebSocket */
+struct ari_websocket_session;
+
+/*!
+ * \brief Create an ARI WebSocket session.
+ *
+ * \param ws_session Underlying WebSocket session.
+ * \return New ARI WebSocket session.
+ * \return \c NULL on error.
+ */
+struct ari_websocket_session *ari_websocket_session_create(
+ struct ast_websocket *ws_session);
+
+/*!
+ * \brief Read a message from an ARI WebSocket.
+ *
+ * \param session Session to read from.
+ * \return Message received.
+ * \return \c NULL if WebSocket could not be read.
+ */
+struct ast_json *ari_websocket_session_read(
+ struct ari_websocket_session *session);
+
+/*!
+ * \brief Send a message to an ARI WebSocket.
+ *
+ * \param session Session to write to.
+ * \param message Message to send.
+ * \return 0 on success.
+ * \return Non-zero on error.
+ */
+int ari_websocket_session_write(struct ari_websocket_session *session,
+ struct ast_json *message);
+
+/*!
+ * \brief The stock message to return when out of memory.
+ *
+ * The refcount is NOT bumped on this object, so ast_json_ref() if you want to
+ * keep the reference.
+ *
+ * \return JSON message specifying an out-of-memory error.
+ */
+struct ast_json *ari_oom_json(void);
/*!
* \brief Fill in an error \a stasis_http_response.
Modified: trunk/include/asterisk/utils.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/utils.h?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/include/asterisk/utils.h (original)
+++ trunk/include/asterisk/utils.h Wed Jul 3 11:32:00 2013
@@ -938,4 +938,43 @@
void _dtor_ ## varname (vartype * v) { dtor(*v); } \
vartype varname __attribute__((cleanup(_dtor_ ## varname))) = (initval)
+/*!
+ * \brief Asterisk wrapper around crypt(3).
+ *
+ * The interpretation of the salt (which determines the password hashing
+ * algorithm) is system specific. Application code should prefer to use
+ * ast_crypt_encrypt() or ast_crypt_validate().
+ *
+ * The returned string is heap allocated, and should be freed with ast_free().
+ *
+ * \param key User's password to crypt.
+ * \param salt Salt to crypt with.
+ * \return Crypted password.
+ * \return \c NULL on error.
+ */
+char *ast_crypt(const char *key, const char *salt);
+
+/*
+ * \brief Asterisk wrapper around crypt(3) for encrypting passwords.
+ *
+ * This function will generate a random salt and encrypt the given password.
+ *
+ * The returned string is heap allocated, and should be freed with ast_free().
+ *
+ * \param key User's password to crypt.
+ * \return Crypted password.
+ * \return \c NULL on error.
+ */
+char *ast_crypt_encrypt(const char *key);
+
+/*
+ * \brief Asterisk wrapper around crypt(3) for validating passwords.
+ *
+ * \param key User's password to validate.
+ * \param expected Expected result from crypt.
+ * \return True (non-zero) if \a key matches \a expected.
+ * \return False (zero) if \a key doesn't match.
+ */
+int ast_crypt_validate(const char *key, const char *expected);
+
#endif /* _ASTERISK_UTILS_H */
Modified: trunk/res/Makefile
URL: http://svnview.digium.com/svn/asterisk/trunk/res/Makefile?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/res/Makefile (original)
+++ trunk/res/Makefile Wed Jul 3 11:32:00 2013
@@ -80,5 +80,8 @@
$(if $(filter res_parking,$(EMBEDDED_MODS)),modules.link,res_parking.so): $(subst .c,.o,$(wildcard parking/*.c))
$(subst .c,.o,$(wildcard parking/*.c)): _ASTCFLAGS+=$(call MOD_ASTCFLAGS,res_parking)
+res_stasis_http.so: stasis_http/ari_websockets.o
+stasis_http/ari_websockets.o: _ASTCFLAGS+=$(call MOD_ASTCFLAGS,res_stasis_http_asterisk)
+
# Dependencies for res_stasis_http_*.so are generated, so they're in this file
include stasis_http.make
Modified: trunk/res/res_http_websocket.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_http_websocket.c?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/res/res_http_websocket.c (original)
+++ trunk/res/res_http_websocket.c Wed Jul 3 11:32:00 2013
@@ -282,7 +282,7 @@
void AST_OPTIONAL_API_NAME(ast_websocket_unref)(struct ast_websocket *session)
{
- ao2_ref(session, -1);
+ ao2_cleanup(session);
}
int AST_OPTIONAL_API_NAME(ast_websocket_fd)(struct ast_websocket *session)
@@ -495,6 +495,21 @@
}
return 0;
+}
+
+/*!
+ * \brief If the server has exactly one configured protocol, return it.
+ */
+static struct websocket_protocol *one_protocol(
+ struct ast_websocket_server *server)
+{
+ SCOPED_AO2LOCK(lock, server->protocols);
+
+ if (ao2_container_count(server->protocols) != 1) {
+ return NULL;
+ }
+
+ return ao2_callback(server->protocols, OBJ_NOLOCK, NULL, NULL);
}
int ast_websocket_uri_cb(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_vars, struct ast_variable *headers)
@@ -541,11 +556,18 @@
ast_http_error(ser, 426, "Upgrade Required", NULL);
return -1;
} else if (ast_strlen_zero(requested_protocols)) {
- ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted - no protocols requested\n",
- ast_sockaddr_stringify(&ser->remote_address));
- fputs("HTTP/1.1 400 Bad Request\r\n"
- "Sec-WebSocket-Version: 7, 8, 13\r\n\r\n", ser->f);
- return -1;
+ /* If there's only a single protocol registered, and the
+ * client doesn't specify what protocol it's using, go ahead
+ * and accept the connection */
+ protocol_handler = one_protocol(server);
+ if (!protocol_handler) {
+ /* Multiple registered subprotocols; client must specify */
+ ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted - no protocols requested\n",
+ ast_sockaddr_stringify(&ser->remote_address));
+ fputs("HTTP/1.1 400 Bad Request\r\n"
+ "Sec-WebSocket-Version: 7, 8, 13\r\n\r\n", ser->f);
+ return -1;
+ }
} else if (key1 && key2) {
/* Specification defined in http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76 and
* http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00 -- not currently supported*/
@@ -557,10 +579,8 @@
}
/* Iterate through the requested protocols trying to find one that we have a handler for */
- while ((protocol = strsep(&requested_protocols, ","))) {
- if ((protocol_handler = ao2_find(server->protocols, ast_strip(protocol), OBJ_KEY))) {
- break;
- }
+ while (!protocol_handler && (protocol = strsep(&requested_protocols, ","))) {
+ protocol_handler = ao2_find(server->protocols, ast_strip(protocol), OBJ_KEY);
}
/* If no protocol handler exists bump this back to the requester */
Modified: trunk/res/res_http_websocket.exports.in
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_http_websocket.exports.in?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/res/res_http_websocket.exports.in (original)
+++ trunk/res/res_http_websocket.exports.in Wed Jul 3 11:32:00 2013
@@ -13,6 +13,10 @@
LINKER_SYMBOL_PREFIX*ast_websocket_remote_address;
LINKER_SYMBOL_PREFIX*ast_websocket_is_secure;
LINKER_SYMBOL_PREFIX*ast_websocket_set_nonblock;
+ LINKER_SYMBOL_PREFIX*ast_websocket_uri_cb;
+ LINKER_SYMBOL_PREFIX*ast_websocket_server_create;
+ LINKER_SYMBOL_PREFIX*ast_websocket_server_add_protocol;
+ LINKER_SYMBOL_PREFIX*ast_websocket_server_remove_protocol;
local:
*;
};
Modified: trunk/res/res_stasis_http.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_stasis_http.c?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/res/res_stasis_http.c (original)
+++ trunk/res/res_stasis_http.c Wed Jul 3 11:32:00 2013
@@ -72,6 +72,7 @@
*/
/*** MODULEINFO
+ <depend type="module">res_http_websocket</depend>
<support_level>core</support_level>
***/
@@ -211,7 +212,12 @@
static struct stasis_rest_handlers *root_handler;
/*! Pre-defined message for allocation failures. */
-static struct ast_json *alloc_failed_message;
+static struct ast_json *oom_json;
+
+struct ast_json *ari_oom_json(void)
+{
+ return oom_json;
+}
int stasis_http_add_handler(struct stasis_rest_handlers *handler)
{
@@ -286,7 +292,7 @@
if (!handler) {
return NULL;
}
- handler->path_segment = "stasis";
+ handler->path_segment = "ari";
ao2_ref(handler, +1);
return handler;
@@ -325,7 +331,7 @@
void stasis_http_response_alloc_failed(struct stasis_http_response *response)
{
- response->message = ast_json_ref(alloc_failed_message);
+ response->message = ast_json_ref(oom_json);
response->response_code = 500;
response->response_text = "Internal Server Error";
}
@@ -495,11 +501,10 @@
}
}
-void stasis_http_invoke(const char *uri,
- enum ast_http_method method,
- struct ast_variable *get_params,
- struct ast_variable *headers,
- struct stasis_http_response *response)
+void stasis_http_invoke(struct ast_tcptls_session_instance *ser,
+ const char *uri, enum ast_http_method method,
+ struct ast_variable *get_params, struct ast_variable *headers,
+ struct stasis_http_response *response)
{
RAII_VAR(char *, response_text, NULL, ast_free);
RAII_VAR(struct stasis_rest_handlers *, root, NULL, ao2_cleanup);
@@ -556,6 +561,19 @@
stasis_http_response_error(
response, 405, "Method Not Allowed",
"Invalid method");
+ return;
+ }
+
+ if (handler->ws_server && method == AST_HTTP_GET) {
+ /* WebSocket! */
+ struct ast_http_uri fake_urih = {
+ .data = handler->ws_server,
+ };
+ ast_websocket_uri_cb(ser, &fake_urih, uri, method, get_params,
+ headers);
+ /* Since the WebSocket code handles the connection, we shouldn't
+ * do anything else; setting no_response */
+ response->no_response = 1;
return;
}
@@ -686,7 +704,7 @@
if (host != NULL) {
ast_json_object_set(
obj, "basePath",
- ast_json_stringf("http://%s/stasis", host->value));
+ ast_json_stringf("http://%s/ari", host->value));
} else {
/* Without the host, we don't have the basePath */
ast_json_object_del(obj, "basePath");
@@ -719,7 +737,7 @@
* is probably our best bet.
*/
stasis_http_response_error(response, 404, "Not Found",
- "ARI URLs do not end with a slash. Try /%s", slashless);
+ "ARI URLs do not end with a slash. Try /ari/%s", slashless);
}
/*!
@@ -831,7 +849,14 @@
}
} else {
/* Other RESTful resources */
- stasis_http_invoke(uri, method, get_params, headers, &response);
+ stasis_http_invoke(ser, uri, method, get_params, headers,
+ &response);
+ }
+
+ if (response.no_response) {
+ /* The handler indicates no further response is necessary.
+ * Probably because it already handled it */
+ return 0;
}
/* Leaving message unset is only allowed for 204 (No Content).
@@ -873,7 +898,7 @@
static struct ast_http_uri http_uri = {
.callback = stasis_http_callback,
.description = "Asterisk RESTful API",
- .uri = "stasis",
+ .uri = "ari",
.has_subtree = 1,
.data = NULL,
@@ -883,6 +908,14 @@
static int load_module(void)
{
+ oom_json = ast_json_pack(
+ "{s: s}", "error", "AllocationFailed");
+
+ if (!oom_json) {
+ /* Ironic */
+ return AST_MODULE_LOAD_FAILURE;
+ }
+
ast_mutex_init(&root_handler_lock);
root_handler = root_handler_create();
@@ -905,9 +938,6 @@
return AST_MODULE_LOAD_DECLINE;
}
- alloc_failed_message = ast_json_pack(
- "{s: s}", "message", "Allocation failed");
-
if (is_enabled()) {
ast_http_uri_link(&http_uri);
}
@@ -917,8 +947,8 @@
static int unload_module(void)
{
- ast_json_unref(alloc_failed_message);
- alloc_failed_message = NULL;
+ ast_json_unref(oom_json);
+ oom_json = NULL;
if (is_enabled()) {
ast_http_uri_unlink(&http_uri);
@@ -951,9 +981,10 @@
return AST_MODULE_LOAD_SUCCESS;
}
-AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Stasis HTTP bindings",
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Asterisk RESTful Interface",
.load = load_module,
.unload = unload_module,
.reload = reload_module,
+ .nonoptreq = "res_stasis,res_http_websocket",
.load_pri = AST_MODPRI_APP_DEPEND,
);
Modified: trunk/res/res_stasis_http.exports.in
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_stasis_http.exports.in?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/res/res_stasis_http.exports.in (original)
+++ trunk/res/res_stasis_http.exports.in Wed Jul 3 11:32:00 2013
@@ -1,6 +1,7 @@
{
global:
LINKER_SYMBOL_PREFIXstasis_http_*;
+ LINKER_SYMBOL_PREFIXari_*;
local:
*;
};
Modified: trunk/res/res_stasis_http_asterisk.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_stasis_http_asterisk.c?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/res/res_stasis_http_asterisk.c (original)
+++ trunk/res/res_stasis_http_asterisk.c Wed Jul 3 11:32:00 2013
@@ -88,8 +88,10 @@
static int load_module(void)
{
+ int res = 0;
stasis_app_ref();
- return stasis_http_add_handler(&asterisk);
+ res |= stasis_http_add_handler(&asterisk);
+ return res;
}
static int unload_module(void)
Modified: trunk/res/res_stasis_http_bridges.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_stasis_http_bridges.c?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/res/res_stasis_http_bridges.c (original)
+++ trunk/res/res_stasis_http_bridges.c Wed Jul 3 11:32:00 2013
@@ -276,8 +276,10 @@
static int load_module(void)
{
+ int res = 0;
stasis_app_ref();
- return stasis_http_add_handler(&bridges);
+ res |= stasis_http_add_handler(&bridges);
+ return res;
}
static int unload_module(void)
Modified: trunk/res/res_stasis_http_channels.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_stasis_http_channels.c?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/res/res_stasis_http_channels.c (original)
+++ trunk/res/res_stasis_http_channels.c Wed Jul 3 11:32:00 2013
@@ -528,8 +528,10 @@
static int load_module(void)
{
+ int res = 0;
stasis_app_ref();
- return stasis_http_add_handler(&channels);
+ res |= stasis_http_add_handler(&channels);
+ return res;
}
static int unload_module(void)
Modified: trunk/res/res_stasis_http_endpoints.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_stasis_http_endpoints.c?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/res/res_stasis_http_endpoints.c (original)
+++ trunk/res/res_stasis_http_endpoints.c Wed Jul 3 11:32:00 2013
@@ -139,8 +139,10 @@
static int load_module(void)
{
+ int res = 0;
stasis_app_ref();
- return stasis_http_add_handler(&endpoints);
+ res |= stasis_http_add_handler(&endpoints);
+ return res;
}
static int unload_module(void)
Modified: trunk/res/res_stasis_http_events.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_stasis_http_events.c?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/res/res_stasis_http_events.c (original)
+++ trunk/res/res_stasis_http_events.c Wed Jul 3 11:32:00 2013
@@ -45,17 +45,11 @@
#include "asterisk/stasis_app.h"
#include "stasis_http/resource_events.h"
-/*!
- * \brief Parameter parsing callback for /events.
- * \param get_params GET parameters in the HTTP request.
- * \param path_vars Path variables extracted from the request.
- * \param headers HTTP headers.
- * \param[out] response Response to the HTTP request.
- */
-static void stasis_http_event_websocket_cb(
- struct ast_variable *get_params, struct ast_variable *path_vars,
- struct ast_variable *headers, struct stasis_http_response *response)
+static void stasis_http_event_websocket_ws_cb(struct ast_websocket *ws_session,
+ struct ast_variable *get_params, struct ast_variable *headers)
{
+ RAII_VAR(struct ast_websocket *, s, ws_session, ast_websocket_unref);
+ RAII_VAR(struct ari_websocket_session *, session, NULL, ao2_cleanup);
struct ast_event_websocket_args args = {};
struct ast_variable *i;
@@ -65,14 +59,18 @@
} else
{}
}
- stasis_http_event_websocket(headers, &args, response);
+ session = ari_websocket_session_create(ws_session);
+ if (!session) {
+ ast_log(LOG_ERROR, "Failed to create ARI session\n");
+ return;
+ }
+ ari_websocket_event_websocket(session, headers, &args);
}
/*! \brief REST handler for /api-docs/events.{format} */
static struct stasis_rest_handlers events = {
.path_segment = "events",
.callbacks = {
- [AST_HTTP_GET] = stasis_http_event_websocket_cb,
},
.num_children = 0,
.children = { }
@@ -80,13 +78,23 @@
static int load_module(void)
{
+ int res = 0;
+ events.ws_server = ast_websocket_server_create();
+ if (!events.ws_server) {
+ return AST_MODULE_LOAD_FAILURE;
+ }
+ res |= ast_websocket_server_add_protocol(events.ws_server,
+ "ari", stasis_http_event_websocket_ws_cb);
stasis_app_ref();
- return stasis_http_add_handler(&events);
+ res |= stasis_http_add_handler(&events);
+ return res;
}
static int unload_module(void)
{
stasis_http_remove_handler(&events);
+ ao2_cleanup(events.ws_server);
+ events.ws_server = NULL;
stasis_app_unref();
return 0;
}
Modified: trunk/res/res_stasis_http_playback.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_stasis_http_playback.c?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/res/res_stasis_http_playback.c (original)
+++ trunk/res/res_stasis_http_playback.c Wed Jul 3 11:32:00 2013
@@ -149,8 +149,10 @@
static int load_module(void)
{
+ int res = 0;
stasis_app_ref();
- return stasis_http_add_handler(&playback);
+ res |= stasis_http_add_handler(&playback);
+ return res;
}
static int unload_module(void)
Modified: trunk/res/res_stasis_http_recordings.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_stasis_http_recordings.c?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/res/res_stasis_http_recordings.c (original)
+++ trunk/res/res_stasis_http_recordings.c Wed Jul 3 11:32:00 2013
@@ -383,8 +383,10 @@
static int load_module(void)
{
+ int res = 0;
stasis_app_ref();
- return stasis_http_add_handler(&recordings);
+ res |= stasis_http_add_handler(&recordings);
+ return res;
}
static int unload_module(void)
Modified: trunk/res/res_stasis_http_sounds.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_stasis_http_sounds.c?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/res/res_stasis_http_sounds.c (original)
+++ trunk/res/res_stasis_http_sounds.c Wed Jul 3 11:32:00 2013
@@ -115,8 +115,10 @@
static int load_module(void)
{
+ int res = 0;
stasis_app_ref();
- return stasis_http_add_handler(&sounds);
+ res |= stasis_http_add_handler(&sounds);
+ return res;
}
static int unload_module(void)
Copied: trunk/res/stasis_http/ari_websockets.c (from r393524, team/dlee/private/merges/res/stasis_http/ari_websockets.c)
URL: http://svnview.digium.com/svn/asterisk/trunk/res/stasis_http/ari_websockets.c?view=diff&rev=393528&p1=team/dlee/private/merges/res/stasis_http/ari_websockets.c&r1=393524&p2=trunk/res/stasis_http/ari_websockets.c&r2=393528
==============================================================================
--- team/dlee/private/merges/res/stasis_http/ari_websockets.c (original)
+++ trunk/res/stasis_http/ari_websockets.c Wed Jul 3 11:32:00 2013
@@ -1,0 +1,124 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * David M. Lee, II <dlee at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/astobj2.h"
+#include "asterisk/stasis_http.h"
+
+/*! \file
+ *
+ * \brief WebSocket support for RESTful API's.
+ * \author David M. Lee, II <dlee at digium.com>
+ */
+
+struct ari_websocket_session {
+ struct ast_websocket *ws_session;
+};
+
+static void websocket_session_dtor(void *obj)
+{
+ struct ari_websocket_session *session = obj;
+
+ ast_websocket_unref(session->ws_session);
+ session->ws_session = NULL;
+}
+
+struct ari_websocket_session *ari_websocket_session_create(
+ struct ast_websocket *ws_session)
+{
+ RAII_VAR(struct ari_websocket_session *, session, NULL, ao2_cleanup);
+
+ if (ws_session == NULL) {
+ return NULL;
+ }
+
+ if (ast_websocket_set_nonblock(ws_session) != 0) {
+ ast_log(LOG_ERROR,
+ "Stasis web socket failed to set nonblock; closing\n");
+ return NULL;
+ }
+
+ session = ao2_alloc(sizeof(*session), websocket_session_dtor);
+ if (!session) {
+ return NULL;
+ }
+
+ ao2_ref(ws_session, +1);
+ session->ws_session = ws_session;
+
+ ao2_ref(session, +1);
+ return session;
+}
+
+struct ast_json *ari_websocket_session_read(
+ struct ari_websocket_session *session)
+{
+ RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
+
+ while (!message) {
+ int res;
+ char *payload;
+ uint64_t payload_len;
+ enum ast_websocket_opcode opcode;
+ int fragmented;
+
+ res = ast_wait_for_input(
+ ast_websocket_fd(session->ws_session), -1);
+
+ if (res <= 0) {
+ return NULL;
+ }
+
+ res = ast_websocket_read(session->ws_session, &payload,
+ &payload_len, &opcode, &fragmented);
+
+ if (res != 0) {
+ return NULL;
+ }
+
+ switch (opcode) {
+ case AST_WEBSOCKET_OPCODE_CLOSE:
+ return NULL;
+ case AST_WEBSOCKET_OPCODE_TEXT:
+ message = ast_json_load_buf(payload, payload_len, NULL);
+ break;
+ default:
+ /* Ignore all other message types */
+ break;
+ }
+ }
+
+ return ast_json_ref(message);
+}
+
+int ari_websocket_session_write(struct ari_websocket_session *session,
+ struct ast_json *message)
+{
+ RAII_VAR(char *, str, ast_json_dump_string(message), ast_free);
+
+ if (str == NULL) {
+ ast_log(LOG_ERROR, "Failed to encode JSON object\n");
+ return -1;
+ }
+
+ return ast_websocket_write(session->ws_session,
+ AST_WEBSOCKET_OPCODE_TEXT, str, strlen(str));
+}
Modified: trunk/res/stasis_http/resource_events.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/stasis_http/resource_events.c?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/res/stasis_http/resource_events.c (original)
+++ trunk/res/stasis_http/resource_events.c Wed Jul 3 11:32:00 2013
@@ -1,4 +1,4 @@
-/* -*- C -*-
+/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2012 - 2013, Digium, Inc.
@@ -18,23 +18,188 @@
/*! \file
*
- * \brief Implementation for stasis-http stubs.
+ * \brief /api-docs/events.{format} implementation- WebSocket resource
*
* \author David M. Lee, II <dlee at digium.com>
*/
-/*** MODULEINFO
- <support_level>core</support_level>
- ***/
-
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+#include "asterisk/astobj2.h"
+#include "asterisk/stasis_app.h"
#include "resource_events.h"
-void stasis_http_event_websocket(struct ast_variable *headers, struct ast_event_websocket_args *args, struct stasis_http_response *response)
-{
- /* TODO: This should promote this socket to a websocket connection */
- ast_log(LOG_ERROR, "TODO: stasis_http_event_websocket\n");
-}
+/*! Number of buckets for the Stasis application hash table. Remember to keep it
+ * a prime number!
+ */
+#define APPS_NUM_BUCKETS 7
+
+/*! \brief A connection to the event WebSocket */
+struct event_session {
+ struct ari_websocket_session *ws_session;
+ struct ao2_container *websocket_apps;
+};
+
+/*!
+ * \brief Explicitly shutdown a session.
+ *
+ * An explicit shutdown is necessary, since stasis-app has a reference to this
+ * session. We also need to be sure to null out the \c ws_session field, since
+ * the websocket is about to go away.
+ *
+ * \param session Session info struct.
+ */
+static void session_shutdown(struct event_session *session)
+{
+ struct ao2_iterator i;
+ char *app;
+ SCOPED_AO2LOCK(lock, session);
+
+ i = ao2_iterator_init(session->websocket_apps, 0);
+ while ((app = ao2_iterator_next(&i))) {
+ stasis_app_unregister(app);
+ ao2_cleanup(app);
+ }
+ ao2_iterator_destroy(&i);
+ ao2_cleanup(session->websocket_apps);
+
+ session->websocket_apps = NULL;
+ session->ws_session = NULL;
+}
+
+static void session_dtor(void *obj)
+{
+#ifdef AST_DEVMODE /* Avoid unused variable warning */
+ struct event_session *session = obj;
+#endif
+
+ /* session_shutdown should have been called before */
+ ast_assert(session->ws_session == NULL);
+ ast_assert(session->websocket_apps == NULL);
+}
+
+static void session_cleanup(struct event_session *session)
+{
+ session_shutdown(session);
+ ao2_cleanup(session);
+}
+
+static struct event_session *session_create(
+ struct ari_websocket_session *ws_session)
+{
+ RAII_VAR(struct event_session *, session, NULL, ao2_cleanup);
+
+ session = ao2_alloc(sizeof(*session), session_dtor);
+
+ session->ws_session = ws_session;
+ session->websocket_apps =
+ ast_str_container_alloc(APPS_NUM_BUCKETS);
+
+ if (!session->websocket_apps) {
+ return NULL;
+ }
+
+ ao2_ref(session, +1);
+ return session;
+}
+
+/*!
+ * \brief Callback handler for Stasis application messages.
+ */
+static void app_handler(void *data, const char *app_name,
+ struct ast_json *message)
+{
+ struct event_session *session = data;
+ int res;
+
+ res = ast_json_object_set(message, "application",
+ ast_json_string_create(app_name));
+ if(res != 0) {
+ return;
+ }
+
+ ao2_lock(session);
+ if (session->ws_session) {
+ ari_websocket_session_write(session->ws_session, message);
+ }
+ ao2_unlock(session);
+}
+
+/*!
+ * \brief Register for all of the apps given.
+ * \param session Session info struct.
+ * \param app_list Comma seperated list of app names to register.
+ */
+static int session_register_apps(struct event_session *session,
+ const char *app_list)
+{
+ RAII_VAR(char *, to_free, NULL, ast_free);
+ char *apps, *app_name;
+ SCOPED_AO2LOCK(lock, session);
+
+ ast_assert(session->ws_session != NULL);
+ ast_assert(session->websocket_apps != NULL);
+
+ if (!app_list) {
+ return -1;
+ }
+
+ to_free = apps = ast_strdup(app_list);
+ if (!apps) {
+ ari_websocket_session_write(session->ws_session, ari_oom_json());
+ return -1;
+ }
+ while ((app_name = strsep(&apps, ","))) {
+ if (ast_str_container_add(session->websocket_apps, app_name)) {
+ ari_websocket_session_write(session->ws_session, ari_oom_json());
+ return -1;
+ }
+
+ stasis_app_register(app_name, app_handler, session);
+ }
+ return 0;
+}
+
+void ari_websocket_event_websocket(struct ari_websocket_session *ws_session,
+ struct ast_variable *headers,
+ struct ast_event_websocket_args *args)
+{
+ RAII_VAR(struct event_session *, session, NULL, session_cleanup);
+ struct ast_json *msg;
+ int res;
+
+ ast_debug(3, "/events WebSocket connection\n");
+
+ session = session_create(ws_session);
+ if (!session) {
+ ari_websocket_session_write(ws_session, ari_oom_json());
+ return;
+ }
+
+ if (!args->app) {
+ RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
+
+ msg = ast_json_pack("{s: s, s: [s]}",
+ "error", "MissingParams",
+ "params", "app");
+ if (!msg) {
+ msg = ast_json_ref(ari_oom_json());
+ }
+
+ ari_websocket_session_write(session->ws_session, msg);
+ return;
+ }
+
+ res = session_register_apps(session, args->app);
+ if (res != 0) {
+ ari_websocket_session_write(ws_session, ari_oom_json());
+ return;
+ }
+
+ /* We don't process any input, but we'll consume it waiting for EOF */
+ while ((msg = ari_websocket_session_read(ws_session))) {
+ ast_json_unref(msg);
+ }
+}
Modified: trunk/res/stasis_http/resource_events.h
URL: http://svnview.digium.com/svn/asterisk/trunk/res/stasis_http/resource_events.h?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/res/stasis_http/resource_events.h (original)
+++ trunk/res/stasis_http/resource_events.h Wed Jul 3 11:32:00 2013
@@ -43,16 +43,14 @@
struct ast_event_websocket_args {
/*! \brief Comma seperated list of applications to subscribe to. */
const char *app;
- /*! \brief RFC6455 header for upgrading a connection to a websocket. */
- const char *upgrade;
};
/*!
* \brief WebSocket connection for events.
*
- * \param headers HTTP headers
- * \param args Swagger parameters
- * \param[out] response HTTP response
+ * \param session ARI WebSocket.
+ * \param headers HTTP headers.
+ * \param args Swagger parameters.
*/
-void stasis_http_event_websocket(struct ast_variable *headers, struct ast_event_websocket_args *args, struct stasis_http_response *response);
+void ari_websocket_event_websocket(struct ari_websocket_session *session, struct ast_variable *headers, struct ast_event_websocket_args *args);
#endif /* _ASTERISK_RESOURCE_EVENTS_H */
Modified: trunk/rest-api-templates/asterisk_processor.py
URL: http://svnview.digium.com/svn/asterisk/trunk/rest-api-templates/asterisk_processor.py?view=diff&rev=393528&r1=393527&r2=393528
==============================================================================
--- trunk/rest-api-templates/asterisk_processor.py (original)
+++ trunk/rest-api-templates/asterisk_processor.py Wed Jul 3 11:32:00 2013
@@ -144,6 +144,7 @@
segment = resource_api.root_path.get_child(api.path.split('/'))
for operation in api.operations:
segment.operations.append(operation)
+ api.full_name = segment.full_name
resource_api.api_declaration.has_events = False
for model in resource_api.api_declaration.models:
if model.id == "Event":
Copied: trunk/rest-api-templates/param_parsing.mustache (from r393524, team/dlee/private/merges/rest-api-templates/param_parsing.mustache)
[... 458 lines stripped ...]
More information about the asterisk-commits
mailing list