mirror of
https://github.com/square/okhttp.git
synced 2025-08-06 01:35:50 +03:00
Make a JPMS-compatible JavaNetCookieJar (#8074)
* Make a JPMS-compatible JavaNetCookieJar * Add missing imports * Depend on okhttpUrlconnection This is necessary to pass the OSGi test.
This commit is contained in:
@@ -22,7 +22,7 @@ dependencies {
|
||||
implementation(libs.assertj.core)
|
||||
implementation(projects.mockwebserver3)
|
||||
implementation(projects.mockwebserver)
|
||||
implementation(projects.okhttpUrlconnection)
|
||||
implementation(projects.okhttpJavaNetCookiejar)
|
||||
implementation(projects.mockwebserver3Junit4)
|
||||
implementation(projects.mockwebserver3Junit5)
|
||||
implementation(libs.aqute.resolve)
|
||||
|
11
okhttp-java-net-cookiejar/README.md
Normal file
11
okhttp-java-net-cookiejar/README.md
Normal file
@@ -0,0 +1,11 @@
|
||||
OkHttp java.net.CookieHandler
|
||||
=============================
|
||||
|
||||
This module integrates OkHttp with `CookieHandler` from `java.net`.
|
||||
This used to be part of `okhttp-urlconnection`
|
||||
|
||||
### Download
|
||||
|
||||
```kotlin
|
||||
testImplementation("com.squareup.okhttp3:okhttp-java-net-cookiehandler:4.11.0")
|
||||
```
|
@@ -0,0 +1,6 @@
|
||||
public final class okhttp3/java/net/cookiejar/JavaNetCookieJar : okhttp3/CookieJar {
|
||||
public fun <init> (Ljava/net/CookieHandler;)V
|
||||
public fun loadForRequest (Lokhttp3/HttpUrl;)Ljava/util/List;
|
||||
public fun saveFromResponse (Lokhttp3/HttpUrl;Ljava/util/List;)V
|
||||
}
|
||||
|
25
okhttp-java-net-cookiejar/build.gradle.kts
Normal file
25
okhttp-java-net-cookiejar/build.gradle.kts
Normal file
@@ -0,0 +1,25 @@
|
||||
import com.vanniktech.maven.publish.JavadocJar
|
||||
import com.vanniktech.maven.publish.KotlinJvm
|
||||
|
||||
plugins {
|
||||
kotlin("jvm")
|
||||
id("org.jetbrains.dokka")
|
||||
id("com.vanniktech.maven.publish.base")
|
||||
id("binary-compatibility-validator")
|
||||
}
|
||||
|
||||
project.applyOsgi(
|
||||
"Export-Package: okhttp3.java.net.cookiejar",
|
||||
"Automatic-Module-Name: okhttp3.java.net.cookiejar",
|
||||
"Bundle-SymbolicName: com.squareup.okhttp3.java.net.cookiejar"
|
||||
)
|
||||
|
||||
dependencies {
|
||||
api(projects.okhttp)
|
||||
compileOnly(libs.findbugs.jsr305)
|
||||
compileOnly(libs.animalsniffer.annotations)
|
||||
}
|
||||
|
||||
mavenPublishing {
|
||||
configure(KotlinJvm(javadocJar = JavadocJar.Empty()))
|
||||
}
|
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Square, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
package okhttp3.java.net.cookiejar
|
||||
|
||||
|
||||
import java.io.IOException
|
||||
import java.net.CookieHandler
|
||||
import java.net.HttpCookie
|
||||
import java.util.Collections
|
||||
import okhttp3.Cookie
|
||||
import okhttp3.CookieJar
|
||||
import okhttp3.HttpUrl
|
||||
import okhttp3.internal.cookieToString
|
||||
import okhttp3.internal.delimiterOffset
|
||||
import okhttp3.internal.platform.Platform
|
||||
import okhttp3.internal.platform.Platform.Companion.WARN
|
||||
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>) {
|
||||
val cookieStrings = mutableListOf<String>()
|
||||
for (cookie in cookies) {
|
||||
cookieStrings.add(cookieToString(cookie, true))
|
||||
}
|
||||
val multimap = mapOf("Set-Cookie" to cookieStrings)
|
||||
try {
|
||||
cookieHandler.put(url.toUri(), multimap)
|
||||
} catch (e: IOException) {
|
||||
Platform.get().log("Saving cookies failed for " + url.resolve("/...")!!, WARN, e)
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
var cookies: MutableList<Cookie>? = null
|
||||
for ((key, value) in cookieHeaders) {
|
||||
if (("Cookie".equals(key, ignoreCase = true) || "Cookie2".equals(key, ignoreCase = true)) &&
|
||||
value.isNotEmpty()) {
|
||||
for (header in value) {
|
||||
if (cookies == null) cookies = mutableListOf()
|
||||
cookies.addAll(decodeHeaderAsJavaNetCookies(url, header))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return if (cookies != null) {
|
||||
Collections.unmodifiableList(cookies)
|
||||
} else {
|
||||
emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
val result = mutableListOf<Cookie>()
|
||||
var pos = 0
|
||||
val limit = header.length
|
||||
var pairEnd: Int
|
||||
while (pos < limit) {
|
||||
pairEnd = header.delimiterOffset(";,", pos, limit)
|
||||
val equalsSign = header.delimiterOffset('=', pos, pairEnd)
|
||||
val name = header.trimSubstring(pos, equalsSign)
|
||||
if (name.startsWith("$")) {
|
||||
pos = pairEnd + 1
|
||||
continue
|
||||
}
|
||||
|
||||
// We have either name=value or just a name.
|
||||
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()
|
||||
.name(name)
|
||||
.value(value)
|
||||
.domain(url.host)
|
||||
.build())
|
||||
pos = pairEnd + 1
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
@@ -3,6 +3,8 @@ OkHttp URLConnection
|
||||
|
||||
This module integrates OkHttp with `Authenticator` and `CookieHandler` from `java.net`.
|
||||
|
||||
This module is obsolete; prefer `okhttp-java-net-cookiejar`.
|
||||
|
||||
### Download
|
||||
|
||||
```kotlin
|
||||
|
@@ -17,14 +17,9 @@ project.applyOsgi(
|
||||
|
||||
dependencies {
|
||||
api(projects.okhttp)
|
||||
api(projects.okhttpJavaNetCookiejar)
|
||||
compileOnly(libs.findbugs.jsr305)
|
||||
compileOnly(libs.animalsniffer.annotations)
|
||||
|
||||
testImplementation(projects.okhttpTestingSupport)
|
||||
testImplementation(projects.okhttpTls)
|
||||
testImplementation(projects.mockwebserver)
|
||||
testImplementation(libs.junit)
|
||||
testImplementation(libs.assertj.core)
|
||||
}
|
||||
|
||||
mavenPublishing {
|
||||
|
@@ -16,14 +16,21 @@
|
||||
package okhttp3
|
||||
|
||||
import java.io.IOException
|
||||
import java.net.Authenticator
|
||||
import okhttp3.Authenticator.Companion.JAVA_NET_AUTHENTICATOR
|
||||
|
||||
/**
|
||||
* Adapts [Authenticator] to [okhttp3.Authenticator]. Configure OkHttp to use [Authenticator] with
|
||||
* [okhttp3.OkHttpClient.Builder.authenticator] or [okhttp3.OkHttpClient.Builder.proxyAuthenticator].
|
||||
* Do not use this.
|
||||
*
|
||||
* Instead, configure your OkHttpClient.Builder to use `Authenticator.JAVA_NET_AUTHENTICATOR`:
|
||||
*
|
||||
* ```
|
||||
* val okHttpClient = OkHttpClient.Builder()
|
||||
* .authenticator(okhttp3.Authenticator.Companion.JAVA_NET_AUTHENTICATOR)
|
||||
* .build()
|
||||
* ```
|
||||
*/
|
||||
class JavaNetAuthenticator : okhttp3.Authenticator {
|
||||
@Deprecated(message = "Use okhttp3.Authenticator.Companion.JAVA_NET_AUTHENTICATOR instead")
|
||||
class JavaNetAuthenticator : Authenticator {
|
||||
@Throws(IOException::class)
|
||||
override fun authenticate(route: Route?, response: Response): Request? =
|
||||
JAVA_NET_AUTHENTICATOR.authenticate(route, response)
|
||||
|
@@ -14,98 +14,25 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
|
||||
package okhttp3
|
||||
|
||||
import java.io.IOException
|
||||
import java.net.CookieHandler
|
||||
import java.net.HttpCookie
|
||||
import java.util.Collections
|
||||
import okhttp3.internal.cookieToString
|
||||
import okhttp3.internal.delimiterOffset
|
||||
import okhttp3.internal.platform.Platform
|
||||
import okhttp3.internal.platform.Platform.Companion.WARN
|
||||
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>) {
|
||||
val cookieStrings = mutableListOf<String>()
|
||||
for (cookie in cookies) {
|
||||
cookieStrings.add(cookieToString(cookie, true))
|
||||
}
|
||||
val multimap = mapOf("Set-Cookie" to cookieStrings)
|
||||
try {
|
||||
cookieHandler.put(url.toUri(), multimap)
|
||||
} catch (e: IOException) {
|
||||
Platform.get().log("Saving cookies failed for " + url.resolve("/...")!!, WARN, e)
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
var cookies: MutableList<Cookie>? = null
|
||||
for ((key, value) in cookieHeaders) {
|
||||
if (("Cookie".equals(key, ignoreCase = true) || "Cookie2".equals(key, ignoreCase = true)) &&
|
||||
value.isNotEmpty()) {
|
||||
for (header in value) {
|
||||
if (cookies == null) cookies = mutableListOf()
|
||||
cookies.addAll(decodeHeaderAsJavaNetCookies(url, header))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return if (cookies != null) {
|
||||
Collections.unmodifiableList(cookies)
|
||||
} else {
|
||||
emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
/**
|
||||
* A cookie jar that delegates to a [java.net.CookieHandler].
|
||||
*
|
||||
* This implementation delegates everything to [okhttp3.java.net.cookiejar.JavaNetCookieJar], which
|
||||
* conforms to the package-naming limitations of JPMS.
|
||||
*
|
||||
* Callers should prefer to use [okhttp3.java.net.cookiejar.JavaNetCookieJar] directly.
|
||||
*/
|
||||
private fun decodeHeaderAsJavaNetCookies(url: HttpUrl, header: String): List<Cookie> {
|
||||
val result = mutableListOf<Cookie>()
|
||||
var pos = 0
|
||||
val limit = header.length
|
||||
var pairEnd: Int
|
||||
while (pos < limit) {
|
||||
pairEnd = header.delimiterOffset(";,", pos, limit)
|
||||
val equalsSign = header.delimiterOffset('=', pos, pairEnd)
|
||||
val name = header.trimSubstring(pos, equalsSign)
|
||||
if (name.startsWith("$")) {
|
||||
pos = pairEnd + 1
|
||||
continue
|
||||
}
|
||||
|
||||
// We have either name=value or just a name.
|
||||
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()
|
||||
.name(name)
|
||||
.value(value)
|
||||
.domain(url.host)
|
||||
.build())
|
||||
pos = pairEnd + 1
|
||||
}
|
||||
return result
|
||||
}
|
||||
class JavaNetCookieJar private constructor(
|
||||
delegate: okhttp3.java.net.cookiejar.JavaNetCookieJar,
|
||||
) : CookieJar by delegate {
|
||||
constructor(cookieHandler: CookieHandler) : this(
|
||||
okhttp3.java.net.cookiejar.JavaNetCookieJar(
|
||||
cookieHandler
|
||||
)
|
||||
)
|
||||
}
|
||||
|
@@ -109,6 +109,7 @@ kotlin {
|
||||
getByName("jvmTest") {
|
||||
dependencies {
|
||||
dependsOn(commonTest)
|
||||
implementation(projects.okhttpJavaNetCookiejar)
|
||||
implementation(projects.okhttpTls)
|
||||
implementation(projects.okhttpUrlconnection)
|
||||
implementation(projects.mockwebserver3)
|
||||
|
@@ -29,6 +29,7 @@ import mockwebserver3.MockResponse
|
||||
import mockwebserver3.MockWebServer
|
||||
import okhttp3.Headers.Companion.headersOf
|
||||
import okhttp3.internal.buildCache
|
||||
import okhttp3.java.net.cookiejar.JavaNetCookieJar
|
||||
import okhttp3.okio.LoggingFilesystem
|
||||
import okhttp3.testing.PlatformRule
|
||||
import okio.Path.Companion.toPath
|
||||
|
@@ -40,6 +40,7 @@ import mockwebserver3.RecordedRequest;
|
||||
import mockwebserver3.junit5.internal.MockWebServerInstance;
|
||||
import okhttp3.internal.Internal;
|
||||
import okhttp3.internal.platform.Platform;
|
||||
import okhttp3.java.net.cookiejar.JavaNetCookieJar;
|
||||
import okhttp3.testing.PlatformRule;
|
||||
import okhttp3.tls.HandshakeCertificates;
|
||||
import okio.Buffer;
|
||||
|
@@ -78,6 +78,7 @@ import okhttp3.internal.http.HTTP_EARLY_HINTS
|
||||
import okhttp3.internal.http.HTTP_PROCESSING
|
||||
import okhttp3.internal.http.RecordingProxySelector
|
||||
import okhttp3.internal.userAgent
|
||||
import okhttp3.java.net.cookiejar.JavaNetCookieJar
|
||||
import okhttp3.okio.LoggingFilesystem
|
||||
import okhttp3.testing.Flaky
|
||||
import okhttp3.testing.PlatformRule
|
||||
|
@@ -30,6 +30,7 @@ import java.util.Map;
|
||||
import mockwebserver3.MockResponse;
|
||||
import mockwebserver3.MockWebServer;
|
||||
import mockwebserver3.RecordedRequest;
|
||||
import okhttp3.java.net.cookiejar.JavaNetCookieJar;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
|
@@ -57,9 +57,11 @@ import okhttp3.RequestBody.Companion.asRequestBody
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import okhttp3.ResponseBody.Companion.asResponseBody
|
||||
import okhttp3.ResponseBody.Companion.toResponseBody
|
||||
import okhttp3.internal.authenticator.JavaNetAuthenticator
|
||||
import okhttp3.internal.http2.Settings
|
||||
import okhttp3.internal.proxy.NullProxySelector
|
||||
import okhttp3.internal.tls.OkHostnameVerifier
|
||||
import okhttp3.java.net.cookiejar.JavaNetCookieJar
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import okhttp3.logging.LoggingEventListener
|
||||
import okhttp3.mockwebserver.MockResponse
|
||||
|
@@ -69,10 +69,12 @@ import okhttp3.TestUtil.assertSuppressed
|
||||
import okhttp3.internal.RecordingAuthenticator
|
||||
import okhttp3.internal.RecordingOkAuthenticator
|
||||
import okhttp3.internal.addHeaderLenient
|
||||
import okhttp3.internal.authenticator.JavaNetAuthenticator
|
||||
import okhttp3.internal.http.HTTP_PERM_REDIRECT
|
||||
import okhttp3.internal.http.HTTP_TEMP_REDIRECT
|
||||
import okhttp3.internal.platform.Platform.Companion.get
|
||||
import okhttp3.internal.userAgent
|
||||
import okhttp3.java.net.cookiejar.JavaNetCookieJar
|
||||
import okhttp3.testing.Flaky
|
||||
import okhttp3.testing.PlatformRule
|
||||
import okio.Buffer
|
||||
|
@@ -25,15 +25,15 @@ include(":okcurl")
|
||||
include(":okhttp")
|
||||
include(":okhttp-bom")
|
||||
include(":okhttp-brotli")
|
||||
include(":okhttp-coroutines")
|
||||
include(":okhttp-dnsoverhttps")
|
||||
include(":okhttp-hpacktests")
|
||||
include(":okhttp-idna-mapping-table")
|
||||
include(":okhttp-java-net-cookiejar")
|
||||
include(":okhttp-logging-interceptor")
|
||||
project(":okhttp-logging-interceptor").name = "logging-interceptor"
|
||||
include(":okhttp-sse")
|
||||
include(":okhttp-testing-support")
|
||||
include(":okhttp-tls")
|
||||
include(":okhttp-coroutines")
|
||||
include(":okhttp-urlconnection")
|
||||
include(":samples:compare")
|
||||
include(":samples:crawler")
|
||||
@@ -44,6 +44,8 @@ include(":samples:static-server")
|
||||
include(":samples:tlssurvey")
|
||||
include(":samples:unixdomainsockets")
|
||||
|
||||
project(":okhttp-logging-interceptor").name = "logging-interceptor"
|
||||
|
||||
if (!isKnownBrokenIntelliJ()) {
|
||||
include(":okhttp-android")
|
||||
include(":android-test")
|
||||
|
Reference in New Issue
Block a user