Skip to content

Commit

Permalink
Make a JPMS-compatible JavaNetCookieJar (#8074)
Browse files Browse the repository at this point in the history
* Make a JPMS-compatible JavaNetCookieJar

* Add missing imports

* Depend on okhttpUrlconnection

This is necessary to pass the OSGi test.
  • Loading branch information
squarejesse authored Nov 20, 2023
1 parent 0343ff8 commit dda13bb
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 103 deletions.
2 changes: 1 addition & 1 deletion native-image-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions okhttp-java-net-cookiejar/README.md
Original file line number Diff line number Diff line change
@@ -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")
```
6 changes: 6 additions & 0 deletions okhttp-java-net-cookiejar/api/okhttp-java-net-cookiejar.api
Original file line number Diff line number Diff line change
@@ -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 changes: 25 additions & 0 deletions okhttp-java-net-cookiejar/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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()))
}
Original file line number Diff line number Diff line change
@@ -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
}
}
2 changes: 2 additions & 0 deletions okhttp-urlconnection/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions okhttp-urlconnection/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
107 changes: 17 additions & 90 deletions okhttp-urlconnection/src/main/kotlin/okhttp3/JavaNetCookieJar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
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
}
/**
* 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.
*/
class JavaNetCookieJar private constructor(
delegate: okhttp3.java.net.cookiejar.JavaNetCookieJar,
) : CookieJar by delegate {
constructor(cookieHandler: CookieHandler) : this(
okhttp3.java.net.cookiejar.JavaNetCookieJar(
cookieHandler
)
)
}
1 change: 1 addition & 0 deletions okhttp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ kotlin {
getByName("jvmTest") {
dependencies {
dependsOn(commonTest)
implementation(projects.okhttpJavaNetCookiejar)
implementation(projects.okhttpTls)
implementation(projects.okhttpUrlconnection)
implementation(projects.mockwebserver3)
Expand Down
1 change: 1 addition & 0 deletions okhttp/src/jvmTest/java/okhttp3/CacheCorruptionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions okhttp/src/jvmTest/java/okhttp3/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
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;
Expand Down
1 change: 1 addition & 0 deletions okhttp/src/jvmTest/java/okhttp3/CallTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions okhttp/src/jvmTest/java/okhttp3/CookiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down
2 changes: 2 additions & 0 deletions okhttp/src/jvmTest/java/okhttp3/KotlinSourceModernTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit dda13bb

Please sign in to comment.