Permalink
Browse files

form-decode decodes into strings or maps

  • Loading branch information...
1 parent 053832d commit a7a883cc16a9390f5f79d62e2c0e3b1b83f25efd @weavejester weavejester committed Mar 24, 2012
Showing with 24 additions and 20 deletions.
  1. +14 −13 ring-core/src/ring/util/codec.clj
  2. +10 −7 ring-core/test/ring/util/test/codec.clj
View
27 ring-core/src/ring/util/codec.clj
@@ -90,16 +90,17 @@
(form-encode* (str x) encoding))) (form-encode* (str x) encoding)))
(defn form-decode (defn form-decode
- "Parse parameters from a string into a map." + "Decode the supplied www-form-urlencoded string using the specified encoding,
- ([^String param-string] + or UTF-8 by default. If the encoded value is a string, a string is returned.
- (form-decode param-string "UTF-8")) + If the encoded value is a map of parameters, a map is returned."
- ([^String param-string encoding] + [^String encoded & [encoding]]
- (reduce + (letfn [(decode [s] (URLDecoder/decode s (or encoding "UTF-8")))]
- (fn [param-map encoded-param] + (if-not (.contains encoded "=")
- (if-let [[_ key val] (re-matches #"([^=]+)=(.*)" encoded-param)] + (decode encoded)
- (assoc+ param-map + (reduce
- (url-decode key encoding) + (fn [m param]
- (url-decode (or val "") encoding)) + (if-let [[k v] (str/split param #"=" 2)]
- param-map)) + (assoc+ m (decode k) (decode v))
- {} + m))
- (str/split param-string #"&")))) + {}
+ (str/split encoded #"&")))))
View
17 ring-core/test/ring/util/test/codec.clj
@@ -44,10 +44,13 @@
{"a" "b c"} "a=b+c") {"a" "b c"} "a=b+c")
(is (= (form-encode {"a" "foo/bar"} "UTF-16") "a=foo%FE%FF%00%2Fbar")))) (is (= (form-encode {"a" "foo/bar"} "UTF-16") "a=foo%FE%FF%00%2Fbar"))))
-(deftest form-encoding +(deftest test-form-decode
- (let [encoded-params "p%2F1=v%2F1&p%2F2=v%2F21&p%2F2=v%2F22"] + (are [x y] (= (form-decode x) y)
- (is (= (form-decode encoded-params) + "foo" "foo"
- (-> encoded-params + "a=b" {"a" "b"}
- form-decode + "a=b&c=d" {"a" "b" "c" "d"}
- form-encode + "foo+bar" "foo bar"
- form-decode))))) + "a=b+c" {"a" "b c"}
+ "a=b%2Fc" {"a" "b/c"})
+ (is (= (form-decode "a=foo%FE%FF%00%2Fbar" "UTF-16")
+ {"a" "foo/bar"})))

0 comments on commit a7a883c

Please sign in to comment.