-
Notifications
You must be signed in to change notification settings - Fork 974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[UNDERTOW-2216] fix resource serving via predicates + test #1465
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
core/src/test/java/io/undertow/server/handlers/ResourcePredicateHandlerTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2023 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.undertow.server.handlers; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.attribute.FileAttribute; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.util.EntityUtils; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import io.undertow.Handlers; | ||
import io.undertow.server.HttpHandler; | ||
import io.undertow.server.HttpServerExchange; | ||
import io.undertow.server.handlers.builder.PredicatedHandler; | ||
import io.undertow.server.handlers.builder.PredicatedHandlersParser; | ||
import io.undertow.testutils.DefaultServer; | ||
import io.undertow.testutils.TestHttpClient; | ||
import io.undertow.util.StatusCodes; | ||
|
||
/** | ||
* Test basic resource serving via predicate handlers | ||
* | ||
* @author baranowb | ||
* | ||
*/ | ||
@RunWith(DefaultServer.class) | ||
public class ResourcePredicateHandlerTestCase { | ||
|
||
private static final String DIR_PREFIXED = "prefix-resource-dir"; | ||
private static final String DIR_PATH = "path-resource-dir"; | ||
private static final String FILE_NAME_LEVEL_0 = "file0"; | ||
private static final String FILE_NAME_LEVEL_1 = "file1"; | ||
private static final String DIR_SUB = "sub_dir"; | ||
private static final String GIBBERISH = "Gibberish, what did you expect?"; | ||
|
||
private static final String TEST_PREFIX = "prefixToTest"; | ||
|
||
@Test | ||
public void testPrefixMatchWithURIAltering() throws IOException { | ||
final PathsRetainer pathsRetainer = createTestDir(DIR_PREFIXED, false); | ||
DefaultServer.setRootHandler(Handlers.predicates( | ||
|
||
PredicatedHandlersParser | ||
.parse("path-prefix(/" + TEST_PREFIX + ")-> { set(attribute=%U,value=${remaining}); resource(location='" | ||
+ pathsRetainer.root.toString() + "',allow-listing=true) }", getClass().getClassLoader()), | ||
new HttpHandler() { | ||
@Override | ||
public void handleRequest(HttpServerExchange exchange) throws Exception { | ||
} | ||
})); | ||
testURLListing(pathsRetainer, true); | ||
} | ||
|
||
@Test | ||
public void testPrefixMatchNoURIAltering() throws IOException { | ||
final PathsRetainer pathsRetainer = createTestDir(DIR_PREFIXED, true); | ||
DefaultServer.setRootHandler(Handlers.predicates( | ||
|
||
PredicatedHandlersParser | ||
.parse("path-prefix(/" + TEST_PREFIX + ")-> { resource(location='" | ||
+ pathsRetainer.root.toString() + "',allow-listing=true) }", getClass().getClassLoader()), | ||
new HttpHandler() { | ||
@Override | ||
public void handleRequest(HttpServerExchange exchange) throws Exception { | ||
} | ||
})); | ||
testURLListing(pathsRetainer, true); | ||
} | ||
|
||
@Test | ||
public void testPathMatchNoURIAltering() throws IOException { | ||
final PathsRetainer pathsRetainer = createTestDir(DIR_PATH, true); | ||
//No idea why parsing does not work in one go | ||
final List<PredicatedHandler> lst = new ArrayList<>(); | ||
lst.addAll(PredicatedHandlersParser | ||
.parse("path(/" + TEST_PREFIX + ")-> { resource(location='" | ||
+ pathsRetainer.root.toString() + "',allow-listing=true)", getClass().getClassLoader())); | ||
lst.addAll(PredicatedHandlersParser | ||
.parse("path(/" + TEST_PREFIX + "/" +pathsRetainer.sub.getFileName() + ")-> { resource(location='" | ||
+ pathsRetainer.root.toString() + "',allow-listing=true) }", getClass().getClassLoader())); | ||
DefaultServer.setRootHandler(Handlers.predicates(lst, | ||
new HttpHandler() { | ||
@Override | ||
public void handleRequest(HttpServerExchange exchange) throws Exception { | ||
Assert.assertFalse(true); | ||
} | ||
})); | ||
testURLListing(pathsRetainer, false); | ||
} | ||
|
||
private void testURLListing(final PathsRetainer pathsRetainer, boolean testFile) throws IOException { | ||
|
||
TestHttpClient client = new TestHttpClient(); | ||
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/" + TEST_PREFIX+"/"); | ||
HttpResponse result = client.execute(get); | ||
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode()); | ||
String bodyToTest = EntityUtils.toString(result.getEntity()); | ||
//this is not optimal... | ||
Assert.assertTrue(bodyToTest.contains("href='/"+TEST_PREFIX+"/"+pathsRetainer.sub.getFileName()+"/'>"+pathsRetainer.sub.getFileName()+"</a>")); | ||
Assert.assertTrue(bodyToTest.contains("href='/"+TEST_PREFIX+"/"+pathsRetainer.rootFile.getFileName()+"'>"+pathsRetainer.rootFile.getFileName()+"</a>")); | ||
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/" + TEST_PREFIX+ "/" +pathsRetainer.sub.getFileName()+ "/"); | ||
result = client.execute(get); | ||
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode()); | ||
bodyToTest = EntityUtils.toString(result.getEntity()); | ||
Assert.assertTrue(bodyToTest.contains("href='/"+TEST_PREFIX+"/'>[..]</a>")); | ||
Assert.assertTrue(bodyToTest.contains("href='/"+TEST_PREFIX+"/"+pathsRetainer.sub.getFileName()+"/"+pathsRetainer.subFile.getFileName()+"'>"+pathsRetainer.subFile.getFileName()+"</a>")); | ||
if(testFile) { | ||
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/"+TEST_PREFIX+"/"+pathsRetainer.sub.getFileName()+"/"+pathsRetainer.subFile.getFileName()); | ||
result = client.execute(get); | ||
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode()); | ||
bodyToTest = EntityUtils.toString(result.getEntity()); | ||
Assert.assertEquals(GIBBERISH, bodyToTest); | ||
} | ||
} | ||
|
||
private PathsRetainer createTestDir(final String dirName, final boolean prefixDirectory) throws IOException { | ||
final FileAttribute<?>[] attribs = new FileAttribute<?>[] {}; | ||
final PathsRetainer pathsRetainer = new PathsRetainer(); | ||
Path dir = Files.createTempDirectory(dirName); | ||
if (prefixDirectory) { | ||
//dont use temp, as it will add random stuff | ||
//parent is already temp | ||
File f = dir.toFile(); | ||
f = new File(f,TEST_PREFIX); | ||
Assert.assertTrue(f.mkdir()); | ||
pathsRetainer.root = dir; | ||
dir = f.toPath(); | ||
} else { | ||
pathsRetainer.root = dir; | ||
} | ||
|
||
Path file = Files.createTempFile(dir, FILE_NAME_LEVEL_0,".txt", attribs); | ||
pathsRetainer.rootFile = file; | ||
writeGibberish(file); | ||
final Path subdir = Files.createTempDirectory(dir, DIR_SUB); | ||
pathsRetainer.sub = subdir; | ||
file = Files.createTempFile(subdir, FILE_NAME_LEVEL_1,".txt", attribs); | ||
pathsRetainer.subFile = file; | ||
writeGibberish(file); | ||
return pathsRetainer; | ||
} | ||
|
||
private void writeGibberish(final Path p) throws IOException { | ||
Files.write(p,GIBBERISH.getBytes()); | ||
} | ||
private static class PathsRetainer{ | ||
private Path root; | ||
private Path rootFile; | ||
private Path sub; | ||
private Path subFile; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@baranowb can the original method be deprecated with an attribute marking it for removal on 2.4.0.Final?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fl4via yes, however, the question is this fix required, given that more or less this can be achieved with some URL rewriting?