ari.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012 - 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. #ifndef _ASTERISK_ARI_H
  19. #define _ASTERISK_ARI_H
  20. /*! \file
  21. *
  22. * \brief Asterisk RESTful API hooks.
  23. *
  24. * This header file is used mostly as glue code between generated declarations
  25. * and res_ari.c.
  26. *
  27. * \author David M. Lee, II <dlee@digium.com>
  28. */
  29. #include "asterisk/http.h"
  30. #include "asterisk/json.h"
  31. /* Forward-declare websocket structs. This avoids including http_websocket.h,
  32. * which causes optional_api stuff to happen, which makes optional_api more
  33. * difficult to debug. */
  34. struct ast_websocket_server;
  35. struct ast_websocket;
  36. /*!
  37. * \brief Configured encoding format for JSON output.
  38. * \return JSON output encoding (compact, pretty, etc.)
  39. */
  40. enum ast_json_encoding_format ast_ari_json_format(void);
  41. struct ast_ari_response;
  42. /*!
  43. * \brief Callback type for RESTful method handlers.
  44. * \param ser TCP/TLS session object
  45. * \param get_params GET parameters from the HTTP request.
  46. * \param path_vars Path variables from any wildcard path segments.
  47. * \param headers HTTP headers from the HTTP requiest.
  48. * \param[out] response The RESTful response.
  49. */
  50. typedef void (*stasis_rest_callback)(
  51. struct ast_tcptls_session_instance *ser,
  52. struct ast_variable *get_params, struct ast_variable *path_vars,
  53. struct ast_variable *headers, struct ast_json *body,
  54. struct ast_ari_response *response);
  55. /*!
  56. * \brief Handler for a single RESTful path segment.
  57. */
  58. struct stasis_rest_handlers {
  59. /*! Path segement to handle */
  60. const char *path_segment;
  61. /*! If true (non-zero), path_segment is a wildcard, and will match all
  62. * values.
  63. *
  64. * Value of the segement will be passed into the \a path_vars parameter
  65. * of the callback.
  66. */
  67. int is_wildcard;
  68. /*! Callbacks for all handled HTTP methods. */
  69. stasis_rest_callback callbacks[AST_HTTP_MAX_METHOD];
  70. /*! WebSocket server for handling WebSocket upgrades. */
  71. struct ast_websocket_server *ws_server;
  72. /*! Number of children in the children array */
  73. size_t num_children;
  74. /*! Handlers for sub-paths */
  75. struct stasis_rest_handlers *children[];
  76. };
  77. /*!
  78. * Response type for RESTful requests
  79. */
  80. struct ast_ari_response {
  81. /*! Response message */
  82. struct ast_json *message;
  83. /*! \r\n seperated response headers */
  84. struct ast_str *headers;
  85. /*! HTTP response code.
  86. * See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html */
  87. int response_code;
  88. /*! Corresponding text for the response code */
  89. const char *response_text; /* Shouldn't http.c handle this? */
  90. /*! Flag to indicate that no further response is needed */
  91. int no_response:1;
  92. };
  93. /*!
  94. * Add a resource for REST handling.
  95. * \param handler Handler to add.
  96. * \return 0 on success.
  97. * \return non-zero on failure.
  98. */
  99. int ast_ari_add_handler(struct stasis_rest_handlers *handler);
  100. /*!
  101. * Remove a resource for REST handling.
  102. * \param handler Handler to add.
  103. * \return 0 on success.
  104. * \return non-zero on failure.
  105. */
  106. int ast_ari_remove_handler(struct stasis_rest_handlers *handler);
  107. /*!
  108. * \internal
  109. * \brief Stasis RESTful invocation handler.
  110. *
  111. * Only call from res_ari and test_ari. Only public to allow
  112. * for unit testing.
  113. *
  114. * \param ser TCP/TLS connection.
  115. * \param uri HTTP URI, relative to the API path.
  116. * \param method HTTP method.
  117. * \param get_params HTTP \c GET parameters.
  118. * \param headers HTTP headers.
  119. * \param[out] response RESTful HTTP response.
  120. */
  121. void ast_ari_invoke(struct ast_tcptls_session_instance *ser,
  122. const char *uri, enum ast_http_method method,
  123. struct ast_variable *get_params, struct ast_variable *headers,
  124. struct ast_json *body, struct ast_ari_response *response);
  125. /*!
  126. * \internal
  127. * \brief Service function for API declarations.
  128. *
  129. * Only call from res_ari and test_ari. Only public to allow
  130. * for unit testing.
  131. *
  132. * \param uri Requested URI, relative to the docs path.
  133. * \param prefix prefix that prefixes all http requests
  134. * \param headers HTTP headers.
  135. * \param[out] response RESTful HTTP response.
  136. */
  137. void ast_ari_get_docs(const char *uri, const char *prefix, struct ast_variable *headers, struct ast_ari_response *response);
  138. /*! \brief Abstraction for reading/writing JSON to a WebSocket */
  139. struct ast_ari_websocket_session;
  140. /*!
  141. * \brief Create an ARI WebSocket session.
  142. *
  143. * If \c NULL is given for the validator function, no validation will be
  144. * performed.
  145. *
  146. * \param ws_session Underlying WebSocket session.
  147. * \param validator Function to validate outgoing messages.
  148. * \return New ARI WebSocket session.
  149. * \return \c NULL on error.
  150. */
  151. struct ast_ari_websocket_session *ast_ari_websocket_session_create(
  152. struct ast_websocket *ws_session, int (*validator)(struct ast_json *));
  153. /*!
  154. * \brief Read a message from an ARI WebSocket.
  155. *
  156. * \param session Session to read from.
  157. * \return Message received.
  158. * \return \c NULL if WebSocket could not be read.
  159. */
  160. struct ast_json *ast_ari_websocket_session_read(
  161. struct ast_ari_websocket_session *session);
  162. /*!
  163. * \brief Send a message to an ARI WebSocket.
  164. *
  165. * \param session Session to write to.
  166. * \param message Message to send.
  167. * \return 0 on success.
  168. * \return Non-zero on error.
  169. */
  170. int ast_ari_websocket_session_write(struct ast_ari_websocket_session *session,
  171. struct ast_json *message);
  172. /*!
  173. * \brief Get the Session ID for an ARI WebSocket.
  174. *
  175. * \param session Session to query.
  176. * \return Session ID.
  177. * \return \c NULL on error.
  178. */
  179. const char *ast_ari_websocket_session_id(
  180. const struct ast_ari_websocket_session *session);
  181. /*!
  182. * \brief Get the remote address from an ARI WebSocket.
  183. *
  184. * \param session Session to write to.
  185. * \return ast_sockaddr (does not have to be freed)
  186. */
  187. struct ast_sockaddr *ast_ari_websocket_session_get_remote_addr(
  188. struct ast_ari_websocket_session *session);
  189. /*!
  190. * \brief The stock message to return when out of memory.
  191. *
  192. * The refcount is NOT bumped on this object, so ast_json_ref() if you want to
  193. * keep the reference.
  194. *
  195. * \return JSON message specifying an out-of-memory error.
  196. */
  197. struct ast_json *ast_ari_oom_json(void);
  198. /*!
  199. * \brief Fill in an error \a ast_ari_response.
  200. * \param response Response to fill in.
  201. * \param response_code HTTP response code.
  202. * \param response_text Text corresponding to the HTTP response code.
  203. * \param message_fmt Error message format string.
  204. */
  205. void ast_ari_response_error(struct ast_ari_response *response,
  206. int response_code,
  207. const char *response_text,
  208. const char *message_fmt, ...)
  209. __attribute__((format(printf, 4, 5)));
  210. /*!
  211. * \brief Fill in an \c OK (200) \a ast_ari_response.
  212. * \param response Response to fill in.
  213. * \param message JSON response. This reference is stolen, so just \ref
  214. * ast_json_ref if you need to keep a reference to it.
  215. */
  216. void ast_ari_response_ok(struct ast_ari_response *response,
  217. struct ast_json *message);
  218. /*!
  219. * \brief Fill in a <tt>No Content</tt> (204) \a ast_ari_response.
  220. */
  221. void ast_ari_response_no_content(struct ast_ari_response *response);
  222. /*!
  223. * \brief Fill in a <tt>Accepted</tt> (202) \a ast_ari_response.
  224. */
  225. void ast_ari_response_accepted(struct ast_ari_response *response);
  226. /*!
  227. * \brief Fill in a <tt>Created</tt> (201) \a ast_ari_response.
  228. * \param response Response to fill in.
  229. * \param url URL to the created resource.
  230. * \param message JSON response. This reference is stolen, so just \ref
  231. * ast_json_ref if you need to keep a reference to it.
  232. */
  233. void ast_ari_response_created(struct ast_ari_response *response,
  234. const char *url, struct ast_json *message);
  235. /*!
  236. * \brief Fill in \a response with a 500 message for allocation failures.
  237. * \param response Response to fill in.
  238. */
  239. void ast_ari_response_alloc_failed(struct ast_ari_response *response);
  240. /*! \brief Determines whether the res_ari module is loaded */
  241. #define CHECK_ARI_MODULE_LOADED() \
  242. do { \
  243. if (!ast_module_check("res_ari.so") \
  244. || !ast_ari_oom_json()) { \
  245. return AST_MODULE_LOAD_DECLINE; \
  246. } \
  247. } while(0)
  248. #endif /* _ASTERISK_ARI_H */