From 91a9406c1ed0076d1c10060c6f110df63ad90523 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Th=C3=B6mmes?= Date: Thu, 29 Feb 2024 12:01:03 +0100 Subject: [PATCH] out_websocket: Add ability to provide additional headers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds the ability to add additional headers to the websocket handshake, identical to how we specify headers in out_http. This allows users of the websocket output to handshake with an authenticated endpoint for example. Signed-off-by: Markus Thömmes --- plugins/out_websocket/websocket.c | 19 +++++++++++++++++++ plugins/out_websocket/websocket.h | 3 +++ 2 files changed, 22 insertions(+) diff --git a/plugins/out_websocket/websocket.c b/plugins/out_websocket/websocket.c index 34d6d41a079..130d5458068 100644 --- a/plugins/out_websocket/websocket.c +++ b/plugins/out_websocket/websocket.c @@ -43,6 +43,10 @@ static int flb_ws_handshake(struct flb_connection *u_conn, int ret; size_t bytes_sent; struct flb_http_client *c; + struct mk_list *head; + struct flb_config_map_val *mv; + struct flb_slist_entry *key = NULL; + struct flb_slist_entry *val = NULL; if (!u_conn) { flb_error("[output_ws] upstream connection error"); @@ -63,6 +67,16 @@ static int flb_ws_handshake(struct flb_connection *u_conn, flb_http_add_header(c, "Sec-WebSocket-Key", 17, "dGhlIHNhbXBsZSBub25jZQ==", 24); flb_http_add_header(c, "Sec-WebSocket-Version", 21, "13", 2); + /* Append additional headers from configuration */ + flb_config_map_foreach(head, mv, ctx->headers) { + key = mk_list_entry_first(mv->val.list, struct flb_slist_entry, _head); + val = mk_list_entry_last(mv->val.list, struct flb_slist_entry, _head); + + flb_http_add_header(c, + key->str, flb_sds_len(key->str), + val->str, flb_sds_len(val->str)); + } + /* Perform request*/ ret = flb_http_do(c, &bytes_sent); @@ -315,6 +329,11 @@ static struct flb_config_map config_map[] = { 0, FLB_TRUE, offsetof(struct flb_out_ws, json_date_key), "Specify the name of the date field in output" }, + { + FLB_CONFIG_MAP_SLIST_1, "header", NULL, + FLB_CONFIG_MAP_MULT, FLB_TRUE, offsetof(struct flb_out_ws, headers), + "Add a HTTP header key/value pair to the initial HTTP request. Multiple headers can be set" + }, /* EOF */ {0} }; diff --git a/plugins/out_websocket/websocket.h b/plugins/out_websocket/websocket.h index f215b0f2c85..7769c7ae30f 100644 --- a/plugins/out_websocket/websocket.h +++ b/plugins/out_websocket/websocket.h @@ -47,6 +47,9 @@ struct flb_out_ws { time_t last_input_timestamp; int idle_interval; + /* Arbitrary HTTP headers */ + struct mk_list *headers; + /* Plugin instance */ struct flb_output_instance *ins; };