use of org.eclipse.jetty.http2.frames.PushPromiseFrame in project jetty.project by eclipse.
the class PushCacheFilterTest method testSelfPush.
@Test
public void testSelfPush() throws Exception {
// The test case is that of a login page, for example.
// When the user sends the credentials to the login page,
// the login may fail and redirect to the same login page,
// perhaps with different query parameters.
// In this case a request for the login page will push
// the login page itself, which will generate the pushed
// request for the login page, which will push the login
// page itself, etc. which is not the desired behavior.
final String primaryResource = "/login.html";
start(new HttpServlet() {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream output = response.getOutputStream();
String credentials = request.getParameter("credentials");
if (credentials == null) {
output.print("<html><head></head><body>LOGIN</body></html>");
} else if ("secret".equals(credentials)) {
output.print("<html><head></head><body>OK</body></html>");
} else {
response.setStatus(HttpStatus.TEMPORARY_REDIRECT_307);
response.setHeader(HttpHeader.LOCATION.asString(), primaryResource);
}
}
});
final String primaryURI = newURI(primaryResource);
final Session session = newClient(new Session.Listener.Adapter());
// Login with the wrong credentials, causing a redirect to self.
HttpFields primaryFields = new HttpFields();
MetaData.Request primaryRequest = newRequest("GET", primaryResource + "?credentials=wrong", primaryFields);
final CountDownLatch warmupLatch = new CountDownLatch(1);
session.newStream(new HeadersFrame(primaryRequest, null, true), new Promise.Adapter<>(), new Stream.Listener.Adapter() {
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
if (frame.isEndStream()) {
MetaData.Response response = (MetaData.Response) frame.getMetaData();
if (response.getStatus() == HttpStatus.TEMPORARY_REDIRECT_307) {
// Follow the redirect.
String location = response.getFields().get(HttpHeader.LOCATION);
HttpFields redirectFields = new HttpFields();
redirectFields.put(HttpHeader.REFERER, primaryURI);
MetaData.Request redirectRequest = newRequest("GET", location, redirectFields);
session.newStream(new HeadersFrame(redirectRequest, null, true), new Promise.Adapter<>(), new Adapter() {
@Override
public void onData(Stream stream, DataFrame frame, Callback callback) {
callback.succeeded();
if (frame.isEndStream())
warmupLatch.countDown();
}
});
}
}
}
});
Assert.assertTrue(warmupLatch.await(5, TimeUnit.SECONDS));
Thread.sleep(1000);
// Login with the right credentials, there must be no push.
primaryRequest = newRequest("GET", primaryResource + "?credentials=secret", primaryFields);
final CountDownLatch primaryResponseLatch = new CountDownLatch(1);
final CountDownLatch pushLatch = new CountDownLatch(1);
session.newStream(new HeadersFrame(primaryRequest, null, true), new Promise.Adapter<>(), new Stream.Listener.Adapter() {
@Override
public void onData(Stream stream, DataFrame frame, Callback callback) {
callback.succeeded();
if (frame.isEndStream())
primaryResponseLatch.countDown();
}
@Override
public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
pushLatch.countDown();
return null;
}
});
Assert.assertFalse(pushLatch.await(1, TimeUnit.SECONDS));
Assert.assertTrue(primaryResponseLatch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.http2.frames.PushPromiseFrame in project jetty.project by eclipse.
the class HttpReceiverOverHTTP2 method onPush.
@Override
public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
HttpExchange exchange = getHttpExchange();
if (exchange == null)
return null;
HttpRequest request = exchange.getRequest();
MetaData.Request metaData = (MetaData.Request) frame.getMetaData();
HttpRequest pushRequest = (HttpRequest) getHttpDestination().getHttpClient().newRequest(metaData.getURIString());
BiFunction<Request, Request, Response.CompleteListener> pushListener = request.getPushListener();
if (pushListener != null) {
Response.CompleteListener listener = pushListener.apply(request, pushRequest);
if (listener != null) {
HttpChannelOverHTTP2 pushChannel = getHttpChannel().getHttpConnection().newHttpChannel(true);
List<Response.ResponseListener> listeners = Collections.singletonList(listener);
HttpExchange pushExchange = new HttpExchange(getHttpDestination(), pushRequest, listeners);
pushChannel.associate(pushExchange);
pushChannel.setStream(stream);
// TODO: idle timeout ?
pushExchange.requestComplete(null);
pushExchange.terminateRequest();
return pushChannel.getStreamListener();
}
}
stream.reset(new ResetFrame(stream.getId(), ErrorCode.REFUSED_STREAM_ERROR.code), Callback.NOOP);
return null;
}
use of org.eclipse.jetty.http2.frames.PushPromiseFrame in project jetty.project by eclipse.
the class PushPromiseGenerateParseTest method testGenerateParseOneByteAtATime.
@Test
public void testGenerateParseOneByteAtATime() 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(ByteBuffer.wrap(new byte[] { buffer.get() }));
}
}
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.http2.frames.PushPromiseFrame in project jetty.project by eclipse.
the class PushPromiseBodyParser method onPushPromise.
private void onPushPromise(int streamId, MetaData metaData) {
PushPromiseFrame frame = new PushPromiseFrame(getStreamId(), streamId, metaData);
notifyPushPromise(frame);
}
use of org.eclipse.jetty.http2.frames.PushPromiseFrame 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));
}
}
}
Aggregations