Search in sources :

Example 56 with DefaultFullHttpRequest

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

the class HttpPostRequestDecoderTest method testDecodeMalformedNotEncodedContentDispositionFieldParameters.

// https://github.com/netty/netty/pull/7265
@Test
public void testDecodeMalformedNotEncodedContentDispositionFieldParameters() throws Exception {
    final String boundary = "74e78d11b0214bdcbc2f86491eeb4902";
    final String body = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"file\"; filename*=not-encoded\r\n" + "\r\n" + "foo\r\n" + "\r\n" + "--" + boundary + "--";
    final DefaultFullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "http://localhost", Unpooled.wrappedBuffer(body.getBytes()));
    req.headers().add(HttpHeaderNames.CONTENT_TYPE, "multipart/form-data; boundary=" + boundary);
    final DefaultHttpDataFactory inMemoryFactory = new DefaultHttpDataFactory(false);
    try {
        new HttpPostRequestDecoder(inMemoryFactory, req);
        fail("Was expecting an ErrorDataDecoderException");
    } catch (HttpPostRequestDecoder.ErrorDataDecoderException e) {
        assertTrue(e.getCause() instanceof ArrayIndexOutOfBoundsException);
    } finally {
        assertTrue(req.release());
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) Test(org.junit.jupiter.api.Test)

Example 57 with DefaultFullHttpRequest

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

the class HttpPostRequestDecoderTest method testMultipartRequestWithoutContentTypeBody.

@Test
public void testMultipartRequestWithoutContentTypeBody() {
    final String boundary = "dLV9Wyq26L_-JQxk6ferf-RT153LhOO";
    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);
    // Force to use memory-based data.
    final DefaultHttpDataFactory inMemoryFactory = new DefaultHttpDataFactory(false);
    for (String data : Arrays.asList("", "\r", "\r\r", "\r\r\r")) {
        final String body = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"tmp-0.txt\"\r\n" + "\r\n" + data + "\r\n" + "--" + boundary + "--\r\n";
        req.content().writeBytes(body.getBytes(CharsetUtil.UTF_8));
    }
    // Create decoder instance to test without any exception.
    final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(inMemoryFactory, req);
    assertFalse(decoder.getBodyHttpDatas().isEmpty());
    decoder.destroy();
    assertTrue(req.release());
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) Test(org.junit.jupiter.api.Test)

Example 58 with DefaultFullHttpRequest

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

the class HttpPostRequestDecoderTest method testDecodeFullHttpRequestWithUrlEncodedBodyWithInvalidHexNibbleHi.

@Test
public void testDecodeFullHttpRequestWithUrlEncodedBodyWithInvalidHexNibbleHi() {
    byte[] bodyBytes = "foo=bar&a=b&empty=%Zc&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: '%Zc'", 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 59 with DefaultFullHttpRequest

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

the class HttpPostRequestDecoderTest method testDecodeMultipartRequest.

@Test
public void testDecodeMultipartRequest() {
    byte[] bodyBytes = ("--be38b42a9ad2713f\n" + "content-disposition: form-data; name=\"title\"\n" + "content-length: 10\n" + "content-type: text/plain; charset=UTF-8\n" + "\n" + "bar-stream\n" + "--be38b42a9ad2713f\n" + "content-disposition: form-data; name=\"data\"; filename=\"data.json\"\n" + "content-length: 16\n" + "content-type: application/json; charset=UTF-8\n" + "\n" + "{\"title\":\"Test\"}\n" + "--be38b42a9ad2713f--").getBytes();
    ByteBuf content = Unpooled.directBuffer(bodyBytes.length);
    content.writeBytes(bodyBytes);
    FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", content);
    req.headers().add("Content-Type", "multipart/form-data;boundary=be38b42a9ad2713f");
    try {
        HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), req);
        assertEquals(2, decoder.getBodyHttpDatas().size());
        InterfaceHttpData data = decoder.getBodyHttpData("title");
        assertTrue(data instanceof MemoryAttribute);
        assertEquals("bar-stream", ((MemoryAttribute) data).getString());
        assertTrue(data.release());
        data = decoder.getBodyHttpData("data");
        assertTrue(data instanceof MemoryFileUpload);
        assertEquals("{\"title\":\"Test\"}", ((MemoryFileUpload) data).getString());
        assertTrue(data.release());
        decoder.destroy();
    } catch (HttpPostRequestDecoder.ErrorDataDecoderException e) {
        fail("Was not expecting an exception");
    } 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 60 with DefaultFullHttpRequest

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

the class HttpPostRequestDecoderTest method testDecodeFullHttpRequestWithUrlEncodedBody.

@Test
public void testDecodeFullHttpRequestWithUrlEncodedBody() throws Exception {
    byte[] bodyBytes = "foo=bar&a=b&empty=&city=%3c%22new%22%20york%20city%3e&other_city=los+angeles".getBytes();
    ByteBuf content = Unpooled.directBuffer(bodyBytes.length);
    content.writeBytes(bodyBytes);
    FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", content);
    HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(req);
    assertFalse(decoder.getBodyHttpDatas().isEmpty());
    assertFalse(decoder.getBodyHttpDatas().isEmpty());
    assertEquals(5, decoder.getBodyHttpDatas().size());
    Attribute attr = (Attribute) decoder.getBodyHttpData("foo");
    assertTrue(attr.getByteBuf().isDirect());
    assertEquals("bar", attr.getValue());
    attr = (Attribute) decoder.getBodyHttpData("a");
    assertTrue(attr.getByteBuf().isDirect());
    assertEquals("b", attr.getValue());
    attr = (Attribute) decoder.getBodyHttpData("empty");
    assertTrue(attr.getByteBuf().isDirect());
    assertEquals("", attr.getValue());
    attr = (Attribute) decoder.getBodyHttpData("city");
    assertTrue(attr.getByteBuf().isDirect());
    assertEquals("<\"new\" york city>", attr.getValue());
    attr = (Attribute) decoder.getBodyHttpData("other_city");
    assertTrue(attr.getByteBuf().isDirect());
    assertEquals("los angeles", attr.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