-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an option to limit the response length
- Loading branch information
Showing
12 changed files
with
142 additions
and
9 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
77 changes: 77 additions & 0 deletions
77
core/src/main/scala/sttp/client4/internal/LimitedInputStream.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,77 @@ | ||
package sttp.tapir.server.jdkhttp.internal | ||
|
||
import sttp.capabilities.StreamMaxLengthExceededException | ||
import java.io.FilterInputStream | ||
import java.io.InputStream | ||
import java.io.IOException | ||
|
||
class FailingLimitedInputStream(in: InputStream, limit: Long) extends LimitedInputStream(in, limit) { | ||
override def onLimit: Int = { | ||
throw new StreamMaxLengthExceededException(limit) | ||
} | ||
} | ||
|
||
/** Based on Guava's https://github.com/google/guava/blob/master/guava/src/com/google/common/io/ByteStreams.java */ | ||
class LimitedInputStream(in: InputStream, limit: Long) extends FilterInputStream(in) { | ||
protected var left: Long = limit | ||
private var mark: Long = -1L | ||
|
||
override def available(): Int = Math.min(in.available(), left.toInt) | ||
|
||
override def mark(readLimit: Int): Unit = { | ||
in.mark(readLimit) | ||
mark = left | ||
} | ||
|
||
override def read(): Int = { | ||
if (left == 0) { | ||
onLimit | ||
} else { | ||
val result = in.read() | ||
if (result != -1) { | ||
left -= 1 | ||
} | ||
result | ||
} | ||
} | ||
|
||
override def read(b: Array[Byte], off: Int, len: Int): Int = { | ||
if (left == 0) { | ||
// Temporarily perform a read to check if more bytes are available | ||
val checkRead = in.read() | ||
if (checkRead == -1) { | ||
-1 // No more bytes available in the stream | ||
} else { | ||
onLimit | ||
} | ||
} else { | ||
val adjustedLen = Math.min(len, left.toInt) | ||
val result = in.read(b, off, adjustedLen) | ||
if (result != -1) { | ||
left -= result | ||
} | ||
result | ||
} | ||
} | ||
|
||
override def reset(): Unit = { | ||
if (!in.markSupported) { | ||
throw new IOException("Mark not supported") | ||
} | ||
if (mark == -1) { | ||
throw new IOException("Mark not set") | ||
} | ||
|
||
in.reset() | ||
left = mark | ||
} | ||
|
||
override def skip(n: Long): Long = { | ||
val toSkip = Math.min(n, left) | ||
val skipped = in.skip(toSkip) | ||
left -= skipped | ||
skipped | ||
} | ||
|
||
protected def onLimit: Int = -1 | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Body | ||
# Request body | ||
|
||
## Text data | ||
|
||
|
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