=htc small improvement to ContentLengthParser

This commit is contained in:
Mathias 2014-10-08 13:41:48 +02:00
parent 46d51e531f
commit fe7db8e66a
3 changed files with 9 additions and 8 deletions

View file

@ -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()

View file

@ -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

View file

@ -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"))
}
}
}