Skip to content

Commit

Permalink
fix: Response.charset does not support RFC 7231 compliant Content-Typ…
Browse files Browse the repository at this point in the history
…e headers using quotation marks as application/json; charset="utf-8" (#2444)

Co-authored-by: Stefan Vitz (C804185) <[email protected]>
Co-authored-by: Marvin <[email protected]>
  • Loading branch information
3 people authored Jun 12, 2024
1 parent 28be904 commit 133374d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
25 changes: 16 additions & 9 deletions core/src/main/java/feign/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ public int status() {
}

/**
* Nullable and not set when using http/2
*
* <p>See https://github.com/http2/http2-spec/issues/202
* Nullable and not set when using http/2 See <a
* href="https://github.com/http2/http2-spec/issues/202">...</a> See <a
* href="https://github.com/http2/http2-spec/issues/202">...</a>
*/
public String reason() {
return reason;
Expand Down Expand Up @@ -200,6 +200,12 @@ public ProtocolVersion protocolVersion() {
return protocolVersion;
}

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

Collection<String> contentTypeHeaders = headers().get("Content-Type");
Expand All @@ -210,7 +216,8 @@ public Charset charset() {
if (contentTypeParmeters.length > 1) {
String[] charsetParts = contentTypeParmeters[1].split("=");
if (charsetParts.length == 2 && "charset".equalsIgnoreCase(charsetParts[0].trim())) {
return Charset.forName(charsetParts[1]);
String charsetString = charsetParts[1].replaceAll("\"", "");
return Charset.forName(charsetString);
}
}
}
Expand Down Expand Up @@ -309,7 +316,7 @@ public Reader asReader() {
}

@Override
public Reader asReader(Charset charset) throws IOException {
public Reader asReader(Charset charset) {
checkNotNull(charset, "charset should not be null");
return new InputStreamReader(inputStream, charset);
}
Expand Down Expand Up @@ -354,23 +361,23 @@ public boolean isRepeatable() {
}

@Override
public InputStream asInputStream() throws IOException {
public InputStream asInputStream() {
return new ByteArrayInputStream(data);
}

@SuppressWarnings("deprecation")
@Override
public Reader asReader() throws IOException {
public Reader asReader() {
return new InputStreamReader(asInputStream(), UTF_8);
}

@Override
public Reader asReader(Charset charset) throws IOException {
public Reader asReader(Charset charset) {
checkNotNull(charset, "charset should not be null");
return new InputStreamReader(asInputStream(), charset);
}

@Override
public void close() throws IOException {}
public void close() {}
}
}
16 changes: 16 additions & 0 deletions core/src/test/java/feign/ResponseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ void canAccessHeadersCaseInsensitively() {
});
}

@Test
void charsetSupportsMediaTypesWithQuotedCharset() {
Map<String, Collection<String>> headersMap = new LinkedHashMap<>();
List<String> valueList = Collections.singletonList("application/json; charset=\"utf-8\"");
headersMap.put("Content-Type", valueList);
Response response =
Response.builder()
.status(200)
.headers(headersMap)
.request(
Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
.body(new byte[0])
.build();
assertThat(response.charset()).isEqualTo(Util.UTF_8);
}

@Test
void headerValuesWithSameNameOnlyVaryingInCaseAreMerged() {
Map<String, Collection<String>> headersMap = new LinkedHashMap<>();
Expand Down

0 comments on commit 133374d

Please sign in to comment.