Search in sources :

Example 1 with ResourceHandler

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

the class FileHandler method createHandler.

@Override
public HttpHandler createHandler(final OperationContext context, ModelNode model) throws OperationFailedException {
    final String path = PATH.resolveModelAttribute(context, model).asString();
    final boolean directoryListing = DIRECTORY_LISTING.resolveModelAttribute(context, model).asBoolean();
    final boolean followSymlink = FOLLOW_SYMLINK.resolveModelAttribute(context, model).asBoolean();
    final boolean caseSensitive = CASE_SENSITIVE.resolveModelAttribute(context, model).asBoolean();
    final int cacheBufferSize = CACHE_BUFFER_SIZE.resolveModelAttribute(context, model).asInt();
    final int cacheBuffers = CACHE_BUFFERS.resolveModelAttribute(context, model).asInt();
    final List<String> safePaths = SAFE_SYMLINK_PATHS.unwrap(context, model);
    final String[] paths = safePaths.toArray(new String[safePaths.size()]);
    UndertowLogger.ROOT_LOGGER.creatingFileHandler(path, directoryListing, followSymlink, caseSensitive, safePaths);
    File base = null;
    try {
        base = new File(path).getCanonicalFile();
    } catch (IOException e) {
        throw new OperationFailedException(e);
    }
    FileResourceManager resourceManager = new FileResourceManager(base, cacheBufferSize * cacheBuffers, caseSensitive, followSymlink, paths);
    ResourceHandler handler = new ResourceHandler(resourceManager);
    handler.setDirectoryListingEnabled(directoryListing);
    return handler;
}
Also used : FileResourceManager(io.undertow.server.handlers.resource.FileResourceManager) OperationFailedException(org.jboss.as.controller.OperationFailedException) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) IOException(java.io.IOException) File(java.io.File)

Example 2 with ResourceHandler

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

the class ComplexSSLTestCase method complexSSLTestCase.

@Test
public void complexSSLTestCase() throws IOException, GeneralSecurityException, URISyntaxException, InterruptedException {
    final PathHandler pathHandler = new PathHandler();
    Path rootPath = Paths.get(FileHandlerTestCase.class.getResource("page.html").toURI()).getParent();
    final NameVirtualHostHandler virtualHostHandler = new NameVirtualHostHandler();
    HttpHandler root = virtualHostHandler;
    root = new SimpleErrorPageHandler(root);
    root = new CanonicalPathHandler(root);
    virtualHostHandler.addHost("default-host", pathHandler);
    virtualHostHandler.setDefaultHandler(pathHandler);
    pathHandler.addPrefixPath("/", new ResourceHandler(new PathResourceManager(rootPath, 10485760)).setDirectoryListingEnabled(true));
    DefaultServer.setRootHandler(root);
    DefaultServer.startSSLServer();
    TestHttpClient client = new TestHttpClient();
    client.setSSLContext(DefaultServer.getClientSSLContext());
    try {
        //get file list, this works
        HttpGet getFileList = new HttpGet(DefaultServer.getDefaultServerSSLAddress());
        HttpResponse resultList = client.execute(getFileList);
        Assert.assertEquals(StatusCodes.OK, resultList.getStatusLine().getStatusCode());
        String responseList = HttpClientUtils.readResponse(resultList);
        Assert.assertTrue(responseList, responseList.contains("page.html"));
        Header[] headersList = resultList.getHeaders("Content-Type");
        Assert.assertEquals("text/html; charset=UTF-8", headersList[0].getValue());
        //get file itself, breaks
        HttpGet getFile = new HttpGet(DefaultServer.getDefaultServerSSLAddress() + "/page.html");
        HttpResponse result = client.execute(getFile);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        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();
        DefaultServer.stopSSLServer();
    }
}
Also used : Path(java.nio.file.Path) HttpHandler(io.undertow.server.HttpHandler) 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) FileHandlerTestCase(io.undertow.server.handlers.file.FileHandlerTestCase) HttpResponse(org.apache.http.HttpResponse) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) NameVirtualHostHandler(io.undertow.server.handlers.NameVirtualHostHandler) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) TestHttpClient(io.undertow.testutils.TestHttpClient) Header(org.apache.http.Header) SimpleErrorPageHandler(io.undertow.server.handlers.error.SimpleErrorPageHandler) Test(org.junit.Test)

Example 3 with ResourceHandler

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

the class FileHandlerSymlinksTestCase method testExplicitAccessSymlinkDeniedForEmptySafePath.

@Test
public void testExplicitAccessSymlinkDeniedForEmptySafePath() 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 404 error, followLinks is true, but empty "" safePaths forbids all symbolics paths inside base path
             */
        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();
    }
}
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 4 with ResourceHandler

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

the class FileHandlerTestCase method main.

/*
    Starts simple file server, it is useful for testing directory browsing
     */
public static void main(String[] args) throws URISyntaxException {
    Path rootPath = Paths.get(FileHandlerTestCase.class.getResource("page.html").toURI()).getParent().getParent();
    HttpHandler root = new CanonicalPathHandler().setNext(new PathHandler().addPrefixPath("/path", new ResourceHandler(new PathResourceManager(rootPath, 1)).setDirectoryListingEnabled(true)));
    Undertow undertow = Undertow.builder().addHttpListener(8888, "localhost").setHandler(root).build();
    undertow.start();
}
Also used : Path(java.nio.file.Path) HttpHandler(io.undertow.server.HttpHandler) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) PathHandler(io.undertow.server.handlers.PathHandler) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) Undertow(io.undertow.Undertow)

Example 5 with ResourceHandler

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

the class FileHandlerTestCase method testDotSuffix.

@Test
public void testDotSuffix() 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.NOT_FOUND, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
    } 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)

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