From f869d6ccbc9e0b6f912445f5961f4bf00bc7a18a Mon Sep 17 00:00:00 2001 From: Mathias Date: Mon, 23 Feb 2015 12:47:27 +0100 Subject: [PATCH] =htp #16814 Fix ClassCastException in `PathMatchers.NumberMatcher` Direct port of https://github.com/spray/spray/commit/43bb625bbd91f9005a0e2218b739a6905fb516e7 --- .../http/server/directives/PathDirectivesSpec.scala | 13 +++++++++++++ .../main/scala/akka/http/server/PathMatcher.scala | 7 +++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/akka-http-tests/src/test/scala/akka/http/server/directives/PathDirectivesSpec.scala b/akka-http-tests/src/test/scala/akka/http/server/directives/PathDirectivesSpec.scala index ebe4be4e10..c86a3dc7ef 100644 --- a/akka-http-tests/src/test/scala/akka/http/server/directives/PathDirectivesSpec.scala +++ b/akka-http-tests/src/test/scala/akka/http/server/directives/PathDirectivesSpec.scala @@ -80,6 +80,19 @@ class PathDirectivesSpec extends RoutingSpec with Inside { "reject [/2147483648]" in test() // > Int.MaxValue } + "pathPrefix(CustomShortNumber)" should { + object CustomShortNumber extends NumberMatcher[Short](Short.MaxValue, 10) { + def fromChar(c: Char) = fromDecimalChar(c) + } + + val test = testFor(pathPrefix(CustomShortNumber) { echoCaptureAndUnmatchedPath }) + "accept [/23]" in test("23:") + "accept [/12345yes]" in test("12345:yes") + "reject [/]" in test() + "reject [/abc]" in test() + "reject [/33000]" in test() // > Short.MaxValue + } + "pathPrefix(JavaUUID)" should { val test = testFor(pathPrefix(JavaUUID) { echoCaptureAndUnmatchedPath }) "accept [/bdea8652-f26c-40ca-8157-0b96a2a8389d]" in test("bdea8652-f26c-40ca-8157-0b96a2a8389d:") diff --git a/akka-http/src/main/scala/akka/http/server/PathMatcher.scala b/akka-http/src/main/scala/akka/http/server/PathMatcher.scala index 986b6437f6..251a646480 100644 --- a/akka-http/src/main/scala/akka/http/server/PathMatcher.scala +++ b/akka-http/src/main/scala/akka/http/server/PathMatcher.scala @@ -378,13 +378,12 @@ trait PathMatchers { def fromChar(c: Char): T - def fromDecimalChar(c: Char): T = if ('0' <= c && c <= '9') (c - '0').asInstanceOf[T] else minusOne + def fromDecimalChar(c: Char): T = if ('0' <= c && c <= '9') x.fromInt(c - '0') else minusOne def fromHexChar(c: Char): T = - if ('0' <= c && c <= '9') (c - '0').asInstanceOf[T] else { + if ('0' <= c && c <= '9') x.fromInt(c - '0') else { val cn = c | 0x20 // normalize to lowercase - if ('a' <= cn && cn <= 'f') (cn - 'a' + 10).asInstanceOf[T] else - minusOne + if ('a' <= cn && cn <= 'f') x.fromInt(cn - 'a' + 10) else minusOne } }