protocol-websocket 0.11.1 → 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: 17a6b7e17274fe574b07bc2ad242cbe383671f7f05c84fd784ec474fbde5e28a
4
- data.tar.gz: f1098660970a0cf6069d5037dd736b52b18b7da51727c9968a96970ecd1015b0
3
+ metadata.gz: 336e62c38fbb57000bf3183e14f750224706504cd8777bbc7d09505f71a0d65e
4
+ data.tar.gz: 2bb63e4bab32ba833f90e5f2857ae198a4f0b9aa66404044ac192e9bcc04c726
5
5
  SHA512:
6
- metadata.gz: 1720e16bd8d32258b7583d9cde912229d64af57428ad3bb8644832c5b6f4e5c1588a798c9368a9638c5801f320e9b8489094d40ca5ffe45634b6d582d3f12170
7
- data.tar.gz: 17ac4461055d2de24188f365a85e5fb80c907ab041fc62bdc2f376e24bec1178f950ef8ff4388d39c6c144e056e63d8fc8e28ab53dcd537a4f92af73c02a5dd2
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.1"
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.1
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-06-01 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
@@ -154,14 +154,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
154
154
  requirements:
155
155
  - - ">="
156
156
  - !ruby/object:Gem::Version
157
- version: 2.7.6
157
+ version: '3.0'
158
158
  required_rubygems_version: !ruby/object:Gem::Requirement
159
159
  requirements:
160
160
  - - ">="
161
161
  - !ruby/object:Gem::Version
162
162
  version: '0'
163
163
  requirements: []
164
- rubygems_version: 3.4.7
164
+ rubygems_version: 3.5.0.dev
165
165
  signing_key:
166
166
  specification_version: 4
167
167
  summary: A low level implementation of the WebSocket protocol.
metadata.gz.sig CHANGED
Binary file