Skip to content

Commit

Permalink
in_http: Add support for HTTP GET requests
Browse files Browse the repository at this point in the history
Requested by Josh Keegan. Evidently Azure App uses HTTP GET to check
if the HTTP server is working all right. Since Fluentd responds with
"400 Bad Requests" to GET requests, it does not work well.

This commit teaches Fluentd to respond nicely with a plain "200 OK"

Signed-off-by: Fujimoto Seiji <fujimoto@ceptord.net>
  • Loading branch information
fujimotos committed May 13, 2021
1 parent b64ffca commit 9b59187
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/fluent/plugin/in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,12 @@ def on_body(chunk)
RES_200_STATUS = "200 OK".freeze
RES_403_STATUS = "403 Forbidden".freeze

# Azure App Service uses `GET /` as a health check.
# Simply return `200 OK` here.
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
Expand All @@ -469,7 +475,7 @@ def handle_options_request
return send_response_and_close(RES_403_STATUS, {}, "")
end

# in_http does not support HTTP methods except POST
# in_http does not support HTTP methods except POST and GET
if @access_control_request_method != 'POST'
return send_response_and_close(RES_403_STATUS, {}, "")
end
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions test/plugin/test_in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 ["*"]')

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9b59187

Please sign in to comment.