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

57 lines
1.7 KiB
Kotlin
Raw Normal View History

2020-08-18 16:17:04 +00:00
package oppen.tva.io
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
fun parseHeader(header: String): Header{
2020-08-18 19:29:16 +00:00
var cleanHeader = header.replace("\\s+".toRegex(), " ")
2020-08-18 19:02:16 +00:00
var meta = ""
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-18 19:02:16 +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)
2020-08-18 16:17:04 +00:00
else -> Header(UNKNOWN, meta)
}
}
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)
}