HTTP utilities for Ktor framework - iOS ARM64 variant providing URL building, content types, headers, and HTTP message handling.
—
Rich content type system with MIME type support, character encoding, parameter handling, and comprehensive predefined content types for web and mobile applications.
class ContentType(
val contentType: String,
val contentSubtype: String,
val parameters: List<HeaderValueParam>
) {
override fun toString(): String
override fun equals(other: Any?): Boolean
override fun hashCode(): Int
fun withParameter(name: String, value: String): ContentType
fun withoutParameters(): ContentType
fun match(pattern: ContentType): Boolean
fun match(pattern: String): Boolean
}class ContentType {
companion object {
fun parse(value: String): ContentType
val Any: ContentType // */*
}
}object ContentType.Application {
val Any: ContentType // application/*
val Atom: ContentType // application/atom+xml
val JavaScript: ContentType // application/javascript
val Json: ContentType // application/json
val OctetStream: ContentType // application/octet-stream
val FontWoff: ContentType // application/font-woff
val Rss: ContentType // application/rss+xml
val Xml: ContentType // application/xml
val Zip: ContentType // application/zip
val GZip: ContentType // application/gzip
val FormUrlEncoded: ContentType // application/x-www-form-urlencoded
val Pdf: ContentType // application/pdf
val ProtoBuf: ContentType // application/x-protobuf
val Wasm: ContentType // application/wasm
val ProblemJson: ContentType // application/problem+json
val ProblemXml: ContentType // application/problem+xml
}object ContentType.Audio {
val Any: ContentType // audio/*
val MP4: ContentType // audio/mp4
val MPEG: ContentType // audio/mpeg
val OGG: ContentType // audio/ogg
}object ContentType.Image {
val Any: ContentType // image/*
val GIF: ContentType // image/gif
val JPEG: ContentType // image/jpeg
val PNG: ContentType // image/png
val SVG: ContentType // image/svg+xml
val XIcon: ContentType // image/x-icon
val WebP: ContentType // image/webp
}object ContentType.Message {
val Any: ContentType // message/*
val Http: ContentType // message/http
}object ContentType.MultiPart {
val Any: ContentType // multipart/*
val Mixed: ContentType // multipart/mixed
val Alternative: ContentType // multipart/alternative
val Related: ContentType // multipart/related
val FormData: ContentType // multipart/form-data
val Signed: ContentType // multipart/signed
val Encrypted: ContentType // multipart/encrypted
val ByteRanges: ContentType // multipart/byteranges
}object ContentType.Text {
val Any: ContentType // text/*
val Plain: ContentType // text/plain
val CSS: ContentType // text/css
val CSV: ContentType // text/csv
val Html: ContentType // text/html
val JavaScript: ContentType // text/javascript
val VCard: ContentType // text/vcard
val Xml: ContentType // text/xml
val EventStream: ContentType // text/event-stream
val Markdown: ContentType // text/markdown
}object ContentType.Video {
val Any: ContentType // video/*
val MPEG: ContentType // video/mpeg
val MP4: ContentType // video/mp4
val OGG: ContentType // video/ogg
val QuickTime: ContentType // video/quicktime
}object ContentType.Font {
val Any: ContentType // font/*
val Collection: ContentType // font/collection
val Otf: ContentType // font/otf
val Sfnt: ContentType // font/sfnt
val Ttf: ContentType // font/ttf
val Woff: ContentType // font/woff
val Woff2: ContentType // font/woff2
}fun ContentType.withCharset(charset: Charset): ContentType
fun ContentType.withCharsetIfNeeded(charset: Charset): ContentType
fun HeaderValueWithParameters.charset(): Charset?interface ContentTypeMatcher {
fun contains(contentType: ContentType): Boolean
}data class HeaderValueParam(val name: String, val value: String)abstract class HeaderValueWithParameters(
protected val content: String,
val parameters: List<HeaderValueParam>
) {
fun parameter(name: String): String?
fun charset(): Charset?
override fun toString(): String
}// Using predefined content types
val jsonType = ContentType.Application.Json
val htmlType = ContentType.Text.Html
val pngType = ContentType.Image.PNG
println(jsonType) // application/json
println(htmlType) // text/html
println(pngType) // image/png
// Parsing content types from strings
val parsedType = ContentType.parse("application/json; charset=UTF-8")
println("Type: ${parsedType.contentType}") // application
println("Subtype: ${parsedType.contentSubtype}") // json
println("Parameters: ${parsedType.parameters}") // [HeaderValueParam(charset, UTF-8)]// Adding parameters to content types
val jsonWithCharset = ContentType.Application.Json
.withParameter("charset", "UTF-8")
val xmlWithEncoding = ContentType.Application.Xml
.withParameter("charset", "UTF-8")
.withParameter("boundary", "----WebKitFormBoundary7MA4YWxkTrZu0gW")
println(jsonWithCharset) // application/json; charset=UTF-8
println(xmlWithEncoding) // application/xml; charset=UTF-8; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
// Removing parameters
val plainJson = jsonWithCharset.withoutParameters()
println(plainJson) // application/json// Adding charset with convenience methods
val htmlUtf8 = ContentType.Text.Html.withCharset(Charsets.UTF_8)
val plainAscii = ContentType.Text.Plain.withCharset(Charsets.US_ASCII)
println(htmlUtf8) // text/html; charset=UTF-8
println(plainAscii) // text/plain; charset=US-ASCII
// Conditional charset addition
val maybeUtf8 = ContentType.Application.Json.withCharsetIfNeeded(Charsets.UTF_8)
val alreadyHasCharset = ContentType.parse("text/plain; charset=ISO-8859-1")
val stillIso = alreadyHasCharset.withCharsetIfNeeded(Charsets.UTF_8)
println(maybeUtf8) // application/json; charset=UTF-8
println(stillIso) // text/plain; charset=ISO-8859-1
// Extracting charset
val charset = htmlUtf8.charset()
println(charset) // UTF-8// Exact matching
val isJson = ContentType.Application.Json == ContentType.parse("application/json")
println(isJson) // true
// Pattern matching with wildcards
val anyApplication = ContentType.Application.Any
val specificJson = ContentType.Application.Json
val matches = specificJson.match(anyApplication)
println(matches) // true
val anyContentType = ContentType.Any
val matchesAny = specificJson.match(anyContentType)
println(matchesAny) // true
// Custom content type matcher
class JsonMatcher : ContentTypeMatcher {
override fun contains(contentType: ContentType): Boolean {
return contentType.contentType == "application" &&
(contentType.contentSubtype == "json" ||
contentType.contentSubtype.endsWith("+json"))
}
}
val matcher = JsonMatcher()
println(matcher.contains(ContentType.Application.Json)) // true
println(matcher.contains(ContentType.Application.ProblemJson)) // true
println(matcher.contains(ContentType.Text.Html)) // false// Creating multipart content types
val formData = ContentType.MultiPart.FormData
.withParameter("boundary", "----formdata-boundary-${System.currentTimeMillis()}")
val mixedContent = ContentType.MultiPart.Mixed
.withParameter("boundary", "----mixed-boundary-12345")
println(formData)
// multipart/form-data; boundary=----formdata-boundary-1234567890
// Working with form data boundaries
fun createFormDataContentType(): ContentType {
val boundary = "----WebKitFormBoundary" +
(1..16).joinToString("") {
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
.random().toString()
}
return ContentType.MultiPart.FormData.withParameter("boundary", boundary)
}
val dynamicFormData = createFormDataContentType()// Creating custom content types
val customType = ContentType("application", "vnd.myapp+json")
.withParameter("version", "1.0")
.withParameter("charset", "UTF-8")
println(customType) // application/vnd.myapp+json; version=1.0; charset=UTF-8
// Vendor-specific content types
val vendorXml = ContentType("application", "vnd.api+xml")
val vendorJson = ContentType("application", "vnd.api+json")
// API versioning with content types
fun createVersionedContentType(version: String): ContentType {
return ContentType.Application.Json
.withParameter("version", version)
.withParameter("charset", "UTF-8")
}
val v1Api = createVersionedContentType("1.0")
val v2Api = createVersionedContentType("2.0")// Helper functions for common patterns
fun isTextContent(contentType: ContentType): Boolean {
return contentType.contentType == "text" ||
contentType.match(ContentType.Application.Json) ||
contentType.match(ContentType.Application.Xml)
}
fun isBinaryContent(contentType: ContentType): Boolean {
return contentType.contentType in setOf("image", "video", "audio") ||
contentType.match(ContentType.Application.OctetStream)
}
fun requiresCharset(contentType: ContentType): Boolean {
return contentType.contentType == "text" ||
contentType.match(ContentType.Application.Json) ||
contentType.match(ContentType.Application.Xml)
}
// Usage
val htmlType = ContentType.Text.Html
val imageType = ContentType.Image.PNG
val jsonType = ContentType.Application.Json
println("HTML is text: ${isTextContent(htmlType)}") // true
println("PNG is binary: ${isBinaryContent(imageType)}") // true
println("JSON requires charset: ${requiresCharset(jsonType)}") // truetry {
val invalidType = ContentType.parse("invalid/content/type/format")
} catch (e: BadContentTypeFormatException) {
println("Invalid content type format: ${e.message}")
}
// Safe parsing with fallback
fun parseContentTypeSafe(value: String): ContentType {
return try {
ContentType.parse(value)
} catch (e: BadContentTypeFormatException) {
ContentType.Application.OctetStream // fallback
}
}
val safeType = parseContentTypeSafe("malformed-content-type")
println(safeType) // application/octet-streamInstall with Tessl CLI
npx tessl i tessl/gradle-io-ktor--ktor-http-iosarm64