jdk/src/java.httpclient/share/classes/java/net/http/CharsetToolkit.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
/*
39730
196f4e25d9f5 8160993: Fix headers in the java/net/http package
chegar
parents: 37720
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
37720
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
 */
39730
196f4e25d9f5 8160993: Fix headers in the java/net/http package
chegar
parents: 37720
diff changeset
    25
37720
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.nio.ByteBuffer;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    29
import java.nio.CharBuffer;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    30
import java.nio.charset.CharacterCodingException;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    31
import java.nio.charset.CharsetDecoder;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    32
import java.nio.charset.CharsetEncoder;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    33
import java.nio.charset.CoderResult;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    34
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    35
import static java.nio.charset.StandardCharsets.UTF_8;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    36
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    37
// The purpose of this class is to separate charset-related tasks from the main
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    38
// WebSocket logic, simplifying where possible.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    39
//
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    40
//     * Coders hide the differences between coding and flushing stages on the
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    41
//       API level
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    42
//     * Verifier abstracts the way the verification is performed
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    43
//       (spoiler: it's a decoding into a throw-away buffer)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    44
//
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    45
// Coding methods throw exceptions instead of returning coding result denoting
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    46
// errors, since any kind of handling and recovery is not expected.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    47
final class CharsetToolkit {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    48
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    49
    private CharsetToolkit() { }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    50
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    51
    static final class Verifier {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    52
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    53
        private final CharsetDecoder decoder = UTF_8.newDecoder();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    54
        // A buffer used to check validity of UTF-8 byte stream by decoding it.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    55
        // The contents of this buffer are never used.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    56
        // The size is arbitrary, though it should probably be chosen from the
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    57
        // performance perspective since it affects the total number of calls to
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    58
        // decoder.decode() and amount of work in each of these calls
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    59
        private final CharBuffer blackHole = CharBuffer.allocate(1024);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    60
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    61
        void verify(ByteBuffer in, boolean endOfInput)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    62
                throws CharacterCodingException {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    63
            while (true) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    64
                // Since decoder.flush() cannot produce an error, it's not
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    65
                // helpful for verification. Therefore this step is skipped.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    66
                CoderResult r = decoder.decode(in, blackHole, endOfInput);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    67
                if (r.isOverflow()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    68
                    blackHole.clear();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    69
                } else if (r.isUnderflow()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    70
                    break;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    71
                } else if (r.isError()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    72
                    r.throwException();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    73
                } else {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    74
                    // Should not happen
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    75
                    throw new InternalError();
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
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    79
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    80
        Verifier reset() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    81
            decoder.reset();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    82
            return this;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    83
        }
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
    static final class Encoder {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    87
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    88
        private final CharsetEncoder encoder = UTF_8.newEncoder();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    89
        private boolean coding = true;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    90
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    91
        CoderResult encode(CharBuffer in, ByteBuffer out, boolean endOfInput)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    92
                throws CharacterCodingException {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    93
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    94
            if (coding) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    95
                CoderResult r = encoder.encode(in, out, endOfInput);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    96
                if (r.isOverflow()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    97
                    return r;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    98
                } else if (r.isUnderflow()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    99
                    if (endOfInput) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   100
                        coding = false;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   101
                    } else {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   102
                        return r;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   103
                    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   104
                } else if (r.isError()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   105
                    r.throwException();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   106
                } else {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   107
                    // Should not happen
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   108
                    throw new InternalError();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   109
                }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   110
            }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   111
            assert !coding;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   112
            return encoder.flush(out);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   113
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   114
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   115
        Encoder reset() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   116
            coding = true;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   117
            encoder.reset();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   118
            return this;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   119
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   120
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   121
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   122
    static CharBuffer decode(ByteBuffer in) throws CharacterCodingException {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   123
        return UTF_8.newDecoder().decode(in);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   124
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   125
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   126
    static final class Decoder {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   127
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   128
        private final CharsetDecoder decoder = UTF_8.newDecoder();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   129
        private boolean coding = true; // Either coding or flushing
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   130
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   131
        CoderResult decode(ByteBuffer in, CharBuffer out, boolean endOfInput)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   132
                throws CharacterCodingException {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   133
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   134
            if (coding) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   135
                CoderResult r = decoder.decode(in, out, endOfInput);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   136
                if (r.isOverflow()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   137
                    return r;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   138
                } else if (r.isUnderflow()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   139
                    if (endOfInput) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   140
                        coding = false;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   141
                    } else {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   142
                        return r;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   143
                    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   144
                } else if (r.isError()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   145
                    r.throwException();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   146
                } else {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   147
                    // Should not happen
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   148
                    throw new InternalError();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   149
                }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   150
            }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   151
            assert !coding;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   152
            return decoder.flush(out);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   153
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   154
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   155
        Decoder reset() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   156
            coding = true;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   157
            decoder.reset();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   158
            return this;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   159
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   160
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   161
}