Skip to content

Commit d5d1f06

Browse files
authored
remove unprintable chars (#747)
1 parent 273fca9 commit d5d1f06

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

services-core/src/main/java/com/mapbox/core/utils/ApiCallHelper.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mapbox.core.utils;
22

3+
import android.support.annotation.NonNull;
34
import android.support.annotation.Nullable;
45
import com.mapbox.core.constants.Constants;
56

@@ -12,6 +13,8 @@
1213
*/
1314
public final class ApiCallHelper {
1415

16+
private static final String ONLY_PRINTABLE_CHARS = "[^\\p{ASCII}]";
17+
1518
private ApiCallHelper() {
1619
// Private constructor preventing instances of class
1720
}
@@ -32,10 +35,31 @@ public static String getHeaderUserAgent(@Nullable String clientAppName) {
3235
if (TextUtils.isEmpty(osName) || TextUtils.isEmpty(osVersion) || TextUtils.isEmpty(osArch)) {
3336
return Constants.HEADER_USER_AGENT;
3437
} else {
38+
return getHeaderUserAgent(clientAppName, osName, osVersion, osArch);
39+
}
40+
}
41+
42+
/**
43+
* Computes a full user agent header of the form:
44+
* {@code MapboxJava/1.2.0 Mac OS X/10.11.5 (x86_64)}.
45+
*
46+
* @param clientAppName Application Name
47+
* @param osName OS name
48+
* @param osVersion OS version
49+
* @param osArch OS Achitecture
50+
* @return {@link String} representing the header user agent
51+
* @since 1.0.0
52+
*/
53+
public static String getHeaderUserAgent(@Nullable String clientAppName,
54+
@NonNull String osName, @NonNull String osVersion, @NonNull String osArch) {
55+
56+
osName = osName.replaceAll(ONLY_PRINTABLE_CHARS, "");
57+
osVersion = osVersion.replaceAll(ONLY_PRINTABLE_CHARS, "");
58+
osArch = osArch.replaceAll(ONLY_PRINTABLE_CHARS, "");
3559
String baseUa = String.format(
3660
Locale.US, "%s %s/%s (%s)", Constants.HEADER_USER_AGENT, osName, osVersion, osArch);
61+
3762
return TextUtils.isEmpty(clientAppName) ? baseUa : String.format(Locale.US, "%s %s",
3863
clientAppName, baseUa);
39-
}
4064
}
4165
}

services-core/src/test/java/com/mapbox/core/utils/ApiCallHelperTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package com.mapbox.core.utils;
22

3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertSame;
35
import static org.junit.Assert.assertTrue;
46

57
import com.mapbox.core.constants.Constants;
8+
9+
import org.junit.Assert;
610
import org.junit.Test;
711

12+
import java.util.Locale;
13+
814
public class ApiCallHelperTest {
915

1016
@Test
@@ -14,4 +20,22 @@ public void getHeaderUserAgent_formatsStringCorrectly() throws Exception {
1420
assertTrue(ApiCallHelper.getHeaderUserAgent(
1521
"AppName").startsWith("AppName"));
1622
}
23+
24+
@Test
25+
public void getHeaderUserAgent_nonAsciiCharsRemoved() {
26+
27+
String osName = "os.name";
28+
String osVersion = "os.version";
29+
String osArch = "os.arch";
30+
31+
String userAgent = String.format(
32+
Locale.US, "%s %s/%s (%s)", Constants.HEADER_USER_AGENT, osName, osVersion, osArch);
33+
34+
String userAgentWithExtraChars = ApiCallHelper.getHeaderUserAgent(null,
35+
osName + '\u00A9', // copyright
36+
osVersion + '\u00AE', // Registered Sign
37+
osArch + '\u2122'); // TM
38+
39+
Assert.assertEquals(userAgent, userAgentWithExtraChars);
40+
}
1741
}

0 commit comments

Comments
 (0)