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