Skip to content

Commit

Permalink
Convert Network Module [2]
Browse files Browse the repository at this point in the history
Differential Revision: D55817599
  • Loading branch information
Thomas Nardone authored and facebook-github-bot committed Jun 7, 2024
1 parent 1a67a5d commit 8b8ed87
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 317 deletions.
28 changes: 14 additions & 14 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -3423,11 +3423,11 @@ public abstract interface class com/facebook/react/modules/network/CustomClientB
public abstract fun apply (Lokhttp3/OkHttpClient$Builder;)V
}

public class com/facebook/react/modules/network/ForwardingCookieHandler : java/net/CookieHandler {
public final class com/facebook/react/modules/network/ForwardingCookieHandler : java/net/CookieHandler {
public fun <init> (Lcom/facebook/react/bridge/ReactContext;)V
public fun addCookies (Ljava/lang/String;Ljava/util/List;)V
public fun clearCookies (Lcom/facebook/react/bridge/Callback;)V
public fun destroy ()V
public final fun addCookies (Ljava/lang/String;Ljava/util/List;)V
public final fun clearCookies (Lcom/facebook/react/bridge/Callback;)V
public final fun destroy ()V
public fun get (Ljava/net/URI;Ljava/util/Map;)Ljava/util/Map;
public fun put (Ljava/net/URI;Ljava/util/Map;)V
}
Expand Down Expand Up @@ -3480,15 +3480,15 @@ public abstract interface class com/facebook/react/modules/network/OkHttpClientF
public abstract fun createNewNetworkModuleClient ()Lokhttp3/OkHttpClient;
}

