mirror of
https://github.com/square/okhttp.git
synced 2025-11-24 18:41:06 +03:00
Change MediaType's failure mode to not crash on charset problems.
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.
This commit is contained in:
@@ -18,7 +18,6 @@ package okhttp3.logging;
|
||||
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;
|
||||
@@ -241,15 +240,7 @@ public final class HttpLoggingInterceptor implements Interceptor {
|
||||
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)) {
|
||||
|
||||
@@ -563,7 +563,7 @@ public final class HttpLoggingInterceptorTest {
|
||||
|
||||
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();
|
||||
|
||||
@@ -578,8 +578,8 @@ public final class HttpLoggingInterceptorTest {
|
||||
.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
|
||||
@@ -589,8 +589,8 @@ public final class HttpLoggingInterceptorTest {
|
||||
.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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user