Permalink
Browse files

Fix small bugs

  • Loading branch information...
1 parent 5678d3b commit e28a826a5535a8739525b8f5fa649b8905de76ec brainopia committed Oct 1, 2010
Showing with 23 additions and 16 deletions.
  1. +23 −16 lib/rack/contrib/common_cookies.rb
@@ -1,34 +1,41 @@
module Rack
- # Rack middleware to use the same cookies inside domain and all subdomains.
+ # Rack middleware to use common cookies across domain and subdomains.
class CommonCookies
- DOMAIN_REGEXP = /([^.]*)\.([^.]*|..\...|...\...)$/
- LOCALHOST_OR_IP_REGEXP = /^([\d.]+(:\d+)?|localhost)$/
+ DOMAIN_REGEXP = /([^.]*)\.([^.]*|..\...|...\...|..\....)$/
+ LOCALHOST_OR_IP_REGEXP = /^([\d.]+|localhost)$/
+ PORT = /:\d+$/
def initialize(app)
@app = app
end
- def domain(env)
- env['HTTP_HOST'] =~ DOMAIN_REGEXP
- ".#{$1}.#{$2}"
+ def call(env)
+ @app.call(env).tap do |(status, headers, response)|
+ @host = env['HTTP_HOST'].sub PORT, ''
+ share_cookie headers
+ end
end
- def update_domain(env, headers)
- headers['Set-Cookie'] &&= rewrite cookies if env['HTTP_HOST'] !~ LOCALHOST_OR_IP_REGEXP
- end
+ private
- def call(env)
- @app.call(env).tap {|(status, headers, response)| update_domain(env, headers) }
+ def domain
+ @domain ||= begin
+ @host =~ DOMAIN_REGEXP
+ ".#{$1}.#{$2}"
+ end
end
- private
+ def share_cookie(headers)
+ headers['Set-Cookie'] &&= common_cookie(headers) if @host !~ LOCALHOST_OR_IP_REGEXP
+ end
- def cookies
- Array[*headers['Set-Cookie']].join "\n"
+ def cookie(headers)
+ cookies = headers['Set-Cookie']
+ cookies.is_a?(Array) ? cookies.join("\n") : cookies
end
- def rewrite(cookies)
- *set_cookies.gsub(/; domain=[^;]*/, '').gsub(/$/, "; domain=#{domain(env)}").split("\n")
+ def common_cookie(headers)
+ cookie(headers).gsub(/; domain=[^;]*/, '').gsub(/$/, "; domain=#{domain}")
end
end
end

0 comments on commit e28a826

Please sign in to comment.