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

Commit

Permalink
Add response format option #122
Browse files Browse the repository at this point in the history
  • Loading branch information
wkh237 committed Sep 8, 2016
1 parent d22c5c9 commit 8de6725
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
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 @@ -437,6 +453,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 @@ -447,7 +467,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 @@ -357,18 +373,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

0 comments on commit 8de6725

Please sign in to comment.