Skip to content

Commit

Permalink
Improve persistence origin verification
Browse files Browse the repository at this point in the history
  • Loading branch information
ixti committed Apr 7, 2015
1 parent b42accd commit 5b81f62
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
9 changes: 2 additions & 7 deletions lib/http/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def close

# Verify our request isn't going to be made against another URI
def verify_connection!(uri)
if default_options.persistent? && base_host(uri) != default_options.persistent
fail StateError, "Persistence is enabled for #{default_options.persistent}, but we got #{base_host(uri)}"
if default_options.persistent? && uri.origin != default_options.persistent
fail StateError, "Persistence is enabled for #{default_options.persistent}, but we got #{uri.origin}"
# We re-create the connection object because we want to let prior requests
# lazily load the body as long as possible, and this mimics prior functionality.
elsif @connection && (!@connection.keep_alive? || @connection.expired?)
Expand All @@ -120,11 +120,6 @@ def verify_connection!(uri)
end
end

# Strips out query/path to give us a consistent way of comparing hosts
def base_host(uri)
uri.omit(:query, :path).to_s
end

# Merges query params if needed
#
# @param [#to_s] uri
Expand Down
13 changes: 9 additions & 4 deletions lib/http/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "openssl"
require "socket"
require "http/cache/null_cache"
require "http/uri"

module HTTP
class Options
Expand Down Expand Up @@ -81,6 +82,14 @@ def follow=(value)
end
end

def persistent=(value)
@persistent = value ? HTTP::URI.parse(value).origin : nil
end

def persistent?
!persistent.nil?
end

def_option :cache do |cache_or_cache_options|
if cache_or_cache_options.respond_to? :perform
cache_or_cache_options
Expand All @@ -90,10 +99,6 @@ def follow=(value)
end
end

def persistent?
!persistent.nil? && persistent != ""
end

def [](option)
send(option) rescue nil
end
Expand Down

0 comments on commit 5b81f62

Please sign in to comment.