Search in sources :

Example 96 with DefaultHttpRequest

use of io.netty.handler.codec.http.DefaultHttpRequest in project graylog2-server by Graylog2.

the class HttpHandlerTest method successfullyProcessOPTIONSRequestWithoutOrigin.

@Test
public void successfullyProcessOPTIONSRequestWithoutOrigin() {
    final HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.OPTIONS, "/gelf");
    httpRequest.headers().add(HttpHeaderNames.HOST, "localhost");
    httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
    channel.writeInbound(httpRequest);
    channel.finish();
    final HttpResponse httpResponse = channel.readOutbound();
    assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.OK);
    final HttpHeaders headers = httpResponse.headers();
    assertThat(headers.get(HttpHeaderNames.CONTENT_LENGTH)).isEqualTo("0");
    assertThat(headers.contains(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN)).isFalse();
    assertThat(headers.contains(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS)).isFalse();
    assertThat(headers.contains(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS)).isFalse();
}
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) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Example 97 with DefaultHttpRequest

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

the class RtspEncoderTest method testSendSetupRequest.

/**
 * Test of a SETUP request, with no body.
 */
@Test
public void testSendSetupRequest() {
    String expected = "SETUP rtsp://172.10.20.30:554/d3abaaa7-65f2-42b4-" + "8d6b-379f492fcf0f RTSP/1.0\r\n" + "transport: MP2T/DVBC/UDP;unicast;client=01234567;" + "source=172.10.20.30;" + "destination=1.1.1.1;client_port=6922\r\n" + "cseq: 1\r\n" + "\r\n";
    HttpRequest request = new DefaultHttpRequest(RtspVersions.RTSP_1_0, RtspMethods.SETUP, "rtsp://172.10.20.30:554/d3abaaa7-65f2-42b4-8d6b-379f492fcf0f");
    request.headers().add(RtspHeaderNames.TRANSPORT, "MP2T/DVBC/UDP;unicast;client=01234567;source=172.10.20.30;" + "destination=1.1.1.1;client_port=6922");
    request.headers().add(RtspHeaderNames.CSEQ, "1");
    EmbeddedChannel ch = new EmbeddedChannel(new RtspEncoder());
    ch.writeOutbound(request);
    ByteBuf buf = ch.readOutbound();
    String actual = buf.toString(CharsetUtil.UTF_8);
    buf.release();
    assertEquals(expected, actual);
}
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) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 98 with DefaultHttpRequest

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

the class HttpPostMultipartRequestDecoderBenchmark method testHighNumberChunks.

public double testHighNumberChunks(boolean big, boolean noDisk) {
    String BOUNDARY = "01f136d9282f";
    int size = 8 * 1024;
    int chunkNumber = 64;
    StringBuilder stringBuilder = new StringBuilder(size);
    stringBuilder.setLength(size);
    String data = stringBuilder.toString();
    byte[] bodyStartBytes = ("--" + BOUNDARY + "\n" + "Content-Disposition: form-data; name=\"msg_id\"\n\n15200\n--" + BOUNDARY + "\nContent-Disposition: form-data; name=\"msg1\"; filename=\"file1.txt\"\n\n" + data).getBytes(CharsetUtil.UTF_8);
    byte[] bodyPartBigBytes = data.getBytes(CharsetUtil.UTF_8);
    byte[] intermediaryBytes = ("\n--" + BOUNDARY + "\nContent-Disposition: form-data; name=\"msg2\"; filename=\"file2.txt\"\n\n" + data).getBytes(CharsetUtil.UTF_8);
    byte[] finalBigBytes = ("\n" + "--" + BOUNDARY + "--\n").getBytes(CharsetUtil.UTF_8);
    ByteBuf firstBuf = Unpooled.wrappedBuffer(bodyStartBytes);
    ByteBuf finalBuf = Unpooled.wrappedBuffer(finalBigBytes);
    ByteBuf nextBuf;
    if (big) {
        nextBuf = Unpooled.wrappedBuffer(bodyPartBigBytes);
    } else {
        nextBuf = Unpooled.wrappedBuffer(intermediaryBytes);
    }
    DefaultHttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.POST, "/up");
    req.headers().add(HttpHeaderNames.CONTENT_TYPE, "multipart/form-data; boundary=" + BOUNDARY);
    long start = System.nanoTime();
    DefaultHttpDataFactory defaultHttpDataFactory = new DefaultHttpDataFactory(noDisk ? 1024 * 1024 : 16 * 1024);
    HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(defaultHttpDataFactory, req);
    firstBuf.retain();
    decoder.offer(new DefaultHttpContent(firstBuf));
    firstBuf.release();
    for (int i = 1; i < chunkNumber; i++) {
        nextBuf.retain();
        decoder.offer(new DefaultHttpContent(nextBuf));
        nextBuf.release();
        nextBuf.readerIndex(0);
    }
    finalBuf.retain();
    decoder.offer(new DefaultLastHttpContent(finalBuf));
    finalBuf.release();
    while (decoder.hasNext()) {
        InterfaceHttpData httpData = decoder.next();
    }
    while (finalBuf.refCnt() > 0) {
        finalBuf.release();
    }
    while (nextBuf.refCnt() > 0) {
        nextBuf.release();
    }
    while (finalBuf.refCnt() > 0) {
        finalBuf.release();
    }
    long stop = System.nanoTime();
    double time = (stop - start) / 1000000.0;
    defaultHttpDataFactory.cleanAllHttpData();
    defaultHttpDataFactory.cleanRequestHttpData(req);
    decoder.destroy();
    return time;
}
Also used : DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteBuf(io.netty.buffer.ByteBuf) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest)

