Skip to content

Commit 429a832

Browse files
committed
HADOOP-7093. Servlets should default to text/plain. Contributed by Todd Lipcon
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1058822 13f79535-47bb-0310-9956-ffa450edef68
1 parent 330ec75 commit 429a832

File tree

6 files changed

+81
-16
lines changed

6 files changed

+81
-16
lines changed

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ Release 0.22.0 - Unreleased
438438
HADOOP-7097. JAVA_LIBRARY_PATH missing base directory. (Noah Watkins via
439439
todd)
440440

441+
HADOOP-7093. Servlets should default to text/plain (todd)
442+
441443
Release 0.21.1 - Unreleased
442444

443445
IMPROVEMENTS

src/java/org/apache/hadoop/conf/ConfServlet.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
6969
}
7070

7171
if (FORMAT_XML.equals(format)) {
72-
response.setContentType("text/xml");
72+
response.setContentType("text/xml; charset=utf-8");
7373
} else if (FORMAT_JSON.equals(format)) {
74-
response.setContentType("text/javascript");
74+
response.setContentType("application/json; charset=utf-8");
7575
}
7676

77-
OutputStreamWriter out = new OutputStreamWriter(response.getOutputStream());
77+
Writer out = response.getWriter();
7878
try {
7979
writeResponse(getConfFromContext(), out, format);
8080
} catch (BadFormatException bfe) {

src/java/org/apache/hadoop/http/HttpServer.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
739739
return;
740740
}
741741

742-
PrintWriter out = new PrintWriter
743-
(HtmlQuoting.quoteOutputStream(response.getOutputStream()));
742+
PrintWriter out = response.getWriter();
744743
ReflectionUtils.printThreadInfo(out, "");
745744
out.close();
746745
ReflectionUtils.logThreadInfo(LOG, "jsp requested", 1);
@@ -858,12 +857,16 @@ public void doFilter(ServletRequest request,
858857
HttpServletResponse httpResponse = (HttpServletResponse) response;
859858

860859
String mime = inferMimeType(request);
861-
if (mime == null || mime.equals("text/html")) {
862-
// no extension or HTML with unspecified encoding, we want to
860+
if (mime == null) {
861+
httpResponse.setContentType("text/plain; charset=utf-8");
862+
} else if (mime.startsWith("text/html")) {
863+
// HTML with unspecified encoding, we want to
863864
// force HTML with utf-8 encoding
864865
// This is to avoid the following security issue:
865866
// http://openmya.hacker.jp/hasegawa/security/utf7cs.html
866867
httpResponse.setContentType("text/html; charset=utf-8");
868+
} else if (mime.startsWith("application/xml")) {
869+
httpResponse.setContentType("text/xml; charset=utf-8");
867870
}
868871
chain.doFilter(quoted, httpResponse);
869872
}

src/java/org/apache/hadoop/metrics/MetricsServlet.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,26 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
112112
return;
113113
}
114114

115-
PrintWriter out = new PrintWriter(response.getOutputStream());
116115
String format = request.getParameter("format");
117116
Collection<MetricsContext> allContexts =
118117
ContextFactory.getFactory().getAllContexts();
119118
if ("json".equals(format)) {
120-
// Uses Jetty's built-in JSON support to convert the map into JSON.
121-
out.print(new JSON().toJSON(makeMap(allContexts)));
119+
response.setContentType("application/json; charset=utf-8");
120+
PrintWriter out = response.getWriter();
121+
try {
122+
// Uses Jetty's built-in JSON support to convert the map into JSON.
123+
out.print(new JSON().toJSON(makeMap(allContexts)));
124+
} finally {
125+
out.close();
126+
}
122127
} else {
123-
printMap(out, makeMap(allContexts));
128+
PrintWriter out = response.getWriter();
129+
try {
130+
printMap(out, makeMap(allContexts));
131+
} finally {
132+
out.close();
133+
}
124134
}
125-
out.close();
126135
}
127136

