-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3227 from jnatten/jdk-multipart-tmp
jdkhttp-server: Write multipart parts bigger than threshold to files
- Loading branch information
Showing
7 changed files
with
298 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
server/jdkhttp-server/src/main/scala/sttp/tapir/server/jdkhttp/internal/KMPMatcher.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package sttp.tapir.server.jdkhttp.internal | ||
import scala.collection.mutable | ||
|
||
class KMPMatcher(delimiter: Array[Byte]) { | ||
private val table = KMPMatcher.buildLongestPrefixSuffixTable(delimiter) | ||
private var matches: Int = 0 | ||
|
||
def noMatches = this.matches == 0 | ||
def getMatches: Int = this.matches | ||
def getDelimiter: Array[Byte] = this.delimiter | ||
|
||
def matchByte(b: Byte): KMPMatcher.MatchResult = { | ||
val numMatchesBeforeReset = getMatches | ||
while (getMatches > 0 && b != delimiter(getMatches)) { | ||
this.matches = this.table(getMatches - 1) | ||
} | ||
|
||
val matchesBeforeCurrentByte = getMatches | ||
|
||
if (b == delimiter(matches)) { | ||
matches += 1 | ||
if (this.matches == delimiter.length) { | ||
this.matches = 0 | ||
KMPMatcher.Match | ||
} else { | ||
KMPMatcher.NotMatched(numMatchesBeforeReset - matchesBeforeCurrentByte) | ||
} | ||
} else { | ||
KMPMatcher.NotMatched(numMatchesBeforeReset - matchesBeforeCurrentByte) | ||
} | ||
} | ||
} | ||
|
||
object KMPMatcher { | ||
sealed trait MatchResult | ||
case object Match extends MatchResult | ||
case class NotMatched(numNoLongerMatchedBytes: Int) extends MatchResult | ||
|
||
private def buildLongestPrefixSuffixTable(s: Array[Byte]): mutable.ArrayBuffer[Int] = { | ||
val lookupTable = mutable.ArrayBuffer.fill(s.length)(-1) | ||
lookupTable(0) = 0 | ||
var len = 0 | ||
var i = 1 | ||
while (i < s.length) { | ||
if (s(i) == s(len)) { | ||
len += 1 | ||
lookupTable(i) = len | ||
i += 1 | ||
} else { | ||
if (len == 0) { | ||
lookupTable(i) = 0 | ||
i = i + 1 | ||
} else { | ||
len = lookupTable(len - 1) | ||
} | ||
} | ||
} | ||
lookupTable | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
server/jdkhttp-server/src/test/scala/sttp/tapir/server/jdkhttp/internal/KMPMatcherTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package sttp.tapir.server.jdkhttp.internal | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import sttp.tapir.server.jdkhttp.internal.KMPMatcher.{Match, NotMatched} | ||
|
||
class KMPMatcherTest extends AnyFlatSpec with Matchers { | ||
|
||
it should "match over a set of bytes and not allow writing of any bytes if only matching" in { | ||
val matchBytes = "--abc\r\n".getBytes | ||
val matcher = new KMPMatcher(matchBytes) | ||
matcher.matchByte('-'.toByte) shouldBe NotMatched(0) | ||
matcher.matchByte('-'.toByte) shouldBe NotMatched(0) | ||
matcher.matchByte('a'.toByte) shouldBe NotMatched(0) | ||
matcher.matchByte('b'.toByte) shouldBe NotMatched(0) | ||
matcher.matchByte('c'.toByte) shouldBe NotMatched(0) | ||
matcher.matchByte('\r'.toByte) shouldBe NotMatched(0) | ||
matcher.matchByte('\n'.toByte) shouldBe Match | ||
} | ||
|
||
it should "match over a set of bytes and allow writing of any non-matched bytes" in { | ||
val matchBytes = "--abc\r\n".getBytes | ||
val matcher = new KMPMatcher(matchBytes) | ||
matcher.matchByte('-'.toByte) shouldBe NotMatched(0) | ||
matcher.matchByte('-'.toByte) shouldBe NotMatched(0) | ||
matcher.matchByte('-'.toByte) shouldBe NotMatched(1) | ||
matcher.matchByte('a'.toByte) shouldBe NotMatched(0) | ||
matcher.matchByte('a'.toByte) shouldBe NotMatched(3) | ||
matcher.matchByte('-'.toByte) shouldBe NotMatched(0) | ||
matcher.matchByte('-'.toByte) shouldBe NotMatched(0) | ||
matcher.matchByte('a'.toByte) shouldBe NotMatched(0) | ||
matcher.matchByte('b'.toByte) shouldBe NotMatched(0) | ||
matcher.matchByte('c'.toByte) shouldBe NotMatched(0) | ||
matcher.matchByte('\r'.toByte) shouldBe NotMatched(0) | ||
matcher.matchByte('\n'.toByte) shouldBe Match | ||
} | ||
} |
Oops, something went wrong.