public class com/facebook/react/modules/network/OkHttpClientProvider {
public fun <init> ()V
public static fun createClient ()Lokhttp3/OkHttpClient;
public static fun createClient (Landroid/content/Context;)Lokhttp3/OkHttpClient;
public static fun createClientBuilder ()Lokhttp3/OkHttpClient$Builder;
public static fun createClientBuilder (Landroid/content/Context;)Lokhttp3/OkHttpClient$Builder;
public static fun createClientBuilder (Landroid/content/Context;I)Lokhttp3/OkHttpClient$Builder;
public static fun getOkHttpClient ()Lokhttp3/OkHttpClient;
public static fun setOkHttpClientFactory (Lcom/facebook/react/modules/network/OkHttpClientFactory;)V
public final class com/facebook/react/modules/network/OkHttpClientProvider {
public static final field INSTANCE Lcom/facebook/react/modules/network/OkHttpClientProvider;
public static final fun createClient ()Lokhttp3/OkHttpClient;
public static final fun createClient (Landroid/content/Context;)Lokhttp3/OkHttpClient;
public static final fun createClientBuilder ()Lokhttp3/OkHttpClient$Builder;
public static final fun createClientBuilder (Landroid/content/Context;)Lokhttp3/OkHttpClient$Builder;
public static final fun createClientBuilder (Landroid/content/Context;I)Lokhttp3/OkHttpClient$Builder;
public static final fun getOkHttpClient ()Lokhttp3/OkHttpClient;
public static final fun setOkHttpClientFactory (Lcom/facebook/react/modules/network/OkHttpClientFactory;)V
}

public abstract interface class com/facebook/react/modules/network/ProgressListener {
Expand All @@ -3503,7 +3503,7 @@ public class com/facebook/react/modules/network/ProgressResponseBody : okhttp3/R
public fun totalBytesRead ()J
}

public class com/facebook/react/modules/network/ReactCookieJarContainer : com/facebook/react/modules/network/CookieJarContainer {
public final class com/facebook/react/modules/network/ReactCookieJarContainer : com/facebook/react/modules/network/CookieJarContainer {
public fun <init> ()V
public fun loadForRequest (Lokhttp3/HttpUrl;)Ljava/util/List;
public fun removeCookieJar ()V
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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.
*/

package com.facebook.react.modules.network

import android.webkit.CookieManager
import com.facebook.react.bridge.Callback
import com.facebook.react.bridge.ReactContext
import java.io.IOException
import java.net.CookieHandler
import java.net.URI
import java.util.Collections

/**
* Cookie handler that forwards all cookies to the WebView CookieManager.
*
* This class relies on CookieManager to persist cookies to disk so cookies may be lost if the
* application is terminated before it syncs.
*/
@Suppress("UNUSED_PARAMETER")
public class ForwardingCookieHandler(context: ReactContext) : CookieHandler() {

private var _cookieManager: CookieManager? = null

@Throws(IOException::class)
override fun get(uri: URI, headers: Map<String?, List<String?>?>?): Map<String, List<String?>> {
val cookies = cookieManager?.getCookie(uri.toString())
return if (cookies.isNullOrEmpty()) {
Collections.emptyMap()
} else {
mapOf(COOKIE_HEADER to listOf(cookies))
}
}

@Throws(IOException::class)
override fun put(uri: URI?, headers: Map<String?, List<String?>>) {
val url = uri.toString()
headers.forEach { entry ->
if (entry.key.isCookieHeader) {
addCookies(url, entry.value)
}
}
}

public fun clearCookies(callback: Callback) {
cookieManager?.removeAllCookies { value -> callback.invoke(value) }
}

public fun destroy(): Unit = Unit

public fun addCookies(url: String?, cookies: List<String?>) {
val manager = cookieManager ?: return
cookies.forEach { cookie -> addCookieAsync(url, cookie) }
manager.flush()
}

private fun addCookieAsync(url: String?, cookie: String?) {
cookieManager?.setCookie(url, cookie, null)
}

private val cookieManager: CookieManager?
/**
* Instantiating CookieManager will load the Chromium task taking a 100ish ms so we do it lazily
* to make sure it's done on a background thread as needed.
*/
@Suppress("CatchGeneralException")
get() {
if (_cookieManager == null) {
_cookieManager =
try {
CookieManager.getInstance()
} catch (ex: IllegalArgumentException) {
// https://bugs.chromium.org/p/chromium/issues/detail?id=559720
null
} catch (exception: Exception) {
// Ideally we would like to catch a `MissingWebViewPackageException` here.
// That API is private so we can't access it.
// Historically we used string matching on the error message to understand
// if the exception was a Missing Webview One.
// OEMs have been customizing that message making really hard to catch it.
// Therefore we result to returning null as a default instead of rethrowing
// the exception as it will result in a app crash at runtime.
// a) We will return null for all the other unhandled conditions when a webview
// provider is
// not found.
// b) We already have null checks in place for `getCookieManager()` calls.
// c) We have annotated the method as @Nullable to notify future devs about our return
// type.
null
}
}
return _cookieManager
}
}

private const val VERSION_ZERO_HEADER = "Set-cookie"
private const val VERSION_ONE_HEADER = "Set-cookie2"
private const val COOKIE_HEADER = "Cookie"

private val String?.isCookieHeader: Boolean
get() {
return this != null &&
(equals(VERSION_ZERO_HEADER, ignoreCase = true) ||
equals(VERSION_ONE_HEADER, ignoreCase = true))
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,25 @@
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.modules.network;
package com.facebook.react.modules.network

/**
* The class purpose is to weaken too strict OkHttp restriction on http headers. See:
* https://github.com/square/okhttp/issues/2016 Auth headers might have an Authentication
* information. It is better to get 401 from the server in this case, rather than non descriptive
* error as 401 could be handled to invalidate the wrong token in the client code.
*/
class HeaderUtil {

public static String stripHeaderName(String name) {
StringBuilder builder = new StringBuilder(name.length());
boolean modified = false;
for (int i = 0, length = name.length(); i < length; i++) {
char c = name.charAt(i);
internal object HeaderUtil {
public fun stripHeaderName(name: String): String {
val builder = StringBuilder(name.length)
var modified = false
name.forEach { c ->
if (c > '\u0020' && c < '\u007f') {
builder.append(c);
builder.append(c)
} else {
modified = true;
modified = true
}
}
return modified ? builder.toString() : name;
return if (modified) builder.toString() else name
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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.
*/

package com.facebook.react.modules.network

import okhttp3.Interceptor

/**
* Classes implementing this fun interface return a new [Interceptor] when the [create] method is
* called.
*/
public fun interface NetworkInterceptorCreator {
public fun create(): Interceptor?
}
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ public void removeListeners(double count) {}
if (header == null || header.size() != 2) {
return null;
}
String headerName = HeaderUtil.stripHeaderName(header.getString(0));
String headerName = HeaderUtil.INSTANCE.stripHeaderName(header.getString(0));
String headerValue = header.getString(1);
if (headerName == null || headerValue == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.modules.network;
package com.facebook.react.modules.network

import okhttp3.OkHttpClient;
import okhttp3.OkHttpClient

public interface OkHttpClientFactory {
OkHttpClient createNewNetworkModuleClient();
public fun interface OkHttpClientFactory {
public fun createNewNetworkModuleClient(): OkHttpClient
}
;
Loading

0 comments on commit 8b8ed87

Please sign in to comment.