Remove Generic Request/Response #8291

Handle redirection of encoded directory URIs
This commit is contained in:
Greg Wilkins 2022-07-13 14:48:01 +10:00
parent 821d5c8d36
commit 13aa9b0bc8
4 changed files with 39 additions and 5 deletions

View File

@ -443,7 +443,7 @@ public class ResourceService
response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, 0);
// TODO need helper code to edit URIs
String uri = URIUtil.encodePath(URIUtil.addPaths(request.getContextPath(), welcome));
String uri = URIUtil.addPaths(request.getContextPath(), welcome);
String q = request.getHttpURI().getQuery();
if (q != null && !q.isEmpty())
uri += "?" + q;

View File

@ -525,6 +525,39 @@ public class ResourceHandlerTest
assertThat(response.getContent(), containsString("Hello"));
}
@Test
public void testWelcomeDirWithQuestion() throws Exception
{
Path dir = TEST_PATH.resolve("dir?");
Files.createDirectories(dir);
Path welcome = dir.resolve("welcome.txt");
try (FileOutputStream out = new FileOutputStream(welcome.toFile()))
{
out.write("Hello".getBytes());
}
welcome.toFile().deleteOnExit();
HttpTester.Response response = HttpTester.parseResponse(
_local.getResponse("GET /resource/dir? HTTP/1.0\r\n\r\n"));
assertThat(response.getStatus(), is(HttpStatus.NOT_FOUND_404));
response = HttpTester.parseResponse(
_local.getResponse("GET /resource/dir%3F HTTP/1.0\r\n\r\n"));
assertThat(response.getStatus(), is(HttpStatus.FOUND_302));
assertThat(response.getField(LOCATION).getValue(), endsWith("/resource/dir%3F/"));
response = HttpTester.parseResponse(
_local.getResponse("GET /resource/dir%3F/ HTTP/1.0\r\n\r\n"));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), containsString("Hello"));
_resourceHandler.setRedirectWelcome(true);
response = HttpTester.parseResponse(
_local.getResponse("GET /resource/dir%3F/ HTTP/1.0\r\n\r\n"));
assertThat(response.getStatus(), is(HttpStatus.FOUND_302));
assertThat(response.getField(LOCATION).getValue(), endsWith("/resource/dir%3F/welcome.txt"));
}
@Test
public void testWelcomeRedirect() throws Exception
{

View File

@ -711,7 +711,7 @@ public class DefaultServlet extends HttpServlet implements ResourceService.Welco
// Redirect to the index
response.setContentLength(0);
String uri = URIUtil.encodePath(URIUtil.addPaths(request.getContextPath(), welcome));
String uri = URIUtil.addPaths(request.getContextPath(), welcome);
String q = request.getQueryString();
if (q != null && !q.isEmpty())
uri += "?" + q;

View File

@ -880,7 +880,6 @@ public class DefaultServletTest
}
@Test
@Disabled
public void testWelcomeRedirect() throws Exception
{
Path dir = docRoot.resolve("dir");
@ -942,7 +941,6 @@ public class DefaultServletTest
}
@Test
@Disabled
public void testRelativeRedirect() throws Exception
{
Path dir = docRoot.resolve("dir");
@ -986,7 +984,6 @@ public class DefaultServletTest
* Ensure that oddball directory names are served with proper escaping
*/
@Test
@Disabled
public void testWelcomeRedirectDirWithQuestion() throws Exception
{
FS.ensureDirExists(docRoot);
@ -1004,6 +1001,10 @@ public class DefaultServletTest
String rawResponse;
HttpTester.Response response;
rawResponse = connector.getResponse("GET /context/dir%3F/index.html HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200));
rawResponse = connector.getResponse("GET /context/dir%3F HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.MOVED_TEMPORARILY_302));