src/java.net.http/share/classes/jdk/internal/net/http/websocket/MessageDecoder.java
author prappo
Mon, 19 Mar 2018 14:20:18 +0000
branchhttp-client-branch
changeset 56320 f82729ca8660
parent 56304 065641767a75
child 56389 0ba90c4f1e3f
permissions -rw-r--r--
http-client-branch: (WebSocket) removed MessagePart
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) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    76
        debug.log(Level.DEBUG, "fin %s", value);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    77
        fin = value;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    78
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    79
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    80
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    81
    public void rsv1(boolean value) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    82
        debug.log(Level.DEBUG, "rsv1 %s", value);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    83
        if (value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    84
            throw new FailWebSocketException("Unexpected rsv1 bit");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    85
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    86
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    87
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    88
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    89
    public void rsv2(boolean value) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    90
        debug.log(Level.DEBUG, "rsv2 %s", value);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    91
        if (value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    92
            throw new FailWebSocketException("Unexpected rsv2 bit");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    93
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    94
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    95
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    96
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    97
    public void rsv3(boolean value) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    98
        debug.log(Level.DEBUG, "rsv3 %s", value);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    99
        if (value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   100
            throw new FailWebSocketException("Unexpected rsv3 bit");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   101
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   102
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   103
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   104
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   105
    public void opcode(Opcode v) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
   106
        debug.log(Level.DEBUG, "opcode %s", v);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   107
        if (v == Opcode.PING || v == Opcode.PONG || v == Opcode.CLOSE) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   108
            if (!fin) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   109
                throw new FailWebSocketException("Fragmented control frame  " + v);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   110
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   111
            opcode = v;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   112
        } else if (v == Opcode.TEXT || v == Opcode.BINARY) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   113
            if (originatingOpcode != null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   114
                throw new FailWebSocketException(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   115
                        format("Unexpected frame %s (fin=%s)", v, fin));
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   116
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   117
            opcode = v;
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
                originatingOpcode = 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
        } else if (v == Opcode.CONTINUATION) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   122
            if (originatingOpcode == null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   123
                throw new FailWebSocketException(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   124
                        format("Unexpected frame %s (fin=%s)", v, fin));
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   125
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   126
            opcode = v;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   127
        } else {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   128
            throw new FailWebSocketException("Unexpected opcode " + v);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   129
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   130
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   131
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   132
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   133
    public void mask(boolean value) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
   134
        debug.log(Level.DEBUG, "mask %s", value);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   135
        if (value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   136
            throw new FailWebSocketException("Masked frame received");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   137
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   138
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   139
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   140
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   141
    public void payloadLen(long value) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
   142
        debug.log(Level.DEBUG, "payloadLen %s", value);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   143
        if (opcode.isControl()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   144
            if (value > Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   145
                throw new FailWebSocketException(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   146
                        format("%s's payload length %s", opcode, value));
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   147
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   148
            assert Opcode.CLOSE.isControl();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   149
            if (opcode == Opcode.CLOSE && value == 1) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   150
                throw new FailWebSocketException("Incomplete status code");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   151
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   152
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   153
        payloadLen = value;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   154
        unconsumedPayloadLen = value;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   155
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   156
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   157
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   158
    public void maskingKey(int value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   159
        // `MessageDecoder.mask(boolean)` is where a masked frame is detected and
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   160
        // reported on; `MessageDecoder.mask(boolean)` MUST be invoked before
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   161
        // this method;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   162
        // So this method (`maskingKey`) is not supposed to be invoked while
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   163
        // reading a frame that has came from the server. If this method is
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   164
        // invoked, then it's an error in implementation, thus InternalError
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   165
        throw new InternalError();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   166
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   167
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   168
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   169
    public void payloadData(ByteBuffer data) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
   170
        debug.log(Level.DEBUG, "payload %s", data);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   171
        unconsumedPayloadLen -= data.remaining();
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56304
diff changeset
   172
        boolean lastPayloadChunk = unconsumedPayloadLen == 0;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   173
        if (opcode.isControl()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   174
            if (binaryData != null) { // An intermediate or the last chunk
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   175
                binaryData.put(data);
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56304
diff changeset
   176
            } else if (!lastPayloadChunk) { // The first chunk
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   177
                int remaining = data.remaining();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   178
                // It shouldn't be 125, otherwise the next chunk will be of size
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   179
                // 0, which is not what Reader promises to deliver (eager
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   180
                // reading)
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   181
                assert remaining < Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   182
                        : dump(remaining);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   183
                binaryData = ByteBuffer.allocate(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   184
                        Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH).put(data);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   185
            } else { // The only chunk
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   186
                binaryData = ByteBuffer.allocate(data.remaining()).put(data);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   187
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   188
        } else {
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56304
diff changeset
   189
            boolean last = fin && lastPayloadChunk;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   190
            boolean text = opcode == Opcode.TEXT || originatingOpcode == Opcode.TEXT;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   191
            if (!text) {
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56304
diff changeset
   192
                output.onBinary(data.slice(), last);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   193
                data.position(data.limit()); // Consume
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   194
            } else {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   195
                boolean binaryNonEmpty = data.hasRemaining();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   196
                CharBuffer textData;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   197
                try {
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56304
diff changeset
   198
                    textData = decoder.decode(data, last);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   199
                } catch (CharacterCodingException e) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   200
                    throw new FailWebSocketException(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   201
                            "Invalid UTF-8 in frame " + opcode,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   202
                            StatusCodes.NOT_CONSISTENT).initCause(e);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   203
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   204
                if (!(binaryNonEmpty && !textData.hasRemaining())) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   205
                    // 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
   206
                    // don't deliver anything, otherwise:
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56304
diff changeset
   207
                    output.onText(textData, last);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   208
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   209
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   210
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   211
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   212
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   213
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   214
    public void endFrame() {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
   215
        debug.log(Level.DEBUG, "end frame");
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   216
        if (opcode.isControl()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   217
            binaryData.flip();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   218
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   219
        switch (opcode) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   220
            case CLOSE:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   221
                char statusCode = NO_STATUS_CODE;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   222
                String reason = "";
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   223
                if (payloadLen != 0) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   224
                    int len = binaryData.remaining();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   225
                    assert 2 <= len
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   226
                            && len <= Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   227
                            : dump(len, payloadLen);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   228
                    statusCode = binaryData.getChar();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   229
                    if (!isLegalToReceiveFromServer(statusCode)) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   230
                        throw new FailWebSocketException(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   231
                                "Illegal status code: " + statusCode);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   232
                    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   233
                    try {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   234
                        reason = UTF_8.newDecoder().decode(binaryData).toString();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   235
                    } catch (CharacterCodingException e) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   236
                        throw new FailWebSocketException("Illegal close reason")
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   237
                                .initCause(e);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   238
                    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   239
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   240
                output.onClose(statusCode, reason);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   241
                break;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   242
            case PING:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   243
                output.onPing(binaryData);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   244
                binaryData = null;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   245
                break;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   246
            case PONG:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   247
                output.onPong(binaryData);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   248
                binaryData = null;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   249
                break;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   250
            default:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   251
                assert opcode == Opcode.TEXT || opcode == Opcode.BINARY
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   252
                        || opcode == Opcode.CONTINUATION : dump(opcode);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   253
                if (fin) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   254
                    // It is always the last chunk:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   255
                    // either TEXT(FIN=TRUE)/BINARY(FIN=TRUE) or CONT(FIN=TRUE)
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   256
                    originatingOpcode = null;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   257
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   258
                break;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   259
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   260
        payloadLen = 0;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   261
        opcode = null;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   262
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   263
}