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

dev.pellet.server.codec.http.HTTPHeaders.kt Maven / Gradle / Ivy

There is a newer version: 0.0.16
Show newest version
package dev.pellet.server.codec.http

import java.util.Locale

data class HTTPHeaders(
    private val storage: MutableMap> = mutableMapOf()
) {

    fun getSingleOrNull(headerName: String): HTTPHeader? {
        val headers = get(headerName)
        if (headers.size != 1) {
            return null
        }

        return headers.firstOrNull()
    }

    fun get(headerName: String): List {
        val normalisedName = normaliseName(headerName)
        return storage[normalisedName] ?: listOf()
    }

    fun add(header: HTTPHeader): HTTPHeaders {
        val normalisedName = normaliseName(header.rawName)
        val list = storage[normalisedName] ?: mutableListOf()
        list.add(header)
        storage[normalisedName] = list
        return this
    }

    private fun normaliseName(headerName: String): String {
        return headerName.lowercase(Locale.ENGLISH)
    }

    override fun toString(): String {
        return storage.toString()
    }

    fun forEach(lambda: (String, List) -> Unit) {
        storage.forEach {
            lambda(it.key, it.value)
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy