2015-04-13 07:19:20 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2016 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;
|
|
|
|
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
|
|
import io.netty.handler.codec.http.FullHttpMessage;
|
|
|
|
import io.netty.handler.codec.http.HttpHeaders;
|
|
|
|
import io.netty.handler.codec.http.HttpScheme;
|
2016-04-12 14:22:41 +02:00
|
|
|
import io.netty.util.internal.UnstableApi;
|
2015-04-13 07:19:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Translates HTTP/1.x object reads into HTTP/2 frames.
|
|
|
|
*/
|
2016-04-12 14:22:41 +02:00
|
|
|
@UnstableApi
|
2015-04-13 07:19:20 +02:00
|
|
|
public class InboundHttpToHttp2Adapter extends ChannelInboundHandlerAdapter {
|
|
|
|
private final Http2Connection connection;
|
|
|
|
private final Http2FrameListener listener;
|
|
|
|
|
|
|
|
public InboundHttpToHttp2Adapter(Http2Connection connection, Http2FrameListener listener) {
|
|
|
|
this.connection = connection;
|
|
|
|
this.listener = listener;
|
|
|
|
}
|
|
|
|
|
2017-08-11 07:51:45 +02:00
|
|
|
private static int getStreamId(Http2Connection connection, HttpHeaders httpHeaders) {
|
2015-04-13 07:19:20 +02:00
|
|
|
return httpHeaders.getInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(),
|
2016-03-25 20:51:11 +01:00
|
|
|
connection.remote().incrementAndGetNextStreamId());
|
2015-04-13 07:19:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
|
|
|
if (msg instanceof FullHttpMessage) {
|
2017-08-11 07:51:45 +02:00
|
|
|
handle(ctx, connection, listener, (FullHttpMessage) msg);
|
2015-04-13 07:19:20 +02:00
|
|
|
} else {
|
|
|
|
super.channelRead(ctx, msg);
|
|
|
|
}
|
|
|
|
}
|
2017-08-11 07:51:45 +02:00
|
|
|
|
|
|
|
static void handle(ChannelHandlerContext ctx, Http2Connection connection,
|
|
|
|
Http2FrameListener listener, FullHttpMessage message) throws Http2Exception {
|
|
|
|
try {
|
|
|
|
int streamId = getStreamId(connection, message.headers());
|
|
|
|
Http2Stream stream = connection.stream(streamId);
|
|
|
|
if (stream == null) {
|
|
|
|
stream = connection.remote().createStream(streamId, false);
|
|
|
|
}
|
|
|
|
message.headers().set(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), HttpScheme.HTTP.name());
|
|
|
|
Http2Headers messageHeaders = HttpConversionUtil.toHttp2Headers(message, true);
|
|
|
|
boolean hasContent = message.content().isReadable();
|
|
|
|
boolean hasTrailers = !message.trailingHeaders().isEmpty();
|
|
|
|
listener.onHeadersRead(
|
|
|
|
ctx, streamId, messageHeaders, 0, !(hasContent || hasTrailers));
|
|
|
|
if (hasContent) {
|
|
|
|
listener.onDataRead(ctx, streamId, message.content(), 0, !hasTrailers);
|
|
|
|
}
|
|
|
|
if (hasTrailers) {
|
|
|
|
Http2Headers headers = HttpConversionUtil.toHttp2Headers(message.trailingHeaders(), true);
|
|
|
|
listener.onHeadersRead(ctx, streamId, headers, 0, true);
|
|
|
|
}
|
|
|
|
stream.closeRemoteSide();
|
|
|
|
} finally {
|
|
|
|
message.release();
|
|
|
|
}
|
|
|
|
}
|
2015-04-13 07:19:20 +02:00
|
|
|
}
|