protocol-websocket 0.11.0 → 0.12.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: 58b07db9f128f30d49b779a04a08624afbeb59abe9cc3e0028ded1e2d5e6a7f3
4
- data.tar.gz: 646c2a5004051f46d6f4872fec0842d695d6e4a1b2273421bc164293315df4e0
3
+ metadata.gz: 336e62c38fbb57000bf3183e14f750224706504cd8777bbc7d09505f71a0d65e
4
+ data.tar.gz: 2bb63e4bab32ba833f90e5f2857ae198a4f0b9aa66404044ac192e9bcc04c726
5
5
  SHA512:
6
- metadata.gz: f1f28c28daa4dc109df788ae226c4b9a0a1081d20a2316a4e23ff48e1632dd9662a32c2bad2573fed32cde1624c7ab7c23a53b805e946068a70543f93b188cbf
7
- data.tar.gz: 0d64b3c9f3d809bd6b965b15780be053c15cfc37ff7fc5c66d0d7dc73e614821550ee25746f6a4b28b9c6f69603254daa3f4bd83b63e4cd5dbe8b4f223be9ab6
6
+ metadata.gz: eba17e42ae64f93fd5d427245b916fb25c6c09971db2d15969d6b8e29aaea22fb1ab39d52aff19fb46157003bf5c277b0aab1194f00a3fc04532fb1bfa1ab3ca
7
+ data.tar.gz: e228e38de1c954c9c7c506bc73c94194eea02bb51ee5d0baaae61b98820f831a409970c00a74846a70d0a7642ea807d7e6c09734c388d9634379f0e1bef659cc
checksums.yaml.gz.sig CHANGED
Binary file
@@ -61,8 +61,17 @@ module Protocol
61
61
  return self
62
62
  end
63
63
 
64
+ # If not already closed, transition the connection to the closed state and send a close frame.
64
65
  def close!(...)
65
- @state = :closed
66
+ unless @state == :closed
67
+ @state = :closed
68
+
69
+ begin
70
+ send_close(...)
71
+ rescue
72
+ # Ignore errors.
73
+ end
74
+ end
66
75
 
67
76
  return self
68
77
  end
@@ -71,16 +80,9 @@ module Protocol
71
80
  @state == :closed
72
81
  end
73
82
 
83
+ # Immediately transition the connection to the closed state and close the underlying connection.
74
84
  def close(...)
75
- unless @state == :closed
76
- close!
77
-
78
- begin
79
- send_close(...)
80
- rescue
81
- # Ignore.
82
- end
83
- end
85
+ close!(...)
84
86
 
85
87
  @framer.close
86
88
  end
@@ -101,11 +103,9 @@ module Protocol
101
103
  return frame
102
104
  rescue ProtocolError => error
103
105
  close(error.code, error.message)
104
-
105
106
  raise
106
- rescue
107
- close(Error::PROTOCOL_ERROR, $!.message)
108
-
107
+ rescue => error
108
+ close(Error::PROTOCOL_ERROR, error.message)
109
109
  raise
110
110
  end
111
111
 
@@ -142,11 +142,8 @@ module Protocol
142
142
  def receive_close(frame)
143
143
  code, reason = frame.unpack
144
144
 
145
- # If we're already closed, then we don't need to send a close frame. Otherwise, according to the RFC, we should echo the close frame. However, it's possible it will fail to send if the connection is already closed.
146
- unless @state == :closed
147
- close!
148
- send_close(code, reason)
149
- end
145
+ # On receiving a close frame, we must enter the closed state:
146
+ close!(code, reason)
150
147
 
151
148
  if code and code != Error::NO_ERROR
152
149
  raise ClosedError.new reason, code
@@ -202,6 +199,7 @@ module Protocol
202
199
  write_frame(@writer.pack_binary_frame(buffer, **options))
203
200
  end
204
201
 
202
+ # Send a control frame with data containing a specified control sequence to begin the closing handshake. Does not close the connection, until the remote end responds with a close frame.
205
203
  def send_close(code = Error::NO_ERROR, reason = "")
206
204
  frame = CloseFrame.new(mask: @mask)
