Skip to content

Commit

Permalink
Convert Network Module [3] (#44213)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #44213

Third batch of conversions for the module
Changelog: [Internal]

Reviewed By: cortinico

Differential Revision: D55985527
  • Loading branch information
Thomas Nardone authored and facebook-github-bot committed Jun 11, 2024
1 parent 7cb45bb commit 29bb278
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 152 deletions.
6 changes: 3 additions & 3 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -3497,12 +3497,12 @@ public abstract interface class com/facebook/react/modules/network/ProgressListe
public abstract fun onProgress (JJZ)V
}

public class com/facebook/react/modules/network/ProgressResponseBody : okhttp3/ResponseBody {
public final class com/facebook/react/modules/network/ProgressResponseBody : okhttp3/ResponseBody {
public fun <init> (Lokhttp3/ResponseBody;Lcom/facebook/react/modules/network/ProgressListener;)V
public fun contentLength ()J
public fun contentType ()Lokhttp3/MediaType;
public fun source ()Lokio/BufferedSource;
public fun totalBytesRead ()J
public final fun totalBytesRead ()J
}

public final class com/facebook/react/modules/network/ReactCookieJarContainer : com/facebook/react/modules/network/CookieJarContainer {
Expand All @@ -3525,7 +3525,7 @@ public class com/facebook/react/modules/network/ResponseUtil {
public static fun onResponseReceived (Lcom/facebook/react/bridge/ReactApplicationContext;IILcom/facebook/react/bridge/WritableMap;Ljava/lang/String;)V
}

public class com/facebook/react/modules/network/TLSSocketFactory : javax/net/ssl/SSLSocketFactory {
public final class com/facebook/react/modules/network/TLSSocketFactory : javax/net/ssl/SSLSocketFactory {
public fun <init> ()V
public fun createSocket (Ljava/lang/String;I)Ljava/net/Socket;
public fun createSocket (Ljava/lang/String;ILjava/net/InetAddress;I)Ljava/net/Socket;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

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

public interface ProgressListener {
void onProgress(long bytesWritten, long contentLength, boolean done);
public fun interface ProgressListener {
public fun onProgress(bytesWritten: Long, contentLength: Long, done: Boolean)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 java.io.IOException
import okhttp3.MediaType
import okhttp3.ResponseBody
import okio.Buffer
import okio.BufferedSource
import okio.ForwardingSource
import okio.Okio
import okio.Source

public class ProgressResponseBody(
private val responseBody: ResponseBody,
private val progressListener: ProgressListener
) : ResponseBody() {

private val _bufferedSource: BufferedSource by
lazy(LazyThreadSafetyMode.NONE) { Okio.buffer(source(responseBody.source())) }
private var _totalBytesRead = 0L

override fun contentType(): MediaType? = responseBody.contentType()

override fun contentLength(): Long = responseBody.contentLength()

public fun totalBytesRead(): Long = _totalBytesRead

override fun source(): BufferedSource = _bufferedSource

private fun source(source: Source): Source {
return object : ForwardingSource(source) {
@Throws(IOException::class)
override fun read(sink: Buffer, byteCount: Long): Long {
val bytesRead = super.read(sink, byteCount)
// read() returns the number of bytes read, or -1 if this source is exhausted.
if (bytesRead != -1L) {
_totalBytesRead += bytesRead
}
progressListener.onProgress(_totalBytesRead, responseBody.contentLength(), bytesRead == -1L)
return bytesRead
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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 java.io.IOException
import java.net.InetAddress
import java.net.Socket
import java.net.UnknownHostException
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocket
import javax.net.ssl.SSLSocketFactory

/**
* This class is needed for TLS 1.2 support on Android 4.x
*
* Source: http://blog.dev-area.net/2015/08/13/android-4-1-enable-tls-1-1-and-tls-1-2/
*/
public class TLSSocketFactory : SSLSocketFactory() {

private val delegate: SSLSocketFactory

init {
val context = SSLContext.getInstance("TLS")
context.init(null, null, null)
delegate = context.socketFactory
}

override fun getDefaultCipherSuites(): Array<String> = delegate.defaultCipherSuites

override fun getSupportedCipherSuites(): Array<String> = delegate.supportedCipherSuites

@Throws(IOException::class)
override fun createSocket(s: Socket, host: String, port: Int, autoClose: Boolean): Socket =
enableTLSOnSocket(delegate.createSocket(s, host, port, autoClose))

@Throws(IOException::class, UnknownHostException::class)
override fun createSocket(host: String?, port: Int): Socket =
enableTLSOnSocket(delegate.createSocket(host, port))

@Throws(IOException::class, UnknownHostException::class)
override fun createSocket(
host: String?,
port: Int,
localHost: InetAddress,
localPort: Int
): Socket = enableTLSOnSocket(delegate.createSocket(host, port, localHost, localPort))

@Throws(IOException::class)
override fun createSocket(host: InetAddress, port: Int): Socket =
enableTLSOnSocket(delegate.createSocket(host, port))

@Throws(IOException::class)
override fun createSocket(
address: InetAddress,
port: Int,
localAddress: InetAddress,
localPort: Int
): Socket? = enableTLSOnSocket(delegate.createSocket(address, port, localAddress, localPort))

private fun enableTLSOnSocket(socket: Socket): Socket {
(socket as? SSLSocket)?.enabledProtocols = arrayOf("TLSv1", "TLSv1.1", "TLSv1.2")
return socket
}
}

0 comments on commit 29bb278

Please sign in to comment.