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