Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Only decode valid percent codes
  • Loading branch information
James Conroy-Finn committed Mar 12, 2015
commit 780088590b57a0caf2d64f378f8ab852547a698e
4 changes: 2 additions & 2 deletions src/ring/util/codec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
(str/join)))

(defn- parse-bytes [encoded-bytes]
(->> (re-seq #"%.." encoded-bytes)
(->> (re-seq #"%[A-Za-z0-9]{2}" encoded-bytes)
(map #(subs % 1))
(map #(.byteValue (Integer/valueOf % 16)))
(byte-array)))
Expand All @@ -47,7 +47,7 @@
specified encoding, or UTF-8 by default."
[^String encoded & [^String encoding]]
(str/replace encoded
#"(?:%..)+"
#"(?:%[A-Za-z0-9]{2})+"
(fn [chars]
(-> ^bytes (parse-bytes chars)
(String. (or encoding "UTF-8"))
Expand Down
1 change: 1 addition & 0 deletions test/ring/util/test/codec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
(is (= (percent-encode "foo") "%66%6F%6F")))

(deftest test-percent-decode
(is (= (percent-decode "%s/") "%s/"))
(is (= (percent-decode "%20") " "))
(is (= (percent-decode "foo%20bar") "foo bar"))
(is (= (percent-decode "foo%FE%FF%00%2Fbar" "UTF-16") "foo/bar"))
Expand Down