Class: LibWebSocket::Response

Inherits:
Message
  • Object
show all
Defined in:
lib/libwebsocket/response.rb

Overview

Construct or parse a WebSocket response.

Instance Attribute Summary collapse

Attributes inherited from Message

#challenge, #checksum, #error, #fields, #host, #origin, #subprotocol, #version

Attributes included from Stateful

#state

Instance Method Summary collapse

Methods inherited from Message

#field, #initialize

Methods included from Stateful

#done, #done?, #state?

Constructor Details

This class inherits a constructor from LibWebSocket::Message

Instance Attribute Details

#cookiesObject

Returns the value of attribute cookies.



5
6
7
# File 'lib/libwebsocket/response.rb', line 5

def cookies
  @cookies
end

#key1Object

Returns the value of attribute key1.



5
6
7
# File 'lib/libwebsocket/response.rb', line 5

def key1
  @key1
end

#key2Object

Returns the value of attribute key2.



5
6
7
# File 'lib/libwebsocket/response.rb', line 5

def key2
  @key2
end

#locationObject

Returns the value of attribute location.



5
6
7
# File 'lib/libwebsocket/response.rb', line 5

def location
  @location
end

#resource_nameObject

Returns the value of attribute resource_name.



5
6
7
# File 'lib/libwebsocket/response.rb', line 5

def resource_name
  @resource_name
end

#secureObject

Returns the value of attribute secure.



5
6
7
# File 'lib/libwebsocket/response.rb', line 5

def secure
  @secure
end

Instance Method Details

#cookie=(hash) ⇒ Object

Build cookies from hash

See Also:



137
138
139
# File 'lib/libwebsocket/response.rb', line 137

def cookie=(hash)
  self.cookies.push self.build_cookie(hash)
end

#number1Object

Draft 76 number 1 reader



142
143
144
# File 'lib/libwebsocket/response.rb', line 142

def number1
  self.number('number1','key1')
end

#number1=(val) ⇒ Object

Draft 76 number 1 writter



146
147
148
# File 'lib/libwebsocket/response.rb', line 146

def number1=(val)
  self.number('number1','key1',val)
end

#number2Object

Draft 76 number 2 reader



150
151
152
# File 'lib/libwebsocket/response.rb', line 150

def number2
  self.number('number2','key2')
end

#number2=(val) ⇒ Object

Draft 76 number 2 writter



154
155
156
# File 'lib/libwebsocket/response.rb', line 154

def number2=(val)
  self.number('number2','key2',val)
end

#parse(string) ⇒ Object

Parse a WebSocket response.

Examples:

Parser

res = LibWebSocket::Response.new;
res.parse("HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a")
res.parse("Upgrade: WebSocket\x0d\x0a")
res.parse("Connection: Upgrade\x0d\x0a")
res.parse("Sec-WebSocket-Origin: file://\x0d\x0a")
res.parse("Sec-WebSocket-Location: ws://example.com/demo\x0d\x0a")
res.parse("\x0d\x0a")
res.parse("0st3Rl&q-2ZU^weu")

See Also:



18
19
20
# File 'lib/libwebsocket/response.rb', line 18

def parse(string)
  super
end

#to_rackObject

Construct a WebSocket response in rack format.

Examples:

Construct

res = LibWebSocket::Response.new(
  :host          => 'example.com',
  :resource_name => '/demo',
  :origin        => 'file://',
  :number1       => 777_007_543,
  :number2       => 114_997_259,
  :challenge     => "\x47\x30\x22\x2D\x5A\x3F\x47\x58"
)
res.to_rack # [ 101,
            # {
            #   'Upgrade'                => 'WebSocket'
            #   'Connection'             => 'Upgrade'
            #   'Sec-WebSocket-Origin'   => 'file://'
            #   'Sec-WebSocket-Location' => 'ws://example.com/demo'
            #   'Content-Length'         => 16
            # },
            # [ 0st3Rl&q-2ZU^weu ] ]


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/libwebsocket/response.rb', line 98

def to_rack
  status = 101
  hash = {}
  body = ''

  hash = {'Upgrade' => 'WebSocket', 'Connection' => 'Upgrade'}

  raise 'host is required' unless self.host

  location = self.build_url(
      :host => self.host,
      :secure => self.secure,
      :resource_name => self.resource_name
  )
  origin = self.origin || 'http://' + location.host

  if self.version <= 75
      hash.merge!('WebSocket-Protocol' => self.subprotocol) if self.subprotocol
      hash.merge!('WebSocket-Origin'   => origin)
      hash.merge!('WebSocket-Location' => location.to_s)
  else
      hash.merge!('Sec-WebSocket-Protocol' => self.subprotocol) if self.subprotocol
      hash.merge!('Sec-WebSocket-Origin'   => origin)
      hash.merge!('Sec-WebSocket-Location' => location.to_s)
  end

  unless self.cookies.empty?
    hash.merge!('Set-Cookie' => self.cookies.collect(&:to_s).join(','))
  end

  body = self.checksum if self.version > 75

  hash.merge!('Content-Length' => body.length.to_s)

  return [ status, hash, [ body ]]
end

#to_sObject

Construct a WebSocket response in string format.

Examples:

Construct

res = LibWebSocket::Response.new(
  :host          => 'example.com',
  :resource_name => '/demo',
  :origin        => 'file://',
  :number1       => 777_007_543,
  :number2       => 114_997_259,
  :challenge     => "\x47\x30\x22\x2D\x5A\x3F\x47\x58"
)
res.to_s # HTTP/1.1 101 WebSocket Protocol Handshake
         # Upgrade: WebSocket
         # Connection: Upgrade
         # Sec-WebSocket-Origin: file://
         # Sec-WebSocket-Location: ws://example.com/demo
         #
         # 0st3Rl&q-2ZU^weu


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/libwebsocket/response.rb', line 39

def to_s
  string = ''

  string += "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a"

  string += "Upgrade: WebSocket\x0d\x0a"
  string += "Connection: Upgrade\x0d\x0a"

  raise 'host is required' unless self.host

  location = self.build_url(
      :host => self.host,
      :secure => self.secure,
      :resource_name => self.resource_name
  )
  origin = self.origin || 'http://' + location.host

  if self.version <= 75
      string += 'WebSocket-Protocol: ' + self.subprotocol + "\x0d\x0a" if self.subprotocol
      string += 'WebSocket-Origin: ' + origin + "\x0d\x0a"
      string += 'WebSocket-Location: ' + location.to_s + "\x0d\x0a"
  else
      string += 'Sec-WebSocket-Protocol: ' + self.subprotocol + "\x0d\x0a" if self.subprotocol
      string += 'Sec-WebSocket-Origin: ' + origin + "\x0d\x0a"
      string += 'Sec-WebSocket-Location: ' + location.to_s + "\x0d\x0a"
  end

  unless self.cookies.empty?
      string += 'Set-Cookie: '
      string += self.cookies.collect(&:to_s).join(',')
      string += "\x0d\x0a"
  end

  string += "\x0d\x0a"

  string += self.checksum if self.version > 75

  return string
end