mirror of
https://github.com/asterisk/asterisk.git
synced 2025-10-12 15:45:18 +00:00
ARI/PJSIP: Add the ability to redirect (transfer) a channel in a Stasis app
This patch adds a new feature to ARI to redirect a channel to another server, and fixes a few bugs in PJSIP's handling of the Transfer dialplan application/ARI redirect capability. *New Feature* A new operation has been added to the ARI channels resource, redirect. With this, a channel in a Stasis application can be redirected to another endpoint of the same underlying channel technology. *Bug fixes* In the process of writing this new feature, two bugs were fixed in the PJSIP stack: (1) The existing .transfer channel callback had the limitation that it could only transfer channels to a SIP URI, i.e., you had to pass 'PJSIP/sip:foo@my_provider.com' to the dialplan application. While this is still supported, it is somewhat unintuitive - particularly in a world full of endpoints. As such, we now also support specifying the PJSIP endpoint to transfer to. (2) res_pjsip_multihomed was, unfortunately, trying to 'help' a 302 redirect by updating its Contact header. Alas, that resulted in the forwarding destination set by the dialplan application/ARI resource/whatever being rewritten with very incorrect information. Hence, we now don't bother updating an outgoing response if it is a 302. Since this took a looong time to find, some additional debug statements have been added to those modules that update the Contact headers. Review: https://reviewboard.asterisk.org/r/4316/ ASTERISK-24015 #close Reported by: Private Name ASTERISK-24703 #close Reported by: Matt Jordan git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/13@431717 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
@@ -704,6 +704,106 @@ static void ast_ari_channels_continue_in_dialplan_cb(
|
||||
}
|
||||
#endif /* AST_DEVMODE */
|
||||
|
||||
fin: __attribute__((unused))
|
||||
return;
|
||||
}
|
||||
int ast_ari_channels_redirect_parse_body(
|
||||
struct ast_json *body,
|
||||
struct ast_ari_channels_redirect_args *args)
|
||||
{
|
||||
struct ast_json *field;
|
||||
/* Parse query parameters out of it */
|
||||
field = ast_json_object_get(body, "endpoint");
|
||||
if (field) {
|
||||
args->endpoint = ast_json_string_get(field);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Parameter parsing callback for /channels/{channelId}/redirect.
|
||||
* \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_channels_redirect_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_ari_channels_redirect_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;
|
||||
#endif /* AST_DEVMODE */
|
||||
|
||||
for (i = get_params; i; i = i->next) {
|
||||
if (strcmp(i->name, "endpoint") == 0) {
|
||||
args.endpoint = (i->value);
|
||||
} else
|
||||
{}
|
||||
}
|
||||
for (i = path_vars; i; i = i->next) {
|
||||
if (strcmp(i->name, "channelId") == 0) {
|
||||
args.channel_id = (i->value);
|
||||
} 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_channels_redirect_parse_body(body, &args)) {
|
||||
ast_ari_response_alloc_failed(response);
|
||||
goto fin;
|
||||
}
|
||||
ast_ari_channels_redirect(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 400: /* Endpoint parameter not provided */
|
||||
case 404: /* Channel or endpoint not found */
|
||||
case 409: /* Channel not in a Stasis application */
|
||||
case 422: /* Endpoint is not the same type as the channel */
|
||||
is_valid = 1;
|
||||
break;
|
||||
default:
|
||||
if (200 <= code && code <= 299) {
|
||||
is_valid = ast_ari_validate_void(
|
||||
response->message);
|
||||
} else {
|
||||
ast_log(LOG_ERROR, "Invalid error response %d for /channels/{channelId}/redirect\n", code);
|
||||
is_valid = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_valid) {
|
||||
ast_log(LOG_ERROR, "Response validation failed for /channels/{channelId}/redirect\n");
|
||||
ast_ari_response_error(response, 500,
|
||||
"Internal Server Error", "Response validation failed");
|
||||
}
|
||||
#endif /* AST_DEVMODE */
|
||||
|
||||
fin: __attribute__((unused))
|
||||
return;
|
||||
}
|
||||
@@ -2462,6 +2562,15 @@ static struct stasis_rest_handlers channels_channelId_continue = {
|
||||
.children = { }
|
||||
};
|
||||
/*! \brief REST handler for /api-docs/channels.{format} */
|
||||
static struct stasis_rest_handlers channels_channelId_redirect = {
|
||||
.path_segment = "redirect",
|
||||
.callbacks = {
|
||||
[AST_HTTP_POST] = ast_ari_channels_redirect_cb,
|
||||
},
|
||||
.num_children = 0,
|
||||
.children = { }
|
||||
};
|
||||
/*! \brief REST handler for /api-docs/channels.{format} */
|
||||
static struct stasis_rest_handlers channels_channelId_answer = {
|
||||
.path_segment = "answer",
|
||||
.callbacks = {
|
||||
@@ -2595,8 +2704,8 @@ static struct stasis_rest_handlers channels_channelId = {
|
||||
[AST_HTTP_POST] = ast_ari_channels_originate_with_id_cb,
|
||||
[AST_HTTP_DELETE] = ast_ari_channels_hangup_cb,
|
||||
},
|
||||
.num_children = 12,
|
||||
.children = { &channels_channelId_continue,&channels_channelId_answer,&channels_channelId_ring,&channels_channelId_dtmf,&channels_channelId_mute,&channels_channelId_hold,&channels_channelId_moh,&channels_channelId_silence,&channels_channelId_play,&channels_channelId_record,&channels_channelId_variable,&channels_channelId_snoop, }
|
||||
.num_children = 13,
|
||||
.children = { &channels_channelId_continue,&channels_channelId_redirect,&channels_channelId_answer,&channels_channelId_ring,&channels_channelId_dtmf,&channels_channelId_mute,&channels_channelId_hold,&channels_channelId_moh,&channels_channelId_silence,&channels_channelId_play,&channels_channelId_record,&channels_channelId_variable,&channels_channelId_snoop, }
|
||||
};
|
||||
/*! \brief REST handler for /api-docs/channels.{format} */
|
||||
static struct stasis_rest_handlers channels = {
|
||||
|
Reference in New Issue
Block a user