Skip to content

Commit 6801d80

Browse files
authored
Merge pull request #112 from halaei/explicit-locale
Use root locale for string operations
2 parents a7d6056 + 46876cb commit 6801d80

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilConfig.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.facebook.react.bridge.ReadableArray;
44
import com.facebook.react.bridge.ReadableMap;
55

6+
import java.util.Locale;
7+
68
class ReactNativeBlobUtilConfig {
79

810
public Boolean fileCache;
@@ -33,7 +35,7 @@ class ReactNativeBlobUtilConfig {
3335
}
3436
if (options.hasKey("binaryContentTypes"))
3537
this.binaryContentTypes = options.getArray("binaryContentTypes");
36-
if (this.path != null && path.toLowerCase().contains("?append=true")) {
38+
if (this.path != null && path.toLowerCase(Locale.ROOT).contains("?append=true")) {
3739
this.overwrite = false;
3840
}
3941
if (options.hasKey("overwrite"))

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.security.MessageDigest;
2626
import java.util.ArrayList;
2727
import java.util.HashMap;
28+
import java.util.Locale;
2829
import java.util.Map;
2930
import java.util.UUID;
3031

@@ -209,7 +210,7 @@ else if (resolved == null) {
209210
return;
210211
}
211212

212-
switch (encoding.toLowerCase()) {
213+
switch (encoding.toLowerCase(Locale.ROOT)) {
213214
case "base64":
214215
promise.resolve(Base64.encodeToString(bytes, Base64.NO_WRAP));
215216
break;
@@ -1095,7 +1096,7 @@ protected Integer doInBackground(ReadableArray... paths) {
10951096
private static byte[] stringToBytes(String data, String encoding) {
10961097
if (encoding.equalsIgnoreCase("ascii")) {
10971098
return data.getBytes(Charset.forName("US-ASCII"));
1098-
} else if (encoding.toLowerCase().contains("base64")) {
1099+
} else if (encoding.toLowerCase(Locale.ROOT).contains("base64")) {
10991100
return Base64.decode(data, Base64.NO_WRAP);
11001101

11011102
} else if (encoding.equalsIgnoreCase("utf8")) {

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import java.util.List;
5555
import java.util.HashMap;
5656

57+
import java.util.Locale;
5758
import java.util.concurrent.TimeUnit;
5859

5960
import javax.net.ssl.SSLSocketFactory;
@@ -120,7 +121,7 @@ enum ResponseFormat {
120121
OkHttpClient client;
121122

122123
public ReactNativeBlobUtilReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, OkHttpClient client, final Callback callback) {
123-
this.method = method.toUpperCase();
124+
this.method = method.toUpperCase(Locale.ROOT);
124125
this.options = new ReactNativeBlobUtilConfig(options);
125126
this.taskId = taskId;
126127
this.url = url;
@@ -311,14 +312,14 @@ else if (this.options.fileCache)
311312
else if (value.equalsIgnoreCase("utf8"))
312313
responseFormat = ResponseFormat.UTF8;
313314
} else {
314-
builder.header(key.toLowerCase(), value);
315-
mheaders.put(key.toLowerCase(), value);
315+
builder.header(key.toLowerCase(Locale.ROOT), value);
316+
mheaders.put(key.toLowerCase(Locale.ROOT), value);
316317
}
317318
}
318319
}
319320

320321
if (method.equalsIgnoreCase("post") || method.equalsIgnoreCase("put") || method.equalsIgnoreCase("patch")) {
321-
String cType = getHeaderIgnoreCases(mheaders, "Content-Type").toLowerCase();
322+
String cType = getHeaderIgnoreCases(mheaders, "Content-Type").toLowerCase(Locale.ROOT);
322323

323324
if (rawRequestBodyArray != null) {
324325
requestType = RequestType.Form;
@@ -332,7 +333,7 @@ else if (value.equalsIgnoreCase("utf8"))
332333
if (rawRequestBody.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX)
333334
|| rawRequestBody.startsWith(ReactNativeBlobUtilConst.CONTENT_PREFIX)) {
334335
requestType = RequestType.SingleFile;
335-
} else if (cType.toLowerCase().contains(";base64") || cType.toLowerCase().startsWith("application/octet")) {
336+
} else if (cType.toLowerCase(Locale.ROOT).contains(";base64") || cType.toLowerCase(Locale.ROOT).startsWith("application/octet")) {
336337
cType = cType.replace(";base64", "").replace(";BASE64", "");
337338
if (mheaders.containsKey("content-type"))
338339
mheaders.put("content-type", cType);
@@ -718,7 +719,7 @@ private boolean isBlobResponse(Response resp) {
718719
boolean isCustomBinary = false;
719720
if (options.binaryContentTypes != null) {
720721
for (int i = 0; i < options.binaryContentTypes.size(); i++) {
721-
if (ctype.toLowerCase().contains(options.binaryContentTypes.getString(i).toLowerCase())) {
722+
if (ctype.toLowerCase(Locale.ROOT).contains(options.binaryContentTypes.getString(i).toLowerCase(Locale.ROOT))) {
722723
isCustomBinary = true;
723724
break;
724725
}
@@ -730,13 +731,13 @@ private boolean isBlobResponse(Response resp) {
730731
private String getHeaderIgnoreCases(Headers headers, String field) {
731732
String val = headers.get(field);
732733
if (val != null) return val;
733-
return headers.get(field.toLowerCase()) == null ? "" : headers.get(field.toLowerCase());
734+
return headers.get(field.toLowerCase(Locale.ROOT)) == null ? "" : headers.get(field.toLowerCase(Locale.ROOT));
734735
}
735736

736737
private String getHeaderIgnoreCases(HashMap<String, String> headers, String field) {
737738
String val = headers.get(field);
738739
if (val != null) return val;
739-
String lowerCasedValue = headers.get(field.toLowerCase());
740+
String lowerCasedValue = headers.get(field.toLowerCase(Locale.ROOT));
740741
return lowerCasedValue == null ? "" : lowerCasedValue;
741742
}
742743

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.security.MessageDigest;
88
import java.security.cert.CertificateException;
9+
import java.util.Locale;
910

1011
import javax.net.ssl.HostnameVerifier;
1112
import javax.net.ssl.SSLContext;
@@ -30,7 +31,7 @@ public static String getMD5(String input) {
3031
StringBuilder sb = new StringBuilder();
3132

3233
for (byte b : digest) {
33-
sb.append(String.format("%02x", b & 0xff));
34+
sb.append(String.format(Locale.ROOT, "%02x", b & 0xff));
3435
}
3536

3637
result = sb.toString();

0 commit comments

Comments
 (0)