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)


67
68
69
# File 'lib/protocol/http/header/cookie.rb', line 67

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
# File 'lib/protocol/http/header/cookie.rb', line 51

def to_h
	cookies = self.collect do |string|
		HTTP::Cookie.parse(string)
	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.



60
61
62
# File 'lib/protocol/http/header/cookie.rb', line 60

def to_s
	join("; ")
end