Skip to content

Commit 1c93e04

Browse files
committed
HTTP/2 Headers Type Updates
Motivation: The HTTP/2 RFC (https://tools.ietf.org/html/rfc7540#section-8.1.2) indicates that header names consist of ASCII characters. We currently use ByteString to represent HTTP/2 header names. The HTTP/2 RFC (https://tools.ietf.org/html/rfc7540#section-10.3) also eludes to header values inheriting the same validity characteristics as HTTP/1.x. Using AsciiString for the value type of HTTP/2 headers would allow for re-use of predefined HTTP/1.x values, and make comparisons more intuitive. The Headers<T> interface could also be expanded to allow for easier use of header types which do not have the same Key and Value type. Motivation: - Change Headers<T> to Headers<K, V> - Change Http2Headers<ByteString> to Http2Headers<CharSequence, CharSequence> - Remove ByteString. Having AsciiString extend ByteString complicates equality comparisons when the hash code algorithm is no longer shared. Result: Http2Header types are more representative of the HTTP/2 RFC, and relationship between HTTP/2 header name/values more directly relates to HTTP/1.x header names/values.
1 parent 8af712a commit 1c93e04

File tree

44 files changed

+1864
-3595
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1864
-3595
lines changed

buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.nio.channels.ScatteringByteChannel;
3434
import java.nio.charset.Charset;
3535

36+
import static io.netty.util.internal.MathUtil.isOutOfBounds;
3637

3738
/**
3839
* A skeletal implementation of a buffer.
@@ -1110,32 +1111,28 @@ protected final void checkIndex(int index, int fieldLength) {
11101111
}
11111112

11121113
final void checkIndex0(int index, int fieldLength) {
1113-
if (isInvalid(index, fieldLength, capacity())) {
1114+
if (isOutOfBounds(index, fieldLength, capacity())) {
11141115
throw new IndexOutOfBoundsException(String.format(
11151116
"index: %d, length: %d (expected: range(0, %d))", index, fieldLength, capacity()));
11161117
}
11171118
}
11181119

11191120
protected final void checkSrcIndex(int index, int length, int srcIndex, int srcCapacity) {
11201121
checkIndex(index, length);
1121-
if (isInvalid(srcIndex, length, srcCapacity)) {
1122+
if (isOutOfBounds(srcIndex, length, srcCapacity)) {
11221123
throw new IndexOutOfBoundsException(String.format(
11231124
"srcIndex: %d, length: %d (expected: range(0, %d))", srcIndex, length, srcCapacity));
11241125
}
11251126
}
11261127

11271128
protected final void checkDstIndex(int index, int length, int dstIndex, int dstCapacity) {
11281129
checkIndex(index, length);
1129-
if (isInvalid(dstIndex, length, dstCapacity)) {
1130+
if (isOutOfBounds(dstIndex, length, dstCapacity)) {
11301131
throw new IndexOutOfBoundsException(String.format(
11311132
"dstIndex: %d, length: %d (expected: range(0, %d))", dstIndex, length, dstCapacity));
11321133
}
11331134
}
11341135

1135-
static boolean isInvalid(int index, int length, int capacity) {
1136-
return (index | length | (index + length) | (capacity - (index + length))) < 0;
1137-
}
1138-
11391136
/**
11401137
* Throws an {@link IndexOutOfBoundsException} if the current
11411138
* {@linkplain #readableBytes() readable bytes} of this buffer is less

buffer/src/main/java/io/netty/buffer/AbstractUnsafeSwappedByteBuf.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import java.nio.ByteOrder;
2121

22+
import static io.netty.util.internal.PlatformDependent.BIG_ENDIAN_NATIVE_ORDER;
23+
2224
/**
2325
* Special {@link SwappedByteBuf} for {@link ByteBuf}s that is using unsafe.
2426
*/
@@ -30,7 +32,7 @@ abstract class AbstractUnsafeSwappedByteBuf extends SwappedByteBuf {
3032
super(buf);
3133
assert PlatformDependent.isUnaligned();
3234
wrapped = buf;
33-
nativeByteOrder = UnsafeByteBufUtil.BIG_ENDIAN_NATIVE_ORDER == (order() == ByteOrder.BIG_ENDIAN);
35+
nativeByteOrder = BIG_ENDIAN_NATIVE_ORDER == (order() == ByteOrder.BIG_ENDIAN);
3436
}
3537

3638
@Override

buffer/src/main/java/io/netty/buffer/ByteBufUtil.java

+12-16
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@
1515
*/
1616
package io.netty.buffer;
1717

18-
import static io.netty.util.internal.ObjectUtil.checkNotNull;
19-
2018
import io.netty.util.AsciiString;
2119
import io.netty.util.ByteProcessor;
22-
import io.netty.util.ByteString;
2320
import io.netty.util.CharsetUtil;
2421
import io.netty.util.Recycler;
2522
import io.netty.util.Recycler.Handle;
@@ -41,6 +38,9 @@
4138
import java.util.Arrays;
4239
import java.util.Locale;
4340

41+
import static io.netty.util.internal.ObjectUtil.checkNotNull;
42+
import static io.netty.util.internal.MathUtil.isOutOfBounds;
43+
4444
/**
4545
* A collection of utility methods that is related with handling {@link ByteBuf},
4646
* such as the generation of hex dump and swapping an integer's byte order.
@@ -661,7 +661,7 @@ public static ByteBuf threadLocalDirectBuffer() {
661661
* The copy will start at {@link ByteBuf#readerIndex()} and copy {@link ByteBuf#readableBytes()} bytes.
662662
*/
663663
public static byte[] getBytes(ByteBuf buf) {
664-
return getBytes(buf, checkNotNull(buf, "buf").readerIndex(), buf.readableBytes());
664+
return getBytes(buf, buf.readerIndex(), buf.readableBytes());
665665
}
666666

667667
/**
@@ -679,7 +679,7 @@ public static byte[] getBytes(ByteBuf buf, int start, int length) {
679679
* If {@code copy} is false the underlying storage will be shared, if possible.
680680
*/
681681
public static byte[] getBytes(ByteBuf buf, int start, int length, boolean copy) {
682-
if (start < 0 || length > checkNotNull(buf, "buf").capacity() - start) {
682+
if (isOutOfBounds(start, length, buf.capacity())) {
683683
throw new IndexOutOfBoundsException("expected: " + "0 <= start(" + start + ") <= start + length(" + length
684684
+ ") <= " + "buf.capacity(" + buf.capacity() + ')');
685685
}
@@ -706,12 +706,10 @@ public static byte[] getBytes(ByteBuf buf, int start, int length, boolean copy)
706706
* @param dstIdx the starting offset in the destination byte array.
707707
* @param length the number of characters to copy.
708708
*/
709-
public static void copy(ByteString src, int srcIdx, ByteBuf dst, int dstIdx, int length) {
710-
final int thisLen = src.length();
711-
712-
if (srcIdx < 0 || length > thisLen - srcIdx) {
709+
public static void copy(AsciiString src, int srcIdx, ByteBuf dst, int dstIdx, int length) {
710+
if (isOutOfBounds(srcIdx, length, src.length())) {
713711
throw new IndexOutOfBoundsException("expected: " + "0 <= srcIdx(" + srcIdx + ") <= srcIdx + length("
714-
+ length + ") <= srcLen(" + thisLen + ')');
712+
+ length + ") <= srcLen(" + src.length() + ')');
715713
}
716714

717715
checkNotNull(dst, "dst").setBytes(dstIdx, src.array(), srcIdx + src.arrayOffset(), length);
@@ -724,12 +722,10 @@ public static void copy(ByteString src, int srcIdx, ByteBuf dst, int dstIdx, int
724722
* @param dst the destination byte array.
725723
* @param length the number of characters to copy.
726724
*/
727-
public static void copy(ByteString src, int srcIdx, ByteBuf dst, int length) {
728-
final int thisLen = src.length();
729-
730-
if (srcIdx < 0 || length > thisLen - srcIdx) {
725+
public static void copy(AsciiString src, int srcIdx, ByteBuf dst, int length) {
726+
if (isOutOfBounds(srcIdx, length, src.length())) {
731727
throw new IndexOutOfBoundsException("expected: " + "0 <= srcIdx(" + srcIdx + ") <= srcIdx + length("
732-
+ length + ") <= srcLen(" + thisLen + ')');
728+
+ length + ") <= srcLen(" + src.length() + ')');
733729
}
734730

735731
checkNotNull(dst, "dst").writeBytes(src.array(), srcIdx + src.arrayOffset(), length);
@@ -771,7 +767,7 @@ public static void appendPrettyHexDump(StringBuilder dump, ByteBuf buf) {
771767
* the given {@code length}.
772768
*/
773769
public static void appendPrettyHexDump(StringBuilder dump, ByteBuf buf, int offset, int length) {
774-
if (offset < 0 || length > checkNotNull(buf, "buf").capacity() - offset) {
770+
if (isOutOfBounds(offset, length, buf.capacity())) {
775771
throw new IndexOutOfBoundsException(
776772
"expected: " + "0 <= offset(" + offset + ") <= offset + length(" + length
777773
+ ") <= " + "buf.capacity(" + buf.capacity() + ')');

buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
import java.nio.ByteBuffer;
2424
import java.nio.ByteOrder;
2525

26+
import static io.netty.util.internal.MathUtil.isOutOfBounds;
2627
import static io.netty.util.internal.ObjectUtil.checkNotNull;
28+
import static io.netty.util.internal.PlatformDependent.BIG_ENDIAN_NATIVE_ORDER;
2729

2830
/**
2931
* All operations get and set as {@link ByteOrder#BIG_ENDIAN}.
3032
*/
3133
final class UnsafeByteBufUtil {
32-
33-
static final boolean BIG_ENDIAN_NATIVE_ORDER = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
3434
private static final boolean UNALIGNED = PlatformDependent.isUnaligned();
3535

3636
static byte getByte(long address) {
@@ -282,7 +282,7 @@ static int setBytes(AbstractByteBuf buf, long addr, int index, InputStream in, i
282282
static void getBytes(AbstractByteBuf buf, long addr, int index, ByteBuf dst, int dstIndex, int length) {
283283
buf.checkIndex(index, length);
284284
checkNotNull(dst, "dst");
285-
if (AbstractByteBuf.isInvalid(dstIndex, length, dst.capacity())) {
285+
if (isOutOfBounds(dstIndex, length, dst.capacity())) {
286286
throw new IndexOutOfBoundsException("dstIndex: " + dstIndex);
287287
}
288288

@@ -298,7 +298,7 @@ static void getBytes(AbstractByteBuf buf, long addr, int index, ByteBuf dst, int
298298
static void getBytes(AbstractByteBuf buf, long addr, int index, byte[] dst, int dstIndex, int length) {
299299
buf.checkIndex(index, length);
300300
checkNotNull(dst, "dst");
301-
if (AbstractByteBuf.isInvalid(dstIndex, length, dst.length)) {
301+
if (isOutOfBounds(dstIndex, length, dst.length)) {
302302
throw new IndexOutOfBoundsException("dstIndex: " + dstIndex);
303303
}
304304
if (length != 0) {
@@ -328,7 +328,7 @@ static void getBytes(AbstractByteBuf buf, long addr, int index, ByteBuffer dst)
328328
static void setBytes(AbstractByteBuf buf, long addr, int index, ByteBuf src, int srcIndex, int length) {
329329
buf.checkIndex(index, length);
330330
checkNotNull(src, "src");
331-
if (AbstractByteBuf.isInvalid(srcIndex, length, src.capacity())) {
331+
if (isOutOfBounds(srcIndex, length, src.capacity())) {
332332
throw new IndexOutOfBoundsException("srcIndex: " + srcIndex);
333333
}
334334

0 commit comments

Comments
 (0)