Search in sources :

Example 41 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project netty by netty.

the class HttpPostRequestEncoderTest method testDataIsMultipleOfChunkSize2.

@Test
public void testDataIsMultipleOfChunkSize2() throws Exception {
    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "http://localhost");
    HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(request, true);
    int length = 7943;
    char[] array = new char[length];
    Arrays.fill(array, 'a');
    String longText = new String(array);
    encoder.addBodyAttribute("foo", longText);
    assertNotNull(encoder.finalizeRequest());
    checkNextChunkSize(encoder, 8080);
    HttpContent httpContent = encoder.readChunk((ByteBufAllocator) null);
    assertTrue(httpContent instanceof LastHttpContent, "Expected LastHttpContent is not received");
    httpContent.release();
    assertTrue(encoder.isEndOfInput(), "Expected end of input is not receive");
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) Test(org.junit.jupiter.api.Test)

Example 42 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project netty by netty.

the class HttpPostRequestEncoderTest method testHttpPostRequestEncoderSlicedBuffer.

@Test
public void testHttpPostRequestEncoderSlicedBuffer() throws Exception {
    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "http://localhost");
    HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(request, true);
    // add Form attribute
    encoder.addBodyAttribute("getform", "POST");
    encoder.addBodyAttribute("info", "first value");
    encoder.addBodyAttribute("secondinfo", "secondvalue a&");
    encoder.addBodyAttribute("thirdinfo", "short text");
    int length = 100000;
    char[] array = new char[length];
    Arrays.fill(array, 'a');
    String longText = new String(array);
    encoder.addBodyAttribute("fourthinfo", longText.substring(0, 7470));
    File file1 = new File(getClass().getResource("/file-01.txt").toURI());
    encoder.addBodyFileUpload("myfile", file1, "application/x-zip-compressed", false);
    encoder.finalizeRequest();
    while (!encoder.isEndOfInput()) {
        HttpContent httpContent = encoder.readChunk((ByteBufAllocator) null);
        ByteBuf content = httpContent.content();
        int refCnt = content.refCnt();
        assertTrue((content.unwrap() == content || content.unwrap() == null) && refCnt == 1 || content.unwrap() != content && refCnt == 2, "content: " + content + " content.unwrap(): " + content.unwrap() + " refCnt: " + refCnt);
        httpContent.release();
    }
    encoder.cleanFiles();
    encoder.close();
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) File(java.io.File) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) Test(org.junit.jupiter.api.Test)

Example 43 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project zuul by Netflix.

the class ClientRequestReceiver method channelReadInternal.

private void channelReadInternal(final ChannelHandlerContext ctx, Object msg) throws Exception {
    // a response to the client channel.
    if (msg instanceof LastHttpContent) {
        ctx.channel().attr(ATTR_LAST_CONTENT_RECEIVED).set(Boolean.TRUE);
    }
    if (msg instanceof HttpRequest) {
        clientRequest = (HttpRequest) msg;
        zuulRequest = buildZuulHttpRequest(clientRequest, ctx);
        // Handle invalid HTTP requests.
        if (clientRequest.decoderResult().isFailure()) {
            LOG.warn("Invalid http request. clientRequest = {} , uri = {}, info = {}", clientRequest, clientRequest.uri(), ChannelUtils.channelInfoForLogging(ctx.channel()), clientRequest.decoderResult().cause());
            StatusCategoryUtils.setStatusCategory(zuulRequest.getContext(), ZuulStatusCategory.FAILURE_CLIENT_BAD_REQUEST);
            RejectionUtils.rejectByClosingConnection(ctx, ZuulStatusCategory.FAILURE_CLIENT_BAD_REQUEST, "decodefailure", clientRequest, /* injectedLatencyMillis= */
            null);
            return;
        } else if (zuulRequest.hasBody() && zuulRequest.getBodyLength() > zuulRequest.getMaxBodySize()) {
            String errorMsg = "Request too large. " + "clientRequest = " + clientRequest.toString() + ", uri = " + String.valueOf(clientRequest.uri()) + ", info = " + ChannelUtils.channelInfoForLogging(ctx.channel());
            final ZuulException ze = new ZuulException(errorMsg);
            ze.setStatusCode(HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE.code());
            StatusCategoryUtils.setStatusCategory(zuulRequest.getContext(), ZuulStatusCategory.FAILURE_CLIENT_BAD_REQUEST);
            zuulRequest.getContext().setError(ze);
            zuulRequest.getContext().setShouldSendErrorResponse(true);
        } else if (zuulRequest.getHeaders().getAll(HttpHeaderNames.HOST.toString()).size() > 1) {
            LOG.debug("Multiple Host headers. clientRequest = {} , uri = {}, info = {}", clientRequest, clientRequest.uri(), ChannelUtils.channelInfoForLogging(ctx.channel()));
            final ZuulException ze = new ZuulException("Multiple Host headers");
            ze.setStatusCode(HttpResponseStatus.BAD_REQUEST.code());
            StatusCategoryUtils.setStatusCategory(zuulRequest.getContext(), ZuulStatusCategory.FAILURE_CLIENT_BAD_REQUEST);
            zuulRequest.getContext().setError(ze);
            zuulRequest.getContext().setShouldSendErrorResponse(true);
        }
        handleExpect100Continue(ctx, clientRequest);
        // Send the request down the filter pipeline
        ctx.fireChannelRead(zuulRequest);
    } else if (msg instanceof HttpContent) {
        if ((zuulRequest != null) && (!zuulRequest.getContext().isCancelled())) {
            ctx.fireChannelRead(msg);
        } else {
            // We already sent response for this request, these are laggard request body chunks that are still arriving
            ReferenceCountUtil.release(msg);
        }
    } else if (msg instanceof HAProxyMessage) {
        // do nothing, should already be handled by ElbProxyProtocolHandler
        LOG.debug("Received HAProxyMessage for Proxy Protocol IP: {}", ((HAProxyMessage) msg).sourceAddress());
        ReferenceCountUtil.release(msg);
    } else {
        LOG.debug("Received unrecognized message type. " + msg.getClass().getName());
        ReferenceCountUtil.release(msg);
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) ZuulException(com.netflix.zuul.exception.ZuulException) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) HAProxyMessage(io.netty.handler.codec.haproxy.HAProxyMessage) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Example 44 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project zuul by Netflix.

