All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.http4k.server.HelidonHandler.kt Maven / Gradle / Ivy

package org.http4k.server

import io.helidon.http.Status.create
import io.helidon.webserver.http.Handler
import io.helidon.webserver.http.ServerRequest
import io.helidon.webserver.http.ServerResponse
import org.http4k.core.HttpHandler
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.RequestSource
import org.http4k.core.Response
import org.http4k.core.Status.Companion.NOT_IMPLEMENTED
import org.http4k.core.Uri

fun HelidonHandler(http: HttpHandler) = Handler { req, res ->
    (req.toHttp4k()?.let(http) ?: Response(NOT_IMPLEMENTED)).into(res)
}

private fun ServerRequest.toHttp4k(): Request? =
    Method.supportedOrNull(prologue().method().text())
        ?.let {
            Request(it, Uri.of(path().rawPath() + query()), prologue().protocolVersion())
                .body(content().inputStream(), headers().contentLength().let {
                    if (it.isPresent) it.asLong else null
                })
                .headers(
                    headers()
                        .flatMap { value -> value.allValues().map { value.headerName().defaultCase() to it } }
                )
                .source(RequestSource(remotePeer().host(), remotePeer().port()))
        }

private fun Response.into(res: ServerResponse) = res.apply {
    status(create(status.code, status.description))
    [email protected] { it.first }.forEach {
        header(it.key, *it.value.map { it.second ?: "" }.toTypedArray())
    }
    outputStream().use { body.stream.copyTo(it) }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy