Skip to content

Instantly share code, notes, and snippets.

@osfunapps
Created June 8, 2020 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osfunapps/e56a0f7afda0f0b3f97908271d8eebd0 to your computer and use it in GitHub Desktop.
Save osfunapps/e56a0f7afda0f0b3f97908271d8eebd0 to your computer and use it in GitHub Desktop.
a simple class to turn a raw http res to an object with status, reason and headers
class HttpResParser {
companion object {
fun parseRawHttpStringRes(httpRes: ByteArray): HttpRes {
val br = ByteArrayInputStream(httpRes).bufferedReader()
val version: String
val statusCode: String
val reason: String
var splatHeader: List<String>
val headersMap = HashMap<String, String>()
val line = br.readLine()
val splatLine = line.split(' ')
version = splatLine[0]
statusCode = splatLine[1]
reason = splatLine[2]
while(br.ready()) {
splatHeader = br.readLine().split(':')
if(splatHeader.size == 2) { // a fail safe mechanism for extra lines in the response
headersMap[splatHeader[0]] = splatHeader[1]
}
}
return HttpRes(version, statusCode.toInt(), reason, headersMap)
}
}
}
data class HttpRes(val version: String, val statusCode: Int, val reason: String, val headers: HashMap<String, String>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment