Skip to content

Commit

Permalink
HTTP/2 Headers Type Updates
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Scottmitch committed Oct 30, 2015
1 parent 8af712a commit 1c93e04
Show file tree
Hide file tree
Showing 44 changed files with 1,864 additions and 3,595 deletions.
11 changes: 4 additions & 7 deletions buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.Charset;

import static io.netty.util.internal.MathUtil.isOutOfBounds;

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

final void checkIndex0(int index, int fieldLength) {
if (isInvalid(index, fieldLength, capacity())) {
if (isOutOfBounds(index, fieldLength, capacity())) {
throw new IndexOutOfBoundsException(String.format(
"index: %d, length: %d (expected: range(0, %d))", index, fieldLength, capacity()));
}
}

protected final void checkSrcIndex(int index, int length, int srcIndex, int srcCapacity) {
checkIndex(index, length);
if (isInvalid(srcIndex, length, srcCapacity)) {
if (isOutOfBounds(srcIndex, length, srcCapacity)) {
throw new IndexOutOfBoundsException(String.format(
"srcIndex: %d, length: %d (expected: range(0, %d))", srcIndex, length, srcCapacity));
}
}

protected final void checkDstIndex(int index, int length, int dstIndex, int dstCapacity) {
checkIndex(index, length);
if (isInvalid(dstIndex, length, dstCapacity)) {
if (isOutOfBounds(dstIndex, length, dstCapacity)) {
throw new IndexOutOfBoundsException(String.format(
"dstIndex: %d, length: %d (expected: range(0, %d))", dstIndex, length, dstCapacity));
}
}

static boolean isInvalid(int index, int length, int capacity) {
return (index | length | (index + length) | (capacity - (index + length))) < 0;
}

/**
* Throws an {@link IndexOutOfBoundsException} if the current
* {@linkplain #readableBytes() readable bytes} of this buffer is less
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import java.nio.ByteOrder;

import static io.netty.util.internal.PlatformDependent.BIG_ENDIAN_NATIVE_ORDER;

/**
* Special {@link SwappedByteBuf} for {@link ByteBuf}s that is using unsafe.
*/
Expand All @@ -30,7 +32,7 @@ abstract class AbstractUnsafeSwappedByteBuf extends SwappedByteBuf {
super(buf);
assert PlatformDependent.isUnaligned();
wrapped = buf;
nativeByteOrder = UnsafeByteBufUtil.BIG_ENDIAN_NATIVE_ORDER == (order() == ByteOrder.BIG_ENDIAN);
nativeByteOrder = BIG_ENDIAN_NATIVE_ORDER == (order() == ByteOrder.BIG_ENDIAN);
}

@Override
Expand Down
28 changes: 12 additions & 16 deletions buffer/src/main/java/io/netty/buffer/ByteBufUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
*/
package io.netty.buffer;

import static io.netty.util.internal.ObjectUtil.checkNotNull;

import io.netty.util.AsciiString;
import io.netty.util.ByteProcessor;
import io.netty.util.ByteString;
import io.netty.util.CharsetUtil;
import io.netty.util.Recycler;
import io.netty.util.Recycler.Handle;
Expand All @@ -41,6 +38,9 @@
import java.util.Arrays;
import java.util.Locale;

import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.MathUtil.isOutOfBounds;

/**
* A collection of utility methods that is related with handling {@link ByteBuf},
* such as the generation of hex dump and swapping an integer's byte order.
Expand Down Expand Up @@ -661,7 +661,7 @@ public static ByteBuf threadLocalDirectBuffer() {
* The copy will start at {@link ByteBuf#readerIndex()} and copy {@link ByteBuf#readableBytes()} bytes.
*/
public static byte[] getBytes(ByteBuf buf) {
return getBytes(buf, checkNotNull(buf, "buf").readerIndex(), buf.readableBytes());
return getBytes(buf, buf.readerIndex(), buf.readableBytes());
}

/**
Expand All @@ -679,7 +679,7 @@ public static byte[] getBytes(ByteBuf buf, int start, int length) {
* If {@code copy} is false the underlying storage will be shared, if possible.
*/
public static byte[] getBytes(ByteBuf buf, int start, int length, boolean copy) {
if (start < 0 || length > checkNotNull(buf, "buf").capacity() - start) {
if (isOutOfBounds(start, length, buf.capacity())) {
throw new IndexOutOfBoundsException("expected: " + "0 <= start(" + start + ") <= start + length(" + length
+ ") <= " + "buf.capacity(" + buf.capacity() + ')');
}
Expand All @@ -706,12 +706,10 @@ public static byte[] getBytes(ByteBuf buf, int start, int length, boolean copy)
* @param dstIdx the starting offset in the destination byte array.
* @param length the number of characters to copy.
*/
public static void copy(ByteString src, int srcIdx, ByteBuf dst, int dstIdx, int length) {
final int thisLen = src.length();

if (srcIdx < 0 || length > thisLen - srcIdx) {
public static void copy(AsciiString src, int srcIdx, ByteBuf dst, int dstIdx, int length) {
if (isOutOfBounds(srcIdx, length, src.length())) {
throw new IndexOutOfBoundsException("expected: " + "0 <= srcIdx(" + srcIdx + ") <= srcIdx + length("
+ length + ") <= srcLen(" + thisLen + ')');
+ length + ") <= srcLen(" + src.length() + ')');
}

checkNotNull(dst, "dst").setBytes(dstIdx, src.array(), srcIdx + src.arrayOffset(), length);
Expand All @@ -724,12 +722,10 @@ public static void copy(ByteString src, int srcIdx, ByteBuf dst, int dstIdx, int
* @param dst the destination byte array.
* @param length the number of characters to copy.
*/
public static void copy(ByteString src, int srcIdx, ByteBuf dst, int length) {
final int thisLen = src.length();

if (srcIdx < 0 || length > thisLen - srcIdx) {
public static void copy(AsciiString src, int srcIdx, ByteBuf dst, int length) {
if (isOutOfBounds(srcIdx, length, src.length())) {
throw new IndexOutOfBoundsException("expected: " + "0 <= srcIdx(" + srcIdx + ") <= srcIdx + length("
+ length + ") <= srcLen(" + thisLen + ')');
+ length + ") <= srcLen(" + src.length() + ')');
}

checkNotNull(dst, "dst").writeBytes(src.array(), srcIdx + src.arrayOffset(), length);
Expand Down Expand Up @@ -771,7 +767,7 @@ public static void appendPrettyHexDump(StringBuilder dump, ByteBuf buf) {
* the given {@code length}.
*/
public static void appendPrettyHexDump(StringBuilder dump, ByteBuf buf, int offset, int length) {
if (offset < 0 || length > checkNotNull(buf, "buf").capacity() - offset) {
if (isOutOfBounds(offset, length, buf.capacity())) {
throw new IndexOutOfBoundsException(
"expected: " + "0 <= offset(" + offset + ") <= offset + length(" + length
+ ") <= " + "buf.capacity(" + buf.capacity() + ')');
Expand Down
10 changes: 5 additions & 5 deletions buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import static io.netty.util.internal.MathUtil.isOutOfBounds;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.PlatformDependent.BIG_ENDIAN_NATIVE_ORDER;

/**
* All operations get and set as {@link ByteOrder#BIG_ENDIAN}.
*/
final class UnsafeByteBufUtil {

static final boolean BIG_ENDIAN_NATIVE_ORDER = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
private static final boolean UNALIGNED = PlatformDependent.isUnaligned();

static byte getByte(long address) {
Expand Down Expand Up @@ -282,7 +282,7 @@ static int setBytes(AbstractByteBuf buf, long addr, int index, InputStream in, i
static void getBytes(AbstractByteBuf buf, long addr, int index, ByteBuf dst, int dstIndex, int length) {
buf.checkIndex(index, length);
checkNotNull(dst, "dst");
if (AbstractByteBuf.isInvalid(dstIndex, length, dst.capacity())) {
if (isOutOfBounds(dstIndex, length, dst.capacity())) {
throw new IndexOutOfBoundsException("dstIndex: " + dstIndex);
}

Expand All @@ -298,7 +298,7 @@ static void getBytes(AbstractByteBuf buf, long addr, int index, ByteBuf dst, int
static void getBytes(AbstractByteBuf buf, long addr, int index, byte[] dst, int dstIndex, int length) {
buf.checkIndex(index, length);
checkNotNull(dst, "dst");
if (AbstractByteBuf.isInvalid(dstIndex, length, dst.length)) {
if (isOutOfBounds(dstIndex, length, dst.length)) {
throw new IndexOutOfBoundsException("dstIndex: " + dstIndex);
}
if (length != 0) {
Expand Down Expand Up @@ -328,7 +328,7 @@ static void getBytes(AbstractByteBuf buf, long addr, int index, ByteBuffer dst)
static void setBytes(AbstractByteBuf buf, long addr, int index, ByteBuf src, int srcIndex, int length) {
buf.checkIndex(index, length);
checkNotNull(src, "src");
if (AbstractByteBuf.isInvalid(srcIndex, length, src.capacity())) {
if (isOutOfBounds(srcIndex, length, src.capacity())) {
throw new IndexOutOfBoundsException("srcIndex: " + srcIndex);
}

Expand Down
Loading

0 comments on commit 1c93e04

Please sign in to comment.