Class: Funnel::WebSocket::Headers

Inherits:
Object
  • Object
show all
Defined in:
lib/funnel/web_socket/headers.rb

Constant Summary collapse

PATH_PATTERN =
/^GET (\/[^\s]*) HTTP\/1\.1$/
HEADER_PATTERN =
/^([^:]+):\s*([^$]+)/

Class Method Summary collapse

Class Method Details

.encode(data) ⇒ Object

encode ruby hash into HTTP style headers



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/funnel/web_socket/headers.rb', line 31

def self.encode data
	result = ""

	data.each_pair do |k,v|
		result << k.to_s
		result << ": "
		result << v.to_s
		result << "\r\n"
	end

	result << "\r\n"
	result
end

.parse(data) ⇒ Object

Parse http style headers into a ruby hash



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/funnel/web_socket/headers.rb', line 10

def self.parse data
	lines = data.split("\r\n")
    line  = lines.shift

	headers = {}

    if line =~ PATH_PATTERN
      headers[:path] = PATH_PATTERN.match(line)[1]
    else
      throw "Unrecognized Header!"
    end

	lines.each do |line|
		kvp = HEADER_PATTERN.match(line)
		headers[kvp[1].strip.downcase.to_sym] = kvp[2].strip
	end

	headers
end