Fixes #1099 - PushCacheFilter pushes POST requests.

This commit is contained in:
Simone Bordet 2016-11-14 22:28:15 +01:00
parent 708104ccaf
commit 5e5d6913e6
2 changed files with 94 additions and 4 deletions

View File

@ -799,4 +799,90 @@ public class PushCacheFilterTest extends AbstractTest
Assert.assertTrue(pushLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(primaryResponseLatch.await(5, TimeUnit.SECONDS));
}
@Test
public void testPOSTRequestIsNotPushed() throws Exception
{
final String primaryResource = "/primary.html";
final String secondaryResource = "/secondary.png";
final byte[] secondaryData = "SECONDARY".getBytes("UTF-8");
start(new HttpServlet()
{
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
String requestURI = req.getRequestURI();
ServletOutputStream output = resp.getOutputStream();
if (requestURI.endsWith(primaryResource))
output.print("<html><head></head><body>PRIMARY</body></html>");
else if (requestURI.endsWith(secondaryResource))
output.write(secondaryData);
}
});
final Session session = newClient(new Session.Listener.Adapter());
// Request for the primary and secondary resource to build the cache.
final String referrerURI = "http://localhost:" + connector.getLocalPort() + servletPath + primaryResource;
HttpFields primaryFields = new HttpFields();
MetaData.Request primaryRequest = newRequest("GET", primaryResource, primaryFields);
final CountDownLatch warmupLatch = 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())
{
// Request for the secondary resource.
HttpFields secondaryFields = new HttpFields();
secondaryFields.put(HttpHeader.REFERER, referrerURI);
MetaData.Request secondaryRequest = newRequest("GET", secondaryResource, secondaryFields);
session.newStream(new HeadersFrame(secondaryRequest, null, true), new Promise.Adapter<>(), new Stream.Listener.Adapter()
{
@Override
public void onData(Stream stream, DataFrame frame, Callback callback)
{
callback.succeeded();
warmupLatch.countDown();
}
});
}
}
});
Assert.assertTrue(warmupLatch.await(5, TimeUnit.SECONDS));
// Request again the primary resource with POST, we should not get the secondary resource pushed.
primaryRequest = newRequest("POST", primaryResource, 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 Stream.Listener onPush(Stream stream, PushPromiseFrame frame)
{
return new Adapter()
{
@Override
public void onData(Stream stream, DataFrame frame, Callback callback)
{
callback.succeeded();
if (frame.isEndStream())
pushLatch.countDown();
}
};
}
@Override
public void onData(Stream stream, DataFrame frame, Callback callback)
{
callback.succeeded();
if (frame.isEndStream())
primaryResponseLatch.countDown();
}
});
Assert.assertTrue(primaryResponseLatch.await(5, TimeUnit.SECONDS));
Assert.assertFalse(pushLatch.await(1, TimeUnit.SECONDS));
}
}

View File

@ -44,6 +44,7 @@ import javax.servlet.http.HttpServletRequest;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.Dispatcher;
@ -115,17 +116,20 @@ public class PushCacheFilter implements Filter
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException
{
if (HttpVersion.fromString(req.getProtocol()).getVersion() < 20)
HttpServletRequest request = (HttpServletRequest)req;
if (HttpVersion.fromString(req.getProtocol()).getVersion() < 20 ||
!HttpMethod.GET.is(request.getMethod()))
{
chain.doFilter(req, resp);
return;
}
long now = System.nanoTime();
HttpServletRequest request = (HttpServletRequest)req;
// Iterating over fields is more efficient than multiple gets
HttpFields fields = Request.getBaseRequest(request).getHttpFields();
Request jettyRequest = Request.getBaseRequest(request);
HttpFields fields = jettyRequest.getHttpFields();
boolean conditional = false;
String referrer = null;
loop:
@ -174,7 +178,7 @@ public class PushCacheFilter implements Filter
if (referredFromHere)
{
if ("GET".equalsIgnoreCase(request.getMethod()))
if (HttpMethod.GET.is(request.getMethod()))
{
String referrerPath = referrerURI.getPath();
if (referrerPath == null)