Module Rufus::Verbs::CookieMixin

  1. lib/rufus/verbs/cookies.rb

Cookies related methods

www.ietf.org/rfc/rfc2109.txt

Attributes

cookies [R] making the cookie jar available

Protected instance methods

cookie_acceptable? (opts, response, cookie)

Checks if the cookie is acceptable in the context of the request that sent it.

[show source]
     # File lib/rufus/verbs/cookies.rb, line 117
117:     def cookie_acceptable? (opts, response, cookie)
118: 
119:       # reject if :
120:       #
121:       # * The value for the Path attribute is not a prefix of the
122:       #   request-URI.
123:       # * The value for the Domain attribute contains no embedded dots
124:       #   or does not start with a dot.
125:       # * The value for the request-host does not domain-match the
126:       #   Domain attribute.
127:       # * The request-host is a FQDN (not IP address) and has the form
128:       #   HD, where D is the value of the Domain attribute, and H is a
129:       #   string that contains one or more dots.
130: 
131:       cdomain = cookie.domain
132: 
133:       if cdomain
134: 
135:         return false unless cdomain.index '.'
136:         return false if cdomain[0, 1] != '.'
137: 
138:         h, d = split_host(opts[:host])
139:         return false if d != cdomain
140:       end
141: 
142:       #path = opts[:path]
143:       path = response.request.path
144: 
145:       cpath = cookie.path || "/"
146: 
147:       return false if path[0..cpath.length-1] != cpath
148: 
149:       true
150:     end
mention_cookies (request, opts)

Places the ‘Cookie’ header in the request if appropriate.

(This method will have no effect if the EndPoint is not tracking cookies)

[show source]
     # File lib/rufus/verbs/cookies.rb, line 158
158:     def mention_cookies (request, opts)
159: 
160:       return unless @cookies
161: 
162:       cs = @cookies.fetch_cookies opts[:host], opts[:path]
163: 
164:       request['Cookie'] = cs.collect { |c| c.to_header_s }.join(",")
165:     end
parse_cookies (response)

Parses the HTTP response for a potential ‘Set-Cookie’ header, parses and returns it as a hash.

[show source]
    # File lib/rufus/verbs/cookies.rb, line 71
71:     def parse_cookies (response)
72: 
73:       c = response['Set-Cookie']
74:       return nil unless c
75:       Cookie.parse_set_cookies c
76:     end
prepare_cookie_jar ()

Prepares the instance variable @cookies for storing cooking for this endpoint.

Reads the :cookies endpoint option for determining the size of the cookie jar (77 by default).

[show source]
    # File lib/rufus/verbs/cookies.rb, line 55
55:     def prepare_cookie_jar
56: 
57:       o = @opts[:cookies]
58: 
59:       return unless o and o != false
60: 
61:       s = o.to_s.to_i
62:       s = 77 if s < 1
63: 
64:       @cookies = CookieJar.new s
65:     end
register_cookies (response, opts)

(This method will have no effect if the EndPoint is not tracking cookies)

Registers a potential cookie set by the server.

[show source]
     # File lib/rufus/verbs/cookies.rb, line 84
 84:     def register_cookies (response, opts)
 85: 
 86:       return unless @cookies
 87: 
 88:       cs = parse_cookies response
 89: 
 90:       return unless cs
 91: 
 92:       # "The origin server effectively ends a session by
 93:       #  sending the client a Set-Cookie header with Max-Age=0"
 94: 
 95:       cs.each do |c|
 96: 
 97:         host = opts[:host]
 98:         path = opts[:path]
 99:         cpath = c.path || "/"
100: 
101:         next unless cookie_acceptable?(opts, response, c)
102: 
103:         domain = c.domain || host
104: 
105:         if c.max_age == 0
106:           @cookies.remove_cookie domain, path, c
107:         else
108:           @cookies.add_cookie domain, path, c
109:         end
110:       end
111:     end