Repository: ktorio/ktor
File: ktor-server/ktor-server-core/jvm/src/io/ktor/server/http/content/StaticContentResolution.kt (Kotlin)

Code before:
            if (file.isFile) LocalFileContent(file, mimeResolve(url)) else null
        }

        "jar" -> {
            if (path.endsWith("/")) {
                null
            } else {
                val zipFile = findSimpleJarFile(url.toString())
                if (zipFile == null) {
                    return URIFileContent(url, mimeResolve(url))
                }
                JarFileContent(zipFile, path, mimeResolve(url)).takeIf { it.isFile }
            }
        }

        "jrt", "resource" -> {
            URIFileContent(url, mimeResolve(url))
        }

        else -> null
    }
}

// Only optimize single-level local 'jar:file:/...!/entry'
internal fun findSimpleJarFile(url: String): File? {
    if (!url.startsWith("jar:file:")) return null

    val jarPathSeparator = url.indexOf("!", startIndex = 9)
    if (jarPathSeparator == -1) {
        return null
    }
    val nextJarSeparator = url.indexOf("!", startIndex = jarPathSeparator + 1)
    if (nextJarSeparator != -1) {
        return null
    }

    return File(url.substring(9, jarPathSeparator).decodeURLPart())
}

internal fun String.extension(): String {
    val indexOfName = lastIndexOf('/').takeIf { it != -1 } ?: lastIndexOf('\\').takeIf { it != -1 } ?: 0
    val indexOfDot = indexOf('.', indexOfName)
    return if (indexOfDot >= 0) substring(indexOfDot) else ""
}

private fun normalisedPath(resourcePackage: String?, path: String): String {
    // note: we don't need to check for ".." in the normalizedPath because all ".." get replaced with //
    val pathComponents = path.split('/', '\\')
    if (pathComponents.contains("..")) {
        throw BadRequestException("Relative path should not contain path traversing characters: $path")
    }

Review comment (refactor):
Could you extract `"jar:file:"` into a constant and reference it like `JAR_PREFIX.length` below as well?  The 9 is a bit of a magic number.

Code after:
            if (file.isFile) LocalFileContent(file, mimeResolve(url)) else null
        }

        "jar" -> {
            if (path.endsWith("/")) {
                null
            } else {
                val zipFile = findContainingJarFile(url.toString())
                if (zipFile == null) {
                    return URIFileContent(url, mimeResolve(url))
                }
                JarFileContent(zipFile, path, mimeResolve(url)).takeIf { it.isFile }
            }
        }

        "jrt", "resource" -> {
            URIFileContent(url, mimeResolve(url))
        }

        else -> null
    }
}

/**
 * Returns the local jar file containing the resource pointed to by the given URL,
 * or `null` if URL is not a standard JAR URL.
 *
 * The syntax of a JAR URL is: `jar:<url>!/{entry}`
 * Example: `jar:file:/path/to/app.jar!/entry/inside/jar`
 *
 * Examples:
 * - `jar:file:/dist/app.jar!/static/index.html`              → returns `/dist/app.jar`
 * - `jar:file:/dist/app.jar!`                                → returns `/dist/app.jar`
 * - `jar:file:/outer.jar!/lib/dep.jar!/x.css`                → throws IllegalArgumentException (nested)
 * - `jar:nested:/path/to/app.jar/!BOOT-INF/classes/!/static` → returns `null` (non-local container)
 */
internal fun findContainingJarFile(url: String): File? {
    if (!url.startsWith(JAR_PREFIX)) return null

    val jarPathSeparator = url.indexOf("!", startIndex = JAR_PREFIX.length)
    if (jarPathSeparator == -1) {
        return null
    }
    val nextJarSeparator = url.indexOf("!", startIndex = jarPathSeparator + 1)
    if (nextJarSeparator != -1) {
        // KTOR-8883 Support nested jars in static resources
        throw IllegalArgumentException("Only local jars are supported (jar:file:)")
    }

    return File(url.substring(JAR_PREFIX.length, jarPathSeparator).decodeURLPart())
}