Search in sources :

Example 16 with PathResourceManager

use of io.undertow.server.handlers.resource.PathResourceManager 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 17 with PathResourceManager

use of io.undertow.server.handlers.resource.PathResourceManager 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 long cacheBufferSize = CACHE_BUFFER_SIZE.resolveModelAttribute(context, model).asLong();
    final long cacheBuffers = CACHE_BUFFERS.resolveModelAttribute(context, model).asLong();
    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);
    Path base;
    try {
        // workaround for JBEAP-10231
        base = Paths.get(path).normalize().toRealPath();
    } catch (IOException e) {
        throw new OperationFailedException(UndertowLogger.ROOT_LOGGER.unableAddHandlerForPath(path));
    }
    PathResourceManager resourceManager = new PathResourceManager(base, cacheBufferSize * cacheBuffers, caseSensitive, followSymlink, paths);
    ResourceHandler handler = new ResourceHandler(resourceManager);
    handler.setDirectoryListingEnabled(directoryListing);
    return handler;
}
Also used : Path(java.nio.file.Path) OperationFailedException(org.jboss.as.controller.OperationFailedException) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) IOException(java.io.IOException) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager)

Example 18 with PathResourceManager

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

the class Host method configureRootHandler.

private HttpHandler configureRootHandler() {
    AccessLogService logService = accessLogService;
    HttpHandler rootHandler = pathHandler;
    final Function<HttpHandler, HttpHandler> accessLogHttpHandler = this.accessLogHttpHandler;
    ArrayList<UndertowFilter> filters = new ArrayList<>(this.filters);
    // handle options * requests
    rootHandler = new OptionsHandler(rootHandler);
    // handle requests that use the Expect: 100-continue header
    rootHandler = Handlers.httpContinueRead(rootHandler);
    rootHandler = LocationService.configureHandlerChain(rootHandler, filters);
    if (logService != null) {
        rootHandler = logService.configureAccessLogHandler(rootHandler);
    }
    if (accessLogHttpHandler != null) {
        rootHandler = accessLogHttpHandler.apply(rootHandler);
    }
    // handle .well-known requests from ACME certificate authorities
    String path = WildFlySecurityManager.getPropertyPrivileged("jboss.home.dir", ".");
    Path base;
    try {
        base = Paths.get(path).normalize().toRealPath();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    final int cacheBufferSize = 1024;
    final int cacheBuffers = 1024;
    PathResourceManager resourceManager = new PathResourceManager(base, cacheBufferSize * cacheBuffers, true, false);
    rootHandler = new AcmeResourceHandler(resourceManager, rootHandler);
    GateHandlerWrapper gateHandlerWrapper = this.gateHandlerWrapper;
    if (gateHandlerWrapper != null) {
        rootHandler = gateHandlerWrapper.wrap(rootHandler);
    }
    return rootHandler;
}
Also used : Path(java.nio.file.Path) HttpHandler(io.undertow.server.HttpHandler) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) IOException(java.io.IOException) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) GateHandlerWrapper(org.wildfly.extension.undertow.deployment.GateHandlerWrapper)

Example 19 with PathResourceManager

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

the class GetResourceTestCase method testSpecialCharacterInFileURL.

@Test
public void testSpecialCharacterInFileURL() throws IOException {
    String tmp = System.getProperty("java.io.tmpdir");
    PathResourceManager pathResourceManager = new PathResourceManager(Paths.get(tmp), 1);
    Path file = Paths.get(tmp, "1#2.txt");
    Files.write(file, "Hi".getBytes());
    Resource res = pathResourceManager.getResource("1#2.txt");
    try (InputStream in = res.getUrl().openStream()) {
        Assert.assertEquals("Hi", FileUtils.readFile(in));
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) Resource(io.undertow.server.handlers.resource.Resource) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) Test(org.junit.Test)

Example 20 with PathResourceManager

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

the class Http2Server method main.

