Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed extracting charset from response. #2545

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions core/src/main/java/feign/FeignException.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 The Feign Authors
* Copyright 2012-2024 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand All @@ -21,6 +21,7 @@
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
Expand All @@ -29,6 +30,7 @@
import java.util.regex.Pattern;
import static feign.Util.*;
import static java.lang.String.format;
import static java.util.regex.Pattern.CASE_INSENSITIVE;

/**
* Origin exception type for all Http Apis.
Expand Down Expand Up @@ -519,14 +521,18 @@ private static Charset getResponseCharset(Map<String, Collection<String>> header
return null;
}

Pattern pattern = Pattern.compile(".*charset=([^\\s|^;]+).*");
Pattern pattern = Pattern.compile(".*charset=\"?([^\\s|^;|^\"]+).*", CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(strings.iterator().next());
if (!matcher.lookingAt()) {
return null;
}

String group = matcher.group(1);
if (!Charset.isSupported(group)) {
try {
if (!Charset.isSupported(group)) {
return null;
}
} catch (IllegalCharsetNameException ex) {
return null;
}
return Charset.forName(group);
Expand Down
28 changes: 28 additions & 0 deletions core/src/test/java/feign/FeignExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class FeignExceptionTest {

Expand Down Expand Up @@ -76,6 +78,32 @@ void createFeignExceptionWithCorrectCharsetResponse() {
.isEqualTo("[400] during [GET] to [/home] [methodKey]: [response]");
}

@ParameterizedTest
@ValueSource(strings = {
"application/json;charset=\"UTF-16BE\"",
"application/json; charset=UTF-16BE",
"application/json; charset=\"UTF-16BE\"",
"application/json;charset=UTF-16BE"
})
void createFeignExceptionWithCorrectCharsetResponseButDifferentContentTypeFormats(String contentType) {
Map<String, Collection<String>> map = new HashMap<>();
map.put("connection", new ArrayList<>(Collections.singletonList("keep-alive")));
map.put("content-length", new ArrayList<>(Collections.singletonList("100")));
map.put("content-type",
new ArrayList<>(Collections.singletonList(contentType)));

Request request = Request.create(Request.HttpMethod.GET, "/home", Collections.emptyMap(),
"data".getBytes(StandardCharsets.UTF_16BE), StandardCharsets.UTF_16BE, null);

Response response =
Response.builder().status(400).body("response".getBytes(StandardCharsets.UTF_16BE))
.headers(map).request(request).build();

FeignException exception = FeignException.errorStatus("methodKey", response);
assertThat(exception.getMessage())
.isEqualTo("[400] during [GET] to [/home] [methodKey]: [response]");
}

@Test
void createFeignExceptionWithErrorCharsetResponse() {
Map<String, Collection<String>> map = new HashMap<>();
Expand Down
Loading