Skip to content

Commit b6fcaab

Browse files
authored
out_http: added PUT support (#10882)
--------- Signed-off-by: Nicholas Nezis <[email protected]>
1 parent a8d39bb commit b6fcaab

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

plugins/out_http/http.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ static void append_headers(struct flb_http_client *c,
109109
}
110110
}
111111

112-
static int http_post(struct flb_out_http *ctx,
113-
const void *body, size_t body_len,
114-
const char *tag, int tag_len,
115-
char **headers)
112+
static int http_request(struct flb_out_http *ctx,
113+
const void *body, size_t body_len,
114+
const char *tag, int tag_len,
115+
char **headers)
116116
{
117117
int ret = 0;
118118
int out_ret = FLB_OK;
@@ -173,7 +173,7 @@ static int http_post(struct flb_out_http *ctx,
173173

174174

175175
/* Create HTTP client context */
176-
c = flb_http_client(u_conn, FLB_HTTP_POST, ctx->uri,
176+
c = flb_http_client(u_conn, ctx->http_method, ctx->uri,
177177
payload_buf, payload_size,
178178
ctx->host, ctx->port,
179179
ctx->proxy, 0);
@@ -541,7 +541,7 @@ static char **extract_headers(msgpack_object *obj) {
541541
return NULL;
542542
}
543543

544-
static int post_all_requests(struct flb_out_http *ctx,
544+
static int send_all_requests(struct flb_out_http *ctx,
545545
const char *data, size_t size,
546546
flb_sds_t body_key,
547547
flb_sds_t headers_key,
@@ -610,8 +610,10 @@ static int post_all_requests(struct flb_out_http *ctx,
610610
}
611611

612612
if (body_found && headers_found) {
613-
flb_plg_trace(ctx->ins, "posting record %zu", record_count++);
614-
ret = http_post(ctx, body, body_size, event_chunk->tag,
613+
flb_plg_trace(ctx->ins, "sending record %zu via %s",
614+
record_count++,
615+
ctx->http_method == FLB_HTTP_POST ? "POST" : "PUT");
616+
ret = http_request(ctx, body, body_size, event_chunk->tag,
615617
flb_sds_len(event_chunk->tag), headers);
616618
}
617619
else {
@@ -643,11 +645,11 @@ static void cb_http_flush(struct flb_event_chunk *event_chunk,
643645
(void) i_ins;
644646

645647
if (ctx->body_key) {
646-
ret = post_all_requests(ctx, event_chunk->data, event_chunk->size,
648+
ret = send_all_requests(ctx, event_chunk->data, event_chunk->size,
647649
ctx->body_key, ctx->headers_key, event_chunk);
648650
if (ret < 0) {
649651
flb_plg_error(ctx->ins,
650-
"failed to post requests body key \"%s\"", ctx->body_key);
652+
"failed to send requests using body key \"%s\"", ctx->body_key);
651653
}
652654
}
653655
else {
@@ -661,15 +663,15 @@ static void cb_http_flush(struct flb_event_chunk *event_chunk,
661663
(ctx->out_format == FLB_PACK_JSON_FORMAT_STREAM) ||
662664
(ctx->out_format == FLB_PACK_JSON_FORMAT_LINES) ||
663665
(ctx->out_format == FLB_HTTP_OUT_GELF)) {
664-
ret = http_post(ctx, out_body, out_size,
665-
event_chunk->tag, flb_sds_len(event_chunk->tag), NULL);
666+
ret = http_request(ctx, out_body, out_size,
667+
event_chunk->tag, flb_sds_len(event_chunk->tag), NULL);
666668
flb_sds_destroy(out_body);
667669
}
668670
else {
669671
/* msgpack */
670-
ret = http_post(ctx,
671-
event_chunk->data, event_chunk->size,
672-
event_chunk->tag, flb_sds_len(event_chunk->tag), NULL);
672+
ret = http_request(ctx,
673+
event_chunk->data, event_chunk->size,
674+
event_chunk->tag, flb_sds_len(event_chunk->tag), NULL);
673675
}
674676
}
675677

@@ -771,6 +773,11 @@ static struct flb_config_map config_map[] = {
771773
0, FLB_TRUE, offsetof(struct flb_out_http, uri),
772774
"Specify an optional HTTP URI for the target web server, e.g: /something"
773775
},
776+
{
777+
FLB_CONFIG_MAP_STR, "http_method", "POST",
778+
0, FLB_FALSE, 0,
779+
"Specify the HTTP method to use. Supported methods are POST and PUT"
780+
},
774781

775782
/* Gelf Properties */
776783
{

plugins/out_http/http.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ struct flb_out_http {
6767
char *host;
6868
int port;
6969

70+
/* HTTP method */
71+
int http_method;
72+
7073
/* GELF fields */
7174
struct flb_gelf_fields gelf_fields;
7275

plugins/out_http/http_conf.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <fluent-bit/flb_pack.h>
2323
#include <fluent-bit/flb_sds.h>
2424
#include <fluent-bit/flb_kv.h>
25+
#include <fluent-bit/flb_http_client.h>
2526
#include <fluent-bit/flb_record_accessor.h>
2627

2728
#ifdef FLB_HAVE_SIGNV4
@@ -272,6 +273,23 @@ struct flb_out_http *flb_http_conf_create(struct flb_output_instance *ins,
272273
}
273274
}
274275

276+
/* HTTP method */
277+
ctx->http_method = FLB_HTTP_POST;
278+
tmp = flb_output_get_property("http_method", ins);
279+
if (tmp) {
280+
if (strcasecmp(tmp, "POST") == 0) {
281+
ctx->http_method = FLB_HTTP_POST;
282+
}
283+
else if (strcasecmp(tmp, "PUT") == 0) {
284+
ctx->http_method = FLB_HTTP_PUT;
285+
}
286+
else {
287+
flb_plg_error(ctx->ins, "invalid http_method option '%s'. Supported methods are POST and PUT", tmp);
288+
flb_free(ctx);
289+
return NULL;
290+
}
291+
}
292+
275293
ctx->u = upstream;
276294
ctx->uri = uri;
277295
ctx->host = ins->host.name;

0 commit comments

Comments
 (0)