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.cookie.CookieJar
import khttp.structures.files.FileLike
import khttp.structures.cookie.Cookie
import khttp.responses.Response
@ -18,21 +19,6 @@ class HttpSession {
companion object {
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> {
val a = cookie1.toMutableMap()
if (!cookie2.isNullOrEmpty()) {
@ -57,14 +43,12 @@ class HttpSession {
val res = khttp.get(
url, headers, params,
data, json, auth,
mergeCookies(sessionCookies, cookies), timeout,
allowRedirects,
mergeCookies(sessionCookies, cookies),
timeout, allowRedirects,
stream, files
)
sessionCookies.putAll(res.cookies.toMap())
if (res.headers.containsKey("set-cookie")) {
sessionCookies.putAll(cookieStrToMap(res.headers["set-cookie"].toString().replace("path=/,", "")))
}
sessionCookies.putAll(res.cookies)
sessionCookies.putAll(CookieJar(*res.headers.filter { it.key.toLowerCase() == "set-cookie" }.map { Cookie(it.value) }.toTypedArray()))
return res
}
@ -81,14 +65,12 @@ class HttpSession {
val res = khttp.post(
url, headers, params,
data, json, auth,
mergeCookies(sessionCookies, cookies), timeout,
allowRedirects,
mergeCookies(sessionCookies, cookies),
timeout, allowRedirects,
stream, files
)
sessionCookies.putAll(res.cookies.toMap())
if (res.headers.containsKey("set-cookie")) {
sessionCookies.putAll(cookieStrToMap(res.headers["set-cookie"].toString().replace("path=/,", "")))
}
sessionCookies.putAll(res.cookies)
sessionCookies.putAll(CookieJar(*res.headers.filter { it.key.toLowerCase() == "set-cookie" }.map { Cookie(it.value) }.toTypedArray()))
return res
}
}