Don't use null charset in RequestLogger (#22197)
If the response comes back with a content type with a `null` charset we were blindly using it, causing `NullPointerException`s. Closes #22190
This commit is contained in:
parent
b6cbcc49ba
commit
2aa89820f3
|
@ -152,7 +152,7 @@ final class RequestLogger {
|
|||
httpResponse.setEntity(entity);
|
||||
ContentType contentType = ContentType.get(entity);
|
||||
Charset charset = StandardCharsets.UTF_8;
|
||||
if (contentType != null) {
|
||||
if (contentType != null && contentType.getCharset() != null) {
|
||||
charset = contentType.getCharset();
|
||||
}
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), charset))) {
|
||||
|
|
|
@ -44,6 +44,7 @@ import java.io.ByteArrayInputStream;
|
|||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
|
@ -51,7 +52,6 @@ import static org.junit.Assert.assertEquals;
|
|||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class RequestLoggerTests extends RestClientTestCase {
|
||||
|
||||
public void testTraceRequest() throws IOException, URISyntaxException {
|
||||
HttpHost host = new HttpHost("localhost", 9200, randomBoolean() ? "http" : "https");
|
||||
String expectedEndpoint = "/index/type/_api";
|
||||
|
@ -69,7 +69,7 @@ public class RequestLoggerTests extends RestClientTestCase {
|
|||
expected += " -d '" + requestBody + "'";
|
||||
HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request;
|
||||
HttpEntity entity;
|
||||
switch(randomIntBetween(0, 3)) {
|
||||
switch(randomIntBetween(0, 4)) {
|
||||
case 0:
|
||||
entity = new StringEntity(requestBody, StandardCharsets.UTF_8);
|
||||
break;
|
||||
|
@ -82,6 +82,10 @@ public class RequestLoggerTests extends RestClientTestCase {
|
|||
case 3:
|
||||
entity = new NByteArrayEntity(requestBody.getBytes(StandardCharsets.UTF_8));
|
||||
break;
|
||||
case 4:
|
||||
// Evil entity without a charset
|
||||
entity = new StringEntity(requestBody, (Charset) null);
|
||||
break;
|
||||
default:
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
@ -116,11 +120,20 @@ public class RequestLoggerTests extends RestClientTestCase {
|
|||
expected += "\n# \"field\": \"value\"";
|
||||
expected += "\n# }";
|
||||
HttpEntity entity;
|
||||
if (getRandom().nextBoolean()) {
|
||||
switch(randomIntBetween(0, 2)) {
|
||||
case 0:
|
||||
entity = new StringEntity(responseBody, StandardCharsets.UTF_8);
|
||||
} else {
|
||||
break;
|
||||
case 1:
|
||||
//test a non repeatable entity
|
||||
entity = new InputStreamEntity(new ByteArrayInputStream(responseBody.getBytes(StandardCharsets.UTF_8)));
|
||||
break;
|
||||
case 2:
|
||||
// Evil entity without a charset
|
||||
entity = new StringEntity(responseBody, (Charset) null);
|
||||
break;
|
||||
default:
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
httpResponse.setEntity(entity);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue