src/java.net.http/share/classes/jdk/internal/net/http/websocket/MessageDecoder.java
author dfuchs
Wed, 14 Mar 2018 13:35:33 +0000
branchhttp-client-branch
changeset 56304 065641767a75
parent 56295 898dfb226bd0
child 56320 f82729ca8660
permissions -rw-r--r--
http-client-branch: change websocket to use System.Logger for debug logging
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.net.http.WebSocket.MessagePart;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    32
import java.nio.ByteBuffer;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    33
import java.nio.CharBuffer;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    34
import java.nio.charset.CharacterCodingException;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    35
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    36
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
    37
import java.lang.System.Logger.Level;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    38
import static java.nio.charset.StandardCharsets.UTF_8;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    39
import static java.util.Objects.requireNonNull;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    40
import static jdk.internal.net.http.common.Utils.dump;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    41
import static jdk.internal.net.http.websocket.StatusCodes.NO_STATUS_CODE;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    42
import static jdk.internal.net.http.websocket.StatusCodes.isLegalToReceiveFromServer;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    43
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    44
/*
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    45
 * Consumes frame parts and notifies a message consumer, when there is
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    46
 * sufficient data to produce a message, or part thereof.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    47
 *
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    48
 * Data consumed but not yet translated is accumulated until it's sufficient to
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    49
 * form a message.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    50
 */
56290
e178d19ff91c http-client-branch: (WebSocket) minor cleanup
prappo
parents: 56263
diff changeset
    51
