Skip to content
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

MOBY-836: Implemented WebkitCookieManager in CookieJar #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
buildscript {
ext {
kotlinVersion = '1.6.20'
kotlinVersion = '1.8.10'
buildToolsVersion = '33.0.0'
compileSdkVersion = 33
targetSdkVersion = 33
Expand All @@ -20,7 +20,7 @@ buildscript {

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-parcelize'

def DEFAULT_COMPILE_SDK_VERSION = 28
def DEFAULT_BUILD_TOOLS_VERSION = "28.0.3"
Expand Down
37 changes: 28 additions & 9 deletions android/src/main/java/com/vydia/RNUploader/UploaderModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.webkit.CookieManager;
import android.util.Log
import android.webkit.MimeTypeMap
import com.facebook.react.BuildConfig
Expand All @@ -30,6 +31,7 @@ class UploaderModule(val reactContext: ReactApplicationContext) : ReactContextBa
private val TAG = "UploaderBridge"
private var notificationChannelID = "fn_channel_file_upload"
private var isGlobalRequestObserver = false
private val cookieManager = CookieManager.getInstance()

override fun getName(): String {
return "RNFileUploader"
Expand Down Expand Up @@ -113,15 +115,32 @@ class UploaderModule(val reactContext: ReactApplicationContext) : ReactContextBa
}
readTimeout = options.getInt("readTimeout")
}
httpStack = OkHttpStack(OkHttpClient().newBuilder()
.followRedirects(followRedirects)
.followSslRedirects(followSslRedirects)
.retryOnConnectionFailure(retryOnConnectionFailure)
.connectTimeout(connectTimeout.toLong(), TimeUnit.SECONDS)
.writeTimeout(writeTimeout.toLong(), TimeUnit.SECONDS)
.readTimeout(readTimeout.toLong(), TimeUnit.SECONDS)
.cache(null)
.build())
if (options.hasKey("isNativeCookie")){
if (options.getType("isNativeCookie") != ReadableType.Boolean) {
promise.reject(IllegalArgumentException("isNativeCookie must be a boolean."))
return
}
httpStack = OkHttpStack(OkHttpClient().newBuilder()
.followRedirects(followRedirects)
.followSslRedirects(followSslRedirects)
.retryOnConnectionFailure(retryOnConnectionFailure)
.connectTimeout(connectTimeout.toLong(), TimeUnit.SECONDS)
.writeTimeout(writeTimeout.toLong(), TimeUnit.SECONDS)
.readTimeout(readTimeout.toLong(), TimeUnit.SECONDS)
.cookieJar(WebkitCookieManager(cookieManager))
.cache(null)
.build())
}else{
httpStack = OkHttpStack(OkHttpClient().newBuilder()
.followRedirects(followRedirects)
.followSslRedirects(followSslRedirects)
.retryOnConnectionFailure(retryOnConnectionFailure)
.connectTimeout(connectTimeout.toLong(), TimeUnit.SECONDS)
.writeTimeout(writeTimeout.toLong(), TimeUnit.SECONDS)
.readTimeout(readTimeout.toLong(), TimeUnit.SECONDS)
.cache(null)
.build())
}
}

/*
Expand Down
21 changes: 21 additions & 0 deletions android/src/main/java/com/vydia/RNUploader/WebkitCookieManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.vydia.RNUploader

import android.webkit.CookieManager
import okhttp3.Cookie
import okhttp3.CookieJar
import okhttp3.HttpUrl

class WebkitCookieManager (private val cookieManager: CookieManager) : CookieJar {

override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
cookies.forEach { cookie ->
cookieManager.setCookie(url.toString(), cookie.toString())
}
}

override fun loadForRequest(url: HttpUrl): List<Cookie> =
when (val cookies = cookieManager.getCookie(url.toString())) {
null -> emptyList()
else -> cookies.split("; ").mapNotNull { Cookie.parse(url, it) }
}
}