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