207
205
  frame.pack(code, reason)
@@ -246,7 +244,6 @@ module Protocol
246
244
  end
247
245
  rescue ProtocolError => error
248
246
  close(error.code, error.message)
249
-
250
247
  raise
251
248
  end
252
249
  end
@@ -86,6 +86,30 @@ module Protocol
86
86
  attr_accessor :length
87
87
  attr_accessor :payload
88
88
 
89
+ if IO.const_defined?(:Buffer) && IO::Buffer.respond_to?(:for) && IO::Buffer.method_defined?(:xor!)
90
+ private def mask_xor(data, mask)
91
+ buffer = data.dup
92
+ mask_buffer = IO::Buffer.for(mask)
93
+
94
+ IO::Buffer.for(buffer) do |buffer|
95
+ buffer.xor!(mask_buffer)
96
+ end
97
+
98
+ return buffer
99
+ end
100
+ else
101
+ warn "IO::Buffer not available, falling back to slow implementation of mask_xor!"
102
+ private def mask_xor(data, mask)
103
+ result = String.new(encoding: Encoding::BINARY)
104
+
105
+ for i in 0...data.bytesize do
106
+ result << (data.getbyte(i) ^ mask.getbyte(i % 4))
107
+ end
108
+
109
+ return result
110
+ end
111
+ end
112
+
89
113
  def pack(data = "")
90
114
  length = data.bytesize
91
115
 
@@ -94,12 +118,7 @@ module Protocol
94
118
  end
95
119
 
96
120
  if @mask
97
- @payload = String.new(encoding: Encoding::BINARY)
98
-
99
- for i in 0...data.bytesize do
100
- @payload << (data.getbyte(i) ^ mask.getbyte(i % 4))
101
- end
102
-
121
+ @payload = mask_xor(data, mask)
103
122
  @length = length
104
123
  else
105
124
  @payload = data
@@ -111,13 +130,7 @@ module Protocol
111
130
 
112
131
  def unpack
113
132
  if @mask and !@payload.empty?
114
- data = String.new(encoding: Encoding::BINARY)
115
-
116
- for i in 0...@payload.bytesize do
117
- data << (@payload.getbyte(i) ^ @mask.getbyte(i % 4))
118
- end
119
-
120
- return data
133
+ return mask_xor(@payload, @mask)
121
134
  else
122
135
  return @payload
123
136
  end
@@ -5,6 +5,6 @@
5
5
 
6
6
  module Protocol
7
7
  module WebSocket
8
- VERSION = "0.11.0"
8
+ VERSION = "0.12.0"
9
9
  end
10
10
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protocol-websocket
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -41,7 +41,7 @@ cert_chain:
41
41
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
42
42
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
43
43
  -----END CERTIFICATE-----
44
- date: 2023-03-07 00:00:00.000000000 Z
44
+ date: 2023-07-01 00:00:00.000000000 Z
45
45
  dependencies:
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: protocol-http
@@ -119,8 +119,6 @@ executables: []
119
119
  extensions: []
120
120
  extra_rdoc_files: []
121
121
  files:
122
- - lib/.DS_Store
123
- - lib/protocol/.DS_Store
124
122
  - lib/protocol/websocket.rb
125
123
  - lib/protocol/websocket/binary_frame.rb
126
124
  - lib/protocol/websocket/close_frame.rb
@@ -156,14 +154,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
156
154
  requirements:
157
155
  - - ">="
158
156
  - !ruby/object:Gem::Version
159
- version: 2.5.0
157
+ version: '3.0'
160
158
  required_rubygems_version: !ruby/object:Gem::Requirement
161
159
  requirements:
162
160
  - - ">="
163
161
  - !ruby/object:Gem::Version
164
162
  version: '0'
165
163
  requirements: []
166
- rubygems_version: 3.4.6
164
+ rubygems_version: 3.5.0.dev
167
165
  signing_key:
168
166
  specification_version: 4
169
167
  summary: A low level implementation of the WebSocket protocol.
metadata.gz.sig CHANGED
Binary file
data/lib/.DS_Store DELETED
Binary file
Binary file