diff --git a/ring-core/src/ring/middleware/multipart_params.clj b/ring-core/src/ring/middleware/multipart_params.clj index 257347c..5b1ffec 100644 --- a/ring-core/src/ring/middleware/multipart_params.clj +++ b/ring-core/src/ring/middleware/multipart_params.clj @@ -1,6 +1,6 @@ (ns ring.middleware.multipart-params "Parse multipart upload into params." - (:use [ring.util.codec :only (assoc-param)]) + (:use [ring.util.data :only (assoc+)]) (:import [org.apache.commons.fileupload.util Streams] [org.apache.commons.fileupload RequestContext @@ -54,7 +54,7 @@ (->> (request-context request encoding) (file-item-seq) (map #(parse-file-item % store)) - (reduce (fn [m [k v]] (assoc-param m k v)) {}))) + (reduce (fn [m [k v]] (assoc+ m k v)) {}))) (defn- load-var "Returns the var named by the supplied symbol, or nil if not found. Attempts diff --git a/ring-core/src/ring/util/codec.clj b/ring-core/src/ring/util/codec.clj index c2f29be..9dbff02 100644 --- a/ring-core/src/ring/util/codec.clj +++ b/ring-core/src/ring/util/codec.clj @@ -1,19 +1,20 @@ (ns ring.util.codec "Encoding and decoding utilities." + (:use ring.util.data) + (:require [clojure.string :as string]) (:import java.io.File (java.net URLEncoder URLDecoder) - org.apache.commons.codec.binary.Base64) - (:require [clojure.string :as string])) + org.apache.commons.codec.binary.Base64)) (defn url-encode - "Returns the form-url-encoded version of the given string, using either a - specified encoding or UTF-8 by default." + "Returns the url-encoded version of the given string, using either a specified + encoding or UTF-8 by default." [unencoded & [encoding]] (URLEncoder/encode unencoded (or encoding "UTF-8"))) (defn url-decode - "Returns the form-url-decoded version of the given string, using either a - specified encoding or UTF-8 by default." + "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]] (try (URLDecoder/decode encoded (or encoding "UTF-8")) @@ -29,17 +30,6 @@ [^String encoded] (Base64/decodeBase64 (.getBytes encoded))) -(defn assoc-param - "Associate a key with a value. If the key already exists in the map, - create a vector of values." - [map key val] - (assoc map key - (if-let [cur (map key)] - (if (vector? cur) - (conj cur val) - [cur val]) - val))) - (defn form-decode "Parse parameters from a string into a map." ([^String param-string] @@ -48,9 +38,9 @@ (reduce (fn [param-map encoded-param] (if-let [[_ key val] (re-matches #"([^=]+)=(.*)" encoded-param)] - (assoc-param param-map - (url-decode key encoding) - (url-decode (or val "") encoding)) + (assoc+ param-map + (url-decode key encoding) + (url-decode (or val "") encoding)) param-map)) {} (string/split param-string #"&")))) diff --git a/ring-core/src/ring/util/data.clj b/ring-core/src/ring/util/data.clj new file mode 100644 index 0000000..9a2c473 --- /dev/null +++ b/ring-core/src/ring/util/data.clj @@ -0,0 +1,13 @@ +(ns ring.util.data + "Miscellaneous functions for manipulating data structures.") + +(defn assoc+ + "Associate a key with a value in a map. If the key already exists in the map, + a vector of values is associated with the key." + [map key val] + (assoc map key + (if-let [cur (get map key)] + (if (vector? cur) + (conj cur val) + [cur val]) + val)))