Skip to content

Commit da80710

Browse files
committed
add safeByteChar
1 parent 8457a4f commit da80710

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

http-core/src/main/scala-2.13/org/apache/pekko/http/impl/engine/package.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ package object parsing {
3737
case x => x.toString
3838
}
3939

40+
/**
41+
* Like `byteChar` but doesn't throw `NotEnoughDataException` if the index is out of bounds.
42+
* Used in places where we know that the index is valid because we checked the length beforehand.
43+
*/
44+
@inline
45+
private[http] def safeByteChar(input: ByteString, ix: Int): Char =
46+
(input(ix) & 0xFF).toChar
47+
4048
@inline
4149
private[http] def byteChar(input: ByteString, ix: Int): Char = (byteAt(input, ix) & 0xFF).toChar
4250

http-core/src/main/scala-3/org/apache/pekko/http/impl/engine/package.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ package object parsing {
3636
case x => x.toString
3737
}
3838

39+
/**
40+
* Like `byteChar` but doesn't throw `NotEnoughDataException` if the index is out of bounds.
41+
* Used in places where we know that the index is valid because we checked the length beforehand.
42+
*/
43+
private[http] inline def safeByteChar(input: ByteString, ix: Int): Char =
44+
(input(ix) & 0xFF).toChar
45+
3946
private[http] inline def byteChar(input: ByteString, ix: Int): Char = (byteAt(input, ix) & 0xFF).toChar
4047

4148
private[http] inline def byteAt(input: ByteString, ix: Int): Byte =

http-core/src/main/scala/org/apache/pekko/http/impl/engine/parsing/HttpHeaderParser.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ private[engine] final class HttpHeaderParser private (
242242
private def insert(input: ByteString, value: AnyRef)(cursor: Int = 0, endIx: Int = input.length, nodeIx: Int = 0,
243243
colonIx: Int = 0): Unit = {
244244
val char =
245-
if (cursor < colonIx) toLowerCase(byteChar(input, cursor))
246-
else if (cursor < endIx) byteChar(input, cursor)
245+
if (cursor < colonIx) toLowerCase(safeByteChar(input, cursor))
246+
else if (cursor < endIx) safeByteChar(input, cursor)
247247
else '\u0000'
248248
val node = nodes(nodeIx)
249249
if (char == node) insert(input, value)(cursor + 1, endIx, nodeIx + 1, colonIx) // fast match, descend into only subnode
@@ -290,7 +290,7 @@ private[engine] final class HttpHeaderParser private (
290290
endIx: Int = input.length, valueIx: Int = newValueIndex, colonIx: Int = 0): Unit = {
291291
val newNodeIx = newNodeIndex
292292
if (cursor < endIx) {
293-
val c = byteChar(input, cursor)
293+
val c = safeByteChar(input, cursor)
294294
val char = if (cursor < colonIx) toLowerCase(c) else c
295295
nodes(newNodeIx) = char
296296
insertRemainingCharsAsNewNodes(input, value)(cursor + 1, endIx, valueIx, colonIx)

0 commit comments

Comments
 (0)