Merge "rtp_engine/res_rtp_asterisk: Fix RTP struct reentrancy crashes."
[asterisk/asterisk.git] / res / res_ari_recordings.c
index 0657fa3..57d80f2 100644 (file)
 
 /*** MODULEINFO
        <depend type="module">res_ari</depend>
 
 /*** MODULEINFO
        <depend type="module">res_ari</depend>
+       <depend type="module">res_ari_model</depend>
        <depend type="module">res_stasis</depend>
        <support_level>core</support_level>
  ***/
 
 #include "asterisk.h"
 
        <depend type="module">res_stasis</depend>
        <support_level>core</support_level>
  ***/
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
 #include "asterisk/app.h"
 #include "asterisk/module.h"
 #include "asterisk/stasis_app.h"
 #include "asterisk/app.h"
 #include "asterisk/module.h"
 #include "asterisk/stasis_app.h"
@@ -61,10 +60,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 static void ast_ari_recordings_list_stored_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
 static void ast_ari_recordings_list_stored_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
-       struct ast_variable *headers, struct ast_ari_response *response)
+       struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
 {
        struct ast_ari_recordings_list_stored_args args = {};
 {
        struct ast_ari_recordings_list_stored_args args = {};
-       RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
@@ -112,11 +110,10 @@ fin: __attribute__((unused))
 static void ast_ari_recordings_get_stored_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
 static void ast_ari_recordings_get_stored_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
-       struct ast_variable *headers, struct ast_ari_response *response)
+       struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
 {
        struct ast_ari_recordings_get_stored_args args = {};
        struct ast_variable *i;
 {
        struct ast_ari_recordings_get_stored_args args = {};
        struct ast_variable *i;
-       RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
@@ -171,11 +168,10 @@ fin: __attribute__((unused))
 static void ast_ari_recordings_delete_stored_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
 static void ast_ari_recordings_delete_stored_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
-       struct ast_variable *headers, struct ast_ari_response *response)
+       struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
 {
        struct ast_ari_recordings_delete_stored_args args = {};
        struct ast_variable *i;
 {
        struct ast_ari_recordings_delete_stored_args args = {};
        struct ast_variable *i;
-       RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
@@ -220,6 +216,65 @@ static void ast_ari_recordings_delete_stored_cb(
 fin: __attribute__((unused))
        return;
 }
 fin: __attribute__((unused))
        return;
 }
+/*!
+ * \brief Parameter parsing callback for /recordings/stored/{recordingName}/file.
+ * \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 ast_ari_recordings_get_stored_file_cb(
+       struct ast_tcptls_session_instance *ser,
+       struct ast_variable *get_params, struct ast_variable *path_vars,
+       struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
+{
+       struct ast_ari_recordings_get_stored_file_args args = {};
+       struct ast_variable *i;
+#if defined(AST_DEVMODE)
+       int is_valid;
+       int code;
+#endif /* AST_DEVMODE */
+
+       for (i = path_vars; i; i = i->next) {
+               if (strcmp(i->name, "recordingName") == 0) {
+                       args.recording_name = (i->value);
+               } else
+               {}
+       }
+       ast_ari_recordings_get_stored_file(ser, headers, &args, response);
+#if defined(AST_DEVMODE)
+       code = response->response_code;
+
+       switch (code) {
+       case 0: /* Implementation is still a stub, or the code wasn't set */
+               is_valid = response->message == NULL;
+               break;
+       case 500: /* Internal Server Error */
+       case 501: /* Not Implemented */
+       case 403: /* The recording file could not be opened */
+       case 404: /* Recording not found */
+               is_valid = 1;
+               break;
+       default:
+               if (200 <= code && code <= 299) {
+                       /* No validation on a raw binary response */
+                       is_valid = 1;
+               } else {
+                       ast_log(LOG_ERROR, "Invalid error response %d for /recordings/stored/{recordingName}/file\n", code);
+                       is_valid = 0;
+               }
+       }
+
+       if (!is_valid) {
+               ast_log(LOG_ERROR, "Response validation failed for /recordings/stored/{recordingName}/file\n");
+               ast_ari_response_error(response, 500,
+                       "Internal Server Error", "Response validation failed");
+       }
+#endif /* AST_DEVMODE */
+
+fin: __attribute__((unused))
+       return;
+}
 int ast_ari_recordings_copy_stored_parse_body(
        struct ast_json *body,
        struct ast_ari_recordings_copy_stored_args *args)
 int ast_ari_recordings_copy_stored_parse_body(
        struct ast_json *body,
        struct ast_ari_recordings_copy_stored_args *args)
@@ -243,11 +298,10 @@ int ast_ari_recordings_copy_stored_parse_body(
 static void ast_ari_recordings_copy_stored_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
 static void ast_ari_recordings_copy_stored_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
-       struct ast_variable *headers, struct ast_ari_response *response)
+       struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
 {
        struct ast_ari_recordings_copy_stored_args args = {};
        struct ast_variable *i;
 {
        struct ast_ari_recordings_copy_stored_args args = {};
        struct ast_variable *i;
-       RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
@@ -265,21 +319,6 @@ static void ast_ari_recordings_copy_stored_cb(
                } else
                {}
        }
                } else
                {}
        }
-       /* Look for a JSON request entity */
-       body = ast_http_get_json(ser, headers);
-       if (!body) {
-               switch (errno) {
-               case EFBIG:
-                       ast_ari_response_error(response, 413, "Request Entity Too Large", "Request body too large");
-                       goto fin;
-               case ENOMEM:
-                       ast_ari_response_error(response, 500, "Internal Server Error", "Error processing request");
-                       goto fin;
-               case EIO:
-                       ast_ari_response_error(response, 400, "Bad Request", "Error parsing request body");
-                       goto fin;
-               }
-       }
        if (ast_ari_recordings_copy_stored_parse_body(body, &args)) {
                ast_ari_response_alloc_failed(response);
                goto fin;
        if (ast_ari_recordings_copy_stored_parse_body(body, &args)) {
                ast_ari_response_alloc_failed(response);
                goto fin;
@@ -328,11 +367,10 @@ fin: __attribute__((unused))
 static void ast_ari_recordings_get_live_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
 static void ast_ari_recordings_get_live_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
-       struct ast_variable *headers, struct ast_ari_response *response)
+       struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
 {
        struct ast_ari_recordings_get_live_args args = {};
        struct ast_variable *i;
 {
        struct ast_ari_recordings_get_live_args args = {};
        struct ast_variable *i;
-       RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
@@ -387,11 +425,10 @@ fin: __attribute__((unused))
 static void ast_ari_recordings_cancel_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
 static void ast_ari_recordings_cancel_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
-       struct ast_variable *headers, struct ast_ari_response *response)
+       struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
 {
        struct ast_ari_recordings_cancel_args args = {};
        struct ast_variable *i;
 {
        struct ast_ari_recordings_cancel_args args = {};
        struct ast_variable *i;
-       RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
@@ -446,11 +483,10 @@ fin: __attribute__((unused))
 static void ast_ari_recordings_stop_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
 static void ast_ari_recordings_stop_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
-       struct ast_variable *headers, struct ast_ari_response *response)
+       struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
 {
        struct ast_ari_recordings_stop_args args = {};
        struct ast_variable *i;
 {
        struct ast_ari_recordings_stop_args args = {};
        struct ast_variable *i;
-       RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
@@ -505,11 +541,10 @@ fin: __attribute__((unused))
 static void ast_ari_recordings_pause_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
 static void ast_ari_recordings_pause_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
-       struct ast_variable *headers, struct ast_ari_response *response)
+       struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
 {
        struct ast_ari_recordings_pause_args args = {};
        struct ast_variable *i;
 {
        struct ast_ari_recordings_pause_args args = {};
        struct ast_variable *i;
-       RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
@@ -565,11 +600,10 @@ fin: __attribute__((unused))
 static void ast_ari_recordings_unpause_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
 static void ast_ari_recordings_unpause_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
-       struct ast_variable *headers, struct ast_ari_response *response)
+       struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
 {
        struct ast_ari_recordings_unpause_args args = {};
        struct ast_variable *i;
 {
        struct ast_ari_recordings_unpause_args args = {};
        struct ast_variable *i;
-       RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
@@ -625,11 +659,10 @@ fin: __attribute__((unused))
 static void ast_ari_recordings_mute_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
 static void ast_ari_recordings_mute_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
-       struct ast_variable *headers, struct ast_ari_response *response)
+       struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
 {
        struct ast_ari_recordings_mute_args args = {};
        struct ast_variable *i;
 {
        struct ast_ari_recordings_mute_args args = {};
        struct ast_variable *i;
-       RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
@@ -685,11 +718,10 @@ fin: __attribute__((unused))
 static void ast_ari_recordings_unmute_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
 static void ast_ari_recordings_unmute_cb(
        struct ast_tcptls_session_instance *ser,
        struct ast_variable *get_params, struct ast_variable *path_vars,
-       struct ast_variable *headers, struct ast_ari_response *response)
+       struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
 {
        struct ast_ari_recordings_unmute_args args = {};
        struct ast_variable *i;
 {
        struct ast_ari_recordings_unmute_args args = {};
        struct ast_variable *i;
-       RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
 #if defined(AST_DEVMODE)
        int is_valid;
        int code;
@@ -736,7 +768,16 @@ fin: __attribute__((unused))
        return;
 }
 
        return;
 }
 
-/*! \brief REST handler for /api-docs/recordings.{format} */
+/*! \brief REST handler for /api-docs/recordings.json */
+static struct stasis_rest_handlers recordings_stored_recordingName_file = {
+       .path_segment = "file",
+       .callbacks = {
+               [AST_HTTP_GET] = ast_ari_recordings_get_stored_file_cb,
+       },
+       .num_children = 0,
+       .children = {  }
+};
+/*! \brief REST handler for /api-docs/recordings.json */
 static struct stasis_rest_handlers recordings_stored_recordingName_copy = {
        .path_segment = "copy",
        .callbacks = {
 static struct stasis_rest_handlers recordings_stored_recordingName_copy = {
        .path_segment = "copy",
        .callbacks = {
@@ -745,7 +786,7 @@ static struct stasis_rest_handlers recordings_stored_recordingName_copy = {
        .num_children = 0,
        .children = {  }
 };
        .num_children = 0,
        .children = {  }
 };
-/*! \brief REST handler for /api-docs/recordings.{format} */
+/*! \brief REST handler for /api-docs/recordings.json */
 static struct stasis_rest_handlers recordings_stored_recordingName = {
        .path_segment = "recordingName",
        .is_wildcard = 1,
 static struct stasis_rest_handlers recordings_stored_recordingName = {
        .path_segment = "recordingName",
        .is_wildcard = 1,
@@ -753,10 +794,10 @@ static struct stasis_rest_handlers recordings_stored_recordingName = {
                [AST_HTTP_GET] = ast_ari_recordings_get_stored_cb,
                [AST_HTTP_DELETE] = ast_ari_recordings_delete_stored_cb,
        },
                [AST_HTTP_GET] = ast_ari_recordings_get_stored_cb,
                [AST_HTTP_DELETE] = ast_ari_recordings_delete_stored_cb,
        },
-       .num_children = 1,
-       .children = { &recordings_stored_recordingName_copy, }
+       .num_children = 2,
+       .children = { &recordings_stored_recordingName_file,&recordings_stored_recordingName_copy, }
 };
 };
-/*! \brief REST handler for /api-docs/recordings.{format} */
+/*! \brief REST handler for /api-docs/recordings.json */
 static struct stasis_rest_handlers recordings_stored = {
        .path_segment = "stored",
        .callbacks = {
 static struct stasis_rest_handlers recordings_stored = {
        .path_segment = "stored",
        .callbacks = {
@@ -765,7 +806,7 @@ static struct stasis_rest_handlers recordings_stored = {
        .num_children = 1,
        .children = { &recordings_stored_recordingName, }
 };
        .num_children = 1,
        .children = { &recordings_stored_recordingName, }
 };
-/*! \brief REST handler for /api-docs/recordings.{format} */
+/*! \brief REST handler for /api-docs/recordings.json */
 static struct stasis_rest_handlers recordings_live_recordingName_stop = {
        .path_segment = "stop",
        .callbacks = {
 static struct stasis_rest_handlers recordings_live_recordingName_stop = {
        .path_segment = "stop",
        .callbacks = {
@@ -774,7 +815,7 @@ static struct stasis_rest_handlers recordings_live_recordingName_stop = {
        .num_children = 0,
        .children = {  }
 };
        .num_children = 0,
        .children = {  }
 };
-/*! \brief REST handler for /api-docs/recordings.{format} */
+/*! \brief REST handler for /api-docs/recordings.json */
 static struct stasis_rest_handlers recordings_live_recordingName_pause = {
        .path_segment = "pause",
        .callbacks = {
 static struct stasis_rest_handlers recordings_live_recordingName_pause = {
        .path_segment = "pause",
        .callbacks = {
@@ -784,7 +825,7 @@ static struct stasis_rest_handlers recordings_live_recordingName_pause = {
        .num_children = 0,
        .children = {  }
 };
        .num_children = 0,
        .children = {  }
 };
-/*! \brief REST handler for /api-docs/recordings.{format} */
+/*! \brief REST handler for /api-docs/recordings.json */
 static struct stasis_rest_handlers recordings_live_recordingName_mute = {
        .path_segment = "mute",
        .callbacks = {
 static struct stasis_rest_handlers recordings_live_recordingName_mute = {
        .path_segment = "mute",
        .callbacks = {
@@ -794,7 +835,7 @@ static struct stasis_rest_handlers recordings_live_recordingName_mute = {
        .num_children = 0,
        .children = {  }
 };
        .num_children = 0,
        .children = {  }
 };
-/*! \brief REST handler for /api-docs/recordings.{format} */
+/*! \brief REST handler for /api-docs/recordings.json */
 static struct stasis_rest_handlers recordings_live_recordingName = {
        .path_segment = "recordingName",
        .is_wildcard = 1,
 static struct stasis_rest_handlers recordings_live_recordingName = {
        .path_segment = "recordingName",
        .is_wildcard = 1,
@@ -805,7 +846,7 @@ static struct stasis_rest_handlers recordings_live_recordingName = {
        .num_children = 3,
        .children = { &recordings_live_recordingName_stop,&recordings_live_recordingName_pause,&recordings_live_recordingName_mute, }
 };
        .num_children = 3,
        .children = { &recordings_live_recordingName_stop,&recordings_live_recordingName_pause,&recordings_live_recordingName_mute, }
 };
-/*! \brief REST handler for /api-docs/recordings.{format} */
+/*! \brief REST handler for /api-docs/recordings.json */
 static struct stasis_rest_handlers recordings_live = {
        .path_segment = "live",
        .callbacks = {
 static struct stasis_rest_handlers recordings_live = {
        .path_segment = "live",
        .callbacks = {
@@ -813,7 +854,7 @@ static struct stasis_rest_handlers recordings_live = {
        .num_children = 1,
        .children = { &recordings_live_recordingName, }
 };
        .num_children = 1,
        .children = { &recordings_live_recordingName, }
 };
-/*! \brief REST handler for /api-docs/recordings.{format} */
+/*! \brief REST handler for /api-docs/recordings.json */
 static struct stasis_rest_handlers recordings = {
        .path_segment = "recordings",
        .callbacks = {
 static struct stasis_rest_handlers recordings = {
        .path_segment = "recordings",
        .callbacks = {
@@ -822,19 +863,24 @@ static struct stasis_rest_handlers recordings = {
        .children = { &recordings_stored,&recordings_live, }
 };
 
        .children = { &recordings_stored,&recordings_live, }
 };
 
+static int unload_module(void)
+{
+       ast_ari_remove_handler(&recordings);
+       stasis_app_unref();
+       return 0;
+}
+
 static int load_module(void)
 {
        int res = 0;
        stasis_app_ref();
        res |= ast_ari_add_handler(&recordings);
 static int load_module(void)
 {
        int res = 0;
        stasis_app_ref();
        res |= ast_ari_add_handler(&recordings);
-       return res;
-}
+       if (res) {
+               unload_module();
+               return AST_MODULE_LOAD_DECLINE;
+       }
 
 
-static int unload_module(void)
-{
-       ast_ari_remove_handler(&recordings);
-       stasis_app_unref();
-       return 0;
+       return AST_MODULE_LOAD_SUCCESS;
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Recording resources",
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Recording resources",
@@ -842,4 +888,4 @@ AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Rec
        .load = load_module,
        .unload = unload_module,
        .nonoptreq = "res_ari,res_stasis",
        .load = load_module,
        .unload = unload_module,
        .nonoptreq = "res_ari,res_stasis",
-       );
+);