Skip to content

Commit

Permalink
webdav: fix parsing of urls with two slashes in the path
Browse files Browse the repository at this point in the history
Motivation:
The surrent implementations will strip the urls like:

https://door.domain.foo:1234//pnfs/domain.foo/path/to/file

into

/domain.foo/path/to/file

Modification:

Stop using doggy ServletRequest.stripToPath in a favor of own
implementation.

Result:
Correct behavior parsing of files.

Acked-by: Paul Millar
Target: master, 9.2, 9.1, 9.0, 8.2
Require-book: no
Require-notes: yes
(cherry picked from commit 830a8a1)
Signed-off-by: Tigran Mkrtchyan <tigran.mkrtchyan@desy.de>
  • Loading branch information
kofemann authored and mksahakyan committed Nov 7, 2023
1 parent 428f7c7 commit 0e33db6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
Expand Up @@ -1692,7 +1692,7 @@ public HttpTransfer(PnfsHandler pnfs, Subject subject,

var request = ServletRequest.getRequest();
request.setAttribute(TRANSACTION_ATTRIBUTE, getTransaction());
_requestPath = ServletRequest.stripToPath(request.getRequestURL().toString());
_requestPath = Requests.stripToPath(request.getRequestURL().toString());
}

protected ProtocolInfo createProtocolInfo(InetSocketAddress address) {
Expand Down
Expand Up @@ -28,6 +28,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.util.Collection;

/**
Expand Down Expand Up @@ -158,4 +161,24 @@ private static float qValueOf(MediaType m) {
return 1.0f;
}
}

/**
* Extract the normalized path element of the given URL String excluding query information.
*
* @param url The string representation of the URL.
* @return The path component of the URL.
*/
public static String stripToPath(String uri) {
return stripToPath(URI.create(uri).getPath());
}

/**
* Extract the normalized path element of the given URL excluding query information.
*
* @param url The URL to extract path from.
* @return The path component of the URL.
*/
public static String stripToPath(URL url) {
return Path.of(url.getPath()).normalize().toString();
}
}
Expand Up @@ -20,6 +20,8 @@

import com.google.common.net.MediaType;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;

import org.junit.Test;
Expand Down Expand Up @@ -107,4 +109,27 @@ public void shouldAcceptWithMatchingParameter() {

assertThat(responseType, equalTo(PLAIN_TEXT_UTF_8));
}

@Test
public void shouldReturnFilePathOfUrl() throws MalformedURLException {
var u = new URL("https://door.domain.foo/pnfs/domain.foo/path/to/file");
assertThat(Requests.stripToPath(u), equalTo("/pnfs/domain.foo/path/to/file"));
}

@Test
public void shouldReturnFilePathOfUrlWithPort() throws MalformedURLException {
var u = new URL("https://door.domain.foo:1234/pnfs/domain.foo/path/to/file?foo=bar");
assertThat(Requests.stripToPath(u), equalTo("/pnfs/domain.foo/path/to/file"));
}
@Test
public void shouldReturnFilePathOfUrlWithPortAndExtraSlash() throws MalformedURLException {
var u = new URL("https://door.domain.foo:1234//pnfs/domain.foo//path/to/file");
assertThat(Requests.stripToPath(u), equalTo("/pnfs/domain.foo/path/to/file"));
}

@Test
public void shouldReturnFilePathOfUrlWithQuery() throws MalformedURLException {
var u = new URL("https://door.domain.foo:1234/pnfs/domain.foo/path/to/file?foo=bar");
assertThat(Requests.stripToPath(u), equalTo("/pnfs/domain.foo/path/to/file"));
}
}

0 comments on commit 0e33db6

Please sign in to comment.