Sha256: 4690b089aa5364f8794fdd9f457a9ebcd4b09895d0697243faa3f0be9fba3c06

Contents?: true

Size: 1.82 KB

Versions: 31

Compression:

Stored size: 1.82 KB

Contents

package com.jcoglan.websocket;

import java.io.IOException;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
import org.jruby.RubyObject;
import org.jruby.RubyString;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.load.BasicLibraryService;

public class WebsocketMaskService implements BasicLibraryService {
    private Ruby runtime;

    public boolean basicLoad(Ruby runtime) throws IOException {
        this.runtime = runtime;

        RubyModule websocket = runtime.defineModule("WebSocket");
        RubyClass webSocketMask = websocket.defineClassUnder("Mask", runtime.getObject(), getAllocator());

        webSocketMask.defineAnnotatedMethods(WebsocketMask.class);
        return true;
    }

    ObjectAllocator getAllocator() {
        return new ObjectAllocator() {
            public IRubyObject allocate(Ruby runtime, RubyClass rubyClass) {
                return new WebsocketMask(runtime, rubyClass);
            }
        };
    }

    public class WebsocketMask extends RubyObject {
        public WebsocketMask(final Ruby runtime, RubyClass rubyClass) {
            super(runtime, rubyClass);
        }

        @JRubyMethod
        public IRubyObject mask(ThreadContext context, IRubyObject payload, IRubyObject mask) {
            if (mask.isNil()) return payload;

            byte[] payload_a = ((RubyString)payload).getBytes();
            byte[] mask_a    = ((RubyString)mask).getBytes();
            int i, n         = payload_a.length;

            if (n == 0) return payload;

            for (i = 0; i < n; i++) {
                payload_a[i] ^= mask_a[i % 4];
            }
            return RubyString.newStringNoCopy(runtime, payload_a);
        }
    }
}

Version data entries

31 entries across 31 versions & 12 rubygems

Version Path
websocket-driver-0.7.7-java ext/websocket-driver/WebsocketMaskService.java
websocket-driver-0.7.7 ext/websocket-driver/WebsocketMaskService.java
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/websocket-driver-0.7.6/ext/websocket-driver/WebsocketMaskService.java
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/websocket-driver-0.7.6/ext/websocket-driver/WebsocketMaskService.java
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/websocket-driver-0.7.6/ext/websocket-driver/WebsocketMaskService.java
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/websocket-driver-0.7.6/ext/websocket-driver/WebsocketMaskService.java
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/websocket-driver-0.7.6/ext/websocket-driver/WebsocketMaskService.java
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/websocket-driver-0.7.6/ext/websocket-driver/WebsocketMaskService.java
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/websocket-driver-0.7.5/ext/websocket-driver/WebsocketMaskService.java
websocket-driver-0.7.6-java ext/websocket-driver/WebsocketMaskService.java
websocket-driver-0.7.6 ext/websocket-driver/WebsocketMaskService.java
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/websocket-driver-0.7.5/ext/websocket-driver/WebsocketMaskService.java
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/websocket-driver-0.7.5/ext/websocket-driver/WebsocketMaskService.java
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/websocket-driver-0.7.5/ext/websocket-driver/WebsocketMaskService.java
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/websocket-driver-0.7.5/ext/websocket-driver/WebsocketMaskService.java
websocket-driver-0.7.5-java ext/websocket-driver/WebsocketMaskService.java
websocket-driver-0.7.5 ext/websocket-driver/WebsocketMaskService.java
websocket-driver-0.7.4-java ext/websocket-driver/WebsocketMaskService.java
websocket-driver-0.7.4 ext/websocket-driver/WebsocketMaskService.java
websocket-driver-0.7.3-java ext/websocket-driver/WebsocketMaskService.java