Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add subdomain_cookies middleware

  • Loading branch information...
commit 3c88ef7c543615da70be490db91f2f5eab7b5035 1 parent e9eb1f5
@brainopia brainopia authored
Showing with 33 additions and 0 deletions.
  1. +33 −0 lib/rack/contrib/subdomain_cookies.rb
View
33 lib/rack/contrib/subdomain_cookies.rb
@@ -0,0 +1,33 @@
+module Rack
+ class SubdomainCookies
+ DOMAIN_REGEXP = /([^.]*)\.([^.]*|..\...|...\...)$/
+ LOCALHOST_OR_IP_REGEXP = /^([\d.]+(:\d+)?|localhost)$/
+
+ def initialize(app)
+ @app = app
+ end
+
+ def domain(env)
+ env['HTTP_HOST'] =~ DOMAIN_REGEXP
+ ".#{$1}.#{$2}"
+ end
+
+ def update_domain(env, headers)
+ headers['Set-Cookie'] &&= rewrite cookies if env['HTTP_HOST'] !~ LOCALHOST_OR_IP_REGEXP
+ end
+
+ def call(env)
+ @app.call(env).tap {|(status, headers, response)| update_domain(env, headers) }
+ end
+
+ private
+
+ def cookies
+ Array[*headers['Set-Cookie']].join "\n"
+ end
+
+ def rewrite(cookies)
+ *set_cookies.gsub(/; domain=[^;]*/, '').gsub(/$/, "; domain=#{domain(env)}").split("\n")
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.