From 6c01234e7cc025e9e81e751fa0a50984dd31d015 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Thu, 25 Nov 2010 02:45:00 +0000 Subject: [PATCH] Several minor fixes: Limit size of indirect buffer for direct serving of content. default servlet redirects directories without trailing / better git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2557 7e9141cc-0065-0410-87d8-b60c137991c4 --- .../jetty/embedded/OneServletContext.java | 6 ++++ .../org/eclipse/jetty/http/HttpContent.java | 11 +++++++ .../eclipse/jetty/server/ResourceCache.java | 2 +- .../server/handler/ContextHandlerTest.java | 32 +++++++++++++++++++ .../eclipse/jetty/servlet/DefaultServlet.java | 4 +-- 5 files changed, 52 insertions(+), 3 deletions(-) diff --git a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/OneServletContext.java b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/OneServletContext.java index 3e0a9cc95bd..c81db02b2c3 100644 --- a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/OneServletContext.java +++ b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/OneServletContext.java @@ -27,6 +27,12 @@ public class OneServletContext context.setContextPath("/"); server.setHandler(context); + // Server content from tmp + ServletHolder holder = context.addServlet(org.eclipse.jetty.servlet.DefaultServlet.class,"/tmp/*"); + holder.setInitParameter("resourceBase","/tmp"); + holder.setInitParameter("pathInfoOnly","true"); + + // Serve some hello world servlets context.addServlet(new ServletHolder(new HelloServlet()),"/*"); context.addServlet(new ServletHolder(new HelloServlet("Buongiorno Mondo")),"/it/*"); context.addServlet(new ServletHolder(new HelloServlet("Bonjour le Monde")),"/fr/*"); diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpContent.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpContent.java index 38a73d12ab9..f37a14c9d19 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpContent.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpContent.java @@ -43,11 +43,20 @@ public interface HttpContent { final Resource _resource; final Buffer _mimeType; + final int _maxBuffer; public ResourceAsHttpContent(final Resource resource, final Buffer mimeType) { _resource=resource; _mimeType=mimeType; + _maxBuffer=-1; + } + + public ResourceAsHttpContent(final Resource resource, final Buffer mimeType, int maxBuffer) + { + _resource=resource; + _mimeType=mimeType; + _maxBuffer=maxBuffer; } /* ------------------------------------------------------------ */ @@ -73,6 +82,8 @@ public interface HttpContent { try { + if (_maxBuffer<_resource.length()) + return null; ByteArrayBuffer buffer = new ByteArrayBuffer((int)_resource.length()); buffer.readFrom(_resource.getInputStream(),(int)_resource.length()); return buffer; diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceCache.java b/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceCache.java index 1f0393753ac..36be39d471c 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceCache.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceCache.java @@ -241,7 +241,7 @@ public class ResourceCache return content; } - return new HttpContent.ResourceAsHttpContent(resource,_mimeTypes.getMimeByExtension(resource.toString())); + return new HttpContent.ResourceAsHttpContent(resource,_mimeTypes.getMimeByExtension(resource.toString()),getMaxCachedFileSize()); } diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java index ad2282bab2b..ca0fe62513c 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java @@ -122,6 +122,38 @@ public class ContextHandlerTest ContextHandlerCollection contexts = new ContextHandlerCollection(); server.setHandler(contexts); + ContextHandler rootA = new ContextHandler(contexts,"/"); + ContextHandler fooA = new ContextHandler(contexts,"/foo"); + ContextHandler foobarA = new ContextHandler(contexts,"/foo/bar"); + + server.start(); + + // System.err.println(server.dump()); + + Assert.assertEquals(rootA._scontext,rootA._scontext.getContext("/")); + Assert.assertEquals(fooA._scontext,rootA._scontext.getContext("/foo")); + Assert.assertEquals(foobarA._scontext,rootA._scontext.getContext("/foo/bar")); + Assert.assertEquals(foobarA._scontext,rootA._scontext.getContext("/foo/bar/bob.jsp")); + Assert.assertEquals(rootA._scontext,rootA._scontext.getContext("/other")); + Assert.assertEquals(fooA._scontext,rootA._scontext.getContext("/foo/other")); + + Assert.assertEquals(rootA._scontext,foobarA._scontext.getContext("/")); + Assert.assertEquals(fooA._scontext,foobarA._scontext.getContext("/foo")); + Assert.assertEquals(foobarA._scontext,foobarA._scontext.getContext("/foo/bar")); + Assert.assertEquals(foobarA._scontext,foobarA._scontext.getContext("/foo/bar/bob.jsp")); + Assert.assertEquals(rootA._scontext,foobarA._scontext.getContext("/other")); + Assert.assertEquals(fooA._scontext,foobarA._scontext.getContext("/foo/other")); + } + + @Test + public void testContextVirtualGetContext() throws Exception + { + Server server = new Server(); + LocalConnector connector = new LocalConnector(); + server.setConnectors(new Connector[] { connector }); + ContextHandlerCollection contexts = new ContextHandlerCollection(); + server.setHandler(contexts); + ContextHandler rootA = new ContextHandler(contexts,"/"); rootA.setVirtualHosts(new String[] {"a.com"}); 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 002d9996e00..e8e644a7e97 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 @@ -389,7 +389,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory } String pathInContext=URIUtil.addPaths(servletPath,pathInfo); - boolean endsWithSlash=pathInContext.endsWith(URIUtil.SLASH); + boolean endsWithSlash=request.getRequestURI().endsWith(URIUtil.SLASH); // Can we gzip this request? String pathInContextGz=null; @@ -465,7 +465,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory { // ensure we have content if (content==null) - content=new HttpContent.ResourceAsHttpContent(resource,_mimeTypes.getMimeByExtension(resource.toString())); + content=new HttpContent.ResourceAsHttpContent(resource,_mimeTypes.getMimeByExtension(resource.toString()),response.getBufferSize()); if (included.booleanValue() || passConditionalHeaders(request,response, resource,content)) {