Debian Codegraph @

res/ari/ari_websocket_requests.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2025, Sangoma Technologies Corporation
5 *
6 * George Joseph <[email protected]>
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
19 #include "asterisk.h"
20
21 #include "ari_websockets.h"
22 #include "asterisk/ari.h"
23 #include "asterisk/json.h"
24 #include "asterisk/stasis_app.h"
25
26 struct rest_request_msg {
27 char *request_type;
28 char *transaction_id;
29 char *request_id;
30 enum ast_http_method method;
31 char *uri;
32 char *content_type;
33 struct ast_variable *query_strings;
34 struct ast_json *body;
35 };
36
37 static void request_destroy(struct rest_request_msg *request)
38 {
39 if (!request) {
40 return;
41 }
42
43 ast_free(request->request_type);
44 ast_free(request->transaction_id);
45 ast_free(request->request_id);
46 ast_free(request->uri);
47 ast_free(request->content_type);
48 ast_variables_destroy(request->query_strings);
49 ast_json_unref(request->body);
50
51 ast_free(request);
52 }
53
54 #define SET_RESPONSE_AND_EXIT(_reponse_code, _reponse_text, \
55 _reponse_msg, _remote_addr, _request, _request_msg) \
56 ({ \
57 RAII_VAR(char *, _msg_str, NULL, ast_json_free); \
58 if (_request_msg) { \
59 _msg_str = ast_json_dump_string_format(_request_msg, AST_JSON_COMPACT); \
60 if (!_msg_str) { \
61 response->response_code = 500; \
62 response->response_text = "Server error. Out of memory"; \
63 } \
64 } \
65 response->message = ast_json_pack("{ s:s }", \
66 "message", _reponse_msg); \
67 response->response_code = _reponse_code; \
68 response->response_text = _reponse_text; \
69 SCOPE_EXIT_LOG_RTN_VALUE(_request, LOG_WARNING, \
70 "%s: %s Request: %s\n", _remote_addr, _reponse_text, S_OR(_msg_str, "<none>")); \
71 })
72
73 static struct rest_request_msg *parse_rest_request_msg(
74 const char *remote_addr, struct ast_json *request_msg, struct ast_variable *get_params,
75 struct ast_ari_response *response, int debug_app)
76 {
77 struct rest_request_msg *request = NULL;
78 RAII_VAR(char *, body, NULL, ast_free);
79 enum ast_json_nvp_ast_vars_code nvp_code;
80 char *query_string_start = NULL;
81 SCOPE_ENTER(4, "%s: Parsing RESTRequest message\n", remote_addr);
82
83 response->response_code = 200;
84 response->response_text = "OK";
85
86 if (!request_msg) {
87 SET_RESPONSE_AND_EXIT(500,
88 "Server error","No message to parse.",
89 remote_addr, request, NULL);
90 }
91
92 request = ast_calloc(1, sizeof(*request));
93 if (!request) {
94 SET_RESPONSE_AND_EXIT(500,
95 "Server error","Out of memory",
96 remote_addr, request, NULL);
97 }
98
99 /* transaction_id is optional */
100 request->transaction_id = ast_strdup(
101 ast_json_string_get(ast_json_object_get(
102 request_msg, "transaction_id")));
103
104 /* request_id is optional */
105 request->request_id = ast_strdup(
106 ast_json_string_get(ast_json_object_get(
107 request_msg, "request_id")));
108
109 request->request_type = ast_strdup(
110 ast_json_string_get(ast_json_object_get(request_msg, "type")));
111 if (ast_strlen_zero(request->request_type)) {
112 SET_RESPONSE_AND_EXIT(400,
113 "Bad request","No 'type' property.",
114 remote_addr, request, request_msg);
115 }
116
117 if (!ast_strings_equal(request->request_type, "RESTRequest")) {
118 SET_RESPONSE_AND_EXIT(400,
119 "Bad request","Unknown request type.",
120 remote_addr, request, request_msg);
121 }
122
123 request->uri = ast_strdup(
124 ast_json_string_get(ast_json_object_get(request_msg, "uri")));
125 if (ast_strlen_zero(request->uri)) {
126 SET_RESPONSE_AND_EXIT(400,
127 "Bad request","Empty or missing 'uri' property.",
128 remote_addr, request, request_msg);
129 }
130 if ((query_string_start = strchr(request->uri, '?')))
131 {
132 *query_string_start = '\0';
133 query_string_start++;
134 request->query_strings = ast_http_parse_post_form(
135 query_string_start, strlen(query_string_start), "application/x-www-form-urlencoded");
136 }
137
138 request->method = ast_get_http_method_from_string(
139 ast_json_string_get(ast_json_object_get(request_msg, "method")));
140 if (request->method == AST_HTTP_UNKNOWN) {
141 SET_RESPONSE_AND_EXIT(400,
142 "Bad request","Unknown or missing 'method' property.",
143 remote_addr, request, request_msg);
144 }
145
146 /* query_strings is optional */
147 nvp_code = ast_json_nvp_array_to_ast_variables(
148 ast_json_object_get(request_msg, "query_strings"),
149 &request->query_strings);
150 if (nvp_code != AST_JSON_NVP_AST_VARS_CODE_SUCCESS &&
151 nvp_code != AST_JSON_NVP_AST_VARS_CODE_NO_INPUT) {
152 SET_RESPONSE_AND_EXIT(400,
153 "Bad request","Unable to parse 'query_strings' array.",
154 remote_addr, request, request_msg);
155 }
156
157 /*
158 * If there were get_params on the original HTTP GET request that created
159 * the websocket, we need to append any api_key parameter to the request->query_strings
160 * to properly authenticate and authorize the request.
161 */
162 if (get_params) {
163 struct ast_variable *v;
164 ast_trace(-1, "%s: Websocket query params:\n", remote_addr);
165 for (v = get_params; v; v = v->next) {
166 if (ast_strings_equal(v->name, "api_key")) {
167 struct ast_variable *api_key = ast_variable_new(v->name, v->value, __FILE__);
168 if (api_key) {
169 ast_variable_list_append_hint(&request->query_strings, NULL, api_key);
170 }
171 break;
172 }
173 }
174 }
175
176 request->body = ast_json_null();
177
178 body = ast_strdup(ast_json_string_get(
179 ast_json_object_get(request_msg, "message_body")));
180
181 if (ast_strlen_zero(body)) {
182 SCOPE_EXIT_RTN_VALUE(request,
183 "%s: Done parsing RESTRequest message.\n", remote_addr);
184 }
185
186 /* content_type is optional */
187 request->content_type = ast_strdup(
188 ast_json_string_get(ast_json_object_get(request_msg, "content_type")));
189
190 if (ast_strlen_zero(request->content_type)) {
191 SET_RESPONSE_AND_EXIT(400,
192 "Bad request","No 'content_type' for 'message_body'.",
193 remote_addr, request, request_msg);
194 }
195
196 if (ast_strings_equal(request->content_type, "application/x-www-form-urlencoded")) {
197 struct ast_variable *vars = ast_http_parse_post_form(body, strlen(body),
198 request->content_type);
199 if (!vars) {
200 SET_RESPONSE_AND_EXIT(400,
201 "Bad request","Unable to parse 'message_body' as 'application/x-www-form-urlencoded'.",
202 remote_addr, request, request_msg);
203 }
204 ast_variable_list_append(&request->query_strings, vars);
205 } else if (ast_strings_equal(request->content_type, "application/json")) {
206 struct ast_json_error error;
207 request->body = ast_json_load_buf(body, strlen(body), &error);
208 if (!request->body) {
209 SET_RESPONSE_AND_EXIT(400,
210 "Bad request","Unable to parse 'message_body' as 'application/json'.",
211 remote_addr, request, request_msg);
212 }
213 } else {
214 SET_RESPONSE_AND_EXIT(400,
215 "Bad request","Unknown content type.",
216 remote_addr, request, request_msg);
217 }
218
219 if (TRACE_ATLEAST(3) || debug_app) {
220 struct ast_variable *v = request->query_strings;
221 for (; v; v = v->next) {
222 ast_trace(-1, "Query string: %s=%s\n", v->name, v->value);
223 }
224 }
225
226 SCOPE_EXIT_RTN_VALUE(request,
227 "%s: Done parsing RESTRequest message.\n", remote_addr);
228 }
229
230 static void send_rest_response(
231 struct ari_ws_session *ari_ws_session,
232 const char *remote_addr, const char *app_name,
233 struct rest_request_msg *request,
234 struct ast_ari_response *response, int debug_app)
235 {
236 struct ast_json *app_resp_json = NULL;
237 char *message = NULL;
238 SCOPE_ENTER(4, "%s: Sending REST response %d:%s for uri %s\n",
239 remote_addr, response->response_code, response->response_text,
240 request ? request->uri : "N/A");
241
242 if (response->fd >= 0) {
243 close(response->fd);
244 response->response_code = 406;
245 response->response_text = "Not Acceptable. Use HTTP GET";
246 } else if (response->message && !ast_json_is_null(response->message)) {
247 message = ast_json_dump_string_format(response->message, AST_JSON_COMPACT);
248 ast_json_unref(response->message);
249 }
250
251 app_resp_json = ast_json_pack(
252 "{s:s, s:s*, s:s*, s:i, s:s, s:s, s:s*, s:s* }",
253 "type", "RESTResponse",
254 "transaction_id", request ? S_OR(request->transaction_id, "") : "",
255 "request_id", request ? S_OR(request->request_id, "") : "",
256 "status_code", response->response_code,
257 "reason_phrase", response->response_text,
258 "uri", request ? S_OR(request->uri, "") : "",
259 "content_type", message ? "application/json" : NULL,
260 "message_body", message);
261
262 ast_json_free(message);
263 if (!app_resp_json || ast_json_is_null(app_resp_json)) {
264 SCOPE_EXIT_LOG_RTN(LOG_WARNING,
265 "%s: Failed to pack JSON response for request %s\n",
266 remote_addr, request ? request->uri : "N/A");
267 }
268
269 SCOPE_CALL(-1, ari_websocket_send_event, ari_ws_session,
270 app_name, app_resp_json, debug_app);
271
272 ast_json_unref(app_resp_json);
273
274 SCOPE_EXIT("%s: Done. response: %d : %s\n",
275 remote_addr,
276 response->response_code,
277 response->response_text);
278 }
279
280 int ari_websocket_process_request(struct ari_ws_session *ari_ws_session,
281 const char *remote_addr, struct ast_variable *get_params,
282 struct ast_variable *upgrade_headers,
283 const char *app_name, struct ast_json *request_msg)
284 {
285 int debug_app = stasis_app_get_debug_by_name(app_name);
286 RAII_VAR(struct rest_request_msg *, request, NULL, request_destroy);
287 struct ast_ari_response response = { .fd = -1, 0 };
288
289 SCOPE_ENTER(3, "%s: New WebSocket Msg\n", remote_addr);
290
291 if (TRACE_ATLEAST(3) || debug_app) {
292 char *str = ast_json_dump_string_format(request_msg, AST_JSON_PRETTY);
293 /* If we can't allocate a string, we can't respond to the client either. */
294 if (!str) {
295 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Failed to dump JSON request\n",
296 remote_addr);
297 }
298 ast_verbose("<--- Received ARI message from %s --->\n%s\n",
299 remote_addr, str);
300 ast_json_free(str);
301 }
302
303 request = SCOPE_CALL_WITH_RESULT(-1, struct rest_request_msg *,
304 parse_rest_request_msg, remote_addr, request_msg, get_params, &response, debug_app);
305
306 if (!request || response.response_code != 200) {
307 SCOPE_CALL(-1, send_rest_response, ari_ws_session,
308 remote_addr, app_name, request, &response, debug_app);
309 SCOPE_EXIT_RTN_VALUE(0, "%s: Done with message\n", remote_addr);
310 }
311
312 /*
313 * We don't actually use the headers in the response
314 * but we have to allocate it because ast_ari_invoke
315 * and the resource handlers expect it.
316 */
317 response.headers = ast_str_create(80);
318 if (!response.headers) {
319 /* If we can't allocate a string, we can't respond to the client either. */
320 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Failed allocate headers string\n",
321 remote_addr);
322 }
323
324 SCOPE_CALL(-1, ast_ari_invoke, NULL, ARI_INVOKE_SOURCE_WEBSOCKET,
325 NULL, request->uri, request->method, request->query_strings,
326 upgrade_headers, request->body, &response);
327
328 ast_free(response.headers);
329
330 if (response.no_response) {
331 SCOPE_EXIT_RTN_VALUE(0, "No response needed\n");
332 }
333
334 SCOPE_CALL(-1, send_rest_response, ari_ws_session,
335 remote_addr, app_name, request, &response, debug_app);
336
337 SCOPE_EXIT_RTN_VALUE(0, "%s: Done with message\n", remote_addr);
338 }
339