add charset to text content type if missing

This commit is contained in:
Jörg Prante 2023-12-19 17:51:53 +01:00
parent e41d5cf701
commit 3c7e102765
2 changed files with 13 additions and 6 deletions

View file

@ -1,3 +1,3 @@
group = org.xbib group = org.xbib
name = net-http name = net-http
version = 4.0.7 version = 4.0.8

View file

@ -156,6 +156,9 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder {
return this; return this;
} }
if (HttpHeaderNames.CONTENT_TYPE.equalsIgnoreCase(name.toString())) { if (HttpHeaderNames.CONTENT_TYPE.equalsIgnoreCase(name.toString())) {
if (value.startsWith("text") && charset != null) {
value = value + "; charset=" + charset.name().toLowerCase(Locale.ROOT);
}
setContentType(value); setContentType(value);
} }
if (headers.containsHeader(name)) { if (headers.containsHeader(name)) {
@ -191,7 +194,7 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder {
if (done) { if (done) {
return this; return this;
} }
this.contentType = contentType; this.contentType = Objects.requireNonNull(contentType);
return this; return this;
} }
@ -351,13 +354,17 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder {
public void buildHeaders(long contentLength) { public void buildHeaders(long contentLength) {
this.length = contentLength; this.length = contentLength;
if (!headers.containsHeader(HttpHeaderNames.CONTENT_TYPE)) { if (!headers.containsHeader(HttpHeaderNames.CONTENT_TYPE)) {
if (contentType == null) { if (contentType != null && contentType.startsWith("text") && !contentType.contains("charset") && charset != null) {
contentType = HttpHeaderValues.APPLICATION_OCTET_STREAM;
}
if (!contentType.contains("charset=") && charset != null) {
contentType = contentType + "; charset=" + charset.name(); contentType = contentType + "; charset=" + charset.name();
} }
headers.add(HttpHeaderNames.CONTENT_TYPE, contentType); headers.add(HttpHeaderNames.CONTENT_TYPE, contentType);
} else {
String s = headers.get(HttpHeaderNames.CONTENT_TYPE);
if (s != null && s.startsWith("text") && !s.contains("charset") && charset != null) {
s = s + "; charset=" + charset.name();
}
headers.remove(HttpHeaderNames.CONTENT_TYPE);
headers.add(HttpHeaderNames.CONTENT_TYPE, s);
} }
if (status == null) { if (status == null) {
logger.log(Level.WARNING, "no status code set by handlers, assuming OK"); logger.log(Level.WARNING, "no status code set by handlers, assuming OK");