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();
}
}
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;
}
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;
}
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));
}
}
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();
}
Aggregations