Skip to content

Commit

Permalink
HTTPCLIENT-2300: abstract char message consumers to use UTF-8 by defa…
Browse files Browse the repository at this point in the history
…ult if a charset has not been explicitly specified by the Content-Type
  • Loading branch information
ok2c committed Sep 26, 2023
1 parent 2bc3991 commit 5f46cde
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -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);
Expand Down
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 5f46cde

Please sign in to comment.