Search in sources :

Example 1 with Http1xServerConnection

use of io.vertx.core.http.impl.Http1xServerConnection in project vert.x by eclipse.

the class HttpServerHandlerBenchmark method setup.

@Setup
public void setup() {
    vertx = (VertxInternal) Vertx.vertx();
    HttpServerOptions options = new HttpServerOptions();
    vertxChannel = new EmbeddedChannel(new VertxHttpRequestDecoder(options), // We don't use the VertxHttpResponseDecoder because it will use the PartialPooledByteBufAllocator
    new HttpResponseEncoder() {

        @Override
        protected void encodeHeaders(HttpHeaders headers, ByteBuf buf) {
            ((HeadersMultiMap) headers).encode(buf);
        }
    });
    vertxChannel.config().setAllocator(new Alloc());
    ContextInternal context = vertx.createEventLoopContext(vertxChannel.eventLoop(), null, Thread.currentThread().getContextClassLoader());
    Handler<HttpServerRequest> app = request -> {
        HttpServerResponse response = request.response();
        MultiMap headers = response.headers();
        headers.add(HEADER_CONTENT_TYPE, RESPONSE_TYPE_PLAIN).add(HEADER_SERVER, SERVER).add(HEADER_DATE, DATE_STRING).add(HEADER_CONTENT_LENGTH, HELLO_WORLD_LENGTH);
        response.end(HELLO_WORLD_BUFFER);
    };
    VertxHandler<Http1xServerConnection> handler = VertxHandler.create(chctx -> {
        Http1xServerConnection conn = new Http1xServerConnection(() -> context, null, new HttpServerOptions(), chctx, context, "localhost", null);
        conn.handler(app);
        return conn;
    });
    vertxChannel.pipeline().addLast("handler", handler);
    nettyChannel = new EmbeddedChannel(new HttpRequestDecoder(options.getMaxInitialLineLength(), options.getMaxHeaderSize(), options.getMaxChunkSize(), false, options.getDecoderInitialBufferSize()), new HttpResponseEncoder(), new SimpleChannelInboundHandler<HttpRequest>() {

        private final byte[] STATIC_PLAINTEXT = "Hello, World!".getBytes(CharsetUtil.UTF_8);

        private final int STATIC_PLAINTEXT_LEN = STATIC_PLAINTEXT.length;

        private final ByteBuf PLAINTEXT_CONTENT_BUFFER = Unpooled.unreleasableBuffer(Unpooled.directBuffer().writeBytes(STATIC_PLAINTEXT));

        private final CharSequence PLAINTEXT_CLHEADER_VALUE = new AsciiString(String.valueOf(STATIC_PLAINTEXT_LEN));

        private final CharSequence TYPE_PLAIN = new AsciiString("text/plain");

        private final CharSequence SERVER_NAME = new AsciiString("Netty");

        private final CharSequence CONTENT_TYPE_ENTITY = HttpHeaderNames.CONTENT_TYPE;

        private final CharSequence DATE_ENTITY = HttpHeaderNames.DATE;

        private final CharSequence CONTENT_LENGTH_ENTITY = HttpHeaderNames.CONTENT_LENGTH;

        private final CharSequence SERVER_ENTITY = HttpHeaderNames.SERVER;

        private final DateFormat FORMAT = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");

        private final CharSequence date = new AsciiString(FORMAT.format(new Date()));

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
            writeResponse(ctx, msg, PLAINTEXT_CONTENT_BUFFER.duplicate(), TYPE_PLAIN, PLAINTEXT_CLHEADER_VALUE);
        }

        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            ctx.flush();
        }

        private void writeResponse(ChannelHandlerContext ctx, HttpRequest request, ByteBuf buf, CharSequence contentType, CharSequence contentLength) {
            // Build the response object.
            FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf, false);
            HttpHeaders headers = response.headers();
            headers.set(CONTENT_TYPE_ENTITY, contentType);
            headers.set(SERVER_ENTITY, SERVER_NAME);
            headers.set(DATE_ENTITY, date);
            headers.set(CONTENT_LENGTH_ENTITY, contentLength);
            // Close the non-keep-alive connection after the write operation is done.
            ctx.write(response, ctx.voidPromise());
        }
    });
    nettyChannel.config().setAllocator(new Alloc());
    GET = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer(("GET / HTTP/1.1\r\n" + "\r\n").getBytes()));
    readerIndex = GET.readerIndex();
    writeIndex = GET.writerIndex();
}
Also used : HttpVersion(io.netty.handler.codec.http.HttpVersion) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Date(java.util.Date) MultiMap(io.vertx.core.MultiMap) AsciiString(io.netty.util.AsciiString) ContextInternal(io.vertx.core.impl.ContextInternal) SimpleDateFormat(java.text.SimpleDateFormat) Scope(org.openjdk.jmh.annotations.Scope) Unpooled(io.netty.buffer.Unpooled) HeadersMultiMap(io.vertx.core.http.impl.headers.HeadersMultiMap) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) CharsetUtil(io.netty.util.CharsetUtil) DateFormat(java.text.DateFormat) VertxHandler(io.vertx.core.net.impl.VertxHandler) Setup(org.openjdk.jmh.annotations.Setup) HttpRequest(io.netty.handler.codec.http.HttpRequest) VertxInternal(io.vertx.core.impl.VertxInternal) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Vertx(io.vertx.core.Vertx) VertxHttpRequestDecoder(io.vertx.core.http.impl.VertxHttpRequestDecoder) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Http1xServerConnection(io.vertx.core.http.impl.Http1xServerConnection) State(org.openjdk.jmh.annotations.State) Benchmark(org.openjdk.jmh.annotations.Benchmark) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Buffer(io.vertx.core.buffer.Buffer) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) HttpServerResponse(io.vertx.core.http.HttpServerResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Fork(org.openjdk.jmh.annotations.Fork) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) Handler(io.vertx.core.Handler) CompilerControl(org.openjdk.jmh.annotations.CompilerControl) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) HttpServerOptions(io.vertx.core.http.HttpServerOptions) ContextInternal(io.vertx.core.impl.ContextInternal) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) Http1xServerConnection(io.vertx.core.http.impl.Http1xServerConnection) MultiMap(io.vertx.core.MultiMap) HeadersMultiMap(io.vertx.core.http.impl.headers.HeadersMultiMap) HeadersMultiMap(io.vertx.core.http.impl.headers.HeadersMultiMap) VertxHttpRequestDecoder(io.vertx.core.http.impl.VertxHttpRequestDecoder) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) HttpServerResponse(io.vertx.core.http.HttpServerResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) VertxHttpRequestDecoder(io.vertx.core.http.impl.VertxHttpRequestDecoder) HttpServerRequest(io.vertx.core.http.HttpServerRequest) AsciiString(io.netty.util.AsciiString) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Date(java.util.Date) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Setup(org.openjdk.jmh.annotations.Setup)

