Search in sources :

Example 36 with DefaultHttpRequest

use of io.netty.handler.codec.http.DefaultHttpRequest in project netty by netty.

the class WebSocketServerProtocolHandlerTest method writeUpgradeRequest.

private static void writeUpgradeRequest(EmbeddedChannel ch, boolean full) {
    HttpRequest request = WebSocketRequestBuilder.successful();
    if (full) {
        ch.writeInbound(request);
    } else {
        if (request instanceof FullHttpRequest) {
            FullHttpRequest fullHttpRequest = (FullHttpRequest) request;
            HttpRequest req = new DefaultHttpRequest(fullHttpRequest.protocolVersion(), fullHttpRequest.method(), fullHttpRequest.uri(), fullHttpRequest.headers().copy());
            ch.writeInbound(req);
            ch.writeInbound(new DefaultHttpContent(fullHttpRequest.content().copy()));
            ch.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
            fullHttpRequest.release();
        } else {
            ch.writeInbound(request);
        }
    }
}
Also used : DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent)

Example 37 with DefaultHttpRequest

use of io.netty.handler.codec.http.DefaultHttpRequest in project netty by netty.

the class HttpPostRequestDecoderTest method testMultipartFormDataContentType.

@Test
public void testMultipartFormDataContentType() {
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    assertFalse(HttpPostRequestDecoder.isMultipart(request));
    String multipartDataValue = HttpHeaderValues.MULTIPART_FORM_DATA + ";" + "boundary=gc0p4Jq0M2Yt08jU534c0p";
    request.headers().set(HttpHeaderNames.CONTENT_TYPE, ";" + multipartDataValue);
    assertFalse(HttpPostRequestDecoder.isMultipart(request));
    request.headers().set(HttpHeaderNames.CONTENT_TYPE, multipartDataValue);
    assertTrue(HttpPostRequestDecoder.isMultipart(request));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) Test(org.junit.jupiter.api.Test)

Example 38 with DefaultHttpRequest

use of io.netty.handler.codec.http.DefaultHttpRequest in project netty by netty.

the class HttpPostRequestDecoderTest method testNoZeroOut.

// See https://github.com/netty/netty/issues/1848
@Test
public void testNoZeroOut() throws Exception {
    final String boundary = "E832jQp_Rq2ErFmAduHSR8YlMSm0FCY";
    final DefaultHttpDataFactory aMemFactory = new DefaultHttpDataFactory(false);
    DefaultHttpRequest aRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "http://localhost");
    aRequest.headers().set(HttpHeaderNames.CONTENT_TYPE, "multipart/form-data; boundary=" + boundary);
    aRequest.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
    HttpPostRequestDecoder aDecoder = new HttpPostRequestDecoder(aMemFactory, aRequest);
    final String aData = "some data would be here. the data should be long enough that it " + "will be longer than the original buffer length of 256 bytes in " + "the HttpPostRequestDecoder in order to trigger the issue. Some more " + "data just to be on the safe side.";
    final String body = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"root\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + aData + "\r\n" + "--" + boundary + "--\r\n";
    byte[] aBytes = body.getBytes();
    int split = 125;
    ByteBufAllocator aAlloc = new UnpooledByteBufAllocator(true);
    ByteBuf aSmallBuf = aAlloc.heapBuffer(split, split);
    ByteBuf aLargeBuf = aAlloc.heapBuffer(aBytes.length - split, aBytes.length - split);
    aSmallBuf.writeBytes(aBytes, 0, split);
    aLargeBuf.writeBytes(aBytes, split, aBytes.length - split);
    aDecoder.offer(new DefaultHttpContent(aSmallBuf));
    aDecoder.offer(new DefaultHttpContent(aLargeBuf));
    aDecoder.offer(LastHttpContent.EMPTY_LAST_CONTENT);
    assertTrue(aDecoder.hasNext(), "Should have a piece of data");
    InterfaceHttpData aDecodedData = aDecoder.next();
    assertEquals(InterfaceHttpData.HttpDataType.Attribute, aDecodedData.getHttpDataType());
    Attribute aAttr = (Attribute) aDecodedData;
    assertEquals(aData, aAttr.getValue());
    aDecodedData.release();
    aDecoder.destroy();
    aSmallBuf.release();
    aLargeBuf.release();
}
Also used : UnpooledByteBufAllocator(io.netty.buffer.UnpooledByteBufAllocator) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) UnpooledByteBufAllocator(io.netty.buffer.UnpooledByteBufAllocator) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 39 with DefaultHttpRequest

use of io.netty.handler.codec.http.DefaultHttpRequest in project netty by netty.

the class HttpPostMultiPartRequestDecoderTest method commonNotBadReleaseBuffersDuringDecoding.

