From d7a5638dfbe68d0a92958c0e81f44054ddd7d291 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Mon, 4 Apr 2011 21:52:37 -0300 Subject: raise if someone tries to modify the flash when it was already streamed back to the client or converted to HTTP headers --- actionpack/lib/action_dispatch/middleware/flash.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/middleware') diff --git a/actionpack/lib/action_dispatch/middleware/flash.rb b/actionpack/lib/action_dispatch/middleware/flash.rb index 21aeeb217a..410d3f7127 100644 --- a/actionpack/lib/action_dispatch/middleware/flash.rb +++ b/actionpack/lib/action_dispatch/middleware/flash.rb @@ -43,9 +43,15 @@ module ActionDispatch class FlashNow #:nodoc: def initialize(flash) @flash = flash + @closed = false end + attr_reader :closed + alias :closed? :closed + def close!; @closed = true end + def []=(k, v) + raise ClosedError, "Cannot modify flash because it was closed. This means it was already streamed back to the client or converted to HTTP headers." if closed? @flash[k] = v @flash.discard(k) v @@ -70,9 +76,15 @@ module ActionDispatch def initialize #:nodoc: super @used = Set.new + @closed = false end + attr_reader :closed + alias :closed? :closed + def close!; @closed = true end + def []=(k, v) #:nodoc: + raise ClosedError, "Cannot modify flash because it was closed. This means it was already streamed back to the client or converted to HTTP headers." if closed? keep(k) super end @@ -184,8 +196,11 @@ module ActionDispatch session = env['rack.session'] || {} flash_hash = env['action_dispatch.request.flash_hash'] - if flash_hash && (!flash_hash.empty? || session.key?('flash')) + if flash_hash + if !flash_hash.empty? || session.key?('flash') session["flash"] = flash_hash + end + flash_hash.close! end if session.key?('flash') && session['flash'].empty? @@ -193,4 +208,7 @@ module ActionDispatch end end end + + class ClosedError < StandardError #:nodoc: + end end -- cgit v1.2.3 From 90ecad0bc944fc3adb847c0c754d8f0dc2bed4b5 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 6 Apr 2011 12:30:08 -0300 Subject: Add ClosedError message to the initializer --- actionpack/lib/action_dispatch/middleware/closed_error.rb | 7 +++++++ actionpack/lib/action_dispatch/middleware/flash.rb | 7 ++----- 2 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 actionpack/lib/action_dispatch/middleware/closed_error.rb (limited to 'actionpack/lib/action_dispatch/middleware') diff --git a/actionpack/lib/action_dispatch/middleware/closed_error.rb b/actionpack/lib/action_dispatch/middleware/closed_error.rb new file mode 100644 index 0000000000..0a4db47f4b --- /dev/null +++ b/actionpack/lib/action_dispatch/middleware/closed_error.rb @@ -0,0 +1,7 @@ +module ActionDispatch + class ClosedError < StandardError #:nodoc: + def initialize(kind) + super "Cannot modify #{kind} because it was closed. This means it was already streamed back to the client or converted to HTTP headers." + end + end +end diff --git a/actionpack/lib/action_dispatch/middleware/flash.rb b/actionpack/lib/action_dispatch/middleware/flash.rb index 410d3f7127..6eda1f31a7 100644 --- a/actionpack/lib/action_dispatch/middleware/flash.rb +++ b/actionpack/lib/action_dispatch/middleware/flash.rb @@ -51,7 +51,7 @@ module ActionDispatch def close!; @closed = true end def []=(k, v) - raise ClosedError, "Cannot modify flash because it was closed. This means it was already streamed back to the client or converted to HTTP headers." if closed? + raise ClosedError, :flash if closed? @flash[k] = v @flash.discard(k) v @@ -84,7 +84,7 @@ module ActionDispatch def close!; @closed = true end def []=(k, v) #:nodoc: - raise ClosedError, "Cannot modify flash because it was closed. This means it was already streamed back to the client or converted to HTTP headers." if closed? + raise ClosedError, :flash if closed? keep(k) super end @@ -208,7 +208,4 @@ module ActionDispatch end end end - - class ClosedError < StandardError #:nodoc: - end end -- cgit v1.2.3 From 0c5aded0922f80bd1a31c7d2a3974469a18160a8 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 6 Apr 2011 12:05:58 -0300 Subject: raise if someone tries to modify the cookies when it was already streamed back to the client or converted to HTTP headers --- actionpack/lib/action_dispatch/middleware/cookies.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'actionpack/lib/action_dispatch/middleware') diff --git a/actionpack/lib/action_dispatch/middleware/cookies.rb b/actionpack/lib/action_dispatch/middleware/cookies.rb index 7ac608f0a8..67c4b83d45 100644 --- a/actionpack/lib/action_dispatch/middleware/cookies.rb +++ b/actionpack/lib/action_dispatch/middleware/cookies.rb @@ -115,10 +115,15 @@ module ActionDispatch @delete_cookies = {} @host = host @secure = secure + @closed = false super() end + attr_reader :closed + alias :closed? :closed + def close!; @closed = true end + # Returns the value of the cookie by +name+, or +nil+ if no such cookie exists. def [](name) super(name.to_s) @@ -145,6 +150,7 @@ module ActionDispatch # Sets the cookie named +name+. The second argument may be the very cookie # value, or a hash of options as documented above. def []=(key, options) + raise ClosedError, :cookies if closed? if options.is_a?(Hash) options.symbolize_keys! value = options[:value] @@ -225,6 +231,7 @@ module ActionDispatch end def []=(key, options) + raise ClosedError, :cookies if closed? if options.is_a?(Hash) options.symbolize_keys! else @@ -263,6 +270,7 @@ module ActionDispatch end def []=(key, options) + raise ClosedError, :cookies if closed? if options.is_a?(Hash) options.symbolize_keys! options[:value] = @verifier.generate(options[:value]) @@ -305,6 +313,7 @@ module ActionDispatch end def call(env) + cookie_jar = nil status, headers, body = @app.call(env) if cookie_jar = env['action_dispatch.cookies'] @@ -315,6 +324,9 @@ module ActionDispatch end [status, headers, body] + ensure + cookie_jar = ActionDispatch::Request.new(env).cookie_jar unless cookie_jar + cookie_jar.close! end end end -- cgit v1.2.3 From 0ca69ca65f83b4bb34f81f077c1c38c66ad868b9 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 6 Apr 2011 16:27:05 -0700 Subject: CookieJar should prefer composition over inheritance --- actionpack/lib/action_dispatch/middleware/cookies.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'actionpack/lib/action_dispatch/middleware') diff --git a/actionpack/lib/action_dispatch/middleware/cookies.rb b/actionpack/lib/action_dispatch/middleware/cookies.rb index 67c4b83d45..24ebb8fed7 100644 --- a/actionpack/lib/action_dispatch/middleware/cookies.rb +++ b/actionpack/lib/action_dispatch/middleware/cookies.rb @@ -83,7 +83,7 @@ module ActionDispatch # Raised when storing more than 4K of session data. class CookieOverflow < StandardError; end - class CookieJar < Hash #:nodoc: + class CookieJar #:nodoc: # This regular expression is used to split the levels of a domain. # The top level domain can be any string without a period or @@ -116,8 +116,7 @@ module ActionDispatch @host = host @secure = secure @closed = false - - super() + @cookies = {} end attr_reader :closed @@ -126,7 +125,12 @@ module ActionDispatch # Returns the value of the cookie by +name+, or +nil+ if no such cookie exists. def [](name) - super(name.to_s) + @cookies[name.to_s] + end + + def update(other_hash) + @cookies.update other_hash + self end def handle_options(options) #:nodoc: @@ -159,7 +163,7 @@ module ActionDispatch options = { :value => value } end - value = super(key.to_s, value) + value = @cookies[key.to_s] = value handle_options(options) @@ -176,7 +180,7 @@ module ActionDispatch handle_options(options) - value = super(key.to_s) + value = @cookies.delete(key.to_s) @delete_cookies[key] = options value end -- cgit v1.2.3 From 29592a7f09dda2e7e1e0a915d9230fe6a9b5c0af Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 6 Apr 2011 20:53:48 -0300 Subject: Use freeze instead of close! --- actionpack/lib/action_dispatch/middleware/cookies.rb | 7 ++----- actionpack/lib/action_dispatch/middleware/flash.rb | 12 +++--------- 2 files changed, 5 insertions(+), 14 deletions(-) (limited to 'actionpack/lib/action_dispatch/middleware') diff --git a/actionpack/lib/action_dispatch/middleware/cookies.rb b/actionpack/lib/action_dispatch/middleware/cookies.rb index 24ebb8fed7..3ed5f1055f 100644 --- a/actionpack/lib/action_dispatch/middleware/cookies.rb +++ b/actionpack/lib/action_dispatch/middleware/cookies.rb @@ -115,13 +115,10 @@ module ActionDispatch @delete_cookies = {} @host = host @secure = secure - @closed = false @cookies = {} end - attr_reader :closed - alias :closed? :closed - def close!; @closed = true end + alias :closed? :frozen? # Returns the value of the cookie by +name+, or +nil+ if no such cookie exists. def [](name) @@ -330,7 +327,7 @@ module ActionDispatch [status, headers, body] ensure cookie_jar = ActionDispatch::Request.new(env).cookie_jar unless cookie_jar - cookie_jar.close! + cookie_jar.freeze end end end diff --git a/actionpack/lib/action_dispatch/middleware/flash.rb b/actionpack/lib/action_dispatch/middleware/flash.rb index 6eda1f31a7..e7090ecf1b 100644 --- a/actionpack/lib/action_dispatch/middleware/flash.rb +++ b/actionpack/lib/action_dispatch/middleware/flash.rb @@ -43,12 +43,9 @@ module ActionDispatch class FlashNow #:nodoc: def initialize(flash) @flash = flash - @closed = false end - attr_reader :closed - alias :closed? :closed - def close!; @closed = true end + alias :closed? :frozen? def []=(k, v) raise ClosedError, :flash if closed? @@ -76,12 +73,9 @@ module ActionDispatch def initialize #:nodoc: super @used = Set.new - @closed = false end - attr_reader :closed - alias :closed? :closed - def close!; @closed = true end + alias :closed? :frozen? def []=(k, v) #:nodoc: raise ClosedError, :flash if closed? @@ -200,7 +194,7 @@ module ActionDispatch if !flash_hash.empty? || session.key?('flash') session["flash"] = flash_hash end - flash_hash.close! + flash_hash.freeze end if session.key?('flash') && session['flash'].empty? -- cgit v1.2.3 From 76c2ea7882a83159408bdf1f7c363f442a65c4f1 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 6 Apr 2011 17:26:55 -0700 Subject: favor composition over inheritance, have FlashHash delegate to a Hash --- actionpack/lib/action_dispatch/middleware/flash.rb | 47 ++++++++++++++++++---- 1 file changed, 40 insertions(+), 7 deletions(-) (limited to 'actionpack/lib/action_dispatch/middleware') diff --git a/actionpack/lib/action_dispatch/middleware/flash.rb b/actionpack/lib/action_dispatch/middleware/flash.rb index 6eda1f31a7..98dbe13438 100644 --- a/actionpack/lib/action_dispatch/middleware/flash.rb +++ b/actionpack/lib/action_dispatch/middleware/flash.rb @@ -72,11 +72,13 @@ module ActionDispatch end end - class FlashHash < Hash + class FlashHash + include Enumerable + def initialize #:nodoc: - super - @used = Set.new - @closed = false + @used = Set.new + @closed = false + @flashes = {} end attr_reader :closed @@ -86,19 +88,50 @@ module ActionDispatch def []=(k, v) #:nodoc: raise ClosedError, :flash if closed? keep(k) - super + @flashes[k] = v + end + + def [](k) + @flashes[k] end def update(h) #:nodoc: h.keys.each { |k| keep(k) } - super + @flashes.update h + self + end + + def keys + @flashes.keys + end + + def delete(key) + @flashes.delete key + self + end + + def to_hash + @flashes.dup + end + + def empty? + @flashes.empty? + end + + def clear + @flashes.clear + end + + def each(&block) + @flashes.each(&block) end alias :merge! :update def replace(h) #:nodoc: @used = Set.new - super + @flashes.replace h + self end # Sets a flash that will not be available to the next action, only to the current. -- cgit v1.2.3 From 32f876786aa1eb718f122b116203b01ee0260113 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 6 Apr 2011 18:01:03 -0700 Subject: getting the flash hash under test --- actionpack/lib/action_dispatch/middleware/flash.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'actionpack/lib/action_dispatch/middleware') diff --git a/actionpack/lib/action_dispatch/middleware/flash.rb b/actionpack/lib/action_dispatch/middleware/flash.rb index 98dbe13438..b5194c675c 100644 --- a/actionpack/lib/action_dispatch/middleware/flash.rb +++ b/actionpack/lib/action_dispatch/middleware/flash.rb @@ -105,6 +105,10 @@ module ActionDispatch @flashes.keys end + def key?(name) + @flashes.key? name + end + def delete(key) @flashes.delete key self -- cgit v1.2.3 From dffeda377021ba8691381195f5a2889f8e040b93 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 6 Apr 2011 22:25:07 -0300 Subject: Eagerly load Signed and Permanent cookies --- .../lib/action_dispatch/middleware/cookies.rb | 27 ++++++++++++++-------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'actionpack/lib/action_dispatch/middleware') diff --git a/actionpack/lib/action_dispatch/middleware/cookies.rb b/actionpack/lib/action_dispatch/middleware/cookies.rb index 3ed5f1055f..820df8f499 100644 --- a/actionpack/lib/action_dispatch/middleware/cookies.rb +++ b/actionpack/lib/action_dispatch/middleware/cookies.rb @@ -116,6 +116,8 @@ module ActionDispatch @host = host @secure = secure @cookies = {} + @permanent = PermanentCookieJar.new(self, @secret) + @signed = @secret && SignedCookieJar.new(self, @secret) end alias :closed? :frozen? @@ -193,9 +195,7 @@ module ActionDispatch # # cookies.permanent.signed[:remember_me] = current_user.id # # => Set-Cookie: remember_me=BAhU--848956038e692d7046deab32b7131856ab20e14e; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT - def permanent - @permanent ||= PermanentCookieJar.new(self, @secret) - end + attr_reader :permanent # Returns a jar that'll automatically generate a signed representation of cookie value and verify it when reading from # the cookie again. This is useful for creating cookies with values that the user is not supposed to change. If a signed @@ -211,7 +211,8 @@ module ActionDispatch # # cookies.signed[:discount] # => 45 def signed - @signed ||= SignedCookieJar.new(self, @secret) + SignedCookieJar.ensure_secret_provided(@secret) + @signed end def write(headers) @@ -228,7 +229,9 @@ module ActionDispatch class PermanentCookieJar < CookieJar #:nodoc: def initialize(parent_jar, secret) - @parent_jar, @secret = parent_jar, secret + @parent_jar = parent_jar + @secret = secret + @signed = @secret && SignedCookieJar.new(self, @secret) end def []=(key, options) @@ -244,7 +247,8 @@ module ActionDispatch end def signed - @signed ||= SignedCookieJar.new(self, @secret) + SignedCookieJar.ensure_secret_provided(@secret) + @signed end def method_missing(method, *arguments, &block) @@ -257,7 +261,8 @@ module ActionDispatch SECRET_MIN_LENGTH = 30 # Characters def initialize(parent_jar, secret) - ensure_secret_secure(secret) + self.class.ensure_secret_provided(secret) + self.class.ensure_secret_length(secret) @parent_jar = parent_jar @verifier = ActiveSupport::MessageVerifier.new(secret) end @@ -289,9 +294,7 @@ module ActionDispatch protected - # To prevent users from using something insecure like "Password" we make sure that the - # secret they've provided is at least 30 characters in length. - def ensure_secret_secure(secret) + def self.ensure_secret_provided(secret) if secret.blank? raise ArgumentError, "A secret is required to generate an " + "integrity hash for cookie session data. Use " + @@ -299,7 +302,11 @@ module ActionDispatch "least #{SECRET_MIN_LENGTH} characters\"" + "in config/initializers/secret_token.rb" end + end + # To prevent users from using something insecure like "Password" we make sure that the + # secret they've provided is at least 30 characters in length. + def self.ensure_secret_length(secret) if secret.length < SECRET_MIN_LENGTH raise ArgumentError, "Secret should be something secure, " + "like \"#{ActiveSupport::SecureRandom.hex(16)}\". The value you " + -- cgit v1.2.3 From 5b0149a17aa423d0adbec10c8fb8449f15d16673 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 7 Apr 2011 09:20:56 -0300 Subject: Revert "Eagerly load Signed and Permanent cookies" This reverts commit dffeda377021ba8691381195f5a2889f8e040b93. --- .../lib/action_dispatch/middleware/cookies.rb | 27 ++++++++-------------- 1 file changed, 10 insertions(+), 17 deletions(-) (limited to 'actionpack/lib/action_dispatch/middleware') diff --git a/actionpack/lib/action_dispatch/middleware/cookies.rb b/actionpack/lib/action_dispatch/middleware/cookies.rb index 820df8f499..3ed5f1055f 100644 --- a/actionpack/lib/action_dispatch/middleware/cookies.rb +++ b/actionpack/lib/action_dispatch/middleware/cookies.rb @@ -116,8 +116,6 @@ module ActionDispatch @host = host @secure = secure @cookies = {} - @permanent = PermanentCookieJar.new(self, @secret) - @signed = @secret && SignedCookieJar.new(self, @secret) end alias :closed? :frozen? @@ -195,7 +193,9 @@ module ActionDispatch # # cookies.permanent.signed[:remember_me] = current_user.id # # => Set-Cookie: remember_me=BAhU--848956038e692d7046deab32b7131856ab20e14e; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT - attr_reader :permanent + def permanent + @permanent ||= PermanentCookieJar.new(self, @secret) + end # Returns a jar that'll automatically generate a signed representation of cookie value and verify it when reading from # the cookie again. This is useful for creating cookies with values that the user is not supposed to change. If a signed @@ -211,8 +211,7 @@ module ActionDispatch # # cookies.signed[:discount] # => 45 def signed - SignedCookieJar.ensure_secret_provided(@secret) - @signed + @signed ||= SignedCookieJar.new(self, @secret) end def write(headers) @@ -229,9 +228,7 @@ module ActionDispatch class PermanentCookieJar < CookieJar #:nodoc: def initialize(parent_jar, secret) - @parent_jar = parent_jar - @secret = secret - @signed = @secret && SignedCookieJar.new(self, @secret) + @parent_jar, @secret = parent_jar, secret end def []=(key, options) @@ -247,8 +244,7 @@ module ActionDispatch end def signed - SignedCookieJar.ensure_secret_provided(@secret) - @signed + @signed ||= SignedCookieJar.new(self, @secret) end def method_missing(method, *arguments, &block) @@ -261,8 +257,7 @@ module ActionDispatch SECRET_MIN_LENGTH = 30 # Characters def initialize(parent_jar, secret) - self.class.ensure_secret_provided(secret) - self.class.ensure_secret_length(secret) + ensure_secret_secure(secret) @parent_jar = parent_jar @verifier = ActiveSupport::MessageVerifier.new(secret) end @@ -294,7 +289,9 @@ module ActionDispatch protected - def self.ensure_secret_provided(secret) + # To prevent users from using something insecure like "Password" we make sure that the + # secret they've provided is at least 30 characters in length. + def ensure_secret_secure(secret) if secret.blank? raise ArgumentError, "A secret is required to generate an " + "integrity hash for cookie session data. Use " + @@ -302,11 +299,7 @@ module ActionDispatch "least #{SECRET_MIN_LENGTH} characters\"" + "in config/initializers/secret_token.rb" end - end - # To prevent users from using something insecure like "Password" we make sure that the - # secret they've provided is at least 30 characters in length. - def self.ensure_secret_length(secret) if secret.length < SECRET_MIN_LENGTH raise ArgumentError, "Secret should be something secure, " + "like \"#{ActiveSupport::SecureRandom.hex(16)}\". The value you " + -- cgit v1.2.3 From 03d561ad77085f17ba816ebec619a3d359b2164e Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 7 Apr 2011 09:26:04 -0300 Subject: Revert "Use freeze instead of close!" This reverts commit 29592a7f09dda2e7e1e0a915d9230fe6a9b5c0af. --- actionpack/lib/action_dispatch/middleware/cookies.rb | 7 +++++-- actionpack/lib/action_dispatch/middleware/flash.rb | 12 +++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) (limited to 'actionpack/lib/action_dispatch/middleware') diff --git a/actionpack/lib/action_dispatch/middleware/cookies.rb b/actionpack/lib/action_dispatch/middleware/cookies.rb index 3ed5f1055f..24ebb8fed7 100644 --- a/actionpack/lib/action_dispatch/middleware/cookies.rb +++ b/actionpack/lib/action_dispatch/middleware/cookies.rb @@ -115,10 +115,13 @@ module ActionDispatch @delete_cookies = {} @host = host @secure = secure + @closed = false @cookies = {} end - alias :closed? :frozen? + attr_reader :closed + alias :closed? :closed + def close!; @closed = true end # Returns the value of the cookie by +name+, or +nil+ if no such cookie exists. def [](name) @@ -327,7 +330,7 @@ module ActionDispatch [status, headers, body] ensure cookie_jar = ActionDispatch::Request.new(env).cookie_jar unless cookie_jar - cookie_jar.freeze + cookie_jar.close! end end end diff --git a/actionpack/lib/action_dispatch/middleware/flash.rb b/actionpack/lib/action_dispatch/middleware/flash.rb index e7090ecf1b..6eda1f31a7 100644 --- a/actionpack/lib/action_dispatch/middleware/flash.rb +++ b/actionpack/lib/action_dispatch/middleware/flash.rb @@ -43,9 +43,12 @@ module ActionDispatch class FlashNow #:nodoc: def initialize(flash) @flash = flash + @closed = false end - alias :closed? :frozen? + attr_reader :closed + alias :closed? :closed + def close!; @closed = true end def []=(k, v) raise ClosedError, :flash if closed? @@ -73,9 +76,12 @@ module ActionDispatch def initialize #:nodoc: super @used = Set.new + @closed = false end - alias :closed? :frozen? + attr_reader :closed + alias :closed? :closed + def close!; @closed = true end def []=(k, v) #:nodoc: raise ClosedError, :flash if closed? @@ -194,7 +200,7 @@ module ActionDispatch if !flash_hash.empty? || session.key?('flash') session["flash"] = flash_hash end - flash_hash.freeze + flash_hash.close! end if session.key?('flash') && session['flash'].empty? -- cgit v1.2.3 From 0e624ce9eb9056c8986621f3e6b7f5ca67b4cb12 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 7 Apr 2011 09:27:00 -0300 Subject: Cache flash now --- actionpack/lib/action_dispatch/middleware/flash.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/middleware') diff --git a/actionpack/lib/action_dispatch/middleware/flash.rb b/actionpack/lib/action_dispatch/middleware/flash.rb index 6eda1f31a7..0af86e4df9 100644 --- a/actionpack/lib/action_dispatch/middleware/flash.rb +++ b/actionpack/lib/action_dispatch/middleware/flash.rb @@ -112,7 +112,7 @@ module ActionDispatch # # Entries set via now are accessed the same way as standard entries: flash['my-key']. def now - FlashNow.new(self) + @now ||= FlashNow.new(self) end # Keeps either the entire current flash or a specific flash entry available for the next action: -- cgit v1.2.3 From 199c0bb338568c801792933594d618caf0cdcd4c Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 14 Apr 2011 11:27:27 -0700 Subject: generated session ids should be encoded as UTF-8 --- actionpack/lib/action_dispatch/middleware/session/abstract_store.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/middleware') diff --git a/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb b/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb index 64d3a87fd0..1a811ce1b1 100644 --- a/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb +++ b/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb @@ -29,7 +29,9 @@ module ActionDispatch end def generate_sid - ActiveSupport::SecureRandom.hex(16) + sid = ActiveSupport::SecureRandom.hex(16) + sid.encode!('UTF-8') if sid.respond_to?(:encode!) + sid end protected -- cgit v1.2.3 From d5ad92ced1786b742c3ecce3cb60d851c7200bc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 15 Apr 2011 20:09:39 +0200 Subject: Make static faster as we don't have to serve multiple paths anymore. --- .../lib/action_dispatch/middleware/static.rb | 43 ++++++++-------------- 1 file changed, 15 insertions(+), 28 deletions(-) (limited to 'actionpack/lib/action_dispatch/middleware') diff --git a/actionpack/lib/action_dispatch/middleware/static.rb b/actionpack/lib/action_dispatch/middleware/static.rb index c57f694c4d..348f7b86b8 100644 --- a/actionpack/lib/action_dispatch/middleware/static.rb +++ b/actionpack/lib/action_dispatch/middleware/static.rb @@ -2,25 +2,23 @@ require 'rack/utils' module ActionDispatch class FileHandler - def initialize(at, root) - @at, @root = at.chomp('/'), root.chomp('/') - @compiled_at = @at.blank? ? nil : /^#{Regexp.escape(at)}/ + def initialize(root) + @root = root.chomp('/') @compiled_root = /^#{Regexp.escape(root)}/ @file_server = ::Rack::File.new(@root) end def match?(path) path = path.dup - if !@compiled_at || path.sub!(@compiled_at, '') - full_path = path.empty? ? @root : File.join(@root, ::Rack::Utils.unescape(path)) - paths = "#{full_path}#{ext}" - matches = Dir[paths] - match = matches.detect { |m| File.file?(m) } - if match - match.sub!(@compiled_root, '') - match - end + full_path = path.empty? ? @root : File.join(@root, ::Rack::Utils.unescape(path)) + paths = "#{full_path}#{ext}" + + matches = Dir[paths] + match = matches.detect { |m| File.file?(m) } + if match + match.sub!(@compiled_root, '') + match end end @@ -39,9 +37,9 @@ module ActionDispatch class Static FILE_METHODS = %w(GET HEAD).freeze - def initialize(app, roots) + def initialize(app, path) @app = app - @file_handlers = create_file_handlers(roots) + @file_handler = FileHandler.new(path) end def call(env) @@ -49,24 +47,13 @@ module ActionDispatch method = env['REQUEST_METHOD'] if FILE_METHODS.include?(method) - @file_handlers.each do |file_handler| - if match = file_handler.match?(path) - env["PATH_INFO"] = match - return file_handler.call(env) - end + if match = @file_handler.match?(path) + env["PATH_INFO"] = match + return @file_handler.call(env) end end @app.call(env) end - - private - def create_file_handlers(roots) - roots = { '' => roots } unless roots.is_a?(Hash) - - roots.map do |at, root| - FileHandler.new(at, root) if File.exist?(root) - end.compact - end end end -- cgit v1.2.3