Search in sources :

Example 6 with FullHttpMessage

use of io.netty.handler.codec.http.FullHttpMessage in project netty by netty.

the class InboundHttp2ToHttpAdapter method onDataRead.

@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
    Http2Stream stream = connection.stream(streamId);
    FullHttpMessage msg = getMessage(stream);
    if (msg == null) {
        throw connectionError(PROTOCOL_ERROR, "Data Frame received for unknown stream id %d", streamId);
    }
    ByteBuf content = msg.content();
    final int dataReadableBytes = data.readableBytes();
    if (content.readableBytes() > maxContentLength - dataReadableBytes) {
        throw connectionError(INTERNAL_ERROR, "Content length exceeded max of %d for stream id %d", maxContentLength, streamId);
    }
    content.writeBytes(data, data.readerIndex(), dataReadableBytes);
    if (endOfStream) {
        fireChannelRead(ctx, msg, false, stream);
    }
    // All bytes have been processed.
    return dataReadableBytes + padding;
}
Also used : FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) ByteBuf(io.netty.buffer.ByteBuf)

Example 7 with FullHttpMessage

use of io.netty.handler.codec.http.FullHttpMessage in project netty by netty.

the class InboundHttp2ToHttpAdapter method onHeadersRead.

@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endOfStream) throws Http2Exception {
    Http2Stream stream = connection.stream(streamId);
    FullHttpMessage msg = processHeadersBegin(ctx, stream, headers, endOfStream, true, true);
    if (msg != null) {
        processHeadersEnd(ctx, stream, msg, endOfStream);
    }
}
Also used : FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage)

Example 8 with FullHttpMessage

use of io.netty.handler.codec.http.FullHttpMessage in project netty by netty.

the class InboundHttpToHttp2Adapter method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof FullHttpMessage) {
        FullHttpMessage message = (FullHttpMessage) msg;
        try {
            int streamId = getStreamId(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();
        }
    } else {
        super.channelRead(ctx, msg);
    }
}
Also used : FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage)

Example 9 with FullHttpMessage

use of io.netty.handler.codec.http.FullHttpMessage in project netty by netty.

the class InboundHttp2ToHttpAdapterTest method clientRequestMultipleEmptyDataFrames.

@Test
public void clientRequestMultipleEmptyDataFrames() throws Exception {
    boostrapEnv(1, 1, 1);
    final String text = "";
    final ByteBuf content = Unpooled.copiedBuffer(text.getBytes());
    final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/some/path/resource2", content, true);
    try {
        HttpHeaders httpHeaders = request.headers();
        httpHeaders.setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), 3);
        httpHeaders.setInt(HttpHeaderNames.CONTENT_LENGTH, text.length());
        httpHeaders.setShort(HttpConversionUtil.ExtensionHeaderNames.STREAM_WEIGHT.text(), (short) 16);
        final Http2Headers http2Headers = new DefaultHttp2Headers().method(new AsciiString("GET")).path(new AsciiString("/some/path/resource2"));
        runInChannel(clientChannel, new Http2Runnable() {

            @Override
            public void run() throws Http2Exception {
                clientHandler.encoder().writeHeaders(ctxClient(), 3, http2Headers, 0, false, newPromiseClient());
                clientHandler.encoder().writeData(ctxClient(), 3, content.retain(), 0, false, newPromiseClient());
                clientHandler.encoder().writeData(ctxClient(), 3, content.retain(), 0, false, newPromiseClient());
                clientHandler.encoder().writeData(ctxClient(), 3, content.retain(), 0, true, newPromiseClient());
                clientChannel.flush();
            }
        });
        awaitRequests();
        ArgumentCaptor<FullHttpMessage> requestCaptor = ArgumentCaptor.forClass(FullHttpMessage.class);
        verify(serverListener).messageReceived(requestCaptor.capture());
        capturedRequests = requestCaptor.getAllValues();
        assertEquals(request, capturedRequests.get(0));
    } finally {
        request.release();
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Http2CodecUtil.getEmbeddedHttp2Exception(io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) AsciiString(io.netty.util.AsciiString) AsciiString(io.netty.util.AsciiString) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 10 with FullHttpMessage

use of io.netty.handler.codec.http.FullHttpMessage in project netty by netty.

the class InboundHttp2ToHttpAdapterTest method clientRequestSingleHeaderNoDataFrames.

@Test
public void clientRequestSingleHeaderNoDataFrames() throws Exception {
    boostrapEnv(1, 1, 1);
    final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/some/path/resource2", true);
    try {
        HttpHeaders httpHeaders = request.headers();
        httpHeaders.set(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), "https");
        httpHeaders.set(HttpHeaderNames.HOST, "example.org");
        httpHeaders.setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), 3);
        httpHeaders.setInt(HttpHeaderNames.CONTENT_LENGTH, 0);
        httpHeaders.setShort(HttpConversionUtil.ExtensionHeaderNames.STREAM_WEIGHT.text(), (short) 16);
        final Http2Headers http2Headers = new DefaultHttp2Headers().method(new AsciiString("GET")).scheme(new AsciiString("https")).authority(new AsciiString("example.org")).path(new AsciiString("/some/path/resource2"));
        runInChannel(clientChannel, new Http2Runnable() {

            @Override
            public void run() throws Http2Exception {
                clientHandler.encoder().writeHeaders(ctxClient(), 3, http2Headers, 0, true, newPromiseClient());
                clientChannel.flush();
            }
        });
        awaitRequests();
        ArgumentCaptor<FullHttpMessage> requestCaptor = ArgumentCaptor.forClass(FullHttpMessage.class);
        verify(serverListener).messageReceived(requestCaptor.capture());
        capturedRequests = requestCaptor.getAllValues();
        assertEquals(request, capturedRequests.get(0));
    } finally {
        request.release();
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Http2CodecUtil.getEmbeddedHttp2Exception(io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) AsciiString(io.netty.util.AsciiString) Test(org.junit.Test)

Aggregations

FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)20 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)12 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)12 ByteBuf (io.netty.buffer.ByteBuf)11 Http2CodecUtil.getEmbeddedHttp2Exception (io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception)11 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)11 AsciiString (io.netty.util.AsciiString)11 Test (org.junit.Test)11 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)10 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)3 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)1 EmptyHttpHeaders (io.netty.handler.codec.http.EmptyHttpHeaders)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 HttpContent (io.netty.handler.codec.http.HttpContent)1 HttpMessage (io.netty.handler.codec.http.HttpMessage)1 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)1 SimpleChannelPromiseAggregator (io.netty.handler.codec.http2.Http2CodecUtil.SimpleChannelPromiseAggregator)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1