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);
}
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);
}
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();
}
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();
}
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);
}
Aggregations