Skip to content

Commit 765b1a7

Browse files
committed
Defaulting to UTF-8 if charset is missing
Some servers don't return the charset. This causes german characters to be encoded incorrectly, since ISO_8859_1 does not work very well in such cases defaulting to UTF-8 if its missing. https://www.iana.org/assignments/media-types/text/csv
1 parent 41e54fa commit 765b1a7

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java

+4
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,10 @@ public Charset getContentCharset() {
534534
// https://tools.ietf.org/html/rfc4627 - JSON must be encoded with UTF-8
535535
return StandardCharsets.UTF_8;
536536
}
537+
// fallback to well-kown charset for text/csv
538+
if ("text".equals(mediaType.getType()) && "csv".equals(mediaType.getSubType())) {
539+
return StandardCharsets.UTF_8;
540+
}
537541
}
538542
return StandardCharsets.ISO_8859_1;
539543
}

google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java

+28
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public void testParseAsString_none() throws Exception {
6868
private static final String VALID_CONTENT_TYPE = "text/plain";
6969
private static final String VALID_CONTENT_TYPE_WITH_PARAMS =
7070
"application/vnd.com.google.datastore.entity+json; charset=utf-8; version=v1; q=0.9";
71+
private static final String VALID_CONTENT_TYPE_WITHOUT_CHARSET =
72+
"text/csv; version=v1; q=0.9";
7173
private static final String INVALID_CONTENT_TYPE = "!!!invalid!!!";
7274
private static final String JSON_CONTENT_TYPE = "application/json";
7375

@@ -194,6 +196,32 @@ public LowLevelHttpResponse execute() throws IOException {
194196
assertEquals("ISO-8859-1", response.getContentCharset().name());
195197
}
196198

199+
public void testParseAsString_validContentTypeWithoutCharSetWithParams() throws Exception {
200+
HttpTransport transport =
201+
new MockHttpTransport() {
202+
@Override
203+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
204+
return new MockLowLevelHttpRequest() {
205+
@Override
206+
public LowLevelHttpResponse execute() throws IOException {
207+
MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
208+
result.setContent(SAMPLE2);
209+
result.setContentType(VALID_CONTENT_TYPE_WITHOUT_CHARSET);
210+
return result;
211+
}
212+
};
213+
}
214+
};
215+
HttpRequest request =
216+
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
217+
218+
HttpResponse response = request.execute();
219+
assertEquals(SAMPLE2, response.parseAsString());
220+
assertEquals(VALID_CONTENT_TYPE_WITHOUT_CHARSET, response.getContentType());
221+
assertNotNull(response.getMediaType());
222+
assertEquals("UTF-8", response.getContentCharset().name());
223+
}
224+
197225
public void testParseAsString_jsonContentType() throws IOException {
198226
HttpTransport transport =
199227
new MockHttpTransport() {

0 commit comments

Comments
 (0)