diff --git a/lib/fluent/plugin/in_http.rb b/lib/fluent/plugin/in_http.rb index 06f047f2c7..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' @@ -152,7 +154,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 ["200 OK", {'Content-Type'=>'text/plain'}, ""] + if @use_204_response + return ["204 No Content", {}] + else + return ["200 OK", {'Content-Type'=>'text/plain'}, ""] + end end end @@ -219,7 +225,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 ["200 OK", {'Content-Type'=>'text/plain'}, ""] + 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 e8ae607cf6..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) @@ -549,8 +550,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 +578,61 @@ 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 ["200", "200"], 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_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