Search in sources :

Example 6 with DecoderResult

use of io.netty.handler.codec.DecoderResult in project netty by netty.

the class HttpInvalidMessageTest method testResponseWithBadHeader.

@Test
public void testResponseWithBadHeader() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
    ch.writeInbound(Unpooled.copiedBuffer("HTTP/1.0 200 Maybe OK\r\n", CharsetUtil.UTF_8));
    ch.writeInbound(Unpooled.copiedBuffer("Good_Name: Good Value\r\n", CharsetUtil.UTF_8));
    ch.writeInbound(Unpooled.copiedBuffer("Bad=Name: Bad Value\r\n", CharsetUtil.UTF_8));
    ch.writeInbound(Unpooled.copiedBuffer("\r\n", CharsetUtil.UTF_8));
    HttpResponse res = ch.readInbound();
    DecoderResult dr = res.decoderResult();
    assertFalse(dr.isSuccess());
    assertTrue(dr.isFailure());
    assertEquals("Maybe OK", res.status().reasonPhrase());
    assertEquals("Good Value", res.headers().get(of("Good_Name")));
    ensureInboundTrafficDiscarded(ch);
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DecoderResult(io.netty.handler.codec.DecoderResult) Test(org.junit.Test)

Example 7 with DecoderResult

use of io.netty.handler.codec.DecoderResult in project riposte by Nike-Inc.

the class RequestInfoSetterHandlerTest method doChannelRead_HttpRequest_throws_exception_when_failed_decoder_result.

