Update HttpSession.kt

This commit is contained in:
Arjix 2021-09-02 15:56:47 +03:00 committed by GitHub
parent 51d4d296fc
commit 23c7f91214
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,6 +4,7 @@ package com.lagradost.cloudstream3.utils
import khttp.structures.authorization.Authorization import khttp.structures.authorization.Authorization
import khttp.structures.cookie.CookieJar import khttp.structures.cookie.CookieJar
import khttp.structures.files.FileLike import khttp.structures.files.FileLike
import khttp.structures.cookie.Cookie
import khttp.responses.Response import khttp.responses.Response
@ -18,21 +19,6 @@ class HttpSession {
companion object { companion object {
const val DEFAULT_TIMEOUT = 30.0 const val DEFAULT_TIMEOUT = 30.0
fun cookieStrToMap(cookie: String): Map<String, String> {
val cookies = mutableMapOf<String, String>()
for (string in cookie.split("; ")) {
val split = string.split("=").toMutableList()
val name = split.removeFirst().trim()
val value = if (split.size == 0) {
"true"
} else {
split.joinToString("=")
}
cookies[name] = value
}
return cookies.toMap()
}
fun mergeCookies(cookie1: Map<String, String>, cookie2: Map<String, String>?): Map<String, String> { fun mergeCookies(cookie1: Map<String, String>, cookie2: Map<String, String>?): Map<String, String> {
val a = cookie1.toMutableMap() val a = cookie1.toMutableMap()
if (!cookie2.isNullOrEmpty()) { if (!cookie2.isNullOrEmpty()) {
@ -57,14 +43,12 @@ class HttpSession {
val res = khttp.get( val res = khttp.get(
url, headers, params, url, headers, params,
data, json, auth, data, json, auth,
mergeCookies(sessionCookies, cookies), timeout, mergeCookies(sessionCookies, cookies),
allowRedirects, timeout, allowRedirects,
stream, files stream, files
) )
sessionCookies.putAll(res.cookies.toMap()) sessionCookies.putAll(res.cookies)
if (res.headers.containsKey("set-cookie")) { sessionCookies.putAll(CookieJar(*res.headers.filter { it.key.toLowerCase() == "set-cookie" }.map { Cookie(it.value) }.toTypedArray()))
sessionCookies.putAll(cookieStrToMap(res.headers["set-cookie"].toString().replace("path=/,", "")))
}
return res return res
} }
@ -81,14 +65,12 @@ class HttpSession {
val res = khttp.post( val res = khttp.post(
url, headers, params, url, headers, params,
data, json, auth, data, json, auth,
mergeCookies(sessionCookies, cookies), timeout, mergeCookies(sessionCookies, cookies),
allowRedirects, timeout, allowRedirects,
stream, files stream, files
) )
sessionCookies.putAll(res.cookies.toMap()) sessionCookies.putAll(res.cookies)
if (res.headers.containsKey("set-cookie")) { sessionCookies.putAll(CookieJar(*res.headers.filter { it.key.toLowerCase() == "set-cookie" }.map { Cookie(it.value) }.toTypedArray()))
sessionCookies.putAll(cookieStrToMap(res.headers["set-cookie"].toString().replace("path=/,", "")))
}
return res return res
} }
} }