Skip to content

Commit

Permalink
MT#61630 support CLI commands via POST
Browse files Browse the repository at this point in the history
Change-Id: I2970b331f4889bed7eab11d33ab16751ef4246a5
  • Loading branch information
rfuchs committed Jan 20, 2025
1 parent 3e0bd2e commit 17c7fd5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
20 changes: 20 additions & 0 deletions daemon/websocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,22 @@ static const char *websocket_http_cli(struct websocket_message *wm) {
}


static const char *websocket_http_cli_post(struct websocket_message *wm) {
ilogs(http, LOG_DEBUG, "Respoding to POST /cli");

struct cli_writer cw = {
.cw_printf = websocket_queue_printf,
.ptr = wm->wc,
};
cli_handle(&STR_LEN(wm->body->str, wm->body->len), &cw);

size_t len = websocket_queue_len(wm->wc);

websocket_http_complete(wm->wc, 200, "text/plain", len, NULL);
return NULL;
}


static const char *websocket_cli_process(struct websocket_message *wm) {
ilogs(http, LOG_DEBUG, "Processing websocket CLI req '%s'", wm->body->str);

Expand Down Expand Up @@ -622,6 +638,8 @@ static int websocket_http_post(struct websocket_conn *wc) {
wm->content_type = CT_JSON;
else if (!strcasecmp(ct, "application/x-rtpengine-ng"))
wm->content_type = CT_NG;
else if (!strcasecmp(ct, "text/plain"))
wm->content_type = CT_TEXT;
else
ilogs(http, LOG_WARN, "Unsupported content-type '%s'", ct);

Expand Down Expand Up @@ -677,6 +695,8 @@ static int websocket_http_body(struct websocket_conn *wc, const char *body, size
handler = websocket_janus_process;
else if (!strncmp(uri, "/janus/", 7) && wm->method == M_POST && wm->content_type == CT_JSON)
handler = websocket_janus_post;
else if (!strcmp(uri, "/cli") && wm->method == M_POST && wm->content_type == CT_TEXT)
handler = websocket_http_cli_post;
else
handler = websocket_http_404;

Expand Down
12 changes: 7 additions & 5 deletions docs/http_websocket_support.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ are sent to it.
This interface supports the same commands as the CLI tool `rtpengine-ctl` that
comes packaged with `rtpengine`. For HTTP and HTTPS, the command is appended to
the URI base `/cli/` and the request is made via `GET`, with spaces replaced by
plus signs as required by HTTP (e.g. `GET /cli/list+totals`). For WebSockets,
the subprotocol is `cli.rtpengine.com` and each WebSocket message corresponds
to one CLI command and produces one message in response. The format of each
response is exactly the same as produced by the CLI tool `rtpengine-ctl` and
therefore meant for plain text representation.
plus signs as required by HTTP (e.g. `GET /cli/list+totals`), or alternatively,
the command is sent as request body if the request is made via `POST`, using a
content-type of `text/plain`. For WebSockets, the subprotocol is
`cli.rtpengine.com` and each WebSocket message corresponds to one CLI command
and produces one message in response. The format of each response is exactly
the same as produced by the CLI tool `rtpengine-ctl` and therefore meant for
plain text representation.

## *ng* Protocol Interface

Expand Down
1 change: 1 addition & 0 deletions include/websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct websocket_message {
CT_UNKNOWN = 0,
CT_JSON,
CT_NG,
CT_TEXT,
} content_type;
GString *body;

Expand Down

0 comments on commit 17c7fd5

Please sign in to comment.