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

dk.magical.loom.response.HttpResponseBuilder.kt Maven / Gradle / Ivy

package dk.magical.loom.response

import dk.magical.loom.Status

/**
 * Created by Christian on 25/10/2016.
 */
class HttpResponseBuilder {
    fun build(status: Status, headers: Map, body: String?): List {
        val list: MutableList = mutableListOf()
        list.add(statusLine(status))
        list.add(contentLength(body))
        list.addAll(headerLiners(headers))
        list.add("")
        if (body != null)
            list.add(body)

        return list
    }

    private fun statusLine(status: Status): String {
        return "HTTP/1.1 ${status.statusCode} ${status.message}"
    }

    private fun contentLength(body: String?): String {
        val length = if (body == null) 0 else body.toByteArray(Charsets.UTF_8).size
        return "Content-Length: ${length}"
    }

    private fun headerLiners(headers: Map): List {
        return headers.map { "${it.key}: ${it.value}" }.toList()
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy