mirror of
https://github.com/asterisk/asterisk.git
synced 2026-07-04 05:55:58 -07:00
c9a3d4562d
This patch started with the simple idea of changing the /events data
model to be more sane. The original model would send out events like:
{ "stasis_start": { "args": [], "channel": { ... } } }
The event discriminator was the field name instead of being a value in
the object, due to limitations in how Swagger 1.1 could model objects.
While technically sufficient in communicating event information, it was
really difficult to deal with in terms of client side JSON handling.
This patch takes advantage of a proposed extension[1] to Swagger which
allows type variance through the use of a discriminator field. This had
a domino effect that made this a surprisingly large patch.
[1]: https://groups.google.com/d/msg/wordnik-api/EC3rGajE0os/ey_5dBI_jWcJ
In changing the models, I also had to change the swagger_model.py
processor so it can handle the type discriminator and subtyping. I took
that a big step forward, and using that information to generate an
ari_model module, which can validate a JSON object against the Swagger
model.
The REST and WebSocket generators were changed to take advantage of the
validators. If compiled with AST_DEVMODE enabled, JSON objects that
don't match their corresponding models will not be sent out. For REST
API calls, a 500 Internal Server response is sent. For WebSockets, the
invalid JSON message is replaced with an error message.
Since this took over about half of the job of the existing JSON
generators, and the .to_json virtual function on messages took over the
other half, I reluctantly removed the generators.
The validators turned up all sorts of errors and inconsistencies in our
data models, and the code. These were cleaned up, with checks in the
code generator avoid some of the consistency problems in the future.
* The model for a channel snapshot was trimmed down to match the
information sent via AMI. Many of the field being sent were not
useful in the general case.
* The model for a bridge snapshot was updated to be more consistent
with the other ARI models.
Another impact of introducing subtyping was that the swagger-codegen
documentation generator was insufficient (at least until it catches up
with Swagger 1.2). I wanted it to be easier to generate docs for the API
anyways, so I ported the wiki pages to use the Asterisk Swagger
generator. In the process, I was able to clean up many of the model
links, which would occasionally give inconsistent results on the wiki. I
also added error responses to the wiki docs, making the wiki
documentation more complete.
Finally, since Stasis-HTTP will now be named Asterisk REST Interface
(ARI), any new functions and files I created carry the ari_ prefix. I
changed a few stasis_http references to ari where it was non-intrusive
and made sense.
(closes issue ASTERISK-21885)
Review: https://reviewboard.asterisk.org/r/2639/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@393529 65c4cc65-6c06-0410-ace0-fbb531ad65f3
232 lines
6.9 KiB
C
232 lines
6.9 KiB
C
/*
|
|
* Asterisk -- An open source telephony toolkit.
|
|
*
|
|
* Copyright (C) 2012 - 2013, Digium, Inc.
|
|
*
|
|
* David M. Lee, II <dlee@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.
|
|
*/
|
|
|
|
#ifndef _ASTERISK_STASIS_HTTP_H
|
|
#define _ASTERISK_STASIS_HTTP_H
|
|
|
|
/*! \file
|
|
*
|
|
* \brief Stasis RESTful API hooks.
|
|
*
|
|
* This header file is used mostly as glue code between generated declarations
|
|
* and res_stasis_http.c.
|
|
*
|
|
* \author David M. Lee, II <dlee@digium.com>
|
|
*/
|
|
|
|
#include "asterisk/http.h"
|
|
#include "asterisk/json.h"
|
|
#include "asterisk/http_websocket.h"
|
|
|
|
/*!
|
|
* \brief Configured encoding format for JSON output.
|
|
* \return JSON output encoding (compact, pretty, etc.)
|
|
*/
|
|
enum ast_json_encoding_format stasis_http_json_format(void);
|
|
|
|
struct stasis_http_response;
|
|
|
|
/*!
|
|
* \brief Callback type for RESTful method handlers.
|
|
* \param get_params GET parameters from the HTTP request.
|
|
* \param path_vars Path variables from any wildcard path segments.
|
|
* \param headers HTTP headers from the HTTP requiest.
|
|
* \param[out] response The RESTful response.
|
|
*/
|
|
typedef void (*stasis_rest_callback)(struct ast_variable *get_params,
|
|
struct ast_variable *path_vars,
|
|
struct ast_variable *headers,
|
|
struct stasis_http_response *response);
|
|
|
|
/*!
|
|
* \brief Handler for a single RESTful path segment.
|
|
*/
|
|
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.
|
|
*
|
|
* 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 */
|
|
struct stasis_rest_handlers *children[];
|
|
};
|
|
|
|
/*!
|
|
* Response type for RESTful requests
|
|
*/
|
|
struct stasis_http_response {
|
|
/*! Response message */
|
|
struct ast_json *message;
|
|
/*! \r\n seperated response headers */
|
|
struct ast_str *headers;
|
|
/*! HTTP response code.
|
|
* 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? */
|
|
/*! Flag to indicate that no further response is needed */
|
|
int no_response:1;
|
|
};
|
|
|
|
/*!
|
|
* Add a resource for REST handling.
|
|
* \param handler Handler to add.
|
|
* \return 0 on success.
|
|
* \return non-zero on failure.
|
|
*/
|
|
int stasis_http_add_handler(struct stasis_rest_handlers *handler);
|
|
|
|
/*!
|
|
* Remove a resource for REST handling.
|
|
* \param handler Handler to add.
|
|
* \return 0 on success.
|
|
* \return non-zero on failure.
|
|
*/
|
|
int stasis_http_remove_handler(struct stasis_rest_handlers *handler);
|
|
|
|
/*!
|
|
* \internal
|
|
* \brief Stasis RESTful invocation handler.
|
|
*
|
|
* 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(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
|
|
* \brief Service function for API declarations.
|
|
*
|
|
* Only call from res_stasis_http and test_stasis_http. Only public to allow
|
|
* for unit testing.
|
|
*
|
|
* \param uri Requested URI, relative to the docs path.
|
|
* \param headers HTTP headers.
|
|
* \param[out] response RESTful HTTP response.
|
|
*/
|
|
void stasis_http_get_docs(const char *uri, struct ast_variable *headers, struct stasis_http_response *response);
|
|
|
|
/*! \brief Abstraction for reading/writing JSON to a WebSocket */
|
|
struct ari_websocket_session;
|
|
|
|
/*!
|
|
* \brief Create an ARI WebSocket session.
|
|
*
|
|
* If \c NULL is given for the validator function, no validation will be
|
|
* performed.
|
|
*
|
|
* \param ws_session Underlying WebSocket session.
|
|
* \param validator Function to validate outgoing messages.
|
|
* \return New ARI WebSocket session.
|
|
* \return \c NULL on error.
|
|
*/
|
|
struct ari_websocket_session *ari_websocket_session_create(
|
|
struct ast_websocket *ws_session, int (*validator)(struct ast_json *));
|
|
|
|
/*!
|
|
* \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.
|
|
* \param response Response to fill in.
|
|
* \param response_code HTTP response code.
|
|
* \param response_text Text corresponding to the HTTP response code.
|
|
* \param message_fmt Error message format string.
|
|
*/
|
|
void stasis_http_response_error(struct stasis_http_response *response,
|
|
int response_code,
|
|
const char *response_text,
|
|
const char *message_fmt, ...)
|
|
__attribute__((format(printf, 4, 5)));
|
|
|
|
/*!
|
|
* \brief Fill in an \c OK (200) \a stasis_http_response.
|
|
* \param response Response to fill in.
|
|
* \param message JSON response. This reference is stolen, so just \ref
|
|
* ast_json_incref if you need to keep a reference to it.
|
|
*/
|
|
void stasis_http_response_ok(struct stasis_http_response *response,
|
|
struct ast_json *message);
|
|
|
|
/*!
|
|
* \brief Fill in a <tt>No Content</tt> (204) \a stasis_http_response.
|
|
*/
|
|
void stasis_http_response_no_content(struct stasis_http_response *response);
|
|
|
|
/*!
|
|
* \brief Fill in a <tt>Created</tt> (201) \a stasis_http_response.
|
|
*/
|
|
void stasis_http_response_created(struct stasis_http_response *response,
|
|
const char *url, struct ast_json *message);
|
|
|
|
/*!
|
|
* \brief Fill in \a response with a 500 message for allocation failures.
|
|
* \param response Response to fill in.
|
|
*/
|
|
void stasis_http_response_alloc_failed(struct stasis_http_response *response);
|
|
|
|
#endif /* _ASTERISK_STASIS_HTTP_H */
|