diff --git a/lib/net/http.rb b/lib/net/http.rb index f64f7ba7..aa8c014f 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -512,6 +512,10 @@ class HTTPHeaderSyntaxError < StandardError; end # Returns the write timeout. # - {write_timeout=}[rdoc-ref:Net::HTTP#write_timeout=]: # Sets the write timeout. + # - {:capitalize_headers}[rdoc-ref:Net::HTTP#capitalize_headers]: + # Returns the capitalize_headers config. + # - {:capitalize_headers=}[rdoc-ref:Net::HTTP#capitalize_headers=]: + # Sets the capitalize_headers config. # # === Requests # @@ -1192,6 +1196,7 @@ def initialize(address, port = nil) # :nodoc: @ssl_context = nil @ssl_session = nil @sspi_enabled = false + @capitalize_headers = true SSL_IVNAMES.each do |ivname| instance_variable_set ivname, nil end @@ -1587,6 +1592,9 @@ def use_ssl=(flag) # See {OpenSSL::SSL::SSLContext#verify_hostname=}[OpenSSL::SSL::SSL::Context#verify_hostname=]. attr_accessor :verify_hostname + # Sets or returns whether to capitalize the headers + attr_accessor :capitalize_headers + # Returns the X509 certificate chain (an array of strings) # for the session's socket peer, # or +nil+ if none. @@ -2372,6 +2380,7 @@ def request(req, body = nil, &block) # :yield: +response+ return request(req, body, &block) } end + req.capitalize_headers = capitalize_headers if proxy_user() req.proxy_basic_auth proxy_user(), proxy_pass() unless use_ssl? end diff --git a/lib/net/http/generic_request.rb b/lib/net/http/generic_request.rb index 44e329a0..34e57556 100644 --- a/lib/net/http/generic_request.rb +++ b/lib/net/http/generic_request.rb @@ -55,6 +55,7 @@ def initialize(m, reqbody, resbody, uri_or_path, initheader = nil) # :nodoc: @body = nil @body_stream = nil @body_data = nil + @capitalize_headers = true end # Returns the string method name for the request: @@ -94,6 +95,9 @@ def initialize(m, reqbody, resbody, uri_or_path, initheader = nil) # :nodoc: # attr_reader :decode_content + # Sets if will capitalize headers + attr_accessor :capitalize_headers + # Returns a string representation of the request: # # Net::HTTP::Post.new(uri).inspect # => "#" @@ -403,8 +407,14 @@ def write_header(sock, ver, path) end buf = +'' buf << reqline << "\r\n" - each_capitalized do |k,v| - buf << "#{k}: #{v}\r\n" + if capitalize_headers + each_capitalized do |k,v| + buf << "#{k}: #{v}\r\n" + end + else + each_header do |k,v| + buf << "#{k}: #{v}\r\n" + end end buf << "\r\n" sock.write buf diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb index c9a27d87..9000b7cb 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -1403,4 +1403,27 @@ def test_partial_response http.ignore_eof = false assert_raise(EOFError) {http.get('/')} end + + def test_capitalized_headers + headers = { accept: '*/*' } + expected_raw_header = "Accept: */*\r\n" + @server.mount_proc('/') do |req, _res| + assert_includes(req.raw_header, expected_raw_header) + end + + http = Net::HTTP.new(config('host'), config('port')) + http.get('/', headers) + end + + def test_no_capitalized_headers + headers = { accept: '*/*' } + expected_raw_header = "accept: */*\r\n" + @server.mount_proc('/') do |req, _res| + assert_includes(req.raw_header, expected_raw_header) + end + + http = Net::HTTP.new(config('host'), config('port')) + http.capitalize_headers = false + http.get('/', headers) + end end