Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Never throw converting an HttpUrl to a java.net.URI. #2146

Merged
merged 1 commit into from
Dec 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 41 additions & 19 deletions okhttp-tests/src/test/java/okhttp3/HttpUrlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,12 +483,7 @@ public final class HttpUrlTest {
assertEquals("http://host/#\u0080", url.toString());
assertEquals("\u0080", url.fragment());
assertEquals("\u0080", url.encodedFragment());
try {
url.uri();
fail();
} catch (IllegalStateException expected) {
// Possibly a bug in java.net.URI. Many non-ASCII code points work, this one doesn't!
}
assertEquals(new URI("http://host/#"), url.uri()); // Control characters may be stripped!
}

@Test public void fragmentPercentEncodedNonAscii() throws Exception {
Expand Down Expand Up @@ -1058,19 +1053,46 @@ public final class HttpUrlTest {
assertEquals("http://host/#=[]:;%22~%7C?%23@%5E/$%25*", url.uri().toString());
}

@Test public void toUriWithMalformedPercentEscape() throws Exception {
HttpUrl url = new HttpUrl.Builder()
.scheme("http")
.host("host")
.encodedPath("/%xx")
.build();
assertEquals("http://host/%xx", url.toString());
try {
url.uri();
fail();
} catch (IllegalStateException expected) {
assertEquals("not valid as a java.net.URI: http://host/%xx", expected.getMessage());
}
@Test public void toUriWithControlCharacters() throws Exception {
// Percent-encoded in the path.
assertEquals(new URI("http://host/a%00b"), HttpUrl.parse("http://host/a\u0000b").uri());
assertEquals(new URI("http://host/a%C2%80b"), HttpUrl.parse("http://host/a\u0080b").uri());
assertEquals(new URI("http://host/a%C2%9Fb"), HttpUrl.parse("http://host/a\u009fb").uri());
// Percent-encoded in the query.
assertEquals(new URI("http://host/?a%00b"), HttpUrl.parse("http://host/?a\u0000b").uri());
assertEquals(new URI("http://host/?a%C2%80b"), HttpUrl.parse("http://host/?a\u0080b").uri());
assertEquals(new URI("http://host/?a%C2%9Fb"), HttpUrl.parse("http://host/?a\u009fb").uri());
// Stripped from the fragment.
assertEquals(new URI("http://host/#a%00b"), HttpUrl.parse("http://host/#a\u0000b").uri());
assertEquals(new URI("http://host/#ab"), HttpUrl.parse("http://host/#a\u0080b").uri());
assertEquals(new URI("http://host/#ab"), HttpUrl.parse("http://host/#a\u009fb").uri());
}

@Test public void toUriWithSpaceCharacters() throws Exception {
// Percent-encoded in the path.
assertEquals(new URI("http://host/a%0Bb"), HttpUrl.parse("http://host/a\u000bb").uri());
assertEquals(new URI("http://host/a%20b"), HttpUrl.parse("http://host/a b").uri());
assertEquals(new URI("http://host/a%E2%80%89b"), HttpUrl.parse("http://host/a\u2009b").uri());
assertEquals(new URI("http://host/a%E3%80%80b"), HttpUrl.parse("http://host/a\u3000b").uri());
// Percent-encoded in the query.
assertEquals(new URI("http://host/?a%0Bb"), HttpUrl.parse("http://host/?a\u000bb").uri());
assertEquals(new URI("http://host/?a%20b"), HttpUrl.parse("http://host/?a b").uri());
assertEquals(new URI("http://host/?a%E2%80%89b"), HttpUrl.parse("http://host/?a\u2009b").uri());
assertEquals(new URI("http://host/?a%E3%80%80b"), HttpUrl.parse("http://host/?a\u3000b").uri());
// Stripped from the fragment.
assertEquals(new URI("http://host/#a%0Bb"), HttpUrl.parse("http://host/#a\u000bb").uri());
assertEquals(new URI("http://host/#a%20b"), HttpUrl.parse("http://host/#a b").uri());
assertEquals(new URI("http://host/#ab"), HttpUrl.parse("http://host/#a\u2009b").uri());
assertEquals(new URI("http://host/#ab"), HttpUrl.parse("http://host/#a\u3000b").uri());
}

@Test public void toUriWithNonHexPercentEscape() throws Exception {
assertEquals(new URI("http://host/%25xx"), HttpUrl.parse("http://host/%xx").uri());
}

@Test public void toUriWithTruncatedPercentEscape() throws Exception {
assertEquals(new URI("http://host/%25a"), HttpUrl.parse("http://host/%a").uri());
assertEquals(new URI("http://host/%25"), HttpUrl.parse("http://host/%").uri());
}

@Test public void fromJavaNetUrl() throws Exception {
Expand Down
8 changes: 4 additions & 4 deletions okhttp/src/main/java/okhttp3/FormBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ public static final class Builder {
private final List<String> values = new ArrayList<>();

public Builder add(String name, String value) {
names.add(HttpUrl.canonicalize(name, FORM_ENCODE_SET, false, true, true));
values.add(HttpUrl.canonicalize(value, FORM_ENCODE_SET, false, true, true));
names.add(HttpUrl.canonicalize(name, FORM_ENCODE_SET, false, false, true, true));
values.add(HttpUrl.canonicalize(value, FORM_ENCODE_SET, false, false, true, true));
return this;
}

public Builder addEncoded(String name, String value) {
names.add(HttpUrl.canonicalize(name, FORM_ENCODE_SET, true, true, true));
values.add(HttpUrl.canonicalize(value, FORM_ENCODE_SET, true, true, true));
names.add(HttpUrl.canonicalize(name, FORM_ENCODE_SET, true, false, true, true));
values.add(HttpUrl.canonicalize(value, FORM_ENCODE_SET, true, false, true, true));
return this;
}

Expand Down
Loading