package io.netty.handler.codec.http2;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.base64.Base64;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpServerUpgradeHandler;
import io.netty.util.CharsetUtil;
import io.netty.util.internal.UnstableApi;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.nio.CharBuffer;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static io.netty.handler.codec.base64.Base64Dialect.URL_SAFE;
import static io.netty.handler.codec.http2.Http2CodecUtil.FRAME_HEADER_LENGTH;
import static io.netty.handler.codec.http2.Http2CodecUtil.HTTP_UPGRADE_SETTINGS_HEADER;
import static io.netty.handler.codec.http2.Http2CodecUtil.writeFrameHeader;
import static io.netty.handler.codec.http2.Http2FrameTypes.SETTINGS;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
@UnstableApi
public class Http2ServerUpgradeCodec implements HttpServerUpgradeHandler.UpgradeCodec {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(Http2ServerUpgradeCodec.class);
private static final List<CharSequence> REQUIRED_UPGRADE_HEADERS =
Collections.singletonList(HTTP_UPGRADE_SETTINGS_HEADER);
private static final ChannelHandler[] EMPTY_HANDLERS = new ChannelHandler[0];
private final String handlerName;
private final Http2ConnectionHandler connectionHandler;
private final ChannelHandler[] handlers;
private final Http2FrameReader frameReader;
private Http2Settings settings;
public Http2ServerUpgradeCodec(Http2ConnectionHandler connectionHandler) {
this(null, connectionHandler, EMPTY_HANDLERS);
}
public Http2ServerUpgradeCodec(Http2MultiplexCodec http2Codec) {
this(null, http2Codec, EMPTY_HANDLERS);
}
public Http2ServerUpgradeCodec(String handlerName, Http2ConnectionHandler connectionHandler) {
this(handlerName, connectionHandler, EMPTY_HANDLERS);
}
public Http2ServerUpgradeCodec(String handlerName, Http2MultiplexCodec http2Codec) {
this(handlerName, http2Codec, EMPTY_HANDLERS);
}
public Http2ServerUpgradeCodec(Http2FrameCodec http2Codec, ChannelHandler... handlers) {
this(null, http2Codec, handlers);
}
private Http2ServerUpgradeCodec(String handlerName, Http2ConnectionHandler connectionHandler,
ChannelHandler... handlers) {
this.handlerName = handlerName;
this.connectionHandler = connectionHandler;
this.handlers = handlers;
frameReader = new DefaultHttp2FrameReader();
}
@Override
public Collection<CharSequence> requiredUpgradeHeaders() {
return REQUIRED_UPGRADE_HEADERS;
}
@Override
public boolean prepareUpgradeResponse(ChannelHandlerContext ctx, FullHttpRequest upgradeRequest,
HttpHeaders headers) {
try {
List<String> upgradeHeaders = upgradeRequest.headers().getAll(HTTP_UPGRADE_SETTINGS_HEADER);
if (upgradeHeaders.isEmpty() || upgradeHeaders.size() > 1) {
throw new IllegalArgumentException("There must be 1 and only 1 "
+ HTTP_UPGRADE_SETTINGS_HEADER + " header.");
}
settings = decodeSettingsHeader(ctx, upgradeHeaders.get(0));
return true;
} catch (Throwable cause) {
logger.info("Error during upgrade to HTTP/2", cause);
return false;
}
}
@Override
public void upgradeTo(final ChannelHandlerContext ctx, FullHttpRequest upgradeRequest) {
try {
ctx.pipeline().addAfter(ctx.name(), handlerName, connectionHandler);
connectionHandler.onHttpServerUpgrade(settings);
} catch (Http2Exception e) {
ctx.fireExceptionCaught(e);
ctx.close();
return;
}
if (handlers != null) {
final String name = ctx.pipeline().context(connectionHandler).name();
for (int i = handlers.length - 1; i >= 0; i--) {
ctx.pipeline().addAfter(name, null, handlers[i]);
}
}
}
private Http2Settings decodeSettingsHeader(ChannelHandlerContext ctx, CharSequence settingsHeader)
throws Http2Exception {
ByteBuf header = ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(settingsHeader), CharsetUtil.UTF_8);
try {
ByteBuf payload = Base64.decode(header, URL_SAFE);
ByteBuf frame = createSettingsFrame(ctx, payload);
return decodeSettings(ctx, frame);
} finally {
header.release();
}
}
private Http2Settings decodeSettings(ChannelHandlerContext ctx, ByteBuf frame) throws Http2Exception {
try {
final Http2Settings decodedSettings = new Http2Settings();
frameReader.readFrame(ctx, frame, new Http2FrameAdapter() {
@Override
public void onSettingsRead(ChannelHandlerContext ctx, Http2Settings settings) {
decodedSettings.copyFrom(settings);
}
});
return decodedSettings;
} finally {
frame.release();
}
}
private static ByteBuf createSettingsFrame(ChannelHandlerContext ctx, ByteBuf payload) {
ByteBuf frame = ctx.alloc().buffer(FRAME_HEADER_LENGTH + payload.readableBytes());
writeFrameHeader(frame, payload.readableBytes(), SETTINGS, new Http2Flags(), 0);
frame.writeBytes(payload);
payload.release();
return frame;
}
}