jdk/src/java.httpclient/share/classes/java/net/http/DataFrame.java
author vinnie
Mon, 15 Aug 2016 14:25:51 +0100
changeset 40276 2217f830ca7b
parent 39730 196f4e25d9f5
permissions -rw-r--r--
8163503: PKCS12 keystore cannot store non-X.509 certificates Reviewed-by: weijun, xuelei, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     1
/*
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     4
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    10
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    15
 * accompanied this code).
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    16
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    20
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
39730
196f4e25d9f5 8160993: Fix headers in the java/net/http package
chegar
parents: 37720
diff changeset
    23
 * questions.
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    24
 */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    25
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    26
package java.net.http;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    27
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    28
import java.io.IOException;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    29
import java.nio.ByteBuffer;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    30
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    31
class DataFrame extends Http2Frame {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    32
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    33
    public final static int TYPE = 0x0;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    34
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    35
    DataFrame() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    36
        type = TYPE;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    37
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    38
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    39
    // Flags
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    40
    public static final int END_STREAM = 0x1;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    41
    public static final int PADDED = 0x8;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    42
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    43
    int padLength;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    44
    int dataLength;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    45
    ByteBuffer[] data;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    46
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    47
    public void setData(ByteBuffer[] data) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    48
        this.data = data;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    49
        setDataLength();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    50
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    51
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    52
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    53
    String flagAsString(int flag) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    54
        switch (flag) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    55
        case END_STREAM:
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    56
            return "END_STREAM";
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    57
        case PADDED:
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    58
            return "PADDED";
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    59
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    60
        return super.flagAsString(flag);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    61
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    62
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    63
    public synchronized void setData(ByteBuffer data) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    64
        ByteBuffer[] bb;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    65
        if (data == null) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    66
            bb = new ByteBuffer[0];
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    67
        } else {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    68
            bb = new ByteBuffer[1];
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    69
            bb[0] = data;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    70
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    71
        setData(bb);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    72
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    73
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    74
    public synchronized ByteBuffer[] getData() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    75
        return data;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    76
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    77
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    78
    private void setDataLength() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    79
        int len = 0;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    80
        for (ByteBuffer buf : data) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    81
            len += buf.remaining();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    82
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    83
        dataLength = len;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    84
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    85
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    86
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    87
    void readIncomingImpl(ByteBufferConsumer bc) throws IOException {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    88
        if ((flags & PADDED) != 0) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    89
            padLength = bc.getByte();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    90
            dataLength = length - (padLength + 1);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    91
        } else {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    92
            dataLength = length;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    93
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    94
        data = bc.getBuffers(dataLength);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    95
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    96
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    97
    int getPadLength() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    98
        return padLength;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    99
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   100
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   101
    int getDataLength() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   102
        return dataLength;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   103
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   104
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   105
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   106
    void writeOutgoing(ByteBufferGenerator bg) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   107
        super.writeOutgoing(bg);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   108
        if ((flags & PADDED) != 0) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   109
            ByteBuffer buf = bg.getBuffer(1);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   110
            buf.put((byte)getPadLength());
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   111
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   112
        for (int i=0; i<data.length; i++) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   113
            bg.addByteBuffer(data[i]);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   114
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   115
        if ((flags & PADDED) != 0) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   116
            bg.addPadding(padLength);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   117
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   118
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   119
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   120
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   121
    void computeLength() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   122
        length = dataLength;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   123
        if ((flags & PADDED) != 0) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   124
            length += (1 + padLength);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   125
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   126
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   127
}