From 183041acd3df8694b3d3bed7cacdb442aa471200 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Thu, 7 Dec 2023 17:26:48 +0100 Subject: [PATCH] fix percent decoding when parameter is query domain --- gradle.properties | 2 +- .../java/org/xbib/net/path/PathDecoder.java | 17 ++++++++----- .../org/xbib/net/path/PathDecoderTest.java | 14 ++++++----- net/src/main/java/org/xbib/net/Parameter.java | 14 +++++++++-- .../java/org/xbib/net/ParameterBuilder.java | 25 ++++++++++++++++++- .../java/org/xbib/net/ParameterException.java | 4 +++ .../test/java/org/xbib/net/ParameterTest.java | 11 ++++++++ .../java/org/xbib/net/PercentDecoderTest.java | 5 ++++ .../test/java/org/xbib/net/URLParserTest.java | 3 ++- 9 files changed, 78 insertions(+), 17 deletions(-) diff --git a/gradle.properties b/gradle.properties index c84789c..5d2f4ec 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ group = org.xbib name = net -version = 4.0.0 +version = 4.0.1 org.gradle.warning.mode = ALL diff --git a/net-path/src/main/java/org/xbib/net/path/PathDecoder.java b/net-path/src/main/java/org/xbib/net/path/PathDecoder.java index 89dc5ac..c0c09b3 100644 --- a/net-path/src/main/java/org/xbib/net/path/PathDecoder.java +++ b/net-path/src/main/java/org/xbib/net/path/PathDecoder.java @@ -4,34 +4,39 @@ import org.xbib.net.Parameter; import org.xbib.net.ParameterBuilder; import org.xbib.net.PathNormalizer; +import java.nio.charset.Charset; + public class PathDecoder { private final String path; private final String query; + private final Charset charset; + private final ParameterBuilder params; - public PathDecoder(String pathAndQuery) { - this(pathAndQuery, null); + public PathDecoder(String pathAndQuery, Charset charset) { + this(pathAndQuery, null, charset); } - public PathDecoder(String pathAndQuery, String queryString) { + public PathDecoder(String pathAndQuery, String queryString, Charset charset) { + this.charset = charset; int pos = pathAndQuery.indexOf('?'); String path = pos > 0 ? pathAndQuery.substring(0, pos) : pathAndQuery; this.query = pos > 0 ? pathAndQuery.substring(pos + 1) : null; this.path = PathNormalizer.normalize(path); this.params = Parameter.builder().domain(Parameter.Domain.PATH).enablePercentDecoding(); if (query != null) { - this.params.add(query); + this.params.add(query, charset); } if (queryString != null) { - this.params.add(queryString); + this.params.add(queryString, charset); } } public void parse(String queryString) { - this.params.add(queryString); + this.params.add(queryString, charset); } public String path() { diff --git a/net-path/src/test/java/org/xbib/net/path/PathDecoderTest.java b/net-path/src/test/java/org/xbib/net/path/PathDecoderTest.java index 0a5bb54..0620b17 100644 --- a/net-path/src/test/java/org/xbib/net/path/PathDecoderTest.java +++ b/net-path/src/test/java/org/xbib/net/path/PathDecoderTest.java @@ -4,6 +4,8 @@ import org.junit.jupiter.api.Test; import org.xbib.net.Parameter; import org.xbib.net.URL; +import java.nio.charset.StandardCharsets; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; @@ -11,27 +13,27 @@ class PathDecoderTest { @Test void testPlusSign() throws Exception { - PathDecoder decoder = new PathDecoder("/path?a=b+c", "d=e+f"); + PathDecoder decoder = new PathDecoder("/path?a=b+c", "d=e+f", StandardCharsets.UTF_8); assertEquals("[b c]", decoder.getParameter().getAll("a", Parameter.Domain.PATH).toString()); assertEquals("[e f]", decoder.getParameter().getAll("d", Parameter.Domain.PATH).toString()); } @Test void testSlash() throws Exception { - PathDecoder decoder = new PathDecoder("path/foo/bar/?a=b+c", "d=e+f"); + PathDecoder decoder = new PathDecoder("path/foo/bar/?a=b+c", "d=e+f", StandardCharsets.UTF_8); assertEquals("[b c]", decoder.getParameter().getAll("a", Parameter.Domain.PATH).toString()); assertEquals("[e f]", decoder.getParameter().getAll("d", Parameter.Domain.PATH).toString()); } @Test void testDoubleSlashes() { - PathDecoder decoder = new PathDecoder("//path", ""); + PathDecoder decoder = new PathDecoder("//path", "", StandardCharsets.UTF_8); assertEquals("/path", decoder.path()); } @Test void testSlashes() throws Exception { - PathDecoder decoder = new PathDecoder("//path?a=b+c", "d=e+f"); + PathDecoder decoder = new PathDecoder("//path?a=b+c", "d=e+f", StandardCharsets.UTF_8); assertEquals("/path", decoder.path()); assertEquals("[b c]", decoder.getParameter().getAll("a", Parameter.Domain.PATH).toString()); assertEquals("[e f]", decoder.getParameter().getAll("d", Parameter.Domain.PATH).toString()); @@ -39,7 +41,7 @@ class PathDecoderTest { @Test void testPlusPercent() throws Exception { - PathDecoder decoder = new PathDecoder("//path?a=b%2Bc", "d=e%2Bf"); + PathDecoder decoder = new PathDecoder("//path?a=b%2Bc", "d=e%2Bf", StandardCharsets.UTF_8); assertEquals("/path", decoder.path()); assertEquals("[b+c]", decoder.getParameter().getAll("a", Parameter.Domain.PATH).toString()); assertEquals("[e+f]", decoder.getParameter().getAll("d", Parameter.Domain.PATH).toString()); @@ -53,7 +55,7 @@ class PathDecoderTest { assertNull(url.getPort()); assertEquals("/pdfconverter/index.gtpl", url.getPath()); assertNull(url.getFragment()); - PathDecoder decoder = new PathDecoder(requestURI); + PathDecoder decoder = new PathDecoder(requestURI, StandardCharsets.UTF_8); if (url.getQuery() != null) { decoder.parse(url.getDecodedQuery()); } diff --git a/net/src/main/java/org/xbib/net/Parameter.java b/net/src/main/java/org/xbib/net/Parameter.java index ba97385..a53887f 100644 --- a/net/src/main/java/org/xbib/net/Parameter.java +++ b/net/src/main/java/org/xbib/net/Parameter.java @@ -1,5 +1,7 @@ package org.xbib.net; +import java.nio.charset.MalformedInputException; +import java.nio.charset.UnmappableCharacterException; import java.util.Collection; import java.util.Iterator; import java.util.LinkedHashMap; @@ -102,9 +104,17 @@ public class Parameter implements Iterable>, Comparable map = Map.of( diff --git a/net/src/test/java/org/xbib/net/PercentDecoderTest.java b/net/src/test/java/org/xbib/net/PercentDecoderTest.java index a91c2d5..38cfd0c 100644 --- a/net/src/test/java/org/xbib/net/PercentDecoderTest.java +++ b/net/src/test/java/org/xbib/net/PercentDecoderTest.java @@ -56,6 +56,11 @@ class PercentDecoderTest { }); } + @Test + void testJoerg() throws UnmappableCharacterException, MalformedInputException { + assertEquals("Hello Jörg", decoder.decode("Hello%20J%C3%B6rg")); + } + @Test void testRandomStrings() throws MalformedInputException, UnmappableCharacterException { PercentEncoder encoder = PercentEncoders.getQueryEncoder(StandardCharsets.UTF_8); diff --git a/net/src/test/java/org/xbib/net/URLParserTest.java b/net/src/test/java/org/xbib/net/URLParserTest.java index fc4b6b3..bbc9d7a 100644 --- a/net/src/test/java/org/xbib/net/URLParserTest.java +++ b/net/src/test/java/org/xbib/net/URLParserTest.java @@ -3,6 +3,7 @@ package org.xbib.net; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.net.URI; import java.nio.charset.Charset; import java.nio.charset.CodingErrorAction; import java.nio.charset.StandardCharsets; @@ -498,7 +499,7 @@ class URLParserTest { private void assertUrlCompatibility(String url) throws Exception { String s = URL.from(url).toExternalForm(); assertEquals(s, URL.from(s).toExternalForm()); - assertEquals(s, new java.net.URL(url).toExternalForm()); + assertEquals(s, URI.create(url).toURL().toExternalForm()); } private void assertRoundTrip(String url) {