2015-01-20 01:48:11 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2014 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project 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 io.netty.handler.codec.http2;
|
|
|
|
|
2016-04-14 10:31:48 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
import io.netty.channel.ChannelPromise;
|
|
|
|
import io.netty.handler.codec.http2.Http2CodecUtil.SimpleChannelPromiseAggregator;
|
|
|
|
import io.netty.handler.codec.http2.Http2FrameWriter.Configuration;
|
2016-09-16 15:57:33 +02:00
|
|
|
import io.netty.handler.codec.http2.Http2HeadersEncoder.SensitivityDetector;
|
2017-01-14 02:09:44 +01:00
|
|
|
import io.netty.util.internal.PlatformDependent;
|
2016-04-14 10:31:48 +02:00
|
|
|
import io.netty.util.internal.UnstableApi;
|
|
|
|
|
2015-02-04 14:00:43 +01:00
|
|
|
import static io.netty.buffer.Unpooled.directBuffer;
|
|
|
|
import static io.netty.buffer.Unpooled.unreleasableBuffer;
|
2015-01-31 04:54:35 +01:00
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.CONTINUATION_FRAME_HEADER_LENGTH;
|
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.DATA_FRAME_HEADER_LENGTH;
|
2015-01-20 01:48:11 +01:00
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_MAX_FRAME_SIZE;
|
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.FRAME_HEADER_LENGTH;
|
2015-01-31 04:54:35 +01:00
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.GO_AWAY_FRAME_HEADER_LENGTH;
|
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.HEADERS_FRAME_HEADER_LENGTH;
|
2015-01-20 01:48:11 +01:00
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.INT_FIELD_LENGTH;
|
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.MAX_UNSIGNED_BYTE;
|
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.MAX_UNSIGNED_INT;
|
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.MAX_WEIGHT;
|
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.MIN_WEIGHT;
|
2015-09-09 22:40:07 +02:00
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.PING_FRAME_PAYLOAD_LENGTH;
|
2015-01-20 01:48:11 +01:00
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.PRIORITY_ENTRY_LENGTH;
|
2015-01-31 04:54:35 +01:00
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.PRIORITY_FRAME_LENGTH;
|
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.PUSH_PROMISE_FRAME_HEADER_LENGTH;
|
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.RST_STREAM_FRAME_LENGTH;
|
2015-01-20 01:48:11 +01:00
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.SETTING_ENTRY_LENGTH;
|
2015-01-31 04:54:35 +01:00
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.WINDOW_UPDATE_FRAME_LENGTH;
|
2015-01-20 01:48:11 +01:00
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.isMaxFrameSizeValid;
|
2016-09-16 15:57:33 +02:00
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.verifyPadding;
|
2015-01-31 04:54:35 +01:00
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.writeFrameHeaderInternal;
|
2015-01-20 01:48:11 +01:00
|
|
|
import static io.netty.handler.codec.http2.Http2Error.FRAME_SIZE_ERROR;
|
2015-01-31 04:54:35 +01:00
|
|
|
import static io.netty.handler.codec.http2.Http2Exception.connectionError;
|
2015-01-20 01:48:11 +01:00
|
|
|
import static io.netty.handler.codec.http2.Http2FrameTypes.CONTINUATION;
|
|
|
|
import static io.netty.handler.codec.http2.Http2FrameTypes.DATA;
|
|
|
|
import static io.netty.handler.codec.http2.Http2FrameTypes.GO_AWAY;
|
|
|
|
import static io.netty.handler.codec.http2.Http2FrameTypes.HEADERS;
|
|
|
|
import static io.netty.handler.codec.http2.Http2FrameTypes.PING;
|
|
|
|
import static io.netty.handler.codec.http2.Http2FrameTypes.PRIORITY;
|
|
|
|
import static io.netty.handler.codec.http2.Http2FrameTypes.PUSH_PROMISE;
|
|
|
|
import static io.netty.handler.codec.http2.Http2FrameTypes.RST_STREAM;
|
|
|
|
import static io.netty.handler.codec.http2.Http2FrameTypes.SETTINGS;
|
|
|
|
import static io.netty.handler.codec.http2.Http2FrameTypes.WINDOW_UPDATE;
|
|
|
|
import static io.netty.util.internal.ObjectUtil.checkNotNull;
|
2015-11-19 05:46:11 +01:00
|
|
|
import static java.lang.Math.max;
|
|
|
|
import static java.lang.Math.min;
|
|
|
|
|
2015-01-20 01:48:11 +01:00
|
|
|
/**
|
|
|
|
* A {@link Http2FrameWriter} that supports all frame types defined by the HTTP/2 specification.
|
|
|
|
*/
|
2016-04-12 14:22:41 +02:00
|
|
|
@UnstableApi
|
2015-01-20 01:48:11 +01:00
|
|
|
public class DefaultHttp2FrameWriter implements Http2FrameWriter, Http2FrameSizePolicy, Configuration {
|
|
|
|
private static final String STREAM_ID = "Stream ID";
|
|
|
|
private static final String STREAM_DEPENDENCY = "Stream Dependency";
|
2015-01-31 04:54:35 +01:00
|
|
|
/**
|
2016-06-23 14:09:23 +02:00
|
|
|
* This buffer is allocated to the maximum size of the padding field, and filled with zeros.
|
2015-01-31 04:54:35 +01:00
|
|
|
* When padding is needed it can be taken as a slice of this buffer. Users should call {@link ByteBuf#retain()}
|
|
|
|
* before using their slice.
|
|
|
|
*/
|
2016-04-13 15:25:15 +02:00
|
|
|
private static final ByteBuf ZERO_BUFFER =
|
|
|
|
unreleasableBuffer(directBuffer(MAX_UNSIGNED_BYTE).writeZero(MAX_UNSIGNED_BYTE)).asReadOnly();
|
2015-01-20 01:48:11 +01:00
|
|
|
|
|
|
|
private final Http2HeadersEncoder headersEncoder;
|
|
|
|
private int maxFrameSize;
|
|
|
|
|
|
|
|
public DefaultHttp2FrameWriter() {
|
|
|
|
this(new DefaultHttp2HeadersEncoder());
|
|
|
|
}
|
|
|
|
|
2017-04-19 22:37:03 +02:00
|
|
|
public DefaultHttp2FrameWriter(SensitivityDetector headersSensitivityDetector) {
|
|
|
|
this(new DefaultHttp2HeadersEncoder(headersSensitivityDetector));
|
2016-09-16 15:57:33 +02:00
|
|
|
}
|
|
|
|
|
2017-04-19 22:37:03 +02:00
|
|
|
public DefaultHttp2FrameWriter(SensitivityDetector headersSensitivityDetector, boolean ignoreMaxHeaderListSize) {
|
|
|
|
this(new DefaultHttp2HeadersEncoder(headersSensitivityDetector, ignoreMaxHeaderListSize));
|
2015-11-24 04:14:21 +01:00
|
|
|
}
|
|
|
|
|
2015-01-20 01:48:11 +01:00
|
|
|
public DefaultHttp2FrameWriter(Http2HeadersEncoder headersEncoder) {
|
|
|
|
this.headersEncoder = headersEncoder;
|
|
|
|
maxFrameSize = DEFAULT_MAX_FRAME_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Configuration configuration() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-01-14 02:09:44 +01:00
|
|
|
public Http2HeadersEncoder.Configuration headersConfiguration() {
|
|
|
|
return headersEncoder.configuration();
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Http2FrameSizePolicy frameSizePolicy() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void maxFrameSize(int max) throws Http2Exception {
|
|
|
|
if (!isMaxFrameSizeValid(max)) {
|
|
|
|
throw connectionError(FRAME_SIZE_ERROR, "Invalid MAX_FRAME_SIZE specified in sent settings: %d", max);
|
|
|
|
}
|
|
|
|
maxFrameSize = max;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int maxFrameSize() {
|
|
|
|
return maxFrameSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() { }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture writeData(ChannelHandlerContext ctx, int streamId, ByteBuf data,
|
|
|
|
int padding, boolean endStream, ChannelPromise promise) {
|
2015-11-19 05:46:11 +01:00
|
|
|
final SimpleChannelPromiseAggregator promiseAggregator =
|
2015-01-31 04:54:35 +01:00
|
|
|
new SimpleChannelPromiseAggregator(promise, ctx.channel(), ctx.executor());
|
2017-11-18 08:22:29 +01:00
|
|
|
ByteBuf frameHeader = null;
|
2015-01-20 01:48:11 +01:00
|
|
|
try {
|
|
|
|
verifyStreamId(streamId, STREAM_ID);
|
|
|
|
verifyPadding(padding);
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
int remainingData = data.readableBytes();
|
2017-11-18 08:22:29 +01:00
|
|
|
Http2Flags flags = new Http2Flags();
|
|
|
|
flags.endOfStream(false);
|
|
|
|
flags.paddingPresent(false);
|
|
|
|
// Fast path to write frames of payload size maxFrameSize first.
|
|
|
|
if (remainingData > maxFrameSize) {
|
|
|
|
frameHeader = ctx.alloc().buffer(FRAME_HEADER_LENGTH);
|
|
|
|
writeFrameHeaderInternal(frameHeader, maxFrameSize, DATA, flags, streamId);
|
|
|
|
do {
|
|
|
|
// Write the header.
|
|
|
|
ctx.write(frameHeader.retainedSlice(), promiseAggregator.newPromise());
|
|
|
|
|
|
|
|
// Write the payload.
|
|
|
|
ctx.write(data.readRetainedSlice(maxFrameSize), promiseAggregator.newPromise());
|
|
|
|
|
|
|
|
remainingData -= maxFrameSize;
|
|
|
|
// Stop iterating if remainingData == maxFrameSize so we can take care of reference counts below.
|
|
|
|
} while (remainingData > maxFrameSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (padding == 0) {
|
|
|
|
// Write the header.
|
|
|
|
if (frameHeader != null) {
|
|
|
|
frameHeader.release();
|
|
|
|
frameHeader = null;
|
2015-11-19 05:46:11 +01:00
|
|
|
}
|
2017-11-18 08:22:29 +01:00
|
|
|
ByteBuf frameHeader2 = ctx.alloc().buffer(FRAME_HEADER_LENGTH);
|
|
|
|
flags.endOfStream(endStream);
|
|
|
|
writeFrameHeaderInternal(frameHeader2, remainingData, DATA, flags, streamId);
|
|
|
|
ctx.write(frameHeader2, promiseAggregator.newPromise());
|
|
|
|
|
|
|
|
// Write the payload.
|
|
|
|
ByteBuf lastFrame = data.readSlice(remainingData);
|
|
|
|
data = null;
|
|
|
|
ctx.write(lastFrame, promiseAggregator.newPromise());
|
|
|
|
} else {
|
|
|
|
if (remainingData != maxFrameSize) {
|
|
|
|
if (frameHeader != null) {
|
|
|
|
frameHeader.release();
|
|
|
|
frameHeader = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
remainingData -= maxFrameSize;
|
|
|
|
// Write the header.
|
|
|
|
ByteBuf lastFrame;
|
|
|
|
if (frameHeader == null) {
|
|
|
|
lastFrame = ctx.alloc().buffer(FRAME_HEADER_LENGTH);
|
|
|
|
writeFrameHeaderInternal(lastFrame, maxFrameSize, DATA, flags, streamId);
|
|
|
|
} else {
|
|
|
|
lastFrame = frameHeader.slice();
|
|
|
|
frameHeader = null;
|
|
|
|
}
|
|
|
|
ctx.write(lastFrame, promiseAggregator.newPromise());
|
|
|
|
|
|
|
|
// Write the payload.
|
2018-11-29 19:45:52 +01:00
|
|
|
lastFrame = data.readableBytes() != maxFrameSize ? data.readSlice(maxFrameSize) : data;
|
2017-11-18 08:22:29 +01:00
|
|
|
data = null;
|
|
|
|
ctx.write(lastFrame, promiseAggregator.newPromise());
|
2017-11-04 01:09:42 +01:00
|
|
|
}
|
2017-11-18 08:22:29 +01:00
|
|
|
|
|
|
|
do {
|
|
|
|
int frameDataBytes = min(remainingData, maxFrameSize);
|
|
|
|
int framePaddingBytes = min(padding, max(0, (maxFrameSize - 1) - frameDataBytes));
|
|
|
|
|
|
|
|
// Decrement the remaining counters.
|
|
|
|
padding -= framePaddingBytes;
|
|
|
|
remainingData -= frameDataBytes;
|
|
|
|
|
|
|
|
// Write the header.
|
|
|
|
ByteBuf frameHeader2 = ctx.alloc().buffer(DATA_FRAME_HEADER_LENGTH);
|
|
|
|
flags.endOfStream(endStream && remainingData == 0 && padding == 0);
|
|
|
|
flags.paddingPresent(framePaddingBytes > 0);
|
|
|
|
writeFrameHeaderInternal(frameHeader2, framePaddingBytes + frameDataBytes, DATA, flags, streamId);
|
|
|
|
writePaddingLength(frameHeader2, framePaddingBytes);
|
|
|
|
ctx.write(frameHeader2, promiseAggregator.newPromise());
|
|
|
|
|
|
|
|
// Write the payload.
|
|
|
|
if (frameDataBytes != 0) {
|
|
|
|
if (remainingData == 0) {
|
|
|
|
ByteBuf lastFrame = data.readSlice(frameDataBytes);
|
|
|
|
data = null;
|
|
|
|
ctx.write(lastFrame, promiseAggregator.newPromise());
|
|
|
|
} else {
|
|
|
|
ctx.write(data.readRetainedSlice(frameDataBytes), promiseAggregator.newPromise());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Write the frame padding.
|
|
|
|
if (paddingBytes(framePaddingBytes) > 0) {
|
|
|
|
ctx.write(ZERO_BUFFER.slice(0, paddingBytes(framePaddingBytes)),
|
|
|
|
promiseAggregator.newPromise());
|
|
|
|
}
|
|
|
|
} while (remainingData != 0 || padding != 0);
|
|
|
|
}
|
|
|
|
} catch (Throwable cause) {
|
|
|
|
if (frameHeader != null) {
|
|
|
|
frameHeader.release();
|
|
|
|
}
|
|
|
|
// Use a try/finally here in case the data has been released before calling this method. This is not
|
|
|
|
// necessary above because we internally allocate frameHeader.
|
|
|
|
try {
|
|
|
|
if (data != null) {
|
2017-11-04 01:09:42 +01:00
|
|
|
data.release();
|
|
|
|
}
|
|
|
|
} finally {
|
2017-11-18 08:22:29 +01:00
|
|
|
promiseAggregator.setFailure(cause);
|
2017-11-04 01:09:42 +01:00
|
|
|
promiseAggregator.doneAllocatingPromises();
|
2015-01-31 04:54:35 +01:00
|
|
|
}
|
2017-11-04 01:09:42 +01:00
|
|
|
return promiseAggregator;
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
2016-03-07 19:05:22 +01:00
|
|
|
return promiseAggregator.doneAllocatingPromises();
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture writeHeaders(ChannelHandlerContext ctx, int streamId,
|
2017-01-26 18:03:42 +01:00
|
|
|
Http2Headers headers, int padding, boolean endStream, ChannelPromise promise) {
|
2015-01-31 04:54:35 +01:00
|
|
|
return writeHeadersInternal(ctx, streamId, headers, padding, endStream,
|
|
|
|
false, 0, (short) 0, false, promise);
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture writeHeaders(ChannelHandlerContext ctx, int streamId,
|
|
|
|
Http2Headers headers, int streamDependency, short weight, boolean exclusive,
|
2017-01-26 18:03:42 +01:00
|
|
|
int padding, boolean endStream, ChannelPromise promise) {
|
2015-01-31 04:54:35 +01:00
|
|
|
return writeHeadersInternal(ctx, streamId, headers, padding, endStream,
|
|
|
|
true, streamDependency, weight, exclusive, promise);
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture writePriority(ChannelHandlerContext ctx, int streamId,
|
|
|
|
int streamDependency, short weight, boolean exclusive, ChannelPromise promise) {
|
|
|
|
try {
|
|
|
|
verifyStreamId(streamId, STREAM_ID);
|
|
|
|
verifyStreamId(streamDependency, STREAM_DEPENDENCY);
|
|
|
|
verifyWeight(weight);
|
|
|
|
|
2015-01-31 04:54:35 +01:00
|
|
|
ByteBuf buf = ctx.alloc().buffer(PRIORITY_FRAME_LENGTH);
|
|
|
|
writeFrameHeaderInternal(buf, PRIORITY_ENTRY_LENGTH, PRIORITY, new Http2Flags(), streamId);
|
2017-03-30 03:54:44 +02:00
|
|
|
buf.writeInt(exclusive ? (int) (0x80000000L | streamDependency) : streamDependency);
|
2015-01-20 01:48:11 +01:00
|
|
|
// Adjust the weight so that it fits into a single byte on the wire.
|
2015-01-31 04:54:35 +01:00
|
|
|
buf.writeByte(weight - 1);
|
|
|
|
return ctx.write(buf, promise);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
return promise.setFailure(t);
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture writeRstStream(ChannelHandlerContext ctx, int streamId, long errorCode,
|
|
|
|
ChannelPromise promise) {
|
|
|
|
try {
|
|
|
|
verifyStreamId(streamId, STREAM_ID);
|
|
|
|
verifyErrorCode(errorCode);
|
|
|
|
|
2015-01-31 04:54:35 +01:00
|
|
|
ByteBuf buf = ctx.alloc().buffer(RST_STREAM_FRAME_LENGTH);
|
|
|
|
writeFrameHeaderInternal(buf, INT_FIELD_LENGTH, RST_STREAM, new Http2Flags(), streamId);
|
2017-03-30 03:54:44 +02:00
|
|
|
buf.writeInt((int) errorCode);
|
2015-01-31 04:54:35 +01:00
|
|
|
return ctx.write(buf, promise);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
return promise.setFailure(t);
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture writeSettings(ChannelHandlerContext ctx, Http2Settings settings,
|
|
|
|
ChannelPromise promise) {
|
|
|
|
try {
|
|
|
|
checkNotNull(settings, "settings");
|
|
|
|
int payloadLength = SETTING_ENTRY_LENGTH * settings.size();
|
2015-01-31 04:54:35 +01:00
|
|
|
ByteBuf buf = ctx.alloc().buffer(FRAME_HEADER_LENGTH + settings.size() * SETTING_ENTRY_LENGTH);
|
|
|
|
writeFrameHeaderInternal(buf, payloadLength, SETTINGS, new Http2Flags(), 0);
|
2015-07-13 23:53:20 +02:00
|
|
|
for (Http2Settings.PrimitiveEntry<Long> entry : settings.entries()) {
|
2017-03-30 03:54:44 +02:00
|
|
|
buf.writeChar(entry.key());
|
|
|
|
buf.writeInt(entry.value().intValue());
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
2015-01-31 04:54:35 +01:00
|
|
|
return ctx.write(buf, promise);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
return promise.setFailure(t);
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture writeSettingsAck(ChannelHandlerContext ctx, ChannelPromise promise) {
|
|
|
|
try {
|
2015-01-31 04:54:35 +01:00
|
|
|
ByteBuf buf = ctx.alloc().buffer(FRAME_HEADER_LENGTH);
|
|
|
|
writeFrameHeaderInternal(buf, 0, SETTINGS, new Http2Flags().ack(true), 0);
|
|
|
|
return ctx.write(buf, promise);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
return promise.setFailure(t);
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-02-05 11:57:49 +01:00
|
|
|
public ChannelFuture writePing(ChannelHandlerContext ctx, boolean ack, long data, ChannelPromise promise) {
|
|
|
|
Http2Flags flags = ack ? new Http2Flags().ack(true) : new Http2Flags();
|
|
|
|
ByteBuf buf = ctx.alloc().buffer(FRAME_HEADER_LENGTH + PING_FRAME_PAYLOAD_LENGTH);
|
|
|
|
// Assume nothing below will throw until buf is written. That way we don't have to take care of ownership
|
|
|
|
// in the catch block.
|
|
|
|
writeFrameHeaderInternal(buf, PING_FRAME_PAYLOAD_LENGTH, PING, flags, 0);
|
|
|
|
buf.writeLong(data);
|
|
|
|
return ctx.write(buf, promise);
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture writePushPromise(ChannelHandlerContext ctx, int streamId,
|
2017-01-26 18:03:42 +01:00
|
|
|
int promisedStreamId, Http2Headers headers, int padding, ChannelPromise promise) {
|
2015-01-20 01:48:11 +01:00
|
|
|
ByteBuf headerBlock = null;
|
2015-01-31 04:54:35 +01:00
|
|
|
SimpleChannelPromiseAggregator promiseAggregator =
|
|
|
|
new SimpleChannelPromiseAggregator(promise, ctx.channel(), ctx.executor());
|
2015-01-20 01:48:11 +01:00
|
|
|
try {
|
|
|
|
verifyStreamId(streamId, STREAM_ID);
|
|
|
|
verifyStreamId(promisedStreamId, "Promised Stream ID");
|
|
|
|
verifyPadding(padding);
|
|
|
|
|
|
|
|
// Encode the entire header block into an intermediate buffer.
|
|
|
|
headerBlock = ctx.alloc().buffer();
|
2016-10-17 14:33:27 +02:00
|
|
|
headersEncoder.encodeHeaders(streamId, headers, headerBlock);
|
2015-01-20 01:48:11 +01:00
|
|
|
|
|
|
|
// Read the first fragment (possibly everything).
|
|
|
|
Http2Flags flags = new Http2Flags().paddingPresent(padding > 0);
|
2015-01-31 04:54:35 +01:00
|
|
|
// INT_FIELD_LENGTH is for the length of the promisedStreamId
|
2016-06-23 14:09:23 +02:00
|
|
|
int nonFragmentLength = INT_FIELD_LENGTH + padding;
|
2015-01-20 01:48:11 +01:00
|
|
|
int maxFragmentLength = maxFrameSize - nonFragmentLength;
|
2016-04-14 10:31:48 +02:00
|
|
|
ByteBuf fragment = headerBlock.readRetainedSlice(min(headerBlock.readableBytes(), maxFragmentLength));
|
2015-01-20 01:48:11 +01:00
|
|
|
|
2015-01-31 04:54:35 +01:00
|
|
|
flags.endOfHeaders(!headerBlock.isReadable());
|
2015-01-20 01:48:11 +01:00
|
|
|
|
|
|
|
int payloadLength = fragment.readableBytes() + nonFragmentLength;
|
2015-01-31 04:54:35 +01:00
|
|
|
ByteBuf buf = ctx.alloc().buffer(PUSH_PROMISE_FRAME_HEADER_LENGTH);
|
|
|
|
writeFrameHeaderInternal(buf, payloadLength, PUSH_PROMISE, flags, streamId);
|
|
|
|
writePaddingLength(buf, padding);
|
2015-01-20 01:48:11 +01:00
|
|
|
|
|
|
|
// Write out the promised stream ID.
|
2015-01-31 04:54:35 +01:00
|
|
|
buf.writeInt(promisedStreamId);
|
|
|
|
ctx.write(buf, promiseAggregator.newPromise());
|
2015-01-20 01:48:11 +01:00
|
|
|
|
|
|
|
// Write the first fragment.
|
2016-04-14 10:31:48 +02:00
|
|
|
ctx.write(fragment, promiseAggregator.newPromise());
|
2015-01-20 01:48:11 +01:00
|
|
|
|
2016-06-23 14:09:23 +02:00
|
|
|
// Write out the padding, if any.
|
|
|
|
if (paddingBytes(padding) > 0) {
|
|
|
|
ctx.write(ZERO_BUFFER.slice(0, paddingBytes(padding)), promiseAggregator.newPromise());
|
2015-01-31 04:54:35 +01:00
|
|
|
}
|
2015-01-20 01:48:11 +01:00
|
|
|
|
2015-01-31 04:54:35 +01:00
|
|
|
if (!flags.endOfHeaders()) {
|
|
|
|
writeContinuationFrames(ctx, streamId, headerBlock, padding, promiseAggregator);
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
2017-01-14 02:09:44 +01:00
|
|
|
} catch (Http2Exception e) {
|
|
|
|
promiseAggregator.setFailure(e);
|
2015-01-31 04:54:35 +01:00
|
|
|
} catch (Throwable t) {
|
2016-03-07 19:05:22 +01:00
|
|
|
promiseAggregator.setFailure(t);
|
2017-01-14 02:09:44 +01:00
|
|
|
promiseAggregator.doneAllocatingPromises();
|
|
|
|
PlatformDependent.throwException(t);
|
2015-01-20 01:48:11 +01:00
|
|
|
} finally {
|
|
|
|
if (headerBlock != null) {
|
|
|
|
headerBlock.release();
|
|
|
|
}
|
|
|
|
}
|
2016-03-07 19:05:22 +01:00
|
|
|
return promiseAggregator.doneAllocatingPromises();
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture writeGoAway(ChannelHandlerContext ctx, int lastStreamId, long errorCode,
|
|
|
|
ByteBuf debugData, ChannelPromise promise) {
|
2015-01-31 04:54:35 +01:00
|
|
|
SimpleChannelPromiseAggregator promiseAggregator =
|
|
|
|
new SimpleChannelPromiseAggregator(promise, ctx.channel(), ctx.executor());
|
2015-01-20 01:48:11 +01:00
|
|
|
try {
|
|
|
|
verifyStreamOrConnectionId(lastStreamId, "Last Stream ID");
|
|
|
|
verifyErrorCode(errorCode);
|
|
|
|
|
|
|
|
int payloadLength = 8 + debugData.readableBytes();
|
2015-01-31 04:54:35 +01:00
|
|
|
ByteBuf buf = ctx.alloc().buffer(GO_AWAY_FRAME_HEADER_LENGTH);
|
2017-11-04 01:09:42 +01:00
|
|
|
// Assume nothing below will throw until buf is written. That way we don't have to take care of ownership
|
|
|
|
// in the catch block.
|
2015-01-31 04:54:35 +01:00
|
|
|
writeFrameHeaderInternal(buf, payloadLength, GO_AWAY, new Http2Flags(), 0);
|
|
|
|
buf.writeInt(lastStreamId);
|
2017-03-30 03:54:44 +02:00
|
|
|
buf.writeInt((int) errorCode);
|
2015-01-31 04:54:35 +01:00
|
|
|
ctx.write(buf, promiseAggregator.newPromise());
|
|
|
|
} catch (Throwable t) {
|
2017-11-04 01:09:42 +01:00
|
|
|
try {
|
2015-01-31 04:54:35 +01:00
|
|
|
debugData.release();
|
2017-11-04 01:09:42 +01:00
|
|
|
} finally {
|
|
|
|
promiseAggregator.setFailure(t);
|
|
|
|
promiseAggregator.doneAllocatingPromises();
|
2015-01-31 04:54:35 +01:00
|
|
|
}
|
2017-11-04 01:09:42 +01:00
|
|
|
return promiseAggregator;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
ctx.write(debugData, promiseAggregator.newPromise());
|
|
|
|
} catch (Throwable t) {
|
2016-03-07 19:05:22 +01:00
|
|
|
promiseAggregator.setFailure(t);
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
2016-03-07 19:05:22 +01:00
|
|
|
return promiseAggregator.doneAllocatingPromises();
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture writeWindowUpdate(ChannelHandlerContext ctx, int streamId,
|
|
|
|
int windowSizeIncrement, ChannelPromise promise) {
|
|
|
|
try {
|
|
|
|
verifyStreamOrConnectionId(streamId, STREAM_ID);
|
|
|
|
verifyWindowSizeIncrement(windowSizeIncrement);
|
|
|
|
|
2015-01-31 04:54:35 +01:00
|
|
|
ByteBuf buf = ctx.alloc().buffer(WINDOW_UPDATE_FRAME_LENGTH);
|
|
|
|
writeFrameHeaderInternal(buf, INT_FIELD_LENGTH, WINDOW_UPDATE, new Http2Flags(), streamId);
|
|
|
|
buf.writeInt(windowSizeIncrement);
|
|
|
|
return ctx.write(buf, promise);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
return promise.setFailure(t);
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture writeFrame(ChannelHandlerContext ctx, byte frameType, int streamId,
|
|
|
|
Http2Flags flags, ByteBuf payload, ChannelPromise promise) {
|
2015-01-31 04:54:35 +01:00
|
|
|
SimpleChannelPromiseAggregator promiseAggregator =
|
|
|
|
new SimpleChannelPromiseAggregator(promise, ctx.channel(), ctx.executor());
|
2015-01-20 01:48:11 +01:00
|
|
|
try {
|
|
|
|
verifyStreamOrConnectionId(streamId, STREAM_ID);
|
2015-01-31 04:54:35 +01:00
|
|
|
ByteBuf buf = ctx.alloc().buffer(FRAME_HEADER_LENGTH);
|
2017-11-04 01:09:42 +01:00
|
|
|
// Assume nothing below will throw until buf is written. That way we don't have to take care of ownership
|
|
|
|
// in the catch block.
|
2015-01-31 04:54:35 +01:00
|
|
|
writeFrameHeaderInternal(buf, payload.readableBytes(), frameType, flags, streamId);
|
|
|
|
ctx.write(buf, promiseAggregator.newPromise());
|
|
|
|
} catch (Throwable t) {
|
2017-11-04 01:09:42 +01:00
|
|
|
try {
|
2015-01-31 04:54:35 +01:00
|
|
|
payload.release();
|
2017-11-04 01:09:42 +01:00
|
|
|
} finally {
|
|
|
|
promiseAggregator.setFailure(t);
|
|
|
|
promiseAggregator.doneAllocatingPromises();
|
2015-01-31 04:54:35 +01:00
|
|
|
}
|
2017-11-04 01:09:42 +01:00
|
|
|
return promiseAggregator;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
ctx.write(payload, promiseAggregator.newPromise());
|
|
|
|
} catch (Throwable t) {
|
2016-03-07 19:05:22 +01:00
|
|
|
promiseAggregator.setFailure(t);
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
2016-03-07 19:05:22 +01:00
|
|
|
return promiseAggregator.doneAllocatingPromises();
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
|
|
|
|
2015-01-31 04:54:35 +01:00
|
|
|
private ChannelFuture writeHeadersInternal(ChannelHandlerContext ctx,
|
2015-01-20 01:48:11 +01:00
|
|
|
int streamId, Http2Headers headers, int padding, boolean endStream,
|
2017-01-26 18:03:42 +01:00
|
|
|
boolean hasPriority, int streamDependency, short weight, boolean exclusive, ChannelPromise promise) {
|
2015-01-20 01:48:11 +01:00
|
|
|
ByteBuf headerBlock = null;
|
2015-01-31 04:54:35 +01:00
|
|
|
SimpleChannelPromiseAggregator promiseAggregator =
|
|
|
|
new SimpleChannelPromiseAggregator(promise, ctx.channel(), ctx.executor());
|
2015-01-20 01:48:11 +01:00
|
|
|
try {
|
|
|
|
verifyStreamId(streamId, STREAM_ID);
|
|
|
|
if (hasPriority) {
|
|
|
|
verifyStreamOrConnectionId(streamDependency, STREAM_DEPENDENCY);
|
|
|
|
verifyPadding(padding);
|
|
|
|
verifyWeight(weight);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode the entire header block.
|
|
|
|
headerBlock = ctx.alloc().buffer();
|
2016-10-17 14:33:27 +02:00
|
|
|
headersEncoder.encodeHeaders(streamId, headers, headerBlock);
|
2015-01-20 01:48:11 +01:00
|
|
|
|
|
|
|
Http2Flags flags =
|
|
|
|
new Http2Flags().endOfStream(endStream).priorityPresent(hasPriority).paddingPresent(padding > 0);
|
|
|
|
|
|
|
|
// Read the first fragment (possibly everything).
|
2016-06-23 14:09:23 +02:00
|
|
|
int nonFragmentBytes = padding + flags.getNumPriorityBytes();
|
2015-01-20 01:48:11 +01:00
|
|
|
int maxFragmentLength = maxFrameSize - nonFragmentBytes;
|
2016-04-14 10:31:48 +02:00
|
|
|
ByteBuf fragment = headerBlock.readRetainedSlice(min(headerBlock.readableBytes(), maxFragmentLength));
|
2015-01-20 01:48:11 +01:00
|
|
|
|
|
|
|
// Set the end of headers flag for the first frame.
|
2015-01-31 04:54:35 +01:00
|
|
|
flags.endOfHeaders(!headerBlock.isReadable());
|
2015-01-20 01:48:11 +01:00
|
|
|
|
|
|
|
int payloadLength = fragment.readableBytes() + nonFragmentBytes;
|
2015-01-31 04:54:35 +01:00
|
|
|
ByteBuf buf = ctx.alloc().buffer(HEADERS_FRAME_HEADER_LENGTH);
|
|
|
|
writeFrameHeaderInternal(buf, payloadLength, HEADERS, flags, streamId);
|
|
|
|
writePaddingLength(buf, padding);
|
2015-01-20 01:48:11 +01:00
|
|
|
|
|
|
|
if (hasPriority) {
|
2017-03-30 03:54:44 +02:00
|
|
|
buf.writeInt(exclusive ? (int) (0x80000000L | streamDependency) : streamDependency);
|
2015-01-20 01:48:11 +01:00
|
|
|
|
|
|
|
// Adjust the weight so that it fits into a single byte on the wire.
|
2015-01-31 04:54:35 +01:00
|
|
|
buf.writeByte(weight - 1);
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
2015-01-31 04:54:35 +01:00
|
|
|
ctx.write(buf, promiseAggregator.newPromise());
|
2015-01-20 01:48:11 +01:00
|
|
|
|
|
|
|
// Write the first fragment.
|
2016-04-14 10:31:48 +02:00
|
|
|
ctx.write(fragment, promiseAggregator.newPromise());
|
2015-01-20 01:48:11 +01:00
|
|
|
|
2016-06-23 14:09:23 +02:00
|
|
|
// Write out the padding, if any.
|
|
|
|
if (paddingBytes(padding) > 0) {
|
|
|
|
ctx.write(ZERO_BUFFER.slice(0, paddingBytes(padding)), promiseAggregator.newPromise());
|
2015-01-31 04:54:35 +01:00
|
|
|
}
|
2015-01-20 01:48:11 +01:00
|
|
|
|
2015-01-31 04:54:35 +01:00
|
|
|
if (!flags.endOfHeaders()) {
|
|
|
|
writeContinuationFrames(ctx, streamId, headerBlock, padding, promiseAggregator);
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
2017-01-14 02:09:44 +01:00
|
|
|
} catch (Http2Exception e) {
|
|
|
|
promiseAggregator.setFailure(e);
|
2015-01-31 04:54:35 +01:00
|
|
|
} catch (Throwable t) {
|
2016-03-07 19:05:22 +01:00
|
|
|
promiseAggregator.setFailure(t);
|
2017-01-14 02:09:44 +01:00
|
|
|
promiseAggregator.doneAllocatingPromises();
|
|
|
|
PlatformDependent.throwException(t);
|
2015-01-20 01:48:11 +01:00
|
|
|
} finally {
|
|
|
|
if (headerBlock != null) {
|
|
|
|
headerBlock.release();
|
|
|
|
}
|
|
|
|
}
|
2016-03-07 19:05:22 +01:00
|
|
|
return promiseAggregator.doneAllocatingPromises();
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-01-31 04:54:35 +01:00
|
|
|
* Writes as many continuation frames as needed until {@code padding} and {@code headerBlock} are consumed.
|
2015-01-20 01:48:11 +01:00
|
|
|
*/
|
2015-01-31 04:54:35 +01:00
|
|
|
private ChannelFuture writeContinuationFrames(ChannelHandlerContext ctx, int streamId,
|
|
|
|
ByteBuf headerBlock, int padding, SimpleChannelPromiseAggregator promiseAggregator) {
|
2015-01-20 01:48:11 +01:00
|
|
|
Http2Flags flags = new Http2Flags().paddingPresent(padding > 0);
|
2016-06-23 14:09:23 +02:00
|
|
|
int maxFragmentLength = maxFrameSize - padding;
|
2015-01-31 04:54:35 +01:00
|
|
|
// TODO: same padding is applied to all frames, is this desired?
|
|
|
|
if (maxFragmentLength <= 0) {
|
|
|
|
return promiseAggregator.setFailure(new IllegalArgumentException(
|
|
|
|
"Padding [" + padding + "] is too large for max frame size [" + maxFrameSize + "]"));
|
|
|
|
}
|
2015-01-20 01:48:11 +01:00
|
|
|
|
2015-01-31 04:54:35 +01:00
|
|
|
if (headerBlock.isReadable()) {
|
|
|
|
// The frame header (and padding) only changes on the last frame, so allocate it once and re-use
|
2015-11-19 05:46:11 +01:00
|
|
|
int fragmentReadableBytes = min(headerBlock.readableBytes(), maxFragmentLength);
|
2016-06-23 14:09:23 +02:00
|
|
|
int payloadLength = fragmentReadableBytes + padding;
|
2015-01-31 04:54:35 +01:00
|
|
|
ByteBuf buf = ctx.alloc().buffer(CONTINUATION_FRAME_HEADER_LENGTH);
|
|
|
|
writeFrameHeaderInternal(buf, payloadLength, CONTINUATION, flags, streamId);
|
|
|
|
writePaddingLength(buf, padding);
|
|
|
|
|
|
|
|
do {
|
2015-11-19 05:46:11 +01:00
|
|
|
fragmentReadableBytes = min(headerBlock.readableBytes(), maxFragmentLength);
|
2016-04-14 10:31:48 +02:00
|
|
|
ByteBuf fragment = headerBlock.readRetainedSlice(fragmentReadableBytes);
|
2015-01-31 04:54:35 +01:00
|
|
|
|
2016-06-23 14:09:23 +02:00
|
|
|
payloadLength = fragmentReadableBytes + padding;
|
2015-01-31 04:54:35 +01:00
|
|
|
if (headerBlock.isReadable()) {
|
|
|
|
ctx.write(buf.retain(), promiseAggregator.newPromise());
|
|
|
|
} else {
|
|
|
|
// The frame header is different for the last frame, so re-allocate and release the old buffer
|
|
|
|
flags = flags.endOfHeaders(true);
|
|
|
|
buf.release();
|
|
|
|
buf = ctx.alloc().buffer(CONTINUATION_FRAME_HEADER_LENGTH);
|
|
|
|
writeFrameHeaderInternal(buf, payloadLength, CONTINUATION, flags, streamId);
|
|
|
|
writePaddingLength(buf, padding);
|
|
|
|
ctx.write(buf, promiseAggregator.newPromise());
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.write(fragment, promiseAggregator.newPromise());
|
|
|
|
|
|
|
|
// Write out the padding, if any.
|
2016-06-23 14:09:23 +02:00
|
|
|
if (paddingBytes(padding) > 0) {
|
|
|
|
ctx.write(ZERO_BUFFER.slice(0, paddingBytes(padding)), promiseAggregator.newPromise());
|
2015-01-31 04:54:35 +01:00
|
|
|
}
|
|
|
|
} while(headerBlock.isReadable());
|
|
|
|
}
|
|
|
|
return promiseAggregator;
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
|
|
|
|
2016-06-23 14:09:23 +02:00
|
|
|
/**
|
|
|
|
* Returns the number of padding bytes that should be appended to the end of a frame.
|
|
|
|
*/
|
|
|
|
private static int paddingBytes(int padding) {
|
|
|
|
// The padding parameter contains the 1 byte pad length field as well as the trailing padding bytes.
|
|
|
|
// Subtract 1, so to only get the number of padding bytes that need to be appended to the end of a frame.
|
|
|
|
return padding - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void writePaddingLength(ByteBuf buf, int padding) {
|
|
|
|
if (padding > 0) {
|
2015-01-31 04:54:35 +01:00
|
|
|
// It is assumed that the padding length has been bounds checked before this
|
2016-06-23 14:09:23 +02:00
|
|
|
// Minus 1, as the pad length field is included in the padding parameter and is 1 byte wide.
|
|
|
|
buf.writeByte(padding - 1);
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void verifyStreamId(int streamId, String argumentName) {
|
|
|
|
if (streamId <= 0) {
|
|
|
|
throw new IllegalArgumentException(argumentName + " must be > 0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void verifyStreamOrConnectionId(int streamId, String argumentName) {
|
|
|
|
if (streamId < 0) {
|
|
|
|
throw new IllegalArgumentException(argumentName + " must be >= 0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void verifyWeight(short weight) {
|
|
|
|
if (weight < MIN_WEIGHT || weight > MAX_WEIGHT) {
|
|
|
|
throw new IllegalArgumentException("Invalid weight: " + weight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void verifyErrorCode(long errorCode) {
|
|
|
|
if (errorCode < 0 || errorCode > MAX_UNSIGNED_INT) {
|
|
|
|
throw new IllegalArgumentException("Invalid errorCode: " + errorCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void verifyWindowSizeIncrement(int windowSizeIncrement) {
|
|
|
|
if (windowSizeIncrement < 0) {
|
|
|
|
throw new IllegalArgumentException("WindowSizeIncrement must be >= 0");
|
|
|
|
}
|
|
|
|
}
|
2015-09-09 22:40:07 +02:00
|
|
|
|
|
|
|
private static void verifyPingPayload(ByteBuf data) {
|
|
|
|
if (data == null || data.readableBytes() != PING_FRAME_PAYLOAD_LENGTH) {
|
|
|
|
throw new IllegalArgumentException("Opaque data must be " + PING_FRAME_PAYLOAD_LENGTH + " bytes");
|
|
|
|
}
|
|
|
|
}
|
2015-01-20 01:48:11 +01:00
|
|
|
}
|