/* Exposed for testing purposes */
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    52
class MessageDecoder implements Frame.Consumer {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    53
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    54
    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
    55
    private static final System.Logger debug =
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    56
            Utils.getWebSocketLogger("[Input]"::toString, DEBUG);
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    57
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    58
    private final MessageStreamConsumer output;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    59
    private final UTF8AccumulatingDecoder decoder = new UTF8AccumulatingDecoder();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    60
    private boolean fin;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    61
    private Opcode opcode, originatingOpcode;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    62
    private MessagePart part = MessagePart.WHOLE;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    63
    private long payloadLen;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    64
    private long unconsumedPayloadLen;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    65
    private ByteBuffer binaryData;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    66
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    67
    MessageDecoder(MessageStreamConsumer output) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    68
        this.output = requireNonNull(output);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    69
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    70
56290
e178d19ff91c http-client-branch: (WebSocket) minor cleanup
prappo
parents: 56263
diff changeset
    71
    /* Exposed for testing purposes */
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    72
    MessageStreamConsumer getOutput() {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    73
        return output;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    74
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    75
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    76
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    77
    public void fin(boolean value) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    78
        debug.log(Level.DEBUG, "fin %s", value);
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) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    84
        debug.log(Level.DEBUG, "rsv1 %s", value);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    85
        if (value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    86
            throw new FailWebSocketException("Unexpected rsv1 bit");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    87
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    88
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    89
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    90
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    91
    public void rsv2(boolean value) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    92
        debug.log(Level.DEBUG, "rsv2 %s", value);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    93
        if (value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    94
            throw new FailWebSocketException("Unexpected rsv2 bit");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    95
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    96
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    97
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    98
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    99
    public void rsv3(boolean value) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
   100
        debug.log(Level.DEBUG, "rsv3 %s", value);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   101
        if (value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   102
            throw new FailWebSocketException("Unexpected rsv3 bit");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   103
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   104
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   105
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   106
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   107
    public void opcode(Opcode v) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
   108
        debug.log(Level.DEBUG, "opcode %s", v);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   109
        if (v == Opcode.PING || v == Opcode.PONG || v == Opcode.CLOSE) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   110
            if (!fin) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   111
                throw new FailWebSocketException("Fragmented control frame  " + v);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   112
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   113
            opcode = v;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   114
        } else if (v == Opcode.TEXT || v == Opcode.BINARY) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   115
            if (originatingOpcode != null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   116
                throw new FailWebSocketException(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   117
                        format("Unexpected frame %s (fin=%s)", v, fin));
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   118
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   119
            opcode = v;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   120
            if (!fin) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   121
                originatingOpcode = v;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   122
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   123
        } else if (v == Opcode.CONTINUATION) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   124
            if (originatingOpcode == null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   125
                throw new FailWebSocketException(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   126
                        format("Unexpected frame %s (fin=%s)", v, fin));
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   127
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   128
            opcode = v;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   129
        } else {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   130
            throw new FailWebSocketException("Unexpected opcode " + v);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   131
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   132
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   133
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   134
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   135
    public void mask(boolean value) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
   136
        debug.log(Level.DEBUG, "mask %s", value);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   137
        if (value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   138
            throw new FailWebSocketException("Masked frame received");
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 payloadLen(long value) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
   144
        debug.log(Level.DEBUG, "payloadLen %s", value);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   145
        if (opcode.isControl()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   146
            if (value > Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   147
                throw new FailWebSocketException(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   148
                        format("%s's payload length %s", opcode, value));
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   149
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   150
            assert Opcode.CLOSE.isControl();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   151
            if (opcode == Opcode.CLOSE && value == 1) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   152
                throw new FailWebSocketException("Incomplete status code");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   153
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   154
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   155
        payloadLen = value;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   156
        unconsumedPayloadLen = value;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   157
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   158
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   159
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   160
    public void maskingKey(int value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   161
        // `MessageDecoder.mask(boolean)` is where a masked frame is detected and
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   162
        // reported on; `MessageDecoder.mask(boolean)` MUST be invoked before
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   163
        // this method;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   164
        // So this method (`maskingKey`) is not supposed to be invoked while
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   165
        // reading a frame that has came from the server. If this method is
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   166
        // invoked, then it's an error in implementation, thus InternalError
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   167
        throw new InternalError();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   168
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   169
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   170
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   171
    public void payloadData(ByteBuffer data) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
   172
        debug.log(Level.DEBUG, "payload %s", data);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   173
        unconsumedPayloadLen -= data.remaining();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   174
        boolean isLast = unconsumedPayloadLen == 0;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   175
        if (opcode.isControl()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   176
            if (binaryData != null) { // An intermediate or the last chunk
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   177
                binaryData.put(data);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   178
            } else if (!isLast) { // The first chunk
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   179
                int remaining = data.remaining();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   180
                // It shouldn't be 125, otherwise the next chunk will be of size
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   181
                // 0, which is not what Reader promises to deliver (eager
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   182
                // reading)
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   183
                assert remaining < Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   184
                        : dump(remaining);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   185
                binaryData = ByteBuffer.allocate(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   186
                        Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH).put(data);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   187
            } else { // The only chunk
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   188
                binaryData = ByteBuffer.allocate(data.remaining()).put(data);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   189
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   190
        } else {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   191
            part = determinePart(isLast);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   192
            boolean text = opcode == Opcode.TEXT || originatingOpcode == Opcode.TEXT;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   193
            if (!text) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   194
                output.onBinary(data.slice(), part);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   195
                data.position(data.limit()); // Consume
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   196
            } else {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   197
                boolean binaryNonEmpty = data.hasRemaining();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   198
                CharBuffer textData;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   199
                try {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   200
                    boolean eof = part == MessagePart.WHOLE
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   201
                            || part == MessagePart.LAST;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   202
                    textData = decoder.decode(data, eof);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   203
                } catch (CharacterCodingException e) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   204
                    throw new FailWebSocketException(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   205
                            "Invalid UTF-8 in frame " + opcode,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   206
                            StatusCodes.NOT_CONSISTENT).initCause(e);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   207
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   208
                if (!(binaryNonEmpty && !textData.hasRemaining())) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   209
                    // If there's a binary data, that result in no text, then we
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   210
                    // don't deliver anything
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   211
                    output.onText(textData, part);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   212
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   213
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   214
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   215
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   216
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   217
    @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   218
    public void endFrame() {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
   219
        debug.log(Level.DEBUG, "end frame");
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   220
        if (opcode.isControl()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   221
            binaryData.flip();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   222
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   223
        switch (opcode) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   224
            case CLOSE:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   225
                char statusCode = NO_STATUS_CODE;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   226
                String reason = "";
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   227
                if (payloadLen != 0) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   228
                    int len = binaryData.remaining();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   229
                    assert 2 <= len
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   230
                            && len <= Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   231
                            : dump(len, payloadLen);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   232
                    statusCode = binaryData.getChar();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   233
                    if (!isLegalToReceiveFromServer(statusCode)) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   234
                        throw new FailWebSocketException(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   235
                                "Illegal status code: " + statusCode);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   236
                    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   237
                    try {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   238
                        reason = UTF_8.newDecoder().decode(binaryData).toString();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   239
                    } catch (CharacterCodingException e) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   240
                        throw new FailWebSocketException("Illegal close reason")
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   241
                                .initCause(e);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   242
                    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   243
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   244
                output.onClose(statusCode, reason);
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 PING:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   247
                output.onPing(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
            case PONG:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   251
                output.onPong(binaryData);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   252
                binaryData = null;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   253
                break;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   254
            default:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   255
                assert opcode == Opcode.TEXT || opcode == Opcode.BINARY
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   256
                        || opcode == Opcode.CONTINUATION : dump(opcode);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   257
                if (fin) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   258
                    // It is always the last chunk:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   259
                    // either TEXT(FIN=TRUE)/BINARY(FIN=TRUE) or CONT(FIN=TRUE)
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   260
                    originatingOpcode = null;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   261
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   262
                break;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   263
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   264
        payloadLen = 0;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   265
        opcode = null;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   266
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   267
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   268
    private MessagePart determinePart(boolean isLast) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   269
        boolean lastChunk = fin && isLast;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   270
        switch (part) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   271
            case LAST:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   272
            case WHOLE:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   273
                return lastChunk ? MessagePart.WHOLE : MessagePart.FIRST;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   274
            case FIRST:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   275
            case PART:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   276
                return lastChunk ? MessagePart.LAST : MessagePart.PART;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   277
            default:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   278
                throw new InternalError(String.valueOf(part));
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   279
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   280
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   281
}