private static void commonNotBadReleaseBuffersDuringDecoding(HttpDataFactory factory, boolean inMemory) throws IOException {
    int nbItems = 20;
    int bytesPerItem = 1000;
    int maxMemory = 500;
    String prefix1 = "\n--861fbeab-cd20-470c-9609-d40a0f704466\n" + "Content-Disposition: form-data; name=\"image";
    String prefix2 = "\"; filename=\"guangzhou.jpeg\"\n" + "Content-Type: image/jpeg\n" + "Content-Length: " + bytesPerItem + "\n" + "\n";
    String suffix = "\n--861fbeab-cd20-470c-9609-d40a0f704466--\n";
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/upload");
    request.headers().set("content-type", "multipart/form-data; boundary=861fbeab-cd20-470c-9609-d40a0f704466");
    request.headers().set("content-length", nbItems * (prefix1.length() + prefix2.length() + 2 + bytesPerItem) + suffix.length());
    HttpPostMultipartRequestDecoder decoder = new HttpPostMultipartRequestDecoder(factory, request);
    decoder.setDiscardThreshold(maxMemory);
    for (int rank = 0; rank < nbItems; rank++) {
        byte[] bp1 = prefix1.getBytes(CharsetUtil.UTF_8);
        byte[] bp2 = prefix2.getBytes(CharsetUtil.UTF_8);
        byte[] prefix = new byte[bp1.length + 2 + bp2.length];
        for (int i = 0; i < bp1.length; i++) {
            prefix[i] = bp1[i];
        }
        byte[] brank = Integer.toString(10 + rank).getBytes(CharsetUtil.UTF_8);
        prefix[bp1.length] = brank[0];
        prefix[bp1.length + 1] = brank[1];
        for (int i = 0; i < bp2.length; i++) {
            prefix[bp1.length + 2 + i] = bp2[i];
        }
        ByteBuf buf = Unpooled.wrappedBuffer(prefix);
        DefaultHttpContent httpContent = new DefaultHttpContent(buf);
        decoder.offer(httpContent);
        httpContent.release();
        byte[] body = new byte[bytesPerItem];
        Arrays.fill(body, (byte) rank);
        ByteBuf content = Unpooled.wrappedBuffer(body, 0, bytesPerItem);
        httpContent = new DefaultHttpContent(content);
        decoder.offer(httpContent);
        httpContent.release();
    }
    byte[] lastbody = suffix.getBytes(CharsetUtil.UTF_8);
    ByteBuf content2 = Unpooled.wrappedBuffer(lastbody, 0, lastbody.length);
    DefaultHttpContent httpContent = new DefaultHttpContent(content2);
    decoder.offer(httpContent);
    httpContent.release();
    decoder.offer(new DefaultLastHttpContent());
    for (int rank = 0; rank < nbItems; rank++) {
        FileUpload data = (FileUpload) decoder.getBodyHttpData("image" + (10 + rank));
        assertEquals(bytesPerItem, data.length());
        assertEquals(inMemory, data.isInMemory());
        byte[] body = new byte[bytesPerItem];
        Arrays.fill(body, (byte) rank);
        assertTrue(Arrays.equals(body, data.get()));
    }
    // Not mandatory since implicitely called during destroy of decoder
    for (InterfaceHttpData httpData : decoder.getBodyHttpDatas()) {
        httpData.release();
        factory.removeHttpDataFromClean(request, httpData);
    }
    factory.cleanAllHttpData();
    decoder.destroy();
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteBuf(io.netty.buffer.ByteBuf)

Example 40 with DefaultHttpRequest

use of io.netty.handler.codec.http.DefaultHttpRequest in project netty by netty.

the class HttpPostRequestEncoderTest method testEncodeChunkedContent.

@Test
public void testEncodeChunkedContent() throws Exception {
    HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(req, false);
    int length = 8077 + 8096;
    char[] array = new char[length];
    Arrays.fill(array, 'a');
    String longText = new String(array);
    encoder.addBodyAttribute("data", longText);
    encoder.addBodyAttribute("moreData", "abcd");
    assertNotNull(encoder.finalizeRequest());
    while (!encoder.isEndOfInput()) {
        encoder.readChunk((ByteBufAllocator) null).release();
    }
    assertTrue(encoder.isEndOfInput());
    encoder.cleanFiles();
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)111 HttpRequest (io.netty.handler.codec.http.HttpRequest)79 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)52 Test (org.junit.Test)51 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)43 HttpMethod (io.netty.handler.codec.http.HttpMethod)23 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)18 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)18 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)16 HttpContent (io.netty.handler.codec.http.HttpContent)13 HttpObject (io.netty.handler.codec.http.HttpObject)13 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)12 ByteBuf (io.netty.buffer.ByteBuf)11 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)11 Test (org.junit.jupiter.api.Test)10 Channel (io.netty.channel.Channel)9 Nettys4Test (org.jocean.http.util.Nettys4Test)9 ArrayList (java.util.ArrayList)8 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)7 DisposableWrapper (org.jocean.idiom.DisposableWrapper)6