Fixes #654 - ServletContext.getResourceAsStream("/") should return null

+ Jetty 9.2 (and earlier) used a Resource.getInputStream() implementation
   that would trigger an IOException on-construction (vs on-first-access)
   due its use of java.io.FileInputStream(File) which had a isDirectory
   check in its constructor.
 + Jetty 9.3 and onward uses java.nio.file.Files.newInputStream() which
   has the bad on-first-access behavior.
 + Changing the behavior of PathResource.getInputStream() to behave the
   same way as the prior FileResource.getInputStream(), as well as adding
   the Resource.isDirectory() check back into ContextHandler.getResourceAsStream(String)
   to prevent creation of the InputStream in the first place.
This commit is contained in:
Joakim Erdfelt 2016-06-22 09:09:05 -07:00
parent cb03ffd9a2
commit cce7837e64
2 changed files with 10 additions and 0 deletions

View File

@ -2123,6 +2123,9 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
if (url == null)
return null;
Resource r = Resource.newResource(url);
// Cannot serve directories as an InputStream
if(r.isDirectory())
return null;
return r.getInputStream();
}
catch (Exception e)

View File

@ -377,6 +377,13 @@ public class PathResource extends Resource
@Override
public InputStream getInputStream() throws IOException
{
/* Mimic behavior from old FileResource class and its
* usage of java.io.FileInputStream(File) which will trigger
* an IOException on construction if the path is a directory
*/
if (Files.isDirectory(path))
throw new IOException(path + " is a directory");
return Files.newInputStream(path,StandardOpenOption.READ);
}