protocol-websocket 0.15.0 → 0.17.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: ec6ec828408c7579273d456c0a9ce582a794757b5c532e228c45eca4779acee1
4
- data.tar.gz: 5abe7c8dcb8e1e5cd64e62e06cb0f7585ec0fa8646baf4cecaa6945542cf8b33
3
+ metadata.gz: 6c565e42a63322140910616237842446c9faf392325569500454cff2bcfe500d
4
+ data.tar.gz: db2129c90eb70609ef3b14f62c5a8f261cca9d4e5796be21c15ed24f545f3a19
5
5
  SHA512:
6
- metadata.gz: e5daf9a48e29e811b255516dd777fafbbdf084c8ed681d81ebe2fc67eead205702186b42231abfa032060afc62d439a2e6c8c90d1696eec58be37c40cf52d2a9
7
- data.tar.gz: 6dc89a62a3179b9e2d397cac57204a5d73897ada519abba42d3f85ec184c6f74c1c2f26d81b2ef5277c0fa4b17c35cf283019f99e9a01537d32fece66c76db2b
6
+ metadata.gz: 68c058f51adbcadb882cfd139bc88a0dbdb0d1024d3c7cc62794796e83ab730340b72289f505f5fcd1d213cda0901f4b16aaee42c899c25ffca57e3baa057078
7
+ data.tar.gz: 80121ca724e4f275a88479d4778aa35070b0ddc62660ad4acda47247a1ed7461137a5227e65ea8c3569120a0bf98ff0c0a24b33039d4cf67602b825c2289891a
checksums.yaml.gz.sig CHANGED
Binary file
@@ -59,6 +59,14 @@ module Protocol
59
59
  end
60
60
  end
61
61
 
62
+ # Generate a suitable reply.
63
+ # @returns [CloseFrame]
64
+ def reply(code = Error::NO_ERROR, reason = "")
65
+ frame = CloseFrame.new
66
+ frame.pack(code, reason)
67
+ return frame
68
+ end
69
+
62
70
  # Apply this frame to the specified connection.
63
71
  def apply(connection)
64
72
  connection.receive_close(self)
@@ -89,13 +89,34 @@ module Protocol
89
89
  @state == :closed
90
90
  end
91
91
 
92
- # Immediately transition the connection to the closed state *and* close the underlying connection.
92
+ # Immediately transition the connection to the closed state *and* close the underlying connection. Any data not yet read will be lost.
93
93
  def close(...)
94
94
  close!(...)
95
95
 
96
96
  @framer.close
97
97
  end
98
98
 
99
+ # Close the connection gracefully, sending a close frame with the specified error code and reason. If an error occurs while sending the close frame, the connection will be closed immediately. You may continue to read data from the connection after calling this method, but you should not write any more data.
100
+ #
101
+ # @parameter error [Error | Nil] The error that occurred, if any.
102
+ def close_write(error = nil)
103
+ if error
104
+ send_close(Error::INTERNAL_ERROR, error.message)
105
+ else
106
+ send_close
107
+ end
108
+ end
109
+
110
+ # Close the connection gracefully. This will send a close frame and wait for the remote end to respond with a close frame. Any data received after the close frame is sent will be ignored. If you want to process this data, use {#close_write} instead, and read the data before calling {#close}.
111
+ def shutdown
112
+ send_close unless @state == :closed
113
+
114
+ # `read_frame` will return nil after receiving a close frame:
115
+ while read_frame
116
+ # Drain the connection.
117
+ end
118
+ end
119
+
99
120
  # Read a frame from the framer, and apply it to the connection.
100
121
  def read_frame
101
122
  return nil if closed?
@@ -244,16 +265,19 @@ module Protocol
244
265
  # Write a message to the connection.
245
266
  # @parameter message [Message] The message to send.
246
267
  def write(message, **options)
268
+ case message
269
+ when String
247
270
  # This is a compatibility shim for the previous implementation. We may want to eventually deprecate this use case... or maybe it's convenient enough to leave it around.
248
- if message.is_a?(String)
249
271
  if message.encoding == Encoding::UTF_8
250
272
  return send_text(message, **options)
251
273
  else
252
274
  return send_binary(message, **options)
253
275
  end
276
+ when Message
277
+ message.send(self, **options)
278
+ else
279
+ raise ArgumentError, "Unsupported message type: #{message}"
254
280
  end
255
-
256
- message.send(self, **options)
257
281
  end
258
282
 
259
283
  # The default implementation for reading a message buffer. This is used by the {#reader} interface.
@@ -19,10 +19,24 @@ module Protocol
19
19
  # Indicates that an endpoint is terminating the connection due to a protocol error.
20
20
  PROTOCOL_ERROR = 1002
21
21
 
22
- # Indicates that an endpoint is terminating the connection because it has received a type of data it cannot accept.
22
+ # Indicates that an endpoint is terminating the connection because it has received a type of data it cannot accept. (e.g., an endpoint that understands only text data MAY send this if it receives a binary message).
23
23
  INVALID_DATA = 1003
24
24
 
25
- # There are other status codes but most of them are "implementation specific".
25
+
26
+ # Indicates that an endpoint is terminating the connection because it has received data within a message that was not consistent with the type of the message (e.g., non-UTF-8 data within a text message).
27
+ INVALID_PAYLOAD = 1007
28
+
29
+ # Indicates that an endpoint is terminating the connection because it has received a message that violates its policy. This is a generic status code that can be returned when there is no other more suitable status code (e.g., 1003 or 1009) or if there is a need to hide specific details about the policy.
30
+ POLICY_VIOLATION = 1008
31
+
32
+ # Indicates that an endpoint is terminating the connection because it has received a message that is too big for it to process.
33
+ MESSAGE_TOO_LARGE = 1009
34
+
35
+ # Indicates that an endpoint (client) is terminating the connection because it has expected the server to negotiate one or more extension, but the server didn't return them in the response message of the WebSocket handshake. The list of extensions that are needed should appear in the /reason/ part of the Close frame. Note that this status code is not used by the server, because it can fail the WebSocket handshake instead.
36
+ MISSING_EXTENSION = 1010
37
+
38
+ # Indicates that a server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request.
39
+ INTERNAL_ERROR = 1011
26
40
  end
27
41
 
28
42
  # Raised by stream or connection handlers, results in GOAWAY frame which signals termination of the current connection. You *cannot* recover from this exception, or any exceptions subclassed from it.
@@ -56,13 +56,14 @@ module Protocol
56
56
  def to_h(...)
57
57
  parse(...).to_h
58
58
  end
59
+
60
+ def send(connection, **options)
61
+ connection.send_text(@buffer, **options)
62
+ end
59
63
  end
60
64
 
61
65
  # Represents a text message that can be sent or received over a WebSocket connection.
62
66
  class TextMessage < Message
63
- def send(connection, **options)
64
- connection.send_text(@buffer, **options)
65
- end
66
67
  end
67
68
 
68
69
  # Represents a binary message that can be sent or received over a WebSocket connection.
@@ -5,6 +5,6 @@
5
5
 
6
6
  module Protocol
7
7
  module WebSocket
8
- VERSION = "0.15.0"
8
+ VERSION = "0.17.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.15.0
4
+ version: 0.17.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: 2024-07-03 00:00:00.000000000 Z
44
+ date: 2024-09-11 00:00:00.000000000 Z
45
45
  dependencies:
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: protocol-http
metadata.gz.sig CHANGED
Binary file