Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 4 additions & 5 deletions src/ring/util/codec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,15 @@
"Returns the url-encoded version of the given string, using either a specified
encoding or UTF-8 by default."
[unencoded & [encoding]]
(str/replace
unencoded
#"[^A-Za-z0-9_~.+-]+"
#(double-escape (percent-encode % encoding))))
(let [codec (URLCodec. (or encoding "UTF-8"))]
(.encode codec unencoded)))

(defn ^String url-decode
"Returns the url-decoded version of the given string, using either a specified
encoding or UTF-8 by default. If the encoding is invalid, nil is returned."
[encoded & [encoding]]
(percent-decode encoded encoding))
(let [codec (URLCodec. (or encoding "UTF-8"))]
(.decode codec encoded)))

(defn base64-encode
"Encode an array of bytes into a base64 encoded string."
Expand Down
29 changes: 9 additions & 20 deletions test/ring/util/test/codec.clj
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
(ns ring.util.test.codec
(:use clojure.test
ring.util.codec)
(:import java.util.Arrays))

(deftest test-percent-encode
(is (= (percent-encode " ") "%20"))
(is (= (percent-encode "+") "%2B"))
(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"))
(is (= (percent-decode "%24") "$")))
(:require [clojure.test :refer :all]
[ring.util.codec :refer :all])
(:import java.util.Arrays
org.apache.commons.codec.DecoderException))

(deftest test-url-encode
(is (= (url-encode "foo/bar") "foo%2Fbar"))
(is (= (url-encode "foo/bar" "UTF-16") "foo%FE%FF%00%2Fbar"))
(is (= (url-encode "foo+bar") "foo+bar"))
(is (= (url-encode "foo bar") "foo%20bar")))
(is (= (url-encode "foo/bar" "UTF-16") "%FE%FF%00f%00o%00o%00%2F%00b%00a%00r"))
(is (= (url-encode "foo+bar") "foo%2Bbar"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect. "foo+bar" should be encoded as "foo+bar", hence the test.

(is (= (url-encode "foo bar") "foo+bar")))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"foo bar" should be encoded as "foo%20bar".


(deftest test-url-decode
(is (= (url-decode "foo%2Fbar") "foo/bar" ))
(is (= (url-decode "foo%FE%FF%00%2Fbar" "UTF-16") "foo/bar"))
(is (= (url-decode "%") "%")))
(is (= (url-decode "%FE%FF%00f%00o%00o%00%2F%00b%00a%00r" "UTF-16") "foo/bar"))
(is (thrown? DecoderException (url-decode "%"))))

(deftest test-base64-encoding
(let [str-bytes (.getBytes "foo?/+" "UTF-8")]
Expand Down