Skip to content
Permalink
Browse files Browse the repository at this point in the history
Fix directory leak in StaticFile.fromUrl
  • Loading branch information
rossabaker committed May 26, 2021
1 parent 3ce0bfc commit 52e1890
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/src/main/scala/org/http4s/StaticFile.scala
Expand Up @@ -80,7 +80,7 @@ object StaticFile {
val fileUrl = url.getFile()
val file = new File(fileUrl)
OptionT.apply(F.defer {
if (file.isDirectory())
if (url.getProtocol === "file" && file.isDirectory)
F.pure(None)
else {
val urlConn = url.openConnection
Expand Down
16 changes: 15 additions & 1 deletion tests/src/test/scala/org/http4s/StaticFileSuite.scala
Expand Up @@ -277,14 +277,28 @@ class StaticFileSuite extends Http4sSuite {
len.assertEquals(Some(24005L))
}

test("return none from a URL that is a directory") {
test("return none from a file URL that is a directory") {
// val url = getClass.getResource("/foo")
StaticFile
.fromURL[IO](getClass.getResource("/foo"), testBlocker)
.value
.assertEquals(None)
}

test("not return none from an HTTP URL whose path is a directory") {
// We need a universal directory that also exists as a resource on
// a server. Creating a temp directory would be better, but then
// we need an HTTP server that responds to a wildcard path.
//
// Or we can be lazy and just use `/`.
assume(new File("/").isDirectory, "/ is not a directory")
StaticFile
.fromURL[IO](new URL("https://github.com//"), testBlocker)
.value
.map(_.fold(Status.NotFound)(_.status))
.assertEquals(Status.Ok)
}

test("return none from a URL that points to a resource that does not exist") {
StaticFile
.fromURL[IO](new URL("https://github.com/http4s/http4s/fooz"), testBlocker)
Expand Down

0 comments on commit 52e1890

Please sign in to comment.