From ed4719f33816b001334e20b1e8bf6734652de5ff Mon Sep 17 00:00:00 2001 From: Fujimoto Seiji Date: Thu, 13 May 2021 10:24:37 +0900 Subject: [PATCH] in_http: Add support for HTTP GET requests Requested by Josh Keegan. Azure App uses GET requests to check if the HTTP server is working all right. Since Fluentd responds with "400 Bad Requests" to HTTP GET, it does not work well. This teaches Fluentd to respond nicely with "200 OK" Signed-off-by: Fujimoto Seiji --- lib/fluent/plugin/in_http.rb | 10 ++++++++++ test/plugin/test_in_http.rb | 15 +++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/fluent/plugin/in_http.rb b/lib/fluent/plugin/in_http.rb index 1f4ff53323..2af6835fed 100644 --- a/lib/fluent/plugin/in_http.rb +++ b/lib/fluent/plugin/in_http.rb @@ -461,6 +461,12 @@ def on_body(chunk) RES_200_STATUS = "200 OK".freeze RES_403_STATUS = "403 Forbidden".freeze + # Azure App Service sends GET requests for health checking purpose. + # Respond with `200 OK` to accommodate it. + def handle_get_request + return send_response_and_close(RES_200_STATUS, {}, "") + end + # Web browsers can send an OPTIONS request before performing POST # to check if cross-origin requests are supported. def handle_options_request @@ -494,6 +500,10 @@ def handle_options_request def on_message_complete return if closing? + if @parser.http_method == 'GET'.freeze + return handle_get_request() + end + if @parser.http_method == 'OPTIONS'.freeze return handle_options_request() end diff --git a/test/plugin/test_in_http.rb b/test/plugin/test_in_http.rb index 757fbf1793..92e7f5cc21 100644 --- a/test/plugin/test_in_http.rb +++ b/test/plugin/test_in_http.rb @@ -770,6 +770,15 @@ def test_cors_allowed_wildcard end end + def test_get_request + d = create_driver(CONFIG) + + d.run do + res = get("/cors.test", {}, {}) + assert_equal "200", res.code + end + end + def test_cors_preflight d = create_driver(CONFIG + 'cors_allow_origins ["*"]') @@ -985,6 +994,12 @@ def test_if_content_type_is_initialized_properly assert_equal $test_in_http_connection_object_ids[0], $test_in_http_connection_object_ids[1] end + def get(path, params, header = {}) + http = Net::HTTP.new("127.0.0.1", PORT) + req = Net::HTTP::Get.new(path, header) + http.request(req) + end + def options(path, params, header = {}) http = Net::HTTP.new("127.0.0.1", PORT) req = Net::HTTP::Options.new(path, header)