Skip to content

Commit

Permalink
fix(android): incorrect http url params encoding (#6586)
Browse files Browse the repository at this point in the history
Co-authored-by: jcesarmobile <[email protected]>
  • Loading branch information
jkeys089 and jcesarmobile committed Feb 29, 2024
1 parent 00b7abf commit e5e692c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -141,7 +143,7 @@ public HttpURLConnectionBuilder setUrlParams(JSObject params, boolean shouldEnco
StringBuilder value = new StringBuilder();
JSONArray arr = params.getJSONArray(key);
for (int x = 0; x < arr.length(); x++) {
value.append(key).append("=").append(arr.getString(x));
this.addUrlParam(value, key, arr.getString(x), shouldEncode);
if (x != arr.length() - 1) {
value.append("&");
}
Expand All @@ -154,30 +156,37 @@ public HttpURLConnectionBuilder setUrlParams(JSObject params, boolean shouldEnco
if (urlQueryBuilder.length() > 0) {
urlQueryBuilder.append("&");
}
urlQueryBuilder.append(key).append("=").append(params.getString(key));
this.addUrlParam(urlQueryBuilder, key, params.getString(key), shouldEncode);
}
}

String urlQuery = urlQueryBuilder.toString();

URI uri = url.toURI();
if (shouldEncode) {
URI encodedUri = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), urlQuery, uri.getFragment());
this.url = encodedUri.toURL();
} else {
String unEncodedUrlString =
uri.getScheme() +
"://" +
uri.getAuthority() +
uri.getPath() +
((!urlQuery.equals("")) ? "?" + urlQuery : "") +
((uri.getFragment() != null) ? uri.getFragment() : "");
this.url = new URL(unEncodedUrlString);
}
String unEncodedUrlString =
uri.getScheme() +
"://" +
uri.getAuthority() +
uri.getPath() +
((!urlQuery.equals("")) ? "?" + urlQuery : "") +
((uri.getFragment() != null) ? uri.getFragment() : "");
this.url = new URL(unEncodedUrlString);

return this;
}

private static void addUrlParam(StringBuilder sb, String key, String value, boolean shouldEncode) {
if (shouldEncode) {
try {
key = URLEncoder.encode(key, "UTF-8");
value = URLEncoder.encode(value, "UTF-8");
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex.getCause());
}
}
sb.append(key).append("=").append(value);
}

public CapacitorHttpUrlConnection build() {
return connection;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.getcapacitor.plugin.util;

import static org.junit.Assert.*;

import com.getcapacitor.JSObject;
import com.getcapacitor.plugin.util.HttpRequestHandler.HttpURLConnectionBuilder;
import java.net.URL;
import org.junit.Test;

public class HttpRequestHandlerTest {

static final String BASE_URL = "https://httpbin.org/get";
static final String PARAMS_JSON = """
{"k": "a&b"}
""";

@Test
public void testHttpURLConnectionBuilderSetUrlParamsEncoded() throws Exception {
String expectedQuery = "k=a%26b";
String expectedUrl = BASE_URL + "?" + expectedQuery;
String actualUrl = new HttpURLConnectionBuilder()
.setUrl(new URL(BASE_URL))
.setUrlParams(new JSObject(PARAMS_JSON), true)
.url.toString();
assertEquals(expectedUrl, actualUrl);
}

@Test
public void testHttpURLConnectionBuilderSetUrlParamsNotEncoded() throws Exception {
String expectedQuery = "k=a&b";
String expectedUrl = BASE_URL + "?" + expectedQuery;
String actualUrl = new HttpURLConnectionBuilder()
.setUrl(new URL(BASE_URL))
.setUrlParams(new JSObject(PARAMS_JSON), false)
.url.toString();
assertEquals(expectedUrl, actualUrl);
}
}

0 comments on commit e5e692c

Please sign in to comment.