use of org.eclipse.jetty.http.MetaData in project jetty.project by eclipse.
the class HeadersGenerateParseTest method testGenerateParseOneByteAtATime.
@Test
public void testGenerateParseOneByteAtATime() throws Exception {
HeadersGenerator generator = new HeadersGenerator(new HeaderGenerator(), new HpackEncoder());
final List<HeadersFrame> frames = new ArrayList<>();
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {
@Override
public void onHeaders(HeadersFrame frame) {
frames.add(frame);
}
}, 4096, 8192);
// Iterate a few times to be sure generator and parser are properly reset.
for (int i = 0; i < 2; ++i) {
int streamId = 13;
HttpFields fields = new HttpFields();
fields.put("Accept", "text/html");
fields.put("User-Agent", "Jetty");
MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP, new HostPortHttpField("localhost:8080"), "/path", HttpVersion.HTTP_2, fields);
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
PriorityFrame priorityFrame = new PriorityFrame(streamId, 3 * streamId, 200, true);
generator.generateHeaders(lease, streamId, metaData, priorityFrame, true);
frames.clear();
for (ByteBuffer buffer : lease.getByteBuffers()) {
buffer = buffer.slice();
while (buffer.hasRemaining()) {
parser.parse(ByteBuffer.wrap(new byte[] { buffer.get() }));
}
}
Assert.assertEquals(1, frames.size());
HeadersFrame frame = frames.get(0);
Assert.assertEquals(streamId, frame.getStreamId());
Assert.assertTrue(frame.isEndStream());
MetaData.Request request = (MetaData.Request) frame.getMetaData();
Assert.assertEquals(metaData.getMethod(), request.getMethod());
Assert.assertEquals(metaData.getURI(), request.getURI());
for (int j = 0; j < fields.size(); ++j) {
HttpField field = fields.getField(j);
Assert.assertTrue(request.getFields().contains(field));
}
PriorityFrame priority = frame.getPriority();
Assert.assertNotNull(priority);
Assert.assertEquals(priorityFrame.getStreamId(), priority.getStreamId());
Assert.assertEquals(priorityFrame.getParentStreamId(), priority.getParentStreamId());
Assert.assertEquals(priorityFrame.getWeight(), priority.getWeight());
Assert.assertEquals(priorityFrame.isExclusive(), priority.isExclusive());
}
}
use of org.eclipse.jetty.http.MetaData in project jetty.project by eclipse.
the class PushPromiseGenerateParseTest method testGenerateParse.
@Test
public void testGenerateParse() throws Exception {
PushPromiseGenerator generator = new PushPromiseGenerator(new HeaderGenerator(), new HpackEncoder());
final List<PushPromiseFrame> frames = new ArrayList<>();
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {
@Override
public void onPushPromise(PushPromiseFrame frame) {
frames.add(frame);
}
}, 4096, 8192);
int streamId = 13;
int promisedStreamId = 17;
HttpFields fields = new HttpFields();
fields.put("Accept", "text/html");
fields.put("User-Agent", "Jetty");
MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP, new HostPortHttpField("localhost:8080"), "/path", HttpVersion.HTTP_2, fields);
// Iterate a few times to be sure generator and parser are properly reset.
for (int i = 0; i < 2; ++i) {
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.generatePushPromise(lease, streamId, promisedStreamId, metaData);
frames.clear();
for (ByteBuffer buffer : lease.getByteBuffers()) {
while (buffer.hasRemaining()) {
parser.parse(buffer);
}
}
Assert.assertEquals(1, frames.size());
PushPromiseFrame frame = frames.get(0);
Assert.assertEquals(streamId, frame.getStreamId());
Assert.assertEquals(promisedStreamId, frame.getPromisedStreamId());
MetaData.Request request = (MetaData.Request) frame.getMetaData();
Assert.assertEquals(metaData.getMethod(), request.getMethod());
Assert.assertEquals(metaData.getURI(), request.getURI());
for (int j = 0; j < fields.size(); ++j) {
HttpField field = fields.getField(j);
Assert.assertTrue(request.getFields().contains(field));
}
}
}
use of org.eclipse.jetty.http.MetaData in project jetty.project by eclipse.
the class HttpSenderOverHTTP2 method sendHeaders.
@Override
protected void sendHeaders(HttpExchange exchange, final HttpContent content, final Callback callback) {
Request request = exchange.getRequest();
String path = relativize(request.getPath());
HttpURI uri = new HttpURI(request.getScheme(), request.getHost(), request.getPort(), path, null, request.getQuery(), null);
MetaData.Request metaData = new MetaData.Request(request.getMethod(), uri, HttpVersion.HTTP_2, request.getHeaders());
HeadersFrame headersFrame = new HeadersFrame(metaData, null, !content.hasContent());
HttpChannelOverHTTP2 channel = getHttpChannel();
Promise<Stream> promise = new Promise<Stream>() {
@Override
public void succeeded(Stream stream) {
getHttpChannel().setStream(stream);
stream.setIdleTimeout(request.getIdleTimeout());
if (content.hasContent() && !expects100Continue(request)) {
boolean advanced = content.advance();
boolean lastContent = content.isLast();
if (advanced || lastContent) {
DataFrame dataFrame = new DataFrame(stream.getId(), content.getByteBuffer(), lastContent);
stream.data(dataFrame, callback);
return;
}
}
callback.succeeded();
}
@Override
public void failed(Throwable failure) {
callback.failed(failure);
}
};
// TODO optimize the send of HEADERS and DATA frames.
channel.getSession().newStream(headersFrame, promise, channel.getStreamListener());
}
use of org.eclipse.jetty.http.MetaData in project jetty.project by eclipse.
the class StreamCloseTest method testRequestDataClosedResponseDataClosedClosesStream.
@Test
public void testRequestDataClosedResponseDataClosedClosesStream() throws Exception {
final CountDownLatch serverDataLatch = new CountDownLatch(1);
start(new ServerSessionListener.Adapter() {
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
HeadersFrame response = new HeadersFrame(stream.getId(), metaData, null, false);
Callback.Completable completable = new Callback.Completable();
stream.headers(response, completable);
return new Stream.Listener.Adapter() {
@Override
public void onData(final Stream stream, DataFrame frame, final Callback callback) {
Assert.assertTrue(((HTTP2Stream) stream).isRemotelyClosed());
// We must copy the data that we send asynchronously.
ByteBuffer data = frame.getData();
ByteBuffer copy = ByteBuffer.allocate(data.remaining());
copy.put(data).flip();
completable.thenRun(() -> stream.data(new DataFrame(stream.getId(), copy, frame.isEndStream()), new Callback() {
@Override
public void succeeded() {
Assert.assertTrue(stream.isClosed());
Assert.assertEquals(0, stream.getSession().getStreams().size());
callback.succeeded();
serverDataLatch.countDown();
}
}));
}
};
}
});
final CountDownLatch completeLatch = new CountDownLatch(1);
Session session = newClient(new Session.Listener.Adapter());
HeadersFrame frame = new HeadersFrame(newRequest("GET", new HttpFields()), null, false);
FuturePromise<Stream> promise = new FuturePromise<>();
session.newStream(frame, promise, new Stream.Listener.Adapter() {
@Override
public void onData(Stream stream, DataFrame frame, Callback callback) {
// The sent data callback may not be notified yet here.
callback.succeeded();
completeLatch.countDown();
}
});
final Stream stream = promise.get(5, TimeUnit.SECONDS);
Assert.assertFalse(stream.isClosed());
Assert.assertFalse(((HTTP2Stream) stream).isLocallyClosed());
final CountDownLatch clientDataLatch = new CountDownLatch(1);
stream.data(new DataFrame(stream.getId(), ByteBuffer.wrap(new byte[512]), true), new Callback() {
@Override
public void succeeded() {
// Here the stream may be just locally closed or fully closed.
clientDataLatch.countDown();
}
});
Assert.assertTrue(clientDataLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(serverDataLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(stream.isClosed());
Assert.assertEquals(0, stream.getSession().getStreams().size());
}
use of org.eclipse.jetty.http.MetaData in project jetty.project by eclipse.
the class StreamCountTest method testServerAllowsOneStreamEnforcedByClient.
@Test
public void testServerAllowsOneStreamEnforcedByClient() throws Exception {
start(new ServerSessionListener.Adapter() {
@Override
public Map<Integer, Integer> onPreface(Session session) {
Map<Integer, Integer> settings = new HashMap<>();
settings.put(SettingsFrame.MAX_CONCURRENT_STREAMS, 1);
return settings;
}
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
return new Stream.Listener.Adapter() {
@Override
public void onData(Stream stream, DataFrame frame, Callback callback) {
if (frame.isEndStream()) {
HttpFields fields = new HttpFields();
MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, 200, fields);
stream.headers(new HeadersFrame(stream.getId(), metaData, null, true), callback);
} else {
callback.succeeded();
}
}
};
}
});
final CountDownLatch settingsLatch = new CountDownLatch(1);
Session session = newClient(new Session.Listener.Adapter() {
@Override
public void onSettings(Session session, SettingsFrame frame) {
settingsLatch.countDown();
}
});
Assert.assertTrue(settingsLatch.await(5, TimeUnit.SECONDS));
HttpFields fields = new HttpFields();
MetaData.Request metaData = newRequest("GET", fields);
HeadersFrame frame1 = new HeadersFrame(metaData, null, false);
FuturePromise<Stream> streamPromise1 = new FuturePromise<>();
final CountDownLatch responseLatch = new CountDownLatch(1);
session.newStream(frame1, streamPromise1, new Stream.Listener.Adapter() {
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
if (frame.isEndStream())
responseLatch.countDown();
}
});
Stream stream1 = streamPromise1.get(5, TimeUnit.SECONDS);
HeadersFrame frame2 = new HeadersFrame(metaData, null, false);
FuturePromise<Stream> streamPromise2 = new FuturePromise<>();
session.newStream(frame2, streamPromise2, new Stream.Listener.Adapter());
try {
streamPromise2.get(5, TimeUnit.SECONDS);
Assert.fail();
} catch (ExecutionException x) {
// Expected
}
stream1.data(new DataFrame(stream1.getId(), BufferUtil.EMPTY_BUFFER, true), Callback.NOOP);
Assert.assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
}
Aggregations