Skip to content

Commit 5137694

Browse files
author
James Reeves
committed
Fixes weavejester#15: route params should be urldecoded
1 parent 81fae95 commit 5137694

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

src/compojure/http/routes.clj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
(:use compojure.str-utils)
1717
(:use compojure.map-utils)
1818
(:use compojure.control)
19+
(:use compojure.encodings)
1920
(:import java.util.regex.Pattern)
2021
(:import java.util.Map))
2122

@@ -95,7 +96,7 @@
9596
(reduce
9697
(fn [m [k v]] (assoc-vec m k v))
9798
{}
98-
(map vector keywords (rest groups))))
99+
(map vector keywords groups)))
99100

100101
(defmulti match-uri
101102
"Match a URL against a compiled URI-matcher or a regular expression. Returns
@@ -107,15 +108,15 @@
107108
(let [matcher (re-matcher (uri-matcher :regex) (or uri "/"))]
108109
(if (.matches matcher)
109110
(assoc-keywords-with-groups
110-
(re-groups matcher)
111+
(map urldecode (re-groups* matcher))
111112
(uri-matcher :keywords)))))
112113

113114
(defmethod match-uri Pattern
114115
[uri-pattern uri]
115116
(let [matches (re-matches uri-pattern (or uri "/"))]
116117
(if matches
117118
(if (vector? matches)
118-
(subvec matches 1)
119+
(vec (map urldecode (rest matches)))
119120
[]))))
120121

121122
(defn match-method

src/compojure/str_utils.clj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@
5252
[string]
5353
(escape "\\.*+|?()[]{}$^" string))
5454

55+
(defn re-groups*
56+
"More consistant re-groups that always returns a vector of groups, even if
57+
there is only one group."
58+
[matcher]
59+
(for [i (range (.groupCount matcher))]
60+
(.group matcher (inc i))))
61+
5562
(defn blank?
5663
"True if s = \"\" or nil"
5764
[s]

test/compojure/http/routes.clj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
"/:foo-bar" "/baz" {:foo-bar "baz"}
3131
"/:foo-" "/baz" {:foo- "baz"}))
3232

33+
(deftest urlencoded-keywords
34+
(are (= (match-uri (compile-uri-matcher _1) _2) _3)
35+
"/:x" "/foo%20bar" {:x "foo bar"}
36+
"/:x" "/foo+bar" {:x "foo bar"}))
37+
3338
(deftest same-keyword-many-times
3439
(are (= (match-uri (compile-uri-matcher _1) _2) _3)
3540
"/:x/:x/:x" "/a/b/c" {:x ["a" "b" "c"]}

0 commit comments

Comments
 (0)