ed3-precompiled_websocket-driver 0.8.0-arm64-darwin

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 704f29e0bc5f57e8e7f581546a69b05604032234cf31e578ce2a08ffc11c12db
4
+ data.tar.gz: 3c8b77e53e8cdf113ceb31b9c0cd5e6094204b1c85442d1173da028b5a0ea4bf
5
+ SHA512:
6
+ metadata.gz: '08628dbeacbbc38457c3b5937388e375bb9523caad8961c17afb12e5d1ee22ba87c7c1b940a297c64c232630daecea866224625e3d8489c7a198b221b896ecdf'
7
+ data.tar.gz: 8428edde5ab04d4db573d84474184d3aef2642b8d8f82ec2d3aa346c091799e9f781f30573d487962b5ec4c762442dbfe298c23c89187cff6b0a8ab6d7fd42b4
data/CHANGELOG.md ADDED
@@ -0,0 +1,169 @@
1
+ ### 0.8.0 / 2025-05-25
2
+
3
+ - Emit binary message as a string with `Encoding::BINARY` instead of an array
4
+ - Add the option `:binary_data_format` to force the previous behaviour
5
+
6
+ ### 0.7.7 / 2025-01-04
7
+
8
+ - Add `base64` gem to the dependencies to support Ruby 3.4
9
+
10
+ ### 0.7.6 / 2023-07-25
11
+
12
+ - Fix handling of default ports in `Host` headers on Ruby 3.1+
13
+
14
+ ### 0.7.5 / 2021-06-12
15
+
16
+ - Do not change the encoding of strings passed to `Driver#text`
17
+
18
+ ### 0.7.4 / 2021-05-24
19
+
20
+ - Optimise conversions between strings and byte arrays and related encoding
21
+ operations, to reduce amount of allocation and copying
22
+
23
+ ### 0.7.3 / 2020-07-09
24
+
25
+ - Let the client accept HTTP responses that have an empty reason phrase
26
+ following the `101` status code
27
+
28
+ ### 0.7.2 / 2020-05-22
29
+
30
+ - Emit `ping` and `pong` events from the `Server` driver
31
+ - Handle draft-76 handshakes correctly if the request's body is a frozen string
32
+
33
+ ### 0.7.1 / 2019-06-10
34
+
35
+ - Catch any exceptions produced while generating a handshake response and send a
36
+ `400 Bad Request` response to the client
37
+ - Pick the RFC-6455 protocol version if the request contains any of the headers
38
+ used by that version
39
+ - Handle errors encountered while handling malformed draft-76 requests
40
+ - Change license from MIT to Apache 2.0
41
+
42
+ ### 0.7.0 / 2017-09-11
43
+
44
+ - Add `ping` and `pong` to the set of events users can listen to
45
+
46
+ ### 0.6.5 / 2017-01-22
47
+
48
+ - Provide a pure-Ruby fallback for the native unmasking code
49
+
50
+ ### 0.6.4 / 2016-05-20
51
+
52
+ - Amend warnings issued when running with -W2
53
+ - Make sure message strings passed in by the app are transcoded to UTF-8
54
+ - Copy strings if necessary for frozen-string compatibility
55
+
56
+ ### 0.6.3 / 2015-11-06
57
+
58
+ - Reject draft-76 handshakes if their Sec-WebSocket-Key headers are invalid
59
+ - Throw a more helpful error if a client is created with an invalid URL
60
+
61
+ ### 0.6.2 / 2015-07-18
62
+
63
+ - When the peer sends a close frame with no error code, emit 1000
64
+
65
+ ### 0.6.1 / 2015-07-13
66
+
67
+ - Fix how events are stored in `EventEmitter` to fix a backward-compatibility
68
+ violation introduced in the last release
69
+ - Use the `Array#pack` and `String#unpack` methods for reading/writing numbers
70
+ to buffers rather than including duplicate logic for this
71
+
72
+ ### 0.6.0 / 2015-07-08
73
+
74
+ - Use `SecureRandom` to generate the `Sec-WebSocket-Key` header
75
+ - Allow the parser to recover cleanly if event listeners raise an error
76
+ - Let the `on()` method take a lambda as a positional argument rather than a
77
+ block
78
+ - Add a `pong` method for sending unsolicited pong frames
79
+
80
+ ### 0.5.4 / 2015-03-29
81
+
82
+ - Don't emit extra close frames if we receive a close frame after we already
83
+ sent one
84
+ - Fail the connection when the driver receives an invalid
85
+ `Sec-WebSocket-Extensions` header
86
+
87
+ ### 0.5.3 / 2015-02-22
88
+
89
+ - Don't treat incoming data as WebSocket frames if a client driver is closed
90
+ before receiving the server handshake
91
+
92
+ ### 0.5.2 / 2015-02-19
93
+
94
+ - Don't emit multiple `error` events
95
+
96
+ ### 0.5.1 / 2014-12-18
97
+
98
+ - Don't allow drivers to be created with unrecognized options
99
+
100
+ ### 0.5.0 / 2014-12-13
101
+
102
+ - Support protocol extensions via the websocket-extensions module
103
+
104
+ ### 0.4.0 / 2014-11-08
105
+
106
+ - Support connection via HTTP proxies using `CONNECT`
107
+
108
+ ### 0.3.5 / 2014-10-04
109
+
110
+ - Fix bug where the `Server` driver doesn't pass `ping` callbacks to its
111
+ delegate
112
+ - Fix an arity error when calling `fail_request`
113
+ - Allow `close` to be called before `start` to close the driver
114
+
115
+ ### 0.3.4 / 2014-07-06
116
+
117
+ - Don't hold references to frame buffers after a message has been emitted
118
+ - Make sure that `protocol` and `version` are exposed properly by the TCP driver
119
+ - Correct HTTP header parsing based on RFC 7230; header names cannot contain
120
+ backslashes
121
+
122
+ ### 0.3.3 / 2014-04-24
123
+
124
+ - Fix problems with loading C and Java native extension code
125
+ - Correct the acceptable characters used in the HTTP parser
126
+ - Correct the draft-76 status line reason phrase
127
+
128
+ ### 0.3.2 / 2013-12-29
129
+
130
+ - Expand `max_length` to cover sequences of continuation frames and
131
+ `draft-{75,76}`
132
+ - Decrease default maximum frame buffer size to 64MB
133
+ - Stop parsing when the protocol enters a failure mode, to save CPU cycles
134
+
135
+ ### 0.3.1 / 2013-12-03
136
+
137
+ - Add a `max_length` option to limit allowed frame size
138
+
139
+ ### 0.3.0 / 2013-09-09
140
+
141
+ - Support client URLs with Basic Auth credentials
142
+
143
+ ### 0.2.3 / 2013-08-04
144
+
145
+ - Fix bug in EventEmitter#emit when listeners are removed
146
+
147
+ ### 0.2.2 / 2013-08-04
148
+
149
+ - Fix bug in EventEmitter#listener_count for unregistered events
150
+
151
+ ### 0.2.1 / 2013-07-05
152
+
153
+ - Queue sent messages if the client has not begun trying to connect
154
+ - Encode all strings sent to I/O as `ASCII-8BIT`
155
+
156
+ ### 0.2.0 / 2013-05-12
157
+
158
+ - Add API for setting and reading headers
159
+ - Add Driver.server() method for getting a driver for TCP servers
160
+
161
+ ### 0.1.0 / 2013-05-04
162
+
163
+ - First stable release
164
+
165
+ ### 0.0.0 / 2013-04-22
166
+
167
+ - First release
168
+ - Proof of concept for people to try out
169
+ - Might be unstable
data/LICENSE.md ADDED
@@ -0,0 +1,12 @@
1
+ Copyright 2010-2025 James Coglan
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4
+ this file except in compliance with the License. You may obtain a copy of the
5
+ License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software distributed
10
+ under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11
+ CONDITIONS OF ANY KIND, either express or implied. See the License for the
12
+ specific language governing permissions and limitations under the License.
data/README.md ADDED
@@ -0,0 +1,394 @@
1
+ # websocket-driver
2
+
3
+ This module provides a complete implementation of the WebSocket protocols that
4
+ can be hooked up to any TCP library. It aims to simplify things by decoupling
5
+ the protocol details from the I/O layer, such that users only need to implement
6
+ code to stream data in and out of it without needing to know anything about how
7
+ the protocol actually works. Think of it as a complete WebSocket system with
8
+ pluggable I/O.
9
+
10
+ Due to this design, you get a lot of things for free. In particular, if you hook
11
+ this module up to some I/O object, it will do all of this for you:
12
+
13
+ - Select the correct server-side driver to talk to the client
14
+ - Generate and send both server- and client-side handshakes
15
+ - Recognize when the handshake phase completes and the WS protocol begins
16
+ - Negotiate subprotocol selection based on `Sec-WebSocket-Protocol`
17
+ - Negotiate and use extensions via the
18
+ [websocket-extensions](https://github.com/faye/websocket-extensions-ruby)
19
+ module
20
+ - Buffer sent messages until the handshake process is finished
21
+ - Deal with proxies that defer delivery of the draft-76 handshake body
22
+ - Notify you when the socket is open and closed and when messages arrive
23
+ - Recombine fragmented messages
24
+ - Dispatch text, binary, ping, pong and close frames
25
+ - Manage the socket-closing handshake process
26
+ - Automatically reply to ping frames with a matching pong
27
+ - Apply masking to messages sent by the client
28
+
29
+ This library was originally extracted from the [Faye](http://faye.jcoglan.com)
30
+ project but now aims to provide simple WebSocket support for any Ruby server or
31
+ I/O system.
32
+
33
+
34
+ ## Installation
35
+
36
+ ```
37
+ $ gem install websocket-driver
38
+ ```
39
+
40
+
41
+ ## Usage
42
+
43
+ To build either a server-side or client-side socket, the only requirement is
44
+ that you supply a `socket` object with these methods:
45
+
46
+ - `socket.url` - returns the full URL of the socket as a string.
47
+ - `socket.write(string)` - writes the given string to a TCP stream.
48
+
49
+ Server-side sockets require one additional method:
50
+
51
+ - `socket.env` - returns a Rack-style env hash that will contain some of the
52
+ following fields. Their values are strings containing the value of the named
53
+ header, unless stated otherwise.
54
+ * `HTTP_CONNECTION`
55
+ * `HTTP_HOST`
56
+ * `HTTP_ORIGIN`
57
+ * `HTTP_SEC_WEBSOCKET_EXTENSIONS`
58
+ * `HTTP_SEC_WEBSOCKET_KEY`
59
+ * `HTTP_SEC_WEBSOCKET_KEY1`
60
+ * `HTTP_SEC_WEBSOCKET_KEY2`
61
+ * `HTTP_SEC_WEBSOCKET_PROTOCOL`
62
+ * `HTTP_SEC_WEBSOCKET_VERSION`
63
+ * `HTTP_UPGRADE`
64
+ * `rack.input`, an `IO` object representing the request body
65
+ * `REQUEST_METHOD`, the request's HTTP verb
66
+
67
+
68
+ ### Server-side with Rack
69
+
70
+ To handle a server-side WebSocket connection, you need to check whether the
71
+ request is a WebSocket handshake, and if so create a protocol driver for it.
72
+ You must give the driver an object with the `env`, `url` and `write` methods. A
73
+ simple example might be:
74
+
75
+ ```ruby
76
+ require 'websocket/driver'
77
+ require 'eventmachine'
78
+
79
+ class WS
80
+ attr_reader :env, :url
81
+
82
+ def initialize(env)
83
+ @env = env
84
+
85
+ secure = Rack::Request.new(env).ssl?
86
+ scheme = secure ? 'wss:' : 'ws:'
87
+ @url = scheme + '//' + env['HTTP_HOST'] + env['REQUEST_URI']
88
+
89
+ @driver = WebSocket::Driver.rack(self)
90
+
91
+ env['rack.hijack'].call
92
+ @io = env['rack.hijack_io']
93
+
94
+ EM.attach(@io, Reader) { |conn| conn.driver = @driver }
95
+
96
+ @driver.start
97
+ end
98
+
99
+ def write(string)
100
+ @io.write(string)
101
+ end
102
+
103
+ module Reader
104
+ attr_writer :driver
105
+
106
+ def receive_data(string)
107
+ @driver.parse(string)
108
+ end
109
+ end
110
+ end
111
+ ```
112
+
113
+ To explain what's going on here: the `WS` class implements the `env`, `url` and
114
+ `write(string)` methods as required. When instantiated with a Rack environment,
115
+ it stores the environment and infers the complete URL from it. Having set up
116
+ the `env` and `url`, it asks `WebSocket::Driver` for a server-side driver for
117
+ the socket. Then it uses the Rack hijack API to gain access to the TCP stream,
118
+ and uses EventMachine to stream in incoming data from the client, handing
119
+ incoming data off to the driver for parsing. Finally, we tell the driver to
120
+ `start`, which will begin sending the handshake response. This will invoke the
121
+ `WS#write` method, which will send the response out over the TCP socket.
122
+
123
+ Having defined this class we could use it like this when handling a request:
124
+
125
+ ```ruby
126
+ if WebSocket::Driver.websocket?(env)
127
+ socket = WS.new(env)
128
+ end
129
+ ```
130
+
131
+ The driver API is described in full below.
132
+
133
+
134
+ ### Server-side with TCP
135
+
136
+ You can also handle WebSocket connections in a bare TCP server, if you're not
137
+ using Rack and don't want to implement HTTP parsing yourself. For this, your
138
+ socket object only needs a `write` method.
139
+
140
+ The driver will emit a `:connect` event when a request is received, and at this
141
+ point you can detect whether it's a WebSocket and handle it as such. Here's an
142
+ example using an EventMachine TCP server.
143
+
144
+ ```ruby
145
+ module Connection
146
+ def initialize
147
+ @driver = WebSocket::Driver.server(self)
148
+
149
+ @driver.on :connect, -> (event) do
150
+ if WebSocket::Driver.websocket?(@driver.env)
151
+ @driver.start
152
+ else
153
+ # handle other HTTP requests, for example
154
+ body = '<h1>hello</h1>'
155
+ response = [
156
+ 'HTTP/1.1 200 OK',
157
+ 'Content-Type: text/plain',
158
+ "Content-Length: #{body.bytesize}",
159
+ '',
160
+ body
161
+ ]
162
+ send_data response.join("\r\n")
163
+ end
164
+ end
165
+
166
+ @driver.on :message, -> (e) { @driver.text(e.data) }
167
+ @driver.on :close, -> (e) { close_connection_after_writing }
168
+ end
169
+
170
+ def receive_data(data)
171
+ @driver.parse(data)
172
+ end
173
+
174
+ def write(data)
175
+ send_data(data)
176
+ end
177
+ end
178
+
179
+ EM.run {
180
+ EM.start_server('127.0.0.1', 4180, Connection)
181
+ }
182
+ ```
183
+
184
+ In the `:connect` event, `@driver.env` is a Rack env representing the request.
185
+ If the request has a body, it will be in the `@driver.env['rack.input']` stream,
186
+ but only as much of the body as you have so far routed to it using the `parse`
187
+ method.
188
+
189
+
190
+ ### Client-side
191
+
192
+ Similarly, to implement a WebSocket client you need an object with `url` and
193
+ `write` methods. Once you have one such object, you ask for a driver for it:
194
+
195
+ ```ruby
196
+ driver = WebSocket::Driver.client(socket)
197
+ ```
198
+
199
+ After this you use the driver API as described below to process incoming data
200
+ and send outgoing data.
201
+
202
+ Client drivers have two additional methods for reading the HTTP data that was
203
+ sent back by the server:
204
+
205
+ - `driver.status` - the integer value of the HTTP status code
206
+ - `driver.headers` - a hash-like object containing the response headers
207
+
208
+
209
+ ### HTTP Proxies
210
+
211
+ The client driver supports connections via HTTP proxies using the `CONNECT`
212
+ method. Instead of sending the WebSocket handshake immediately, it will send a
213
+ `CONNECT` request, wait for a `200` response, and then proceed as normal.
214
+
215
+ To use this feature, call `proxy = driver.proxy(url)` where `url` is the origin
216
+ of the proxy, including a username and password if required. This produces an
217
+ object that manages the process of connecting via the proxy. You should call
218
+ `proxy.start` to begin the connection process, and pass data you receive via the
219
+ socket to `proxy.parse(data)`. When the proxy emits `:connect`, you should then
220
+ start sending incoming data to `driver.parse(data)` as normal, and call
221
+ `driver.start`.
222
+
223
+ ```rb
224
+ proxy = driver.proxy('http://username:password@proxy.example.com')
225
+
226
+ proxy.on :connect, -> (event) do
227
+ driver.start
228
+ end
229
+ ```
230
+
231
+ The proxy's `:connect` event is also where you should perform a TLS handshake on
232
+ your TCP stream, if you are connecting to a `wss:` endpoint.
233
+
234
+ In the event that proxy connection fails, `proxy` will emit an `:error`. You can
235
+ inspect the proxy's response via `proxy.status` and `proxy.headers`.
236
+
237
+ ```rb
238
+ proxy.on :error, -> (error) do
239
+ puts error.message
240
+ puts proxy.status
241
+ puts proxy.headers.inspect
242
+ end
243
+ ```
244
+
245
+ Before calling `proxy.start` you can set custom headers using
246
+ `proxy.set_header`:
247
+
248
+ ```rb
249
+ proxy.set_header('User-Agent', 'ruby')
250
+ proxy.start
251
+ ```
252
+
253
+
254
+ ### Driver API
255
+
256
+ Drivers are created using one of the following methods:
257
+
258
+ ```ruby
259
+ driver = WebSocket::Driver.rack(socket, options)
260
+ driver = WebSocket::Driver.server(socket, options)
261
+ driver = WebSocket::Driver.client(socket, options)
262
+ ```
263
+
264
+ The `rack` method returns a driver chosen using the socket's `env`. The `server`
265
+ method returns a driver that will parse an HTTP request and then decide which
266
+ driver to use for it using the `rack` method. The `client` method always returns
267
+ a driver for the RFC version of the protocol with masking enabled on outgoing
268
+ frames.
269
+
270
+ The `options` argument is optional, and is a hash. It may contain the following
271
+ keys:
272
+
273
+ - `:max_length` - the maximum allowed size of incoming message frames, in bytes.
274
+ The default value is `2^26 - 1`, or 1 byte short of 64 MiB.
275
+ - `:protocols` - an array of strings representing acceptable subprotocols for
276
+ use over the socket. The driver will negotiate one of these to use via the
277
+ `Sec-WebSocket-Protocol` header if supported by the other peer.
278
+ - `:binary_data_format` - in older versions of this library, binary messages
279
+ were represented as arrays of bytes, whereas they're now represented as
280
+ strings with `Encoding::BINARY` for performance reasons. Set this option to
281
+ `:array` to restore the old behaviour.
282
+
283
+ All drivers respond to the following API methods, but some of them are no-ops
284
+ depending on whether the client supports the behaviour.
285
+
286
+ Note that most of these methods are commands: if they produce data that should
287
+ be sent over the socket, they will give this to you by calling
288
+ `socket.write(string)`.
289
+
290
+ #### `driver.on :open, -> (event) {}`
291
+
292
+ Adds a callback block to execute when the socket becomes open.
293
+
294
+ #### `driver.on :message, -> (event) {}`
295
+
296
+ Adds a callback block to execute when a message is received. `event` will have a
297
+ `data` attribute whose value is a string with the encoding `Encoding::UTF_8` for
298
+ text message, and `Encoding::BINARY` for binary message.
299
+
300
+ #### `driver.on :error, -> (event) {}`
301
+
302
+ Adds a callback to execute when a protocol error occurs due to the other peer
303
+ sending an invalid byte sequence. `event` will have a `message` attribute
304
+ describing the error.
305
+
306
+ #### `driver.on :close, -> (event) {}`
307
+
308
+ Adds a callback block to execute when the socket becomes closed. The `event`
309
+ object has `code` and `reason` attributes.
310
+
311
+ #### `driver.on :ping, -> (event) {}`
312
+
313
+ Adds a callback block to execute when a ping is received. You do not need to
314
+ handle this by sending a pong frame yourself; the driver handles this for you.
315
+
316
+ #### `driver.on :pong, -> (event) {}`
317
+
318
+ Adds a callback block to execute when a pong is received. If this was in
319
+ response to a ping you sent, you can also handle this event via the
320
+ `driver.ping(message) { ... }` callback.
321
+
322
+ #### `driver.add_extension(extension)`
323
+
324
+ Registers a protocol extension whose operation will be negotiated via the
325
+ `Sec-WebSocket-Extensions` header. `extension` is any extension compatible with
326
+ the [websocket-extensions](https://github.com/faye/websocket-extensions-ruby)
327
+ framework.
328
+
329
+ #### `driver.set_header(name, value)`
330
+
331
+ Sets a custom header to be sent as part of the handshake response, either from
332
+ the server or from the client. Must be called before `start`, since this is when
333
+ the headers are serialized and sent.
334
+
335
+ #### `driver.start`
336
+
337
+ Initiates the protocol by sending the handshake - either the response for a
338
+ server-side driver or the request for a client-side one. This should be the
339
+ first method you invoke. Returns `true` if and only if a handshake was sent.
340
+
341
+ #### `driver.parse(string)`
342
+
343
+ Takes a string and parses it, potentially resulting in message events being
344
+ emitted (see `on('message')` above) or in data being sent to `socket.write`.
345
+ You should send all data you receive via I/O to this method.
346
+
347
+ #### `driver.text(string)`
348
+
349
+ Sends a text message over the socket. If the socket handshake is not yet
350
+ complete, the message will be queued until it is. Returns `true` if the message
351
+ was sent or queued, and `false` if the socket can no longer send messages.
352
+
353
+ #### `driver.binary(buffer)`
354
+
355
+ Takes either a string with encoding `Encoding::BINARY`, or an array of
356
+ byte-sized integers, and sends it as a binary message. Will queue and return
357
+ `true` or `false` the same way as the `text` method. It will also return `false`
358
+ if the driver does not support binary messages.
359
+
360
+ #### `driver.ping(string = '', &callback)`
361
+
362
+ Sends a ping frame over the socket, queueing it if necessary. `string` and the
363
+ `callback` block are both optional. If a callback is given, it will be invoked
364
+ when the socket receives a pong frame whose content matches `string`. Returns
365
+ `false` if frames can no longer be sent, or if the driver does not support
366
+ ping/pong.
367
+
368
+ #### `driver.pong(string = '')`
369
+
370
+ Sends a pong frame over the socket, queueing it if necessary. `string` is
371
+ optional. Returns `false` if frames can no longer be sent, or if the driver does
372
+ not support ping/pong.
373
+
374
+ You don't need to call this when a ping frame is received; pings are replied to
375
+ automatically by the driver. This method is for sending unsolicited pongs.
376
+
377
+ #### `driver.close`
378
+
379
+ Initiates the closing handshake if the socket is still open. For drivers with no
380
+ closing handshake, this will result in the immediate execution of the
381
+ `on('close')` callback. For drivers with a closing handshake, this sends a
382
+ closing frame and `emit('close')` will execute when a response is received or a
383
+ protocol error occurs.
384
+
385
+ #### `driver.version`
386
+
387
+ Returns the WebSocket version in use as a string. Will either be `hixie-75`,
388
+ `hixie-76` or `hybi-$version`.
389
+
390
+ #### `driver.protocol`
391
+
392
+ Returns a string containing the selected subprotocol, if any was agreed upon
393
+ using the `Sec-WebSocket-Protocol` mechanism. This value becomes available after
394
+ `emit('open')` has fired.
@@ -0,0 +1,57 @@
1
+ package com.jcoglan.websocket;
2
+
3
+ import java.io.IOException;
4
+ import org.jruby.Ruby;
5
+ import org.jruby.RubyClass;
6
+ import org.jruby.RubyModule;
7
+ import org.jruby.RubyObject;
8
+ import org.jruby.RubyString;
9
+ import org.jruby.anno.JRubyMethod;
10
+ import org.jruby.runtime.ObjectAllocator;
11
+ import org.jruby.runtime.ThreadContext;
12
+ import org.jruby.runtime.builtin.IRubyObject;
13
+ import org.jruby.runtime.load.BasicLibraryService;
14
+
15
+ public class WebsocketMaskService implements BasicLibraryService {
16
+ private Ruby runtime;
17
+
18
+ public boolean basicLoad(Ruby runtime) throws IOException {
19
+ this.runtime = runtime;
20
+
21
+ RubyModule websocket = runtime.defineModule("WebSocket");
22
+ RubyClass webSocketMask = websocket.defineClassUnder("Mask", runtime.getObject(), getAllocator());
23
+
24
+ webSocketMask.defineAnnotatedMethods(WebsocketMask.class);
25
+ return true;
26
+ }
27
+
28
+ ObjectAllocator getAllocator() {
29
+ return new ObjectAllocator() {
30
+ public IRubyObject allocate(Ruby runtime, RubyClass rubyClass) {
31
+ return new WebsocketMask(runtime, rubyClass);
32
+ }
33
+ };
34
+ }
35
+
36
+ public class WebsocketMask extends RubyObject {
37
+ public WebsocketMask(final Ruby runtime, RubyClass rubyClass) {
38
+ super(runtime, rubyClass);
39
+ }
40
+
41
+ @JRubyMethod
42
+ public IRubyObject mask(ThreadContext context, IRubyObject payload, IRubyObject mask) {
43
+ if (mask.isNil()) return payload;
44
+
45
+ byte[] payload_a = ((RubyString)payload).getBytes();
46
+ byte[] mask_a = ((RubyString)mask).getBytes();
47
+ int i, n = payload_a.length;
48
+
49
+ if (n == 0) return payload;
50
+
51
+ for (i = 0; i < n; i++) {
52
+ payload_a[i] ^= mask_a[i % 4];
53
+ }
54
+ return RubyString.newStringNoCopy(runtime, payload_a);
55
+ }
56
+ }
57
+ }
@@ -0,0 +1,4 @@
1
+ require 'mkmf'
2
+ extension_name = 'websocket_mask'
3
+ dir_config(extension_name)
4
+ create_makefile(extension_name)
@@ -0,0 +1,32 @@
1
+ #include <ruby.h>
2
+
3
+ VALUE method_websocket_mask(VALUE self, VALUE payload, VALUE mask)
4
+ {
5
+ char *payload_s, *mask_s, *unmasked_s;
6
+ long i, n;
7
+ VALUE unmasked;
8
+
9
+ if (mask == Qnil || RSTRING_LEN(mask) != 4) {
10
+ return payload;
11
+ }
12
+
13
+ payload_s = RSTRING_PTR(payload);
14
+ mask_s = RSTRING_PTR(mask);
15
+ n = RSTRING_LEN(payload);
16
+
17
+ unmasked = rb_str_new(0, n);
18
+ unmasked_s = RSTRING_PTR(unmasked);
19
+
20
+ for (i = 0; i < n; i++) {
21
+ unmasked_s[i] = payload_s[i] ^ mask_s[i % 4];
22
+ }
23
+ return unmasked;
24
+ }
25
+
26
+ void Init_websocket_mask()
27
+ {
28
+ VALUE WebSocket = rb_define_module("WebSocket");
29
+ VALUE Mask = rb_define_module_under(WebSocket, "Mask");
30
+
31
+ rb_define_singleton_method(Mask, "mask", method_websocket_mask, 2);
32
+ }
Binary file
Binary file
Binary file
Binary file
Binary file