|
@@ -19,6 +19,7 @@ import com.facebook.react.bridge.ReadableArray;
|
19
|
19
|
import com.facebook.react.bridge.ReadableMap;
|
20
|
20
|
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
21
|
21
|
import com.facebook.react.bridge.WritableMap;
|
|
22
|
+import com.facebook.react.modules.core.DeviceEventManagerModule;
|
22
|
23
|
|
23
|
24
|
import java.io.ByteArrayInputStream;
|
24
|
25
|
import java.io.File;
|
|
@@ -41,6 +42,7 @@ import okhttp3.Response;
|
41
|
42
|
import okhttp3.ResponseBody;
|
42
|
43
|
import okhttp3.FormBody;
|
43
|
44
|
import okhttp3.internal.framed.Header;
|
|
45
|
+import okhttp3.internal.http.OkHeaders;
|
44
|
46
|
|
45
|
47
|
/**
|
46
|
48
|
* Created by wkh237 on 2016/6/21.
|
|
@@ -49,8 +51,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
49
|
51
|
|
50
|
52
|
enum RequestType {
|
51
|
53
|
Form,
|
52
|
|
- Encoded,
|
53
|
54
|
SingleFile,
|
|
55
|
+ AsIs,
|
54
|
56
|
WithoutBody,
|
55
|
57
|
Others
|
56
|
58
|
};
|
|
@@ -88,14 +90,13 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
88
|
90
|
this.rawRequestBody = body;
|
89
|
91
|
this.rawRequestBodyArray = arrayBody;
|
90
|
92
|
|
91
|
|
- if(this.options.fileCache == true || this.options.path != null)
|
|
93
|
+ if(this.options.fileCache || this.options.path != null)
|
92
|
94
|
responseType = ResponseType.FileStorage;
|
93
|
95
|
else
|
94
|
96
|
responseType = ResponseType.KeepInMemory;
|
95
|
97
|
|
96
|
|
- if (body != null && headers.hasKey("content-type") && "application/x-www-form-urlencoded".equals(headers.getString("content-type")))
|
97
|
|
- requestType = RequestType.Encoded;
|
98
|
|
- else if (body != null)
|
|
98
|
+
|
|
99
|
+ if (body != null)
|
99
|
100
|
requestType = RequestType.SingleFile;
|
100
|
101
|
else if (arrayBody != null)
|
101
|
102
|
requestType = RequestType.Form;
|
|
@@ -182,6 +183,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
182
|
183
|
} catch (MalformedURLException e) {
|
183
|
184
|
e.printStackTrace();
|
184
|
185
|
}
|
|
186
|
+
|
|
187
|
+ HashMap<String, String> mheaders = new HashMap<>();
|
185
|
188
|
// set headers
|
186
|
189
|
if (headers != null) {
|
187
|
190
|
ReadableMapKeySetIterator it = headers.keySetIterator();
|
|
@@ -189,42 +192,60 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
189
|
192
|
String key = it.nextKey();
|
190
|
193
|
String value = headers.getString(key);
|
191
|
194
|
builder.header(key, value);
|
|
195
|
+ mheaders.put(key,value);
|
192
|
196
|
}
|
193
|
197
|
}
|
194
|
198
|
|
|
199
|
+ if(method.equalsIgnoreCase("post") || method.equalsIgnoreCase("put")) {
|
|
200
|
+ String cType = getHeaderIgnoreCases(mheaders, "content-type").toLowerCase();
|
|
201
|
+
|
|
202
|
+ if(cType == null) {
|
|
203
|
+ builder.header("Content-Type", "application/octet-stream");
|
|
204
|
+ requestType = RequestType.SingleFile;
|
|
205
|
+ }
|
|
206
|
+ if(rawRequestBody != null) {
|
|
207
|
+ if(rawRequestBody.startsWith(RNFetchBlobConst.FILE_PREFIX)) {
|
|
208
|
+ requestType = RequestType.SingleFile;
|
|
209
|
+ }
|
|
210
|
+ else if (cType.contains(";base64") || cType.startsWith("application/octet")) {
|
|
211
|
+ requestType = RequestType.SingleFile;
|
|
212
|
+ } else {
|
|
213
|
+ requestType = RequestType.AsIs;
|
|
214
|
+ }
|
|
215
|
+ }
|
|
216
|
+ }
|
|
217
|
+ else {
|
|
218
|
+ requestType = RequestType.WithoutBody;
|
|
219
|
+ }
|
|
220
|
+
|
|
221
|
+
|
195
|
222
|
// set request body
|
196
|
223
|
switch (requestType) {
|
197
|
224
|
case SingleFile:
|
198
|
225
|
builder.method(method, new RNFetchBlobBody(
|
199
|
226
|
taskId,
|
200
|
|
- RequestType.SingleFile,
|
|
227
|
+ requestType,
|
201
|
228
|
rawRequestBody,
|
202
|
|
- RNFetchBlobConst.MIME_OCTET
|
|
229
|
+ MediaType.parse(getHeaderIgnoreCases(mheaders, "content-type"))
|
|
230
|
+ ));
|
|
231
|
+ break;
|
|
232
|
+ case AsIs:
|
|
233
|
+ builder.method(method, new RNFetchBlobBody(
|
|
234
|
+ taskId,
|
|
235
|
+ requestType,
|
|
236
|
+ rawRequestBody,
|
|
237
|
+ MediaType.parse(getHeaderIgnoreCases(mheaders, "content-type"))
|
203
|
238
|
));
|
204
|
239
|
break;
|
205
|
240
|
case Form:
|
206
|
241
|
builder.method(method, new RNFetchBlobBody(
|
207
|
242
|
taskId,
|
208
|
|
- RequestType.Form,
|
|
243
|
+ requestType,
|
209
|
244
|
rawRequestBodyArray,
|
210
|
245
|
MediaType.parse("multipart/form-data; boundary=RNFetchBlob-" + taskId)
|
211
|
246
|
));
|
212
|
247
|
break;
|
213
|
|
- case Encoded:
|
214
|
|
- // rawRequestBody has an expected format of
|
215
|
|
- // key1=value1&key2=value&...
|
216
|
|
- FormBody.Builder formBuilder = new FormBody.Builder();
|
217
|
248
|
|
218
|
|
- String[] pairs = rawRequestBody.split("&");
|
219
|
|
- for ( String pair : pairs ) {
|
220
|
|
- String[] kv = pair.split("=");
|
221
|
|
- formBuilder.add(kv[0], kv[1]);
|
222
|
|
- }
|
223
|
|
-
|
224
|
|
- RequestBody body = formBuilder.build();
|
225
|
|
-
|
226
|
|
- builder.method(method, body);
|
227
|
|
- break;
|
228
|
249
|
case WithoutBody:
|
229
|
250
|
builder.method(method, null);
|
230
|
251
|
break;
|
|
@@ -309,6 +330,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
309
|
330
|
* @param resp OkHttp response object
|
310
|
331
|
*/
|
311
|
332
|
private void done(Response resp) {
|
|
333
|
+ emitStateEvent(getResponseInfo(resp));
|
312
|
334
|
switch (responseType) {
|
313
|
335
|
case KeepInMemory:
|
314
|
336
|
try {
|
|
@@ -402,18 +424,15 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
402
|
424
|
return headers.get(field.toLowerCase()) == null ? "" : headers.get(field.toLowerCase());
|
403
|
425
|
}
|
404
|
426
|
|
405
|
|
- /**
|
406
|
|
- * Build request body by given string
|
407
|
|
- * @param body Content of request body in UTF8 string format.
|
408
|
|
- * @return
|
409
|
|
- */
|
410
|
|
- RequestBody buildRawBody(String body) {
|
411
|
|
- if(body != null) {
|
412
|
|
- this.contentType = MediaType.parse(options.mime);
|
413
|
|
- return RequestBody.create(this.contentType, body);
|
414
|
|
- }
|
415
|
|
- return null;
|
|
427
|
+ private String getHeaderIgnoreCases(HashMap<String,String> headers, String field) {
|
|
428
|
+ String val = headers.get(field);
|
|
429
|
+ if(val != null) return val;
|
|
430
|
+ return headers.get(field.toLowerCase()) == null ? "" : headers.get(field.toLowerCase());
|
|
431
|
+ }
|
416
|
432
|
|
|
433
|
+ private void emitStateEvent(WritableMap args) {
|
|
434
|
+ RNFetchBlob.RCTContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
435
|
+ .emit(RNFetchBlobConst.EVENT_HTTP_STATE, args);
|
417
|
436
|
}
|
418
|
437
|
|
419
|
438
|
@Override
|
|
@@ -436,10 +455,10 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
436
|
455
|
cursor.moveToFirst();
|
437
|
456
|
String filePath = cursor.getString(0);
|
438
|
457
|
cursor.close();
|
439
|
|
- this.callback.invoke(null, filePath);
|
|
458
|
+ this.callback.invoke(null, null, filePath);
|
440
|
459
|
}
|
441
|
460
|
else
|
442
|
|
- this.callback.invoke(null, null);
|
|
461
|
+ this.callback.invoke(null, null, null);
|
443
|
462
|
}
|
444
|
463
|
}
|
445
|
464
|
}
|