Class: Strelka::Cookie

Inherits:
Object
  • Object
show all
Extended by:
Loggability
Includes:
Strelka::Constants::CookieHeader
Defined in:
lib/strelka/cookie.rb

Overview

The Strelka::Cookie class, a class for parsing and generating HTTP cookies.

Large parts of this code were copied from the Webrick::Cookie class in the Ruby standard library. The copyright statements for that module are:

Author: IPR -- Internet Programming with Ruby -- writers
Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
Copyright (c) 2002 Internet Programming with Ruby writers. All rights
reserved.

References:

Constant Summary collapse

'%a, %d %b %Y %H:%M:%S GMT'
UNIT_SECONDS =

Number of seconds in the various offset types

{
	's' => 1,
	'm' => 60,
	'h' => 60*60,
	'd' => 60*60*24,
	'M' => 60*60*24*30,
	'y' => 60*60*24*365,
}

Constants included from Strelka::Constants::CookieHeader

Strelka::Constants::CookieHeader::COOKIE_NAME, Strelka::Constants::CookieHeader::COOKIE_OCTET, Strelka::Constants::CookieHeader::COOKIE_PAIR, Strelka::Constants::CookieHeader::COOKIE_VALUE, Strelka::Constants::CookieHeader::COOKIE_VERSION, Strelka::Constants::CookieHeader::CRLF, Strelka::Constants::CookieHeader::CTL, Strelka::Constants::CookieHeader::DQUOTE, Strelka::Constants::CookieHeader::OBS_FOLD, Strelka::Constants::CookieHeader::OWS, Strelka::Constants::CookieHeader::SEPARATORS, Strelka::Constants::CookieHeader::TOKEN, Strelka::Constants::CookieHeader::WSP

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, options = {}) ⇒ Cookie

Create a new Strelka::Cookie object with the specified name and values. Valid options are:

\version

The cookie version. 0 (the default) is fine for most uses

\domain

The domain the cookie belongs to.

\path

The path the cookie applies to.

\secure

The cookie's 'secure' flag.

\expires

The cookie's expiration (a Time object). See expires= for valid values.

\max_age

The lifetime of the cookie, in seconds.

\httponly

HttpOnly flag.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/strelka/cookie.rb', line 116

def initialize( name, value, options={} )
	options ||= {}
	# self.log.debug "New cookie: %p = %p (%p)" % [ name, value, options ]

	@name     = name
	@value    = value

	@domain   = nil
	@path     = nil
	@secure   = false
	@httponly = false
	@max_age  = nil
	@expires  = nil
	@version  = 0

	# self.log.debug "  setting options: %p" % [ options ]
	options.each do |meth, val|
		self.__send__( "#{meth}=", val )
	end
end

Instance Attribute Details

#domainObject

The domain the cookie belongs to



167
168
169
# File 'lib/strelka/cookie.rb', line 167

def domain
  @domain
end

#expiresObject

The cookie's expiration (a Time object)



179
180
181
# File 'lib/strelka/cookie.rb', line 179

def expires
  @expires
end

#httponlyObject

The cookie's HttpOnly flag



176
177
178
# File 'lib/strelka/cookie.rb', line 176

def httponly
  @httponly
end

#max_ageObject

The lifetime of the cookie, in seconds.



182
183
184
# File 'lib/strelka/cookie.rb', line 182

def max_age
  @max_age
end

#nameObject

The name of the cookie



158
159
160
# File 'lib/strelka/cookie.rb', line 158

def name
  @name
end

#pathObject

The path the cookie applies to



170
171
172
# File 'lib/strelka/cookie.rb', line 170

def path
  @path
end

#secure=(value) ⇒ Object (writeonly)

The cookie's 'secure' flag.



173
174
175
# File 'lib/strelka/cookie.rb', line 173

def secure=(value)
  @secure = value
end

#valueObject

The string value of the cookie



161
162
163
# File 'lib/strelka/cookie.rb', line 161

def value
  @value
end

#versionObject

The cookie version. 0 (the default) is fine for most uses



164
165
166
# File 'lib/strelka/cookie.rb', line 164

def version
  @version
end

Class Method Details

.dequote(string) ⇒ Object

Strip surrounding double quotes from a copy of the specified string and return it.



53
54
55
# File 'lib/strelka/cookie.rb', line 53

def self::dequote( string )
	return string.gsub( /^"|"$/, '' )
