protocol-websocket 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6dbf808fd6d8859dccb3ce6b8e9578ec62fbb9f2530aa1adc809d0143e16141e
|
4
|
+
data.tar.gz: d674db6e338b18db74b8a170aa81980d609a9c4ae235921c4a87a188a333654a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3fe42aa9dabd6965081a86b9eae2b9deffcff628ef4081c48ed3b53b33854a1782deffde2cb84a91f9ec1d5acd69dc38b2e36c82390cd0ea55b49eac431a0908
|
7
|
+
data.tar.gz: d60e9344e0f386a158549bc1e8a332a5ef4b3e7adc934ffde273dfed73def85a3fd994f4d58e0bddbcfba42cee2fcb7250aa1b4afe0ba9df29721d9ff5be0285
|
@@ -23,14 +23,21 @@ require_relative 'framer'
|
|
23
23
|
module Protocol
|
24
24
|
module WebSocket
|
25
25
|
class Connection
|
26
|
-
|
26
|
+
# @option mask [String] 4-byte mask to be used for frames generated by this connection.
|
27
|
+
def initialize(framer, mask: nil)
|
27
28
|
@framer = framer
|
29
|
+
@mask = mask
|
30
|
+
|
28
31
|
@state = :open
|
29
32
|
@frames = []
|
30
33
|
end
|
31
34
|
|
35
|
+
# The framer which is used for reading and writing frames.
|
32
36
|
attr :framer
|
33
37
|
|
38
|
+
# The (optional) mask which is used when generating frames.
|
39
|
+
attr :mask
|
40
|
+
|
34
41
|
# Buffered frames which form part of a complete message.
|
35
42
|
attr_accessor :frames
|
36
43
|
|
@@ -97,21 +104,21 @@ module Protocol
|
|
97
104
|
end
|
98
105
|
|
99
106
|
def send_text(buffer)
|
100
|
-
frame = TextFrame.new
|
107
|
+
frame = TextFrame.new(mask: @mask)
|
101
108
|
frame.pack buffer
|
102
109
|
|
103
110
|
write_frame(frame)
|
104
111
|
end
|
105
112
|
|
106
113
|
def send_binary(buffer)
|
107
|
-
frame = BinaryFrame.new
|
114
|
+
frame = BinaryFrame.new(mask: @mask)
|
108
115
|
frame.pack buffer
|
109
116
|
|
110
117
|
write_frame(frame)
|
111
118
|
end
|
112
119
|
|
113
120
|
def send_close(code = Error::NO_ERROR, message = nil)
|
114
|
-
frame = CloseFrame.new
|
121
|
+
frame = CloseFrame.new(mask: @mask)
|
115
122
|
frame.pack(code, message)
|
116
123
|
|
117
124
|
write_frame(frame)
|
@@ -131,8 +138,8 @@ module Protocol
|
|
131
138
|
|
132
139
|
def send_ping(data)
|
133
140
|
if @state != :closed
|
134
|
-
frame = PingFrame.new
|
135
|
-
frame.pack
|
141
|
+
frame = PingFrame.new(mask: @mask)
|
142
|
+
frame.pack(data)
|
136
143
|
|
137
144
|
write_frame(frame)
|
138
145
|
else
|
@@ -28,7 +28,7 @@ module Protocol
|
|
28
28
|
OPCODE = 0
|
29
29
|
|
30
30
|
# @param length [Integer] the length of the payload, or nil if the header has not been read yet.
|
31
|
-
def initialize(finished = true,
|
31
|
+
def initialize(finished = true, payload = nil, opcode: self.class::OPCODE, mask: false)
|
32
32
|
@finished = finished
|
33
33
|
@opcode = opcode
|
34
34
|
@mask = mask
|
@@ -83,19 +83,21 @@ module Protocol
|
|
83
83
|
attr_accessor :length
|
84
84
|
attr_accessor :payload
|
85
85
|
|
86
|
-
def pack(data
|
86
|
+
def pack(data)
|
87
87
|
length = data.bytesize
|
88
88
|
|
89
89
|
if length.bit_length > 63
|
90
90
|
raise ProtocolError, "Frame length #{@length} bigger than allowed maximum!"
|
91
91
|
end
|
92
92
|
|
93
|
-
if @mask
|
93
|
+
if @mask
|
94
94
|
@payload = String.new.b
|
95
95
|
|
96
96
|
for i in 0...data.bytesize do
|
97
97
|
@payload << (data.getbyte(i) ^ mask.getbyte(i % 4))
|
98
98
|
end
|
99
|
+
|
100
|
+
@length = length
|
99
101
|
else
|
100
102
|
@payload = data
|
101
103
|
@length = length
|
@@ -159,14 +161,14 @@ module Protocol
|
|
159
161
|
raise EOFError, "Incorrect payload length: #{@length} != #{@payload.bytesize}!"
|
160
162
|
end
|
161
163
|
|
162
|
-
return self.new(finished, opcode, mask
|
164
|
+
return self.new(finished, payload, opcode: opcode, mask: mask)
|
163
165
|
end
|
164
166
|
|
165
167
|
def write(stream)
|
166
168
|
buffer = String.new.b
|
167
169
|
|
168
170
|
if @payload&.bytesize != @length
|
169
|
-
raise ProtocolError, "Invalid payload length: #{@length} != #{@payload.bytesize}!"
|
171
|
+
raise ProtocolError, "Invalid payload length: #{@length} != #{@payload.bytesize} for #{self}!"
|
170
172
|
end
|
171
173
|
|
172
174
|
if @mask and @mask.bytesize != 4
|
@@ -192,6 +194,8 @@ module Protocol
|
|
192
194
|
buffer << [@length].pack('Q>')
|
193
195
|
end
|
194
196
|
|
197
|
+
buffer << @mask if @mask
|
198
|
+
|
195
199
|
stream.write(buffer)
|
196
200
|
stream.write(@payload)
|
197
201
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: protocol-websocket
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-05-
|
11
|
+
date: 2019-05-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: protocol-http
|
@@ -140,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
140
|
- !ruby/object:Gem::Version
|
141
141
|
version: '0'
|
142
142
|
requirements: []
|
143
|
-
rubygems_version: 3.0.
|
143
|
+
rubygems_version: 3.0.2
|
144
144
|
signing_key:
|
145
145
|
specification_version: 4
|
146
146
|
summary: A low level implementation of the WebSocket protocol.
|