Yokan / rebuild
Course 1 8624561

HttpService.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/http/HttpService.kt

implementation68 lines
1package eu.friedcutlet.yokan.services.http23fun interface HttpService {4    suspend fun request(request: HttpRequest): HttpResponse56    /**7     * Only a 2xx body reaches [onChunk]; any other body comes back buffered in [HttpStreamResponse.body],8     * capped at [STREAM_ERROR_BODY_LIMIT], so callers can inspect it (Cloudflare challenge pages) without9     * the error page ever landing in the caller's file.10     */11    suspend fun stream(request: HttpRequest, onChunk: suspend (ByteArray) -> Unit): HttpStreamResponse {12        val response = request(request)13        val success = response.statusCode in 200..29914        request.onStreamResponse?.invoke(HttpStreamResponse(response.statusCode, response.headers, response.setCookies))15        if (success && response.bodyBytes.isNotEmpty()) onChunk(response.bodyBytes)16        return HttpStreamResponse(17            statusCode = response.statusCode,18            headers = response.headers,19            setCookies = response.setCookies,20            body = if (success) ByteArray(0) else response.bodyBytes.copyOf(minOf(response.bodyBytes.size, STREAM_ERROR_BODY_LIMIT)),21        )22    }23}2425/** Enough for a challenge page's markers; error bodies past this are dropped, never streamed. */26const val STREAM_ERROR_BODY_LIMIT = 64 * 10242728data class HttpRequest(29    val url: String,30    val method: String = "GET",31    val headers: Map<String, String> = emptyMap(),32    val body: String? = null,33    val bodyBytes: ByteArray? = null,34    /** Streaming consumers validate headers before any bytes are written, including on failed transfers. */35    val onStreamResponse: (suspend (HttpStreamResponse) -> Unit)? = null,36)3738class HttpResponse(39    val statusCode: Int,40    body: String? = null,41    val headers: Map<String, String> = emptyMap(),42    val bodyBytes: ByteArray = body?.encodeToByteArray() ?: ByteArray(0),43    /**44     * Raw `Set-Cookie` response headers, one entry per cookie. Kept separate45     * from [headers] because Ktor's joined-map view loses the boundaries46     * between cookies whose attributes contain commas (`Expires=Mon, …`).47     */48    val setCookies: List<String> = emptyList(),49) {50    private val suppliedBody = body5152    /** Decode only for text consumers; binary callers retain one representation. */53    val body: String by lazy(LazyThreadSafetyMode.NONE) {54        suppliedBody ?: bodyBytes.decodeToString()55    }56}5758data class HttpStreamResponse(59    val statusCode: Int,60    val headers: Map<String, String> = emptyMap(),61    val setCookies: List<String> = emptyList(),62    /** The non-2xx body (see [HttpService.stream]); empty for 2xx, whose bytes went to `onChunk`. */63    val body: ByteArray = ByteArray(0),64)6566/** Case-insensitive header value, trimmed; null when absent or blank. */67fun Map<String, String>.header(name: String): String? =68    entries.firstOrNull { it.key.equals(name, ignoreCase = true) }?.value?.trim()?.takeIf(String::isNotEmpty)69

Search chapters, reference and source filenames.