HttpResponse.kt

  1. package com.hexagonkt.http.model

  2. import com.hexagonkt.http.checkHeaders
  3. import com.hexagonkt.http.model.ws.WsSession

  4. data class HttpResponse(
  5.     override val body: Any = "",
  6.     override val headers: Headers = Headers(),
  7.     override val contentType: ContentType? = null,
  8.     override val cookies: List<Cookie> = emptyList(),
  9.     override val status: HttpStatus = NOT_FOUND_404,
  10.     override val contentLength: Long = -1L,
  11.     override val onConnect: WsSession.() -> Unit = {},
  12.     override val onBinary: WsSession.(data: ByteArray) -> Unit = {},
  13.     override val onText: WsSession.(text: String) -> Unit = {},
  14.     override val onPing: WsSession.(data: ByteArray) -> Unit = {},
  15.     override val onPong: WsSession.(data: ByteArray) -> Unit = {},
  16.     override val onClose: WsSession.(status: Int, reason: String) -> Unit = { _, _ -> },
  17. ) : HttpResponsePort {

  18.     init {
  19.         checkHeaders(headers)
  20.     }

  21.     override fun with(
  22.         status: HttpStatus,
  23.         body: Any,
  24.         headers: Headers,
  25.         contentType: ContentType?,
  26.         cookies: List<Cookie>,
  27.         onConnect: WsSession.() -> Unit,
  28.         onBinary: WsSession.(data: ByteArray) -> Unit,
  29.         onText: WsSession.(text: String) -> Unit,
  30.         onPing: WsSession.(data: ByteArray) -> Unit,
  31.         onPong: WsSession.(data: ByteArray) -> Unit,
  32.         onClose: WsSession.(status: Int, reason: String) -> Unit,
  33.     ): HttpResponsePort =
  34.         copy(
  35.             status = status,
  36.             body = body,
  37.             headers = headers,
  38.             contentType = contentType,
  39.             cookies = cookies,
  40.             onConnect = onConnect,
  41.             onBinary = onBinary,
  42.             onText = onText,
  43.             onPing = onPing,
  44.             onPong = onPong,
  45.             onClose = onClose,
  46.         )
  47. }