Search in sources :

Example 1 with ServerParser

use of org.eclipse.jetty.fcgi.parser.ServerParser in project jetty.project by eclipse.

the class ClientGeneratorTest method testGenerateRequestContent.

private void testGenerateRequestContent(final int contentLength) throws Exception {
    ByteBuffer content = ByteBuffer.allocate(contentLength);
    ByteBufferPool byteBufferPool = new MappedByteBufferPool();
    ClientGenerator generator = new ClientGenerator(byteBufferPool);
    final int id = 13;
    Generator.Result result = generator.generateRequestContent(id, content, true, null);
    final AtomicInteger totalLength = new AtomicInteger();
    ServerParser parser = new ServerParser(new ServerParser.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());
        }
    });
    for (ByteBuffer buffer : result.getByteBuffers()) {
        parser.parse(buffer);
        Assert.assertFalse(buffer.hasRemaining());
    }
    // Parse again one byte at a time
    for (ByteBuffer buffer : result.getByteBuffers()) {
        buffer.flip();
        while (buffer.hasRemaining()) parser.parse(ByteBuffer.wrap(new byte[] { buffer.get() }));
        Assert.assertFalse(buffer.hasRemaining());
    }
}
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) ServerParser(org.eclipse.jetty.fcgi.parser.ServerParser) FCGI(org.eclipse.jetty.fcgi.FCGI)

Example 2 with ServerParser

use of org.eclipse.jetty.fcgi.parser.ServerParser 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)

Aggregations

ByteBuffer (java.nio.ByteBuffer)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ServerParser (org.eclipse.jetty.fcgi.parser.ServerParser)2 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)2 MappedByteBufferPool (org.eclipse.jetty.io.MappedByteBufferPool)2 FCGI (org.eclipse.jetty.fcgi.FCGI)1 HttpField (org.eclipse.jetty.http.HttpField)1 HttpFields (org.eclipse.jetty.http.HttpFields)1 Test (org.junit.Test)1