Skip to content

Commit 69d148e

Browse files
cosmo0920edsiper
authored andcommitted
lib: Implement injecting HTTP response mechanism
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent fef9cb6 commit 69d148e

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

include/fluent-bit/flb_lib.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ FLB_EXPORT int flb_output_set_test(flb_ctx_t *ctx, int ffd, char *test_name,
6868
void *test_ctx);
6969
FLB_EXPORT int flb_output_set_callback(flb_ctx_t *ctx, int ffd, char *name,
7070
void (*cb)(char *, void *, void *));
71+
FLB_EXPORT int flb_output_set_http_test(flb_ctx_t *ctx, int ffd, char *test_name,
72+
void (*out_response) (void *, int, int, void *, size_t, void *),
73+
void *out_callback_data);
7174

7275
FLB_EXPORT int flb_filter_set(flb_ctx_t *ctx, int ffd, ...);
7376
FLB_EXPORT int flb_service_set(flb_ctx_t *ctx, ...);
@@ -84,6 +87,9 @@ FLB_EXPORT int flb_loop(flb_ctx_t *ctx);
8487
FLB_EXPORT int flb_lib_push(flb_ctx_t *ctx, int ffd, const void *data, size_t len);
8588
FLB_EXPORT int flb_lib_config_file(flb_ctx_t *ctx, const char *path);
8689

90+
/* Emulate ingestions of HTTP responses for output plugins */
91+
FLB_EXPORT int flb_lib_response(flb_ctx_t *ctx, int ffd, int status, const void *data, size_t len);
92+
8793
/* library context handling */
8894
FLB_EXPORT void flb_context_set(flb_ctx_t *ctx);
8995
FLB_EXPORT flb_ctx_t *flb_context_get();

src/flb_lib.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,37 @@ int flb_input_set_processor(flb_ctx_t *ctx, int ffd, struct flb_processor *proc)
347347
return 0;
348348
}
349349

350+
int flb_output_set_http_test(flb_ctx_t *ctx, int ffd, char *test_name,
351+
void (*out_response) (void *, int, int, void *, size_t, void *),
352+
void *out_callback_data)
353+
{
354+
struct flb_output_instance *o_ins;
355+
356+
o_ins = out_instance_get(ctx, ffd);
357+
if (!o_ins) {
358+
return -1;
359+
}
360+
361+
/*
362+
* Enabling a test, set the output instance in 'test' mode, so no real
363+
* flush callback is invoked, only the desired implemented test.
364+
*/
365+
366+
/* Response test */
367+
if (strcmp(test_name, "response") == 0) {
368+
o_ins->test_mode = FLB_TRUE;
369+
o_ins->test_response.rt_ctx = ctx;
370+
o_ins->test_response.rt_ffd = ffd;
371+
o_ins->test_response.rt_out_response = out_response;
372+
o_ins->test_response.rt_data = out_callback_data;
373+
}
374+
else {
375+
return -1;
376+
}
377+
378+
return 0;
379+
}
380+
350381
static inline int flb_config_map_property_check(char *plugin_name, struct mk_list *config_map, char *key, char *val)
351382
{
352383
struct flb_kv *kv;
@@ -638,6 +669,41 @@ int flb_lib_free(void* data)
638669
}
639670

640671

672+
static int flb_output_run_response(flb_ctx_t *ctx, struct flb_output_instance *o_ins,
673+
int status, const void *data, size_t len)
674+
{
675+
int ret;
676+
void *out_buf = NULL;
677+
size_t out_size = 0;
678+
struct flb_test_out_response *resp;
679+
680+
if (!o_ins) {
681+
return -1;
682+
}
683+
684+
resp = &o_ins->test_response;
685+
686+
/* Invoke the input plugin formatter test callback */
687+
ret = resp->callback(ctx->config,
688+
o_ins->context,
689+
status, data, len,
690+
&out_buf, &out_size);
691+
692+
/* Call the runtime test callback checker */
693+
if (resp->rt_out_response) {
694+
resp->rt_out_response(resp->rt_ctx,
695+
resp->rt_ffd,
696+
ret,
697+
out_buf, out_size,
698+
resp->rt_data);
699+
}
700+
else {
701+
flb_free(out_buf);
702+
}
703+
704+
return 0;
705+
}
706+
641707
/* Push some data into the Engine */
642708
int flb_lib_push(flb_ctx_t *ctx, int ffd, const void *data, size_t len)
643709
{
@@ -662,6 +728,29 @@ int flb_lib_push(flb_ctx_t *ctx, int ffd, const void *data, size_t len)
662728
return ret;
663729
}
664730

731+
/* Emulate some data from the response */
732+
int flb_lib_response(flb_ctx_t *ctx, int ffd, int status, const void *data, size_t len)
733+
{
734+
int ret;
735+
struct flb_output_instance *o_ins;
736+
737+
if (ctx->status == FLB_LIB_NONE || ctx->status == FLB_LIB_ERROR) {
738+
flb_error("[lib] cannot push data, engine is not running");
739+
return -1;
740+
}
741+
742+
o_ins = out_instance_get(ctx, ffd);
743+
if (!o_ins) {
744+
return -1;
745+
}
746+
747+
/* If input's test_formatter is registered, priorize to run it. */
748+
if (o_ins->test_response.callback != NULL) {
749+
ret = flb_output_run_response(ctx, o_ins, status, data, len);
750+
}
751+
return ret;
752+
}
753+
665754
static void flb_lib_worker(void *data)
666755
{
667756
int ret;

0 commit comments

Comments
 (0)