Skip to content

Commit

Permalink
Merge pull request #250 from leocassarani/keymap-mutex
Browse files Browse the repository at this point in the history
Fix race conditions in Faraday::Utils::Headers
  • Loading branch information
sferik committed Apr 1, 2013
2 parents ca8c169 + 69096c0 commit c395b0c
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/faraday/utils.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'uri'
require 'thread'
Faraday.require_libs 'parameters'

module Faraday
Expand All @@ -17,14 +18,18 @@ def initialize(hash={})
self.update hash
end

# need to synchronize concurrent writes to the shared KeyMap
keymap_mutex = Mutex.new

# symbol -> string mapper + cache
KeyMap = Hash.new do |map, key|
map[key] = if key.respond_to?(:to_str) then key
value = if key.respond_to?(:to_str) then key
else
key.to_s.split('_'). # :user_agent => %w(user agent)
each { |w| w.capitalize! }. # => %w(User Agent)
join('-') # => "User-Agent"
end
keymap_mutex.synchronize { map[key] = value }
end
KeyMap[:etag] = "ETag"

Expand Down

0 comments on commit c395b0c

Please sign in to comment.