diff --git a/akka-http-core/src/main/scala/akka/http/engine/parsing/SpecializedHeaderValueParsers.scala b/akka-http-core/src/main/scala/akka/http/engine/parsing/SpecializedHeaderValueParsers.scala index 35f90d6228..d5a56189f6 100644 --- a/akka-http-core/src/main/scala/akka/http/engine/parsing/SpecializedHeaderValueParsers.scala +++ b/akka-http-core/src/main/scala/akka/http/engine/parsing/SpecializedHeaderValueParsers.scala @@ -7,7 +7,7 @@ package akka.http.engine.parsing import scala.annotation.tailrec import akka.util.ByteString import akka.http.model.{ HttpHeader, ErrorInfo } -import akka.http.model.headers.`Content-Length` +import akka.http.model.headers.{ RawHeader, `Content-Length` } import akka.http.model.parser.CharacterClasses._ /** @@ -22,9 +22,10 @@ private object SpecializedHeaderValueParsers { def apply(input: ByteString, valueStart: Int, warnOnIllegalHeader: ErrorInfo ⇒ Unit): (HttpHeader, Int) = { @tailrec def recurse(ix: Int = valueStart, result: Long = 0): (HttpHeader, Int) = { val c = byteChar(input, ix) - if (DIGIT(c) && result >= 0) recurse(ix + 1, result * 10 + c - '0') + if (result < 0) fail("`Content-Length` header value must not exceed 63-bit integer range") + else if (DIGIT(c)) recurse(ix + 1, result * 10 + c - '0') else if (WSP(c)) recurse(ix + 1, result) - else if (c == '\r' && byteChar(input, ix + 1) == '\n' && result >= 0) (`Content-Length`(result), ix + 2) + else if (c == '\r' && byteChar(input, ix + 1) == '\n') (`Content-Length`(result), ix + 2) else fail("Illegal `Content-Length` header value") } recurse() diff --git a/akka-http-core/src/test/scala/akka/http/engine/parsing/ContentLengthHeaderParserSpec.scala b/akka-http-core/src/test/scala/akka/http/engine/parsing/ContentLengthHeaderParserSpec.scala index 349896d2c0..7b54ff0d7b 100644 --- a/akka-http-core/src/test/scala/akka/http/engine/parsing/ContentLengthHeaderParserSpec.scala +++ b/akka-http-core/src/test/scala/akka/http/engine/parsing/ContentLengthHeaderParserSpec.scala @@ -13,14 +13,14 @@ class ContentLengthHeaderParserSpec extends WordSpec with Matchers { "specialized ContentLength parser" should { "accept zero" in { - parse("0") === 0L + parse("0") shouldEqual 0L } "accept positive value" in { - parse("43234398") === 43234398L + parse("43234398") shouldEqual 43234398L } "accept positive value > Int.MaxValue <= Long.MaxValue" in { - parse("274877906944") === 274877906944L - parse("9223372036854775807") === 9223372036854775807L // Long.MaxValue + parse("274877906944") shouldEqual 274877906944L + parse("9223372036854775807") shouldEqual 9223372036854775807L // Long.MaxValue } "don't accept positive value > Long.MaxValue" in { a[ParsingException] should be thrownBy parse("9223372036854775808") // Long.MaxValue + 1 diff --git a/akka-http-core/src/test/scala/akka/http/engine/parsing/RequestParserSpec.scala b/akka-http-core/src/test/scala/akka/http/engine/parsing/RequestParserSpec.scala index 707389913a..92b7d3a3bd 100644 --- a/akka-http-core/src/test/scala/akka/http/engine/parsing/RequestParserSpec.scala +++ b/akka-http-core/src/test/scala/akka/http/engine/parsing/RequestParserSpec.scala @@ -349,7 +349,7 @@ class RequestParserSpec extends FreeSpec with Matchers with BeforeAndAfterAll { |Content-length: 92233720368547758080 |Host: x | - |""" should parseToError(400: StatusCode, ErrorInfo("Illegal `Content-Length` header value")) + |""" should parseToError(400: StatusCode, ErrorInfo("`Content-Length` header value must not exceed 63-bit integer range")) } } }