Skip to content

Commit 133374d

Browse files
stefanvitzStefan Vitz (C804185)velo
authored
fix: Response.charset does not support RFC 7231 compliant Content-Type headers using quotation marks as application/json; charset="utf-8" (#2444)
Co-authored-by: Stefan Vitz (C804185) <stefan.vitz@sanitas.com> Co-authored-by: Marvin <velo@users.noreply.github.com>
1 parent 28be904 commit 133374d

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

core/src/main/java/feign/Response.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ public int status() {
168168
}
169169

170170
/**
171-
* Nullable and not set when using http/2
172-
*
173-
* <p>See https://github.com/http2/http2-spec/issues/202
171+
* Nullable and not set when using http/2 See <a
172+
* href="/http2/http2-spec/issues/202">...</a> See <a
173+
* href="https://github.com/http2/http2-spec/issues/202">...</a>
174174
*/
175175
public String reason() {
176176
return reason;
@@ -200,6 +200,12 @@ public ProtocolVersion protocolVersion() {
200200
return protocolVersion;
201201
}
202202

203+
/**
204+
* Returns a charset object based on the requests content type. Defaults to UTF-8 See <a
205+
* href="https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.3">rfc7231 - Accept-Charset</a>
206+
* See <a href="https://datatracker.ietf.org/doc/html/rfc7231#section-3.1.1.1">rfc7231 - Media
207+
* Type</a>
208+
*/
203209
public Charset charset() {
204210

205211
Collection<String> contentTypeHeaders = headers().get("Content-Type");
@@ -210,7 +216,8 @@ public Charset charset() {
210216
if (contentTypeParmeters.length > 1) {
211217
String[] charsetParts = contentTypeParmeters[1].split("=");
212218
if (charsetParts.length == 2 && "charset".equalsIgnoreCase(charsetParts[0].trim())) {
213-
return Charset.forName(charsetParts[1]);
219+
String charsetString = charsetParts[1].replaceAll("\"", "");
220+
return Charset.forName(charsetString);
214221
}
215222
}
216223
}
@@ -309,7 +316,7 @@ public Reader asReader() {
309316
}
310317

311318
@Override
312-
public Reader asReader(Charset charset) throws IOException {
319+
public Reader asReader(Charset charset) {
313320
checkNotNull(charset, "charset should not be null");
314321
return new InputStreamReader(inputStream, charset);
315322
}
@@ -354,23 +361,23 @@ public boolean isRepeatable() {
354361
}
355362

356363
@Override
357-
public InputStream asInputStream() throws IOException {
364+
public InputStream asInputStream() {
358365
return new ByteArrayInputStream(data);
359366
}
360367

361368
@SuppressWarnings("deprecation")
362369
@Override
363-
public Reader asReader() throws IOException {
370+
public Reader asReader() {
364371
return new InputStreamReader(asInputStream(), UTF_8);
365372
}
366373

367374
@Override
368-
public Reader asReader(Charset charset) throws IOException {
375+
public Reader asReader(Charset charset) {
369376
checkNotNull(charset, "charset should not be null");
370377
return new InputStreamReader(asInputStream(), charset);
371378
}
372379

373380
@Override
374-
public void close() throws IOException {}
381+
public void close() {}
375382
}
376383
}

core/src/test/java/feign/ResponseTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ void canAccessHeadersCaseInsensitively() {
6969
});
7070
}
7171

72+
@Test
73+
void charsetSupportsMediaTypesWithQuotedCharset() {
74+
Map<String, Collection<String>> headersMap = new LinkedHashMap<>();
75+
List<String> valueList = Collections.singletonList("application/json; charset=\"utf-8\"");
76+
headersMap.put("Content-Type", valueList);
77+
Response response =
78+
Response.builder()
79+
.status(200)
80+
.headers(headersMap)
81+
.request(
82+
Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
83+
.body(new byte[0])
84+
.build();
85+
assertThat(response.charset()).isEqualTo(Util.UTF_8);
86+
}
87+
7288
@Test
7389
void headerValuesWithSameNameOnlyVaryingInCaseAreMerged() {
7490
Map<String, Collection<String>> headersMap = new LinkedHashMap<>();

0 commit comments

Comments
 (0)