public static void main(final String[] args) throws Exception {
    String version = System.getProperty("java.version");
    System.out.println("Java version " + version);
    if (version.charAt(0) == '1' && Integer.parseInt(version.charAt(2) + "") < 8) {
        System.out.println("This example requires Java 1.8 or later");
        System.out.println("The HTTP2 spec requires certain cyphers that are not present in older JVM's");
        System.out.println("See section 9.2.2 of the HTTP2 specification for details");
        System.exit(1);
    }
    String bindAddress = System.getProperty("bind.address", "localhost");
    SSLContext sslContext = createSSLContext(loadKeyStore("server.keystore"), loadKeyStore("server.truststore"));
    Undertow server = Undertow.builder().setServerOption(UndertowOptions.ENABLE_HTTP2, true).addHttpListener(8080, bindAddress).addHttpsListener(8443, bindAddress, sslContext).setHandler(new SessionAttachmentHandler(new LearningPushHandler(100, -1, Handlers.header(predicate(secure(), resource(new PathResourceManager(Paths.get(System.getProperty("example.directory", System.getProperty("user.home"))), 100)).setDirectoryListingEnabled(true), new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseHeaders().add(Headers.LOCATION, "https://" + exchange.getHostName() + ":" + (exchange.getHostPort() + 363) + exchange.getRelativePath());
            exchange.setStatusCode(StatusCodes.TEMPORARY_REDIRECT);
        }
    }), "x-undertow-transport", ExchangeAttributes.transportProtocol())), new InMemorySessionManager("test"), new SessionCookieConfig())).build();
    server.start();
    SSLContext clientSslContext = createSSLContext(loadKeyStore("client.keystore"), loadKeyStore("client.truststore"));
    LoadBalancingProxyClient proxy = new LoadBalancingProxyClient().addHost(new URI("https://localhost:8443"), null, new UndertowXnioSsl(Xnio.getInstance(), OptionMap.EMPTY, clientSslContext), OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).setConnectionsPerThread(20);
    Undertow reverseProxy = Undertow.builder().setServerOption(UndertowOptions.ENABLE_HTTP2, true).addHttpListener(8081, bindAddress).addHttpsListener(8444, bindAddress, sslContext).setHandler(ProxyHandler.builder().setProxyClient(proxy).setMaxRequestTime(30000).build()).build();
    reverseProxy.start();
}
Also used : HttpHandler(io.undertow.server.HttpHandler) SSLContext(javax.net.ssl.SSLContext) LearningPushHandler(io.undertow.server.handlers.LearningPushHandler) URI(java.net.URI) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) LoadBalancingProxyClient(io.undertow.server.handlers.proxy.LoadBalancingProxyClient) HttpServerExchange(io.undertow.server.HttpServerExchange) SessionAttachmentHandler(io.undertow.server.session.SessionAttachmentHandler) SessionCookieConfig(io.undertow.server.session.SessionCookieConfig) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) Undertow(io.undertow.Undertow) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager)

Aggregations

PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)41 Path (java.nio.file.Path)34 Test (org.junit.Test)30 ResourceHandler (io.undertow.server.handlers.resource.ResourceHandler)28 PathHandler (io.undertow.server.handlers.PathHandler)27 CanonicalPathHandler (io.undertow.server.handlers.CanonicalPathHandler)25 TestHttpClient (io.undertow.testutils.TestHttpClient)24 HttpGet (org.apache.http.client.methods.HttpGet)23 HttpResponse (org.apache.http.HttpResponse)20 Header (org.apache.http.Header)12 HttpHandler (io.undertow.server.HttpHandler)5 UnitTest (io.undertow.testutils.category.UnitTest)5 Undertow (io.undertow.Undertow)4 CachingResourceManager (io.undertow.server.handlers.resource.CachingResourceManager)4 PreCompressedResourceSupplier (io.undertow.server.handlers.resource.PreCompressedResourceSupplier)4 Resource (io.undertow.server.handlers.resource.Resource)3 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)3 ServletInfo (io.undertow.servlet.api.ServletInfo)3 IOException (java.io.IOException)3 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)3