aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsaac Green <isaac@wink.com>2015-09-28 02:54:18 -0400
committerNeil Fuller <nfuller@google.com>2016-03-16 13:59:45 +0000
commit49e7226eb85399e3bdd48a66dd898faaf1138b43 (patch)
tree6189889ac3c10894950850a7b25b7fc30d9af180
parent2d6bab1521a4f9aa52008464a2c4f2a4d8b6cab9 (diff)
downloadokhttp-49e7226eb85399e3bdd48a66dd898faaf1138b43.tar.gz
Apply upstream OkHttp HttpUrl fix
Upstream information: Comment: use URI constructor for encoding fixes - https://github.com/square/okhttp/issues/1872 SHA: 2a13fe9d83a596ddbfe8299dc75ab33db9130d20 Bug: 27590872 Change-Id: Ica97a6045a68886d32bfe29bb83cc62f10f98f1f
-rw-r--r--okhttp-tests/src/test/java/com/squareup/okhttp/HttpUrlTest.java21
-rw-r--r--okhttp-tests/src/test/java/com/squareup/okhttp/URLConnectionTest.java2
-rw-r--r--okhttp/src/main/java/com/squareup/okhttp/HttpUrl.java9
3 files changed, 19 insertions, 13 deletions
diff --git a/okhttp-tests/src/test/java/com/squareup/okhttp/HttpUrlTest.java b/okhttp-tests/src/test/java/com/squareup/okhttp/HttpUrlTest.java
index 4ec595d..9651bfb 100644
--- a/okhttp-tests/src/test/java/com/squareup/okhttp/HttpUrlTest.java
+++ b/okhttp-tests/src/test/java/com/squareup/okhttp/HttpUrlTest.java
@@ -928,16 +928,19 @@ public final class HttpUrlTest {
assertEquals("http://host/?d=abc!@[]%5E%60%7B%7D%7C%5C", uri.toString());
}
- @Test public void toUriForbiddenCharacter() throws Exception {
- HttpUrl httpUrl = HttpUrl.parse("http://host/a[b");
- try {
- httpUrl.uri();
- fail();
- } catch (IllegalStateException expected) {
- assertEquals("not valid as a java.net.URI: http://host/a[b", expected.getMessage());
- }
+ @Test public void toUriSpecialPathCharacters() throws Exception {
+ HttpUrl url = new HttpUrl.Builder()
+ .scheme("http")
+ .host("example.com")
+ .addPathSegment("data=[out:json];node[\"name\"~\"Karlsruhe\"]" +
+ "[\"place\"~\"city|village|town\"];out body;")
+ .build();
+ URI uri = url.uri();
+ assertEquals("http://example.com/data=%5Bout:json%5D;node%5B%22name%22~%22Karlsruhe%22%5D" +
+ "%5B%22place%22~%22city%7Cvillage%7Ctown%22%5D;out%20body;",
+ uri.toString());
}
-
+
@Test public void fromJavaNetUrl() throws Exception {
URL javaNetUrl = new URL("http://username:password@host/path?query#fragment");
HttpUrl httpUrl = HttpUrl.get(javaNetUrl);
diff --git a/okhttp-tests/src/test/java/com/squareup/okhttp/URLConnectionTest.java b/okhttp-tests/src/test/java/com/squareup/okhttp/URLConnectionTest.java
index 79a6cf2..4f0ca58 100644
--- a/okhttp-tests/src/test/java/com/squareup/okhttp/URLConnectionTest.java
+++ b/okhttp-tests/src/test/java/com/squareup/okhttp/URLConnectionTest.java
@@ -395,7 +395,7 @@ public final class URLConnectionTest {
try {
connection.connect();
fail();
- } catch (UnknownHostException expected) {
+ } catch (IllegalStateException expected) {
}
}
diff --git a/okhttp/src/main/java/com/squareup/okhttp/HttpUrl.java b/okhttp/src/main/java/com/squareup/okhttp/HttpUrl.java
index 0919b91..d56f525 100644
--- a/okhttp/src/main/java/com/squareup/okhttp/HttpUrl.java
+++ b/okhttp/src/main/java/com/squareup/okhttp/HttpUrl.java
@@ -260,7 +260,6 @@ public final class HttpUrl {
static final String PATH_SEGMENT_ENCODE_SET = " \"<>^`{}|/\\?#";
static final String QUERY_ENCODE_SET = " \"'<>#";
static final String QUERY_COMPONENT_ENCODE_SET = " \"'<>#&=";
- static final String CONVERT_TO_URI_ENCODE_SET = "^`{}|\\";
static final String FORM_ENCODE_SET = " \"':;<=>@[]^`{}|/\\?#&!$(),~";
static final String FRAGMENT_ENCODE_SET = "";
@@ -332,8 +331,12 @@ public final class HttpUrl {
*/
public URI uri() {
try {
- String uriSafeUrl = canonicalize(url, CONVERT_TO_URI_ENCODE_SET, true, false);
- return new URI(uriSafeUrl);
+ String uriUserInfo = username + ":" + password;
+ if (uriUserInfo.equals(":")) uriUserInfo = null;
+ final int uriPort = port == defaultPort(scheme) ? -1 : port; // Don't include default port
+ StringBuilder path = new StringBuilder();
+ pathSegmentsToString(path, pathSegments);
+ return new URI(scheme, uriUserInfo, host, uriPort, path.toString(), query(), fragment);
} catch (URISyntaxException e) {
throw new IllegalStateException("not valid as a java.net.URI: " + url);
}