use of io.undertow.server.handlers.CanonicalPathHandler in project undertow by undertow-io.
the class FileHandlerSymlinksTestCase method testExplicitAccessSymlinkGrantedUsingSpecificFilters.
@Test
public void testExplicitAccessSymlinkGrantedUsingSpecificFilters() throws IOException, URISyntaxException {
TestHttpClient client = new TestHttpClient();
Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();
Path newSymlink = rootPath.resolve("newSymlink");
try {
DefaultServer.setRootHandler(new CanonicalPathHandler().setNext(new PathHandler().addPrefixPath("/path", new ResourceHandler(new PathResourceManager(newSymlink, 10485760, true, rootPath.toAbsolutePath().toString().concat("/newDir"))).setDirectoryListingEnabled(false).addWelcomeFiles("page.html"))));
/**
* This request should return a 200 code as rootPath + "/newDir" is used in the safePaths
*/
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/innerSymlink/page.html");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
final String response = HttpClientUtils.readResponse(result);
Header[] headers = result.getHeaders("Content-Type");
Assert.assertEquals("text/html", headers[0].getValue());
Assert.assertTrue(response, response.contains("A web page"));
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.handlers.CanonicalPathHandler in project undertow by undertow-io.
the class FileHandlerTestCase method testFileTransferLargeFile.
@Test
public void testFileTransferLargeFile() throws IOException, URISyntaxException {
TestHttpClient client = new TestHttpClient();
Path tmp = Paths.get(System.getProperty("java.io.tmpdir"));
StringBuilder message = new StringBuilder();
for (int i = 0; i < 100000; ++i) {
message.append("Hello World");
}
Path large = Files.createTempFile(null, ".txt");
try {
Files.copy(new ByteArrayInputStream(message.toString().getBytes(StandardCharsets.UTF_8)), large, StandardCopyOption.REPLACE_EXISTING);
DefaultServer.setRootHandler(new CanonicalPathHandler().setNext(new PathHandler().addPrefixPath("/path", new ResourceHandler(new PathResourceManager(tmp, 1)).setDirectoryListingEnabled(true))));
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/" + large.getFileName().toString());
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
final String response = HttpClientUtils.readResponse(result);
Header[] headers = result.getHeaders("Content-Type");
Assert.assertEquals("text/plain", headers[0].getValue());
Assert.assertTrue(response, response.equals(message.toString()));
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.handlers.CanonicalPathHandler in project undertow by undertow-io.
the class FileHandlerTestCase method testHeadRequest.
@Test
public void testHeadRequest() throws IOException, URISyntaxException {
TestHttpClient client = new TestHttpClient();
Path file = Paths.get(getClass().getResource("page.html").toURI());
Path rootPath = file.getParent();
try {
DefaultServer.setRootHandler(new CanonicalPathHandler().setNext(new PathHandler().addPrefixPath("/path", new ResourceHandler(new PathResourceManager(rootPath, 10485760)).setDirectoryListingEnabled(true))));
HttpHead get = new HttpHead(DefaultServer.getDefaultServerURL() + "/path/page.html");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals(Long.toString(Files.size(file)), result.getHeaders(Headers.CONTENT_LENGTH_STRING)[0].getValue());
Header[] headers = result.getHeaders("Content-Type");
Assert.assertEquals("text/html", headers[0].getValue());
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.handlers.CanonicalPathHandler in project undertow by undertow-io.
the class FileHandlerTestCase method testFileTransfer.
@Test
public void testFileTransfer() throws IOException, URISyntaxException {
TestHttpClient client = new TestHttpClient();
Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();
try {
DefaultServer.setRootHandler(new CanonicalPathHandler().setNext(new PathHandler().addPrefixPath("/path", new ResourceHandler(new PathResourceManager(rootPath, 1)).setDirectoryListingEnabled(true))));
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/page.html");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
final String response = HttpClientUtils.readResponse(result);
Header[] headers = result.getHeaders("Content-Type");
Assert.assertEquals("text/html", headers[0].getValue());
Assert.assertTrue(response, response.contains("A web page"));
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.handlers.CanonicalPathHandler in project wildfly by wildfly.
the class Server method start.
@Override
public void start(StartContext startContext) throws StartException {
root = virtualHostHandler;
root = new SimpleErrorPageHandler(root);
root = new CanonicalPathHandler(root);
root = new DefaultHostHandler(root);
UndertowLogger.ROOT_LOGGER.startedServer(name);
undertowService.getValue().registerServer(this);
}
Aggregations