Skip to content

Commit

Permalink
Change MediaType's failure mode to not crash on charset problems.
Browse files Browse the repository at this point in the history
As-is it throws unchecked exceptions on unexpected charsets. This is a problem
because it can cause a misbehaving webserver to crash the client.

I don't expect this to break existing clients; returning 'null' has always
been a possibility; it's just returned in more cases.
  • Loading branch information
squarejesse committed Jan 29, 2017
1 parent f541304 commit 6651a9c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.io.EOFException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.concurrent.TimeUnit;
import okhttp3.Connection;
import okhttp3.Headers;
Expand Down Expand Up @@ -241,15 +240,7 @@ public Level getLevel() {
Charset charset = UTF8;
MediaType contentType = responseBody.contentType();
if (contentType != null) {
try {
charset = contentType.charset(UTF8);
} catch (UnsupportedCharsetException e) {
logger.log("");
logger.log("Couldn't decode the response body; charset is likely malformed.");
logger.log("<-- END HTTP");

return response;
}
charset = contentType.charset(UTF8);
}

if (!isPlaintext(buffer)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ private void bodyGetNoBody(int code) throws IOException {

server.enqueue(new MockResponse()
.setHeader("Content-Type", "text/html; charset=0")
.setBody("Ignore This"));
.setBody("Body with unknown charset"));
Response response = client.newCall(request().build()).execute();
response.body().close();

Expand All @@ -578,8 +578,8 @@ private void bodyGetNoBody(int code) throws IOException {
.assertLogEqual("Content-Type: text/html; charset=0")
.assertLogMatch("Content-Length: \\d+")
.assertLogMatch("")
.assertLogEqual("Couldn't decode the response body; charset is likely malformed.")
.assertLogEqual("<-- END HTTP")
.assertLogEqual("Body with unknown charset")
.assertLogEqual("<-- END HTTP (25-byte body)")
.assertNoMoreLogs();

applicationLogs
Expand All @@ -589,8 +589,8 @@ private void bodyGetNoBody(int code) throws IOException {
.assertLogEqual("Content-Type: text/html; charset=0")
.assertLogMatch("Content-Length: \\d+")
.assertLogEqual("")
.assertLogEqual("Couldn't decode the response body; charset is likely malformed.")
.assertLogEqual("<-- END HTTP")
.assertLogEqual("Body with unknown charset")
.assertLogEqual("<-- END HTTP (25-byte body)")
.assertNoMoreLogs();
}

Expand Down
35 changes: 6 additions & 29 deletions okhttp-tests/src/test/java/okhttp3/MediaTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@
package okhttp3;

import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import okhttp3.internal.Util;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
* Test MediaType API and parsing.
Expand Down Expand Up @@ -123,29 +120,17 @@ public class MediaTypeTest {
}

@Test public void testMultipleCharsets() {
try {
MediaType.parse("text/plain; charset=utf-8; charset=utf-16");
fail();
} catch (IllegalArgumentException expected) {
}
assertNull(MediaType.parse("text/plain; charset=utf-8; charset=utf-16"));
}

@Test public void testIllegalCharsetName() {
MediaType mediaType = MediaType.parse("text/plain; charset=\"!@#$%^&*()\"");
try {
mediaType.charset();
fail();
} catch (IllegalCharsetNameException expected) {
}
assertNull(mediaType.charset());
}

@Test public void testUnsupportedCharset() {
MediaType mediaType = MediaType.parse("text/plain; charset=utf-wtf");
try {
mediaType.charset();
fail();
} catch (UnsupportedCharsetException expected) {
}
assertNull(mediaType.charset());
}

/**
Expand All @@ -159,20 +144,12 @@ public class MediaTypeTest {

@Test public void testCharsetNameIsDoubleQuotedAndSingleQuoted() throws Exception {
MediaType mediaType = MediaType.parse("text/plain;charset=\"'utf-8'\"");
try {
mediaType.charset();
fail();
} catch (IllegalCharsetNameException expected) {
}
assertNull(mediaType.charset());
}

@Test public void testCharsetNameIsDoubleQuotedSingleQuote() throws Exception {
MediaType mediaType = MediaType.parse("text/plain;charset=\"'\"");
try {
mediaType.charset();
fail();
} catch (IllegalCharsetNameException expected) {
}
assertNull(mediaType.charset());
}

@Test public void testDefaultCharset() throws Exception {
Expand All @@ -189,7 +166,7 @@ public class MediaTypeTest {
MediaType mediaType = MediaType.parse("text/plain;");
assertEquals("text", mediaType.type());
assertEquals("plain", mediaType.subtype());
assertEquals(null, mediaType.charset());
assertNull(mediaType.charset());
assertEquals("text/plain;", mediaType.toString());
}

Expand Down
14 changes: 9 additions & 5 deletions okhttp/src/main/java/okhttp3/MediaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static MediaType parse(String string) {
charsetParameter = parameter.group(3);
}
if (charset != null && !charsetParameter.equalsIgnoreCase(charset)) {
throw new IllegalArgumentException("Multiple different charsets: " + string);
return null; // Multiple different charsets!
}
charset = charsetParameter;
}
Expand All @@ -100,15 +100,19 @@ public String subtype() {
* Returns the charset of this media type, or null if this media type doesn't specify a charset.
*/
public Charset charset() {
return charset != null ? Charset.forName(charset) : null;
return charset(null);
}

/**
* Returns the charset of this media type, or {@code defaultValue} if this media type doesn't
* specify a charset.
* Returns the charset of this media type, or {@code defaultValue} if either this media type
* doesn't specify a charset, of it its charset is unsupported by the current runtime.
*/
public Charset charset(Charset defaultValue) {
return charset != null ? Charset.forName(charset) : defaultValue;
try {
return charset != null ? Charset.forName(charset) : defaultValue;
} catch (IllegalArgumentException e) {
return defaultValue; // This charset is invalid or unsupported. Give up.
}
}

/**
Expand Down

0 comments on commit 6651a9c

Please sign in to comment.