Skip to content

Commit

Permalink
Merge pull request #1535 from fluent/fix-in_http-x-forwarded-for
Browse files Browse the repository at this point in the history
Fix X-Forwarded-For header handling. Accpet multiple headers. fix #1531
  • Loading branch information
repeatedly committed Apr 19, 2017
1 parent 423cf7f commit 518a50f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/fluent/plugin/in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ def on_headers_complete(headers)
when /Origin/i
@origin = v
when /X-Forwarded-For/i
# For multiple X-Forwarded-For headers. Use first header value.
v = v.first if v.is_a?(Array)
@remote_addr = v.split(",").first
end
}
Expand Down
30 changes: 29 additions & 1 deletion test/plugin/test_in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,33 @@ def test_multi_json_with_add_remote_addr_given_x_forwarded_for

end

def test_add_remote_addr_given_multi_x_forwarded_for
d = create_driver(CONFIG + "add_remote_addr true")

tag = "tag1"
time = event_time("2011-01-02 13:14:15 UTC").to_i
record = {"a" => 1}

d.expect_emit tag, time, {"REMOTE_ADDR"=>"129.78.138.66", "a" => 1}

d.run do
res = post("/#{tag}", {"json" => record.to_json, "time" => time.to_s}) { |http, req|
# net/http can't send multiple headers so overwrite it.
def req.each_capitalized
block_given? or return enum_for(__method__) { @header.size }
@header.each do |k, vs|
vs.each { |v|
yield capitalize(k), v
}
end
end
req.add_field("X-Forwarded-For", "129.78.138.66, 127.0.0.1")
req.add_field("X-Forwarded-For", "8.8.8.8")
}
assert_equal "200", res.code
end
end

def test_multi_json_with_add_http_headers
d = create_driver(CONFIG + "add_http_headers true")

Expand Down Expand Up @@ -431,9 +458,10 @@ def on_message_complete
end
end

def post(path, params, header = {})
def post(path, params, header = {}, &block)
http = Net::HTTP.new("127.0.0.1", PORT)
req = Net::HTTP::Post.new(path, header)
block.call(http, req) if block
if params.is_a?(String)
req.body = params
else
Expand Down

0 comments on commit 518a50f

Please sign in to comment.