Incorrect Host header for IPv6 addresses
When specifying a URI with an IPv6 address as the host, the Host header of the request contains the IPv6 address but it is not surrounded by brackets
e.g. With a locally running apache webserver, the following snippet results in an HTTP 400 Bad Request error because of the malformed host header (Host: ::1 instead of Host: [::1])
require 'net/http'
uri = URI.parse('http://[::1]')
Net::HTTP.get_response(uri)
I tried to debug this issue a little bit.
When URI is not used, the addr_port method is normally called.
The addr_port method handles wrapping an IPv6 address in brackets.
When a URI is used, we end up here, where req['host'] is set and that prevents addr_port from being called.
I don't know what the best fix would be, but having a single source of truth for handling the conversion of the 'host' header would probably help.
Edit:
How we end up with ::1 rather than [::1] is due to using #hostname (vs #host).