1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-17 08:42:25 +03:00

use URI constructor for encoding

fixes - https://github.com/square/okhttp/issues/1872
This commit is contained in:
Isaac Green
2015-09-28 02:54:18 -04:00
parent 18c5cad5d2
commit 2a13fe9d83
3 changed files with 19 additions and 13 deletions

View File

@@ -930,16 +930,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);

View File

@@ -395,7 +395,7 @@ public final class URLConnectionTest {
try {
connection.connect();
fail();
} catch (UnknownHostException expected) {
} catch (IllegalStateException expected) {
}
}

View File

@@ -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);
}