From 795faad37f7e0378655ad016402935ebf6b65b94 Mon Sep 17 00:00:00 2001 From: 2beaucoup Date: Wed, 1 Jul 2015 14:48:00 +0200 Subject: [PATCH] =htp #17873 normalize whitespace for resource paths --- akka-http-tests/lib/i have späces.jar | Bin 0 -> 488 bytes .../FileAndResourceDirectivesSpec.scala | 21 ++++++++++++------ .../FileAndResourceDirectives.scala | 11 +++++---- 3 files changed, 19 insertions(+), 13 deletions(-) create mode 100644 akka-http-tests/lib/i have späces.jar diff --git a/akka-http-tests/lib/i have späces.jar b/akka-http-tests/lib/i have späces.jar new file mode 100644 index 0000000000000000000000000000000000000000..3d009cbab1c3d18571868b12db61cf95afbbc26b GIT binary patch literal 488 zcmWIWW@Zs#;Nak3D64$r#()Gk8CV#6T|*poJ^kGD|D9rBU}gyLX6FE@U;+^g3=Ft6 zi!d-CH2XUGdAhj;{#5j_h!N}vF{ubwX9fla7ZgV%RX%dN zt+=O2l!1Xkn1O*okb!}rB(=Ciw getFromFile(Properties.javaHome) ~> check { handled shouldEqual (false) } } "return the file content with the MediaType matching the file extension" in { - val file = File.createTempFile("akkaHttpTest", ".PDF") + val file = File.createTempFile("akka Http Test", ".PDF") try { writeAllText("This is PDF", file) Get() ~> getFromFile(file.getPath) ~> check { @@ -104,12 +104,19 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins "reject requests to directory resources with trailing slash" in { Get() ~> getFromResource("someDir/") ~> check { handled shouldEqual (false) } } - "reject requests to directory resources from an Archive " in { + "reject requests to directory resources from an archive " in { Get() ~> getFromResource("com/typesafe/config") ~> check { handled shouldEqual (false) } } - "reject requests to directory resources from an Archive with trailing slash" in { + "reject requests to directory resources from an archive with trailing slash" in { Get() ~> getFromResource("com/typesafe/config/") ~> check { handled shouldEqual (false) } } + "return the resource from an archive with spaces and umlauts" in { + // contained within lib/jar with späces.jar + Get() ~> getFromResource("test-resource.txt") ~> check { + mediaType shouldEqual `text/plain` + responseAs[String] shouldEqual "I have spaces, too!" + } + } "return the resource content with the MediaType matching the file extension" in { val route = getFromResource("sample.html") @@ -129,7 +136,7 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins runCheck() runCheck() // additional test to check that no internal state is kept } - "return the resource content from an Archive" in { + "return the resource content from an archive" in { Get() ~> getFromResource("com/typesafe/config/Config.class") ~> check { mediaType shouldEqual `application/octet-stream` responseEntity.toStrict(1.second).awaitResult(1.second).data.asByteBuffer.getInt shouldEqual 0xCAFEBABE @@ -160,7 +167,7 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins "return the resource content with the MediaType matching the file extension - example 3" in { Get("subDirectory/empty.pdf") ~> getFromResourceDirectory("") ~> verify } - "return the resource content from an Archive" in { + "return the resource content from an archive" in { Get("Config.class") ~> getFromResourceDirectory("com/typesafe/config") ~> check { mediaType shouldEqual `application/octet-stream` responseEntity.toStrict(1.second).awaitResult(1.second).data.asByteBuffer.getInt shouldEqual 0xCAFEBABE @@ -178,10 +185,10 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins "reject requests to sub directory resources with trailing slash" in { Get("sub/") ~> getFromResourceDirectory("someDir") ~> check { handled shouldEqual (false) } } - "reject requests to directory resources from an Archive" in { + "reject requests to directory resources from an archive" in { Get() ~> getFromResourceDirectory("com/typesafe/config") ~> check { handled shouldEqual (false) } } - "reject requests to directory resources from an Archive with trailing slash" in { + "reject requests to directory resources from an archive with trailing slash" in { Get() ~> getFromResourceDirectory("com/typesafe/config/") ~> check { handled shouldEqual (false) } } } diff --git a/akka-http/src/main/scala/akka/http/scaladsl/server/directives/FileAndResourceDirectives.scala b/akka-http/src/main/scala/akka/http/scaladsl/server/directives/FileAndResourceDirectives.scala index 0420f9050d..5e3b23f36b 100644 --- a/akka-http/src/main/scala/akka/http/scaladsl/server/directives/FileAndResourceDirectives.scala +++ b/akka-http/src/main/scala/akka/http/scaladsl/server/directives/FileAndResourceDirectives.scala @@ -207,12 +207,11 @@ object FileAndResourceDirectives extends FileAndResourceDirectives { if (file.isDirectory) None else Some(ResourceFile(url, file.length(), file.lastModified())) case "jar" ⇒ - val jarFile = url.getFile - val startIndex = if (jarFile.startsWith("file:")) 5 else 0 - val bangIndex = jarFile.indexOf("!") - val jarFilePath = jarFile.substring(startIndex, bangIndex) - val resourcePath = jarFile.substring(bangIndex + 2) - val jar = new java.util.zip.ZipFile(jarFilePath) + val path = new URI(url.getPath).getPath // remove "file:" prefix and normalize whitespace + val bangIndex = path.indexOf('!') + val filePath = path.substring(0, bangIndex) + val resourcePath = path.substring(bangIndex + 2) + val jar = new java.util.zip.ZipFile(filePath) try { val entry = jar.getEntry(resourcePath) Option(jar.getInputStream(entry)) map { is ⇒