Skip to content

Commit 247fb46

Browse files
authored
fall back to utf-8 for invalid content-type charset (OpenFeign#3427)
Response.charset() and feign-form's FormEncoder.getCharset() passed the parsed charset token straight to Charset.forName, so an unsupported or illegal name from a Content-Type header threw instead of using the documented UTF-8 default. Catch the charset exceptions in both and fall back to UTF-8, as FeignException.getResponseCharset already does.
1 parent 93144ab commit 247fb46

4 files changed

Lines changed: 100 additions & 2 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import feign.Request.ProtocolVersion;
2121
import java.io.*;
2222
import java.nio.charset.Charset;
23+
import java.nio.charset.IllegalCharsetNameException;
2324
import java.nio.charset.StandardCharsets;
25+
import java.nio.charset.UnsupportedCharsetException;
2426
import java.util.*;
2527

2628
/** An immutable response to an http invocation which only returns string content. */
@@ -219,7 +221,11 @@ public Charset charset() {
219221
String[] charsetParts = contentTypeParmeters[1].split("=");
220222
if (charsetParts.length == 2 && "charset".equalsIgnoreCase(charsetParts[0].trim())) {
221223
String charsetString = charsetParts[1].replaceAll("\"", "");
222-
return Charset.forName(charsetString);
224+
try {
225+
return Charset.forName(charsetString);
226+
} catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
227+
return Util.UTF_8;
228+
}
223229
}
224230
}
225231
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,36 @@ void charsetSupportsMediaTypesWithQuotedCharset() {
8787
assertThat(response.charset()).isEqualTo(Util.UTF_8);
8888
}
8989

90+
@Test
91+
void charsetDefaultsToUtf8ForIllegalCharsetName() {
92+
Map<String, Collection<String>> headersMap = new LinkedHashMap<>();
93+
headersMap.put("Content-Type", Collections.singletonList("text/plain; charset=@"));
94+
Response response =
95+
Response.builder()
96+
.status(200)
97+
.headers(headersMap)
98+
.request(
99+
Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
100+
.body(new byte[0])
101+
.build();
102+
assertThat(response.charset()).isEqualTo(Util.UTF_8);
103+
}
104+
105+
@Test
106+
void charsetDefaultsToUtf8ForUnsupportedCharset() {
107+
Map<String, Collection<String>> headersMap = new LinkedHashMap<>();
108+
headersMap.put("Content-Type", Collections.singletonList("text/plain; charset=made-up-99"));
109+
Response response =
110+
Response.builder()
111+
.status(200)
112+
.headers(headersMap)
113+
.request(
114+
Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
115+
.body(new byte[0])
116+
.build();
117+
assertThat(response.charset()).isEqualTo(Util.UTF_8);
118+
}
119+
90120
@Test
91121
void headerValuesWithSameNameOnlyVaryingInCaseAreMerged() {
92122
Map<String, Collection<String>> headersMap = new LinkedHashMap<>();

‎form/src/main/java/feign/form/FormEncoder.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import feign.codec.Encoder;
2828
import java.lang.reflect.Type;
2929
import java.nio.charset.Charset;
30+
import java.nio.charset.IllegalCharsetNameException;
31+
import java.nio.charset.UnsupportedCharsetException;
3032
import java.util.Collection;
3133
import java.util.HashMap;
3234
import java.util.Map;
@@ -130,6 +132,13 @@ private String getContentTypeValue(Map<String, Collection<String>> headers) {
130132

131133
private Charset getCharset(String contentTypeValue) {
132134
val matcher = CHARSET_PATTERN.matcher(contentTypeValue);
133-
return matcher.find() ? Charset.forName(matcher.group(1)) : UTF_8;
135+
if (!matcher.find()) {
136+
return UTF_8;
137+
}
138+
try {
139+
return Charset.forName(matcher.group(1));
140+
} catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
141+
return UTF_8;
142+
}
134143
}
135144
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright © 2012 The Feign Authors (feign@commonhaus.dev)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package feign.form;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import feign.RequestTemplate;
21+
import java.nio.charset.StandardCharsets;
22+
import java.util.LinkedHashMap;
23+
import java.util.Map;
24+
import org.junit.jupiter.api.Test;
25+
26+
class FormEncoderCharsetTest {
27+
28+
@Test
29+
void illegalCharsetInContentTypeFallsBackToUtf8() {
30+
RequestTemplate template = new RequestTemplate();
31+
template.header("Content-Type", "application/x-www-form-urlencoded; charset=_bad");
32+
33+
Map<String, Object> data = new LinkedHashMap<>();
34+
data.put("foo", "bar");
35+
36+
new FormEncoder().encode(data, Map.class, template);
37+
38+
assertThat(new String(template.body(), StandardCharsets.UTF_8)).isEqualTo("foo=bar");
39+
}
40+
41+
@Test
42+
void unsupportedCharsetInContentTypeFallsBackToUtf8() {
43+
RequestTemplate template = new RequestTemplate();
44+
template.header("Content-Type", "application/x-www-form-urlencoded; charset=made-up-99");
45+
46+
Map<String, Object> data = new LinkedHashMap<>();
47+
data.put("foo", "bar");
48+
49+
new FormEncoder().encode(data, Map.class, template);
50+
51+
assertThat(new String(template.body(), StandardCharsets.UTF_8)).isEqualTo("foo=bar");
52+
}
53+
}

0 commit comments

Comments
 (0)