Skip to content

Commit

Permalink
Reapply an upstream OkHttp change to Android for HttpUrl
Browse files Browse the repository at this point in the history
Manually reapplied change from upstream to address HttpUrl encoding issues.
This change did not apply cleanly. Upstream FormEncodingBuilder has
been refactored. Equivalent changes have been made.

Upstream details:
Comment:
Never throw converting an HttpUrl to a java.net.URI.

Just do arbitrary amounts of transformation. Sigh.

Closes square/okhttp#2116
SHA: d77edcc8905148f18a691be180c4f8f366a5ee1b

Bug: 27590872
(cherry picked from commit 4c521731e582bbd36d1ef7276b25a347f91d9bbb)

Change-Id: I15317abbfcd6d7d4af3ce9793f61c912d9b66991
  • Loading branch information
nfuller committed Mar 18, 2016
1 parent cf7f0f3 commit 3281dee
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 69 deletions.
60 changes: 41 additions & 19 deletions okhttp-tests/src/test/java/com/squareup/okhttp/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 @@ -1056,19 +1051,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
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public FormEncodingBuilder add(String name, String value) {
content.writeByte('&');
}
HttpUrl.canonicalize(content, name, 0, name.length(),
HttpUrl.FORM_ENCODE_SET, false, true, true);
HttpUrl.FORM_ENCODE_SET, false, false, true, true);
content.writeByte('=');
HttpUrl.canonicalize(content, value, 0, value.length(),
HttpUrl.FORM_ENCODE_SET, false, true, true);
HttpUrl.FORM_ENCODE_SET, false, false, true, true);
return this;
}

Expand All @@ -46,10 +46,10 @@ public FormEncodingBuilder addEncoded(String name, String value) {
content.writeByte('&');
}
HttpUrl.canonicalize(content, name, 0, name.length(),
HttpUrl.FORM_ENCODE_SET, true, true, true);
HttpUrl.FORM_ENCODE_SET, true, false, true, true);
content.writeByte('=');
HttpUrl.canonicalize(content, value, 0, value.length(),
HttpUrl.FORM_ENCODE_SET, true, true, true);
HttpUrl.FORM_ENCODE_SET, true, false, true, true);
return this;
}

Expand Down
Loading

0 comments on commit 3281dee

Please sign in to comment.