Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

out_http: Add arbitrary HTTP headers #656

Merged
merged 1 commit into from Jun 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 67 additions & 0 deletions plugins/out_http/http.c
Expand Up @@ -23,6 +23,7 @@
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_str.h>
#include <fluent-bit/flb_time.h>
#include <fluent-bit/flb_utils.h>
#include <msgpack.h>

#include <stdio.h>
Expand Down Expand Up @@ -177,6 +178,12 @@ int cb_http_init(struct flb_output_instance *ins, struct flb_config *config,
struct flb_out_http_config *ctx = NULL;
(void)data;

struct mk_list *head;
struct mk_list *split;
struct flb_split_entry *sentry;
struct flb_config_prop *prop;
struct out_http_header *header;

/* Allocate plugin context */
ctx = flb_calloc(1, sizeof(struct flb_out_http_config));
if (!ctx) {
Expand Down Expand Up @@ -365,6 +372,40 @@ int cb_http_init(struct flb_output_instance *ins, struct flb_config *config,
ctx->host = ins->host.name;
ctx->port = ins->host.port;

/* Arbitrary HTTP headers to add */

ctx->headers_cnt = 0;
mk_list_init(&ctx->headers);

mk_list_foreach(head, &ins->properties) {
prop = mk_list_entry(head, struct flb_config_prop, _head);
split = flb_utils_split(prop->val, ' ', 2);

if (strcasecmp(prop->key, "header") == 0) {

header = flb_malloc(sizeof(struct out_http_header));
if (!header) {
flb_error
("[out_http] Unable to allocate memory for header");
flb_free(header);
return -1;
}

sentry = mk_list_entry_first(split, struct flb_split_entry, _head);
header->key_len = strlen(sentry->value);
header->key = flb_strndup(sentry->value, header->key_len);

sentry = mk_list_entry_last(split, struct flb_split_entry, _head);
header->val_len = strlen(sentry->value);
header->val = flb_strndup(sentry->value, header->val_len);

mk_list_add(&header->_head, &ctx->headers);
ctx->headers_cnt++;
}
}

flb_utils_split_free(split);

/* Set the plugin context */
flb_output_set_context(ins, ctx);
return 0;
Expand All @@ -387,6 +428,10 @@ void cb_http_flush(void *data, size_t bytes,
uint64_t body_len;
(void)i_ins;

struct mk_list *tmp;
struct mk_list *head;
struct out_http_header *header;

if ((ctx->out_format == FLB_HTTP_OUT_JSON) ||
(ctx->out_format == FLB_HTTP_OUT_JSON_STREAM) ||
(ctx->out_format == FLB_HTTP_OUT_JSON_LINES)) {
Expand Down Expand Up @@ -444,6 +489,15 @@ void cb_http_flush(void *data, size_t bytes,
flb_http_basic_auth(c, ctx->http_user, ctx->http_passwd);
}

mk_list_foreach_safe(head, tmp, &ctx->headers) {
header = mk_list_entry(head, struct out_http_header, _head);
flb_http_add_header(c,
header->key,
header->key_len,
header->val,
header->val_len);
}

ret = flb_http_do(c, &b_sent);
if (ret == 0) {
/*
Expand Down Expand Up @@ -499,6 +553,10 @@ int cb_http_exit(void *data, struct flb_config *config)
{
struct flb_out_http_config *ctx = data;

struct mk_list *tmp;
struct mk_list *head;
struct out_http_header *header;

if (ctx->u) {
flb_upstream_destroy(ctx->u);
}
Expand All @@ -509,6 +567,15 @@ int cb_http_exit(void *data, struct flb_config *config)
flb_free(ctx->uri);
flb_free(ctx->json_date_key);
flb_free(ctx->header_tag);

mk_list_foreach_safe(head, tmp, &ctx->headers) {
header = mk_list_entry(head, struct out_http_header, _head);
flb_free(header->key);
flb_free(header->val);
mk_list_del(&header->_head);
flb_free(header);
}

flb_free(ctx);

return 0;
Expand Down
12 changes: 12 additions & 0 deletions plugins/out_http/http.h
Expand Up @@ -61,6 +61,18 @@ struct flb_out_http_config {

/* Upstream connection to the backend server */
struct flb_upstream *u;

/* Arbitrary HTTP headers */
struct mk_list headers;
int headers_cnt;
};

struct out_http_header {
char *key;
int key_len;
char *val;
int val_len;
struct mk_list _head;
};

#endif