Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace the form decoder with URLCodec #15

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
:codox
{:output-path "codox"
:source-uri "http://github.com/ring-clojure/ring-codec/blob/{version}/{filepath}#L{line}"}
:aliases {"test-all" ["with-profile" "default:+1.4:+1.5:+1.6:+1.7:+1.8" "test"]}
:aliases {"test-all" ["with-profile" "+1.4:+1.5:+1.6:+1.7:+1.8" "test"]}
:profiles
{:dev {:dependencies [[criterium "0.4.4"]]}
:1.4 {:dependencies [[org.clojure/clojure "1.4.0"]]}
Expand Down
15 changes: 8 additions & 7 deletions src/ring/util/codec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
(:import java.io.File
java.util.Map
[java.net URLEncoder URLDecoder]
org.apache.commons.codec.binary.Base64))
org.apache.commons.codec.binary.Base64
org.apache.commons.codec.net.URLCodec))

(defn assoc-conj
"Associate a key with a value in a map. If the key already exists in the map,
Expand Down Expand Up @@ -57,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 Expand Up @@ -112,7 +112,8 @@
or UTF-8 by default."
[^String encoded & [encoding]]
(try
(URLDecoder/decode encoded (or encoding "UTF-8"))
(let [codec (URLCodec. (or encoding "UTF-8"))]
(.decode codec encoded))
(catch Exception _ nil)))

(defn form-decode
Expand Down
31 changes: 10 additions & 21 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 Expand Up @@ -58,5 +47,5 @@
"foo+bar" "foo bar"
"a=b+c" {"a" "b c"}
"a=b%2Fc" {"a" "b/c"})
(is (= (form-decode "a=foo%FE%FF%00%2Fbar" "UTF-16")
(is (= (form-decode "%FE%FF%00a=%FE%FF%00f%00o%00o%00%2F%00b%00a%00r" "UTF-16")
{"a" "foo/bar"})))