Fixed wrong encoding of URI templates with @-signs (SPR-6874)
This commit is contained in:
parent
e653a9cd03
commit
d207c2294d
|
|
@ -182,7 +182,7 @@ public class UriTemplate {
|
|||
throw new IllegalStateException(ex);
|
||||
}
|
||||
catch (URISyntaxException ex) {
|
||||
throw new IllegalArgumentException("Could not create URI from [" + uri + "]: " + ex);
|
||||
throw new IllegalArgumentException("Could not create URI from [" + uri + "]: " + ex, ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -201,24 +201,23 @@ public class UriTemplate {
|
|||
Matcher m = NAMES_PATTERN.matcher(uriTemplate);
|
||||
int end = 0;
|
||||
while (m.find()) {
|
||||
this.patternBuilder.append(encodeAndQuote(uriTemplate, end, m.start()));
|
||||
this.patternBuilder.append(quote(uriTemplate, end, m.start()));
|
||||
this.patternBuilder.append(VALUE_REGEX);
|
||||
this.variableNames.add(m.group(1));
|
||||
end = m.end();
|
||||
}
|
||||
this.patternBuilder.append(encodeAndQuote(uriTemplate, end, uriTemplate.length()));
|
||||
this.patternBuilder.append(quote(uriTemplate, end, uriTemplate.length()));
|
||||
int lastIdx = this.patternBuilder.length() - 1;
|
||||
if (lastIdx >= 0 && this.patternBuilder.charAt(lastIdx) == '/') {
|
||||
this.patternBuilder.deleteCharAt(lastIdx);
|
||||
}
|
||||
}
|
||||
|
||||
private String encodeAndQuote(String fullPath, int start, int end) {
|
||||
private String quote(String fullPath, int start, int end) {
|
||||
if (start == end) {
|
||||
return "";
|
||||
}
|
||||
String result = encodeUri(fullPath.substring(start, end)).toASCIIString();
|
||||
return Pattern.quote(result);
|
||||
return Pattern.quote(fullPath.substring(start, end));
|
||||
}
|
||||
|
||||
private List<String> getVariableNames() {
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ public abstract class UriUtils {
|
|||
|
||||
private static final String SCHEME_PATTERN = "([^:/?#]+):";
|
||||
|
||||
private static final String USERINFO_PATTERN = "([^@]*)";
|
||||
private static final String USERINFO_PATTERN = "([^@/]*)";
|
||||
|
||||
private static final String HOST_PATTERN = "([^/?#:]*)";
|
||||
|
||||
|
|
|
|||
|
|
@ -127,6 +127,9 @@ public class RestTemplateIntegrationTests {
|
|||
public void uri() throws InterruptedException, URISyntaxException {
|
||||
String result = template.getForObject(URI + "/uri/{query}", String.class, "Z\u00fcrich");
|
||||
assertEquals("Invalid request URI", "/uri/Z%FCrich", result);
|
||||
|
||||
result = template.getForObject(URI + "/uri/query={query}", String.class, "foo@bar");
|
||||
assertEquals("Invalid request URI", "/uri/query=foo@bar", result);
|
||||
}
|
||||
|
||||
/** Servlet that returns and error message for a given status code. */
|
||||
|
|
|
|||
|
|
@ -152,4 +152,11 @@ public class UriTemplateTests {
|
|||
assertEquals("/$replacement", uri.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expandWithAtSign() {
|
||||
UriTemplate template = new UriTemplate("http://localhost/query={query}");
|
||||
URI uri = template.expand("foo@bar");
|
||||
assertEquals("http://localhost/query=foo@bar", uri.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -117,6 +117,7 @@ public class UriUtilsTest {
|
|||
assertEquals("Invalid encoded URI", "../../../demo/jfc/SwingSet2/src/SwingSet2.java",
|
||||
UriUtils.encodeUri("../../../demo/jfc/SwingSet2/src/SwingSet2.java", ENC));
|
||||
assertEquals("Invalid encoded URI", "file:///~/calendar", UriUtils.encodeUri("file:///~/calendar", ENC));
|
||||
assertEquals("Invalid encoded URI", "http://example.com/query=foo@bar", UriUtils.encodeUri("http://example.com/query=foo@bar", ENC));
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue