Fix bug where charset was always added to the Content-Type header even for binary responses. Now charset is only added if it was explicitly set via res.charset, or if the response was actually encoded using the given charset (i.e. writeBinary() wasn't used). Change default charset to UTF-8.

This commit is contained in:
hns 2009-09-10 13:23:38 +00:00
parent 900a251d25
commit 51f31cac8a
2 changed files with 16 additions and 14 deletions

View file

@ -643,8 +643,10 @@ public final class ResponseTrans extends Writer implements Serializable {
/** /**
* This has to be called after writing to this response has finished and before it is shipped back to the * This has to be called after writing to this response has finished and before it is shipped back to the
* web server. Transforms the string buffer into a byte array for transmission. * web server. Transforms the string buffer into a byte array for transmission.
* @param defaultCharset the charset to use if no explicit charset has been set on the response
* @throws UnsupportedEncodingException if the charset is not a valid encoding name
*/ */
public synchronized void close(String cset) throws UnsupportedEncodingException { public synchronized void close(String defaultCharset) throws UnsupportedEncodingException {
// if the response was already written and committed by the application // if the response was already written and committed by the application
// there's no point in closing the response buffer // there's no point in closing the response buffer
HttpServletResponse res = reqtrans.getServletResponse(); HttpServletResponse res = reqtrans.getServletResponse();
@ -657,21 +659,20 @@ public final class ResponseTrans extends Writer implements Serializable {
return; return;
} }
// only use default charset if not explicitly set for this response.
if (charset == null) {
charset = cset;
}
// if charset is not set, use western encoding
if (charset == null) {
charset = "ISO-8859-1";
}
boolean encodingError = false; boolean encodingError = false;
// only close if the response hasn't been closed yet, and if no // only close if the response hasn't been closed yet, and if no
// response was generated using writeBinary(). // response was generated using writeBinary().
if (response == null) { if (response == null) {
// only use default charset if not explicitly set for this response.
if (charset == null) {
charset = defaultCharset;
}
// if charset is not set, use western encoding
if (charset == null) {
charset = "UTF-8";
}
// if debug buffer exists, append it to main buffer // if debug buffer exists, append it to main buffer
if (contentType != null && if (contentType != null &&
contentType.startsWith("text/html") && contentType.startsWith("text/html") &&
@ -719,8 +720,9 @@ public final class ResponseTrans extends Writer implements Serializable {
response = new byte[0]; response = new byte[0];
notModified = true; notModified = true;
} }
} catch (Exception ignore) { } catch (Exception e) {
// Etag creation failed for some reason. Ignore. // Etag creation failed for some reason.
app.logError("Error creating ETag: " + e);
} }
} }

View file

@ -1822,7 +1822,7 @@ public final class Application implements Runnable {
props.update(); props.update();
// character encoding to be used for responses // character encoding to be used for responses
charset = props.getProperty("charset", "ISO-8859-1"); charset = props.getProperty("charset", "UTF-8");
// debug flag // debug flag
debug = "true".equalsIgnoreCase(props.getProperty("debug")); debug = "true".equalsIgnoreCase(props.getProperty("debug"));