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
(cherry picked from commit 49e7226eb85399e3bdd48a66dd898faaf1138b43)
Change-Id: Ic5e34b0b9b8c43931cb760e4f39c0af590e847c5
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 @@
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 @@
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 @@
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 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);
}