end

.parse(header) ⇒ Object

Parse the specified 'Cookie:' header value and return a Hash of one or more new Strelka::Cookie objects, keyed by name.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/strelka/cookie.rb', line 60

def self::parse( header )
	return {} if header.nil? or header.empty?
	self.log.debug "Parsing cookie header: %p" % [ header ]
	cookies = {}
	version = 0
	header = header.strip

	# "$Version" = value
	if m = COOKIE_VERSION.match( header )
		# self.log.debug "  Found cookie version %p" % [ m[:version] ]
		version = Integer( dequote(m[:version]) )
		header.slice!( COOKIE_VERSION )
	end

	# cookie-header = "Cookie:" OWS cookie-string OWS
	# cookie-string = cookie-pair *( ";" SP cookie-pair )
	header.split( /;\x20/ ).each do |cookie_pair|
		# self.log.debug "  parsing cookie-pair: %p" % [ cookie_pair ]
		match = cookie_pair.match( COOKIE_PAIR ) or
			raise Strelka::ParseError, "malformed cookie pair: %p" % [ cookie_pair ]

		# self.log.debug "  matched cookie: %p" % [ match ]
		name = match[:cookie_name]
		value = match[:cookie_value]
		value = self.dequote( value ) if value.start_with?( DQUOTE )
		value = nil if value.empty?

		cookies[ name.to_sym ] = new( name, value, :version => version )
	end

	return cookies
end

Instance Method Details

#binary_valueObject Also known as: wrapped_value

Fetch the cookie's data after un-base64ing it. This is just a convenience method for:

cookie.value.unpack( 'm' ).first


217
218
219
# File 'lib/strelka/cookie.rb', line 217

def binary_value
	return self.value.unpack( 'm' ).first
end

#binary_value=(data) ⇒ Object Also known as: wrapped_value=

Store the base64'ed data as the cookie value. This is just a convenience method for:

cookie.value = [data].pack('m').strip


205
206
207
208
# File 'lib/strelka/cookie.rb', line 205

def binary_value=( data )
	# self.log.debug "Setting cookie value to base64ed %p" % [ data ]
	self.value = [ data ].pack( 'm' ).strip
end

#eql?(other_cookie) ⇒ Boolean

Return true if other_cookie has the same name as the receiver.

Returns:

  • (Boolean)


320
321
322
323
# File 'lib/strelka/cookie.rb', line 320

def eql?( other_cookie )
	# self.log.debug "Comparing %p with other cookie: %p" % [ self, other_cookie ]
	return (self.name == other_cookie.name) ? true : false
end

#expire!Object

Set the cookie expiration to a time in the past



297
298
299
# File 'lib/strelka/cookie.rb', line 297

def expire!
	self.expires = Time.at(0)
end

#hashObject

Generate a Fixnum hash value for this object. Uses the hash of the cookie's name.



327
328
329
# File 'lib/strelka/cookie.rb', line 327

def hash
	return self.name.to_s.hash
end

#httponly?Boolean

Returns true if the 'httponly' flag is set

Returns:

  • (Boolean)


230
231
232
# File 'lib/strelka/cookie.rb', line 230

def httponly?
	return @httponly ? true : false
end

#optionsObject

Return the cookie's options as a hash.



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/strelka/cookie.rb', line 139

def options
	return {
		domain:   self.domain,
		path:     self.path,
		secure:   self.secure?,
		httponly: self.httponly?,
		expires:  self.expires,
		max_age:  self.max_age,
		version:  self.version,
	}
end

#secure?Boolean

Returns true if the secure flag is set

Returns:

  • (Boolean)


224
225
226
# File 'lib/strelka/cookie.rb', line 224

def secure?
	return @secure ? true : false
end

#to_sObject

Return the cookie as a String



303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/strelka/cookie.rb', line 303

def to_s
	rval = "%s=%s" % [ self.name, self.make_valuestring ]

	rval << make_field( "Version", self.version ) if self.version.nonzero?
	rval << make_field( "Domain", self.domain )
	rval << make_field( "Expires", make_cookiedate(self.expires) ) if self.expires
	rval << make_field( "Max-Age", self.max_age )
	rval << make_field( "Path", self.path )

	rval << '; ' << 'HttpOnly' if self.httponly?
	rval << '; ' << 'Secure' if self.secure?

	return rval
end