Skip to content

Commit 957f913

Browse files
authored
more performant boundary check (#799)
* more performant boundary check * try to fix tests * Update BodyPartParser.scala * Update BodyPartParser.scala * Update BodyPartParser.scala * Update BodyPartParser.scala * add HttpConstants
1 parent af7ed86 commit 957f913

File tree

2 files changed

+66
-12
lines changed

2 files changed

+66
-12
lines changed

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

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@ package org.apache.pekko.http.impl.engine.parsing
1616
import org.apache.pekko
1717
import pekko.NotUsed
1818
import pekko.annotation.InternalApi
19-
20-
import scala.annotation.tailrec
2119
import pekko.event.LoggingAdapter
22-
import org.parboiled2.CharPredicate
20+
import pekko.http.impl.util._
21+
import pekko.http.scaladsl.model._
22+
import pekko.http.scaladsl.model.headers._
23+
import pekko.stream.{ Attributes, FlowShape, Inlet, Outlet }
2324
import pekko.stream.scaladsl.Source
2425
import pekko.stream.stage._
2526
import pekko.util.ByteString
26-
import pekko.http.scaladsl.model._
27-
import pekko.http.impl.util._
28-
import pekko.stream.{ Attributes, FlowShape, Inlet, Outlet }
29-
import pekko.http.scaladsl.model.headers._
27+
import org.parboiled2.CharPredicate
3028

29+
import scala.annotation.tailrec
3130
import scala.collection.mutable.ListBuffer
3231

3332
/**
@@ -355,15 +354,38 @@ private[http] object BodyPartParser {
355354
}
356355

357356
case class UndefinedEndOfLineConfiguration(boundary: String) extends EndOfLineConfiguration {
357+
import HttpConstants._
358+
358359
override def eol: String = "\r\n"
359360

360361
override def defineOnce(byteString: ByteString): EndOfLineConfiguration = {
361362
// Hypothesis: There is either CRLF or LF as EOL, no mix possible
362-
val crLfNeedle = ByteString(s"$boundary\r\n")
363-
val lfNeedle = ByteString(s"$boundary\n")
364-
if (byteString.containsSlice(crLfNeedle)) DefinedEndOfLineConfiguration("\r\n", boundary)
365-
else if (byteString.containsSlice(lfNeedle)) DefinedEndOfLineConfiguration("\n", boundary)
366-
else this
363+
checkForBoundary(byteString) match {
364+
case CR_BYTE => DefinedEndOfLineConfiguration("\r\n", boundary)
365+
case LF_BYTE => DefinedEndOfLineConfiguration("\n", boundary)
366+
case _ => this
367+
}
368+
}
369+
370+
// returns CR for CRLF, LF for LF, 0 otherwise
371+
private def checkForBoundary(byteString: ByteString): Byte = {
372+
val check = ByteString(boundary)
373+
@tailrec def findBoundary(offset: Int): Byte = {
374+
val index = byteString.indexOfSlice(check, offset)
375+
if (index != -1) {
376+
val newIndex = index + boundary.length
377+
byteAt(byteString, newIndex) match {
378+
case CR_BYTE =>
379+
if (byteAt(byteString, newIndex + 1) == LF_BYTE) CR_BYTE else findBoundary(index + 1)
380+
case LF_BYTE => LF_BYTE
381+
case _ => findBoundary(index + 1)
382+
}
383+
} else 0
384+
}
385+
try findBoundary(0)
386+
catch {
387+
case NotEnoughDataException => 0
388+
}
367389
}
368390
}
369391
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.pekko.http.impl.util
19+
20+
import org.apache.pekko.annotation.InternalApi
21+
22+
/**
23+
* INTERNAL API
24+
*
25+
* This object contains HTTP related constants that are used in various places.
26+
* It is not intended to be used outside of the HTTP implementation.
27+
*/
28+
@InternalApi
29+
private[http] object HttpConstants {
30+
final val CR_BYTE: Byte = 13
31+
final val LF_BYTE: Byte = 10
32+
}

0 commit comments

Comments
 (0)