Search in sources :

Example 66 with ByteBufferPool

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

the class HttpConnectionOverFCGI method releaseBuffer.

private void releaseBuffer(ByteBuffer buffer) {
    assert this.buffer == buffer;
    HttpClient client = destination.getHttpClient();
    ByteBufferPool bufferPool = client.getByteBufferPool();
    bufferPool.release(buffer);
    this.buffer = null;
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) HttpClient(org.eclipse.jetty.client.HttpClient)

Example 67 with ByteBufferPool

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

the class ClientGeneratorTest method testGenerateRequestHeaders.

@Test
public void testGenerateRequestHeaders() throws Exception {
    HttpFields fields = new HttpFields();
    // Short name, short value
    final String shortShortName = "REQUEST_METHOD";
    final String shortShortValue = "GET";
    fields.put(new HttpField(shortShortName, shortShortValue));
    // Short name, long value
    final String shortLongName = "REQUEST_URI";
    // Be sure it's longer than 127 chars to test the large value
    final String shortLongValue = "/api/0.6/map?bbox=-64.217736,-31.456810,-64.187736,-31.432322,filler=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    fields.put(new HttpField(shortLongName, shortLongValue));
    // Long name, short value
    // Be sure it's longer than 127 chars to test the large name
    final String longShortName = "FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210";
    final String longShortValue = "api.openstreetmap.org";
    fields.put(new HttpField(longShortName, longShortValue));
    // Long name, long value
    char[] chars = new char[ClientGenerator.MAX_PARAM_LENGTH];
    Arrays.fill(chars, 'z');
    final String longLongName = new String(chars);
    final String longLongValue = new String(chars);
    fields.put(new HttpField(longLongName, longLongValue));
    ByteBufferPool byteBufferPool = new MappedByteBufferPool();
    ClientGenerator generator = new ClientGenerator(byteBufferPool);
    final int id = 13;
    Generator.Result result = generator.generateRequestHeaders(id, fields, null);
    // Use the fundamental theorem of arithmetic to test the results.
    // This way we know onHeader() has been called the right number of
    // times with the right arguments, and so onHeaders().
    final int[] primes = new int[] { 2, 3, 5, 7, 11 };
    int value = 1;
    for (int prime : primes) value *= prime;
    final AtomicInteger params = new AtomicInteger(1);
    ServerParser parser = new ServerParser(new ServerParser.Listener.Adapter() {

        @Override
        public void onHeader(int request, HttpField field) {
            Assert.assertEquals(id, request);
            switch(field.getName()) {
                case shortShortName:
                    Assert.assertEquals(shortShortValue, field.getValue());
                    params.set(params.get() * primes[0]);
                    break;
                case shortLongName:
                    Assert.assertEquals(shortLongValue, field.getValue());
                    params.set(params.get() * primes[1]);
                    break;
                case longShortName:
                    Assert.assertEquals(longShortValue, field.getValue());
                    params.set(params.get() * primes[2]);
                    break;
                default:
                    Assert.assertEquals(longLongName, field.getName());
                    Assert.assertEquals(longLongValue, field.getValue());
                    params.set(params.get() * primes[3]);
                    break;
            }
        }

        @Override
        public void onHeaders(int request) {
            Assert.assertEquals(id, request);
            params.set(params.get() * primes[4]);
        }
    });
    for (ByteBuffer buffer : result.getByteBuffers()) {
        parser.parse(buffer);
        Assert.assertFalse(buffer.hasRemaining());
    }
    Assert.assertEquals(value, params.get());
    // Parse again byte by byte
    params.set(1);
    for (ByteBuffer buffer : result.getByteBuffers()) {
        buffer.flip();
        while (buffer.hasRemaining()) parser.parse(ByteBuffer.wrap(new byte[] { buffer.get() }));
        Assert.assertFalse(buffer.hasRemaining());
    }
    Assert.assertEquals(value, params.get());
}
Also used : MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ByteBuffer(java.nio.ByteBuffer) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpField(org.eclipse.jetty.http.HttpField) HttpFields(org.eclipse.jetty.http.HttpFields) ServerParser(org.eclipse.jetty.fcgi.parser.ServerParser) Test(org.junit.Test)

Example 68 with ByteBufferPool

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

the class ClientParserTest method testParseLargeResponseContent.

