Skip to content

Commit

Permalink
out_websocket: Add ability to provide additional headers
Browse files Browse the repository at this point in the history
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 <markusthoemmes@me.com>
  • Loading branch information
markusthoemmes committed Feb 29, 2024
1 parent 9d9ac68 commit 726cc95
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
19 changes: 19 additions & 0 deletions plugins/out_websocket/websocket.c
Expand Up @@ -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");
Expand All @@ -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);

Expand Down Expand Up @@ -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. Multiple headers can be set"
},
/* EOF */
{0}
};
Expand Down
3 changes: 3 additions & 0 deletions plugins/out_websocket/websocket.h
Expand Up @@ -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;
};
Expand Down

0 comments on commit 726cc95

Please sign in to comment.