JVM-specific HTTP utilities and extensions for the Ktor framework providing URL utilities, file content type detection, HTTP message properties, and content handling for JVM platforms
—
Content-Type header handling with JVM-specific file extension detection and MIME type mapping. Provides comprehensive predefined content types and automatic detection for files.
Represents Content-Type header values with parameter support.
/**
* Represents a value for a Content-Type header
* @property contentType type part of the media type
* @property contentSubtype subtype part of the media type
*/
class ContentType private constructor(
val contentType: String,
val contentSubtype: String,
parameters: List<HeaderValueParam> = emptyList()
) : HeaderValueWithParameters {
/**
* Creates a copy with the added parameter
* @param name parameter name
* @param value parameter value
* @return ContentType with added parameter
*/
fun withParameter(name: String, value: String): ContentType
/**
* Creates a copy without parameters
* @return ContentType without parameters
*/
fun withoutParameters(): ContentType
/**
* Check if this content type matches another
* @param contentType content type to match against
* @return true if they match
*/
fun match(contentType: ContentType): Boolean
/**
* Check if this content type matches a pattern
* @param pattern pattern string to match
* @return true if pattern matches
*/
fun match(pattern: String): Boolean
companion object {
/**
* Any content type (*/*)
*/
val Any: ContentType
/**
* Parse content type from string
* @param value content type string
* @return ContentType instance
*/
fun parse(value: String): ContentType
}
}
/**
* Add charset parameter to content type
* @param charset charset to add
* @return ContentType with charset parameter
*/
fun ContentType.withCharset(charset: Charset): ContentType
/**
* Add charset parameter only if not already present
* @param charset charset to add
* @return ContentType with charset parameter if needed
*/
fun ContentType.withCharsetIfNeeded(charset: Charset): ContentType
/**
* Extract charset from content type parameters
* @return Charset or null if not specified
*/
fun HeaderValueWithParameters.charset(): Charset?Application-specific content types for APIs and data exchange.
object ContentType.Application {
val Any: ContentType
val Atom: ContentType
val Cbor: ContentType
val Json: ContentType
val HalJson: ContentType
val ProblemJson: ContentType
val JavaScript: ContentType
val OctetStream: ContentType
val FormUrlEncoded: ContentType
val Pdf: ContentType
val ProtoBuf: ContentType
val Rss: ContentType
val Xml: ContentType
val Xml_Dtd: ContentType
val Zip: ContentType
val GZip: ContentType
val Wasm: ContentType
val ProblemXml: ContentType
val Soap: ContentType
val Yaml: ContentType
val Docx: ContentType
val Pptx: ContentType
val Xlsx: ContentType
/**
* Check if content type is within Application category
*/
fun contains(contentType: ContentType): Boolean
fun contains(contentType: CharSequence): Boolean
}Text-based content types for web content and documents.
object ContentType.Text {
val Any: ContentType
val Plain: ContentType
val CSS: ContentType
val CSV: ContentType
val Html: ContentType
val JavaScript: ContentType
val VCard: ContentType
val Xml: ContentType
val EventStream: ContentType
/**
* Check if content type is within Text category
*/
fun contains(contentType: ContentType): Boolean
fun contains(contentType: CharSequence): Boolean
}Image content types for various image formats.
object ContentType.Image {
val Any: ContentType
val GIF: ContentType
val JPEG: ContentType
val PNG: ContentType
val SVG: ContentType
val XIcon: ContentType
/**
* Check if content type is within Image category
*/
fun contains(contentType: ContentType): Boolean
fun contains(contentType: String): Boolean
}Audio content types for multimedia content.
object ContentType.Audio {
val Any: ContentType
val MPEG: ContentType
val MP4: ContentType
val OGG: ContentType
/**
* Check if content type is within Audio category
*/
fun contains(contentType: ContentType): Boolean
fun contains(contentType: CharSequence): Boolean
}Video content types for multimedia content.
object ContentType.Video {
val Any: ContentType
val MPEG: ContentType
val MP4: ContentType
val OGG: ContentType
val QuickTime: ContentType
/**
* Check if content type is within Video category
*/
fun contains(contentType: ContentType): Boolean
fun contains(contentType: CharSequence): Boolean
}Font content types for web fonts.
object ContentType.Font {
val Any: ContentType
val Collection: ContentType
val Otf: ContentType
val Sfnt: ContentType
val Ttf: ContentType
val Woff: ContentType
val Woff2: ContentType
/**
* Check if content type is within Font category
*/
fun contains(contentType: ContentType): Boolean
fun contains(contentType: CharSequence): Boolean
}MultiPart content types for form data and mixed content.
object ContentType.MultiPart {
val Any: ContentType
val Mixed: ContentType
val Alternative: ContentType
val Related: ContentType
val FormData: ContentType
val Signed: ContentType
val Encrypted: ContentType
val ByteRanges: ContentType
/**
* Check if content type is within MultiPart category
*/
fun contains(contentType: ContentType): Boolean
fun contains(contentType: CharSequence): Boolean
}Message content types for HTTP and email protocols.
object ContentType.Message {
val Any: ContentType
val Http: ContentType
/**
* Check if content type is within Message category
*/
fun contains(contentType: ContentType): Boolean
fun contains(contentType: String): Boolean
}Automatic content type detection based on file extensions and paths.
/**
* Get default content type for file (JVM-specific)
* @param file file to analyze
* @return ContentType based on file extension
*/
fun ContentType.Companion.defaultForFile(file: File): ContentType
/**
* Get default content type for path (JVM-specific)
* @param path path to analyze
* @return ContentType based on path extension
*/
fun ContentType.Companion.defaultForPath(path: Path): ContentType
/**
* Get default content type for file extension
* @param extension file extension (without dot)
* @return ContentType for extension
*/
fun ContentType.Companion.defaultForFileExtension(extension: String): ContentType
/**
* Get default content type for file path
* @param filePath file path string
* @return ContentType based on path extension
*/
fun ContentType.Companion.defaultForFilePath(filePath: String): ContentType
/**
* Get file extensions for content type
* @return List of file extensions for this content type
*/
fun ContentType.fileExtensions(): List<String>
/**
* Get content types for file extension
* @param extension file extension
* @return List of possible content types
*/
fun ContentType.Companion.fromFileExtension(extension: String): List<ContentType>
/**
* Get content types for file path
* @param filePath file path
* @return List of possible content types
*/
fun ContentType.Companion.fromFilePath(filePath: String): List<ContentType>Interface for content type matching logic.
/**
* Interface for matching content types
*/
interface ContentTypeMatcher {
/**
* Check if content type matches
* @param contentType content type to check
* @return true if matches
*/
fun contains(contentType: ContentType): Boolean
}Usage Examples:
import io.ktor.http.*
import java.io.File
import java.nio.file.Paths
// Use predefined content types
val jsonType = ContentType.Application.Json
val htmlType = ContentType.Text.Html
val pngType = ContentType.Image.PNG
// Create with parameters
val jsonUtf8 = ContentType.Application.Json.withCharset(Charsets.UTF_8)
val customText = ContentType.Text.Plain.withParameter("format", "structured")
// Parse from strings
val parsed = ContentType.parse("application/json; charset=utf-8")
val charset = parsed.charset() // UTF-8
// File type detection (JVM-specific)
val imageFile = File("profile.png")
val imageType = ContentType.defaultForFile(imageFile) // image/png
val cssPath = Paths.get("styles.css")
val cssType = ContentType.defaultForPath(cssPath) // text/css
val docType = ContentType.defaultForFileExtension("pdf") // application/pdf
val webType = ContentType.defaultForFilePath("/assets/script.js") // application/javascript
// Get extensions for content type
val jsonExtensions = ContentType.Application.Json.fileExtensions() // [json]
// Find content types for extension
val htmlTypes = ContentType.fromFileExtension("html") // [text/html]
// Content type matching
if (ContentType.Application.contains(receivedType)) {
// Handle application content
}
if (jsonType.match(ContentType.Application.Json)) {
// Types match exactly
}
if (jsonType.match("application/*")) {
// Pattern matching
}
// Remove parameters
val baseType = jsonUtf8.withoutParameters() // application/json without charset/**
* Exception thrown for invalid content type format
*/
class BadContentTypeFormatException(message: String) : ExceptionInstall with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-http-jvm