Skip to content

Commit 3852456

Browse files
GRAILS-11526 - improve URL mapping extension handling
1 parent bce7c37 commit 3852456

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

grails-plugin-url-mappings/src/main/groovy/org/codehaus/groovy/grails/web/mapping/RegexUrlMapping.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,21 @@ protected Pattern convertToRegex(String url) {
227227
.replaceAll("([^\\*])\\*$", "$1[^/]+")
228228
.replaceAll("\\*\\*", ".*");
229229

230-
pattern += urlEnd
230+
if("/(*)(\\.(*))".equals(urlEnd)) {
231+
// shortcut this common special case which will
232+
// happen any time a URL mapping ends with a pattern like
233+
// /$someVariable(.$someExtension)
234+
pattern += "/([^/]+)\\.([^/.]+)?";
235+
} else {
236+
pattern += urlEnd
231237
.replace("(\\.(*))", "\\.?([^/]+)?")
232238
.replaceAll("([^\\*])\\*([^\\*])", "$1[^/]+$2")
233239
.replaceAll("([^\\*])\\*$", "$1[^/]+")
234240
.replaceAll("\\*\\*", ".*")
235241
.replaceAll("\\(\\[\\^\\/\\]\\+\\)\\\\\\.", "([^/.]+)\\\\.")
236242
.replaceAll("\\(\\[\\^\\/\\]\\+\\)\\?\\\\\\.", "([^/.]+)\\?\\\\.")
237243
;
238-
244+
}
239245
pattern += "/??$";
240246
regex = Pattern.compile(pattern);
241247
}

grails-test-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/mapping/RegexUrlMappingTests.groovy

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,26 @@ mappings {
6969
controller = 'hyphenTests'
7070
action = 'view'
7171
}
72+
"/plugins/grails-$plugin/tags/RELEASE_$version/$fullName(.$type)" {
73+
controller = 'website'
74+
action = 'displayPlugin'
75+
}
7276
}
7377
'''
7478

79+
void testExtensionPrecededByTokenWhichMayContainDots() {
80+
def holder = new DefaultUrlMappingsHolder(evaluator.evaluateMappings(new ByteArrayResource(mappingScript.bytes)))
81+
82+
def info = holder.match("/plugins/grails-csv/tags/RELEASE_0.3.1/csv-0.3.1.pom")
83+
assertNotNull info
84+
assertEquals 'website', info.controllerName
85+
assertEquals 'displayPlugin', info.actionName
86+
assertEquals '0.3.1', info.params.version
87+
assertEquals 'csv', info.params.plugin
88+
assertEquals 'csv-0.3.1', info.params.fullName
89+
assertEquals 'pom', info.params.type
90+
}
91+
7592
void testHyphenDelimiters() {
7693
def holder = new DefaultUrlMappingsHolder(evaluator.evaluateMappings(new ByteArrayResource(mappingScript.bytes)))
7794

0 commit comments

Comments
 (0)