Class: Rack::Policy::CookieLimiter
- Inherits:
-
Object
- Object
- Rack::Policy::CookieLimiter
- 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 =
"HTTP_COOKIE".freeze
- SET_COOKIE =
"Set-Cookie".freeze
- CACHE_CONTROL =
"Cache-Control".freeze
- CONSENT_TOKEN =
"cookie_limiter".freeze
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#body ⇒ Object
readonly
HTTP message.
-
#env ⇒ Object
readonly
The environment of the request.
-
#headers ⇒ Object
readonly
HTTP message.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#status ⇒ Object
readonly
HTTP message.
Instance Method Summary collapse
-
#accepts?(request) ⇒ Boolean
Identifies the approval of cookie policy inside rack app.
-
#allowed?(request) ⇒ Boolean
Returns ‘false` if the cookie policy disallows cookie storage for a given request, or `true` otherwise.
- #call(env) ⇒ Object
- #call!(env) ⇒ Object
- #consent_token ⇒ Object
- #expires ⇒ Object
-
#finish ⇒ Object
Finish http response with proper headers.
-
#initialize(app, options = {}) ⇒ CookieLimiter
constructor
A new instance of CookieLimiter.
Constructor Details
#initialize(app, options = {}) ⇒ CookieLimiter
Returns a new instance of CookieLimiter.
24 25 26 |
# File 'lib/rack/policy/cookie_limiter.rb', line 24 def initialize(app, ={}) @app, @options = app, end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
14 15 16 |
# File 'lib/rack/policy/cookie_limiter.rb', line 14 def app @app end |
#body ⇒ Object (readonly)
HTTP message
20 21 22 |
# File 'lib/rack/policy/cookie_limiter.rb', line 20 def body @body end |
#env ⇒ Object (readonly)
The environment of the request
17 18 19 |
# File 'lib/rack/policy/cookie_limiter.rb', line 17 def env @env end |
#headers ⇒ Object (readonly)
HTTP message
20 21 22 |
# File 'lib/rack/policy/cookie_limiter.rb', line 20 def headers @headers end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/rack/policy/cookie_limiter.rb', line 14 def @options end |
#status ⇒ Object (readonly)
HTTP message
20 21 22 |
# File 'lib/rack/policy/cookie_limiter.rb', line 20 def status @status end |
Instance Method Details
#accepts?(request) ⇒ Boolean
Identifies the approval of cookie policy inside rack app.
52 53 54 55 56 57 58 59 |
# File 'lib/rack/policy/cookie_limiter.rb', line 52 def accepts?(request) if ( request..has_key?(.to_s) ) @env['rack-policy.consent'] = 'true' else @env.delete(HTTP_COOKIE) if @env[HTTP_COOKIE] @env['rack-policy.consent'] = nil end end |
#allowed?(request) ⇒ Boolean
Returns ‘false` if the cookie policy disallows cookie storage for a given request, or `true` otherwise.
64 65 66 67 68 69 70 71 |
# File 'lib/rack/policy/cookie_limiter.rb', line 64 def allowed?(request) if ( request..has_key?(.to_s) || .has_key?(.to_s) ) true else false end end |
#call(env) ⇒ Object
36 37 38 |
# File 'lib/rack/policy/cookie_limiter.rb', line 36 def call(env) dup.call!(env) end |
#call!(env) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/rack/policy/cookie_limiter.rb', line 40 def call!(env) @env = env request = Rack::Request.new(env) accepts?(request) @status, @headers, @body = @app.call(env) response = Rack::Response.new body, status, headers (request, response) unless allowed?(request) finish end |
#consent_token ⇒ Object
28 29 30 |
# File 'lib/rack/policy/cookie_limiter.rb', line 28 def @consent_token ||= [:consent_token] || CONSENT_TOKEN end |
#expires ⇒ Object
32 33 34 |
# File 'lib/rack/policy/cookie_limiter.rb', line 32 def expires Time.parse([:expires]) if [:expires] end |
#finish ⇒ Object
Finish http response with proper headers
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rack/policy/cookie_limiter.rb', line 74 def finish 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 |