Skip to content

Commit 93a8afc

Browse files
committed
Fixed #2677 use decoded path for favicon.ico
Signed-off-by: Greg Wilkins <[email protected]>
1 parent ada36ec commit 93a8afc

File tree

2 files changed

+156
-5
lines changed

2 files changed

+156
-5
lines changed

jetty-server/src/main/java/org/eclipse/jetty/server/handler/DefaultHandler.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,30 @@ public class DefaultHandler extends AbstractHandler
5454
private static final Logger LOG = Log.getLogger(DefaultHandler.class);
5555

5656
final long _faviconModified=(System.currentTimeMillis()/1000)*1000L;
57-
byte[] _favicon;
57+
final byte[] _favicon;
5858
boolean _serveIcon=true;
5959
boolean _showContexts=true;
6060

6161
public DefaultHandler()
6262
{
63+
byte[] favbytes=null;
6364
try
6465
{
6566
URL fav = this.getClass().getClassLoader().getResource("org/eclipse/jetty/favicon.ico");
6667
if (fav!=null)
6768
{
6869
Resource r = Resource.newResource(fav);
69-
_favicon=IO.readBytes(r.getInputStream());
70+
favbytes=IO.readBytes(r.getInputStream());
7071
}
7172
}
7273
catch(Exception e)
7374
{
7475
LOG.warn(e);
7576
}
77+
finally
78+
{
79+
_favicon = favbytes;
80+
}
7681
}
7782

7883
/* ------------------------------------------------------------ */
@@ -90,7 +95,7 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques
9095
String method=request.getMethod();
9196

9297
// little cheat for common request
93-
if (_serveIcon && _favicon!=null && HttpMethod.GET.is(method) && request.getRequestURI().equals("/favicon.ico"))
98+
if (_serveIcon && _favicon!=null && HttpMethod.GET.is(method) && target.equals("/favicon.ico"))
9499
{
95100
if (request.getDateHeader(HttpHeader.IF_MODIFIED_SINCE.toString())==_faviconModified)
96101
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
@@ -106,15 +111,14 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques
106111
return;
107112
}
108113

109-
110114
if (!_showContexts || !HttpMethod.GET.is(method) || !request.getRequestURI().equals("/"))
111115
{
112116
response.sendError(HttpServletResponse.SC_NOT_FOUND);
113117
return;
114118
}
115119

116120
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
117-
response.setContentType(MimeTypes.Type.TEXT_HTML.toString());
121+
response.setContentType(MimeTypes.Type.TEXT_HTML_8859_1.asString());
118122

