Ktor HTTP library for iOS ARM64 providing HTTP utilities, content types, headers manipulation, URL building, and HTTP message handling for iOS ARM64 applications built with Kotlin Multiplatform.
—
Comprehensive content type definitions with MIME type constants, parameter handling, pattern matching, and file extension mapping for all common content types.
Content type representation with parameter support and pattern matching capabilities.
/**
* Content type representation with main type, subtype, and parameters
* @param contentType main type (e.g., "application", "text")
* @param contentSubtype subtype (e.g., "json", "html")
* @param parameters list of content type parameters
*/
class ContentType(
val contentType: String,
val contentSubtype: String,
val parameters: List<HeaderValueParam> = emptyList()
) {
/**
* Add a parameter to content type
* @param name parameter name
* @param value parameter value
* @return new ContentType with added parameter
*/
fun withParameter(name: String, value: String): ContentType
/**
* Remove all parameters from content type
* @return new ContentType without parameters
*/
fun withoutParameters(): ContentType
/**
* Check if this content type matches a pattern
* @param pattern content type pattern to match against
* @return true if matches (supports wildcards)
*/
fun match(pattern: ContentType): Boolean
/**
* Check if this content type matches a string pattern
* @param pattern string pattern to match against
* @return true if matches
*/
fun match(pattern: String): Boolean
companion object {
/**
* Parse Content-Type string
* @param value content type string to parse
* @return ContentType instance
*/
fun parse(value: String): ContentType
/** Wildcard content type matching any type */
val Any: ContentType // */*
}
}Content types for application data formats.
object Application {
/** application/* wildcard */
val Any: ContentType
/** application/atom+xml */
val Atom: ContentType
/** application/cbor */
val Cbor: ContentType
/** application/json */
val Json: ContentType
/** application/hal+json */
val HalJson: ContentType
/** application/javascript */
val JavaScript: ContentType
/** application/octet-stream */
val OctetStream: ContentType
/** application/rss+xml */
val Rss: ContentType
/** application/xml */
val Xml: ContentType
/** application/xml-dtd */
val Xml_Dtd: ContentType
/** application/zip */
val Zip: ContentType
/** application/gzip */
val GZip: ContentType
/** application/x-www-form-urlencoded */
val FormUrlEncoded: ContentType
/** application/pdf */
val Pdf: ContentType
/** application/vnd.openxmlformats-officedocument.spreadsheetml.sheet */
val Xlsx: ContentType
/** application/vnd.openxmlformats-officedocument.wordprocessingml.document */
val Docx: ContentType
/** application/vnd.openxmlformats-officedocument.presentationml.presentation */
val Pptx: ContentType
/** application/protobuf */
val ProtoBuf: ContentType
/** application/wasm */
val Wasm: ContentType
/** application/problem+json */
val ProblemJson: ContentType
/** application/problem+xml */
val ProblemXml: ContentType
}Content types for text-based formats.
object Text {
/** text/* wildcard */
val Any: ContentType
/** text/plain */
val Plain: ContentType
/** text/css */
val CSS: ContentType
/** text/csv */
val CSV: ContentType
/** text/html */
val Html: ContentType
/** text/javascript */
val JavaScript: ContentType
/** text/vcard */
val VCard: ContentType
/** text/xml */
val Xml: ContentType
/** text/event-stream */
val EventStream: ContentType
}Content types for image formats.
object Image {
/** image/* wildcard */
val Any: ContentType
/** image/gif */
val GIF: ContentType
/** image/jpeg */
val JPEG: ContentType
/** image/png */
val PNG: ContentType
/** image/svg+xml */
val SVG: ContentType
/** image/x-icon */
val XIcon: ContentType
}Content types for audio formats.
object Audio {
/** audio/* wildcard */
val Any: ContentType
/** audio/mp4 */
val MP4: ContentType
/** audio/mpeg */
val MPEG: ContentType
/** audio/ogg */
val OGG: ContentType
}Content types for video formats.
object Video {
/** video/* wildcard */
val Any: ContentType
/** video/mpeg */
val MPEG: ContentType
/** video/mp4 */
val MP4: ContentType
/** video/ogg */
val OGG: ContentType
/** video/quicktime */
val QuickTime: ContentType
}Content types for multipart data.
object MultiPart {
/** multipart/* wildcard */
val Any: ContentType
/** multipart/mixed */
val Mixed: ContentType
/** multipart/alternative */
val Alternative: ContentType
/** multipart/related */
val Related: ContentType
/** multipart/form-data */
val FormData: ContentType
/** multipart/signed */
val Signed: ContentType
/** multipart/encrypted */
val Encrypted: ContentType
/** multipart/byteranges */
val ByteRanges: ContentType
}Content types for font files.
object Font {
/** font/* wildcard */
val Any: ContentType
/** font/collection */
val Collection: ContentType
/** font/otf */
val Otf: ContentType
/** font/sfnt */
val Sfnt: ContentType
/** font/ttf */
val Ttf: ContentType
/** font/woff */
val Woff: ContentType
/** font/woff2 */
val Woff2: ContentType
}Content types for message formats.
object Message {
/** message/* wildcard */
val Any: ContentType
/** message/http */
val Http: ContentType
}Extension functions for charset handling and content type utilities.
/**
* Add charset parameter to content type
* @param charset the charset to add
* @return new ContentType with charset parameter
*/
fun ContentType.withCharset(charset: Charset): ContentType
/**
* Add charset parameter only if not already present
* @param charset the charset to add if needed
* @return ContentType with charset parameter
*/
fun ContentType.withCharsetIfNeeded(charset: Charset): ContentType
/**
* Extract charset parameter from content type
* @return Charset if present, null otherwise
*/
fun ContentType.charset(): Charset?Functions for mapping file extensions to content types.
/**
* Get default content type for file extension
* @param extension file extension (without dot)
* @return default ContentType for extension
*/
fun ContentType.Companion.defaultForFileExtension(extension: String): ContentType
/**
* Get default content type for file path
* @param path file path
* @return default ContentType for file
*/
fun ContentType.Companion.defaultForFilePath(path: String): ContentType
/**
* Get recommended content types for file path
* @param path file path
* @return list of possible ContentTypes
*/
fun ContentType.Companion.fromFilePath(path: String): List<ContentType>
/**
* Get content types for file extension
* @param ext file extension
* @return list of possible ContentTypes
*/
fun ContentType.Companion.fromFileExtension(ext: String): List<ContentType>
/**
* Get file extensions for content type
* @return list of common file extensions
*/
fun ContentType.fileExtensions(): List<String>Exception thrown for malformed content type strings.
/**
* Exception thrown when content type format is invalid
* @param value the malformed content type string
*/
class BadContentTypeFormatException(val value: String) : ExceptionUsage Examples:
import io.ktor.http.*
// Use predefined content types
val jsonType = ContentType.Application.Json
val htmlType = ContentType.Text.Html
val imageType = ContentType.Image.PNG
// Add parameters to content types
val jsonWithCharset = ContentType.Application.Json.withCharset(Charsets.UTF_8)
val customType = ContentType.Text.Plain.withParameter("boundary", "abc123")
// Parse content type from string
val parsedType = ContentType.parse("application/json; charset=utf-8")
println(parsedType.contentType) // "application"
println(parsedType.contentSubtype) // "json"
println(parsedType.charset()) // UTF_8
// Pattern matching
val isJson = ContentType.Application.Json.match("application/json")
val isAnyApplication = ContentType.Application.Json.match(ContentType.Application.Any)
val isAnyType = ContentType.Text.Plain.match(ContentType.Any)
// Remove parameters
val plainJson = jsonWithCharset.withoutParameters()
// File extension mapping
val pdfType = ContentType.defaultForFileExtension("pdf")
val htmlTypeFromPath = ContentType.defaultForFilePath("/path/to/file.html")
val imageTypes = ContentType.fromFileExtension("jpg") // List of possible types
// Get extensions for content type
val jsonExtensions = ContentType.Application.Json.fileExtensions()
println(jsonExtensions) // [json]
// Work with multipart types
val formDataType = ContentType.MultiPart.FormData
val mixedType = ContentType.MultiPart.Mixed
// Handle complex content types
val complexType = ContentType.parse("multipart/form-data; boundary=----WebKit123")
println(complexType.parameters.find { it.name == "boundary" }?.value)
// Content type comparison and matching
when {
ContentType.Application.Json.match(receivedType) -> {
// Handle JSON
}
ContentType.Text.Any.match(receivedType) -> {
// Handle any text type
}
ContentType.Image.Any.match(receivedType) -> {
// Handle any image type
}
}
// Error handling
try {
ContentType.parse("invalid/content/type/format")
} catch (e: BadContentTypeFormatException) {
println("Invalid content type: ${e.value}")
}/**
* Content type parameter representation
* @param name parameter name
* @param value parameter value
*/
class HeaderValueParam(val name: String, val value: String)Install with Tessl CLI
npx tessl i tessl/kotlin-io-ktor--ktor-http-iosarm64