diff --git a/daemon/websocket.c b/daemon/websocket.c index f413dd4ba..472ce1c1c 100644 --- a/daemon/websocket.c +++ b/daemon/websocket.c @@ -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); @@ -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); @@ -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; diff --git a/docs/http_websocket_support.md b/docs/http_websocket_support.md index e08ccc459..6e007b166 100644 --- a/docs/http_websocket_support.md +++ b/docs/http_websocket_support.md @@ -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 diff --git a/include/websocket.h b/include/websocket.h index 899e8a609..0d7572310 100644 --- a/include/websocket.h +++ b/include/websocket.h @@ -27,6 +27,7 @@ struct websocket_message { CT_UNKNOWN = 0, CT_JSON, CT_NG, + CT_TEXT, } content_type; GString *body;