Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class HttpRequestHandler {

Expand Down Expand Up @@ -317,12 +318,9 @@ public static Object parseJSON(String input) throws JSONException {
} else if (input.trim().length() <= 0) {
return "";
} else if (input.trim().matches("^\".*\"$")) {
// a string enclosed in " " is a json value, return the string without the quotes
return input.trim().substring(1, input.trim().length() - 1);
} else if (input.trim().matches("^-?\\d+$")) {
return Integer.parseInt(input.trim());
} else if (input.trim().matches("^-?\\d+(\\.\\d+)?$")) {
return Double.parseDouble(input.trim());
return parseString(input.trim());
} else if (input.trim().matches("^-?\\d+(\\.\\d+)?([eE][+-]?\\d+)?$")) {
return parseNumber(input.trim());
} else {
try {
return new JSObject(input);
Expand All @@ -335,6 +333,44 @@ public static Object parseJSON(String input) throws JSONException {
}
}

/**
* Parses a JSON string value, resolving escape sequences such as \" or \n
* @param value A string enclosed in " "
* @return The unescaped string, or the content between the quotes when the value is not a single valid JSON string
*/
private static String parseString(String value) {
try {
JSONTokener tokener = new JSONTokener(value);
Object parsed = tokener.nextValue();
if (parsed instanceof String && !tokener.more()) {
return (String) parsed;
}
} catch (JSONException ignored) {}
return value.substring(1, value.length() - 1);
}

/**
* Parses a JSON number into the Java type that holds it: Integer or Long for integers in their range,
* otherwise Double, like JavaScript does
* @param value A number, optionally with a fraction and an exponent
* @return An Integer, a Long, a Double, or the input when it exceeds the Double range, as JSON cannot represent infinity
*/
private static Object parseNumber(String value) {
if (value.matches("^-?\\d+$")) {
try {
long number = Long.parseLong(value);
if (number >= Integer.MIN_VALUE && number <= Integer.MAX_VALUE) {
return (int) number;
}
return number;
} catch (NumberFormatException ignored) {
// beyond the Long range, parsed as a Double below
}
}
double number = Double.parseDouble(value);
return Double.isInfinite(number) ? value : number;
}

/**
* Returns a string based on a base64 InputStream
* @param in The base64 InputStream to convert to a String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

import com.getcapacitor.JSObject;
import com.getcapacitor.plugin.util.HttpRequestHandler.HttpURLConnectionBuilder;
import com.getcapacitor.plugin.util.HttpRequestHandler.ResponseType;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.junit.Test;

public class HttpRequestHandlerTest {
Expand Down Expand Up @@ -35,4 +39,76 @@ public void testHttpURLConnectionBuilderSetUrlParamsNotEncoded() throws Exceptio
.url.toString();
assertEquals(expectedUrl, actualUrl);
}

@Test
public void testParseJSONKeepsIntegerWithinIntRange() throws Exception {
assertEquals(42, HttpRequestHandler.parseJSON("42"));
assertEquals(Integer.MAX_VALUE, HttpRequestHandler.parseJSON("2147483647"));
assertEquals(Integer.MIN_VALUE, HttpRequestHandler.parseJSON("-2147483648"));
}

@Test
public void testParseJSONReturnsLongBeyondIntRange() throws Exception {
assertEquals(8000010570L, HttpRequestHandler.parseJSON("8000010570"));
assertEquals(-8000010570L, HttpRequestHandler.parseJSON("-8000010570"));
assertEquals(Long.MAX_VALUE, HttpRequestHandler.parseJSON(" 9223372036854775807\n"));
}

@Test
public void testParseJSONReturnsDoubleBeyondLongRange() throws Exception {
assertEquals(1.2345678901234567e19, HttpRequestHandler.parseJSON("12345678901234567890"));
}

@Test
public void testParseJSONParsesExponent() throws Exception {
assertEquals(100000.0, HttpRequestHandler.parseJSON("1e5"));
assertEquals(1500.0, HttpRequestHandler.parseJSON("1.5E+3"));
assertEquals(-0.0025, HttpRequestHandler.parseJSON("-2.5e-3"));
}

@Test
public void testParseJSONReturnsInputBeyondDoubleRange() throws Exception {
assertEquals("1e400", HttpRequestHandler.parseJSON("1e400"));
}

@Test
public void testParseJSONKeepsExistingNumberHandling() throws Exception {
assertEquals(1.5, HttpRequestHandler.parseJSON("1.5"));
assertEquals(123, HttpRequestHandler.parseJSON("0123"));
}

@Test
public void testParseJSONUnescapesString() throws Exception {
assertEquals("plain", HttpRequestHandler.parseJSON("\"plain\""));
assertEquals("a\"b", HttpRequestHandler.parseJSON("\"a\\\"b\""));
assertEquals("line\nbreak", HttpRequestHandler.parseJSON("\"line\\nbreak\""));
assertEquals("A/B", HttpRequestHandler.parseJSON("\"\\u0041\\/B\""));
}

@Test
public void testParseJSONKeepsContentBetweenQuotesWhenNotSingleString() throws Exception {
assertEquals("a\" \"b", HttpRequestHandler.parseJSON("\"a\" \"b\""));
}

@Test
public void testReadDataReturnsJsonNumberBeyondIntRange() throws Exception {
ICapacitorHttpUrlConnection connection = new ICapacitorHttpUrlConnection() {
@Override
public InputStream getErrorStream() {
return null;
}

@Override
public String getHeaderField(String name) {
return "Content-Type".equals(name) ? "application/json" : null;
}

@Override
public InputStream getInputStream() {
return new ByteArrayInputStream("8000010570".getBytes(StandardCharsets.UTF_8));
}
};

assertEquals(8000010570L, HttpRequestHandler.readData(connection, ResponseType.DEFAULT));
}
}