@Test
public void testParseLargeResponseContent() throws Exception {
    final int id = 13;
    HttpFields fields = new HttpFields();
    ByteBuffer content = ByteBuffer.wrap(new byte[128 * 1024]);
    final int contentLength = content.remaining();
    final int code = 200;
    final String contentTypeName = "Content-Length";
    final String contentTypeValue = String.valueOf(contentLength);
    fields.put(contentTypeName, contentTypeValue);
    ByteBufferPool byteBufferPool = new MappedByteBufferPool();
    ServerGenerator generator = new ServerGenerator(byteBufferPool);
    Generator.Result result1 = generator.generateResponseHeaders(id, code, "OK", fields, null);
    Generator.Result result2 = generator.generateResponseContent(id, content, true, false, null);
    final AtomicInteger totalLength = new AtomicInteger();
    final AtomicBoolean verifier = new AtomicBoolean();
    ClientParser parser = new ClientParser(new ClientParser.Listener.Adapter() {

        @Override
        public boolean onContent(int request, FCGI.StreamType stream, ByteBuffer buffer) {
            Assert.assertEquals(id, request);
            totalLength.addAndGet(buffer.remaining());
            return false;
        }

        @Override
        public void onEnd(int request) {
            Assert.assertEquals(id, request);
            Assert.assertEquals(contentLength, totalLength.get());
            verifier.set(true);
        }
    });
    for (ByteBuffer buffer : result1.getByteBuffers()) {
        parser.parse(buffer);
        Assert.assertFalse(buffer.hasRemaining());
    }
    for (ByteBuffer buffer : result2.getByteBuffers()) {
        parser.parse(buffer);
        Assert.assertFalse(buffer.hasRemaining());
    }
    Assert.assertTrue(verifier.get());
}
Also used : MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerGenerator(org.eclipse.jetty.fcgi.generator.ServerGenerator) ByteBuffer(java.nio.ByteBuffer) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpFields(org.eclipse.jetty.http.HttpFields) Generator(org.eclipse.jetty.fcgi.generator.Generator) ServerGenerator(org.eclipse.jetty.fcgi.generator.ServerGenerator) FCGI(org.eclipse.jetty.fcgi.FCGI) Test(org.junit.Test)

Example 69 with ByteBufferPool

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

the class ClientParserTest method testParseNoResponseContent.

@Test
public void testParseNoResponseContent() throws Exception {
    final int id = 13;
    HttpFields fields = new HttpFields();
    fields.put("Content-Length", "0");
    ByteBufferPool byteBufferPool = new MappedByteBufferPool();
    ServerGenerator generator = new ServerGenerator(byteBufferPool);
    Generator.Result result1 = generator.generateResponseHeaders(id, 200, "OK", fields, null);
    Generator.Result result2 = generator.generateResponseContent(id, null, true, false, null);
    final AtomicInteger verifier = new AtomicInteger();
    ClientParser parser = new ClientParser(new ClientParser.Listener.Adapter() {

        @Override
        public boolean onContent(int request, FCGI.StreamType stream, ByteBuffer buffer) {
            Assert.assertEquals(id, request);
            verifier.addAndGet(2);
            return false;
        }

        @Override
        public void onEnd(int request) {
            Assert.assertEquals(id, request);
            verifier.addAndGet(3);
        }
    });
    for (ByteBuffer buffer : result1.getByteBuffers()) {
        parser.parse(buffer);
        Assert.assertFalse(buffer.hasRemaining());
    }
    for (ByteBuffer buffer : result2.getByteBuffers()) {
        parser.parse(buffer);
        Assert.assertFalse(buffer.hasRemaining());
    }
    Assert.assertEquals(3, verifier.get());
}
Also used : MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerGenerator(org.eclipse.jetty.fcgi.generator.ServerGenerator) ByteBuffer(java.nio.ByteBuffer) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpFields(org.eclipse.jetty.http.HttpFields) Generator(org.eclipse.jetty.fcgi.generator.Generator) ServerGenerator(org.eclipse.jetty.fcgi.generator.ServerGenerator) FCGI(org.eclipse.jetty.fcgi.FCGI) Test(org.junit.Test)

Example 70 with ByteBufferPool

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

the class ServerReadThread method run.

@Override
public void run() {
    ByteBufferPool bufferPool = conn.getBufferPool();
    ByteBuffer buf = bufferPool.acquire(BUFFER_SIZE, false);
    BufferUtil.clearToFill(buf);
    try {
        while (active) {
            BufferUtil.clearToFill(buf);
            int len = conn.read(buf);
            if (len > 0) {
                LOG.debug("Read {} bytes", len);
                BufferUtil.flipToFlush(buf, 0);
                conn.getParser().parse(buf);
            }
            Queue<WebSocketFrame> frames = conn.getIncomingFrames().getFrames();
            WebSocketFrame frame;
            while ((frame = frames.poll()) != null) {
                frameCount.incrementAndGet();
                if (frame.getOpCode() == OpCode.CLOSE) {
                    active = false;
                    // automatically response to close frame
                    CloseInfo close = new CloseInfo(frame);
                    conn.close(close.getStatusCode());
                }
                expectedMessageCount.countDown();
            }
            if (slowness > 0) {
                TimeUnit.MILLISECONDS.sleep(getSlowness());
            }
        }
    } catch (IOException | InterruptedException e) {
        LOG.warn(e);
    } finally {
        bufferPool.release(buf);
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo)

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