Fix #1254
This commit is contained in:
parent
df0f645aa4
commit
fd33ea26f3
|
@ -399,22 +399,11 @@ public class ResourceService
|
||||||
{
|
{
|
||||||
if (LOG.isDebugEnabled())
|
if (LOG.isDebugEnabled())
|
||||||
LOG.debug("welcome={}",welcome);
|
LOG.debug("welcome={}",welcome);
|
||||||
if (_redirectWelcome)
|
|
||||||
{
|
RequestDispatcher dispatcher=_redirectWelcome?null:request.getRequestDispatcher(welcome);
|
||||||
// Redirect to the index
|
|
||||||
response.setContentLength(0);
|
|
||||||
String q=request.getQueryString();
|
|
||||||
if (q!=null&&q.length()!=0)
|
|
||||||
response.sendRedirect(response.encodeRedirectURL(URIUtil.addPaths(request.getContextPath(),welcome)+"?"+q));
|
|
||||||
else
|
|
||||||
response.sendRedirect(response.encodeRedirectURL(URIUtil.addPaths(request.getContextPath(),welcome)));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Forward to the index
|
|
||||||
RequestDispatcher dispatcher=request.getRequestDispatcher(welcome);
|
|
||||||
if (dispatcher!=null)
|
if (dispatcher!=null)
|
||||||
{
|
{
|
||||||
|
// Forward to the index
|
||||||
if (included)
|
if (included)
|
||||||
dispatcher.include(request,response);
|
dispatcher.include(request,response);
|
||||||
else
|
else
|
||||||
|
@ -423,6 +412,15 @@ public class ResourceService
|
||||||
dispatcher.forward(request,response);
|
dispatcher.forward(request,response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Redirect to the index
|
||||||
|
response.setContentLength(0);
|
||||||
|
String q=request.getQueryString();
|
||||||
|
if (q!=null&&q.length()!=0)
|
||||||
|
response.sendRedirect(response.encodeRedirectURL(URIUtil.addPaths(request.getContextPath(),welcome)+"?"+q));
|
||||||
|
else
|
||||||
|
response.sendRedirect(response.encodeRedirectURL(URIUtil.addPaths(request.getContextPath(),welcome)));
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -394,6 +394,7 @@ public class ResourceHandler extends HandlerWrapper implements ResourceFactory,W
|
||||||
_resourceService.setCacheControl(new PreEncodedHttpField(HttpHeader.CACHE_CONTROL,cacheControl));
|
_resourceService.setCacheControl(new PreEncodedHttpField(HttpHeader.CACHE_CONTROL,cacheControl));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------ */
|
||||||
/**
|
/**
|
||||||
* @param dirAllowed
|
* @param dirAllowed
|
||||||
* If true, directory listings are returned if no welcome file is found. Else 403 Forbidden.
|
* If true, directory listings are returned if no welcome file is found. Else 403 Forbidden.
|
||||||
|
@ -486,6 +487,7 @@ public class ResourceHandler extends HandlerWrapper implements ResourceFactory,W
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------ */
|
||||||
/**
|
/**
|
||||||
* @param pathInfoOnly
|
* @param pathInfoOnly
|
||||||
* true, only the path info will be applied to the resourceBase
|
* true, only the path info will be applied to the resourceBase
|
||||||
|
@ -495,9 +497,12 @@ public class ResourceHandler extends HandlerWrapper implements ResourceFactory,W
|
||||||
_resourceService.setPathInfoOnly(pathInfoOnly);
|
_resourceService.setPathInfoOnly(pathInfoOnly);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------ */
|
||||||
/**
|
/**
|
||||||
* @param redirectWelcome
|
* @param redirectWelcome
|
||||||
* If true, welcome files are redirected rather than forwarded to.
|
* If true, welcome files are redirected rather than forwarded to.
|
||||||
|
* redirection is always used if the ResourceHandler is not scoped by
|
||||||
|
* a ContextHandler
|
||||||
*/
|
*/
|
||||||
public void setRedirectWelcome(boolean redirectWelcome)
|
public void setRedirectWelcome(boolean redirectWelcome)
|
||||||
{
|
{
|
||||||
|
|
|
@ -18,6 +18,14 @@
|
||||||
|
|
||||||
package org.eclipse.jetty.server.handler;
|
package org.eclipse.jetty.server.handler;
|
||||||
|
|
||||||
|
import static org.eclipse.jetty.http.HttpHeader.CONTENT_LENGTH;
|
||||||
|
import static org.eclipse.jetty.http.HttpHeader.CONTENT_TYPE;
|
||||||
|
import static org.eclipse.jetty.http.HttpHeader.LAST_MODIFIED;
|
||||||
|
import static org.eclipse.jetty.http.HttpHeader.LOCATION;
|
||||||
|
import static org.eclipse.jetty.http.HttpHeader.SERVER;
|
||||||
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
import static org.hamcrest.Matchers.endsWith;
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.startsWith;
|
import static org.hamcrest.Matchers.startsWith;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
@ -28,11 +36,11 @@ import java.io.FileOutputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.URI;
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.http.HttpTester;
|
||||||
import org.eclipse.jetty.server.Connector;
|
import org.eclipse.jetty.server.Connector;
|
||||||
import org.eclipse.jetty.server.HttpConfiguration;
|
import org.eclipse.jetty.server.HttpConfiguration;
|
||||||
import org.eclipse.jetty.server.HttpConnectionFactory;
|
import org.eclipse.jetty.server.HttpConnectionFactory;
|
||||||
|
@ -40,7 +48,6 @@ import org.eclipse.jetty.server.LocalConnector;
|
||||||
import org.eclipse.jetty.server.Server;
|
import org.eclipse.jetty.server.Server;
|
||||||
import org.eclipse.jetty.server.ServerConnector;
|
import org.eclipse.jetty.server.ServerConnector;
|
||||||
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
|
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
|
||||||
import org.eclipse.jetty.toolchain.test.SimpleRequest;
|
|
||||||
import org.eclipse.jetty.toolchain.test.annotation.Slow;
|
import org.eclipse.jetty.toolchain.test.annotation.Slow;
|
||||||
import org.eclipse.jetty.util.BufferUtil;
|
import org.eclipse.jetty.util.BufferUtil;
|
||||||
import org.eclipse.jetty.util.IO;
|
import org.eclipse.jetty.util.IO;
|
||||||
|
@ -123,6 +130,7 @@ public class ResourceHandlerTest
|
||||||
_resourceHandler = new ResourceHandler();
|
_resourceHandler = new ResourceHandler();
|
||||||
|
|
||||||
_resourceHandler.setResourceBase(MavenTestingUtils.getTargetFile("test-classes/simple").getAbsolutePath());
|
_resourceHandler.setResourceBase(MavenTestingUtils.getTargetFile("test-classes/simple").getAbsolutePath());
|
||||||
|
_resourceHandler.setWelcomeFiles(new String[]{"welcome.txt"});
|
||||||
|
|
||||||
_contextHandler = new ContextHandler("/resource");
|
_contextHandler = new ContextHandler("/resource");
|
||||||
_contextHandler.setHandler(_resourceHandler);
|
_contextHandler.setHandler(_resourceHandler);
|
||||||
|
@ -143,59 +151,75 @@ public class ResourceHandlerTest
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testJettyDirCss() throws Exception
|
public void testJettyDirRedirect() throws Exception
|
||||||
{
|
{
|
||||||
SimpleRequest sr = new SimpleRequest(new URI("http://localhost:" + _connector.getLocalPort()));
|
HttpTester.Response response = HttpTester.parseResponse(
|
||||||
Assert.assertNotNull(sr.getString("/resource/jetty-dir.css"));
|
_local.getResponse("GET /resource HTTP/1.0\r\n\r\n"));
|
||||||
|
assertThat(response.getStatus(),equalTo(302));
|
||||||
|
assertThat(response.get(LOCATION),containsString("/resource/"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSimple() throws Exception
|
public void testJettyDirListing() throws Exception
|
||||||
{
|
{
|
||||||
SimpleRequest sr = new SimpleRequest(new URI("http://localhost:" + _connector.getLocalPort()));
|
HttpTester.Response response = HttpTester.parseResponse(
|
||||||
Assert.assertEquals("simple text",sr.getString("/resource/simple.txt"));
|
_local.getResponse("GET /resource/ HTTP/1.0\r\n\r\n"));
|
||||||
|
assertThat(response.getStatus(),equalTo(200));
|
||||||
|
assertThat(response.getContent(),containsString("jetty-dir.css"));
|
||||||
|
assertThat(response.getContent(),containsString("<H1>Directory: /resource/"));
|
||||||
|
assertThat(response.getContent(),containsString("big.txt"));
|
||||||
|
assertThat(response.getContent(),containsString("bigger.txt"));
|
||||||
|
assertThat(response.getContent(),containsString("directory"));
|
||||||
|
assertThat(response.getContent(),containsString("simple.txt"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHeaders() throws Exception
|
public void testHeaders() throws Exception
|
||||||
{
|
{
|
||||||
String response = _local.getResponses("GET /resource/simple.txt HTTP/1.0\r\n\r\n");
|
HttpTester.Response response = HttpTester.parseResponse(
|
||||||
assertThat(response,startsWith("HTTP/1.1 200 OK"));
|
_local.getResponse("GET /resource/simple.txt HTTP/1.0\r\n\r\n"));
|
||||||
assertThat(response,Matchers.containsString("Content-Type: text/plain"));
|
assertThat(response.getStatus(),equalTo(200));
|
||||||
assertThat(response,Matchers.containsString("Last-Modified: "));
|
assertThat(response.get(CONTENT_TYPE),equalTo("text/plain"));
|
||||||
assertThat(response,Matchers.containsString("Content-Length: 11"));
|
assertThat(response.get(LAST_MODIFIED),Matchers.notNullValue());
|
||||||
assertThat(response,Matchers.containsString("Server: Jetty"));
|
assertThat(response.get(CONTENT_LENGTH),equalTo("11"));
|
||||||
assertThat(response,Matchers.containsString("simple text"));
|
assertThat(response.get(SERVER),containsString("Jetty"));
|
||||||
|
assertThat(response.getContent(),containsString("simple text"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBigFile() throws Exception
|
public void testBigFile() throws Exception
|
||||||
{
|
{
|
||||||
_config.setOutputBufferSize(2048);
|
_config.setOutputBufferSize(2048);
|
||||||
SimpleRequest sr = new SimpleRequest(new URI("http://localhost:" + _connector.getLocalPort()));
|
|
||||||
String response = sr.getString("/resource/big.txt");
|
HttpTester.Response response = HttpTester.parseResponse(
|
||||||
Assert.assertThat(response,Matchers.startsWith(" 1\tThis is a big file"));
|
_local.getResponse("GET /resource/big.txt HTTP/1.0\r\n\r\n"));
|
||||||
Assert.assertThat(response,Matchers.endsWith(" 400\tThis is a big file" + LN));
|
assertThat(response.getStatus(),equalTo(200));
|
||||||
|
assertThat(response.getContent(),startsWith(" 1\tThis is a big file"));
|
||||||
|
assertThat(response.getContent(),endsWith(" 400\tThis is a big file" + LN));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBigFileBigBuffer() throws Exception
|
public void testBigFileBigBuffer() throws Exception
|
||||||
{
|
{
|
||||||
_config.setOutputBufferSize(16 * 1024);
|
_config.setOutputBufferSize(16 * 1024);
|
||||||
SimpleRequest sr = new SimpleRequest(new URI("http://localhost:" + _connector.getLocalPort()));
|
|
||||||
String response = sr.getString("/resource/big.txt");
|
HttpTester.Response response = HttpTester.parseResponse(
|
||||||
Assert.assertThat(response,Matchers.startsWith(" 1\tThis is a big file"));
|
_local.getResponse("GET /resource/big.txt HTTP/1.0\r\n\r\n"));
|
||||||
Assert.assertThat(response,Matchers.endsWith(" 400\tThis is a big file" + LN));
|
assertThat(response.getStatus(),equalTo(200));
|
||||||
|
assertThat(response.getContent(),startsWith(" 1\tThis is a big file"));
|
||||||
|
assertThat(response.getContent(),endsWith(" 400\tThis is a big file" + LN));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBigFileLittleBuffer() throws Exception
|
public void testBigFileLittleBuffer() throws Exception
|
||||||
{
|
{
|
||||||
_config.setOutputBufferSize(8);
|
_config.setOutputBufferSize(8);
|
||||||
SimpleRequest sr = new SimpleRequest(new URI("http://localhost:" + _connector.getLocalPort()));
|
|
||||||
String response = sr.getString("/resource/big.txt");
|
HttpTester.Response response = HttpTester.parseResponse(
|
||||||
Assert.assertThat(response,Matchers.startsWith(" 1\tThis is a big file"));
|
_local.getResponse("GET /resource/big.txt HTTP/1.0\r\n\r\n"));
|
||||||
Assert.assertThat(response,Matchers.endsWith(" 400\tThis is a big file" + LN));
|
assertThat(response.getStatus(),equalTo(200));
|
||||||
|
assertThat(response.getContent(),startsWith(" 1\tThis is a big file"));
|
||||||
|
assertThat(response.getContent(),endsWith(" 400\tThis is a big file" + LN));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -212,6 +236,32 @@ public class ResourceHandlerTest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWelcome() throws Exception
|
||||||
|
{
|
||||||
|
HttpTester.Response response = HttpTester.parseResponse(
|
||||||
|
_local.getResponse("GET /resource/directory/ HTTP/1.0\r\n\r\n"));
|
||||||
|
assertThat(response.getStatus(),equalTo(200));
|
||||||
|
assertThat(response.getContent(),containsString("Hello"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWelcomeRedirect() throws Exception
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_resourceHandler.setRedirectWelcome(true);
|
||||||
|
HttpTester.Response response = HttpTester.parseResponse(
|
||||||
|
_local.getResponse("GET /resource/directory/ HTTP/1.0\r\n\r\n"));
|
||||||
|
assertThat(response.getStatus(),equalTo(302));
|
||||||
|
assertThat(response.get(LOCATION),containsString("/resource/welcome.txt"));
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_resourceHandler.setRedirectWelcome(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Slow
|
@Slow
|
||||||
public void testSlowBiggest() throws Exception
|
public void testSlowBiggest() throws Exception
|
||||||
|
@ -235,12 +285,10 @@ public class ResourceHandlerTest
|
||||||
|
|
||||||
try (Socket socket = new Socket("localhost",_connector.getLocalPort());OutputStream out=socket.getOutputStream();InputStream in=socket.getInputStream())
|
try (Socket socket = new Socket("localhost",_connector.getLocalPort());OutputStream out=socket.getOutputStream();InputStream in=socket.getInputStream())
|
||||||
{
|
{
|
||||||
|
|
||||||
socket.getOutputStream().write("GET /resource/biggest.txt HTTP/1.0\n\n".getBytes());
|
socket.getOutputStream().write("GET /resource/biggest.txt HTTP/1.0\n\n".getBytes());
|
||||||
|
|
||||||
byte[] array = new byte[102400];
|
byte[] array = new byte[102400];
|
||||||
ByteBuffer buffer=null;
|
ByteBuffer buffer=null;
|
||||||
int i=0;
|
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
content
|
|
@ -0,0 +1 @@
|
||||||
|
Hello
|
Loading…
Reference in New Issue