119123
try (ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500);)
120124
{
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
//
2+
// ========================================================================
3+
// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
4+
// ------------------------------------------------------------------------
5+
// All rights reserved. This program and the accompanying materials
6+
// are made available under the terms of the Eclipse Public License v1.0
7+
// and Apache License v2.0 which accompanies this distribution.
8+
//
9+
// The Eclipse Public License is available at
10+
// http://www.eclipse.org/legal/epl-v10.html
11+
//
12+
// The Apache License v2.0 is available at
13+
// http://www.opensource.org/licenses/apache2.0.php
14+
//
15+
// You may elect to redistribute this code under either of these licenses.
16+
// ========================================================================
17+
//
18+
19+
package org.eclipse.jetty.server.handler;
20+
21+
import static org.hamcrest.Matchers.containsString;
22+
import static org.hamcrest.Matchers.not;
23+
24+
import java.io.OutputStream;
25+
import java.net.Socket;
26+
import java.nio.charset.StandardCharsets;
27+
28+
import org.eclipse.jetty.http.HttpHeader;
29+
import org.eclipse.jetty.http.HttpStatus;
30+
import org.eclipse.jetty.http.HttpTester;
31+
import org.eclipse.jetty.server.Handler;
32+
import org.eclipse.jetty.server.Server;
33+
import org.eclipse.jetty.server.ServerConnector;
34+
import org.junit.After;
35+
import org.junit.Assert;
36+
import org.junit.Before;
37+
import org.junit.Test;
38+
39+
public class DefaultHandlerTest
40+
{
41+
private Server server;
42+
private ServerConnector connector;
43+
private DefaultHandler handler;
44+
45+
@Before
46+
public void before() throws Exception
47+
{
48+
server = new Server();
49+
connector = new ServerConnector(server);
50+
server.addConnector(connector);
51+
52+
53+
ContextHandlerCollection contexts = new ContextHandlerCollection();
54+
handler = new DefaultHandler();
55+
HandlerCollection handlers = new HandlerCollection();
56+
handlers.setHandlers(new Handler[] { contexts, handler });
57+
server.setHandler(handlers);
58+
59+
handler.setServeIcon(true);
60+
handler.setShowContexts(true);
61+
62+
contexts.addHandler(new ContextHandler("/foo"));
63+
contexts.addHandler(new ContextHandler("/bar"));
64+
65+
server.start();
66+
}
67+
68+
@After
69+
public void after() throws Exception
70+
{
71+
server.stop();
72+
}
73+
74+
@Test
75+
public void testRoot() throws Exception
76+
{
77+
try (Socket socket = new Socket("localhost", connector.getLocalPort()))
78+
{
79+
String request = "" +
80+
"GET / HTTP/1.1\r\n" +
81+
"Host: localhost\r\n" +
82+
"\r\n";
83+
OutputStream output = socket.getOutputStream();
84+
output.write(request.getBytes(StandardCharsets.UTF_8));
85+
output.flush();
86+
87+
HttpTester.Input input = HttpTester.from(socket.getInputStream());
88+
HttpTester.Response response = HttpTester.parseResponse(input);
89+
90+
Assert.assertEquals(HttpStatus.NOT_FOUND_404, response.getStatus());
91+
Assert.assertEquals("text/html;charset=ISO-8859-1", response.get(HttpHeader.CONTENT_TYPE));
92+
93+
String content = new String(response.getContentBytes(),StandardCharsets.ISO_8859_1);
94+
Assert.assertThat(content,containsString("Contexts known to this server are:"));
95+
Assert.assertThat(content,containsString("/foo"));
96+
Assert.assertThat(content,containsString("/bar"));
97+
}
98+
}
99+
100+
@Test
101+
public void testSomePath() throws Exception
102+
{
103+
try (Socket socket = new Socket("localhost", connector.getLocalPort()))
104+
{
105+
String request = "" +
106+
"GET /some/path HTTP/1.1\r\n" +
107+
"Host: localhost\r\n" +
108+
"\r\n";
109+
OutputStream output = socket.getOutputStream();
110+
output.write(request.getBytes(StandardCharsets.UTF_8));
111+
output.flush();
112+
113+
HttpTester.Input input = HttpTester.from(socket.getInputStream());
114+
HttpTester.Response response = HttpTester.parseResponse(input);
115+
116+
Assert.assertEquals(HttpStatus.NOT_FOUND_404, response.getStatus());
117+
Assert.assertEquals("text/html;charset=ISO-8859-1", response.get(HttpHeader.CONTENT_TYPE));
118+
119+
String content = new String(response.getContentBytes(),StandardCharsets.ISO_8859_1);
120+
Assert.assertThat(content,not(containsString("Contexts known to this server are:")));
121+
Assert.assertThat(content,not(containsString("/foo")));
122+
Assert.assertThat(content,not(containsString("/bar")));
123+
}
124+
}
125+
126+
@Test
127+
public void testFavIcon() throws Exception
128+
{
129+
try (Socket socket = new Socket("localhost", connector.getLocalPort()))
130+
{
131+
String request = "" +
132+
"GET /favicon.ico HTTP/1.1\r\n" +
133+
"Host: localhost\r\n" +
134+
"\r\n";
135+
OutputStream output = socket.getOutputStream();
136+
output.write(request.getBytes(StandardCharsets.UTF_8));
137+
output.flush();
138+
139+
HttpTester.Input input = HttpTester.from(socket.getInputStream());
140+
HttpTester.Response response = HttpTester.parseResponse(input);
141+
142+
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
143+
Assert.assertEquals("image/x-icon", response.get(HttpHeader.CONTENT_TYPE));
144+
}
145+
}
146+
147+
}

0 commit comments

Comments
 (0)