Handle special characters in file names

This commit is contained in:
René Pfeuffer
2018-10-19 13:57:49 +02:00
parent 5a454139da
commit e01a995acc
2 changed files with 30 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ import sonia.scm.repository.NamespaceAndName;
import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
class ResourceLinks {
@@ -16,7 +17,11 @@ class ResourceLinks {
// we have to add the file path using URI, so that path separators (aka '/') will not be encoded as '%2F'
private static String addPath(String sourceWithPath, String path) {
return URI.create(sourceWithPath).resolve(path).toASCIIString();
try {
return new URI(sourceWithPath).resolve(new URI(null, null, path, null)).toASCIIString();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
GroupLinks group() {

View File

@@ -173,6 +173,30 @@ public class ResourceLinksTest {
assertEquals(BASE_URL + ConfigResource.CONFIG_PATH_V2, url);
}
@Test
public void shouldHandleSpacesInPaths() {
String url = resourceLinks.source().content("space", "name", "rev", "name with spaces");
assertEquals(BASE_URL + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/name/content/rev/name%20with%20spaces", url);
}
@Test
public void shouldHandleBackslashInPaths() {
String url = resourceLinks.source().content("space", "name", "rev", "name_with_\\");
assertEquals(BASE_URL + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/name/content/rev/name_with_%5C", url);
}
@Test
public void shouldHandleNewLineInPaths() {
String url = resourceLinks.source().content("space", "name", "rev", "name_with\nnew_line");
assertEquals(BASE_URL + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/name/content/rev/name_with%0Anew_line", url);
}
@Test
public void shouldKeepSlashesInInPaths() {
String url = resourceLinks.source().content("space", "name", "rev", "some/dir/somewhere");
assertEquals(BASE_URL + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/name/content/rev/some/dir/somewhere", url);
}
@Before
public void initUriInfo() {
initMocks(this);