Skip to content

Commit 4b8c448

Browse files
committed
Allow a regex as upload URI to support URL parameters (issue #11)
1 parent 25feaa5 commit 4b8c448

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

src/main/java/me/desair/tus/server/TusFileUploadService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected void initFeatures() {
6868
}
6969

7070
public TusFileUploadService withUploadURI(String uploadURI) {
71-
Validate.notBlank(uploadURI, "The upload URI cannot be blank");
71+
Validate.notBlank(uploadURI, "The upload URI pattern cannot be blank");
7272
this.idFactory.setUploadURI(uploadURI);
7373
return this;
7474
}

src/main/java/me/desair/tus/server/creation/validation/PostURIValidator.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
package me.desair.tus.server.creation.validation;
22

3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
36
import javax.servlet.http.HttpServletRequest;
47

58
import me.desair.tus.server.HttpMethod;
69
import me.desair.tus.server.RequestValidator;
710
import me.desair.tus.server.exception.PostOnInvalidRequestURIException;
811
import me.desair.tus.server.exception.TusException;
912
import me.desair.tus.server.upload.UploadStorageService;
10-
import org.apache.commons.lang3.StringUtils;
1113

1214
/**
1315
* The Client MUST send a POST request against a known upload creation URL to request a new upload resource.
1416
*/
1517
public class PostURIValidator implements RequestValidator {
1618

19+
private Pattern uploadUriPattern = null;
20+
1721
@Override
1822
public void validate(HttpMethod method, HttpServletRequest request,
1923
UploadStorageService uploadStorageService, String ownerKey)
2024
throws TusException {
2125

22-
if (!StringUtils.equals(request.getRequestURI(), uploadStorageService.getUploadURI())) {
26+
Matcher uploadUriMatcher = getUploadUriPattern(uploadStorageService).matcher(request.getRequestURI());
27+
28+
if (!uploadUriMatcher.matches()) {
2329
throw new PostOnInvalidRequestURIException("POST requests have to be send to "
2430
+ uploadStorageService.getUploadURI());
2531
}
@@ -30,4 +36,12 @@ public boolean supports(HttpMethod method) {
3036
return HttpMethod.POST.equals(method);
3137
}
3238

39+
private Pattern getUploadUriPattern(UploadStorageService uploadStorageService) {
40+
if (uploadUriPattern == null) {
41+
//A POST request should match the full URI
42+
uploadUriPattern = Pattern.compile("^" + uploadStorageService.getUploadURI() + "$");
43+
}
44+
return uploadUriPattern;
45+
}
46+
3347
}

src/main/java/me/desair/tus/server/upload/UploadIdFactory.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
package me.desair.tus.server.upload;
22

33
import java.util.UUID;
4+
import java.util.regex.Matcher;
5+
import java.util.regex.Pattern;
46

57
import org.apache.commons.lang3.StringUtils;
68
import org.apache.commons.lang3.Validate;
79

810
public class UploadIdFactory {
911

1012
private String uploadURI = "/";
13+
private Pattern uploadUriPattern = null;
1114

1215
public void setUploadURI(String uploadURI) {
13-
Validate.notNull(uploadURI, "The upload URI cannot be null");
16+
Validate.notNull(uploadURI, "The upload URI pattern cannot be null");
1417
this.uploadURI = uploadURI;
18+
this.uploadUriPattern = null;
1519
}
1620

1721
public UUID readUploadId(String url) {
18-
String pathId = StringUtils.substringAfter(url, uploadURI + (StringUtils.endsWith(uploadURI, "/") ? "" : "/"));
22+
Matcher uploadUriMatcher = getUploadUriPattern().matcher(StringUtils.trimToEmpty(url));
23+
String pathId = uploadUriMatcher.replaceFirst("");
1924
UUID id = null;
2025

2126
if (StringUtils.isNotBlank(pathId)) {
@@ -36,4 +41,14 @@ public String getUploadURI() {
3641
public synchronized UUID createId() {
3742
return UUID.randomUUID();
3843
}
44+
45+
private Pattern getUploadUriPattern() {
46+
if (uploadUriPattern == null) {
47+
//We will extract the upload ID's by removing the upload URI from the start of the request URI
48+
uploadUriPattern = Pattern.compile("^.*"
49+
+ uploadURI
50+
+ (StringUtils.endsWith(uploadURI, "/") ? "" : "/?"));
51+
}
52+
return uploadUriPattern;
53+
}
3954
}

src/test/java/me/desair/tus/server/upload/UploadIdFactoryTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ public void readUploadId() throws Exception {
4343
hasToString("1911e8a4-6939-490c-b58b-a5d70f8d91fb"));
4444
}
4545

46+
@Test
47+
public void readUploadIdRegex() throws Exception {
48+
idFactory.setUploadURI("/users/[0-9]+/files/upload");
49+
50+
assertThat(idFactory.readUploadId("/users/1337/files/upload/1911e8a4-6939-490c-b58b-a5d70f8d91fb"),
51+
hasToString("1911e8a4-6939-490c-b58b-a5d70f8d91fb"));
52+
}
53+
4654
@Test
4755
public void readUploadIdTrailingSlash() throws Exception {
4856
idFactory.setUploadURI("/test/upload/");
@@ -51,13 +59,29 @@ public void readUploadIdTrailingSlash() throws Exception {
5159
hasToString("1911e8a4-6939-490c-b58b-a5d70f8d91fb"));
5260
}
5361

62+
@Test
63+
public void readUploadIdRegexTrailingSlash() throws Exception {
64+
idFactory.setUploadURI("/users/[0-9]+/files/upload/");
65+
66+
assertThat(idFactory.readUploadId("/users/123456789/files/upload/1911e8a4-6939-490c-b58b-a5d70f8d91fb"),
67+
hasToString("1911e8a4-6939-490c-b58b-a5d70f8d91fb"));
68+
}
69+
5470
@Test
5571
public void readUploadIdNoUUID() throws Exception {
5672
idFactory.setUploadURI("/test/upload");
5773

5874
assertThat(idFactory.readUploadId("/test/upload/not-a-uuid-value"), is(nullValue()));
5975
}
6076

77+
@Test
78+
public void readUploadIdRegexNoMatch() throws Exception {
79+
idFactory.setUploadURI("/users/[0-9]+/files/upload");
80+
81+
assertThat(idFactory.readUploadId("/users/files/upload/1911e8a4-6939-490c-b58b-a5d70f8d91fb"),
82+
is(nullValue()));
83+
}
84+
6185
@Test
6286
public void createId() throws Exception {
6387
assertThat(idFactory.createId(), not(nullValue()));

0 commit comments

Comments
 (0)