Example 2 with Http1xServerConnection

use of io.vertx.core.http.impl.Http1xServerConnection in project vert.x by eclipse.

the class Http1xTest method testTLSDisablesH2CHandlers.

@Test
public void testTLSDisablesH2CHandlers() throws Exception {
    server.close();
    server = vertx.createHttpServer(createBaseServerOptions().setSsl(true).setKeyCertOptions(Cert.SERVER_JKS.get())).connectionHandler(conn -> {
        Channel channel = ((Http1xServerConnection) conn).channel();
        for (Map.Entry<String, ChannelHandler> stringChannelHandlerEntry : channel.pipeline()) {
            ChannelHandler handler = stringChannelHandlerEntry.getValue();
            assertFalse(handler instanceof Http1xUpgradeToH2CHandler);
            assertFalse(handler instanceof Http1xOrH2CHandler);
        }
    }).requestHandler(req -> {
        req.response().end();
    });
    startServer(testAddress);
    client.close();
    client = vertx.createHttpClient(new HttpClientOptions().setTrustAll(true).setSsl(true));
    client.request(requestOptions).compose(req -> req.send().compose(HttpClientResponse::body)).onComplete(onSuccess(v -> {
        testComplete();
    }));
    await();
}
Also used : Http1xServerConnection(io.vertx.core.http.impl.Http1xServerConnection) HttpServerImpl(io.vertx.core.http.impl.HttpServerImpl) IntStream(java.util.stream.IntStream) java.util(java.util) io.vertx.core(io.vertx.core) ContextInternal(io.vertx.core.impl.ContextInternal) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Http1xOrH2CHandler(io.vertx.core.http.impl.Http1xOrH2CHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) SimpleServer(io.vertx.test.verticles.SimpleServer) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WriteStream(io.vertx.core.streams.WriteStream) BiConsumer(java.util.function.BiConsumer) RecordParser(io.vertx.core.parsetools.RecordParser) JsonObject(io.vertx.core.json.JsonObject) Assume(org.junit.Assume) CheckingSender(io.vertx.test.core.CheckingSender) VertxInternal(io.vertx.core.impl.VertxInternal) Http1xUpgradeToH2CHandler(io.vertx.core.http.impl.Http1xUpgradeToH2CHandler) ConcurrentHashSet(io.vertx.core.impl.ConcurrentHashSet) java.util.concurrent(java.util.concurrent) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) Test(org.junit.Test) Http1xServerConnection(io.vertx.core.http.impl.Http1xServerConnection) EventLoop(io.netty.channel.EventLoop) Future(io.vertx.core.Future) io.vertx.core.net(io.vertx.core.net) Collectors(java.util.stream.Collectors) File(java.io.File) LongConsumer(java.util.function.LongConsumer) Channel(io.netty.channel.Channel) Consumer(java.util.function.Consumer) EventLoopContext(io.vertx.core.impl.EventLoopContext) JsonArray(io.vertx.core.json.JsonArray) Repeat(io.vertx.test.core.Repeat) Stream(java.util.stream.Stream) Buffer(io.vertx.core.buffer.Buffer) HttpUtils(io.vertx.core.http.impl.HttpUtils) Cert(io.vertx.test.tls.Cert) ChannelHandler(io.netty.channel.ChannelHandler) PUT(io.vertx.core.http.HttpMethod.PUT) Http1xOrH2CHandler(io.vertx.core.http.impl.Http1xOrH2CHandler) Channel(io.netty.channel.Channel) Http1xUpgradeToH2CHandler(io.vertx.core.http.impl.Http1xUpgradeToH2CHandler) ChannelHandler(io.netty.channel.ChannelHandler) Test(org.junit.Test)

Aggregations

Buffer (io.vertx.core.buffer.Buffer)2 Http1xServerConnection (io.vertx.core.http.impl.Http1xServerConnection)2 ContextInternal (io.vertx.core.impl.ContextInternal)2 VertxInternal (io.vertx.core.impl.VertxInternal)2 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)1 Unpooled (io.netty.buffer.Unpooled)1 Channel (io.netty.channel.Channel)1 ChannelHandler (io.netty.channel.ChannelHandler)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 EventLoop (io.netty.channel.EventLoop)1 SimpleChannelInboundHandler (io.netty.channel.SimpleChannelInboundHandler)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)1 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1