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

Commit

Permalink
Merge branch '0.9.5' into 0.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
wkh237 committed Sep 13, 2016
2 parents 9658ea1 + 595e104 commit 301c8cf
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fetchblob-dev",
"version": "0.9.4",
"version": "0.9.5-beta.2",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
Expand Down
31 changes: 28 additions & 3 deletions src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ enum ResponseType {
FileStorage
}

enum ResponseFormat {
Auto,
UTF8,
BASE64
}

public static HashMap<String, Call> taskTable = new HashMap<>();
static HashMap<String, Boolean> progressReport = new HashMap<>();
static HashMap<String, Boolean> uploadProgressReport = new HashMap<>();
Expand All @@ -83,8 +89,10 @@ enum ResponseType {
RNFetchBlobBody requestBody;
RequestType requestType;
ResponseType responseType;
ResponseFormat responseFormat = ResponseFormat.Auto;
WritableMap respInfo;
boolean timeout = false;

ArrayList<String> redirects = new ArrayList<>();

public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
Expand Down Expand Up @@ -200,8 +208,16 @@ else if(this.options.fileCache)
while (it.hasNextKey()) {
String key = it.nextKey();
String value = headers.getString(key);
builder.header(key, value);
mheaders.put(key,value);
if(key.equalsIgnoreCase("RNFB-Response")) {
if(value.equalsIgnoreCase("base64"))
responseFormat = ResponseFormat.BASE64;
else if (value.equalsIgnoreCase("utf8"))
responseFormat = ResponseFormat.UTF8;
}
else {
builder.header(key, value);
mheaders.put(key, value);
}
}
}

Expand Down Expand Up @@ -439,6 +455,10 @@ private void done(Response resp) {
// string correctly, we should do URL encoding before BASE64.
byte[] b = resp.body().bytes();
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
if(responseFormat == ResponseFormat.BASE64) {
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
return;
}
try {
encoder.encode(ByteBuffer.wrap(b).asCharBuffer());
// if the data contains invalid characters the following lines will be
Expand All @@ -449,7 +469,12 @@ private void done(Response resp) {
// This usually mean the data is contains invalid unicode characters, it's
// binary data
catch(CharacterCodingException ignored) {
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
if(responseFormat == ResponseFormat.UTF8) {
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, "");
}
else {
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
}
}
}
} catch (IOException e) {
Expand Down
38 changes: 33 additions & 5 deletions src/ios/RNFetchBlobNetwork.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
NSMutableDictionary * progressTable;
NSMutableDictionary * uploadProgressTable;

typedef NS_ENUM(NSUInteger, ResponseFormat) {
UTF8,
BASE64,
AUTO
};


@interface RNFetchBlobNetwork ()
{
Expand All @@ -37,6 +43,7 @@ @interface RNFetchBlobNetwork ()
NSMutableDictionary * respInfo;
NSInteger respStatus;
NSMutableArray * redirects;
ResponseFormat responseFormat;
}

@end
Expand Down Expand Up @@ -128,6 +135,15 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
self.options = options;
redirects = [[NSMutableArray alloc] init];
[redirects addObject:req.URL.absoluteString];

// set response format
NSString * rnfbResp = [req.allHTTPHeaderFields valueForKey:@"RNFB-Response"];
if([[rnfbResp lowercaseString] isEqualToString:@"base64"])
responseFormat = BASE64;
else if([[rnfbResp lowercaseString] isEqualToString:@"utf8"])
responseFormat = UTF8;
else
responseFormat = AUTO;

NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
Expand Down Expand Up @@ -365,18 +381,30 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom
// if it turns out not to be `nil` that means the response data contains valid UTF8 string,
// in order to properly encode the UTF8 string, use URL encoding before BASE64 encoding.
NSString * utf8 = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];

if(utf8 != nil)

if(responseFormat == BASE64)
{
rnfbRespType = RESP_TYPE_BASE64;
respStr = [respData base64EncodedStringWithOptions:0];
}
else if (responseFormat == UTF8)
{
rnfbRespType = RESP_TYPE_UTF8;
respStr = utf8;
}
else
{
rnfbRespType = RESP_TYPE_BASE64;
respStr = [respData base64EncodedStringWithOptions:0];
if(utf8 != nil)
{
rnfbRespType = RESP_TYPE_UTF8;
respStr = utf8;
}
else
{
rnfbRespType = RESP_TYPE_BASE64;
respStr = [respData base64EncodedStringWithOptions:0];
}
}

}
}

Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-fetch-blob",
"version": "0.9.4",
"version": "0.9.5-beta.2",
"description": "A module provides upload, download, and files access API. Supports file stream read/write for process large files.",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/react-native-fetch-blob.podspec
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Pod::Spec.new do |s|
s.name = "react-native-fetch-blob"
s.version = "0.9.4"
s.version = "0.9.5-beta.2"
s.summary = "A project committed to make file acess and data transfer easier, effiecient for React Native developers."
s.requires_arc = true
s.license = 'MIT'
s.homepage = 'n/a'
s.authors = { "wkh237" => "xeiyan@gmail.com" }
s.source = { :git => "https://github.com/wkh237/react-native-fetch-blob", :tag => 'v0.9.4-beta.3'}
s.source = { :git => "https://github.com/wkh237/react-native-fetch-blob", :tag => 'v0.9.5-beta.2'}
s.source_files = 'ios/**/*.{h,m}'
s.platform = :ios, "7.0"
s.dependency 'React/Core'
Expand Down
54 changes: 54 additions & 0 deletions test/test-0.9.5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import RNTest from './react-native-testkit/'
import React from 'react'
import RNFetchBlob from 'react-native-fetch-blob'
import {
StyleSheet,
Text,
View,
ScrollView,
Platform,
Dimensions,
Image,
TouchableOpacity,
} from 'react-native';

window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
window.Blob = RNFetchBlob.polyfill.Blob

const fs = RNFetchBlob.fs
const { Assert, Comparer, Info, prop } = RNTest
const describe = RNTest.config({
group : '0.9.5',
run : true,
expand : false,
timeout : 20000,
})
const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
const dirs = RNFetchBlob.fs.dirs

let prefix = ((Platform.OS === 'android') ? 'file://' : '')

describe('issue #122 force response data format', (report, done) => {

RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/json-dummy.json`, {
'RNFB-Response' : 'base64'
})
.then((res) => {
let r = RNFetchBlob.base64.decode(res.data)
report(
<Assert key="test data verify" expect="fetchblob-dev" actual={JSON.parse(r).name}/>,
<Assert key="should successfully decode the data" expect={true} actual={true}/>)
return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/json-dummy.json`)
})
.then((res) => {
report(
<Assert key="response should in format of plain-text" expect="fetchblob-dev" actual={JSON.parse(res.data).name}/>)
done()
})
.catch(() => {
report(
<Assert key="Should successfully decode the data" expect={true} actual={false}/>)
done()
})

})

0 comments on commit 301c8cf

Please sign in to comment.