jdk/src/java.httpclient/share/classes/java/net/http/WSFrame.java
author michaelm
Mon, 16 May 2016 16:04:14 +0100
changeset 38322 f6f9d3ec14ba
parent 37874 02589df0999a
child 39730 196f4e25d9f5
permissions -rw-r--r--
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError Reviewed-by: rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     1
/*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     4
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General  License version 2 only, as
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    10
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General  License
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    15
 * accompanied this code).
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    16
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    17
 * You should have received a copy of the GNU General  License version
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    20
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    23
 * questions.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    24
 */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    25
package java.net.http;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    26
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    27
import java.nio.ByteBuffer;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    28
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    29
import static java.lang.String.format;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    30
import static java.net.http.WSFrame.Opcode.ofCode;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    31
import static java.net.http.WSUtils.dump;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    32
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    33
/*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    34
 * A collection of utilities for reading, writing, and masking frames.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    35
 */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    36
final class WSFrame {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    37
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    38
    private WSFrame() { }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    39
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    40
    static final int MAX_HEADER_SIZE_BYTES = 2 + 8 + 4;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    41
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    42
    enum Opcode {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    43
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    44
        CONTINUATION   (0x0),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    45
        TEXT           (0x1),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    46
        BINARY         (0x2),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    47
        NON_CONTROL_0x3(0x3),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    48
        NON_CONTROL_0x4(0x4),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    49
        NON_CONTROL_0x5(0x5),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    50
        NON_CONTROL_0x6(0x6),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    51
        NON_CONTROL_0x7(0x7),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    52
        CLOSE          (0x8),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    53
        PING           (0x9),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    54
        PONG           (0xA),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    55
        CONTROL_0xB    (0xB),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    56
        CONTROL_0xC    (0xC),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    57
        CONTROL_0xD    (0xD),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    58
        CONTROL_0xE    (0xE),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    59
        CONTROL_0xF    (0xF);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    60
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    61
        private static final Opcode[] opcodes;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    62
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    63
        static {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    64
            Opcode[] values = values();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    65
            opcodes = new Opcode[values.length];
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    66
            for (Opcode c : values) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    67
                assert opcodes[c.code] == null
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    68
                        : WSUtils.dump(c, c.code, opcodes[c.code]);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    69
                opcodes[c.code] = c;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    70
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    71
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    72
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    73
        private final byte code;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    74
        private final char shiftedCode;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    75
        private final String description;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    76
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    77
        Opcode(int code) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    78
            this.code = (byte) code;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    79
            this.shiftedCode = (char) (code << 8);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    80
            this.description = format("%x (%s)", code, name());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    81
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    82
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    83
        boolean isControl() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    84
            return (code & 0x8) != 0;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    85
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    86
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    87
        static Opcode ofCode(int code) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    88
            return opcodes[code & 0xF];
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    89
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    90
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    91
        @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    92
        public String toString() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    93
            return description;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    94
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    95
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    96
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    97
    /*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    98
     * A utility to mask payload data.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    99
     */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   100
    static final class Masker {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   101
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   102
        private final ByteBuffer acc = ByteBuffer.allocate(8);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   103
        private final int[] maskBytes = new int[4];
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   104
        private int offset;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   105
        private long maskLong;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   106
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   107
        /*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   108
         * Sets up the mask.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   109
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   110
        Masker mask(int value) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   111
            acc.clear().putInt(value).putInt(value).flip();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   112
            for (int i = 0; i < maskBytes.length; i++) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   113
                maskBytes[i] = acc.get(i);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   114
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   115
            offset = 0;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   116
            maskLong = acc.getLong(0);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   117
            return this;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   118
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   119
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   120
        /*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   121
         * Reads as many bytes as possible from the given input buffer, writing
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   122
         * the resulting masked bytes to the given output buffer.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   123
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   124
         * src.remaining() <= dst.remaining() // TODO: do we need this restriction?
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   125
         * 'src' and 'dst' can be the same ByteBuffer
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   126
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   127
        Masker applyMask(ByteBuffer src, ByteBuffer dst) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   128
            if (src.remaining() > dst.remaining()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   129
                throw new IllegalArgumentException(dump(src, dst));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   130
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   131
            begin(src, dst);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   132
            loop(src, dst);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   133
            end(src, dst);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   134
            return this;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   135
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   136
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   137
        // Applying the remaining of the mask (strictly not more than 3 bytes)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   138
        // byte-wise
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   139
        private void begin(ByteBuffer src, ByteBuffer dst) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   140
            if (offset > 0) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   141
                for (int i = src.position(), j = dst.position();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   142
                     offset < 4 && i <= src.limit() - 1 && j <= dst.limit() - 1;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   143
                     i++, j++, offset++) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   144
                    dst.put(j, (byte) (src.get(i) ^ maskBytes[offset]));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   145
                    dst.position(j + 1);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   146
                    src.position(i + 1);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   147
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   148
                offset &= 3;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   149
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   150
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   151
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   152
        private void loop(ByteBuffer src, ByteBuffer dst) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   153
            int i = src.position();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   154
            int j = dst.position();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   155
            final int srcLim = src.limit() - 8;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   156
            final int dstLim = dst.limit() - 8;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   157
            for (; i <= srcLim && j <= dstLim; i += 8, j += 8) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   158
                dst.putLong(j, (src.getLong(i) ^ maskLong));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   159
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   160
            if (i > src.limit()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   161
                src.position(i - 8);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   162
            } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   163
                src.position(i);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   164
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   165
            if (j > dst.limit()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   166
                dst.position(j - 8);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   167
            } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   168
                dst.position(j);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   169
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   170
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   171
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   172
        // Applying the mask to the remaining bytes byte-wise (don't make any
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   173
        // assumptions on how many, hopefully not more than 7 for 64bit arch)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   174
        private void end(ByteBuffer src, ByteBuffer dst) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   175
            for (int i = src.position(), j = dst.position();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   176
                 i <= src.limit() - 1 && j <= dst.limit() - 1;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   177
                 i++, j++, offset = (offset + 1) & 3) { // offset cycle through 0..3
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   178
                dst.put(j, (byte) (src.get(i) ^ maskBytes[offset]));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   179
                src.position(i + 1);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   180
                dst.position(j + 1);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   181
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   182
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   183
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   184
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   185
    /*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   186
     * A builder of frame headers, capable of writing to a given buffer.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   187
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   188
     * The builder does not enforce any protocol-level rules, it simply writes
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   189
     * a header structure to the buffer. The order of calls to intermediate
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   190
     * methods is not significant.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   191
     */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   192
    static final class HeaderBuilder {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   193
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   194
        private char firstChar;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   195
        private long payloadLen;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   196
        private int maskingKey;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   197
        private boolean mask;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   198
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   199
        HeaderBuilder fin(boolean value) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   200
            if (value) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   201
                firstChar |=  0b10000000_00000000;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   202
            } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   203
                firstChar &= ~0b10000000_00000000;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   204
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   205
            return this;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   206
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   207
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   208
        HeaderBuilder rsv1(boolean value) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   209
            if (value) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   210
                firstChar |=  0b01000000_00000000;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   211
            } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   212
                firstChar &= ~0b01000000_00000000;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   213
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   214
            return this;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   215
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   216
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   217
        HeaderBuilder rsv2(boolean value) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   218
            if (value) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   219
                firstChar |=  0b00100000_00000000;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   220
            } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   221
                firstChar &= ~0b00100000_00000000;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   222
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   223
            return this;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   224
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   225
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   226
        HeaderBuilder rsv3(boolean value) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   227
            if (value) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   228
                firstChar |=  0b00010000_00000000;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   229
            } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   230
                firstChar &= ~0b00010000_00000000;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   231
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   232
            return this;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   233
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   234
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   235
        HeaderBuilder opcode(Opcode value) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   236
            firstChar = (char) ((firstChar & 0xF0FF) | value.shiftedCode);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   237
            return this;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   238
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   239
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   240
        HeaderBuilder payloadLen(long value) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   241
            payloadLen = value;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   242
            firstChar &= 0b11111111_10000000; // Clear previous payload length leftovers
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   243
            if (payloadLen < 126) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   244
                firstChar |= payloadLen;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   245
            } else if (payloadLen < 65535) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   246
                firstChar |= 126;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   247
            } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   248
                firstChar |= 127;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   249
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   250
            return this;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   251
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   252
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   253
        HeaderBuilder mask(int value) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   254
            firstChar |= 0b00000000_10000000;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   255
            maskingKey = value;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   256
            mask = true;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   257
            return this;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   258
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   259
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   260
        HeaderBuilder noMask() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   261
            firstChar &= ~0b00000000_10000000;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   262
            mask = false;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   263
            return this;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   264
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   265
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   266
        /*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   267
         * Writes the header to the given buffer.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   268
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   269
         * The buffer must have at least MAX_HEADER_SIZE_BYTES remaining. The
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   270
         * buffer's position is incremented by the number of bytes written.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   271
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   272
        void build(ByteBuffer buffer) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   273
            buffer.putChar(firstChar);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   274
            if (payloadLen >= 126) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   275
                if (payloadLen < 65535) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   276
                    buffer.putChar((char) payloadLen);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   277
                } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   278
                    buffer.putLong(payloadLen);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   279
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   280
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   281
            if (mask) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   282
                buffer.putInt(maskingKey);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   283
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   284
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   285
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   286
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   287
    /*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   288
     * A consumer of frame parts.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   289
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   290
     * Guaranteed to be called in the following order by the Frame.Reader:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   291
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   292
     *     fin rsv1 rsv2 rsv3 opcode mask payloadLength maskingKey? payloadData+ endFrame
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   293
     */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   294
    interface Consumer {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   295
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   296
        void fin(boolean value);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   297
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   298
        void rsv1(boolean value);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   299
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   300
        void rsv2(boolean value);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   301
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   302
        void rsv3(boolean value);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   303
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   304
        void opcode(Opcode value);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   305
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   306
        void mask(boolean value);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   307
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   308
        void payloadLen(long value);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   309
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   310
        void maskingKey(int value);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   311
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   312
        /*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   313
         * Called when a part of the payload is ready to be consumed.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   314
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   315
         * Though may not yield a complete payload in a single invocation, i.e.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   316
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   317
         *     data.remaining() < payloadLen
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   318
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   319
         * the sum of `data.remaining()` passed to all invocations of this
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   320
         * method will be equal to 'payloadLen', reported in
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   321
         * `void payloadLen(long value)`
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   322
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   323
         * No unmasking is done.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   324
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   325
        void payloadData(WSShared<ByteBuffer> data, boolean isLast);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   326
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   327
        void endFrame(); // TODO: remove (payloadData(isLast=true)) should be enough
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   328
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   329
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   330
    /*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   331
     * A Reader of Frames.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   332
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   333
     * No protocol-level rules are enforced, only frame structure.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   334
     */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   335
    static final class Reader {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   336
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   337
        private static final int AWAITING_FIRST_BYTE  =  1;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   338
        private static final int AWAITING_SECOND_BYTE =  2;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   339
        private static final int READING_16_LENGTH    =  4;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   340
        private static final int READING_64_LENGTH    =  8;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   341
        private static final int READING_MASK         = 16;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   342
        private static final int READING_PAYLOAD      = 32;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   343
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   344
        // A private buffer used to simplify multi-byte integers reading
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   345
        private final ByteBuffer accumulator = ByteBuffer.allocate(8);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   346
        private int state = AWAITING_FIRST_BYTE;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   347
        private boolean mask;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   348
        private long payloadLength;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   349
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   350
        /*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   351
         * Reads at most one frame from the given buffer invoking the consumer's
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   352
         * methods corresponding to the frame elements found.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   353
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   354
         * As much of the frame's payload, if any, is read. The buffers position
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   355
         * is updated to reflect the number of bytes read.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   356
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   357
         * Throws WSProtocolException if the frame is malformed.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   358
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   359
        void readFrame(WSShared<ByteBuffer> shared, Consumer consumer) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   360
            ByteBuffer input = shared.buffer();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   361
            loop:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   362
            while (true) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   363
                byte b;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   364
                switch (state) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   365
                    case AWAITING_FIRST_BYTE:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   366
                        if (!input.hasRemaining()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   367
                            break loop;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   368
                        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   369
                        b = input.get();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   370
                        consumer.fin( (b & 0b10000000) != 0);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   371
                        consumer.rsv1((b & 0b01000000) != 0);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   372
                        consumer.rsv2((b & 0b00100000) != 0);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   373
                        consumer.rsv3((b & 0b00010000) != 0);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   374
                        consumer.opcode(ofCode(b));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   375
                        state = AWAITING_SECOND_BYTE;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   376
                        continue loop;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   377
                    case AWAITING_SECOND_BYTE:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   378
                        if (!input.hasRemaining()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   379
                            break loop;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   380
                        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   381
                        b = input.get();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   382
                        consumer.mask(mask = (b & 0b10000000) != 0);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   383
                        byte p1 = (byte) (b & 0b01111111);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   384
                        if (p1 < 126) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   385
                            assert p1 >= 0 : p1;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   386
                            consumer.payloadLen(payloadLength = p1);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   387
                            state = mask ? READING_MASK : READING_PAYLOAD;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   388
                        } else if (p1 < 127) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   389
                            state = READING_16_LENGTH;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   390
                        } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   391
                            state = READING_64_LENGTH;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   392
                        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   393
                        continue loop;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   394
                    case READING_16_LENGTH:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   395
                        if (!input.hasRemaining()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   396
                            break loop;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   397
                        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   398
                        b = input.get();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   399
                        if (accumulator.put(b).position() < 2) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   400
                            continue loop;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   401
                        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   402
                        payloadLength = accumulator.flip().getChar();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   403
                        if (payloadLength < 126) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   404
                            throw notMinimalEncoding(payloadLength, 2);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   405
                        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   406
                        consumer.payloadLen(payloadLength);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   407
                        accumulator.clear();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   408
                        state = mask ? READING_MASK : READING_PAYLOAD;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   409
                        continue loop;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   410
                    case READING_64_LENGTH:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   411
                        if (!input.hasRemaining()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   412
                            break loop;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   413
                        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   414
                        b = input.get();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   415
                        if (accumulator.put(b).position() < 8) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   416
                            continue loop;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   417
                        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   418
                        payloadLength = accumulator.flip().getLong();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   419
                        if (payloadLength < 0) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   420
                            throw negativePayload(payloadLength);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   421
                        } else if (payloadLength < 65535) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   422
                            throw notMinimalEncoding(payloadLength, 8);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   423
                        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   424
                        consumer.payloadLen(payloadLength);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   425
                        accumulator.clear();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   426
                        state = mask ? READING_MASK : READING_PAYLOAD;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   427
                        continue loop;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   428
                    case READING_MASK:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   429
                        if (!input.hasRemaining()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   430
                            break loop;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   431
                        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   432
                        b = input.get();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   433
                        if (accumulator.put(b).position() != 4) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   434
                            continue loop;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   435
                        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   436
                        consumer.maskingKey(accumulator.flip().getInt());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   437
                        accumulator.clear();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   438
                        state = READING_PAYLOAD;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   439
                        continue loop;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   440
                    case READING_PAYLOAD:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   441
                        // This state does not require any bytes to be available
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   442
                        // in the input buffer in order to proceed
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   443
                        boolean fullyRead;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   444
                        int limit;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   445
                        if (payloadLength <= input.remaining()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   446
                            limit = input.position() + (int) payloadLength;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   447
                            payloadLength = 0;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   448
                            fullyRead = true;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   449
                        } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   450
                            limit = input.limit();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   451
                            payloadLength -= input.remaining();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   452
                            fullyRead = false;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   453
                        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   454
                        // FIXME: consider a case where payloadLen != 0,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   455
                        // but input.remaining() == 0
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   456
                        //
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   457
                        // There shouldn't be an invocation of payloadData with
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   458
                        // an empty buffer, as it would be an artifact of
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   459
                        // reading
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   460
                        consumer.payloadData(shared.share(input.position(), limit), fullyRead);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   461
                        // Update the position manually, since reading the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   462
                        // payload doesn't advance buffer's position
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   463
                        input.position(limit);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   464
                        if (fullyRead) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   465
                            consumer.endFrame();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   466
                            state = AWAITING_FIRST_BYTE;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   467
                        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   468
                        break loop;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   469
                    default:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   470
                        throw new InternalError(String.valueOf(state));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   471
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   472
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   473
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   474
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   475
        private static WSProtocolException negativePayload(long payloadLength) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   476
            return new WSProtocolException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   477
                    ("5.2.", format("Negative 64-bit payload length %s", payloadLength));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   478
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   479
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   480
        private static WSProtocolException notMinimalEncoding(long payloadLength, int numBytes) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   481
            return new WSProtocolException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   482
                    ("5.2.", format("Payload length (%s) is not encoded with minimal number (%s) of bytes",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   483
                            payloadLength, numBytes));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   484
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   485
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   486
}