use of io.undertow.server.handlers.resource.PathResourceManager in project undertow by undertow-io.
the class FileServer method main.
public static void main(final String[] args) {
Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(resource(new PathResourceManager(Paths.get(System.getProperty("user.home")), 100)).setDirectoryListingEnabled(true)).build();
server.start();
}
use of io.undertow.server.handlers.resource.PathResourceManager in project undertow by undertow-io.
the class FileHandlerSymlinksTestCase method testRelativePathSymlinkFilter.
@Test
public void testRelativePathSymlinkFilter() 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, "innerDir")).setDirectoryListingEnabled(false).addWelcomeFiles("page.html"))));
/**
* This request should return a 200, innerSymlink is a symlink pointed to innerDir
*/
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/innerSymlink/page.html");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.handlers.resource.PathResourceManager in project undertow by undertow-io.
the class FileHandlerSymlinksTestCase method testDefaultAccessSymlinkDenied.
@Test
public void testDefaultAccessSymlinkDenied() 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)).setDirectoryListingEnabled(false).addWelcomeFiles("page.html"))));
/**
* This request should return a 404 error, as path contains a symbolic link and by default followLinks is false
*/
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/innerSymlink/");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.NOT_FOUND, result.getStatusLine().getStatusCode());
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.handlers.resource.PathResourceManager 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.resource.PathResourceManager in project undertow by undertow-io.
the class PathResourceManagerTestCase method testBaseDirInSymlink.
@Test
public void testBaseDirInSymlink() throws Exception {
Assume.assumeFalse(System.getProperty("os.name").toLowerCase().contains("windows"));
Path filePath = Paths.get(getClass().getResource("page.html").toURI());
Path rootPath = filePath.getParent();
Path newDir = rootPath.resolve("newDir");
Path innerPage = newDir.resolve("page.html");
Path newSymlink = rootPath.resolve("newSymlink");
try {
Files.createDirectories(newDir);
Files.copy(filePath, innerPage);
Files.createSymbolicLink(newSymlink, newDir);
Assert.assertTrue("Ensure that newSymlink is still a symlink as expected", Files.isSymbolicLink(newSymlink));
final PathResourceManager resourceManager = new PathResourceManager(newSymlink, 1024 * 1024);
Assert.assertNotNull(resourceManager.getResource("page.html"));
Assert.assertNull(resourceManager.getResource("Page.html"));
Assert.assertNotNull(resourceManager.getResource("./page.html"));
} finally {
Files.deleteIfExists(newSymlink);
Files.deleteIfExists(innerPage);
Files.deleteIfExists(newDir);
Files.deleteIfExists(newDir);
}
}
Aggregations