Search in sources :

Example 61 with DefaultFullHttpRequest

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

the class HttpPostRequestDecoderTest method testDecodeFullHttpRequestWithUrlEncodedBodyWithBrokenHexByte0.

@Test
public void testDecodeFullHttpRequestWithUrlEncodedBodyWithBrokenHexByte0() {
    byte[] bodyBytes = "foo=bar&a=b&empty=%&city=paris".getBytes();
    ByteBuf content = Unpooled.directBuffer(bodyBytes.length);
    content.writeBytes(bodyBytes);
    FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", content);
    try {
        new HttpPostRequestDecoder(req);
        fail("Was expecting an ErrorDataDecoderException");
    } catch (HttpPostRequestDecoder.ErrorDataDecoderException e) {
        assertEquals("Invalid hex byte at index '0' in string: '%'", e.getMessage());
    } finally {
        assertTrue(req.release());
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 62 with DefaultFullHttpRequest

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

the class HttpPostRequestDecoderTest method testDecodeFullHttpRequestWithUrlEncodedBodyWithInvalidHexNibbleLo.

@Test
public void testDecodeFullHttpRequestWithUrlEncodedBodyWithInvalidHexNibbleLo() {
    byte[] bodyBytes = "foo=bar&a=b&empty=%2g&city=london".getBytes();
    ByteBuf content = Unpooled.directBuffer(bodyBytes.length);
    content.writeBytes(bodyBytes);
    FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", content);
    try {
        new HttpPostRequestDecoder(req);
        fail("Was expecting an ErrorDataDecoderException");
    } catch (HttpPostRequestDecoder.ErrorDataDecoderException e) {
        assertEquals("Invalid hex byte at index '0' in string: '%2g'", e.getMessage());
    } finally {
        assertTrue(req.release());
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 63 with DefaultFullHttpRequest

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

the class HttpPostRequestDecoderTest method testMultipartCodecWithCRasEndOfAttribute.

// See https://github.com/netty/netty/issues/2544
@Test
public void testMultipartCodecWithCRasEndOfAttribute() throws Exception {
    final String boundary = "dLV9Wyq26L_-JQxk6ferf-RT153LhOO";
    // Force to use memory-based data.
    final DefaultHttpDataFactory inMemoryFactory = new DefaultHttpDataFactory(false);
    // Build test case
    String extradata = "aaaa";
    String[] datas = new String[5];
    for (int i = 0; i < 4; i++) {
        datas[i] = extradata;
        for (int j = 0; j < i; j++) {
            datas[i] += '\r';
        }
    }
    for (int i = 0; i < 4; i++) {
        final DefaultFullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "http://localhost");
        req.setDecoderResult(DecoderResult.SUCCESS);
        req.headers().add(HttpHeaderNames.CONTENT_TYPE, "multipart/form-data; boundary=" + boundary);
        req.headers().add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
        final String body = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"file" + i + "\"\r\n" + "Content-Type: image/gif\r\n" + "\r\n" + datas[i] + "\r\n" + "--" + boundary + "--\r\n";
        req.content().writeBytes(body.getBytes(CharsetUtil.UTF_8));
        // Create decoder instance to test.
        final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(inMemoryFactory, req);
        assertFalse(decoder.getBodyHttpDatas().isEmpty());
        // Check correctness: data size
        InterfaceHttpData httpdata = decoder.getBodyHttpData("file" + i);
        assertNotNull(httpdata);
        Attribute attribute = (Attribute) httpdata;
        byte[] datar = attribute.get();
        assertNotNull(datar);
        assertEquals(datas[i].getBytes(CharsetUtil.UTF_8).length, datar.length);
        decoder.destroy();
        assertTrue(req.release());
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) Test(org.junit.jupiter.api.Test)

Example 64 with DefaultFullHttpRequest

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

the class HttpPostRequestDecoderTest method testMultipartRequestWithFileInvalidCharset.

@Test
public void testMultipartRequestWithFileInvalidCharset() throws Exception {
    final String boundary = "dLV9Wyq26L_-JQxk6ferf-RT153LhOO";
    final DefaultFullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "http://localhost");
    req.headers().add(HttpHeaderNames.CONTENT_TYPE, "multipart/form-data; boundary=" + boundary);
    // Force to use memory-based data.
    final DefaultHttpDataFactory inMemoryFactory = new DefaultHttpDataFactory(false);
    final String data = "asdf";
    final String filename = "tmp;0.txt";
    final String body = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"" + filename + "\"\r\n" + "Content-Type: image/gif; charset=ABCD\r\n" + "\r\n" + data + "\r\n" + "--" + boundary + "--\r\n";
    req.content().writeBytes(body.getBytes(CharsetUtil.UTF_8));
    // Create decoder instance to test.
    try {
        new HttpPostRequestDecoder(inMemoryFactory, req);
        fail("Was expecting an ErrorDataDecoderException");
    } catch (HttpPostRequestDecoder.ErrorDataDecoderException e) {
        assertTrue(e.getCause() instanceof UnsupportedCharsetException);
    } finally {
        assertTrue(req.release());
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) Test(org.junit.jupiter.api.Test)

Example 65 with DefaultFullHttpRequest

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

the class HttpPostRequestDecoderTest method testMultipartRequest.

// https://github.com/netty/netty/issues/8575
@Test
public void testMultipartRequest() throws Exception {
    String BOUNDARY = "01f136d9282f";
    byte[] bodyBytes = ("--" + BOUNDARY + "\n" + "Content-Disposition: form-data; name=\"msg_id\"\n" + "\n" + "15200\n" + "--" + BOUNDARY + "\n" + "Content-Disposition: form-data; name=\"msg\"\n" + "\n" + "test message\n" + "--" + BOUNDARY + "--").getBytes();
    ByteBuf byteBuf = Unpooled.directBuffer(bodyBytes.length);
    byteBuf.writeBytes(bodyBytes);
    FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.POST, "/up", byteBuf);
    req.headers().add(HttpHeaderNames.CONTENT_TYPE, "multipart/form-data; boundary=" + BOUNDARY);
    HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE), req, CharsetUtil.UTF_8);
    assertTrue(decoder.isMultipart());
    assertFalse(decoder.getBodyHttpDatas().isEmpty());
    assertEquals(2, decoder.getBodyHttpDatas().size());
    Attribute attrMsg = (Attribute) decoder.getBodyHttpData("msg");
    assertTrue(attrMsg.getByteBuf().isDirect());
    assertEquals("test message", attrMsg.getValue());
    Attribute attrMsgId = (Attribute) decoder.getBodyHttpData("msg_id");
    assertTrue(attrMsgId.getByteBuf().isDirect());
    assertEquals("15200", attrMsgId.getValue());
    decoder.destroy();
    assertTrue(req.release());
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)215 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)117 Test (org.junit.jupiter.api.Test)72 Test (org.junit.Test)61 ByteBuf (io.netty.buffer.ByteBuf)56 HttpRequest (io.netty.handler.codec.http.HttpRequest)47 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)43 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)40 AsciiString (io.netty.util.AsciiString)30 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)23 Channel (io.netty.channel.Channel)20 ChannelPromise (io.netty.channel.ChannelPromise)19 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)18 IOException (java.io.IOException)17 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)15 HttpResponse (io.netty.handler.codec.http.HttpResponse)15 URI (java.net.URI)15 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)14 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)13 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)12