Skip to content

Commit 82923d4

Browse files
committed
api: Allow Uri's raw port to be the empty string.
https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3 says port = *DIGIT and #section-6.2.3 explicitly lists http://example.com:/ as a valid URI. Behavior now matches java.net.URI.
1 parent 015bde2 commit 82923d4

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

api/src/main/java/io/grpc/Uri.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,9 @@ public String getRawHost() {
446446
return host;
447447
}
448448

449-
/** Returns the "port" component of this URI, or -1 if not present. */
449+
/** Returns the "port" component of this URI, or -1 if empty or not present. */
450450
public int getPort() {
451-
return port != null ? Integer.parseInt(port) : -1;
451+
return port != null && !port.isEmpty() ? Integer.parseInt(port) : -1;
452452
}
453453

454454
/** Returns the raw port component of this URI in its originally parsed form. */
@@ -943,10 +943,12 @@ public Builder setPort(int port) {
943943

944944
@CanIgnoreReturnValue
945945
Builder setRawPort(String port) {
946-
try {
947-
Integer.parseInt(port); // Result unused.
948-
} catch (NumberFormatException e) {
949-
throw new IllegalArgumentException("Invalid port", e);
946+
if (port != null && !port.isEmpty()) {
947+
try {
948+
Integer.parseInt(port); // Result unused.
949+
} catch (NumberFormatException e) {
950+
throw new IllegalArgumentException("Invalid port", e);
951+
}
950952
}
951953
this.port = port;
952954
return this;

api/src/test/java/io/grpc/UriTest.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,18 @@ public void parse_emptyUserInfo() {
185185
assertThat(uri.toString()).isEqualTo("scheme://@host");
186186
}
187187

188+
@Test
189+
public void parse_emptyPort() {
190+
Uri uri = Uri.create("scheme://host:");
191+
assertThat(uri.getScheme()).isEqualTo("scheme");
192+
assertThat(uri.getAuthority()).isEqualTo("host:");
193+
assertThat(uri.getRawAuthority()).isEqualTo("host:");
194+
assertThat(uri.getHost()).isEqualTo("host");
195+
assertThat(uri.getPort()).isEqualTo(-1);
196+
assertThat(uri.getRawPort()).isEqualTo("");
197+
assertThat(uri.toString()).isEqualTo("scheme://host:");
198+
}
199+
188200
@Test
189201
public void parse_invalidScheme_throws() {
190202
URISyntaxException e =
@@ -259,13 +271,6 @@ public void parse_invalidBackslashScope_throws() {
259271
assertThat(e).hasMessageThat().contains("Invalid character in scope");
260272
}
261273

262-
@Test
263-
public void parse_emptyPort_throws() {
264-
URISyntaxException e =
265-
assertThrows(URISyntaxException.class, () -> Uri.parse("scheme://user@host:/path"));
266-
assertThat(e).hasMessageThat().contains("Invalid port");
267-
}
268-
269274
@Test
270275
public void parse_invalidCharactersInPort_throws() {
271276
URISyntaxException e =

0 commit comments

Comments
 (0)