Merge remote-tracking branch 'origin/master' into servlet-3.1-api

This commit is contained in:
Greg Wilkins 2013-05-31 18:46:10 +10:00
commit 41ac4a0877
6 changed files with 127 additions and 8 deletions

View File

@ -22,4 +22,20 @@
</New>
</Set>
<!-- example rule -->
<!--
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
<Set name="pattern">/favicon.ico</Set>
<Set name="name">Cache-Control</Set>
<Set name="value">Max-Age=3600,public</Set>
<Set name="terminating">true</Set>
</New>
</Arg>
</Call>
-->
<!-- for example rules see jetty-demo.xml -->
</Configure>

View File

@ -635,14 +635,19 @@ public class Response implements HttpServletResponse
return;
}
_fields.add(name, value);
if (HttpHeader.CONTENT_TYPE.is(name))
{
setContentType(value);
return;
}
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

View File

@ -202,6 +202,10 @@ public class ResponseTest
response.setCharacterEncoding(null);
response.setContentType("foo/bar");
assertEquals("foo/bar", response.getContentType());
response.recycle();
response.addHeader("Content-Type","text/something");
assertEquals("text/something",response.getContentType());
}
@Test

View File

@ -738,6 +738,8 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory
{
r.reset(true);
r.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
if (_etags)
r.getHttpFields().add(HttpHeader.ETAG,content.getETag());
r.flushBuffer();
return false;
}
@ -751,6 +753,8 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory
{
r.reset(true);
r.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
if (_etags)
r.getHttpFields().add(HttpHeader.ETAG,content.getETag());
r.flushBuffer();
return false;
}

View File

@ -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.Response http = tester.assertIsResponseGzipCompressed("GET","file.txt",System.currentTimeMillis()-4000);
Assert.assertEquals("Accept-Encoding",http.get("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();
tester.assertIsResponseNotModified("GET","file.txt",System.currentTimeMillis()+4000);
}
finally
{
tester.stop();
}
}
@Test
public void testIsNotGzipCompressedWithQ() throws Exception

View File

@ -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.
* <p>