Class: CookieCollection
- Inherits:
-
Object
- Object
- CookieCollection
- Defined in:
- lib/cookie_collection.rb
Overview
Synopsis
CookieCollection should be utilized by clients that need HTTP cookie management. This class assists in storing and parsing raw cookies and returning non-expired cookies for the cookie’s originating server.
NOTE This class is largely unimplemented and only stores and returns a single raw cookie header without any processing or domain discretion.
Implementation Notes
-
RFC 2109, version 1 cookies (tools.ietf.org/html/rfc2109)
-
Uses Netscapes’ “Expires” instead of RFC’s “Max-Age” attribute. Expires is an RFC 2822 date.
– TODO Fully implement this class. ++
Instance Method Summary collapse
- #clear ⇒ Object
- #cookie_header(domain, path) ⇒ Object
-
#cookie_header_data(domain, path) ⇒ Object
– Don’t return expired cookies.
-
#initialize ⇒ CookieCollection
constructor
– XXX do we want to accept CGI::Cookie or raw cookie strings and convert to CGI::Cookie? ++.
-
#store(cookie) ⇒ Object
– Need to parse set-cookie response string and instantiate a CGI::Cookie for each and store in @cookies.
Constructor Details
#initialize ⇒ CookieCollection
– XXX do we want to accept CGI::Cookie or raw cookie strings and convert to CGI::Cookie? ++
28 29 30 |
# File 'lib/cookie_collection.rb', line 28 def initialize @cookies = nil end |
Instance Method Details
#clear ⇒ Object
44 45 46 |
# File 'lib/cookie_collection.rb', line 44 def clear @cookies = nil end |
#cookie_header(domain, path) ⇒ Object
81 82 83 |
# File 'lib/cookie_collection.rb', line 81 def domain, path "Cookie: " + (domain, path) end |
#cookie_header_data(domain, path) ⇒ Object
– Don’t return expired cookies. Commas seperate cookie values. Only compile cookies that have applicable domain and path. Cookie data needs to be ordered by path.
Cookie header to send in an HTTP request to the originating server.
Cookie Header Syntax:
cookie = "Cookie:" cookie-version
1*((";" | ",") cookie-value)
cookie-value = NAME "=" VALUE [";" path] [";" domain]
cookie-version = "$Version" "=" value
NAME = attr
VALUE = value
path = "$Path" "=" value
domain = "$Domain" "=" value
Cookie Header Example:
$Version=1; Val1="data1"; $Path="/dir"; Val2="data2"; $Path="/"
++
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/cookie_collection.rb', line 70 def domain, path @cookies #@cookies.collect do |cookie| # next if Time.rfc2822(cookie.expires) < Time.now # next if cookie.domain != domain # next if cookie.path !~ /^#{path}/ # "Cookie: #{cookie}" if cookie.domain == domain #end end |
#store(cookie) ⇒ Object
– Need to parse set-cookie response string and instantiate a CGI::Cookie for each and store in @cookies.
Example of 2 cookies returned by WEBrick:
“VAL1=Data1; Version=1; Expires=Sat, 11 Oct 2008 11:17:54 GMT, VAL2=Data2; Max-Age=3600” ++
40 41 42 |
# File 'lib/cookie_collection.rb', line 40 def store @cookies = end |