<commit_before>package org.jetbrains.ktor.features

import org.jetbrains.ktor.application.*
import org.jetbrains.ktor.http.*
import org.jetbrains.ktor.request.*
import org.jetbrains.ktor.response.*
import org.jetbrains.ktor.util.*
import java.net.*
import java.time.*
import java.util.*

class CORS(configuration: Configuration) {
    private val numberRegex = "[0-9]+".toRegex()

    val allowsAnyHost = "*" in configuration.hosts
    val allowCredentials = configuration.allowCredentials
    val allHeaders = configuration.headers + Configuration.CorsDefaultHeaders

    val methods = HashSet<HttpMethod>(configuration.methods + Configuration.CorsDefaultMethods)
    val headers = allHeaders.map { it.toLowerCase() }.toSet()

    private val headersListHeaderValue = allHeaders.sorted().joinToString(", ")
    private val methodsListHeaderValue = methods.map { it.value }.sorted().joinToString(", ")
    private val maxAgeHeaderValue = (configuration.maxAge.toMillis() / 1000).let { if (it > 0) it.toString() else null }
    private val exposedHeaders = if (configuration.exposedHeaders.isNotEmpty()) configuration.exposedHeaders.sorted().joinToString(", ") else null
    private val hostsNormalized = HashSet<String>(configuration.hosts.map { normalizeOrigin(it) })

    suspend fun intercept(call: ApplicationCall) {
        val origin = call.request.headers.getAll(HttpHeaders.Origin)?.singleOrNull()
        if (origin != null && isValidOrigin(origin)) {
            call.corsCheckOrigins(origin)

            if (call.request.httpMethod == HttpMethod.Options) {
                call.preFlight(call, origin)
            }

            call.corsCheckCurrentMethod()
            call.accessControlAllowOrigin(origin)
            call.accessControlAllowCredentials()

            if (exposedHeaders != null) {
                call.response.header(HttpHeaders.AccessControlExposeHeaders, exposedHeaders)
            }
        }
    }

    suspend private fun ApplicationCall.preFlight(call: ApplicationCall, origin: String) {
        corsCheckRequestMethod()
        corsCheckRequestHeaders()

        call.accessControlAllowOrigin(origin)
        call.response.header(HttpHeaders.AccessControlAllowMethods, methodsListHeaderValue)
        call.response.header(HttpHeaders.AccessControlAllowHeaders, headersListHeaderValue)
        call.accessControlAllowCredentials()
        call.accessControlMaxAge()

        call.respond(HttpStatusCode.OK)
    }
<commit_msg>Workaround compiler issue<commit_after>package org.jetbrains.ktor.features

import org.jetbrains.ktor.application.*
import org.jetbrains.ktor.http.*
import org.jetbrains.ktor.request.*
import org.jetbrains.ktor.response.*
import org.jetbrains.ktor.util.*
import java.net.*
import java.time.*
import java.util.*

class CORS(configuration: Configuration) {
    private val numberRegex = "[0-9]+".toRegex()

    val allowsAnyHost = "*" in configuration.hosts
    val allowCredentials = configuration.allowCredentials
    val allHeaders = configuration.headers + Configuration.CorsDefaultHeaders

    val methods = HashSet<HttpMethod>(configuration.methods + Configuration.CorsDefaultMethods)
    val headers = allHeaders.map { it.toLowerCase() }.toSet()

    private val headersListHeaderValue = allHeaders.sorted().joinToString(", ")
    private val methodsListHeaderValue = methods.map { it.value }.sorted().joinToString(", ")
    private val maxAgeHeaderValue = (configuration.maxAge.toMillis() / 1000).let { if (it > 0) it.toString() else null }
    private val exposedHeaders = if (configuration.exposedHeaders.isNotEmpty()) configuration.exposedHeaders.sorted().joinToString(", ") else null
    private val hostsNormalized = HashSet<String>(configuration.hosts.map { normalizeOrigin(it) })

    suspend fun intercept(call: ApplicationCall) {
        val origin = call.request.headers.getAll(HttpHeaders.Origin)?.singleOrNull()
                ?.takeIf(this::isValidOrigin)
                ?: return

        if (isValidOrigin(origin)) {
            call.corsCheckOrigins(origin)

            if (call.request.httpMethod == HttpMethod.Options) {
                call.preFlight(call, origin)
            }

            call.corsCheckCurrentMethod()
            call.accessControlAllowOrigin(origin)
            call.accessControlAllowCredentials()

            if (exposedHeaders != null) {
                call.response.header(HttpHeaders.AccessControlExposeHeaders, exposedHeaders)
            }
        }
    }

    suspend private fun ApplicationCall.preFlight(call: ApplicationCall, origin: String) {
        corsCheckRequestMethod()
        corsCheckRequestHeaders()

        call.accessControlAllowOrigin(origin)
        call.response.header(HttpHeaders.AccessControlAllowMethods, methodsListHeaderValue)
        call.response.header(HttpHeaders.AccessControlAllowHeaders, headersListHeaderValue)
        call.accessControlAllowCredentials()
        call.accessControlMaxAge()

        call.respond(HttpStatusCode.OK)
    }

{'fraction_non_alphanumeric': 0.10166099659795877, 'fraction_numerical': 0.002801681008605163, 'mean_word_length': 3.5108303249097474, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3826941', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1301, 'n_words': 244}