   (:use clojure.contrib.str-utils)
   (:import java.net.URLDecoder))
 
-(defvar *encoding* "UTF-8"
-  "The encoding to use to decode urlencoded parameters. Defaults to
-  UTF-8.")
-
 (defn- urldecode
   "Decode a urlencoded string using the *encoding* var."
-  [s]
-  (URLDecoder/decode s *encoding*))
+  [text encoding]
+  (URLDecoder/decode text encoding))
 
 (defn- assoc-vec
   "Associate a key with a value. If the key already exists in the map,

        [cur val])
      val)))

(defn parse-params
  "Parse parameters from a string into a map."
  [#^String param-string]
  (reduce
    (fn [param-map encoded-param]
      (if-let [[_ key val] (re-matches #"([^=]+)=(.*)" encoded-param)]
        (assoc-vec param-map
          (urldecode key)
          (urldecode (or val "")))
         param-map))
    {}
    (re-split #"&" param-string)))

(defn- assoc-query-params
  "Parse and assoc parameters from the query string with the request."
  [request]
  (if-let [query-string (:query-string request)]
    (let [params (parse-params query-string)]
      (merge-with merge request {:query-params params, :params params}))
    request))



(defn- assoc-form-params
  "Parse and assoc parameters from the request body with the request."
  [request encoding]
  (if-let [body (and (urlencoded-form? request) (:body request))]
    (let [params (parse-params (slurp* body) encoding)]
      (merge-with merge request {:form-params params, :params params}))
    request))

