fix URL scheme colon parsing

This commit is contained in:
Jörg Prante 2023-08-08 13:45:22 +02:00
parent 0ac2d049c8
commit 78a67925e8
3 changed files with 17 additions and 3 deletions

View file

@ -1,5 +1,5 @@
group = org.xbib group = org.xbib
name = net name = net
version = 3.3.1 version = 3.3.2
org.gradle.warning.mode = ALL org.gradle.warning.mode = ALL

View file

@ -72,14 +72,19 @@ public class URLParser {
} }
String parseScheme(URLBuilder builder, String input) { String parseScheme(URLBuilder builder, String input) {
Pair<String, String> p = URL.indexOf(URL.COLON_CHAR, input); // there may be colons in query params, so
// check if input contains query (question mark) and save query for later return
int pos = input.indexOf(URL.QUESTION_CHAR);
String string = pos > 0 ? input.substring(0, pos) : input;
String query = pos > 0 ? input.substring(pos) : "";
Pair<String, String> p = URL.indexOf(URL.COLON_CHAR, string);
if (p.getValue() == null) { if (p.getValue() == null) {
return input; return input;
} }
if (!URL.isNullOrEmpty(p.getKey())) { if (!URL.isNullOrEmpty(p.getKey())) {
builder.scheme(p.getKey()); builder.scheme(p.getKey());
} }
return p.getValue(); return p.getValue() + query;
} }
String parseUserInfo(URLBuilder builder, String input) String parseUserInfo(URLBuilder builder, String input)

View file

@ -486,6 +486,15 @@ class URLParserTest {
}); });
} }
@Test
void testUrlWithUnencodedUrlAsParam() {
URL url = URL.from("/path?a=http://example.com");
Parameter queryParameters = url.getQueryParams();
// %EF%BF%B = 0xFFFD UNICODE REPLACEMENT CHARACTER
assertEquals("/path", url.getPath());
assertEquals("[a=http://example.com]", queryParameters.toString());
}
private void assertUrlCompatibility(String url) throws Exception { private void assertUrlCompatibility(String url) throws Exception {
String s = URL.from(url).toExternalForm(); String s = URL.from(url).toExternalForm();
assertEquals(s, URL.from(s).toExternalForm()); assertEquals(s, URL.from(s).toExternalForm());