From 99f82b1aaee15c2f51287ab620eaab7105a2f07d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Wa=C5=9Bko?= Date: Mon, 18 Dec 2023 12:45:55 +0100 Subject: [PATCH] encode args without Apache --- .../java/org/enso/base/net/URIHelpers.java | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/std-bits/base/src/main/java/org/enso/base/net/URIHelpers.java b/std-bits/base/src/main/java/org/enso/base/net/URIHelpers.java index 70ebbbf58e96a..f35180629e0b2 100644 --- a/std-bits/base/src/main/java/org/enso/base/net/URIHelpers.java +++ b/std-bits/base/src/main/java/org/enso/base/net/URIHelpers.java @@ -2,18 +2,45 @@ import java.net.URI; import java.net.URISyntaxException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.List; -import org.apache.http.client.utils.URIBuilder; public class URIHelpers { public record NameValuePair(String name, String value) {} public static URI addQueryParameters(URI uri, List params) throws URISyntaxException { - URIBuilder builder = new URIBuilder(uri); + StringBuilder query = new StringBuilder(); + if (uri.getRawQuery() != null) { + query.append(uri.getRawQuery()); + } + for (NameValuePair param : params) { - builder.addParameter(param.name(), param.value()); + if (!query.isEmpty()) { + query.append("&"); + } + + query.append(encode(param.name)).append("=").append(encode(param.value)); + } + + StringBuilder uriBuilder = new StringBuilder(); + uriBuilder + .append(uri.getScheme()).append("://") + .append(uri.getRawAuthority()) + .append(uri.getRawPath()); + if (!query.isEmpty()) { + uriBuilder.append("?").append(query); } - return builder.build(); + + if (uri.getRawFragment() != null) { + uriBuilder.append("#").append(uri.getRawFragment()); + } + + return new URI(uriBuilder.toString()); + } + + private static String encode(String value) { + return URLEncoder.encode(value, StandardCharsets.UTF_8); } }