Limit the download size of an Sttp client

Hello,

I’m trying to setup an Sttp (4) client to download some files through Fs2 as a Stream. I was wondering if it was possible to configure the client in a way that it would fail if a file is deemed to large?

Thank you,

There isn’t any configuration for this (maybe there should be! can you create a ticket?) but if you’re using HttpClientFs2Backend I think this should be achievable by subclassing it:

class MyFs2Backend[F[_]: Async] extends HttpClientFs2Backend[F] {
  override protected def bodyHandlerBodyToBody(p: Publisher[util.List[ByteBuffer]]): Stream[F, Byte] =
    addSizeLimit(super.bodyHandlerBodyToBody(p))
}

where addSizeLimit: Stream[F, Byte] => Stream[F, Byte] makes the stream fail if it’s too large. This should work for any kind of body that is specified using .response(...).

However, this isn’t ideal as you’ll also have to copy the backend-creation method, one of HttpClientFs2Backend.apply / resource / …, so that it uses your version of the backend, instead of the default one.

Thanks Adam, I will create a ticket then :+1: In the meantime I’m handling that in the response directly on the Stream

For visibility: I created [Feature Request] Add a configurable download size limit to clients · Issue #1954 · softwaremill/sttp · GitHub