use of io.undertow.server.handlers.CanonicalPathHandler in project undertow by undertow-io.
the class FileHandlerTestCase method testRangeRequests.
@Test
public void testRangeRequests() 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");
get.addHeader("range", "bytes=2-3");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
String response = HttpClientUtils.readResponse(result);
Header[] headers = result.getHeaders("Content-Type");
Assert.assertEquals("text/html", headers[0].getValue());
Assert.assertEquals("--", response);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/page.html");
get.addHeader("range", "bytes=-7");
result = client.execute(get);
Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
headers = result.getHeaders("Content-Type");
Assert.assertEquals("text/html", headers[0].getValue());
Assert.assertEquals("</html>", response);
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.handlers.CanonicalPathHandler in project undertow by undertow-io.
the class FileHandlerIndexTestCase method testWelcomeFile.
@Test
public void testWelcomeFile() 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, 10485760)).setDirectoryListingEnabled(true).addWelcomeFiles("page.html"))));
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
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 FileHandlerStressTestCase method simpleFileStressTest.
@Test
public void simpleFileStressTest() throws IOException, ExecutionException, InterruptedException, URISyntaxException {
ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
try {
Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();
final ResourceHandler handler = new ResourceHandler(new PathResourceManager(rootPath, 10485760));
final CacheHandler cacheHandler = new CacheHandler(new DirectBufferCache(1024, 10, 10480), handler);
final PathHandler path = new PathHandler();
path.addPrefixPath("/path", cacheHandler);
final CanonicalPathHandler root = new CanonicalPathHandler();
root.setNext(path);
DefaultServer.setRootHandler(root);
final List<Future<?>> futures = new ArrayList<>();
for (int i = 0; i < NUM_THREADS; ++i) {
futures.add(executor.submit(new Runnable() {
@Override
public void run() {
TestHttpClient client = new TestHttpClient();
try {
for (int i = 0; i < NUM_REQUESTS; ++i) {
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);
Assert.assertTrue(response, response.contains("A web page"));
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
client.getConnectionManager().shutdown();
}
}
}));
}
for (Future<?> future : futures) {
future.get();
}
} finally {
executor.shutdown();
}
}
use of io.undertow.server.handlers.CanonicalPathHandler in project undertow by undertow-io.
the class FileHandlerSymlinksTestCase method testExplicitAccessSymlinkDeniedForInsideSymlinks.
@Test
public void testExplicitAccessSymlinkDeniedForInsideSymlinks() throws IOException, URISyntaxException {
TestHttpClient client = new TestHttpClient();
Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();
Path newSymlink = rootPath.resolve("newDir");
try {
DefaultServer.setRootHandler(new CanonicalPathHandler().setNext(new PathHandler().addPrefixPath("/path", new ResourceHandler(new PathResourceManager(newSymlink, 10485760, true, "")).setDirectoryListingEnabled(false).addWelcomeFiles("page.html"))));
/**
* This request should return a 200 code as not symbolic links on path
*/
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/innerDir/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"));
/**
* This request should return a 404 error, followLinks is true, but empty "" safePaths forbids all symbolics paths
*/
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/innerSymlink/page.html");
result = client.execute(get);
Assert.assertEquals(StatusCodes.NOT_FOUND, result.getStatusLine().getStatusCode());
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.handlers.CanonicalPathHandler in project undertow by undertow-io.
the class FileHandlerSymlinksTestCase method testExplicitAccessSymlinkDeniedUsingSameSymlinkName.
@Test
public void testExplicitAccessSymlinkDeniedUsingSameSymlinkName() 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("/innerSymlink"))).setDirectoryListingEnabled(false).addWelcomeFiles("page.html"))));
/**
* This request should return a 404 code as rootPath + "/innerSymlink" in safePaths will not match with canonical "/innerSymlink"
*/
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/innerSymlink/page.html");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.NOT_FOUND, result.getStatusLine().getStatusCode());
} finally {
client.getConnectionManager().shutdown();
}
}
Aggregations