ariane/app/src/main/java/oppen/ariane/io/gemini/GeminiResponse.kt

78 lines
2.1 KiB
Kotlin
Raw Normal View History

package oppen.ariane.io.gemini
2020-08-18 16:17:04 +00:00
object GeminiResponse {
const val INPUT = 1
const val SUCCESS = 2
const val REDIRECT = 3
const val TEMPORARY_FAILURE = 4
const val PERMANENT_FAILURE = 5
const val CLIENT_CERTIFICATE_REQUIRED = 6
const val UNKNOWN = -1
2020-08-21 15:12:00 +00:00
fun parseHeader(header: String): Header {
val cleanHeader = header.replace("\\s+".toRegex(), " ")
val meta: String
2020-08-18 19:06:08 +00:00
when {
header.startsWith("2") -> {
2020-08-18 19:29:16 +00:00
val segments = cleanHeader.trim().split(" ")
2020-08-18 19:06:08 +00:00
meta = when {
segments.size > 1 -> segments[1]
else -> "text/gemini; charset=utf-8"
}
2020-08-18 19:02:16 +00:00
}
2020-08-18 19:06:08 +00:00
else -> {
2020-08-18 19:29:16 +00:00
2020-08-18 19:06:08 +00:00
meta = when {
2020-08-18 19:29:16 +00:00
cleanHeader.contains(" ") -> cleanHeader.substring(cleanHeader.indexOf(" ") + 1)
else -> cleanHeader
2020-08-18 19:06:08 +00:00
}
2020-08-18 19:02:16 +00:00
}
2020-08-18 16:17:04 +00:00
}
2020-08-18 19:02:16 +00:00
2020-08-18 16:17:04 +00:00
return when {
2020-08-21 15:12:00 +00:00
header.startsWith("1") -> Header(
INPUT,
meta
)
header.startsWith("2") -> Header(
SUCCESS,
meta
)
header.startsWith("3") -> Header(
REDIRECT,
meta
)
header.startsWith("4") -> Header(
TEMPORARY_FAILURE,
meta
)
header.startsWith("5") -> Header(
PERMANENT_FAILURE,
meta
)
header.startsWith("6") -> Header(
CLIENT_CERTIFICATE_REQUIRED,
meta
)
else -> Header(
UNKNOWN,
meta
)
2020-08-18 16:17:04 +00:00
}
}
2020-08-18 19:02:16 +00:00
fun getCodeString(code: Int): String{
return when(code){
1 -> "Input"
2 -> "Success"
3 -> "Redirect"
4 -> "Temporary Failure"
5 -> "Permanent Failure"
6 -> "Client Certificate Required"
else -> "Unknown: $code"
}
}
2020-08-18 16:17:04 +00:00
class Header(val code: Int, val meta: String)
}