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;
}
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());
}
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());
}
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());
}
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);
}
}
Aggregations