Class: LibWebSocket::Response
- Defined in:
- lib/libwebsocket/response.rb
Overview
Construct or parse a WebSocket response.
Instance Attribute Summary collapse
-
#cookies ⇒ Object
Returns the value of attribute cookies.
-
#key1 ⇒ Object
Returns the value of attribute key1.
-
#key2 ⇒ Object
Returns the value of attribute key2.
-
#location ⇒ Object
Returns the value of attribute location.
-
#resource_name ⇒ Object
Returns the value of attribute resource_name.
-
#secure ⇒ Object
Returns the value of attribute secure.
Attributes inherited from Message
#challenge, #checksum, #error, #fields, #host, #origin, #subprotocol, #version
Attributes included from Stateful
Instance Method Summary collapse
-
#cookie=(hash) ⇒ Object
Build cookies from hash.
-
#number1 ⇒ Object
Draft 76 number 1 reader.
-
#number1=(val) ⇒ Object
Draft 76 number 1 writter.
-
#number2 ⇒ Object
Draft 76 number 2 reader.
-
#number2=(val) ⇒ Object
Draft 76 number 2 writter.
-
#parse(string) ⇒ Object
Parse a WebSocket response.
-
#to_rack ⇒ Object
Construct a WebSocket response in rack format.
-
#to_s ⇒ Object
Construct a WebSocket response in string format.
Methods inherited from Message
Methods included from Stateful
Constructor Details
This class inherits a constructor from LibWebSocket::Message
Instance Attribute Details
#cookies ⇒ Object
Returns the value of attribute cookies.
5 6 7 |
# File 'lib/libwebsocket/response.rb', line 5 def @cookies end |
#key1 ⇒ Object
Returns the value of attribute key1.
5 6 7 |
# File 'lib/libwebsocket/response.rb', line 5 def key1 @key1 end |
#key2 ⇒ Object
Returns the value of attribute key2.
5 6 7 |
# File 'lib/libwebsocket/response.rb', line 5 def key2 @key2 end |
#location ⇒ Object
Returns the value of attribute location.
5 6 7 |
# File 'lib/libwebsocket/response.rb', line 5 def location @location end |
#resource_name ⇒ Object
Returns the value of attribute resource_name.
5 6 7 |
# File 'lib/libwebsocket/response.rb', line 5 def resource_name @resource_name end |
#secure ⇒ Object
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
137 138 139 |
# File 'lib/libwebsocket/response.rb', line 137 def (hash) self..push self.(hash) end |
#number1 ⇒ Object
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 |
#number2 ⇒ Object
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.
18 19 20 |
# File 'lib/libwebsocket/response.rb', line 18 def parse(string) super end |
#to_rack ⇒ Object
Construct a WebSocket response in rack format.
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..empty? hash.merge!('Set-Cookie' => self..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_s ⇒ Object
Construct a WebSocket response in string format.
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..empty? string += 'Set-Cookie: ' string += self..collect(&:to_s).join(',') string += "\x0d\x0a" end string += "\x0d\x0a" string += self.checksum if self.version > 75 return string end |