Search in sources :

Example 11 with ResourceHandler

use of io.undertow.server.handlers.resource.ResourceHandler 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();
    }
}
Also used : Path(java.nio.file.Path) Header(org.apache.http.Header) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) HttpGet(org.apache.http.client.methods.HttpGet) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) PathHandler(io.undertow.server.handlers.PathHandler) HttpResponse(org.apache.http.HttpResponse) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 12 with ResourceHandler

use of io.undertow.server.handlers.resource.ResourceHandler in project undertow by undertow-io.

the class RangeRequestTestCase method setup.

@BeforeClass
public static void setup() throws URISyntaxException {
    Path rootPath = Paths.get(RangeRequestTestCase.class.getResource("range.txt").toURI()).getParent();
    PathHandler path = Handlers.path();
    path.addPrefixPath("/path", new ByteRangeHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseHeaders().put(Headers.LAST_MODIFIED, DateUtils.toDateString(new Date(10000)));
            exchange.getResponseHeaders().put(Headers.ETAG, "\"someetag\"");
            exchange.getResponseSender().send("0123456789");
        }
    }, true));
    path.addPrefixPath("/resource", new ResourceHandler(new PathResourceManager(rootPath, 10485760)).setDirectoryListingEnabled(true));
    path.addPrefixPath("/cachedresource", new ResourceHandler(new CachingResourceManager(1000, 1000000, new DirectBufferCache(1000, 10, 10000), new PathResourceManager(rootPath, 10485760), -1)).setDirectoryListingEnabled(true));
    DefaultServer.setRootHandler(path);
}
Also used : Path(java.nio.file.Path) HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) DirectBufferCache(io.undertow.server.handlers.cache.DirectBufferCache) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) CachingResourceManager(io.undertow.server.handlers.resource.CachingResourceManager) Date(java.util.Date) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) BeforeClass(org.junit.BeforeClass)

Example 13 with ResourceHandler

use of io.undertow.server.handlers.resource.ResourceHandler in project undertow by undertow-io.

the class ContentEncodedResourceTestCase method setup.

@BeforeClass
public static void setup() throws IOException {
    tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), DIR_NAME);
    final PathResourceManager resourceManager = new PathResourceManager(tmpDir, 10485760);
    DefaultServer.setRootHandler(new ResourceHandler(resourceManager).setContentEncodedResourceManager(new ContentEncodedResourceManager(tmpDir, new CachingResourceManager(100, 10000, null, resourceManager, -1), new ContentEncodingRepository().addEncodingHandler("deflate", new DeflateEncodingProvider(), 50, null), 0, 100000, null)));
}
Also used : ContentEncodedResourceManager(io.undertow.server.handlers.encoding.ContentEncodedResourceManager) DeflateEncodingProvider(io.undertow.server.handlers.encoding.DeflateEncodingProvider) ContentEncodingRepository(io.undertow.server.handlers.encoding.ContentEncodingRepository) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) CachingResourceManager(io.undertow.server.handlers.resource.CachingResourceManager) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) BeforeClass(org.junit.BeforeClass)

Example 14 with ResourceHandler

use of io.undertow.server.handlers.resource.ResourceHandler 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();
    }
}
Also used : Path(java.nio.file.Path) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) HttpGet(org.apache.http.client.methods.HttpGet) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) PathHandler(io.undertow.server.handlers.PathHandler) HttpResponse(org.apache.http.HttpResponse) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 15 with ResourceHandler

use of io.undertow.server.handlers.resource.ResourceHandler in project undertow by undertow-io.

the class FileHandlerSymlinksTestCase method testExplicitAccessSymlinkGranted.

@Test
public void testExplicitAccessSymlinkGranted() 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, "/")).setDirectoryListingEnabled(false).addWelcomeFiles("page.html"))));
        /**
             * This request should return a 200 code as "/" can be used to grant all symbolic links paths
             */
        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();
    }
}
Also used : Path(java.nio.file.Path) Header(org.apache.http.Header) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) HttpGet(org.apache.http.client.methods.HttpGet) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) PathHandler(io.undertow.server.handlers.PathHandler) HttpResponse(org.apache.http.HttpResponse) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Aggregations

ResourceHandler (io.undertow.server.handlers.resource.ResourceHandler)24 PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)23 Path (java.nio.file.Path)22 CanonicalPathHandler (io.undertow.server.handlers.CanonicalPathHandler)21 PathHandler (io.undertow.server.handlers.PathHandler)21 TestHttpClient (io.undertow.testutils.TestHttpClient)20 HttpResponse (org.apache.http.HttpResponse)20 Test (org.junit.Test)20 HttpGet (org.apache.http.client.methods.HttpGet)19 Header (org.apache.http.Header)12 HttpHandler (io.undertow.server.HttpHandler)3 DirectBufferCache (io.undertow.server.handlers.cache.DirectBufferCache)2 CachingResourceManager (io.undertow.server.handlers.resource.CachingResourceManager)2 IOException (java.io.IOException)2 BeforeClass (org.junit.BeforeClass)2 Undertow (io.undertow.Undertow)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 NameVirtualHostHandler (io.undertow.server.handlers.NameVirtualHostHandler)1 CacheHandler (io.undertow.server.handlers.cache.CacheHandler)1 ContentEncodedResourceManager (io.undertow.server.handlers.encoding.ContentEncodedResourceManager)1