Skip to content

Commit

Permalink
HTTPCLIENT-2300: AbstractCharResponseConsumer to use UTF-8 by default…
Browse files Browse the repository at this point in the history
… if a charset has not been explicitly specified by the Content-Type
  • Loading branch information
ok2c committed Sep 24, 2023
1 parent 2bc3991 commit dc56b36
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,6 +52,18 @@
public abstract class AbstractCharResponseConsumer<T> extends AbstractCharDataConsumer implements AsyncResponseConsumer<T> {

private volatile FutureCallback<T> 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.
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit dc56b36

Please sign in to comment.