Search in sources :

Example 76 with PathHandler

use of io.undertow.server.handlers.PathHandler 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();
    }
}
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 77 with PathHandler

use of io.undertow.server.handlers.PathHandler 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();
    }
}
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 78 with PathHandler

use of io.undertow.server.handlers.PathHandler in project undertow by undertow-io.

the class FileHandlerSymlinksTestCase method testResourceManagerBaseSymlink.

@Test
public void testResourceManagerBaseSymlink() 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, base is a symlink but it should not be checked in the symlinks filter
             */
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/page.html");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        /**
             * A readResponse() is needed in order to release connection and execute next get.
             */
        HttpClientUtils.readResponse(result);
        /**
             * This request should return a 404 code as rootPath + "/innerSymlink" is not matching in symlinks filter"
             */
        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) 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 79 with PathHandler

use of io.undertow.server.handlers.PathHandler in project undertow by undertow-io.

the class FileHandlerSymlinksTestCase method testExplicitAccessSymlinkDeniedUsingSpecificFilters.

@Test
public void testExplicitAccessSymlinkDeniedUsingSpecificFilters() 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("/otherDir"))).setDirectoryListingEnabled(false).addWelcomeFiles("page.html"))));
        /**
             * This request should return a 404 code as rootPath + "/otherDir" doesnt match in rootPath + "/path/innerSymlink/page.html"
             */
        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 80 with PathHandler

use of io.undertow.server.handlers.PathHandler in project undertow by undertow-io.

the class RequestContentEncodingTestCase method setup.

@BeforeClass
public static void setup() {
    final ContentEncodingRepository contentEncodingRepository = new ContentEncodingRepository().addEncodingHandler("deflate", new DeflateEncodingProvider(), 50).addEncodingHandler("gzip", new GzipEncodingProvider(), 60);
    final EncodingHandler encode = new EncodingHandler(contentEncodingRepository).setNext(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, message.length() + "");
            exchange.getResponseSender().send(message, IoCallback.END_EXCHANGE);
        }
    });
    final EncodingHandler wrappedEncode = new EncodingHandler(contentEncodingRepository).setNext(encode);
    final HttpHandler decode = new RequestEncodingHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getRequestReceiver().receiveFullBytes(new Receiver.FullBytesCallback() {

                @Override
                public void handle(HttpServerExchange exchange, byte[] message) {
                    exchange.getResponseSender().send(ByteBuffer.wrap(message));
                }
            });
        }
    }).addEncoding("deflate", InflatingStreamSourceConduit.WRAPPER).addEncoding("gzip", GzipStreamSourceConduit.WRAPPER);
    final HttpHandler wrappedDecode = new RequestEncodingHandler(decode).addEncoding("deflate", InflatingStreamSourceConduit.WRAPPER).addEncoding("gzip", GzipStreamSourceConduit.WRAPPER);
    PathHandler pathHandler = new PathHandler();
    pathHandler.addPrefixPath("/encode", wrappedEncode);
    pathHandler.addPrefixPath("/decode", wrappedDecode);
    DefaultServer.setRootHandler(pathHandler);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) PathHandler(io.undertow.server.handlers.PathHandler) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Aggregations

PathHandler (io.undertow.server.handlers.PathHandler)94 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)60 DeploymentManager (io.undertow.servlet.api.DeploymentManager)59 ServletContainer (io.undertow.servlet.api.ServletContainer)58 ServletInfo (io.undertow.servlet.api.ServletInfo)50 BeforeClass (org.junit.BeforeClass)48 Test (org.junit.Test)30 TestHttpClient (io.undertow.testutils.TestHttpClient)28 HttpResponse (org.apache.http.HttpResponse)25 HttpGet (org.apache.http.client.methods.HttpGet)24 PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)22 CanonicalPathHandler (io.undertow.server.handlers.CanonicalPathHandler)21 ResourceHandler (io.undertow.server.handlers.resource.ResourceHandler)21 Path (java.nio.file.Path)21 SimpleServletTestCase (io.undertow.servlet.test.SimpleServletTestCase)17 HttpHandler (io.undertow.server.HttpHandler)16 FilterInfo (io.undertow.servlet.api.FilterInfo)14 LoginConfig (io.undertow.servlet.api.LoginConfig)12 Header (org.apache.http.Header)12 TestResourceLoader (io.undertow.servlet.test.util.TestResourceLoader)11