mirror of
https://github.com/square/okhttp.git
synced 2025-08-06 01:35:50 +03:00
Reformat with Spotless (#8180)
* Enable spotless * Run spotlessApply * Fixup trimMargin * Re-run spotlessApply
This commit is contained in:
@@ -14,8 +14,8 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
package okhttp3.java.net.cookiejar
|
||||
|
||||
package okhttp3.java.net.cookiejar
|
||||
|
||||
import java.io.IOException
|
||||
import java.net.CookieHandler
|
||||
@@ -32,8 +32,10 @@ import okhttp3.internal.trimSubstring
|
||||
|
||||
/** A cookie jar that delegates to a [java.net.CookieHandler]. */
|
||||
class JavaNetCookieJar(private val cookieHandler: CookieHandler) : CookieJar {
|
||||
|
||||
override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
|
||||
override fun saveFromResponse(
|
||||
url: HttpUrl,
|
||||
cookies: List<Cookie>,
|
||||
) {
|
||||
val cookieStrings = mutableListOf<String>()
|
||||
for (cookie in cookies) {
|
||||
cookieStrings.add(cookieToString(cookie, true))
|
||||
@@ -47,18 +49,20 @@ class JavaNetCookieJar(private val cookieHandler: CookieHandler) : CookieJar {
|
||||
}
|
||||
|
||||
override fun loadForRequest(url: HttpUrl): List<Cookie> {
|
||||
val cookieHeaders = try {
|
||||
// The RI passes all headers. We don't have 'em, so we don't pass 'em!
|
||||
cookieHandler.get(url.toUri(), emptyMap<String, List<String>>())
|
||||
} catch (e: IOException) {
|
||||
Platform.get().log("Loading cookies failed for " + url.resolve("/...")!!, WARN, e)
|
||||
return emptyList()
|
||||
}
|
||||
val cookieHeaders =
|
||||
try {
|
||||
// The RI passes all headers. We don't have 'em, so we don't pass 'em!
|
||||
cookieHandler.get(url.toUri(), emptyMap<String, List<String>>())
|
||||
} catch (e: IOException) {
|
||||
Platform.get().log("Loading cookies failed for " + url.resolve("/...")!!, WARN, e)
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
var cookies: MutableList<Cookie>? = null
|
||||
for ((key, value) in cookieHeaders) {
|
||||
if (("Cookie".equals(key, ignoreCase = true) || "Cookie2".equals(key, ignoreCase = true)) &&
|
||||
value.isNotEmpty()) {
|
||||
value.isNotEmpty()
|
||||
) {
|
||||
for (header in value) {
|
||||
if (cookies == null) cookies = mutableListOf()
|
||||
cookies.addAll(decodeHeaderAsJavaNetCookies(url, header))
|
||||
@@ -77,7 +81,10 @@ class JavaNetCookieJar(private val cookieHandler: CookieHandler) : CookieJar {
|
||||
* Convert a request header to OkHttp's cookies via [HttpCookie]. That extra step handles
|
||||
* multiple cookies in a single request header, which [Cookie.parse] doesn't support.
|
||||
*/
|
||||
private fun decodeHeaderAsJavaNetCookies(url: HttpUrl, header: String): List<Cookie> {
|
||||
private fun decodeHeaderAsJavaNetCookies(
|
||||
url: HttpUrl,
|
||||
header: String,
|
||||
): List<Cookie> {
|
||||
val result = mutableListOf<Cookie>()
|
||||
var pos = 0
|
||||
val limit = header.length
|
||||
@@ -92,22 +99,25 @@ class JavaNetCookieJar(private val cookieHandler: CookieHandler) : CookieJar {
|
||||
}
|
||||
|
||||
// We have either name=value or just a name.
|
||||
var value = if (equalsSign < pairEnd) {
|
||||
header.trimSubstring(equalsSign + 1, pairEnd)
|
||||
} else {
|
||||
""
|
||||
}
|
||||
var value =
|
||||
if (equalsSign < pairEnd) {
|
||||
header.trimSubstring(equalsSign + 1, pairEnd)
|
||||
} else {
|
||||
""
|
||||
}
|
||||
|
||||
// If the value is "quoted", drop the quotes.
|
||||
if (value.startsWith("\"") && value.endsWith("\"") && value.length >= 2) {
|
||||
value = value.substring(1, value.length - 1)
|
||||
}
|
||||
|
||||
result.add(Cookie.Builder()
|
||||
result.add(
|
||||
Cookie.Builder()
|
||||
.name(name)
|
||||
.value(value)
|
||||
.domain(url.host)
|
||||
.build())
|
||||
.build(),
|
||||
)
|
||||
pos = pairEnd + 1
|
||||
}
|
||||
return result
|
||||
|
Reference in New Issue
Block a user