From 5f46cde33421cf0c8dc1899b68de29e67971fcc5 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sun, 24 Sep 2023 09:47:53 +0200 Subject: [PATCH] HTTPCLIENT-2300: abstract char message consumers to use UTF-8 by default if a charset has not been explicitly specified by the Content-Type --- .../async/methods/AbstractCharPushConsumer.java | 16 +++++++++++++++- .../methods/AbstractCharResponseConsumer.java | 15 ++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/AbstractCharPushConsumer.java b/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/AbstractCharPushConsumer.java index fccf509c6d..2d26172a80 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/AbstractCharPushConsumer.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/AbstractCharPushConsumer.java @@ -37,6 +37,7 @@ import org.apache.hc.core5.http.HttpException; import org.apache.hc.core5.http.HttpRequest; import org.apache.hc.core5.http.HttpResponse; +import org.apache.hc.core5.http.config.CharCodingConfig; import org.apache.hc.core5.http.nio.AsyncPushConsumer; import org.apache.hc.core5.http.nio.entity.AbstractCharDataConsumer; import org.apache.hc.core5.http.protocol.HttpContext; @@ -48,6 +49,19 @@ */ public abstract class AbstractCharPushConsumer extends AbstractCharDataConsumer implements AsyncPushConsumer { + private final Charset defaultCharset; + + public AbstractCharPushConsumer() { + this.defaultCharset = StandardCharsets.UTF_8; + } + + protected AbstractCharPushConsumer(final int bufSize, + final CharCodingConfig charCodingConfig) { + super(bufSize, charCodingConfig); + this.defaultCharset = charCodingConfig != null && charCodingConfig.getCharset() != null + ? charCodingConfig.getCharset() : StandardCharsets.UTF_8; + } + /** * Triggered to signal the beginning of data processing. * @@ -72,7 +86,7 @@ public final void consumePromise( } Charset charset = contentType != null ? contentType.getCharset() : null; if (charset == null) { - charset = StandardCharsets.US_ASCII; + charset = defaultCharset; } setCharset(charset); start(promise, response, contentType != null ? contentType : ContentType.DEFAULT_TEXT); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/AbstractCharResponseConsumer.java b/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/AbstractCharResponseConsumer.java index 825f93f729..eb39dbfaa2 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/AbstractCharResponseConsumer.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/AbstractCharResponseConsumer.java @@ -37,6 +37,7 @@ import org.apache.hc.core5.http.EntityDetails; import org.apache.hc.core5.http.HttpException; import org.apache.hc.core5.http.HttpResponse; +import org.apache.hc.core5.http.config.CharCodingConfig; import org.apache.hc.core5.http.nio.AsyncResponseConsumer; import org.apache.hc.core5.http.nio.entity.AbstractCharDataConsumer; import org.apache.hc.core5.http.protocol.HttpContext; @@ -51,6 +52,18 @@ public abstract class AbstractCharResponseConsumer extends AbstractCharDataConsumer implements AsyncResponseConsumer { private volatile FutureCallback resultCallback; + private final Charset defaultCharset; + + public AbstractCharResponseConsumer() { + this.defaultCharset = StandardCharsets.UTF_8; + } + + protected AbstractCharResponseConsumer(final int bufSize, + final CharCodingConfig charCodingConfig) { + super(bufSize, charCodingConfig); + this.defaultCharset = charCodingConfig != null && charCodingConfig.getCharset() != null + ? charCodingConfig.getCharset() : StandardCharsets.UTF_8; + } /** * Triggered to signal the beginning of data processing. @@ -90,7 +103,7 @@ public final void consumeResponse( } Charset charset = contentType != null ? contentType.getCharset() : null; if (charset == null) { - charset = StandardCharsets.US_ASCII; + charset = defaultCharset; } setCharset(charset); start(response, contentType != null ? contentType : ContentType.DEFAULT_TEXT);