Coverage Report

Created: 2026-02-09 07:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fluent-bit/plugins/in_http/http.c
Line
Count
Source
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*  Fluent Bit
4
 *  ==========
5
 *  Copyright (C) 2015-2026 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
21
#include <fluent-bit/flb_input_plugin.h>
22
#include <fluent-bit/flb_network.h>
23
#include <fluent-bit/flb_config.h>
24
25
#include "http.h"
26
#include "http_conn.h"
27
#include "http_prot.h"
28
#include "http_config.h"
29
30
/*
31
 * For a server event, the collection event means a new client have arrived, we
32
 * accept the connection and create a new TCP instance which will wait for
33
 * JSON map messages.
34
 */
35
static int in_http_collect(struct flb_input_instance *ins,
36
                           struct flb_config *config, void *in_context)
37
0
{
38
0
    struct flb_connection *connection;
39
0
    struct http_conn      *conn;
40
0
    struct flb_http       *ctx;
41
42
0
    ctx = in_context;
43
44
0
    connection = flb_downstream_conn_get(ctx->downstream);
45
46
0
    if (connection == NULL) {
47
0
        flb_plg_error(ctx->ins, "could not accept new connection");
48
49
0
        return -1;
50
0
    }
51
52
0
    flb_plg_trace(ctx->ins, "new TCP connection arrived FD=%i",
53
0
                  connection->fd);
54
55
0
    conn = http_conn_add(connection, ctx);
56
57
0
    if (conn == NULL) {
58
0
        flb_downstream_conn_release(connection);
59
60
0
        return -1;
61
0
    }
62
63
0
    return 0;
64
0
}
65
66
static int in_http_init(struct flb_input_instance *ins,
67
                        struct flb_config *config, void *data)