128137
/**

src/test/core/org/apache/hadoop/http/TestHttpServer.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.IOException;
2121
import java.io.PrintStream;
22+
import java.io.PrintWriter;
2223
import java.net.URLConnection;
2324
import java.net.HttpURLConnection;
2425
import java.net.URL;
@@ -67,7 +68,7 @@ public static class EchoMapServlet extends HttpServlet {
6768
public void doGet(HttpServletRequest request,
6869
HttpServletResponse response
6970
) throws ServletException, IOException {
70-
PrintStream out = new PrintStream(response.getOutputStream());
71+
PrintWriter out = response.getWriter();
7172
Map<String, String[]> params = request.getParameterMap();
7273
SortedSet<String> keys = new TreeSet(params.keySet());
7374
for(String key: keys) {
@@ -94,7 +95,7 @@ public static class EchoServlet extends HttpServlet {
9495
public void doGet(HttpServletRequest request,
9596
HttpServletResponse response
9697
) throws ServletException, IOException {
97-
PrintStream out = new PrintStream(response.getOutputStream());
98+
PrintWriter out = response.getWriter();
9899
SortedSet<String> sortedKeys = new TreeSet();
99100
Enumeration<String> keys = request.getParameterNames();
100101
while(keys.hasMoreElements()) {
@@ -110,10 +111,25 @@ public void doGet(HttpServletRequest request,
110111
}
111112
}
112113

114+
@SuppressWarnings("serial")
115+
public static class HtmlContentServlet extends HttpServlet {
116+
@SuppressWarnings("unchecked")
117+
@Override
118+
public void doGet(HttpServletRequest request,
119+
HttpServletResponse response
120+
) throws ServletException, IOException {
121+
response.setContentType("text/html");
122+
PrintWriter out = response.getWriter();
123+
out.print("hello world");
124+
out.close();
125+
}
126+
}
127+
113128
@BeforeClass public static void setup() throws Exception {
114129
server = createTestServer();
115130
server.addServlet("echo", "/echo", EchoServlet.class);
116131
server.addServlet("echomap", "/echomap", EchoMapServlet.class);
132+
server.addServlet("htmlcontent", "/htmlcontent", HtmlContentServlet.class);
117133
server.start();
118134
baseUrl = getServerURL(server);
119135
}
@@ -176,19 +192,33 @@ public void run() {
176192
assertEquals(200, conn.getResponseCode());
177193
assertEquals("text/css", conn.getContentType());
178194

179-
// Servlets should have text/html with proper encoding
195+
// Servlets should have text/plain with proper encoding by default
180196
URL servletUrl = new URL(baseUrl, "/echo?a=b");
181197
conn = (HttpURLConnection)servletUrl.openConnection();
182198
conn.connect();
183199
assertEquals(200, conn.getResponseCode());
184-
assertEquals("text/html; charset=utf-8", conn.getContentType());
200+
assertEquals("text/plain; charset=utf-8", conn.getContentType());
185201

186202
// We should ignore parameters for mime types - ie a parameter
187203
// ending in .css should not change mime type
188204
servletUrl = new URL(baseUrl, "/echo?a=b.css");
189205
conn = (HttpURLConnection)servletUrl.openConnection();
190206
conn.connect();
191207
assertEquals(200, conn.getResponseCode());
208+
assertEquals("text/plain; charset=utf-8", conn.getContentType());
209+
210+
// Servlets that specify text/html should get that content type
211+
servletUrl = new URL(baseUrl, "/htmlcontent");
212+
conn = (HttpURLConnection)servletUrl.openConnection();
213+
conn.connect();
214+
assertEquals(200, conn.getResponseCode());
215+
assertEquals("text/html; charset=utf-8", conn.getContentType());
216+
217+
// JSPs should default to text/html with utf8
218+
servletUrl = new URL(baseUrl, "/testjsp.jsp");
219+
conn = (HttpURLConnection)servletUrl.openConnection();
220+
conn.connect();
221+
assertEquals(200, conn.getResponseCode());
192222
assertEquals("text/html; charset=utf-8", conn.getContentType());
193223
}
194224

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?><%!
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
%>
20+
<%@ page contentType="text/html; charset=UTF-8" %>
21+
Hello world!

0 commit comments

Comments
 (0)