Search in sources :

Example 46 with ByteBufferPool

use of org.eclipse.jetty.io.ByteBufferPool in project jetty.project by eclipse.

the class HTTP2ServerTest method testNonISOHeader.

@Test
public void testNonISOHeader() throws Exception {
    try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
        startServer(new HttpServlet() {

            @Override
            protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                // Invalid header name, the connection must be closed.
                response.setHeader("Euro_(€)", "42");
            }
        });
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.control(lease, new PrefaceFrame());
        generator.control(lease, new SettingsFrame(new HashMap<>(), false));
        MetaData.Request metaData = newRequest("GET", new HttpFields());
        generator.control(lease, new HeadersFrame(1, metaData, null, true));
        try (Socket client = new Socket("localhost", connector.getLocalPort())) {
            OutputStream output = client.getOutputStream();
            for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
            output.flush();
            Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter(), 4096, 8192);
            boolean closed = parseResponse(client, parser);
            Assert.assertTrue(closed);
        }
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) HttpServlet(javax.servlet.http.HttpServlet) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Socket(java.net.Socket) Test(org.junit.Test)

Example 47 with ByteBufferPool

use of org.eclipse.jetty.io.ByteBufferPool in project jetty.project by eclipse.

the class HTTP2ServerTest method testNoPrefaceBytes.

@Test
public void testNoPrefaceBytes() throws Exception {
    startServer(new HttpServlet() {
    });
    // No preface bytes.
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new HeadersFrame(1, metaData, null, true));
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            output.write(BufferUtil.toArray(buffer));
        }
        final CountDownLatch latch = new CountDownLatch(1);
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onGoAway(GoAwayFrame frame) {
                latch.countDown();
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HttpServlet(javax.servlet.http.HttpServlet) OutputStream(java.io.OutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Socket(java.net.Socket) Test(org.junit.Test)

Example 48 with ByteBufferPool

use of org.eclipse.jetty.io.ByteBufferPool in project jetty.project by eclipse.

the class HTTP2ServerTest method testRequestWithContinuationFrames.

@Test
public void testRequestWithContinuationFrames() throws Exception {
    testRequestWithContinuationFrames(null, () -> {
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.control(lease, new PrefaceFrame());
        generator.control(lease, new SettingsFrame(new HashMap<>(), false));
        MetaData.Request metaData = newRequest("GET", new HttpFields());
        generator.control(lease, new HeadersFrame(1, metaData, null, true));
        return lease;
    });
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) HashMap(java.util.HashMap) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) Test(org.junit.Test)

Example 49 with ByteBufferPool

use of org.eclipse.jetty.io.ByteBufferPool in project jetty.project by eclipse.

the class HTTP2ServerTest method testRequestResponseContent.

@Test
public void testRequestResponseContent() throws Exception {
    final byte[] content = "Hello, world!".getBytes(StandardCharsets.UTF_8);
    final CountDownLatch latch = new CountDownLatch(4);
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            latch.countDown();
            resp.getOutputStream().write(content);
        }
    });
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    generator.control(lease, new HeadersFrame(1, metaData, null, true));
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            output.write(BufferUtil.toArray(buffer));
        }
        final AtomicReference<HeadersFrame> headersRef = new AtomicReference<>();
        final AtomicReference<DataFrame> dataRef = new AtomicReference<>();
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onSettings(SettingsFrame frame) {
                latch.countDown();
            }

            @Override
            public void onHeaders(HeadersFrame frame) {
                headersRef.set(frame);
                latch.countDown();
            }

            @Override
            public void onData(DataFrame frame) {
                dataRef.set(frame);
                latch.countDown();
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
        HeadersFrame response = headersRef.get();
        Assert.assertNotNull(response);
        MetaData.Response responseMetaData = (MetaData.Response) response.getMetaData();
        Assert.assertEquals(200, responseMetaData.getStatus());
        DataFrame responseData = dataRef.get();
        Assert.assertNotNull(responseData);
        Assert.assertArrayEquals(content, BufferUtil.toArray(responseData.getData()));
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) HttpServletResponse(javax.servlet.http.HttpServletResponse) Socket(java.net.Socket) Test(org.junit.Test)

Example 50 with ByteBufferPool

use of org.eclipse.jetty.io.ByteBufferPool in project jetty.project by eclipse.

the class HttpChannelOverHTTP2 method onRequestContent.

public Runnable onRequestContent(DataFrame frame, final Callback callback) {
    Stream stream = getStream();
    if (stream.isReset()) {
        // Consume previously queued content to
        // enlarge the session flow control window.
        consumeInput();
        // Consume immediately this content.
        callback.succeeded();
        return null;
    }
    // We must copy the data since we do not know when the
    // application will consume the bytes (we queue them by
    // calling onContent()), and the parsing will continue
    // as soon as this method returns, eventually leading
    // to reusing the underlying buffer for more reads.
    final ByteBufferPool byteBufferPool = getByteBufferPool();
    ByteBuffer original = frame.getData();
    int length = original.remaining();
    final ByteBuffer copy = byteBufferPool.acquire(length, original.isDirect());
    BufferUtil.clearToFill(copy);
    copy.put(original);
    BufferUtil.flipToFlush(copy, 0);
    boolean handle = onContent(new HttpInput.Content(copy) {

        @Override
        public InvocationType getInvocationType() {
            return callback.getInvocationType();
        }

        @Override
        public void succeeded() {
            byteBufferPool.release(copy);
            callback.succeeded();
        }

        @Override
        public void failed(Throwable x) {
            byteBufferPool.release(copy);
            callback.failed(x);
        }
    });
    boolean endStream = frame.isEndStream();
    if (endStream) {
        boolean handle_content = onContentComplete();
        boolean handle_request = onRequestComplete();
        handle |= handle_content | handle_request;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("HTTP2 Request #{}/{}: {} bytes of {} content, handle: {}", stream.getId(), Integer.toHexString(stream.getSession().hashCode()), length, endStream ? "last" : "some", handle);
    }
    boolean wasDelayed = _delayedUntilContent;
    _delayedUntilContent = false;
    if (wasDelayed)
        _handled = true;
    return handle || wasDelayed ? this : null;
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) HttpInput(org.eclipse.jetty.server.HttpInput) Stream(org.eclipse.jetty.http2.api.Stream) IStream(org.eclipse.jetty.http2.IStream) ByteBuffer(java.nio.ByteBuffer) EndPoint(org.eclipse.jetty.io.EndPoint)

Aggregations

ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)72 ByteBuffer (java.nio.ByteBuffer)53 Test (org.junit.Test)51 MappedByteBufferPool (org.eclipse.jetty.io.MappedByteBufferPool)40 Parser (org.eclipse.jetty.http2.parser.Parser)37 HttpFields (org.eclipse.jetty.http.HttpFields)29 MetaData (org.eclipse.jetty.http.MetaData)24 ArrayList (java.util.ArrayList)22 HashMap (java.util.HashMap)21 HeaderGenerator (org.eclipse.jetty.http2.generator.HeaderGenerator)21 PrefaceFrame (org.eclipse.jetty.http2.frames.PrefaceFrame)20 SettingsFrame (org.eclipse.jetty.http2.frames.SettingsFrame)20 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)19 OutputStream (java.io.OutputStream)16 Socket (java.net.Socket)16 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)14 IOException (java.io.IOException)13 CountDownLatch (java.util.concurrent.CountDownLatch)13 EndPoint (org.eclipse.jetty.io.EndPoint)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8