From 219be496d91b3e60a726a762cc182a7bfd10f083 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Fri, 31 May 2013 10:59:34 +1000 Subject: [PATCH 1/4] 408529 Etags set in 304 response --- .../eclipse/jetty/servlet/DefaultServlet.java | 4 ++ .../jetty/servlets/GzipFilterDefaultTest.java | 53 ++++++++++++++++- .../jetty/servlets/gzip/GzipTester.java | 58 +++++++++++++++++-- 3 files changed, 110 insertions(+), 5 deletions(-) diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java index 5b5f32b8992..5ddcd6686ba 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java @@ -756,6 +756,8 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory { r.reset(true); r.setStatus(HttpServletResponse.SC_NOT_MODIFIED); + if (_etags) + r.getHttpFields().add(HttpHeaders.ETAG_BUFFER,content.getETag()); r.flushBuffer(); return false; } @@ -769,6 +771,8 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory { r.reset(true); r.setStatus(HttpServletResponse.SC_NOT_MODIFIED); + if (_etags) + r.getHttpFields().add(HttpHeaders.ETAG_BUFFER,content.getETag()); r.flushBuffer(); return false; } diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterDefaultTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterDefaultTest.java index f6db6389041..0298b6e8e61 100644 --- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterDefaultTest.java +++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterDefaultTest.java @@ -145,7 +145,7 @@ public class GzipFilterDefaultTest String uri=req.getRequestURI(); if (uri.endsWith(".deferred")) { - System.err.println("type for "+uri.substring(0,uri.length()-9)+" is "+getServletContext().getMimeType(uri.substring(0,uri.length()-9))); + // System.err.println("type for "+uri.substring(0,uri.length()-9)+" is "+getServletContext().getMimeType(uri.substring(0,uri.length()-9))); resp.setContentType(getServletContext().getMimeType(uri.substring(0,uri.length()-9))); } @@ -272,6 +272,57 @@ public class GzipFilterDefaultTest tester.stop(); } } + + + @Test + public void testGzipedIfModified() throws Exception + { + GzipTester tester = new GzipTester(testingdir, compressionType); + + // Test content that is smaller than the buffer. + int filesize = CompressedResponseWrapper.DEFAULT_BUFFER_SIZE * 4; + tester.prepareServerFile("file.txt",filesize); + + FilterHolder holder = tester.setContentServlet(org.eclipse.jetty.servlet.DefaultServlet.class); + holder.setInitParameter("mimeTypes","text/plain"); + + try + { + tester.start(); + HttpTester http = tester.assertIsResponseGzipCompressed("GET","file.txt",System.currentTimeMillis()-4000); + Assert.assertEquals("Accept-Encoding",http.getHeader("Vary")); + } + finally + { + tester.stop(); + } + } + + + @Test + public void testNotGzipedIfNotModified() throws Exception + { + GzipTester tester = new GzipTester(testingdir, compressionType); + + // Test content that is smaller than the buffer. + int filesize = CompressedResponseWrapper.DEFAULT_BUFFER_SIZE * 4; + tester.prepareServerFile("file.txt",filesize); + + FilterHolder holder = tester.setContentServlet(org.eclipse.jetty.servlet.DefaultServlet.class); + holder.setInitParameter("mimeTypes","text/plain"); + holder.setInitParameter("etags","true"); + + try + { + tester.start(); + HttpTester http = tester.assertIsResponseNotModified("GET","file.txt",System.currentTimeMillis()+4000); + } + finally + { + tester.stop(); + } + } + @Test public void testIsNotGzipCompressedWithQ() throws Exception diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/gzip/GzipTester.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/gzip/GzipTester.java index 4a034d4c883..9a16f333af4 100644 --- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/gzip/GzipTester.java +++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/gzip/GzipTester.java @@ -45,6 +45,8 @@ import java.util.zip.InflaterInputStream; import javax.servlet.Servlet; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.http.HttpFields; +import org.eclipse.jetty.http.HttpHeaders; import org.eclipse.jetty.io.ByteArrayBuffer; import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlet.ServletHolder; @@ -54,6 +56,7 @@ import org.eclipse.jetty.testing.ServletTester; import org.eclipse.jetty.toolchain.test.IO; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.toolchain.test.TestingDir; +import org.eclipse.jetty.util.DateCache; import org.hamcrest.Matchers; import org.junit.Assert; @@ -78,10 +81,20 @@ public class GzipTester { return assertIsResponseGzipCompressed(method,filename,filename); } + + public HttpTester assertIsResponseGzipCompressed(String method,String filename,long ifmodifiedsince) throws Exception + { + return assertIsResponseGzipCompressed(method,filename,filename,ifmodifiedsince); + } public HttpTester assertIsResponseGzipCompressed(String method,String requestedFilename, String serverFilename) throws Exception { - System.err.printf("[GzipTester] requesting /context/%s%n",requestedFilename); + return assertIsResponseGzipCompressed(method,requestedFilename,serverFilename,-1); + } + + public HttpTester assertIsResponseGzipCompressed(String method,String requestedFilename, String serverFilename, long ifmodifiedsince) throws Exception + { + //System.err.printf("[GzipTester] requesting /context/%s%n",requestedFilename); HttpTester request = new HttpTester(); HttpTester response = new HttpTester(); @@ -89,6 +102,8 @@ public class GzipTester request.setVersion("HTTP/1.0"); request.setHeader("Host","tester"); request.setHeader("Accept-Encoding",compressionType); + if (ifmodifiedsince>0) + request.setHeader(HttpHeaders.IF_MODIFIED_SINCE,HttpFields.formatDate(ifmodifiedsince)); if (this.userAgent != null) request.setHeader("User-Agent", this.userAgent); request.setURI("/context/" + requestedFilename); @@ -98,7 +113,7 @@ public class GzipTester // Collect the response(s) ByteArrayBuffer respBuff = servletTester.getResponses(reqsBuff); response.parse(respBuff.asArray()); - + // Assert the response headers Assert.assertThat("Response.method",response.getMethod(),nullValue()); // Assert.assertThat("Response.status",response.getStatus(),is(HttpServletResponse.SC_OK)); @@ -145,6 +160,41 @@ public class GzipTester return response; } + + + public HttpTester assertIsResponseNotModified(String method,String requestedFilename, long ifmodifiedsince) throws Exception + { + return assertIsResponseNotModified(method,requestedFilename,requestedFilename,ifmodifiedsince); + } + + public HttpTester assertIsResponseNotModified(String method,String requestedFilename, String serverFilename, long ifmodifiedsince) throws Exception + { + //System.err.printf("[GzipTester] requesting /context/%s%n",requestedFilename); + HttpTester request = new HttpTester(); + HttpTester response = new HttpTester(); + + request.setMethod(method); + request.setVersion("HTTP/1.0"); + request.setHeader("Host","tester"); + request.setHeader("Accept-Encoding",compressionType); + if (ifmodifiedsince>0) + request.setHeader(HttpHeaders.IF_MODIFIED_SINCE,HttpFields.formatDate(ifmodifiedsince)); + if (this.userAgent != null) + request.setHeader("User-Agent", this.userAgent); + request.setURI("/context/" + requestedFilename); + + // Issue the request + ByteArrayBuffer reqsBuff = new ByteArrayBuffer(request.generate().getBytes()); + // Collect the response(s) + ByteArrayBuffer respBuff = servletTester.getResponses(reqsBuff); + response.parse(respBuff.asArray()); + + Assert.assertThat(response.getStatus(),Matchers.equalTo(304)); + Assert.assertThat(response.getHeader("ETag"),Matchers.startsWith("W/")); + + return response; + } + /** * Makes sure that the response contains an unfiltered file contents. @@ -163,7 +213,7 @@ public class GzipTester */ public void assertIsResponseNotGzipFiltered(String requestedFilename, String testResourceSha1Sum, String expectedContentType) throws Exception { - System.err.printf("[GzipTester] requesting /context/%s%n",requestedFilename); + // System.err.printf("[GzipTester] requesting /context/%s%n",requestedFilename); HttpTester request = new HttpTester(); HttpTester response = new HttpTester(); @@ -326,7 +376,7 @@ public class GzipTester private HttpTester executeRequest(String method,String uri) throws IOException, Exception { - System.err.printf("[GzipTester] requesting %s%n",uri); + //System.err.printf("[GzipTester] requesting %s%n",uri); HttpTester request = new HttpTester(); HttpTester response = new HttpTester(); From 3094f93ed99c21ef4e6883282978496c3f0f8d01 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Fri, 31 May 2013 11:06:33 +1000 Subject: [PATCH 2/4] 408642 setContentType from addHeader --- .../src/main/java/org/eclipse/jetty/server/Response.java | 7 +++++++ .../test/java/org/eclipse/jetty/server/ResponseTest.java | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java index 7308f80fdd0..19ebd721293 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java @@ -570,6 +570,7 @@ public class Response implements HttpServletResponse */ public void addHeader(String name, String value) { + if (_connection.isIncluding()) { if (name.startsWith(SET_INCLUDE_HEADER_PREFIX)) @@ -578,6 +579,12 @@ public class Response implements HttpServletResponse return; } + if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) + { + setContentType(value); + return; + } + _connection.getResponseFields().add(name, value); if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) _connection._generator.setContentLength(Long.parseLong(value)); diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java index 233e2f04d09..8361cfd7ba5 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java @@ -170,6 +170,10 @@ public class ResponseTest response.setContentType("foo/bar"); assertEquals("foo/bar", response.getContentType()); + response.recycle(); + response.addHeader("Content-Type","text/something"); + assertEquals("text/something",response.getContentType()); + } @Test From 0e11c8230712c2ce2206fc30f9606d640a231c0d Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Fri, 31 May 2013 18:07:01 +1000 Subject: [PATCH 3/4] fixed merge --- .../org/eclipse/jetty/server/Response.java | 11 +++-- .../eclipse/jetty/servlet/DefaultServlet.java | 4 +- .../jetty/servlets/GzipFilterDefaultTest.java | 6 +-- .../jetty/servlets/gzip/GzipTester.java | 43 ++++++++++++++++++- 4 files changed, 51 insertions(+), 13 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java index 242c3c607ac..2ba9e047384 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java @@ -613,20 +613,19 @@ public class Response implements HttpServletResponse return; } - if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) + if (HttpHeader.CONTENT_TYPE.is(name)) { setContentType(value); return; } - _fields.add(name, value); if (HttpHeader.CONTENT_LENGTH.is(name)) { - if (value == null) - _contentLength = -1l; - else - _contentLength = Long.parseLong(value); + setHeader(name,value); + return; } + + _fields.add(name, value); } @Override diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java index 6a56ab672ff..b5855333a07 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java @@ -739,7 +739,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory r.reset(true); r.setStatus(HttpServletResponse.SC_NOT_MODIFIED); if (_etags) - r.getHttpFields().add(HttpHeaders.ETAG_BUFFER,content.getETag()); + r.getHttpFields().add(HttpHeader.ETAG,content.getETag()); r.flushBuffer(); return false; } @@ -754,7 +754,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory r.reset(true); r.setStatus(HttpServletResponse.SC_NOT_MODIFIED); if (_etags) - r.getHttpFields().add(HttpHeaders.ETAG_BUFFER,content.getETag()); + r.getHttpFields().add(HttpHeader.ETAG,content.getETag()); r.flushBuffer(); return false; } diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterDefaultTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterDefaultTest.java index c496f0563d6..1aaa3010977 100644 --- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterDefaultTest.java +++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterDefaultTest.java @@ -289,8 +289,8 @@ public class GzipFilterDefaultTest try { tester.start(); - HttpTester http = tester.assertIsResponseGzipCompressed("GET","file.txt",System.currentTimeMillis()-4000); - Assert.assertEquals("Accept-Encoding",http.getHeader("Vary")); + HttpTester.Response http = tester.assertIsResponseGzipCompressed("GET","file.txt",System.currentTimeMillis()-4000); + Assert.assertEquals("Accept-Encoding",http.get("Vary")); } finally { @@ -315,7 +315,7 @@ public class GzipFilterDefaultTest try { tester.start(); - HttpTester http = tester.assertIsResponseNotModified("GET","file.txt",System.currentTimeMillis()+4000); + tester.assertIsResponseNotModified("GET","file.txt",System.currentTimeMillis()+4000); } finally { diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/gzip/GzipTester.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/gzip/GzipTester.java index 177af434740..3cf181a5ee8 100644 --- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/gzip/GzipTester.java +++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/gzip/GzipTester.java @@ -47,6 +47,8 @@ import javax.servlet.DispatcherType; import javax.servlet.Servlet; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.http.HttpFields; +import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpTester; import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlet.ServletHolder; @@ -77,10 +79,20 @@ public class GzipTester public HttpTester.Response assertIsResponseGzipCompressed(String method, String filename) throws Exception { - return assertIsResponseGzipCompressed(method,filename,filename); + return assertIsResponseGzipCompressed(method,filename,filename,-1); + } + + public HttpTester.Response assertIsResponseGzipCompressed(String method, String filename, long ifmodifiedsince) throws Exception + { + return assertIsResponseGzipCompressed(method,filename,filename,ifmodifiedsince); } public HttpTester.Response assertIsResponseGzipCompressed(String method, String requestedFilename, String serverFilename) throws Exception + { + return assertIsResponseGzipCompressed(method,requestedFilename,serverFilename,-1); + } + + public HttpTester.Response assertIsResponseGzipCompressed(String method, String requestedFilename, String serverFilename, long ifmodifiedsince) throws Exception { // System.err.printf("[GzipTester] requesting /context/%s%n",requestedFilename); HttpTester.Request request = HttpTester.newRequest(); @@ -90,13 +102,15 @@ public class GzipTester request.setVersion("HTTP/1.0"); request.setHeader("Host","tester"); request.setHeader("Accept-Encoding",compressionType); + if (ifmodifiedsince>0) + request.setHeader(HttpHeader.IF_MODIFIED_SINCE.asString(),HttpFields.formatDate(ifmodifiedsince)); if (this.userAgent != null) request.setHeader("User-Agent", this.userAgent); request.setURI("/context/" + requestedFilename); // Issue the request response = HttpTester.parseResponse(tester.getResponses(request.generate())); - + // Assert the response headers // Assert.assertThat("Response.status",response.getStatus(),is(HttpServletResponse.SC_OK)); Assert.assertThat("Response.header[Content-Length]",response.get("Content-Length"),notNullValue()); @@ -143,6 +157,31 @@ public class GzipTester return response; } + + public HttpTester.Response assertIsResponseNotModified(String method, String requestedFilename, long ifmodifiedsince) throws Exception + { // System.err.printf("[GzipTester] requesting /context/%s%n",requestedFilename); + HttpTester.Request request = HttpTester.newRequest(); + HttpTester.Response response; + + request.setMethod(method); + request.setVersion("HTTP/1.0"); + request.setHeader("Host","tester"); + request.setHeader("Accept-Encoding",compressionType); + if (ifmodifiedsince>0) + request.setHeader(HttpHeader.IF_MODIFIED_SINCE.asString(),HttpFields.formatDate(ifmodifiedsince)); + if (this.userAgent != null) + request.setHeader("User-Agent", this.userAgent); + request.setURI("/context/" + requestedFilename); + + // Issue the request + response = HttpTester.parseResponse(tester.getResponses(request.generate())); + + Assert.assertThat(response.getStatus(),Matchers.equalTo(304)); + Assert.assertThat(response.get("ETag"),Matchers.startsWith("W/")); + + return response; + } + /** * Makes sure that the response contains an unfiltered file contents. *

From 8fd796a3942c65aa4b4be1fad977a7b8c626545b Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Fri, 31 May 2013 18:29:44 +1000 Subject: [PATCH 4/4] 409012 added reference to example rewrite rules --- .../src/main/config/etc/jetty-rewrite.xml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/jetty-rewrite/src/main/config/etc/jetty-rewrite.xml b/jetty-rewrite/src/main/config/etc/jetty-rewrite.xml index e13a162291b..706a9328b03 100644 --- a/jetty-rewrite/src/main/config/etc/jetty-rewrite.xml +++ b/jetty-rewrite/src/main/config/etc/jetty-rewrite.xml @@ -22,4 +22,20 @@ + + + + +