Example 99 with DefaultHttpRequest

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

the class Http2StreamFrameToHttpObjectCodecTest method testEncodeRequestHeaders.

@Test
public void testEncodeRequestHeaders() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/hello/world");
    assertTrue(ch.writeOutbound(request));
    Http2HeadersFrame headersFrame = ch.readOutbound();
    Http2Headers headers = headersFrame.headers();
    assertThat(headers.scheme().toString(), is("http"));
    assertThat(headers.method().toString(), is("GET"));
    assertThat(headers.path().toString(), is("/hello/world"));
    assertFalse(headersFrame.isEndStream());
    assertThat(ch.readOutbound(), is(nullValue()));
    assertFalse(ch.finish());
}
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) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.jupiter.api.Test)

Example 100 with DefaultHttpRequest

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

the class HttpUploadClient method formpostmultipart.

/**
 * Multipart example
 */
private static void formpostmultipart(Bootstrap bootstrap, String host, int port, URI uriFile, HttpDataFactory factory, Iterable<Entry<String, String>> headers, List<InterfaceHttpData> bodylist) throws Exception {
    // XXX /formpostmultipart
    // Start the connection attempt.
    ChannelFuture future = bootstrap.connect(SocketUtils.socketAddress(host, port));
    // Wait until the connection attempt succeeds or fails.
    Channel channel = future.sync().channel();
    // Prepare the HTTP request.
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uriFile.toASCIIString());
    // Use the PostBody encoder
    HttpPostRequestEncoder bodyRequestEncoder = // true => multipart
    new HttpPostRequestEncoder(factory, request, true);
    // it is legal to add directly header or cookie into the request until finalize
    for (Entry<String, String> entry : headers) {
        request.headers().set(entry.getKey(), entry.getValue());
    }
    // add Form attribute from previous request in formpost()
    bodyRequestEncoder.setBodyHttpDatas(bodylist);
    // finalize request
    bodyRequestEncoder.finalizeRequest();
    // send request
    channel.write(request);
    // test if request was chunked and if so, finish the write
    if (bodyRequestEncoder.isChunked()) {
        channel.write(bodyRequestEncoder);
    }
    channel.flush();
    // Now no more use of file representation (and list of HttpData)
    bodyRequestEncoder.cleanFiles();
    // Wait for the server to close the connection.
    channel.closeFuture().sync();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)

Aggregations

DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)115 HttpRequest (io.netty.handler.codec.http.HttpRequest)81 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)53 Test (org.junit.Test)51 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)44 HttpMethod (io.netty.handler.codec.http.HttpMethod)23 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)20 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 ArrayList (java.util.ArrayList)9 Nettys4Test (org.jocean.http.util.Nettys4Test)9 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)8 DisposableWrapper (org.jocean.idiom.DisposableWrapper)6