1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.coyote.http2;
import java.nio.ByteBuffer;
import org.apache.tomcat.util.res.StringManager;
final class Hpack {
private static final StringManager sm = StringManager.getManager(Hpack.class);
private static final byte LOWER_DIFF = 'a' - 'A';
static final int DEFAULT_TABLE_SIZE = 4096;
/*
* The HPack specification says there SHOULD be an upper bound on this.
*
* Tomcat has opted to limit values to INTEGER.MAX_VALUE. Give that there is the prefix byte and then each octet
* provides up to 7-bits, a total a 5 octets plus the prefix may be required.
*
* Note: The maximum value represented by 5 octets is greater than INTEGER.MAX_VALUE.
*
*/
private static final int MAX_INTEGER_OCTETS = 5;
/**
* table that contains powers of two, used as both bitmask and to quickly calculate 2^n
*/
private static final int[] PREFIX_TABLE;
static final HeaderField[] STATIC_TABLE;
static final int STATIC_TABLE_LENGTH;
static {
PREFIX_TABLE = new int[32];
for (int i = 0; i < 32; ++i) {
int n = 0;
for (int j = 0; j < i; ++j) {
n = n << 1;
n |= 1;
}
PREFIX_TABLE[i] = n;
}
HeaderField[] fields = new HeaderField[62];
// note that zero is not used
fields[1] = new HeaderField(":authority", null);
fields[2] = new HeaderField(":method", "GET");
fields[3] = new HeaderField(":method", "POST");
fields[4] = new HeaderField(":path", "/");
fields[5] = new HeaderField(":path", "/index.html");
fields[6] = new HeaderField(":scheme", "http");
fields[7] = new HeaderField(":scheme", "https");
fields[8] = new HeaderField(":status", "200");
fields[9] = new HeaderField(":status", "204");
fields[10] = new HeaderField(":status", "206");
fields[11] = new HeaderField(":status", "304");
fields[12] = new HeaderField(":status", "400");
fields[13] = new HeaderField(":status", "404");
fields[14] = new HeaderField(":status", "500");
fields[15] = new HeaderField("accept-charset", null);
fields[16] = new HeaderField("accept-encoding", "gzip, deflate");
fields[17] = new HeaderField("accept-language", null);
fields[18] = new HeaderField("accept-ranges", null);
fields[19] = new HeaderField("accept", null);
fields[20] = new HeaderField("access-control-allow-origin", null);
fields[21] = new HeaderField("age", null);
fields[22] = new HeaderField("allow", null);
fields[23] = new HeaderField("authorization", null);
fields[24] = new HeaderField("cache-control", null);
fields[25] = new HeaderField("content-disposition", null);
fields[26] = new HeaderField("content-encoding", null);
fields[27] = new HeaderField("content-language", null);
fields[28] = new HeaderField("content-length", null);
fields[29] = new HeaderField("content-location", null);
fields[30] = new HeaderField("content-range", null);
fields[31] = new HeaderField("content-type", null);
fields[32] = new HeaderField("cookie", null);
fields[33] = new HeaderField("date", null);
fields[34] = new HeaderField("etag", null);
fields[35] = new HeaderField("expect", null);
fields[36] = new HeaderField("expires", null);
fields[37] = new HeaderField("from", null);
fields[38] = new HeaderField("host", null);
fields[39] = new HeaderField("if-match", null);
fields[40] = new HeaderField("if-modified-since", null);
fields[41] = new HeaderField("if-none-match", null);
fields[42] = new HeaderField("if-range", null);
fields[43] = new HeaderField("if-unmodified-since", null);
fields[44] = new HeaderField("last-modified", null);
fields[45] = new HeaderField("link", null);
fields[46] = new HeaderField("location", null);
fields[47] = new HeaderField("max-forwards", null);
fields[48] = new HeaderField("proxy-authenticate", null);
fields[49] = new HeaderField("proxy-authorization", null);
fields[50] = new HeaderField("range", null);
fields[51] = new HeaderField("referer", null);
fields[52] = new HeaderField("refresh", null);
fields[53] = new HeaderField("retry-after", null);
fields[54] = new HeaderField("server", null);
fields[55] = new HeaderField("set-cookie", null);
fields[56] = new HeaderField("strict-transport-security", null);
fields[57] = new HeaderField("transfer-encoding", null);
fields[58] = new HeaderField("user-agent", null);
fields[59] = new HeaderField("vary", null);
fields[60] = new HeaderField("via", null);
fields[61] = new HeaderField("www-authenticate", null);
STATIC_TABLE = fields;
STATIC_TABLE_LENGTH = STATIC_TABLE.length - 1;
}
static class HeaderField {
final String name;
final String value;
final int size;
HeaderField(String name, String value) {
this.name = name;
this.value = value;
if (value != null) {
this.size = 32 + name.length() + value.length();
} else {
this.size = -1;
}
}
}
/**
* Decodes an integer in the HPACK prefix format. If the return value is -1 it means that there was not enough data
* in the buffer to complete the decoding sequence.
* <p/>
* If this method returns -1 then the source buffer will not have been modified.
*
* @param source The buffer that contains the integer
* @param n The encoding prefix length
*
* @return The encoded integer, or -1 if there was not enough data
*/
static int decodeInteger(ByteBuffer source, int n) throws HpackException {
if (source.remaining() == 0) {
return -1;
}
int count = 1;
int sp = source.position();
int mask = PREFIX_TABLE[n];
// Use long internally as the value may exceed Integer.MAX_VALUE
long result = mask & source.get();
int b;
if (result < PREFIX_TABLE[n]) {
// Casting is safe as result must be less than 255 at this point.
return (int) result;
} else {
int m = 0;
do {
if (count++ > MAX_INTEGER_OCTETS) {
throw new HpackException(
sm.getString("hpack.integerEncodedOverTooManyOctets", Integer.valueOf(MAX_INTEGER_OCTETS)));
}
if (source.remaining() == 0) {
// we have run out of data
// reset
source.position(sp);
return -1;
}
b = source.get();
result = result + (b & 127) * (PREFIX_TABLE[m] + 1L);
if (result > Integer.MAX_VALUE) {
throw new HpackException(sm.getString("hpack.integerEncodedTooBig"));
}
m += 7;
} while ((b & 128) == 128);
}
// Casting is safe as result must be less than Integer.MAX_VALUE at this point
return (int) result;
}
/**
* Encodes an integer in the HPACK prefix format.
* <p/>
* This method assumes that the buffer has already had the first 8-n bits filled. As such it will modify the last
* byte that is already present in the buffer, and potentially add more if required
*
* @param source The buffer that contains the integer
* @param value The integer to encode
* @param n The encoding prefix length
*/
static void encodeInteger(ByteBuffer source, int value, int n) {
int twoNminus1 = PREFIX_TABLE[n];
int pos = source.position() - 1;
if (value < twoNminus1) {
source.put(pos, (byte) (source.get(pos) | value));
} else {
source.put(pos, (byte) (source.get(pos) | twoNminus1));
value = value - twoNminus1;
while (value >= 128) {
source.put((byte) (value % 128 + 128));
value = value / 128;
}
source.put((byte) value);
}
}
static char toLower(char c) {
if (c >= 'A' && c <= 'Z') {
return (char) (c + LOWER_DIFF);
}
return c;
}
private Hpack() {
}
}
|