src/java.net.http/share/classes/jdk/internal/net/http/websocket/MessageDecoder.java
author prappo
Fri, 06 Apr 2018 15:20:43 +0100
branchhttp-client-branch
changeset 56389 0ba90c4f1e3f
parent 56320 f82729ca8660
child 56437 f8b3f053cfbb
permissions -rw-r--r--
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     1
/*
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     4
 *
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    10
 *
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    15
 * accompanied this code).
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    16
 *
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    20
 *
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    23
 * questions.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    24
 */
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    25
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    26
package jdk.internal.net.http.websocket;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    27
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    28
import jdk.internal.net.http.common.Utils;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    29
import jdk.internal.net.http.websocket.Frame.Opcode;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    30
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    31
import java.nio.ByteBuffer;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    32
import java.nio.CharBuffer;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    33
import java.nio.charset.CharacterCodingException;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    34
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    35
import static java.lang.String.format;
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    36
import java.lang.System.Logger.Level;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    37
import static java.nio.charset.StandardCharsets.UTF_8;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    38
import static java.util.Objects.requireNonNull;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    39
import static jdk.internal.net.http.common.Utils.dump;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    40
import static jdk.internal.net.http.websocket.StatusCodes.NO_STATUS_CODE;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    41
import static jdk.internal.net.http.websocket.StatusCodes.isLegalToReceiveFromServer;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    42
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    43
/*
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    44
 * Consumes frame parts and notifies a message consumer, when there is
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    45
 * sufficient data to produce a message, or part thereof.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    46
 *
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    47
 * Data consumed but not yet translated is accumulated until it's sufficient to
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    48
 * form a message.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    49
 */
56290
e178d19ff91c http-client-branch: (WebSocket) minor cleanup
prappo
parents: 56263
diff changeset
    50
