use of org.eclipse.jetty.http2.frames.DataFrame 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;
}
use of org.eclipse.jetty.http2.frames.DataFrame in project jetty.project by eclipse.
the class HttpTransportOverHTTP2 method send.
private void send(ByteBuffer content, boolean lastContent, Callback callback) {
if (LOG.isDebugEnabled()) {
LOG.debug("HTTP2 Response #{}/{}: {} content bytes{}", stream.getId(), Integer.toHexString(stream.getSession().hashCode()), content.remaining(), lastContent ? " (last chunk)" : "");
}
DataFrame frame = new DataFrame(stream.getId(), content, lastContent);
stream.data(frame, callback);
}
use of org.eclipse.jetty.http2.frames.DataFrame 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.http2.frames.DataFrame in project jetty.project by eclipse.
the class HttpSenderOverHTTP2 method sendContent.
@Override
protected void sendContent(HttpExchange exchange, HttpContent content, Callback callback) {
if (content.isConsumed()) {
callback.succeeded();
} else {
Stream stream = getHttpChannel().getStream();
DataFrame frame = new DataFrame(stream.getId(), content.getByteBuffer(), content.isLast());
stream.data(frame, callback);
}
}
use of org.eclipse.jetty.http2.frames.DataFrame 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());
}
Aggregations