Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

Share cookies with RN, remove cookie utils #388

Merged
merged 1 commit into from
Jun 6, 2017
Merged
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
57 changes: 20 additions & 37 deletions android/src/main/java/com/RNFetchBlob/RNFetchBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import com.RNFetchBlob.Utils.RNFBCookieJar;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.LifecycleEventListener;
Expand All @@ -15,12 +13,16 @@
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;

// Cookies
import com.facebook.react.modules.network.ForwardingCookieHandler;
import com.facebook.react.modules.network.CookieJarContainer;
import com.facebook.react.modules.network.OkHttpClientProvider;
import okhttp3.OkHttpClient;
import okhttp3.JavaNetCookieJar;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand All @@ -30,6 +32,11 @@

public class RNFetchBlob extends ReactContextBaseJavaModule {

// Cookies
private final ForwardingCookieHandler mCookieHandler;
private final CookieJarContainer mCookieJarContainer;
private final OkHttpClient mClient;

static ReactApplicationContext RCTContext;
static LinkedBlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<>();
static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 10, 5000, TimeUnit.MILLISECONDS, taskQueue);
Expand All @@ -42,6 +49,11 @@ public RNFetchBlob(ReactApplicationContext reactContext) {

super(reactContext);

mClient = OkHttpClientProvider.getOkHttpClient();
mCookieHandler = new ForwardingCookieHandler(reactContext);
mCookieJarContainer = (CookieJarContainer) mClient.cookieJar();
mCookieJarContainer.setCookieJar(new JavaNetCookieJar(mCookieHandler));

RCTContext = reactContext;
reactContext.addActivityEventListener(new ActivityEventListener() {
@Override
Expand Down Expand Up @@ -252,35 +264,6 @@ public void run() {

}

@ReactMethod
/**
* Get cookies belongs specific host.
* @param host String domain name.
*/
public void getCookies(String domain, Promise promise) {
try {
WritableMap cookies = RNFBCookieJar.getCookies(domain);
promise.resolve(cookies);
} catch(Exception err) {
promise.reject("RNFetchBlob.getCookies", err.getMessage());
}
}

@ReactMethod
/**
* Remove cookies for specific domain
* @param domain String of the domain
* @param promise JSC promise injected by RN
*/
public void removeCookies(String domain, Promise promise) {
try {
RNFBCookieJar.removeCookies(domain);
promise.resolve(null);
} catch(Exception err) {
promise.reject("RNFetchBlob.removeCookies", err.getMessage());
}
}

@ReactMethod
/**
* @param path Stream file path
Expand Down Expand Up @@ -338,12 +321,12 @@ public void enableUploadProgressReport(String taskId, int interval, int count) {

@ReactMethod
public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
new RNFetchBlobReq(options, taskId, method, url, headers, body, null, callback).run();
}
new RNFetchBlobReq(options, taskId, method, url, headers, body, null, mClient, callback).run();
}

@ReactMethod
public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) {
new RNFetchBlobReq(options, taskId, method, url, headers, null, body, callback).run();
new RNFetchBlobReq(options, taskId, method, url, headers, null, body, mClient, callback).run();
}

@ReactMethod
Expand Down
15 changes: 5 additions & 10 deletions android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import com.RNFetchBlob.Response.RNFetchBlobDefaultResp;
import com.RNFetchBlob.Response.RNFetchBlobFileResp;
import com.RNFetchBlob.Utils.RNFBCookieJar;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
Expand All @@ -21,14 +20,12 @@
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.modules.network.OkHttpClientProvider;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
Expand All @@ -43,7 +40,6 @@

import okhttp3.Call;
import okhttp3.ConnectionPool;
import okhttp3.CookieJar;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
Expand Down Expand Up @@ -98,8 +94,9 @@ enum ResponseFormat {
WritableMap respInfo;
boolean timeout = false;
ArrayList<String> redirects = new ArrayList<>();
OkHttpClient client;

public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, OkHttpClient client, final Callback callback) {
this.method = method.toUpperCase();
this.options = new RNFetchBlobConfig(options);
this.taskId = taskId;
Expand All @@ -108,6 +105,7 @@ public RNFetchBlobReq(ReadableMap options, String taskId, String method, String
this.callback = callback;
this.rawRequestBody = body;
this.rawRequestBodyArray = arrayBody;
this.client = client;

if(this.options.fileCache || this.options.path != null)
responseType = ResponseType.FileStorage;
Expand Down Expand Up @@ -196,7 +194,7 @@ else if(this.options.fileCache)
if (this.options.trusty) {
clientBuilder = RNFetchBlobUtils.getUnsafeOkHttpClient();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should have been clientBuilder = RNFetchBlobUtils.getUnsafeOkHttpClient(client); so it can build on top of the react-native client. I must have forgotten to change it. @wkh237 could you change it on master or should I submit a new PR?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I will do this.

} else {
clientBuilder = new OkHttpClient.Builder();
clientBuilder = client.newBuilder();
}

final Request.Builder builder = new Request.Builder();
Expand Down Expand Up @@ -297,10 +295,7 @@ else if(cType.isEmpty()) {
}

// #156 fix cookie issue


final Request req = builder.build();
clientBuilder.cookieJar(new RNFBCookieJar());
clientBuilder.addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Expand Down
4 changes: 2 additions & 2 deletions android/src/main/java/com/RNFetchBlob/RNFetchBlobUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static void emitWarningEvent(String data) {
.emit(RNFetchBlobConst.EVENT_MESSAGE, args);
}

public static OkHttpClient.Builder getUnsafeOkHttpClient() {
public static OkHttpClient.Builder getUnsafeOkHttpClient(OkHttpClient client) {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]{
Expand All @@ -78,7 +78,7 @@ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

OkHttpClient.Builder builder = new OkHttpClient.Builder();
OkHttpClient.Builder builder = client.newBuilder();
builder.sslSocketFactory(sslSocketFactory);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
Expand Down
66 changes: 0 additions & 66 deletions android/src/main/java/com/RNFetchBlob/Utils/RNFBCookieJar.java

This file was deleted.

2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import polyfill from './polyfill'
import _ from 'lodash'
import android from './android'
import ios from './ios'
import net from './net'
import JSONStream from './json-stream'
const {
RNFetchBlobSession,
Expand Down Expand Up @@ -564,7 +563,6 @@ export default {
session,
fs,
wrap,
net,
polyfill,
JSONStream
}
19 changes: 0 additions & 19 deletions ios/RNFetchBlob/RNFetchBlob.m
Original file line number Diff line number Diff line change
Expand Up @@ -580,25 +580,6 @@ - (UIViewController *) documentInteractionControllerViewControllerForPreview: (U
return window.rootViewController;
}

# pragma mark - getCookies

RCT_EXPORT_METHOD(getCookies:(NSString *)url resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
resolve([RNFetchBlobNetwork getCookies:url]);
}

# pragma mark - removeCookie

RCT_EXPORT_METHOD(removeCookies:(NSString *)domain resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
NSError * err = nil;
[RNFetchBlobNetwork removeCookies:domain error:&err];
if(err)
reject(@"RNFetchBlob failed to remove cookie", @"RNFetchBlob failed to remove cookie", nil);
else
resolve(@[[NSNull null]]);
}

# pragma mark - check expired network events

RCT_EXPORT_METHOD(emitExpiredEvent:(RCTResponseSenderBlock)callback)
Expand Down
2 changes: 0 additions & 2 deletions ios/RNFetchBlobNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse
- (nullable id) init;
- (void) sendRequest;
- (void) sendRequest:(NSDictionary * _Nullable )options contentLength:(long)contentLength bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback;
+ (void) removeCookies:(NSString *) domain error:(NSError **)error;
+ (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config;
+ (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress *)config;
+ (NSDictionary *) getCookies:(NSString *) url;



Expand Down
Loading