Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize byte array operations in Http4sRequestBody #3772

Merged
merged 5 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ object ExceptionHandler {

case class DefaultExceptionHandler[F[_]](response: (StatusCode, String) => ValuedEndpointOutput[_]) extends ExceptionHandler[F] {
override def apply(ctx: ExceptionContext)(implicit monad: MonadError[F]): F[Option[ValuedEndpointOutput[_]]] =
ctx.e match {
case StreamMaxLengthExceededException(maxBytes) =>
(ctx.e, ctx.e.getCause()) match {
case (StreamMaxLengthExceededException(maxBytes), _) =>
monad.unit(Some(response(StatusCode.PayloadTooLarge, s"Payload limit (${maxBytes}B) exceeded")))
case (_, StreamMaxLengthExceededException(maxBytes)) =>
monad.unit(Some(response(StatusCode.PayloadTooLarge, s"Payload limit (${maxBytes}B) exceeded")))
case _ =>
monad.unit(Some(response(StatusCode.InternalServerError, "Internal server error")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import cats.effect.{Async, Sync}
import cats.syntax.all._
import fs2.Chunk
import fs2.io.file.Files
import fs2.io.toInputStreamResource
import fs2.text.decodeWithCharset
import org.http4s.headers.{`Content-Disposition`, `Content-Type`}
import org.http4s.{Charset, EntityDecoder, Request, multipart}
import org.http4s.{Charset, EntityDecoder, Headers, Media, Request, multipart}
import sttp.capabilities.fs2.Fs2Streams
import sttp.model.{Header, Part}
import sttp.tapir.model.ServerRequest
import sttp.tapir.server.interpreter.{RawValue, RequestBody}
import sttp.tapir.{FileRange, InputStreamRange, RawBodyType, RawPart}

import java.io.ByteArrayInputStream
import org.http4s.Media
import org.http4s.Headers

private[http4s] class Http4sRequestBody[F[_]: Async](
serverOptions: Http4sServerOptions[F]
) extends RequestBody[F, Fs2Streams[F]] {
Expand All @@ -42,12 +40,13 @@ private[http4s] class Http4sRequestBody[F[_]: Async](

bodyType match {
case RawBodyType.StringBody(defaultCharset) =>
asByteArray.map(new String(_, charset.map(_.nioCharset).getOrElse(defaultCharset))).map(RawValue(_))
case RawBodyType.ByteArrayBody => asByteArray.map(RawValue(_))
case RawBodyType.ByteBufferBody => asChunk.map(c => RawValue(c.toByteBuffer))
case RawBodyType.InputStreamBody => asByteArray.map(b => RawValue(new ByteArrayInputStream(b)))
case RawBodyType.InputStreamRangeBody => asByteArray.map(b => RawValue(InputStreamRange(() => new ByteArrayInputStream(b))))

body.through(decodeWithCharset(charset.map(_.nioCharset).getOrElse(defaultCharset))).compile.string.map(RawValue(_))
case RawBodyType.ByteArrayBody => asByteArray.map(RawValue(_))
case RawBodyType.ByteBufferBody => asChunk.map(c => RawValue(c.toByteBuffer))
case RawBodyType.InputStreamBody =>
toInputStreamResource(body).allocated.map { case (is, _) => RawValue(is) }
case RawBodyType.InputStreamRangeBody =>
toInputStreamResource(body).allocated.map { case (is, _) => RawValue(InputStreamRange(() => is)) }
case RawBodyType.FileBody =>
serverOptions.createFile(serverRequest).flatMap { file =>
val fileSink = Files[F].writeAll(file.toPath)
Expand Down
Loading