Class: Rack::PotentiallySecureCookies

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/potentially_secure_cookies.rb

Constant Summary collapse

VERSION =
'1.0.1'

Instance Method Summary collapse

Constructor Details

#initialize(app, cookies) ⇒ PotentiallySecureCookies

Returns a new instance of PotentiallySecureCookies.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/rack/potentially_secure_cookies.rb', line 5

def initialize(app, cookies)
  @app = app

  # All in the name to make this as fast as possible anything that
  # could be used in multiple requests have been defined here.
  _cookies = "^((#{cookies.join(')|(')}))".freeze
  @configured_cookies = /#{_cookies}/
  @cookies_with_secure = /(#{_cookies}.*?)(; [Ss]ecure)(.*)$/
  @cookies_without_secure = /(#{_cookies}(?!.*[Ss]ecure).*)/
  @secure = /; [Ss]ecure/
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rack/potentially_secure_cookies.rb', line 17

def call(env)
  status, headers, body = @app.call(env)

  if headers['Set-Cookie'] && @configured_cookies.match(headers['Set-Cookie'])
    request = Rack::Request.new(env)

    if request.ssl?
      headers['Set-Cookie'].gsub!(@cookies_without_secure, '\1; Secure')
    else
      headers['Set-Cookie'].gsub!(@cookies_with_secure, '\1\3')
    end
  end

  [status, headers, body]
end