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

Commit c7fc337

Browse files
committed
Fix Android content type and content length issue #94
1 parent 4791571 commit c7fc337

File tree

2 files changed

+57
-23
lines changed

2 files changed

+57
-23
lines changed

Diff for: src/android/src/main/java/com/RNFetchBlob/RNFetchBlobBody.java

+29-10
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ public RNFetchBlobBody(String taskId, RNFetchBlobReq.RequestType type, String ra
6565
requestType = type;
6666
this.rawBody = rawBody;
6767
mime = contentType;
68+
if(rawBody != null) {
69+
if(requestType == RNFetchBlobReq.RequestType.AsIs)
70+
contentLength = rawBody.length();
71+
else
72+
contentLength = caculateOctetContentLength();
73+
}
6874
}
6975

7076
@Override
@@ -96,7 +102,19 @@ public void writeTo(BufferedSink sink) throws IOException {
96102
buffer.flush();
97103
}
98104

99-
private void caculateOctetContentLength() {
105+
boolean clearRequestBody() {
106+
try {
107+
if (bodyCache != null && bodyCache.exists()) {
108+
bodyCache.delete();
109+
}
110+
} catch(Exception e) {
111+
RNFetchBlobUtils.emitWarningEvent(e.getLocalizedMessage());
112+
return false;
113+
}
114+
return true;
115+
}
116+
117+
private long caculateOctetContentLength() {
100118
long total = 0;
101119
// upload from storage
102120
if (rawBody.startsWith(RNFetchBlobConst.FILE_PREFIX)) {
@@ -106,31 +124,32 @@ private void caculateOctetContentLength() {
106124
if (RNFetchBlobFS.isAsset(orgPath)) {
107125
try {
108126
String assetName = orgPath.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
109-
contentLength = RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
127+
total += RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
110128
requestStream = RNFetchBlob.RCTContext.getAssets().open(assetName);
111129
} catch (IOException e) {
112-
// e.printStackTrace();
130+
RNFetchBlobUtils.emitWarningEvent(e.getLocalizedMessage());
113131
}
114132
} else {
115133
File f = new File(RNFetchBlobFS.normalizePath(orgPath));
116134
try {
117135
if(!f.exists())
118136
f.createNewFile();
119-
contentLength = f.length();
137+
total += f.length();
120138
requestStream = new FileInputStream(f);
121139
} catch (Exception e) {
122-
// callback.invoke(e.getLocalizedMessage(), null);
140+
RNFetchBlobUtils.emitWarningEvent("RNetchBlob error when counting content length: " +e.getLocalizedMessage());
123141
}
124142
}
125143
} else {
126144
try {
127145
byte[] bytes = Base64.decode(rawBody, 0);
128-
contentLength = bytes.length;
129146
requestStream = new ByteArrayInputStream(bytes);
147+
total += requestStream.available();
130148
} catch(Exception ex) {
131-
Log.e("error", ex.getLocalizedMessage());
149+
RNFetchBlobUtils.emitWarningEvent("RNetchBlob error when counting content length: " +ex.getLocalizedMessage());
132150
}
133151
}
152+
return total;
134153
}
135154

136155
/**
@@ -173,7 +192,7 @@ private File createMultipartBodyCache() throws IOException {
173192
InputStream in = ctx.getAssets().open(assetName);
174193
pipeStreamToFileStream(in, os);
175194
} catch (IOException e) {
176-
Log.e("RNFetchBlob", "Failed to create form data asset :" + orgPath + ", " + e.getLocalizedMessage() );
195+
RNFetchBlobUtils.emitWarningEvent("RNFetchBlob Failed to create form data asset :" + orgPath + ", " + e.getLocalizedMessage() );
177196
}
178197
}
179198
// data from normal files
@@ -184,7 +203,7 @@ private File createMultipartBodyCache() throws IOException {
184203
pipeStreamToFileStream(fs, os);
185204
}
186205
else {
187-
Log.e("RNFetchBlob", "Failed to create form data from path :" + orgPath + "file not exists.");
206+
RNFetchBlobUtils.emitWarningEvent("RNFetchBlob Failed to create form data from path :" + orgPath + ", file not exists.");
188207
}
189208
}
190209
}
@@ -284,7 +303,7 @@ private ArrayList<FormField> countFormDataLength() {
284303
long length = ctx.getAssets().open(assetName).available();
285304
total += length;
286305
} catch (IOException e) {
287-
306+
RNFetchBlobUtils.emitWarningEvent(e.getLocalizedMessage());
288307
}
289308
}
290309
// general files

Diff for: src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

+28-13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.IOException;
2626
import java.io.InputStream;
2727
import java.net.MalformedURLException;
28+
import java.net.SocketException;
2829
import java.net.SocketTimeoutException;
2930
import java.net.URL;
3031
import java.nio.ByteBuffer;
@@ -81,6 +82,7 @@ enum ResponseType {
8182
Callback callback;
8283
long contentLength;
8384
long downloadManagerId;
85+
RNFetchBlobBody requestBody;
8486
RequestType requestType;
8587
ResponseType responseType;
8688
WritableMap respInfo;
@@ -207,7 +209,10 @@ else if(this.options.fileCache == true)
207209
if(method.equalsIgnoreCase("post") || method.equalsIgnoreCase("put")) {
208210
String cType = getHeaderIgnoreCases(mheaders, "Content-Type").toLowerCase();
209211

210-
if(cType == null) {
212+
if(rawRequestBodyArray != null) {
213+
requestType = RequestType.Form;
214+
}
215+
else if(cType == null || cType.isEmpty()) {
211216
builder.header("Content-Type", "application/octet-stream");
212217
requestType = RequestType.SingleFile;
213218
}
@@ -235,29 +240,32 @@ else if(this.options.fileCache == true)
235240
// set request body
236241
switch (requestType) {
237242
case SingleFile:
238-
builder.method(method, new RNFetchBlobBody(
243+
requestBody = new RNFetchBlobBody(
239244
taskId,
240245
requestType,
241246
rawRequestBody,
242247
MediaType.parse(getHeaderIgnoreCases(mheaders, "content-type"))
243-
));
248+
);
249+
builder.method(method, requestBody);
244250
break;
245251
case AsIs:
246-
builder.method(method, new RNFetchBlobBody(
252+
requestBody = new RNFetchBlobBody(
247253
taskId,
248254
requestType,
249255
rawRequestBody,
250256
MediaType.parse(getHeaderIgnoreCases(mheaders, "content-type"))
251-
));
257+
);
258+
builder.method(method, requestBody);
252259
break;
253260
case Form:
254261
String boundary = "RNFetchBlob-" + taskId;
255-
builder.method(method, new RNFetchBlobBody(
262+
requestBody = new RNFetchBlobBody(
256263
taskId,
257264
requestType,
258265
rawRequestBodyArray,
259266
MediaType.parse("multipart/form-data; boundary="+ boundary)
260-
));
267+
);
268+
builder.method(method, requestBody);
261269
break;
262270

263271
case WithoutBody:
@@ -301,8 +309,13 @@ public Response intercept(Chain chain) throws IOException {
301309
break;
302310
}
303311
return originalResponse.newBuilder().body(extended).build();
304-
} catch(Exception ex) {
305-
RNFetchBlobUtils.emitWarningEvent(ex.getLocalizedMessage());
312+
}
313+
catch (SocketException ex) {
314+
timeout = true;
315+
}
316+
catch(Exception ex) {
317+
RNFetchBlobUtils.emitWarningEvent("RNFetchBlob error when sending request : " + ex.getLocalizedMessage());
318+
306319
}
307320
return chain.proceed(chain.request());
308321
}
@@ -337,7 +350,7 @@ public void onFailure(Call call, IOException e) {
337350
}
338351
else
339352
callback.invoke(e.getLocalizedMessage(), null, null);
340-
removeTaskInfo();
353+
releaseTaskResource();
341354
}
342355

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

368381
} catch (Exception error) {
369382
error.printStackTrace();
370-
taskTable.remove(taskId);
383+
releaseTaskResource();
371384
callback.invoke("RNFetchBlob request error: " + error.getMessage() + error.getCause());
372385
}
373386
}
374387

375388
/**
376389
* Remove cached information of the HTTP task
377390
*/
378-
private void removeTaskInfo() {
391+
private void releaseTaskResource() {
379392
if(taskTable.containsKey(taskId))
380393
taskTable.remove(taskId);
381394
if(uploadProgressReport.containsKey(taskId))
382395
uploadProgressReport.remove(taskId);
383396
if(progressReport.containsKey(taskId))
384397
progressReport.remove(taskId);
398+
if(requestBody != null)
399+
requestBody.clearRequestBody();
385400
}
386401

387402
/**
@@ -455,7 +470,7 @@ private void done(Response resp) {
455470
}
456471
if(!resp.isSuccessful())
457472
resp.body().close();
458-
removeTaskInfo();
473+
releaseTaskResource();
459474
}
460475

461476
/**

0 commit comments

Comments
 (0)