Merge branch `jetty-9.2.x` into `jetty-9.3.x`

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>

# Conflicts:
#	jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java
#	jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientURITest.java
#	jetty-fcgi/fcgi-server/src/test/java/org/eclipse/jetty/fcgi/server/AbstractHttpClientServerTest.java
#	jetty-fcgi/fcgi-server/src/test/java/org/eclipse/jetty/fcgi/server/HttpClientTest.java
#	jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerTest.java
#	jetty-server/src/main/java/org/eclipse/jetty/server/handler/DefaultHandler.java
#	jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DefaultServletTest.java
#	jetty-spdy/spdy-http-client-transport/src/test/java/org/eclipse/jetty/spdy/client/http/HttpClientTest.java
#	jetty-util/src/main/java/org/eclipse/jetty/util/resource/Resource.java
#	pom.xml
This commit is contained in:
Joakim Erdfelt 2019-04-17 16:47:15 -05:00
commit 3ab1ea3181
6 changed files with 173 additions and 43 deletions

View File

@ -704,6 +704,7 @@ public class HttpClientTest extends AbstractHttpClientServerTest
public void testSendToIPv6Address() throws Exception
{
Assume.assumeTrue(Net.isIpv6InterfaceAvailable());
start(new EmptyServerHandler());
ContentResponse response = client.newRequest("[::1]", connector.getLocalPort())

View File

@ -58,7 +58,7 @@ public class HttpClientURITest extends AbstractHttpClientServerTest
{
@Rule
public ExpectedException expectedException = ExpectedException.none();
public HttpClientURITest(SslContextFactory sslContextFactory)
{
super(sslContextFactory);

View File

@ -506,6 +506,7 @@ public class HttpClientTest extends AbstractHttpClientServerTest
public void testSendToIPv6Address() throws Exception
{
Assume.assumeTrue(Net.isIpv6InterfaceAvailable());
start(new EmptyServerHandler());
ContentResponse response = client.newRequest("[::1]", connector.getLocalPort())

View File

@ -89,6 +89,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest
public void testCONNECTwithIPv6() throws Exception
{
Assume.assumeTrue(Net.isIpv6InterfaceAvailable());
String hostPort = "[::1]:" + serverConnector.getLocalPort();
String request = "" +
"CONNECT " + hostPort + " HTTP/1.1\r\n" +

View File

@ -18,10 +18,11 @@
package org.eclipse.jetty.server.handler;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -32,12 +33,15 @@ import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.ByteArrayISO8859Writer;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource;
import static java.nio.charset.StandardCharsets.UTF_8;
/* ------------------------------------------------------------ */
/** Default Handler.
@ -106,7 +110,6 @@ public class DefaultHandler extends AbstractHandler
return;
}
if (!_showContexts || !HttpMethod.GET.is(method) || !request.getRequestURI().equals("/"))
{
response.sendError(HttpServletResponse.SC_NOT_FOUND);
@ -114,64 +117,87 @@ public class DefaultHandler extends AbstractHandler
}
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
response.setContentType(MimeTypes.Type.TEXT_HTML.toString());
response.setContentType(MimeTypes.Type.TEXT_HTML_UTF_8.toString());
try (ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500);)
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(outputStream, UTF_8))
{
writer.write("<HTML>\n<HEAD>\n<TITLE>Error 404 - Not Found");
writer.write("</TITLE>\n<BODY>\n<H2>Error 404 - Not Found.</H2>\n");
writer.write("No context on this server matched or handled this request.<BR>");
writer.write("Contexts known to this server are: <ul>");
writer.append("<!DOCTYPE html>\n");
writer.append("<html lang=\"en\">\n<head>\n");
writer.append("<title>Error 404 - Not Found</title>\n");
writer.append("<meta charset=\"utf-8\">\n");
writer.append("<style>body { font-family: sans-serif; } table, td { border: 1px solid #333; } td, th { padding: 5px; } thead, tfoot { background-color: #333; color: #fff; } </style>\n");
writer.append("</head>\n<body>\n");
writer.append("<h2>Error 404 - Not Found.</h2>\n");
writer.append("<p>No context on this server matched or handled this request.</p>\n");
writer.append("<p>Contexts known to this server are:</p>\n");
Server server = getServer();
Handler[] handlers = server==null?null:server.getChildHandlersByClass(ContextHandler.class);
writer.append("<table class=\"contexts\"><thead><tr>");
writer.append("<th>Context Path</th>");
writer.append("<th>Display Name</th>");
writer.append("<th>Status</th>");
writer.append("<th>LifeCycle</th>");
writer.append("</tr></thead><tbody>\n");
for (int i=0;handlers!=null && i<handlers.length;i++)
{
writer.append("<tr><td>");
// Context Path
ContextHandler context = (ContextHandler)handlers[i];
String contextPath = context.getContextPath();
String href = URIUtil.encodePath(contextPath);
if (contextPath.length() > 1 && !contextPath.endsWith("/"))
{
href += '/';
}
if (context.isRunning())
{
writer.write("<li><a href=\"");
if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0)
writer.write(request.getScheme()+"://"+context.getVirtualHosts()[0]+":"+request.getLocalPort());
writer.write(context.getContextPath());
if (context.getContextPath().length()>1 && context.getContextPath().endsWith("/"))
writer.write("/");
writer.write("\">");
writer.write(context.getContextPath());
if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0)
writer.write("&nbsp;@&nbsp;"+context.getVirtualHosts()[0]+":"+request.getLocalPort());
writer.write("&nbsp;--->&nbsp;");
writer.write(context.toString());
writer.write("</a></li>\n");
writer.append("<a href=\"").append(href).append("\">");
}
writer.append(contextPath.replaceAll("%", "&#37;"));
if (context.isRunning())
{
writer.append("</a>");
}
writer.append("</td><td>");
// Display Name
if (StringUtil.isNotBlank(context.getDisplayName()))
{
writer.append(StringUtil.sanitizeXmlString(context.getDisplayName()));
}
writer.append("&nbsp;</td><td>");
// Available
if (context.isAvailable())
{
writer.append("Available");
}
else
{
writer.write("<li>");
writer.write(context.getContextPath());
if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0)
writer.write("&nbsp;@&nbsp;"+context.getVirtualHosts()[0]+":"+request.getLocalPort());
writer.write("&nbsp;--->&nbsp;");
writer.write(context.toString());
if (context.isFailed())
writer.write(" [failed]");
if (context.isStopped())
writer.write(" [stopped]");
writer.write("</li>\n");
writer.append("<em>Not</em> Available");
}
writer.append("</td><td>");
// State
writer.append(context.getState());
writer.append("</td></tr>\n");
}
writer.write("</ul><hr>");
baseRequest.getHttpChannel().getHttpConfiguration()
.writePoweredBy(writer,"<a href=\"http://eclipse.org/jetty\"><img border=0 src=\"/favicon.ico\"/></a>&nbsp;","<hr/>\n");
writer.write("\n</BODY>\n</HTML>\n");
writer.append("</tbody></table><hr/>\n");
writer.append("<a href=\"http://eclipse.org/jetty\"><img alt=\"icon\" src=\"/favicon.ico\"/></a>&nbsp;");
writer.append("<a href=\"http://eclipse.org/jetty\">Powered by Eclipse Jetty:// Server</a><hr/>\n");
writer.append("</body>\n</html>\n");
writer.flush();
response.setContentLength(writer.size());
try (OutputStream out=response.getOutputStream())
byte content[] = outputStream.toByteArray();
response.setContentLength(content.length);
try (OutputStream out = response.getOutputStream())
{
writer.writeTo(out);
out.write(content);
}
}
}

View File

@ -0,0 +1,101 @@
//
// ========================================================================
// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.servlet;
import java.io.File;
import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.resource.PathResource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
public class DefaultHandlerTest
{
private Server server;
private LocalConnector localConnector;
private File baseA;
private File baseFoo;
@Before
public void startServer() throws Exception
{
server = new Server();
localConnector = new LocalConnector(server);
server.addConnector(localConnector);
File docRoot = MavenTestingUtils.getTargetTestingDir(DefaultHandlerTest.class.getName());
FS.ensureDirExists(docRoot);
baseA = new File(docRoot, "baseA");
FS.ensureDirExists(baseA);
baseFoo = new File(docRoot, "baseFoo");
FS.ensureDirExists(baseFoo);
ServletContextHandler contextA = new ServletContextHandler();
contextA.setContextPath("/a");
contextA.setBaseResource(new PathResource(baseA));
ServletContextHandler contextFoo = new ServletContextHandler();
contextFoo.setContextPath("/foo");
contextFoo.setBaseResource(new PathResource(baseFoo));
HandlerList handlers = new HandlerList();
handlers.addHandler(contextA);
handlers.addHandler(contextFoo);
handlers.addHandler(new DefaultHandler());
server.setHandler(handlers);
server.start();
}
@After
public void stopServer() throws Exception
{
server.stop();
}
@Test
public void testNotRevealBaseResource() throws Exception
{
StringBuilder req = new StringBuilder();
req.append("GET / HTTP/1.0\r\n");
req.append("\r\n");
String rawResponse = localConnector.getResponses(req.toString());
assertThat(rawResponse, containsString("404 Not Found"));
assertThat(rawResponse, containsString("No context on this server matched or handled this request"));
assertThat(rawResponse, containsString("Contexts known to this server are"));
assertThat(rawResponse, containsString("<a href=\"/a/\">"));
assertThat(rawResponse, containsString("<a href=\"/foo/\">"));
assertThat(rawResponse, not(containsString(baseA.toString())));
assertThat(rawResponse, not(containsString(baseFoo.toString())));
}
}