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: fa6f686dc5e613876cef1283788d5be3894e069243e542f046474b332fbf95dd
4
- data.tar.gz: ee086548d5f7346510acb18ec2dcb2238f869e5a84e806a21a46ddd341a83a59
3
+ metadata.gz: 6dbf808fd6d8859dccb3ce6b8e9578ec62fbb9f2530aa1adc809d0143e16141e
4
+ data.tar.gz: d674db6e338b18db74b8a170aa81980d609a9c4ae235921c4a87a188a333654a
5
5
  SHA512:
6
- metadata.gz: 818fb24badcacf322da7eb6a5b9f4d33a1825a05dd745e83d92917eccdd663ddc1a1f7d3eaa93bdfe10f2c3af75b6abac991da5f9ee8ee2c9281e02d1f25402e
7
- data.tar.gz: '0857e0a599e4b391d265c61efee3555b7d75c6c6b6c0f83ee691e8d413ab971fc47c8d07300ca6815c589f22d12abbdffafc0911bde93eb5670eba120b8478a9'
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
- def initialize(framer)
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 data
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, opcode = self.class::OPCODE, mask = false, payload = nil)
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, mask = @mask)
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 = 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, payload)
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
@@ -27,7 +27,7 @@ module Protocol
27
27
  OPCODE = 0x9
28
28
 
29
29
  def reply
30
- PongFrame.new(true, PongFrame::OPCODE, @mask, @payload)
30
+ PongFrame.new(true, @payload, mask: @mask)
31
31
  end
32
32
 
33
33
  def apply(connection)
@@ -29,8 +29,8 @@ module Protocol
29
29
  super.force_encoding(Encoding::UTF_8)
30
30
  end
31
31
 
32
- def pack(data, mask = @mask)
33
- super(data.b, mask = @mask)
32
+ def pack(data)
33
+ super(data.b)
34
34
  end
35
35
 
36
36
  def apply(connection)
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Protocol
22
22
  module WebSocket
23
- VERSION = "0.3.2"
23
+ VERSION = "0.4.0"
24
24
  end
25
25
  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.3.2
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-10 00:00:00.000000000 Z
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.3
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.