/* Exposed for testing purposes */
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    51
class MessageDecoder implements Frame.Consumer {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    52
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    53
    private static final boolean DEBUG = Utils.DEBUG_WS;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    54
    private static final System.Logger debug =
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    55
            Utils.getWebSocketLogger("[Input]"::toString, DEBUG);
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    56
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    57
    private final MessageStreamConsumer output;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    58
    private final UTF8AccumulatingDecoder decoder = new UTF8AccumulatingDecoder();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    59
    private boolean fin;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    60
    private Opcode opcode, originatingOpcode;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    61
    private long payloadLen;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    62
    private long unconsumedPayloadLen;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    63
    private ByteBuffer binaryData;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    64
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    65
    MessageDecoder(MessageStreamConsumer output) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    66
        this.output = requireNonNull(output);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    67
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    68
56290
e178d19ff91c http-client-branch: (WebSocket) minor cleanup
prappo
parents: 56263
diff changeset
    69
    /* Exposed for testing purposes */
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    70
    MessageStreamConsumer getOutput() {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    71
        return output;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    72
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    73
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    74
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    75
    public void fin(boolean value) {
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
    76
        if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
    77
            debug.log(Level.DEBUG, "fin %s", value);
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
    78
        }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    79
        fin = value;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    80
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    81
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    82
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    83
    public void rsv1(boolean value) {
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
    84
        if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
    85
            debug.log(Level.DEBUG, "rsv1 %s", value);
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
    86
        }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    87
        if (value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    88
            throw new FailWebSocketException("Unexpected rsv1 bit");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    89
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    90
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    91
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    92
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    93
    public void rsv2(boolean value) {
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
    94
        if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
    95
            debug.log(Level.DEBUG, "rsv2 %s", value);
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
    96
        }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    97
        if (value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    98
            throw new FailWebSocketException("Unexpected rsv2 bit");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    99
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   100
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   101
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   102
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   103
    public void rsv3(boolean value) {
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
   104
        if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
   105
            debug.log(Level.DEBUG, "rsv3 %s", value);
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
   106
        }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   107
        if (value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   108
            throw new FailWebSocketException("Unexpected rsv3 bit");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   109
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   110
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   111
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   112
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   113
    public void opcode(Opcode v) {
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
   114
        if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
   115
            debug.log(Level.DEBUG, "opcode %s", v);
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
   116
        }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   117
        if (v == Opcode.PING || v == Opcode.PONG || v == Opcode.CLOSE) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   118
            if (!fin) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   119
                throw new FailWebSocketException("Fragmented control frame  " + v);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   120
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   121
            opcode = v;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   122
        } else if (v == Opcode.TEXT || v == Opcode.BINARY) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   123
            if (originatingOpcode != null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   124
                throw new FailWebSocketException(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   125
                        format("Unexpected frame %s (fin=%s)", v, fin));
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   126
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   127
            opcode = v;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   128
            if (!fin) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   129
                originatingOpcode = v;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   130
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   131
        } else if (v == Opcode.CONTINUATION) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   132
            if (originatingOpcode == null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   133
                throw new FailWebSocketException(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   134
                        format("Unexpected frame %s (fin=%s)", v, fin));
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   135
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   136
            opcode = v;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   137
        } else {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   138
            throw new FailWebSocketException("Unexpected opcode " + v);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   139
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   140
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   141
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   142
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   143
    public void mask(boolean value) {
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
   144
        if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
   145
            debug.log(Level.DEBUG, "mask %s", value);
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
   146
        }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   147
        if (value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   148
            throw new FailWebSocketException("Masked frame received");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   149
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   150
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   151
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   152
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   153
    public void payloadLen(long value) {
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
   154
        if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
   155
            debug.log(Level.DEBUG, "payloadLen %s", value);
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
   156
        }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   157
        if (opcode.isControl()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   158
            if (value > Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   159
                throw new FailWebSocketException(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   160
                        format("%s's payload length %s", opcode, value));
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   161
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   162
            assert Opcode.CLOSE.isControl();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   163
            if (opcode == Opcode.CLOSE && value == 1) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   164
                throw new FailWebSocketException("Incomplete status code");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   165
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   166
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   167
        payloadLen = value;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   168
        unconsumedPayloadLen = value;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   169
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   170
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   171
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   172
    public void maskingKey(int value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   173
        // `MessageDecoder.mask(boolean)` is where a masked frame is detected and
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   174
        // reported on; `MessageDecoder.mask(boolean)` MUST be invoked before
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   175
        // this method;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   176
        // So this method (`maskingKey`) is not supposed to be invoked while
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   177
        // reading a frame that has came from the server. If this method is
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   178
        // invoked, then it's an error in implementation, thus InternalError
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   179
        throw new InternalError();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   180
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   181
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   182
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   183
    public void payloadData(ByteBuffer data) {
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
   184
        if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
   185
            debug.log(Level.DEBUG, "payload %s", data);
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
   186
        }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   187
        unconsumedPayloadLen -= data.remaining();
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56304
diff changeset
   188
        boolean lastPayloadChunk = unconsumedPayloadLen == 0;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   189
        if (opcode.isControl()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   190
            if (binaryData != null) { // An intermediate or the last chunk
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   191
                binaryData.put(data);
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56304
diff changeset
   192
            } else if (!lastPayloadChunk) { // The first chunk
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   193
                int remaining = data.remaining();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   194
                // It shouldn't be 125, otherwise the next chunk will be of size
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   195
                // 0, which is not what Reader promises to deliver (eager
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   196
                // reading)
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   197
                assert remaining < Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   198
                        : dump(remaining);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   199
                binaryData = ByteBuffer.allocate(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   200
                        Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH).put(data);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   201
            } else { // The only chunk
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   202
                binaryData = ByteBuffer.allocate(data.remaining()).put(data);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   203
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   204
        } else {
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56304
diff changeset
   205
            boolean last = fin && lastPayloadChunk;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   206
            boolean text = opcode == Opcode.TEXT || originatingOpcode == Opcode.TEXT;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   207
            if (!text) {
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56304
diff changeset
   208
                output.onBinary(data.slice(), last);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   209
                data.position(data.limit()); // Consume
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   210
            } else {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   211
                boolean binaryNonEmpty = data.hasRemaining();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   212
                CharBuffer textData;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   213
                try {
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56304
diff changeset
   214
                    textData = decoder.decode(data, last);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   215
                } catch (CharacterCodingException e) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   216
                    throw new FailWebSocketException(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   217
                            "Invalid UTF-8 in frame " + opcode,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   218
                            StatusCodes.NOT_CONSISTENT).initCause(e);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   219
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   220
                if (!(binaryNonEmpty && !textData.hasRemaining())) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   221
                    // If there's a binary data, that result in no text, then we
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56304
diff changeset
   222
                    // don't deliver anything, otherwise:
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56304
diff changeset
   223
                    output.onText(textData, last);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   224
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   225
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   226
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   227
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   228
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   229
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   230
    public void endFrame() {
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
   231
        if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
   232
            debug.log(Level.DEBUG, "end frame");
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56320
diff changeset
   233
        }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   234
        if (opcode.isControl()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   235
            binaryData.flip();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   236
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   237
        switch (opcode) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   238
            case CLOSE:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   239
                char statusCode = NO_STATUS_CODE;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   240
                String reason = "";
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   241
                if (payloadLen != 0) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   242
                    int len = binaryData.remaining();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   243
                    assert 2 <= len
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   244
                            && len <= Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   245
                            : dump(len, payloadLen);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   246
                    statusCode = binaryData.getChar();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   247
                    if (!isLegalToReceiveFromServer(statusCode)) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   248
                        throw new FailWebSocketException(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   249
                                "Illegal status code: " + statusCode);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   250
                    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   251
                    try {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   252
                        reason = UTF_8.newDecoder().decode(binaryData).toString();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   253
                    } catch (CharacterCodingException e) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   254
                        throw new FailWebSocketException("Illegal close reason")
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   255
                                .initCause(e);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   256
                    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   257
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   258
                output.onClose(statusCode, reason);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   259
                break;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   260
            case PING:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   261
                output.onPing(binaryData);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   262
                binaryData = null;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   263
                break;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   264
            case PONG:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   265
                output.onPong(binaryData);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   266
                binaryData = null;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   267
                break;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   268
            default:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   269
                assert opcode == Opcode.TEXT || opcode == Opcode.BINARY
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   270
                        || opcode == Opcode.CONTINUATION : dump(opcode);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   271
                if (fin) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   272
                    // It is always the last chunk:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   273
                    // either TEXT(FIN=TRUE)/BINARY(FIN=TRUE) or CONT(FIN=TRUE)
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   274
                    originatingOpcode = null;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   275
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   276
                break;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   277
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   278
        payloadLen = 0;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   279
        opcode = null;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   280
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   281
}