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

in_http: Content-Type rejected for application/json when encoding value is present. #9190

Merged
merged 3 commits into from
Aug 14, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions plugins/in_http/http_prot.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,9 @@ static int process_payload(struct flb_http *ctx, struct http_conn *conn,
return -1;
}

if (header->val.len == 16 &&
strncasecmp(header->val.data, "application/json", 16) == 0) {
if ((header->val.len == 16 && strncasecmp(header->val.data, "application/json", 16) == 0) ||
(header->val.len > 16 && (strncasecmp(header->val.data, "application/json ", 17) == 0) ||
strncasecmp(header->val.data, "application/json;", 17) == 0)) {
type = HTTP_CONTENT_JSON;
}

Expand Down
79 changes: 79 additions & 0 deletions tests/runtime/in_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "flb_tests_runtime.h"

#define JSON_CONTENT_TYPE "application/json"
#define JSON_CHARSET_CONTENT_TYPE "application/json; charset=utf-8"

struct http_client_ctx {
struct flb_upstream *u;
Expand Down Expand Up @@ -350,14 +351,92 @@ void flb_test_http_successful_response_code(char *response_code)
test_ctx_destroy(ctx);
}

void flb_test_http_json_charset_header(char *response_code)
{
struct flb_lib_out_cb cb_data;
struct test_ctx *ctx;
struct flb_http_client *c;
int ret;
int num;
size_t b_sent;

char *buf = "[{\"test\":\"msg\"}]";

clear_output_num();

cb_data.cb = cb_check_result_json;
cb_data.data = "\"test\":\"msg\"";

ctx = test_ctx_create(&cb_data);
if (!TEST_CHECK(ctx != NULL)) {
TEST_MSG("test_ctx_create failed");
exit(EXIT_FAILURE);
}

ret = flb_input_set(ctx->flb, ctx->i_ffd,
"http2", "off",
"successful_response_code", response_code,
NULL);
TEST_CHECK(ret == 0);

ret = flb_output_set(ctx->flb, ctx->o_ffd,
"match", "*",
"format", "json",
NULL);
TEST_CHECK(ret == 0);

/* Start the engine */
ret = flb_start(ctx->flb);
TEST_CHECK(ret == 0);

ctx->httpc = http_client_ctx_create();
TEST_CHECK(ctx->httpc != NULL);

flb_time_msleep(1500);

c = flb_http_client(ctx->httpc->u_conn, FLB_HTTP_POST, "/", buf, strlen(buf),
"127.0.0.1", 9880, NULL, 0);
ret = flb_http_add_header(c, FLB_HTTP_HEADER_CONTENT_TYPE, strlen(FLB_HTTP_HEADER_CONTENT_TYPE),
JSON_CHARSET_CONTENT_TYPE, strlen(JSON_CHARSET_CONTENT_TYPE));
TEST_CHECK(ret == 0);
if (!TEST_CHECK(c != NULL)) {
TEST_MSG("http_client failed");
exit(EXIT_FAILURE);
}

ret = flb_http_do(c, &b_sent);
if (!TEST_CHECK(ret == 0)) {
TEST_MSG("ret error. ret=%d\n", ret);
}
else if (!TEST_CHECK(b_sent > 0)){
TEST_MSG("b_sent size error. b_sent = %lu\n", b_sent);
}
else if (!TEST_CHECK(c->resp.status == atoi(response_code))) {
TEST_MSG("http response code error. expect: %d, got: %d\n", atoi(response_code), c->resp.status);
}

/* waiting to flush */
flb_time_msleep(1500);

num = get_output_num();
if (!TEST_CHECK(num > 0)) {
TEST_MSG("no outputs");
}
flb_http_client_destroy(c);
flb_upstream_conn_release(ctx->httpc->u_conn);
test_ctx_destroy(ctx);
}

void flb_test_http_successful_response_code_200()
{
flb_test_http_successful_response_code("200");
flb_test_http_json_charset_header("200");
}

void flb_test_http_successful_response_code_204()
{
flb_test_http_successful_response_code("204");
flb_test_http_json_charset_header("204");
}

void flb_test_http_failure_400_bad_json() {
Expand Down
Loading