forked from fluent/fluent-bit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflb_http_client.h
478 lines (385 loc) · 17.4 KB
/
flb_http_client.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2015-2024 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FLB_HTTP_CLIENT_H
#define FLB_HTTP_CLIENT_H
#include <fluent-bit/flb_io.h>
#include <fluent-bit/flb_lock.h>
#include <fluent-bit/flb_upstream.h>
#include <fluent-bit/flb_callback.h>
#include <fluent-bit/flb_http_common.h>
#include <fluent-bit/flb_http_client_http1.h>
#include <fluent-bit/flb_http_client_http2.h>
#define HTTP_CLIENT_TEMPORARY_BUFFER_SIZE (1024 * 64)
#define HTTP_CLIENT_SUCCESS 0
#define HTTP_CLIENT_PROVIDER_ERROR -1
#define FLB_HTTP_CLIENT_FLAG_KEEPALIVE (((uint64_t) 1) << 0)
#define FLB_HTTP_CLIENT_FLAG_AUTO_DEFLATE (((uint64_t) 1) << 1)
#define FLB_HTTP_CLIENT_FLAG_AUTO_INFLATE (((uint64_t) 1) << 2)
#define FLB_HTTP_CLIENT_FLAG_STREAM_BODY (((uint64_t) 1) << 3)
/* Buffer size */
#define FLB_HTTP_BUF_SIZE 2048
#define FLB_HTTP_DATA_SIZE_MAX 4096
#define FLB_HTTP_DATA_CHUNK 32768
/* HTTP Methods */
#define FLB_HTTP_GET 0
#define FLB_HTTP_POST 1
#define FLB_HTTP_PUT 2
#define FLB_HTTP_HEAD 3
#define FLB_HTTP_CONNECT 4
#define FLB_HTTP_PATCH 5
#define FLB_HTTP_DELETE 6
/* HTTP Flags */
#define FLB_HTTP_10 1
#define FLB_HTTP_11 2
#define FLB_HTTP_KA 16
/* Proxy */
#define FLB_HTTP_PROXY_NONE 0
#define FLB_HTTP_PROXY_HTTP 1
#define FLB_HTTP_PROXY_HTTPS 2
/* Internal codes */
#define FLB_HTTP_ERROR -1
#define FLB_HTTP_MORE 0
#define FLB_HTTP_OK 1
#define FLB_HTTP_NOT_FOUND 2 /* header not found */
#define FLB_HTTP_CHUNK_AVAILABLE 3 /* means chunk is available, but there is more data. end of all chunks returns FLB_HTTP_OK */
/* Useful headers */
#define FLB_HTTP_HEADER_AUTH "Authorization"
#define FLB_HTTP_HEADER_PROXY_AUTH "Proxy-Authorization"
#define FLB_HTTP_HEADER_CONTENT_TYPE "Content-Type"
#define FLB_HTTP_HEADER_CONTENT_ENCODING "Content-Encoding"
#define FLB_HTTP_HEADER_CONNECTION "Connection"
#define FLB_HTTP_HEADER_KA "keep-alive"
#define FLB_HTTP_CLIENT_HEADER_ARRAY 0
#define FLB_HTTP_CLIENT_HEADER_LIST 1
#define FLB_HTTP_CLIENT_HEADER_CONFIG_MAP_LIST 2
#define FLB_HTTP_CLIENT_ARGUMENT_TYPE_TERMINATOR 0
#define FLB_HTTP_CLIENT_ARGUMENT_TYPE_METHOD 1
#define FLB_HTTP_CLIENT_ARGUMENT_TYPE_HOST 2
#define FLB_HTTP_CLIENT_ARGUMENT_TYPE_URI 3
#define FLB_HTTP_CLIENT_ARGUMENT_TYPE_URL 4
#define FLB_HTTP_CLIENT_ARGUMENT_TYPE_USER_AGENT 5
#define FLB_HTTP_CLIENT_ARGUMENT_TYPE_CONTENT_TYPE 6
#define FLB_HTTP_CLIENT_ARGUMENT_TYPE_BODY 7
#define FLB_HTTP_CLIENT_ARGUMENT_TYPE_HEADERS 8
#define FLB_HTTP_CLIENT_ARGUMENT_TYPE_AUTH_BASIC 9
#define FLB_HTTP_CLIENT_ARGUMENT_TYPE_AUTH_BEARER_TOKEN 10
#define FLB_HTTP_CLIENT_ARGUMENT_TYPE_AUTH_SIGNV4 11
#ifdef FLB_SYSTEM_WINDOWS
#ifdef _WIN64
typedef size_t * flb_http_client_size_t;
typedef size_t * flb_http_client_int64_t;
typedef size_t * flb_http_client_uint64_t;
#else
typedef size_t * flb_http_client_size_t;
typedef int64_t flb_http_client_int64_t;
typedef uint64_t flb_http_client_uint64_t;
#endif
#else
typedef size_t flb_http_client_size_t;
typedef int64_t flb_http_client_int64_t;
typedef uint64_t flb_http_client_uint64_t;
#endif
struct flb_http_client_response {
int status; /* HTTP response status */
int content_length; /* Content length set by headers */
int chunked_encoding; /* Chunked transfer encoding ? */
int connection_close; /* connection: close ? */
char *chunk_processed_end; /* Position to mark last chunk */
char *headers_end; /* Headers end (\r\n\r\n) */
/* Payload: body response: reference to 'data' */
char *payload;
size_t payload_size;
/* Buffer to store server response */
char *data;
size_t data_len;
size_t data_size;
size_t data_size_max;
};
/* It hold information about a possible HTTP proxy set by the caller */
struct flb_http_proxy {
int type; /* One of FLB_HTTP_PROXY_ macros */
int port; /* TCP Port */
const char *host; /* Proxy Host */
};
/* HTTP Debug context */
struct flb_http_debug {
/* HTTP request headers */
int debug_request_headers; /* debug HTTP request headers */
void (*cb_debug_request_headers); /* callback to pass raw headers */
/* HTTP request payload */
int debug_request_payload; /* debug HTTP request payload */
int (*cb_debug_request_payload);
};
/* To make opaque struct */
struct flb_http_client;
/*
* Tests callbacks
* ===============
*/
struct flb_test_http_response {
/*
* Response Test Mode
* ====================
* When the response test enable the test response mode, it needs to
* keep a reference of the context and other information:
*
* - rt_ctx : flb_http_client context
*
* - rt_status : HTTP response code
*
* - rt_in_callback: intermediary function to receive the results of
* the http response test function.
*
* - rt_data: opaque data type for rt_in_callback()
*/
/* runtime library context */
void *rt_ctx;
/* HTTP status */
int rt_status;
/* optional response context */
void *response_ctx;
/*
* "response test callback": this function pointer is used by Fluent Bit
* http client testing mode to reference a test function that must retrieve the
* results of 'callback'. Consider this an intermediary function to
* transfer the results to the runtime test.
*
* This function is private and should not be set manually in the plugin
* code, it's set on src/flb_http_client.c .
*/
void (*rt_resp_callback) (void *, int, void *, size_t, void *);
/*
* opaque data type passed by the runtime library to be used on
* rt_in_callback().
*/
void *rt_data;
/*
* Callback
* =========
* "HTTP response callback": it references the plugin function that performs
* to validate HTTP response by HTTP client. This entry is mostly to
* expose the plugin local function.
*/
int (*callback) (/* plugin that ingested the records */
struct flb_http_client *,
const void *, /* incoming response data */
size_t, /* incoming response size */
void **, /* output buffer */
size_t *); /* output buffer size */
};
/* Set a request type */
struct flb_http_client {
/* Upstream connection */
struct flb_connection *u_conn;
/* Request data */
int method;
int flags;
int header_len;
int header_size;
char *header_buf;
/* Config */
int allow_dup_headers; /* allow duplicated headers */
/* incoming parameters */
const char *uri;
const char *query_string;
const char *host;
int port;
/* payload */
int body_len;
const char *body_buf;
struct mk_list headers;
/* Proxy */
struct flb_http_proxy proxy;
/* Response */
struct flb_http_client_response resp;
/* Tests */
int test_mode;
struct flb_test_http_response test_response;
/* Reference to Callback context */
void *cb_ctx;
};
struct flb_http_client_ng {
struct cfl_list sessions;
uint16_t port;
uint64_t flags;
int protocol_version;
cfl_sds_t temporary_buffer;
int releasable;
void *user_data;
struct flb_upstream *upstream;
struct flb_upstream_ha *upstream_ha;
flb_lock_t lock;
};
struct flb_http_client_session {
struct flb_http1_client_session http1;
struct flb_http2_client_session http2;
struct cfl_list streams;
int protocol_version;
cfl_sds_t incoming_data;
cfl_sds_t outgoing_data;
int releasable;
struct cfl_list response_queue;
int stream_sequence_number;
struct flb_upstream_node *upstream_node;
struct flb_connection *connection;
struct flb_http_client_ng *parent;
struct cfl_list _head;
};
struct flb_aws_provider;
int flb_http_client_ng_init(struct flb_http_client_ng *client,
struct flb_upstream_ha *upstream_ha,
struct flb_upstream *upstream,
int protocol_version,
uint64_t flags);
struct flb_http_client_ng *flb_http_client_ng_create(
struct flb_upstream_ha *upstream_ha,
struct flb_upstream *upstream,
int protocol_version,
uint64_t flags);
void flb_http_client_ng_destroy(struct flb_http_client_ng *client);
int flb_http_client_session_init(struct flb_http_client_session *session,
struct flb_http_client_ng *client,
int protocol_version,
struct flb_connection *connection);
struct flb_http_client_session *flb_http_client_session_create(
struct flb_http_client_ng *client,
int protocol_version,
struct flb_connection *connection);
struct flb_http_client_session *flb_http_client_session_begin(
struct flb_http_client_ng *client);
void flb_http_client_session_destroy(struct flb_http_client_session *session);
struct flb_http_request *flb_http_client_request_begin(
struct flb_http_client_session *session);
struct flb_http_response *flb_http_client_request_execute(
struct flb_http_request *request);
struct flb_http_response *flb_http_client_request_execute_step(
struct flb_http_request *request);
void flb_http_client_request_destroy(struct flb_http_request *request,
int destroy_session);
int flb_http_client_session_ingest(struct flb_http_client_session *session,
unsigned char *buffer,
size_t length);
void flb_http_client_debug(struct flb_http_client *c,
struct flb_callback *cb_ctx);
struct flb_http_client *flb_http_client(struct flb_connection *u_conn,
int method, const char *uri,
const char *body, size_t body_len,
const char *host, int port,
const char *proxy, int flags);
/* For fulfilling HTTP response testing (dummy client) */
struct flb_http_client *flb_http_dummy_client(struct flb_connection *u_conn,
int method, const char *uri,
const char *body, size_t body_len,
const char *host, int port,
const char *proxy, int flags);
int flb_http_add_header(struct flb_http_client *c,
const char *key, size_t key_len,
const char *val, size_t val_len);
flb_sds_t flb_http_get_header(struct flb_http_client *c,
const char *key, size_t key_len);
int flb_http_basic_auth(struct flb_http_client *c,
const char *user, const char *passwd);
int flb_http_proxy_auth(struct flb_http_client *c,
const char *user, const char *passwd);
int flb_http_bearer_auth(struct flb_http_client *c,
const char *token);
int flb_http_set_keepalive(struct flb_http_client *c);
int flb_http_set_content_encoding_gzip(struct flb_http_client *c);
int flb_http_set_callback_context(struct flb_http_client *c,
struct flb_callback *cb_ctx);
int flb_http_set_response_test(struct flb_http_client *c, char *test_name,
const void *data, size_t len,
int status,
void (*resp_callback) (void *, int, void *, size_t, void *),
void *resp_callback_data);
int flb_http_push_response(struct flb_http_client *c, const void *data, size_t len);
int flb_http_get_response_data(struct flb_http_client *c, size_t bytes_consumed);
int flb_http_do_request(struct flb_http_client *c, size_t *bytes);
int flb_http_do(struct flb_http_client *c, size_t *bytes);
int flb_http_client_proxy_connect(struct flb_connection *u_conn);
void flb_http_client_destroy(struct flb_http_client *c);
int flb_http_buffer_size(struct flb_http_client *c, size_t size);
size_t flb_http_buffer_available(struct flb_http_client *c);
int flb_http_buffer_increase(struct flb_http_client *c, size_t size,
size_t *out_size);
int flb_http_strip_port_from_host(struct flb_http_client *c);
int flb_http_allow_duplicated_headers(struct flb_http_client *c, int allow);
int flb_http_client_debug_property_is_valid(char *key, char *val);
#define FLB_HTTP_CLIENT_ARGUMENT(argument_type, ...) \
(flb_http_client_size_t) argument_type, __VA_ARGS__
#define FLB_HTTP_CLIENT_ARGUMENT_TERMINATOR() \
(flb_http_client_size_t) FLB_HTTP_CLIENT_ARGUMENT_TYPE_TERMINATOR
#define FLB_HTTP_CLIENT_ARGUMENT_METHOD(method) \
FLB_HTTP_CLIENT_ARGUMENT(FLB_HTTP_CLIENT_ARGUMENT_TYPE_METHOD, \
(flb_http_client_size_t) method)
#define FLB_HTTP_CLIENT_ARGUMENT_HOST(host) \
FLB_HTTP_CLIENT_ARGUMENT(FLB_HTTP_CLIENT_ARGUMENT_TYPE_HOST, \
host)
#define FLB_HTTP_CLIENT_ARGUMENT_URL(url) \
FLB_HTTP_CLIENT_ARGUMENT(FLB_HTTP_CLIENT_ARGUMENT_TYPE_URL, \
url)
#define FLB_HTTP_CLIENT_ARGUMENT_URI(uri) \
FLB_HTTP_CLIENT_ARGUMENT(FLB_HTTP_CLIENT_ARGUMENT_TYPE_URI, \
uri)
#define FLB_HTTP_CLIENT_ARGUMENT_USER_AGENT(user_agent) \
FLB_HTTP_CLIENT_ARGUMENT(FLB_HTTP_CLIENT_ARGUMENT_TYPE_USER_AGENT, \
user_agent)
#define FLB_HTTP_CLIENT_ARGUMENT_CONTENT_TYPE(content_type) \
FLB_HTTP_CLIENT_ARGUMENT(FLB_HTTP_CLIENT_ARGUMENT_TYPE_CONTENT_TYPE, \
content_type)
#define FLB_HTTP_CLIENT_ARGUMENT_BODY(buffer, length, compression_algorithm) \
FLB_HTTP_CLIENT_ARGUMENT(FLB_HTTP_CLIENT_ARGUMENT_TYPE_BODY, \
buffer, \
(flb_http_client_size_t) length, \
compression_algorithm)
#define FLB_HTTP_CLIENT_ARGUMENT_HEADERS(data_type, headers) \
FLB_HTTP_CLIENT_ARGUMENT(FLB_HTTP_CLIENT_ARGUMENT_TYPE_HEADERS, \
(flb_http_client_size_t) data_type, \
headers)
#define FLB_HTTP_CLIENT_ARGUMENT_BASIC_AUTHORIZATION(username, password) \
FLB_HTTP_CLIENT_ARGUMENT(FLB_HTTP_CLIENT_ARGUMENT_TYPE_AUTH_BASIC, \
username, \
password)
#define FLB_HTTP_CLIENT_ARGUMENT_BEARER_TOKEN(token) \
FLB_HTTP_CLIENT_ARGUMENT(FLB_HTTP_CLIENT_ARGUMENT_TYPE_AUTH_BEARER_TOKEN, \
token)
#define FLB_HTTP_CLIENT_ARGUMENT_SIGNV4(aws_region, aws_service, aws_provider) \
FLB_HTTP_CLIENT_ARGUMENT(FLB_HTTP_CLIENT_ARGUMENT_TYPE_AUTH_SIGNV4, \
aws_region, \
aws_service, \
aws_provider)
int flb_http_request_set_parameters_internal(
struct flb_http_request *request,
va_list arguments);
int flb_http_request_set_parameters_unsafe(
struct flb_http_request *request,
...);
struct flb_http_request *flb_http_client_request_builder_unsafe(
struct flb_http_client_ng *client,
...);
#define flb_http_client_request_builder(client, ...) \
flb_http_client_request_builder_unsafe( \
client, \
__VA_ARGS__, \
FLB_HTTP_CLIENT_ARGUMENT_TERMINATOR());
#define flb_http_request_set_parameters(request, ...) \
flb_http_request_set_parameters_unsafe( \
request, \
__VA_ARGS__, \
FLB_HTTP_CLIENT_ARGUMENT_TERMINATOR());
#endif