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

Commit 301c8cf

Browse files
committed
Merge branch '0.9.5' into 0.10.0
2 parents 9658ea1 + 595e104 commit 301c8cf

File tree

6 files changed

+119
-12
lines changed

6 files changed

+119
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fetchblob-dev",
3-
"version": "0.9.4",
3+
"version": "0.9.5-beta.2",
44
"private": true,
55
"scripts": {
66
"start": "node node_modules/react-native/local-cli/cli.js start",

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ enum ResponseType {
6363
FileStorage
6464
}
6565

66+
enum ResponseFormat {
67+
Auto,
68+
UTF8,
69+
BASE64
70+
}
71+
6672
public static HashMap<String, Call> taskTable = new HashMap<>();
6773
static HashMap<String, Boolean> progressReport = new HashMap<>();
6874
static HashMap<String, Boolean> uploadProgressReport = new HashMap<>();
@@ -83,8 +89,10 @@ enum ResponseType {
8389
RNFetchBlobBody requestBody;
8490
RequestType requestType;
8591
ResponseType responseType;
92+
ResponseFormat responseFormat = ResponseFormat.Auto;
8693
WritableMap respInfo;
8794
boolean timeout = false;
95+
8896
ArrayList<String> redirects = new ArrayList<>();
8997

9098
public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
@@ -200,8 +208,16 @@ else if(this.options.fileCache)
200208
while (it.hasNextKey()) {
201209
String key = it.nextKey();
202210
String value = headers.getString(key);
203-
builder.header(key, value);
204-
mheaders.put(key,value);
211+
if(key.equalsIgnoreCase("RNFB-Response")) {
212+
if(value.equalsIgnoreCase("base64"))
213+
responseFormat = ResponseFormat.BASE64;
214+
else if (value.equalsIgnoreCase("utf8"))
215+
responseFormat = ResponseFormat.UTF8;
216+
}
217+
else {
218+
builder.header(key, value);
219+
mheaders.put(key, value);
220+
}
205221
}
206222
}
207223

@@ -439,6 +455,10 @@ private void done(Response resp) {
439455
// string correctly, we should do URL encoding before BASE64.
440456
byte[] b = resp.body().bytes();
441457
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
458+
if(responseFormat == ResponseFormat.BASE64) {
459+
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
460+
return;
461+
}
442462
try {
443463
encoder.encode(ByteBuffer.wrap(b).asCharBuffer());
444464
// if the data contains invalid characters the following lines will be
@@ -449,7 +469,12 @@ private void done(Response resp) {
449469
// This usually mean the data is contains invalid unicode characters, it's
450470
// binary data
451471
catch(CharacterCodingException ignored) {
452-
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
472+
if(responseFormat == ResponseFormat.UTF8) {
473+
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, "");
474+
}
475+
else {
476+
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
477+
}
453478
}
454479
}
455480
} catch (IOException e) {

src/ios/RNFetchBlobNetwork.m

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
NSMutableDictionary * progressTable;
2828
NSMutableDictionary * uploadProgressTable;
2929

30+
typedef NS_ENUM(NSUInteger, ResponseFormat) {
31+
UTF8,
32+
BASE64,
33+
AUTO
34+
};
35+
3036

3137
@interface RNFetchBlobNetwork ()
3238
{
@@ -37,6 +43,7 @@ @interface RNFetchBlobNetwork ()
3743
NSMutableDictionary * respInfo;
3844
NSInteger respStatus;
3945
NSMutableArray * redirects;
46+
ResponseFormat responseFormat;
4047
}
4148

4249
@end
@@ -128,6 +135,15 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
128135
self.options = options;
129136
redirects = [[NSMutableArray alloc] init];
130137
[redirects addObject:req.URL.absoluteString];
138+
139+
// set response format
140+
NSString * rnfbResp = [req.allHTTPHeaderFields valueForKey:@"RNFB-Response"];
141+
if([[rnfbResp lowercaseString] isEqualToString:@"base64"])
142+
responseFormat = BASE64;
143+
else if([[rnfbResp lowercaseString] isEqualToString:@"utf8"])
144+
responseFormat = UTF8;
145+
else
146+
responseFormat = AUTO;
131147

132148
NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
133149
NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
@@ -365,18 +381,30 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom
365381
// if it turns out not to be `nil` that means the response data contains valid UTF8 string,
366382
// in order to properly encode the UTF8 string, use URL encoding before BASE64 encoding.
367383
NSString * utf8 = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];
368-
369-
if(utf8 != nil)
384+
385+
if(responseFormat == BASE64)
386+
{
387+
rnfbRespType = RESP_TYPE_BASE64;
388+
respStr = [respData base64EncodedStringWithOptions:0];
389+
}
390+
else if (responseFormat == UTF8)
370391
{
371392
rnfbRespType = RESP_TYPE_UTF8;
372393
respStr = utf8;
373394
}
374395
else
375396
{
376-
rnfbRespType = RESP_TYPE_BASE64;
377-
respStr = [respData base64EncodedStringWithOptions:0];
397+
if(utf8 != nil)
398+
{
399+
rnfbRespType = RESP_TYPE_UTF8;
400+
respStr = utf8;
401+
}
402+
else
403+
{
404+
rnfbRespType = RESP_TYPE_BASE64;
405+
respStr = [respData base64EncodedStringWithOptions:0];
406+
}
378407
}
379-
380408
}
381409
}
382410

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-fetch-blob",
3-
"version": "0.9.4",
3+
"version": "0.9.5-beta.2",
44
"description": "A module provides upload, download, and files access API. Supports file stream read/write for process large files.",
55
"main": "index.js",
66
"scripts": {

src/react-native-fetch-blob.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
Pod::Spec.new do |s|
22
s.name = "react-native-fetch-blob"
3-
s.version = "0.9.4"
3+
s.version = "0.9.5-beta.2"
44
s.summary = "A project committed to make file acess and data transfer easier, effiecient for React Native developers."
55
s.requires_arc = true
66
s.license = 'MIT'
77
s.homepage = 'n/a'
88
s.authors = { "wkh237" => "[email protected]" }
9-
s.source = { :git => "https://github.com/wkh237/react-native-fetch-blob", :tag => 'v0.9.4-beta.3'}
9+
s.source = { :git => "https://github.com/wkh237/react-native-fetch-blob", :tag => 'v0.9.5-beta.2'}
1010
s.source_files = 'ios/**/*.{h,m}'
1111
s.platform = :ios, "7.0"
1212
s.dependency 'React/Core'

test/test-0.9.5.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import RNTest from './react-native-testkit/'
2+
import React from 'react'
3+
import RNFetchBlob from 'react-native-fetch-blob'
4+
import {
5+
StyleSheet,
6+
Text,
7+
View,
8+
ScrollView,
9+
Platform,
10+
Dimensions,
11+
Image,
12+
TouchableOpacity,
13+
} from 'react-native';
14+
15+
window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
16+
window.Blob = RNFetchBlob.polyfill.Blob
17+
18+
const fs = RNFetchBlob.fs
19+
const { Assert, Comparer, Info, prop } = RNTest
20+
const describe = RNTest.config({
21+
group : '0.9.5',
22+
run : true,
23+
expand : false,
24+
timeout : 20000,
25+
})
26+
const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
27+
const dirs = RNFetchBlob.fs.dirs
28+
29+
let prefix = ((Platform.OS === 'android') ? 'file://' : '')
30+
31+
describe('issue #122 force response data format', (report, done) => {
32+
33+
RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/json-dummy.json`, {
34+
'RNFB-Response' : 'base64'
35+
})
36+
.then((res) => {
37+
let r = RNFetchBlob.base64.decode(res.data)
38+
report(
39+
<Assert key="test data verify" expect="fetchblob-dev" actual={JSON.parse(r).name}/>,
40+
<Assert key="should successfully decode the data" expect={true} actual={true}/>)
41+
return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/json-dummy.json`)
42+
})
43+
.then((res) => {
44+
report(
45+
<Assert key="response should in format of plain-text" expect="fetchblob-dev" actual={JSON.parse(res.data).name}/>)
46+
done()
47+
})
48+
.catch(() => {
49+
report(
50+
<Assert key="Should successfully decode the data" expect={true} actual={false}/>)
51+
done()
52+
})
53+
54+
})

0 commit comments

Comments
 (0)