Skip to content

Commit

Permalink
Merge pull request #6 from rykov/master
Browse files Browse the repository at this point in the history
Prevent nil headers from passing to Net::HTTP
  • Loading branch information
ncr committed Feb 1, 2012
2 parents 304aff4 + 5cdff83 commit 27e46c1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/rack/proxy.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def perform_request(env)


def extract_http_request_headers(env) def extract_http_request_headers(env)
headers = env.reject do |k, v| headers = env.reject do |k, v|
!(/^HTTP_[A-Z_]+$/ === k) !(/^HTTP_[A-Z_]+$/ === k) || v.nil?
end.map do |k, v| end.map do |k, v|
[reconstruct_header_name(k), v] [reconstruct_header_name(k), v]
end.inject(Utils::HeaderHash.new) do |hash, k_v| end.inject(Utils::HeaderHash.new) do |hash, k_v|
Expand Down
2 changes: 1 addition & 1 deletion test/net_http_hacked_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_net_http_hacked


assert headers.size > 0 assert headers.size > 0
assert headers["content-type"] == "text/html; charset=UTF-8" assert headers["content-type"] == "text/html; charset=UTF-8"
assert headers["content-length"].to_i > 0 assert !headers["date"].nil?


# Body # Body
chunks = [] chunks = []
Expand Down
15 changes: 14 additions & 1 deletion test/rack_proxy_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class RackProxyTest < Test::Unit::TestCase
class TrixProxy < Rack::Proxy class TrixProxy < Rack::Proxy
def rewrite_env(env) def rewrite_env(env)
env["HTTP_HOST"] = "trix.pl" env["HTTP_HOST"] = "trix.pl"

env env
end end
end end
Expand All @@ -29,4 +28,18 @@ def test_header_reconstruction
header = proxy.send(:reconstruct_header_name, "HTTP_ABC_D") header = proxy.send(:reconstruct_header_name, "HTTP_ABC_D")
assert header == "ABC-D" assert header == "ABC-D"
end end

def test_extract_http_request_headers
proxy = Rack::Proxy.new
env = {
'NOT-HTTP-HEADER' => 'test-value',
'HTTP_ACCEPT' => 'text/html',
'HTTP_CONNECTION' => nil
}

headers = proxy.send(:extract_http_request_headers, env)
assert headers.key?('ACCEPT')
assert !headers.key?('CONNECTION')
assert !headers.key?('NOT-HTTP-HEADER')
end
end end

0 comments on commit 27e46c1

Please sign in to comment.