Repository navigation
Support apps that depend on OkHttp 5 #60385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ide
wants to merge
2
commits into
react:main
Choose a base branch
from
expo:feature/okhttp5-compat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
...e/ReactAndroid/src/main/java/com/facebook/react/modules/network/CookieHandlerCookieJar.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| @file:Suppress("DEPRECATION_ERROR") // Conflicting okhttp versions | ||
|
|
||
| package com.facebook.react.modules.network | ||
|
|
||
| import com.facebook.common.logging.FLog | ||
| import com.facebook.react.common.ReactConstants | ||
| import java.io.IOException | ||
| import java.net.CookieHandler | ||
| import okhttp3.Cookie | ||
| import okhttp3.CookieJar | ||
| import okhttp3.HttpUrl | ||
|
|
||
| /** | ||
| * Stores OkHttp's cookies in a [CookieHandler], as `JavaNetCookieJar` from `okhttp-urlconnection` | ||
| * does, through API that OkHttp 3, 4 and 5 share. Each `JavaNetCookieJar` release calls internals | ||
| * of its own OkHttp major version: 4.9.2's throws `NoClassDefFoundError` on OkHttp 5. | ||
| */ | ||
| internal class CookieHandlerCookieJar(private val cookieHandler: CookieHandler) : CookieJar { | ||
|
|
||
| override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) { | ||
| val headers = mapOf("Set-Cookie" to cookies.map { toSetCookieHeader(it) }) | ||
| try { | ||
| cookieHandler.put(url.uri(), headers) | ||
| } catch (e: IOException) { | ||
| FLog.w(ReactConstants.TAG, "Saving cookies failed for ${url.redact()}", e) | ||
| } | ||
| } | ||
|
|
||
| override fun loadForRequest(url: HttpUrl): List<Cookie> { | ||
| val headers = | ||
| try { | ||
| cookieHandler.get(url.uri(), emptyMap()) | ||
| } catch (e: IOException) { | ||
| FLog.w(ReactConstants.TAG, "Loading cookies failed for ${url.redact()}", e) | ||
| return emptyList() | ||
| } | ||
| return headers | ||
| .filterKeys { | ||
| it.equals("Cookie", ignoreCase = true) || it.equals("Cookie2", ignoreCase = true) | ||
| } | ||
| .values | ||
| .flatMap { values -> values.flatMap { decodeCookieHeader(url, it) } } | ||
| } | ||
|
|
||
| private companion object { | ||
| private const val DOMAIN_ATTRIBUTE = "; domain=" | ||
|
|
||
| /** | ||
| * [Cookie.toString], with a leading dot on the domain of a cookie that isn't host-only. | ||
| * Handlers that follow RFC 2965, such as [java.net.CookieManager], need the dot to match | ||
| * `www.example.com` to `example.com`. | ||
| */ | ||
| fun toSetCookieHeader(cookie: Cookie): String { | ||
| val header = cookie.toString() | ||
| if (cookie.hostOnly()) { | ||
| return header | ||
| } | ||
| // Search after "name=value", so a value can't be mistaken for the attribute. | ||
| val attribute = | ||
| header.indexOf(DOMAIN_ATTRIBUTE, cookie.name().length + 1 + cookie.value().length) | ||
| if (attribute < 0) { | ||
| return header | ||
| } | ||
| val domain = attribute + DOMAIN_ATTRIBUTE.length | ||
| return header.substring(0, domain) + "." + header.substring(domain) | ||
| } | ||
|
|
||
| /** | ||
| * Splits a `Cookie` request header into cookies. [Cookie.parse] can't: it reads one | ||
| * `Set-Cookie` value. | ||
| */ | ||
| fun decodeCookieHeader(url: HttpUrl, header: String): List<Cookie> { | ||
| val result = mutableListOf<Cookie>() | ||
| var pos = 0 | ||
| while (pos < header.length) { | ||
| val pairEnd = | ||
| header.indexOfAny(PAIR_DELIMITERS, pos).let { if (it < 0) header.length else it } | ||
| val equalsSign = | ||
| header.indexOf('=', pos).let { if (it < 0 || it > pairEnd) pairEnd else it } | ||
| val name = header.substring(pos, equalsSign).trim(::isAsciiWhitespace) | ||
| pos = pairEnd + 1 | ||
| if (name.startsWith("$")) { | ||
| continue // An attribute such as $Path, not a cookie. | ||
| } | ||
| var value = | ||
| if (equalsSign < pairEnd) | ||
| header.substring(equalsSign + 1, pairEnd).trim(::isAsciiWhitespace) | ||
| else "" | ||
| if (value.length >= 2 && value.startsWith("\"") && value.endsWith("\"")) { | ||
| value = value.substring(1, value.length - 1) | ||
| } | ||
| // Cookie.Builder rejects surrounding whitespace, which unquoting can leave, as in "abc ". | ||
| value = value.trim() | ||
| try { | ||
| // Cookie.Builder accepts characters (such as non-ASCII, DEL, and control characters other | ||
| // than tab) that OkHttp rejects when building the Cookie request header, and it rejects a | ||
| // name or value that its own trim would change (which differs across OkHttp versions). | ||
| // Uncaught, either IllegalArgumentException fails the request, and OkHttp rethrows it on | ||
| // its dispatcher thread, which crashes the app. | ||
| require(name.all(::isValidHeaderCharacter) && value.all(::isValidHeaderCharacter)) { | ||
| "Invalid request header characters" | ||
| } | ||
| result.add(Cookie.Builder().name(name).value(value).domain(url.host()).build()) | ||
| } catch (_: IllegalArgumentException) { | ||
| // Omit cookie contents, exception message, and stack trace: a stored cookie is rejected | ||
| // again on every request to its host. | ||
| FLog.w(ReactConstants.TAG, "Skipped a cookie OkHttp rejects for ${url.redact()}") | ||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| private val PAIR_DELIMITERS = charArrayOf(';', ',') | ||
|
|
||
| private fun isAsciiWhitespace(c: Char): Boolean = | ||
| c == ' ' || c == '\t' || c == '\n' || c == '\u000C' || c == '\r' | ||
|
|
||
| private fun isValidHeaderCharacter(c: Char): Boolean = c == '\t' || c in ' '..'~' | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this can be made non-breaking as apps can declare in their .gradle files that they don't intend to consume this
apidependency from react-native.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ReactAndroidnow keepsokhttp-urlconnectionas anapidependency, so the change is no longer breaking, and the[BREAKING]changelog entry is gone.An app on OkHttp 5 needs to exclude it only if its code or a keep rule retains
JavaNetCookieJar.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably need a page on the website explaining how to do this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a section to the Networking page: react/react-native-website#5363