Class: Protocol::HTTP::Header::Cookie

Inherits:
Array
  • Object
show all
Defined in:
lib/protocol/http/header/cookie.rb

Overview

The cookie header contains stored HTTP cookies previously sent by the server with the set-cookie header.

It is used by clients to send key-value pairs representing stored cookies back to the server. Multiple cookies within a single Cookie header are joined with "; " per RFC 6265.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ Cookie

Initializes the cookie header with the given values.



40
41
42
43
44
45
46
# File 'lib/protocol/http/header/cookie.rb', line 40

def initialize(value = nil)
	super()
	
	if value
		self.concat(value)
	end
end

Class Method Details

.coerce(value) ⇒ Object

Coerces a value into a parsed header object.



28
29
30
31
32
33
34
35
# File 'lib/protocol/http/header/cookie.rb', line 28

def self.coerce(value)
	case value
	when Array
		self.new(value.map(&:to_s))
	else
		self.parse(value.to_s)
	end
end

.parse(value) ⇒ Object

Parses a raw header value.



20
21
22
# File 'lib/protocol/http/header/cookie.rb', line 20

def self.parse(value)
	self.new([value])
end

.trailer?Boolean

Whether this header is acceptable in HTTP trailers. Cookie headers should not appear in trailers as they contain state information needed early in processing.

Returns:

  • (Boolean)


70
71
72
# File 'lib/protocol/http/header/cookie.rb', line 70

def self.trailer?
	false
end

Instance Method Details

#to_hObject

Parses the cookie header into a hash of cookie names and their corresponding cookie objects.



51
52
53
54
55
56
57
58
59
60
# File 'lib/protocol/http/header/cookie.rb', line 51

def to_h
	cookies = self.flat_map do |string|
		# Each header field can contain multiple cookie pairs separated by semicolons:
		string.split(/\s*;\s*/).map do |pair|
			HTTP::Cookie.parse(pair)
		end
	end
	
	cookies.map{|cookie| [cookie.name, cookie]}.to_h
end

#to_sObject

Serializes the cookie header by joining individual cookie strings with "; " per RFC 6265.



63
64
65
# File 'lib/protocol/http/header/cookie.rb', line 63

def to_s
	join("; ")
end