68
0
{
69
0
    unsigned short int  port;
70
0
    int                 ret;
71
0
    struct flb_http    *ctx;
72
73
0
    (void) data;
74
75
    /* Create context and basic conf */
76
0
    ctx = http_config_create(ins);
77
0
    if (!ctx) {
78
0
        return -1;
79
0
    }
80
81
0
    ctx->collector_id = -1;
82
83
    /* Populate context with config map defaults and incoming properties */
84
0
    ret = flb_input_config_map_set(ins, (void *) ctx);
85
0
    if (ret == -1) {
86
0
        flb_plg_error(ctx->ins, "configuration error");
87
0
        http_config_destroy(ctx);
88
0
        return -1;
89
0
    }
90
91
0
    if (ctx->oauth2_cfg.validate) {
92
0
        if (!ctx->oauth2_cfg.issuer || !ctx->oauth2_cfg.jwks_url) {
93
0
            flb_plg_error(ctx->ins, "oauth2.issuer and oauth2.jwks_url are required when oauth2.validate is enabled");
94
0
            http_config_destroy(ctx);
95
0
            return -1;
96
0
        }
97
98
0
        if (ctx->oauth2_cfg.jwks_refresh_interval <= 0) {
99
0
            ctx->oauth2_cfg.jwks_refresh_interval = 300;
100
0
        }
101
102
0
        ctx->oauth2_ctx = flb_oauth2_jwt_context_create(config, &ctx->oauth2_cfg);
103
0
        if (!ctx->oauth2_ctx) {
104
0
            flb_plg_error(ctx->ins, "unable to create oauth2 jwt context");
105
0
            http_config_destroy(ctx);
106
0
            return -1;
107
0
        }
108
0
    }
109
110
    /* Set the context */
111
0
    flb_input_set_context(ins, ctx);
112
113
0
    port = (unsigned short int) strtoul(ctx->tcp_port, NULL, 10);
114
115
0
    if (ctx->enable_http2) {
116
0
        ret = flb_http_server_init(&ctx->http_server,
117
0
                                    HTTP_PROTOCOL_VERSION_AUTODETECT,
118
0
                                    (FLB_HTTP_SERVER_FLAG_KEEPALIVE | FLB_HTTP_SERVER_FLAG_AUTO_INFLATE),
119
0
                                    NULL,
120
0
                                    ins->host.listen,
121
0
                                    ins->host.port,
122
0
                                    ins->tls,
123
0
                                    ins->flags,
124
0
                                    &ins->net_setup,
125
0
                                    flb_input_event_loop_get(ins),
126
0
                                    ins->config,
127
0
                                    (void *) ctx);
128
129
0
        if (ret != 0) {
130
0
            flb_plg_error(ctx->ins,
131
0
                          "could not initialize http server on %s:%u. Aborting",
132
0
                          ins->host.listen, ins->host.port);
133
134
0
            http_config_destroy(ctx);
135
136
0
            return -1;
137
0
        }
138
139
0
        ret = flb_http_server_start(&ctx->http_server);
140
141
0
        if (ret != 0) {
142
0
            flb_plg_error(ctx->ins,
143
0
                          "could not start http server on %s:%u. Aborting",
144
0
                          ins->host.listen, ins->host.port);
145
146
0
            http_config_destroy(ctx);
147
148
0
            return -1;
149
0
        }
150
151
0
        flb_http_server_set_buffer_max_size(&ctx->http_server, ctx->buffer_max_size);
152
153
0
        ctx->http_server.request_callback = http_prot_handle_ng;
154
155
0
        flb_input_downstream_set(ctx->http_server.downstream, ctx->ins);
156
0
    }
157
0
    else {
158
0
        ctx->downstream = flb_downstream_create(FLB_TRANSPORT_TCP,
159
0
                                                ins->flags,
160
0
                                                ctx->listen,
161
0
                                                port,
162
0
                                                ins->tls,
163
0
                                                config,
164
0
                                                &ins->net_setup);
165
166
0
        if (ctx->downstream == NULL) {
167
0
            flb_plg_error(ctx->ins,
168
0
                        "could not initialize downstream on %s:%s. Aborting",
169
0
                        ctx->listen, ctx->tcp_port);
170
171
0
            http_config_destroy(ctx);
172
173
0
            return -1;
174
0
        }
175
176
0
        flb_input_downstream_set(ctx->downstream, ctx->ins);
177
0
    }
178
179
0
    if (ctx->successful_response_code != 200 &&
180
0
        ctx->successful_response_code != 201 &&
181
0
        ctx->successful_response_code != 204) {
182
0
        flb_plg_error(ctx->ins, "%d is not supported response code. Use default 201",
183
0
                      ctx->successful_response_code);
184
0
        ctx->successful_response_code = 201;
185
0
    }
186
187
0
    if (!ctx->enable_http2) {
188
        /* Collect upon data available on the standard input */
189
0
        ret = flb_input_set_collector_socket(ins,
190
0
                                            in_http_collect,
191
0
                                            ctx->downstream->server_fd,
192
0
                                            config);
193
0
        if (ret == -1) {
194
0
            flb_plg_error(ctx->ins, "Could not set collector for IN_TCP input plugin");
195
0
            http_config_destroy(ctx);
196
197
0
            return -1;
198
0
        }
199
200
0
        ctx->collector_id = ret;
201
0
    }
202
203
0
    return 0;
204
0
}
205
206
static int in_http_exit(void *data, struct flb_config *config)
207
0
{
208
0
    struct flb_http *ctx;
209
210
0
    (void) config;
211
212
0
    ctx = data;
213
214
0
    if (ctx != NULL) {
215
0
        http_config_destroy(ctx);
216
0
    }
217
218
0
    return 0;
219
0
}
220
221
/* Configuration properties map */
222
static struct flb_config_map config_map[] = {
223
    {
224
     FLB_CONFIG_MAP_BOOL, "http2", "true",
225
     0, FLB_TRUE, offsetof(struct flb_http, enable_http2),
226
     "Enable HTTP/2 support."
227
    },
228
229
    {
230
     FLB_CONFIG_MAP_BOOL, "add_remote_addr", "false",
231
     0, FLB_TRUE, offsetof(struct flb_http, add_remote_addr),
232
     "Adds REMOTE_ADDR field to the record. The value of REMOTE_ADDR is the client's address."
233
    },
234
235
    {
236
     FLB_CONFIG_MAP_STR, "remote_addr_key", REMOTE_ADDR_KEY,
237
     0, FLB_TRUE, offsetof(struct flb_http, remote_addr_key),
238
     "Key name for the remote address field added to the record."
239
    },
240
241
    {
242
     FLB_CONFIG_MAP_SIZE, "buffer_max_size", HTTP_BUFFER_MAX_SIZE,
243
     0, FLB_TRUE, offsetof(struct flb_http, buffer_max_size),
244
     "Set the maximum buffer size to store incoming data."
245
    },
246
247
    {
248
     FLB_CONFIG_MAP_SIZE, "buffer_chunk_size", HTTP_BUFFER_CHUNK_SIZE,
249
     0, FLB_TRUE, offsetof(struct flb_http, buffer_chunk_size),
250
     "Set the initial buffer size to store incoming data."
251
    },
252
253
    {
254
     FLB_CONFIG_MAP_SLIST_1, "success_header", NULL,
255
     FLB_CONFIG_MAP_MULT, FLB_TRUE, offsetof(struct flb_http, success_headers),
256
     "Add an HTTP header key/value pair on success. Multiple headers can be set."
257
    },
258
259
    {
260
     FLB_CONFIG_MAP_STR, "tag_key", NULL,
261
     0, FLB_TRUE, offsetof(struct flb_http, tag_key),
262
     "Specify a key name for extracting the tag from incoming request data."
263
    },
264
265
    {
266
     FLB_CONFIG_MAP_INT, "successful_response_code", "201",
267
     0, FLB_TRUE, offsetof(struct flb_http, successful_response_code),
268
     "Set successful response code. 200, 201 and 204 are supported."
269
    },
270
271
    /* EOF */
272
    {0}
273
};
274
275
/* Plugin reference */
276
struct flb_input_plugin in_http_plugin = {
277
    .name         = "http",
278
    .description  = "HTTP",
279
    .cb_init      = in_http_init,
280
    .cb_pre_run   = NULL,
281
    .cb_collect   = in_http_collect,
282
    .cb_flush_buf = NULL,
283
    .cb_pause     = NULL,
284
    .cb_resume    = NULL,
285
    .cb_exit      = in_http_exit,
286
    .config_map   = config_map,
287
    .flags        = FLB_INPUT_NET_SERVER | FLB_IO_OPT_TLS
288
};