Skip to content

Commit 9bfd593

Browse files
lecarosedsiper
authored andcommitted
in_splunk: fix the response when receiving empty payload
Signed-off-by: lecaros <[email protected]>
1 parent 08273f2 commit 9bfd593

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

plugins/in_splunk/splunk_prot.c

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ static int send_json_message_response(struct splunk_conn *conn, int http_status,
134134
"Content-Length: %i\r\n\r\n%s",
135135
len, message);
136136
}
137+
else if (http_status == 400) {
138+
flb_sds_printf(&out,
139+
"HTTP/1.1 400 Bad Request\r\n"
140+
"Content-Type: application/json\r\n"
141+
"Content-Length: %i\r\n\r\n%s",
142+
len, message);
143+
}
137144

138145
/* We should check this operations result */
139146
flb_io_net_write(conn->connection,
@@ -593,7 +600,7 @@ static int process_hec_payload(struct flb_splunk *ctx, struct splunk_conn *conn,
593600
}
594601

595602
if (request->data.len <= 0 && !mk_http_parser_is_content_chunked(&session->parser)) {
596-
send_response(conn, 400, "error: no payload found\n");
603+
send_json_message_response(conn, 400, "{\"text\":\"No data\",\"code\":5}");
597604
return -2;
598605
}
599606

@@ -650,7 +657,7 @@ static int process_hec_raw_payload(struct flb_splunk *ctx, struct splunk_conn *c
650657
header = &session->parser.headers[MK_HEADER_CONTENT_TYPE];
651658
if (header->key.data == NULL) {
652659
send_response(conn, 400, "error: header 'Content-Type' is not set\n");
653-
return -1;
660+
return -2;
654661
}
655662
else if (header->val.len != 10 ||
656663
strncasecmp(header->val.data, "text/plain", 10) != 0) {
@@ -659,8 +666,8 @@ static int process_hec_raw_payload(struct flb_splunk *ctx, struct splunk_conn *c
659666
}
660667

661668
if (request->data.len <= 0 && !mk_http_parser_is_content_chunked(&session->parser)) {
662-
send_response(conn, 400, "2 error: no payload found\n");
663-
return -1;
669+
send_json_message_response(conn, 400, "{\"text\":\"No data\",\"code\":5}");
670+
return -2;
664671
}
665672

666673
header_auth = &session->parser.headers[MK_HEADER_AUTHORIZATION];
@@ -868,6 +875,18 @@ int splunk_prot_handle(struct flb_splunk *ctx, struct splunk_conn *conn,
868875
strcasecmp(uri, "/services/collector/raw") == 0) {
869876
ret = process_hec_raw_payload(ctx, conn, tag, session, request);
870877

878+
if (ret == -2) {
879+
/* Response already sent, skip further response */
880+
flb_sds_destroy(tag);
881+
mk_mem_free(uri);
882+
if (out_chunked) {
883+
mk_mem_free(out_chunked);
884+
}
885+
request->data.data = original_data;
886+
request->data.len = original_data_size;
887+
return -1;
888+
}
889+
871890
if (!ret) {
872891
send_json_message_response(conn, 400, "{\"text\":\"Invalid data format\",\"code\":6}");
873892
}
@@ -1096,9 +1115,9 @@ static int process_hec_payload_ng(struct flb_http_request *request,
10961115
}
10971116

10981117
if (request->body == NULL || cfl_sds_len(request->body) <= 0) {
1099-
send_response_ng(response, 400, "error: no payload found\n");
1118+
send_json_message_response_ng(response, 400, "{\"text\":\"No data\",\"code\":5}");
11001119

1101-
return -1;
1120+
return -2;
11021121
}
11031122

11041123
return handle_hec_payload(ctx, type, tag, request->body, cfl_sds_len(request->body));
@@ -1132,9 +1151,9 @@ static int process_hec_raw_payload_ng(struct flb_http_request *request,
11321151
}
11331152

11341153
if (request->body == NULL || cfl_sds_len(request->body) == 0) {
1135-
send_response_ng(response, 400, "error: no payload found\n");
1154+
send_json_message_response_ng(response, 400, "{\"text\":\"No data\",\"code\":5}");
11361155

1137-
return -1;
1156+
return -2;
11381157
}
11391158

11401159
/* Always handle as raw type of payloads here */
@@ -1210,6 +1229,11 @@ int splunk_prot_handle_ng(struct flb_http_request *request,
12101229
if (strcasecmp(request->path, "/services/collector/raw/1.0") == 0 ||
12111230
strcasecmp(request->path, "/services/collector/raw") == 0) {
12121231
ret = process_hec_raw_payload_ng(request, response, tag, context);
1232+
if (ret == -2) {
1233+
/* Response already sent, skip further response */
1234+
flb_sds_destroy(tag);
1235+
return -1;
1236+
}
12131237
if (ret != 0) {
12141238
send_json_message_response_ng(response, 400, "{\"text\":\"Invalid data format\",\"code\":6}");
12151239
ret = -1;
@@ -1223,6 +1247,11 @@ int splunk_prot_handle_ng(struct flb_http_request *request,
12231247
strcasecmp(request->path, "/services/collector/event") == 0 ||
12241248
strcasecmp(request->path, "/services/collector") == 0) {
12251249
ret = process_hec_payload_ng(request, response, tag, context);
1250+
if (ret == -2) {
1251+
/* Response already sent, skip further response */
1252+
flb_sds_destroy(tag);
1253+
return -1;
1254+
}
12261255
if (ret != 0) {
12271256
send_json_message_response_ng(response, 400, "{\"text\":\"Invalid data format\",\"code\":6}");
12281257
ret = -1;

0 commit comments

Comments
 (0)