Search in sources :

Example 51 with DefaultFullHttpRequest

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

the class HttpProxyHandler method newInitialMessage.

@Override
protected Object newInitialMessage(ChannelHandlerContext ctx) throws Exception {
    InetSocketAddress raddr = destinationAddress();
    final String host = NetUtil.toSocketAddressString(raddr);
    FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.CONNECT, host, Unpooled.EMPTY_BUFFER, false);
    req.headers().set(HttpHeaderNames.HOST, host);
    if (authorization != null) {
        req.headers().set(HttpHeaderNames.PROXY_AUTHORIZATION, authorization);
    }
    return req;
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) InetSocketAddress(java.net.InetSocketAddress) AsciiString(io.netty.util.AsciiString)

Example 52 with DefaultFullHttpRequest

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

the class HttpPostRequestDecoderTest method testFilenameContainingSemicolon2.

@Test
public void testFilenameContainingSemicolon2() 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\r\n" + "\r\n" + data + "\r\n" + "--" + boundary + "--\r\n";
    req.content().writeBytes(body.getBytes(CharsetUtil.UTF_8.name()));
    // Create decoder instance to test.
    final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(inMemoryFactory, req);
    assertFalse(decoder.getBodyHttpDatas().isEmpty());
    InterfaceHttpData part1 = decoder.getBodyHttpDatas().get(0);
    assertTrue(part1 instanceof FileUpload);
    FileUpload fileUpload = (FileUpload) part1;
    assertEquals("tmp 0.txt", fileUpload.getFilename());
    decoder.destroy();
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) Test(org.junit.Test)

Example 53 with DefaultFullHttpRequest

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

the class HttpPostRequestDecoderTest method testQuotedBoundary.

// See https://github.com/netty/netty/issues/2542
@Test
public void testQuotedBoundary() throws Exception {
    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" + "Content-Type: image/gif\r\n" + "\r\n" + data + "\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());
    decoder.destroy();
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) Test(org.junit.Test)

Example 54 with DefaultFullHttpRequest

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

the class HttpPostRequestDecoderTest method testFilenameContainingSemicolon.

// See https://github.com/netty/netty/issues/3326
@Test
public void testFilenameContainingSemicolon() 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\r\n" + "\r\n" + data + "\r\n" + "--" + boundary + "--\r\n";
    req.content().writeBytes(body.getBytes(CharsetUtil.UTF_8.name()));
    // Create decoder instance to test.
    final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(inMemoryFactory, req);
    assertFalse(decoder.getBodyHttpDatas().isEmpty());
    decoder.destroy();
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) Test(org.junit.Test)

Example 55 with DefaultFullHttpRequest

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

the class HttpPostRequestDecoderTest method testFullHttpRequestUpload.

// See https://github.com/netty/netty/issues/1089
@Test
public void testFullHttpRequestUpload() throws Exception {
    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" + "Content-Type: image/gif\r\n" + "\r\n" + data + "\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());
    decoder.destroy();
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) Test(org.junit.Test)

Aggregations

DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)92 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)53 Test (org.junit.Test)53 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)31 AsciiString (io.netty.util.AsciiString)28 ByteBuf (io.netty.buffer.ByteBuf)27 HttpRequest (io.netty.handler.codec.http.HttpRequest)22 ChannelPromise (io.netty.channel.ChannelPromise)15 FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)11 Http2CodecUtil.getEmbeddedHttp2Exception (io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception)11 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)11 URI (java.net.URI)9 Channel (io.netty.channel.Channel)8 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)8 Bootstrap (io.netty.bootstrap.Bootstrap)7 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)7 File (java.io.File)7 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)6 Map (java.util.Map)6 NullDispatcher (org.elasticsearch.http.NullDispatcher)6