From 776b54648a44a9d996b362ce71816d1187a84a79 Mon Sep 17 00:00:00 2001 From: Artur Date: Thu, 9 Mar 2017 14:50:46 +0200 Subject: [PATCH] Include charset in text/html responses (#8777) Fixes #8775 --- .../src/main/java/com/vaadin/server/BootstrapHandler.java | 7 ++++--- server/src/main/java/com/vaadin/server/VaadinServlet.java | 3 ++- .../com/vaadin/server/communication/FileUploadHandler.java | 4 +++- server/src/main/java/com/vaadin/ui/LoginForm.java | 3 ++- .../main/java/com/vaadin/shared/ApplicationConstants.java | 5 +++++ 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/server/src/main/java/com/vaadin/server/BootstrapHandler.java b/server/src/main/java/com/vaadin/server/BootstrapHandler.java index 74707842d8..4619a89ce8 100644 --- a/server/src/main/java/com/vaadin/server/BootstrapHandler.java +++ b/server/src/main/java/com/vaadin/server/BootstrapHandler.java @@ -352,7 +352,8 @@ public abstract class BootstrapHandler extends SynchronizedRequestHandler { private void writeBootstrapPage(VaadinResponse response, String html) throws IOException { - response.setContentType("text/html"); + response.setContentType( + ApplicationConstants.CONTENT_TYPE_TEXT_HTML_UTF_8); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(response.getOutputStream(), "UTF-8")); writer.append(html); @@ -372,8 +373,8 @@ public abstract class BootstrapHandler extends SynchronizedRequestHandler { document.child(0).before(doctype); Element head = document.head(); - head.appendElement("meta").attr("http-equiv", "Content-Type") - .attr("content", "text/html; charset=utf-8"); + head.appendElement("meta").attr("http-equiv", "Content-Type").attr( + "content", ApplicationConstants.CONTENT_TYPE_TEXT_HTML_UTF_8); /* * Enable Chrome Frame in all versions of IE if installed. diff --git a/server/src/main/java/com/vaadin/server/VaadinServlet.java b/server/src/main/java/com/vaadin/server/VaadinServlet.java index 3dfb5ba33c..f02ef444b9 100644 --- a/server/src/main/java/com/vaadin/server/VaadinServlet.java +++ b/server/src/main/java/com/vaadin/server/VaadinServlet.java @@ -55,6 +55,7 @@ import com.vaadin.annotations.VaadinServletConfiguration; import com.vaadin.annotations.VaadinServletConfiguration.InitParameterName; import com.vaadin.sass.internal.ScssStylesheet; import com.vaadin.server.communication.ServletUIInitHandler; +import com.vaadin.shared.ApplicationConstants; import com.vaadin.shared.JsonConstants; import com.vaadin.shared.Version; import com.vaadin.ui.UI; @@ -587,7 +588,7 @@ public class VaadinServlet extends HttpServlet implements Constants { output += ""; } getService().writeStringResponse(response, - "text/html; charset=UTF-8", output); + ApplicationConstants.CONTENT_TYPE_TEXT_HTML_UTF_8, output); } } diff --git a/server/src/main/java/com/vaadin/server/communication/FileUploadHandler.java b/server/src/main/java/com/vaadin/server/communication/FileUploadHandler.java index f205e28b7d..6a19680da0 100644 --- a/server/src/main/java/com/vaadin/server/communication/FileUploadHandler.java +++ b/server/src/main/java/com/vaadin/server/communication/FileUploadHandler.java @@ -36,6 +36,7 @@ import com.vaadin.server.UploadException; import com.vaadin.server.VaadinRequest; import com.vaadin.server.VaadinResponse; import com.vaadin.server.VaadinSession; +import com.vaadin.shared.ApplicationConstants; import com.vaadin.ui.Component; import com.vaadin.ui.UI; import com.vaadin.ui.Upload.FailedEvent; @@ -682,7 +683,8 @@ public class FileUploadHandler implements RequestHandler { */ protected void sendUploadResponse(VaadinRequest request, VaadinResponse response) throws IOException { - response.setContentType("text/html"); + response.setContentType( + ApplicationConstants.CONTENT_TYPE_TEXT_HTML_UTF_8); final OutputStream out = response.getOutputStream(); final PrintWriter outWriter = new PrintWriter( new BufferedWriter(new OutputStreamWriter(out, "UTF-8"))); diff --git a/server/src/main/java/com/vaadin/ui/LoginForm.java b/server/src/main/java/com/vaadin/ui/LoginForm.java index 89244004db..1a037ddfc8 100644 --- a/server/src/main/java/com/vaadin/ui/LoginForm.java +++ b/server/src/main/java/com/vaadin/ui/LoginForm.java @@ -25,6 +25,7 @@ import java.util.HashMap; import java.util.Map; import com.vaadin.server.StreamResource; +import com.vaadin.shared.ApplicationConstants; import com.vaadin.shared.ui.loginform.LoginFormConstants; import com.vaadin.shared.ui.loginform.LoginFormRpc; import com.vaadin.shared.ui.loginform.LoginFormState; @@ -309,7 +310,7 @@ public class LoginForm extends AbstractSingleComponentContainer { StreamResource resource = new StreamResource(new LoginStreamSource(), LoginFormConstants.LOGIN_RESOURCE_NAME); - resource.setMIMEType("text/html; charset=utf-8"); + resource.setMIMEType(ApplicationConstants.CONTENT_TYPE_TEXT_HTML_UTF_8); resource.setCacheTime(-1); setResource(LoginFormConstants.LOGIN_RESOURCE_NAME, resource); diff --git a/shared/src/main/java/com/vaadin/shared/ApplicationConstants.java b/shared/src/main/java/com/vaadin/shared/ApplicationConstants.java index 142573ea1f..79a597b4f4 100644 --- a/shared/src/main/java/com/vaadin/shared/ApplicationConstants.java +++ b/shared/src/main/java/com/vaadin/shared/ApplicationConstants.java @@ -158,4 +158,9 @@ public class ApplicationConstants implements Serializable { */ public static final String WIDGETSET_VERSION_ID = "wsver"; + /** + * Content type to use for text/html responses (should always be UTF-8). + */ + public static final String CONTENT_TYPE_TEXT_HTML_UTF_8 = "text/html; charset=utf-8"; + } -- 2.39.5