Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f13488e

Browse files
Reject Uri with empty scheme or host, close #1445
Motivation: Following #1442, we shouldn't accept Uri with empty scheme or empty host, such as "http://". Modifications: * Introduce MiscUtils#isEmpty(String) * Throw IllegalArgumentException on empty scheme or host, with message mentioning missing field and original url Result: Better control on Uri that could cause AHC to choke
1 parent 328ed7f commit f13488e

File tree

4 files changed

+26
-33
lines changed

4 files changed

+26
-33
lines changed

‎client/src/main/java/org/asynchttpclient/uri/Uri.java‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
*/
1313
package org.asynchttpclient.uri;
1414

15-
import static org.asynchttpclient.util.Assertions.assertNotNull;
16-
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
15+
import static org.asynchttpclient.util.Assertions.assertNotEmpty;
16+
import static org.asynchttpclient.util.MiscUtils.*;
1717

1818
import java.net.URI;
1919
import java.net.URISyntaxException;
@@ -36,9 +36,11 @@ public static Uri create(Uri context, final String originalUrl) {
3636
UriParser parser = new UriParser();
3737
parser.parse(context, originalUrl);
3838

39-
if (parser.scheme == null || parser.host == null) {
40-
throw new IllegalArgumentException(String.format("The UriParser could not extract all required values: scheme=%s, host=%s. Please make sure you provide a valid URL.",
41-
parser.scheme, parser.host));
39+
if (isEmpty(parser.scheme)) {
40+
throw new IllegalArgumentException(originalUrl + " could not be parsed into a proper Uri, missing scheme");
41+
}
42+
if (isEmpty(parser.host)) {
43+
throw new IllegalArgumentException(originalUrl + " could not be parsed into a proper Uri, missing host");
4244
}
4345

4446
return new Uri(parser.scheme,//
@@ -66,9 +68,9 @@ public Uri(String scheme,//
6668
String path,//
6769
String query) {
6870

69-
this.scheme = assertNotNull(scheme, "scheme");
71+
this.scheme = assertNotEmpty(scheme, "scheme");
7072
this.userInfo = userInfo;
71-
this.host = assertNotNull(host, "host");
73+
this.host = assertNotEmpty(host, "host");
7274
this.port = port;
7375
this.path = path;
7476
this.query = query;

‎client/src/main/java/org/asynchttpclient/util/Assertions.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static <T> T assertNotNull(T value, String name) {
2626
}
2727

2828
public static String assertNotEmpty(String value, String name) {
29+
assertNotNull(value, name);
2930
if (value.length() == 0)
3031
throw new IllegalArgumentException("empty " + name);
3132
return value;

‎client/src/main/java/org/asynchttpclient/util/MiscUtils.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ private MiscUtils() {
2323
}
2424

2525
public static boolean isNonEmpty(String string) {
26-
return string != null && !string.isEmpty();
26+
return !isEmpty(string);
27+
}
28+
29+
public static boolean isEmpty(String string) {
30+
return string == null || string.isEmpty();
2731
}
2832

2933
public static boolean isNonEmpty(Object[] array) {

‎client/src/test/java/org/asynchttpclient/uri/UriTest.java‎

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
*/
1313
package org.asynchttpclient.uri;
1414

15-
import org.testng.annotations.Test;
16-
1715
import static org.testng.Assert.*;
1816

17+
import org.testng.annotations.Test;
18+
1919
public class UriTest {
2020

2121
@Test
@@ -360,31 +360,17 @@ public void testIsWebsocket() {
360360
}
361361

362362
@Test
363-
public void testCreateWithInvalidUrl_throwsIllegalArgumentException() {
364-
// a valid URL would contain the scheme/protocol
365-
String invalidUrl = "localhost";
366-
367-
Throwable exception = null;
368-
try {
369-
// run
370-
Uri.create(invalidUrl);
371-
} catch (IllegalArgumentException ex) {
372-
exception = ex;
373-
}
363+
public void creatingUriWithDefinedSchemeAndHostWorks() {
364+
Uri.create("http://localhost");
365+
}
374366

375-
// verify
376-
assertNotNull(exception);
377-
assertEquals("The UriParser could not extract all required values: scheme=null, host=null. Please make "
378-
+ "sure you provide a valid URL.", exception.getMessage());
367+
@Test(expectedExceptions = IllegalArgumentException.class)
368+
public void creatingUriWithMissingSchemeThrowsIllegalArgumentException() {
369+
Uri.create("localhost");
379370
}
380371

381-
@Test
382-
public void testCreateWithValidUrl_doesNotThrowException() {
383-
String validUrl = "https://localhost";
384-
try {
385-
Uri.create(validUrl);
386-
} catch (IllegalArgumentException ex) {
387-
fail(ex.getMessage());
388-
}
372+
@Test(expectedExceptions = IllegalArgumentException.class)
373+
public void creatingUriWithMissingHostThrowsIllegalArgumentException() {
374+
Uri.create("http://");
389375
}
390376
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /