Class: Rack::Policy::CookieLimiter

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/rack/policy/cookie_limiter.rb

Overview

This is the class for limiting cookie storage on client machine.

Constant Summary collapse

"HTTP_COOKIE".freeze
"Set-Cookie".freeze
CACHE_CONTROL =
"Cache-Control".freeze
"cookie_limiter".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ CookieLimiter

Returns a new instance of CookieLimiter.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :consent_token (String)


19
20
21
# File 'lib/rack/policy/cookie_limiter.rb', line 19

def initialize(app, options={})
  @app, @options = app, options
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



14
15
16
# File 'lib/rack/policy/cookie_limiter.rb', line 14

def app
  @app
end

#bodyObject

Returns the value of attribute body.



15
16
17
# File 'lib/rack/policy/cookie_limiter.rb', line 15

def body
  @body
end

#headersObject

Returns the value of attribute headers.



15
16
17
# File 'lib/rack/policy/cookie_limiter.rb', line 15

def headers
  @headers
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/rack/policy/cookie_limiter.rb', line 14

def options
  @options
end

#statusObject

Returns the value of attribute status.



15
16
17
# File 'lib/rack/policy/cookie_limiter.rb', line 15

def status
  @status
end

Instance Method Details

#allowed?(request) ⇒ Boolean

Returns ‘false` if the cookie policy disallows cookie storage for a given request, or `true` otherwise.

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
# File 'lib/rack/policy/cookie_limiter.rb', line 46

def allowed?(request)
  if ( request.cookies.has_key?(consent_token.to_s) ||
       parse_cookies.has_key?(consent_token.to_s) )
    true
  else
    false
  end
end

#call(env) ⇒ Object



31
32
33
# File 'lib/rack/policy/cookie_limiter.rb', line 31

def call(env)
  dup.call!(env)
end

#call!(env) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/rack/policy/cookie_limiter.rb', line 35

def call!(env)
  self.status, self.headers, self.body = @app.call(env)
  request = Rack::Request.new(env)
  response = Rack::Response.new body, status, headers
  clear_cookies!(request, response) unless allowed?(request)
  finish(env)
end


23
24
25
# File 'lib/rack/policy/cookie_limiter.rb', line 23

def consent_token
  @consent_token ||= options[:consent_token] || CONSENT_TOKEN
end

#expiresObject



27
28
29
# File 'lib/rack/policy/cookie_limiter.rb', line 27

def expires
  Time.parse(options[:expires]) if options[:expires]
end

#finish(env) ⇒ Object

Finish http response with proper headers



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rack/policy/cookie_limiter.rb', line 56

def finish(env)
  if [204, 304].include?(status.to_i) || (status.to_i / 100 == 1)
    headers.delete "Content-Length"
    headers.delete "Content-Type"
    [status.to_i, headers, []]
  elsif env['REQUEST_METHOD'] == 'HEAD'
    [status.to_i, headers, []]
  else
    [status.to_i, headers, body]
  end
end