the class ZuulFilterChainHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpRequestMessage) {
        zuulRequest = (HttpRequestMessage) msg;
        // Replace NETTY_SERVER_CHANNEL_HANDLER_CONTEXT in SessionContext
        final SessionContext zuulCtx = zuulRequest.getContext();
        zuulCtx.put(NETTY_SERVER_CHANNEL_HANDLER_CONTEXT, ctx);
        requestFilterChain.filter(zuulRequest);
    } else if ((msg instanceof HttpContent) && (zuulRequest != null)) {
        requestFilterChain.filter(zuulRequest, (HttpContent) msg);
    } else {
        LOG.debug("Received unrecognized message type. " + msg.getClass().getName());
        ReferenceCountUtil.release(msg);
    }
}
Also used : HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) SessionContext(com.netflix.zuul.context.SessionContext) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 45 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project zuul by Netflix.

the class Http2ContentLengthEnforcingHandler method channelRead.

/**
 * This checks that the content length does what it says, preventing a client from causing Zuul to misinterpret the
 * request.  Because this class is meant to work in an HTTP/2 setting, the content length and transfer encoding
 * checks are more semantics.  In particular, this checks:
 * <ul>
 *     <li>No duplicate Content length</li>
 *     <li>Content Length (if present) must always be greater than or equal to how much content has been seen</li>
 *     <li>Content Length (if present) must always be equal to how much content has been seen by the end</li>
 *     <li>Content Length cannot be present along with chunked transfer encoding.</li>
 * </ul>
 */
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpRequest) {
        HttpRequest req = (HttpRequest) msg;
        List<String> lengthHeaders = req.headers().getAll(HttpHeaderNames.CONTENT_LENGTH);
        if (lengthHeaders.size() > 1) {
            ctx.writeAndFlush(new DefaultHttp2ResetFrame(Http2Error.PROTOCOL_ERROR));
            return;
        } else if (lengthHeaders.size() == 1) {
            expectedContentLength = Long.parseLong(lengthHeaders.get(0));
            if (expectedContentLength < 0) {
                // TODO(carl-mastrangelo): this is not right, but meh.  Fix this to return a proper 400.
                ctx.writeAndFlush(new DefaultHttp2ResetFrame(Http2Error.PROTOCOL_ERROR));
                return;
            }
        }
        if (hasContentLength() && HttpUtil.isTransferEncodingChunked(req)) {
            // TODO(carl-mastrangelo): this is not right, but meh.  Fix this to return a proper 400.
            ctx.writeAndFlush(new DefaultHttp2ResetFrame(Http2Error.PROTOCOL_ERROR));
            return;
        }
    }
    if (msg instanceof HttpContent) {
        ByteBuf content = ((HttpContent) msg).content();
        incrementSeenContent(content.readableBytes());
        if (hasContentLength() && seenContentLength > expectedContentLength) {
            // TODO(carl-mastrangelo): this is not right, but meh.  Fix this to return a proper 400.
            ctx.writeAndFlush(new DefaultHttp2ResetFrame(Http2Error.PROTOCOL_ERROR));
            return;
        }
    }
    if (msg instanceof LastHttpContent) {
        if (hasContentLength() && seenContentLength != expectedContentLength) {
            // TODO(carl-mastrangelo): this is not right, but meh.  Fix this to return a proper 400.
            ctx.writeAndFlush(new DefaultHttp2ResetFrame(Http2Error.PROTOCOL_ERROR));
            return;
        }
    }
    super.channelRead(ctx, msg);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultHttp2ResetFrame(io.netty.handler.codec.http2.DefaultHttp2ResetFrame) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Aggregations

HttpContent (io.netty.handler.codec.http.HttpContent)158 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)122 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)62 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)59 Test (org.junit.Test)59 ByteBuf (io.netty.buffer.ByteBuf)40 HttpResponse (io.netty.handler.codec.http.HttpResponse)37 HttpRequest (io.netty.handler.codec.http.HttpRequest)35 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)34 ArrayList (java.util.ArrayList)30 HttpObject (io.netty.handler.codec.http.HttpObject)28 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)24 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)20 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)17 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)17 Channel (io.netty.channel.Channel)16 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)15 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)14 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)13 IOException (java.io.IOException)13