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

Commit

Permalink
Fix Android content type and content length issue #94
Browse files Browse the repository at this point in the history
  • Loading branch information
wkh237 committed Aug 18, 2016
1 parent 4791571 commit c7fc337
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 23 deletions.
39 changes: 29 additions & 10 deletions src/android/src/main/java/com/RNFetchBlob/RNFetchBlobBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public RNFetchBlobBody(String taskId, RNFetchBlobReq.RequestType type, String ra
requestType = type;
this.rawBody = rawBody;
mime = contentType;
if(rawBody != null) {
if(requestType == RNFetchBlobReq.RequestType.AsIs)
contentLength = rawBody.length();
else
contentLength = caculateOctetContentLength();
}
}

@Override
Expand Down Expand Up @@ -96,7 +102,19 @@ public void writeTo(BufferedSink sink) throws IOException {
buffer.flush();
}

private void caculateOctetContentLength() {
boolean clearRequestBody() {
try {
if (bodyCache != null && bodyCache.exists()) {
bodyCache.delete();
}
} catch(Exception e) {
RNFetchBlobUtils.emitWarningEvent(e.getLocalizedMessage());
return false;
}
return true;
}

private long caculateOctetContentLength() {
long total = 0;
// upload from storage
if (rawBody.startsWith(RNFetchBlobConst.FILE_PREFIX)) {
Expand All @@ -106,31 +124,32 @@ private void caculateOctetContentLength() {
if (RNFetchBlobFS.isAsset(orgPath)) {
try {
String assetName = orgPath.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
contentLength = RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
total += RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
requestStream = RNFetchBlob.RCTContext.getAssets().open(assetName);
} catch (IOException e) {
// e.printStackTrace();
RNFetchBlobUtils.emitWarningEvent(e.getLocalizedMessage());
}
} else {
File f = new File(RNFetchBlobFS.normalizePath(orgPath));
try {
if(!f.exists())
f.createNewFile();
contentLength = f.length();
total += f.length();
requestStream = new FileInputStream(f);
} catch (Exception e) {
// callback.invoke(e.getLocalizedMessage(), null);
RNFetchBlobUtils.emitWarningEvent("RNetchBlob error when counting content length: " +e.getLocalizedMessage());
}
}
} else {
try {
byte[] bytes = Base64.decode(rawBody, 0);
contentLength = bytes.length;
requestStream = new ByteArrayInputStream(bytes);
total += requestStream.available();
} catch(Exception ex) {
Log.e("error", ex.getLocalizedMessage());
RNFetchBlobUtils.emitWarningEvent("RNetchBlob error when counting content length: " +ex.getLocalizedMessage());
}
}
return total;
}

/**
Expand Down Expand Up @@ -173,7 +192,7 @@ private File createMultipartBodyCache() throws IOException {
InputStream in = ctx.getAssets().open(assetName);
pipeStreamToFileStream(in, os);
} catch (IOException e) {
Log.e("RNFetchBlob", "Failed to create form data asset :" + orgPath + ", " + e.getLocalizedMessage() );
RNFetchBlobUtils.emitWarningEvent("RNFetchBlob Failed to create form data asset :" + orgPath + ", " + e.getLocalizedMessage() );
}
}
// data from normal files
Expand All @@ -184,7 +203,7 @@ private File createMultipartBodyCache() throws IOException {
pipeStreamToFileStream(fs, os);
}
else {
Log.e("RNFetchBlob", "Failed to create form data from path :" + orgPath + "file not exists.");
RNFetchBlobUtils.emitWarningEvent("RNFetchBlob Failed to create form data from path :" + orgPath + ", file not exists.");
}
}
}
Expand Down Expand Up @@ -284,7 +303,7 @@ private ArrayList<FormField> countFormDataLength() {
long length = ctx.getAssets().open(assetName).available();
total += length;
} catch (IOException e) {

RNFetchBlobUtils.emitWarningEvent(e.getLocalizedMessage());
}
}
// general files
Expand Down
41 changes: 28 additions & 13 deletions src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -81,6 +82,7 @@ enum ResponseType {
Callback callback;
long contentLength;
long downloadManagerId;
RNFetchBlobBody requestBody;
RequestType requestType;
ResponseType responseType;
WritableMap respInfo;
Expand Down Expand Up @@ -207,7 +209,10 @@ else if(this.options.fileCache == true)
if(method.equalsIgnoreCase("post") || method.equalsIgnoreCase("put")) {
String cType = getHeaderIgnoreCases(mheaders, "Content-Type").toLowerCase();

if(cType == null) {
if(rawRequestBodyArray != null) {
requestType = RequestType.Form;
}
else if(cType == null || cType.isEmpty()) {
builder.header("Content-Type", "application/octet-stream");
requestType = RequestType.SingleFile;
}
Expand Down Expand Up @@ -235,29 +240,32 @@ else if(this.options.fileCache == true)
// set request body
switch (requestType) {
case SingleFile:
builder.method(method, new RNFetchBlobBody(
requestBody = new RNFetchBlobBody(
taskId,
requestType,
rawRequestBody,
MediaType.parse(getHeaderIgnoreCases(mheaders, "content-type"))
));
);
builder.method(method, requestBody);
break;
case AsIs:
builder.method(method, new RNFetchBlobBody(
requestBody = new RNFetchBlobBody(
taskId,
requestType,
rawRequestBody,
MediaType.parse(getHeaderIgnoreCases(mheaders, "content-type"))
));
);
builder.method(method, requestBody);
break;
case Form:
String boundary = "RNFetchBlob-" + taskId;
builder.method(method, new RNFetchBlobBody(
requestBody = new RNFetchBlobBody(
taskId,
requestType,
rawRequestBodyArray,
MediaType.parse("multipart/form-data; boundary="+ boundary)
));
);
builder.method(method, requestBody);
break;

case WithoutBody:
Expand Down Expand Up @@ -301,8 +309,13 @@ public Response intercept(Chain chain) throws IOException {
break;
}
return originalResponse.newBuilder().body(extended).build();
} catch(Exception ex) {
RNFetchBlobUtils.emitWarningEvent(ex.getLocalizedMessage());
}
catch (SocketException ex) {
timeout = true;
}
catch(Exception ex) {
RNFetchBlobUtils.emitWarningEvent("RNFetchBlob error when sending request : " + ex.getLocalizedMessage());

}
return chain.proceed(chain.request());
}
Expand Down Expand Up @@ -337,7 +350,7 @@ public void onFailure(Call call, IOException e) {
}
else
callback.invoke(e.getLocalizedMessage(), null, null);
removeTaskInfo();
releaseTaskResource();
}

@Override
Expand Down Expand Up @@ -367,21 +380,23 @@ public void onResponse(Call call, Response response) throws IOException {

} catch (Exception error) {
error.printStackTrace();
taskTable.remove(taskId);
releaseTaskResource();
callback.invoke("RNFetchBlob request error: " + error.getMessage() + error.getCause());
}
}

/**
* Remove cached information of the HTTP task
*/
private void removeTaskInfo() {
private void releaseTaskResource() {
if(taskTable.containsKey(taskId))
taskTable.remove(taskId);
if(uploadProgressReport.containsKey(taskId))
uploadProgressReport.remove(taskId);
if(progressReport.containsKey(taskId))
progressReport.remove(taskId);
if(requestBody != null)
requestBody.clearRequestBody();
}

/**
Expand Down Expand Up @@ -455,7 +470,7 @@ private void done(Response resp) {
}
if(!resp.isSuccessful())
resp.body().close();
removeTaskInfo();
releaseTaskResource();
}

/**
Expand Down

0 comments on commit c7fc337

Please sign in to comment.