spice.http.server.handler.SenderHandler.scala Maven / Gradle / Ivy
package spice.http.server.handler
import cats.effect.IO
import scribe.mdc.MDC
import spice.http.content.Content
import spice.http.{Headers, HttpExchange}
case class SenderHandler(content: Content,
length: Option[Long] = None,
caching: CachingManager = CachingManager.Default,
replace: Boolean = false) extends HttpHandler {
override def handle(exchange: HttpExchange)(implicit mdc: MDC): IO[HttpExchange] =
SenderHandler.handle(exchange, content, length, caching, replace)
}
object SenderHandler {
def handle(exchange: HttpExchange,
content: Content,
length: Option[Long] = None,
caching: CachingManager = CachingManager.Default,
replace: Boolean = false): IO[HttpExchange] = {
if (exchange.response.content.nonEmpty && !replace) {
throw new RuntimeException(s"Content already set (${exchange.response.content.get}) for HttpResponse in ${exchange.request.url} when attempting to set $content.")
}
val contentLength = length.getOrElse(content.length)
exchange.modify { response =>
IO(response.withContent(content).withHeader(Headers.`Content-Length`(contentLength)))
}.flatMap(caching.handle)
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy