Skip to content

Commit 561fb70

Browse files
committed
Let Host be automatically inferred on redirects
When redirecting on "http://localhost:<port>" URLs, the Host will be correctly set to "http://localhost:<port>" on the initial request, but on following the redirect the port is lost, because we're only assigning the host. Therefore when we initialize the follow-up request object we de-assign "Host", and let HTTP::Request set it in the same way as it is set on the initial request.
1 parent 85a994c commit 561fb70

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

lib/http/request.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,17 @@ def initialize(opts)
9494

9595
# Returns new Request with updated uri
9696
def redirect(uri, verb = @verb)
97-
req = self.class.new(
97+
headers = self.headers.dup
98+
headers.delete(Headers::HOST)
99+
100+
self.class.new(
98101
:verb => verb,
99102
:uri => @uri.join(uri),
100103
:headers => headers,
101104
:proxy => proxy,
102105
:body => body,
103106
:version => version
104107
)
105-
106-
req[Headers::HOST] = req.uri.host
107-
req
108108
end
109109

110110
# Stream the request to a socket

spec/lib/http/request_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@
9494
expect(redirected["Host"]).to eq "blog.example.com"
9595
end
9696

97+
context "with URL with non-standard port given" do
98+
subject(:redirected) { request.redirect "http://example.com:8080" }
99+
100+
its(:uri) { is_expected.to eq HTTP::URI.parse "http://example.com:8080" }
101+
102+
its(:verb) { is_expected.to eq request.verb }
103+
its(:body) { is_expected.to eq request.body }
104+
its(:proxy) { is_expected.to eq request.proxy }
105+
106+
it "presets new Host header" do
107+
expect(redirected["Host"]).to eq "example.com:8080"
108+
end
109+
end
110+
97111
context "with schema-less absolute URL given" do
98112
subject(:redirected) { request.redirect "//another.example.com/blog" }
99113

0 commit comments

Comments
 (0)