Created
June 8, 2020 12:09
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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