From 7791d86e3ae4425d06223497f82b88fa395f26be Mon Sep 17 00:00:00 2001 From: Chin Date: Tue, 8 Oct 2019 00:07:24 +0800 Subject: [PATCH 1/3] Modify on_request response from 200 to 204 Signed-off-by: Chin 1. Modify response status code in in_http 2. Add test_response_without_empty_img in test_in_http 3. Fix typo of test_response_with_empty_img in test_in_http --- lib/fluent/plugin/in_http.rb | 4 ++-- test/plugin/test_in_http.rb | 30 ++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/fluent/plugin/in_http.rb b/lib/fluent/plugin/in_http.rb index 06f047f2c7..00b805d3f6 100644 --- a/lib/fluent/plugin/in_http.rb +++ b/lib/fluent/plugin/in_http.rb @@ -152,7 +152,7 @@ def on_request(path_info, params) if @respond_with_empty_img return ["200 OK", {'Content-Type'=>'image/gif; charset=utf-8'}, EMPTY_GIF_IMAGE] else - return ["200 OK", {'Content-Type'=>'text/plain'}, ""] + return ["204 No Content", {}] end end @@ -219,7 +219,7 @@ def on_request(path_info, params) if @respond_with_empty_img return ["200 OK", {'Content-Type'=>'image/gif; charset=utf-8'}, EMPTY_GIF_IMAGE] else - return ["200 OK", {'Content-Type'=>'text/plain'}, ""] + return ["204 No Content", {}] end end diff --git a/test/plugin/test_in_http.rb b/test/plugin/test_in_http.rb index e8ae607cf6..aebb82ad1d 100644 --- a/test/plugin/test_in_http.rb +++ b/test/plugin/test_in_http.rb @@ -549,8 +549,8 @@ def test_with_csv assert_equal_event_time time, d.events[1][1] end - def test_resonse_with_empty_img - d = create_driver(CONFIG + "respond_with_empty_img true") + def test_response_with_empty_img + d = create_driver(CONFIG) assert_equal true, d.instance.respond_with_empty_img time = event_time("2011-01-02 13:14:15 UTC") @@ -577,6 +577,32 @@ def test_resonse_with_empty_img assert_equal_event_time time, d.events[1][1] end + def test_response_without_empty_img + d = create_driver(CONFIG + "respond_with_empty_img false") + assert_equal false, d.instance.respond_with_empty_img + + time = event_time("2011-01-02 13:14:15 UTC") + time_i = time.to_i + events = [ + ["tag1", time, {"a"=>1}], + ["tag2", time, {"a"=>2}], + ] + res_codes = [] + res_bodies = [] + + d.run do + events.each do |tag, _t, record| + res = post("/#{tag}", {"json"=>record.to_json, "time"=>time_i.to_s}) + res_codes << res.code + end + end + assert_equal ["204", "204"], res_codes + assert_equal [], res_bodies + assert_equal events, d.events + assert_equal_event_time time, d.events[0][1] + assert_equal_event_time time, d.events[1][1] + end + def test_cors_allowed d = create_driver(CONFIG + "cors_allow_origins [\"http://foo.com\"]") assert_equal ["http://foo.com"], d.instance.cors_allow_origins From 8fd4454267bb5ecee22679e2b72ff0dc9a25a838 Mon Sep 17 00:00:00 2001 From: Chin Date: Tue, 8 Oct 2019 22:45:54 +0800 Subject: [PATCH 2/3] Add use_204_response flag to keep current behaviors consistent. Signed-off-by: Chin 1. Add use_204_response flag to avoid breaking change happened in in_http 2. Modify test to keep testing the edge --- lib/fluent/plugin/in_http.rb | 14 +++++++++++--- test/plugin/test_in_http.rb | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/fluent/plugin/in_http.rb b/lib/fluent/plugin/in_http.rb index 00b805d3f6..922d9c1c8f 100644 --- a/lib/fluent/plugin/in_http.rb +++ b/lib/fluent/plugin/in_http.rb @@ -140,7 +140,7 @@ def close super end - def on_request(path_info, params) + def on_request(path_info, params, use_204_response=false) begin path = path_info[1..-1] # remove / tag = path.split('/').join('.') @@ -152,7 +152,11 @@ def on_request(path_info, params) if @respond_with_empty_img return ["200 OK", {'Content-Type'=>'image/gif; charset=utf-8'}, EMPTY_GIF_IMAGE] else - return ["204 No Content", {}] + if use_204_response + return ["204 No Content", {}] + else + return ["200 OK", {'Content-Type'=>'text/plain'}, ""] + end end end @@ -219,7 +223,11 @@ def on_request(path_info, params) if @respond_with_empty_img return ["200 OK", {'Content-Type'=>'image/gif; charset=utf-8'}, EMPTY_GIF_IMAGE] else - return ["204 No Content", {}] + if use_204_response + return ["204 No Content", {}] + else + return ["200 OK", {'Content-Type'=>'text/plain'}, ""] + end end end diff --git a/test/plugin/test_in_http.rb b/test/plugin/test_in_http.rb index aebb82ad1d..5f8e5648f1 100644 --- a/test/plugin/test_in_http.rb +++ b/test/plugin/test_in_http.rb @@ -596,7 +596,7 @@ def test_response_without_empty_img res_codes << res.code end end - assert_equal ["204", "204"], res_codes + assert_equal ["200", "200"], res_codes assert_equal [], res_bodies assert_equal events, d.events assert_equal_event_time time, d.events[0][1] From ee2ae7b0178ec1f13e53ff18d35e7b5818bc55b1 Mon Sep 17 00:00:00 2001 From: chin Date: Wed, 9 Oct 2019 12:41:47 +0800 Subject: [PATCH 3/3] Add use_204_response in config_param to provide user trigger it Signed-off-by: chin 1.Add config_param use_204_response in in_http 2.Add test_response_use_204_response in test_in_http --- lib/fluent/plugin/in_http.rb | 8 +++++--- test/plugin/test_in_http.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/lib/fluent/plugin/in_http.rb b/lib/fluent/plugin/in_http.rb index 922d9c1c8f..dd493a4e36 100644 --- a/lib/fluent/plugin/in_http.rb +++ b/lib/fluent/plugin/in_http.rb @@ -58,6 +58,8 @@ class HttpInput < Input config_param :cors_allow_origins, :array, default: nil desc 'Respond with empty gif image of 1x1 pixel.' config_param :respond_with_empty_img, :bool, default: false + desc 'Respond status code with 204.' + config_param :use_204_response, :bool, default: false config_section :parse do config_set_default :@type, 'in_http' @@ -140,7 +142,7 @@ def close super end - def on_request(path_info, params, use_204_response=false) + def on_request(path_info, params) begin path = path_info[1..-1] # remove / tag = path.split('/').join('.') @@ -152,7 +154,7 @@ def on_request(path_info, params, use_204_response=false) if @respond_with_empty_img return ["200 OK", {'Content-Type'=>'image/gif; charset=utf-8'}, EMPTY_GIF_IMAGE] else - if use_204_response + if @use_204_response return ["204 No Content", {}] else return ["200 OK", {'Content-Type'=>'text/plain'}, ""] @@ -223,7 +225,7 @@ def on_request(path_info, params, use_204_response=false) if @respond_with_empty_img return ["200 OK", {'Content-Type'=>'image/gif; charset=utf-8'}, EMPTY_GIF_IMAGE] else - if use_204_response + if @use_204_response return ["204 No Content", {}] else return ["200 OK", {'Content-Type'=>'text/plain'}, ""] diff --git a/test/plugin/test_in_http.rb b/test/plugin/test_in_http.rb index 5f8e5648f1..d1511b3b55 100644 --- a/test/plugin/test_in_http.rb +++ b/test/plugin/test_in_http.rb @@ -32,6 +32,7 @@ def teardown body_size_limit 10m keepalive_timeout 5 respond_with_empty_img true + use_204_response false ] def create_driver(conf=CONFIG) @@ -603,6 +604,35 @@ def test_response_without_empty_img assert_equal_event_time time, d.events[1][1] end + def test_response_use_204_response + d = create_driver(CONFIG + %[ + respond_with_empty_img false + use_204_response true + ]) + assert_equal true, d.instance.use_204_response + + time = event_time("2011-01-02 13:14:15 UTC") + time_i = time.to_i + events = [ + ["tag1", time, {"a"=>1}], + ["tag2", time, {"a"=>2}], + ] + res_codes = [] + res_bodies = [] + + d.run do + events.each do |tag, _t, record| + res = post("/#{tag}", {"json"=>record.to_json, "time"=>time_i.to_s}) + res_codes << res.code + end + end + assert_equal ["204", "204"], res_codes + assert_equal [], res_bodies + assert_equal events, d.events + assert_equal_event_time time, d.events[0][1] + assert_equal_event_time time, d.events[1][1] + end + def test_cors_allowed d = create_driver(CONFIG + "cors_allow_origins [\"http://foo.com\"]") assert_equal ["http://foo.com"], d.instance.cors_allow_origins