/src/fluent-bit/plugins/out_websocket/websocket_conf.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* Fluent Bit |
4 | | * ========== |
5 | | * Copyright (C) 2015-2024 The Fluent Bit Authors |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <fluent-bit/flb_info.h> |
21 | | #include <fluent-bit/flb_output.h> |
22 | | #include <fluent-bit/flb_utils.h> |
23 | | #include <fluent-bit/flb_pack.h> |
24 | | #include <fluent-bit/flb_sds.h> |
25 | | |
26 | | #include "websocket.h" |
27 | | #include "websocket_conf.h" |
28 | | |
29 | | struct flb_out_ws *flb_ws_conf_create(struct flb_output_instance *ins, |
30 | | struct flb_config *config) |
31 | 0 | { |
32 | 0 | int ret; |
33 | 0 | int ulen; |
34 | 0 | int io_flags = 0; |
35 | 0 | char *uri = NULL; |
36 | 0 | char *tmp_uri = NULL; |
37 | 0 | const char *tmp; |
38 | 0 | int idle_interval; |
39 | 0 | struct flb_upstream *upstream; |
40 | 0 | struct flb_out_ws *ctx = NULL; |
41 | | |
42 | | /* Allocate plugin context */ |
43 | 0 | ctx = flb_calloc(1, sizeof(struct flb_out_ws)); |
44 | 0 | if (!ctx) { |
45 | 0 | flb_errno(); |
46 | 0 | return NULL; |
47 | 0 | } |
48 | 0 | ctx->ins = ins; |
49 | |
|
50 | 0 | ret = flb_output_config_map_set(ins, (void *) ctx); |
51 | 0 | if (ret == -1) { |
52 | 0 | flb_free(ctx); |
53 | 0 | return NULL; |
54 | 0 | } |
55 | | |
56 | 0 | flb_output_net_default("127.0.0.1", 80, ins); |
57 | | |
58 | | /* Check if SSL/TLS is enabled */ |
59 | 0 | #ifdef FLB_HAVE_TLS |
60 | 0 | if (ins->use_tls == FLB_TRUE) { |
61 | 0 | io_flags = FLB_IO_TLS; |
62 | 0 | } |
63 | 0 | else { |
64 | 0 | io_flags = FLB_IO_TCP; |
65 | 0 | } |
66 | | #else |
67 | | io_flags = FLB_IO_TCP; |
68 | | #endif |
69 | |
|
70 | 0 | upstream = flb_upstream_create(config, ins->host.name, ins->host.port, io_flags, ins->tls); |
71 | 0 | if (!upstream) { |
72 | 0 | flb_free(ctx); |
73 | 0 | return NULL; |
74 | 0 | } |
75 | | |
76 | | /* Output format */ |
77 | 0 | ctx->out_format = FLB_PACK_JSON_FORMAT_NONE; |
78 | 0 | tmp = flb_output_get_property("format", ins); |
79 | 0 | if (tmp) { |
80 | 0 | ret = flb_pack_to_json_format_type(tmp); |
81 | 0 | if (ret == -1) { |
82 | 0 | flb_error("[out_ws] unrecognized 'format' option '%s'. Using 'msgpack'", tmp); |
83 | 0 | } |
84 | 0 | else { |
85 | 0 | ctx->out_format = ret; |
86 | 0 | } |
87 | 0 | } |
88 | | |
89 | | /* Date format for JSON output */ |
90 | 0 | ctx->json_date_format = FLB_PACK_JSON_DATE_DOUBLE; |
91 | 0 | tmp = flb_output_get_property("json_date_format", ins); |
92 | 0 | if (tmp) { |
93 | 0 | ret = flb_pack_to_json_date_type(tmp); |
94 | 0 | if (ret == -1) { |
95 | 0 | flb_error("[out_ws] unrecognized 'json_date_format' option '%s'. Using 'double'", tmp); |
96 | 0 | } |
97 | 0 | else { |
98 | 0 | ctx->json_date_format = ret; |
99 | 0 | } |
100 | 0 | } |
101 | |
|
102 | 0 | if (ins->host.uri) { |
103 | 0 | uri = flb_strdup(ins->host.uri->full); |
104 | 0 | } |
105 | 0 | else { |
106 | 0 | tmp = flb_output_get_property("uri", ins); |
107 | 0 | if (tmp) { |
108 | 0 | uri = flb_strdup(tmp); |
109 | 0 | } |
110 | 0 | } |
111 | |
|
112 | 0 | if (!uri) { |
113 | 0 | uri = flb_strdup("/"); |
114 | 0 | } |
115 | 0 | else if (uri[0] != '/') { |
116 | 0 | ulen = strlen(uri); |
117 | 0 | tmp_uri = flb_malloc(ulen + 2); |
118 | 0 | tmp_uri[0] = '/'; |
119 | 0 | memcpy(tmp_uri + 1, uri, ulen); |
120 | 0 | tmp_uri[ulen + 1] = '\0'; |
121 | 0 | flb_free(uri); |
122 | 0 | uri = tmp_uri; |
123 | 0 | } |
124 | |
|
125 | 0 | idle_interval = ins->net_setup.keepalive_idle_timeout; |
126 | 0 | if (idle_interval > 5) { |
127 | 0 | ctx->idle_interval = idle_interval - 5; |
128 | 0 | } else if (idle_interval <= 2) { |
129 | 0 | flb_error("[out_ws] the keepalive timeout value is smaller than 2, which is meaningless! Please set it higher than 10 seconds. Current value will bring disorder for websocket plugin."); |
130 | 0 | ctx->idle_interval = idle_interval; |
131 | 0 | } else { |
132 | 0 | ctx->idle_interval = idle_interval - 2; |
133 | 0 | } |
134 | |
|
135 | 0 | ctx->u = upstream; |
136 | 0 | ctx->uri = uri; |
137 | 0 | ctx->host = ins->host.name; |
138 | 0 | ctx->port = ins->host.port; |
139 | |
|
140 | 0 | flb_output_upstream_set(ctx->u, ins); |
141 | | |
142 | 0 | flb_info("[out_ws] we have following parameter %s, %s, %d, %d", ctx->uri, ctx->host, ctx->port, ctx->idle_interval); |
143 | 0 | return ctx; |
144 | 0 | } |
145 | | |
146 | | void flb_ws_conf_destroy(struct flb_out_ws *ctx) |
147 | 0 | { |
148 | 0 | flb_info("[out_ws] flb_ws_conf_destroy "); |
149 | 0 | if (!ctx) { |
150 | 0 | return; |
151 | 0 | } |
152 | | |
153 | 0 | if (ctx->u) { |
154 | 0 | flb_upstream_destroy(ctx->u); |
155 | 0 | } |
156 | |
|
157 | 0 | flb_free(ctx->uri); |
158 | 0 | flb_free(ctx); |
159 | 0 | } |