@Test
public void doChannelRead_HttpRequest_throws_exception_when_failed_decoder_result() {
    // given
    HttpRequest msgMock = mock(HttpRequest.class);
    DecoderResult decoderResult = mock(DecoderResult.class);
    doReturn(true).when(decoderResult).isFailure();
    doReturn(decoderResult).when(msgMock).getDecoderResult();
    // when
    Throwable thrownException = Assertions.catchThrowable(() -> handler.doChannelRead(ctxMock, msgMock));
    // then
    assertThat(thrownException).isExactlyInstanceOf(InvalidHttpRequestException.class);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DecoderResult(io.netty.handler.codec.DecoderResult) Test(org.junit.Test)

Example 8 with DecoderResult

use of io.netty.handler.codec.DecoderResult in project riposte by Nike-Inc.

the class RequestInfoSetterHandlerTest method doChannelRead_HttpContent_throws_exception_when_failed_decoder_result.

@Test
public void doChannelRead_HttpContent_throws_exception_when_failed_decoder_result() {
    // given
    DecoderResult decoderResult = mock(DecoderResult.class);
    doReturn(true).when(decoderResult).isFailure();
    doReturn(decoderResult).when(httpContentMock).getDecoderResult();
    // when
    Throwable thrownException = Assertions.catchThrowable(() -> handler.doChannelRead(ctxMock, httpContentMock));
    // then
    assertThat(thrownException).isExactlyInstanceOf(InvalidHttpRequestException.class);
    verify(httpContentMock).release();
}
Also used : DecoderResult(io.netty.handler.codec.DecoderResult) Test(org.junit.Test)

Example 9 with DecoderResult

use of io.netty.handler.codec.DecoderResult in project vert.x by eclipse.

the class ServerConnection method processMessage.

private void processMessage(Object msg) {
    if (msg instanceof HttpObject) {
        HttpObject obj = (HttpObject) msg;
        DecoderResult result = obj.decoderResult();
        if (result.isFailure()) {
            Throwable cause = result.cause();
            if (cause instanceof TooLongFrameException) {
                String causeMsg = cause.getMessage();
                HttpVersion version;
                if (msg instanceof HttpRequest) {
                    version = ((HttpRequest) msg).protocolVersion();
                } else if (currentRequest != null) {
                    version = currentRequest.version() == io.vertx.core.http.HttpVersion.HTTP_1_0 ? HttpVersion.HTTP_1_0 : HttpVersion.HTTP_1_1;
                } else {
                    version = HttpVersion.HTTP_1_1;
                }
                HttpResponseStatus status = causeMsg.startsWith("An HTTP line is larger than") ? HttpResponseStatus.REQUEST_URI_TOO_LONG : HttpResponseStatus.BAD_REQUEST;
                DefaultFullHttpResponse resp = new DefaultFullHttpResponse(version, status);
                writeToChannel(resp);
            }
            // That will close the connection as it is considered as unusable
            channel.pipeline().fireExceptionCaught(result.cause());
            return;
        }
        if (msg instanceof HttpRequest) {
            HttpRequest request = (HttpRequest) msg;
            if (server.options().isHandle100ContinueAutomatically()) {
                if (HttpHeaders.is100ContinueExpected(request)) {
                    write100Continue();
                }
            }
            HttpServerResponseImpl resp = new HttpServerResponseImpl(vertx, this, request);
            HttpServerRequestImpl req = new HttpServerRequestImpl(this, request, resp);
            handleRequest(req, resp);
        }
        if (msg instanceof HttpContent) {
            HttpContent chunk = (HttpContent) msg;
            if (chunk.content().isReadable()) {
                Buffer buff = Buffer.buffer(chunk.content());
                handleChunk(buff);
            }
            //TODO chunk trailers
            if (msg instanceof LastHttpContent) {
                if (!paused) {
                    handleEnd();
                } else {
                    // Requeue
                    pending.add(LastHttpContent.EMPTY_LAST_CONTENT);
                }
            }
        }
    } else if (msg instanceof WebSocketFrameInternal) {
        WebSocketFrameInternal frame = (WebSocketFrameInternal) msg;
        handleWsFrame(frame);
    }
    checkNextTick();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) WebSocketFrameInternal(io.vertx.core.http.impl.ws.WebSocketFrameInternal) DecoderResult(io.netty.handler.codec.DecoderResult) HttpVersion(io.netty.handler.codec.http.HttpVersion)

Example 10 with DecoderResult

use of io.netty.handler.codec.DecoderResult in project vert.x by eclipse.

the class ClientHandler method doMessageReceived.

@Override
protected void doMessageReceived(ClientConnection conn, ChannelHandlerContext ctx, Object msg) {
    if (conn == null) {
        return;
    }
    if (msg instanceof HttpObject) {
        HttpObject obj = (HttpObject) msg;
        DecoderResult result = obj.decoderResult();
        if (result.isFailure()) {
            // Close the connection as Netty's HttpResponseDecoder will not try further processing
            // see https://github.com/netty/netty/issues/3362
            conn.handleException(result.cause());
            conn.close();
            return;
        }
        if (msg instanceof HttpResponse) {
            HttpResponse response = (HttpResponse) obj;
            conn.handleResponse(response);
            return;
        }
        if (msg instanceof HttpContent) {
            HttpContent chunk = (HttpContent) obj;
            if (chunk.content().isReadable()) {
                Buffer buff = Buffer.buffer(chunk.content().slice());
                conn.handleResponseChunk(buff);
            }
            if (chunk instanceof LastHttpContent) {
                conn.handleResponseEnd((LastHttpContent) chunk);
            }
            return;
        }
    } else if (msg instanceof WebSocketFrameInternal) {
        WebSocketFrameInternal frame = (WebSocketFrameInternal) msg;
        switch(frame.type()) {
            case BINARY:
            case CONTINUATION:
            case TEXT:
                conn.handleWsFrame(frame);
                break;
            case PING:
                // Echo back the content of the PING frame as PONG frame as specified in RFC 6455 Section 5.5.2
                ctx.writeAndFlush(new WebSocketFrameImpl(FrameType.PONG, frame.getBinaryData()));
                break;
            case PONG:
                // Just ignore it
                break;
            case CLOSE:
                if (!closeFrameSent) {
                    // Echo back close frame and close the connection once it was written.
                    // This is specified in the WebSockets RFC 6455 Section  5.4.1
                    ctx.writeAndFlush(frame).addListener(ChannelFutureListener.CLOSE);
                    closeFrameSent = true;
                }
                break;
            default:
                throw new IllegalStateException("Invalid type: " + frame.type());
        }
        return;
    }
    throw new IllegalStateException("Invalid object " + msg);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) WebSocketFrameImpl(io.vertx.core.http.impl.ws.WebSocketFrameImpl) HttpObject(io.netty.handler.codec.http.HttpObject) DecoderResult(io.netty.handler.codec.DecoderResult) HttpResponse(io.netty.handler.codec.http.HttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) WebSocketFrameInternal(io.vertx.core.http.impl.ws.WebSocketFrameInternal)

Aggregations

DecoderResult (io.netty.handler.codec.DecoderResult)18 Test (org.junit.Test)7 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)5 Buffer (io.vertx.core.buffer.Buffer)2 WebSocketFrameInternal (io.vertx.core.http.impl.ws.WebSocketFrameInternal)2 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 HttpContent (io.netty.handler.codec.http.HttpContent)1 HttpObject (io.netty.handler.codec.http.HttpObject)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 HttpResponse (io.netty.handler.codec.http.HttpResponse)1 HttpVersion (io.netty.handler.codec.http.HttpVersion)1 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)1 WebSocketFrameImpl (io.vertx.core.http.impl.ws.WebSocketFrameImpl)1