Skip to content

Commit

Permalink
Let Host be automatically inferred on redirects
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
janko committed May 18, 2017
1 parent 85a994c commit 561fb70
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,17 @@ def initialize(opts)

# Returns new Request with updated uri
def redirect(uri, verb = @verb)
req = self.class.new(
headers = self.headers.dup
headers.delete(Headers::HOST)

self.class.new(
:verb => verb,
:uri => @uri.join(uri),
:headers => headers,
:proxy => proxy,
:body => body,
:version => version
)

req[Headers::HOST] = req.uri.host
req
end

# Stream the request to a socket
Expand Down
14 changes: 14 additions & 0 deletions spec/lib/http/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@
expect(redirected["Host"]).to eq "blog.example.com"
end

context "with URL with non-standard port given" do
subject(:redirected) { request.redirect "http://example.com:8080" }

its(:uri) { is_expected.to eq HTTP::URI.parse "http://example.com:8080" }

its(:verb) { is_expected.to eq request.verb }
its(:body) { is_expected.to eq request.body }
its(:proxy) { is_expected.to eq request.proxy }

it "presets new Host header" do
expect(redirected["Host"]).to eq "example.com:8080"
end
end

context "with schema-less absolute URL given" do
subject(:redirected) { request.redirect "//another.example.com/blog" }

Expand Down

0 comments on commit 561fb70

Please sign in to comment.