Skip to content

Commit

Permalink
Bug fix - host was not changing between redirects.
Browse files Browse the repository at this point in the history
The Host header is now always taken from the Resource's uri - any
user-defined Host header is ignored. Previously, if you received a
redirect with the only change being the host, you would end in an
"infinite" loop (at least until you could no longer open connections).
  • Loading branch information
knaveofdiamonds committed Feb 2, 2009
1 parent d8df6d7 commit 7bfaa98
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/resourceful/net_http_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def self.make_request(method, uri, body = nil, header = nil)
conn.start
res = conn.request(req, body)
ensure
conn.finish
conn.finish if conn.started?
end

[ Integer(res.code),
Expand Down
6 changes: 3 additions & 3 deletions lib/resourceful/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def initialize(http_method, resource, body = nil, header = nil)
# Resourceful handled gzip encoding transparently, so set that up
@header.accept_encoding ||= 'gzip, identity'

# 'Host' is a required HTTP/1.1 header, so set it if it isn't already
@header.host ||= Addressable::URI.parse(resource.uri).host
# 'Host' is a required HTTP/1.1 header, overrides Host in user-provided headers
@header.host = @resource.host

# Setting the date isn't a bad idea, either
@header.date ||= Time.now.httpdate
Expand Down Expand Up @@ -105,6 +105,7 @@ def follow_redirect(response)
new_uri = response.header.location.first
logger.info(" Permanently redirected to #{new_uri} - Storing new location.")
resource.update_uri new_uri
@header.host = resource.host
response = fetch_response
elsif response.see_other? # Always use GET for this redirect, regardless of initial method
redirected_resource = Resourceful::Resource.new(self.accessor, response.header['Location'].first)
Expand Down Expand Up @@ -220,7 +221,6 @@ def max_age
def logger
resource.logger
end

end

end
5 changes: 5 additions & 0 deletions lib/resourceful/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ def effective_uri
end
alias uri effective_uri

# Returns the host for this Resource's current uri
def host
Addressable::URI.parse(uri).host
end

# Updates the effective uri after following a permanent redirect
def update_uri(uri)
@uris.unshift(uri)
Expand Down

0 comments on commit 7bfaa98

Please sign in to comment.