Permalink
Browse files

Factor base64 and url encoding/decoding into ring.util.codec.

  • Loading branch information...
1 parent 1bb3893 commit 15fc2b68c47c15e729c3c1c3b5af83dc98ce501b @mmcgrana committed Feb 28, 2010
View
9 ring-core/src/ring/middleware/file.clj
@@ -2,12 +2,7 @@
(:use (clojure.contrib [def :only (defvar-)]
[except :only (throw-if-not)])
ring.util.response)
- (:import java.io.File java.net.URLDecoder))
-
-(defn- url-decode
- "Returns the form-url-decoded version of the given string."
- [encoded]
- (URLDecoder/decode encoded "UTF-8"))
+ (:require (ring.util [codec :as codec])))
(defn- str-includes?
"Returns logical truth iff the given target appears in the given string"
@@ -44,7 +39,7 @@
(let [fdir (ensure-dir dir)]
(fn [req]
(if (#{:get :head} (:request-method req))
- (let [uri (url-decode (:uri req))]
+ (let [uri (codec/url-decode (:uri req))]
(if (str-includes? ".." uri)
forbidden
(let [path (cond
View
15 ring-core/src/ring/middleware/params.clj
@@ -1,8 +1,6 @@
(ns ring.middleware.params
- (:use clojure.contrib.def)
- (:use clojure.contrib.duck-streams)
- (:use clojure.contrib.str-utils)
- (:import java.net.URLDecoder))
+ (:use (clojure.contrib def duck-streams str-utils))
+ (:require (ring.util [codec :as codec])))
(defn assoc-param
"Associate a key with a value. If the key already exists in the map,
@@ -15,20 +13,15 @@
[cur val])
val)))
-(defn- urldecode
- "Decode a urlencoded string using the specified encoding."
- [text encoding]
- (URLDecoder/decode text encoding))
-
(defn- parse-params
"Parse parameters from a string into a map."
[#^String param-string encoding]
(reduce
(fn [param-map encoded-param]
(if-let [[_ key val] (re-matches #"([^=]+)=(.*)" encoded-param)]
(assoc-param param-map
- (urldecode key encoding)
- (urldecode (or val "") encoding))
+ (codec/url-decode key encoding)
+ (codec/url-decode (or val "") encoding))
param-map))
{}
(re-split #"&" param-string)))
View
23 ring-core/src/ring/middleware/session/cookie.clj
@@ -1,11 +1,10 @@
(ns ring.middleware.session.cookie
"Encrypted cookie session storage."
(:use clojure.contrib.def)
- (:use clojure.contrib.base64)
+ (:require (ring.util [codec :as codec]))
(:import java.security.SecureRandom
- [javax.crypto Cipher Mac]
- [javax.crypto.spec SecretKeySpec IvParameterSpec]
- org.apache.commons.codec.binary.Base64))
+ (javax.crypto Cipher Mac)
+ (javax.crypto.spec SecretKeySpec IvParameterSpec)))
(defvar- seed-algorithm "SHA1PRNG"
"Algorithm to seed random numbers.")
@@ -26,22 +25,12 @@
(.nextBytes (SecureRandom/getInstance seed-algorithm) seed)
seed))
-(defn- base64-encode
- "Encode an array of bytes into a base64 encoded string."
- [unencoded]
- (String. (Base64/encodeBase64 unencoded)))
-
-(defn- base64-decode
- "Decode a base64 encoded string into an array of bytes."
- [encoded]
- (Base64/decodeBase64 (.getBytes encoded)))
-
(defn- hmac
"Generates a Base64 HMAC with the supplied key on a string of data."
[key data]
(let [mac (Mac/getInstance hmac-algorithm)]
(.init mac (SecretKeySpec. key hmac-algorithm))
- (base64-encode (.doFinal mac data))))
+ (codec/base64-encode (.doFinal mac data))))
(defn- encrypt
"Encrypt a string with a key."
@@ -78,13 +67,13 @@
"Seal a Clojure data structure into an encrypted and HMACed string."
[key data]
(let [data (encrypt key (.getBytes (pr-str data)))]
- (str (base64-encode data) "--" (hmac key data))))
+ (str (codec/base64-encode data) "--" (hmac key data))))
(defn- unseal
"Retrieve a sealed Clojure data structure from a string"
[key string]
(let [[data mac] (.split string "--")
- data (base64-decode data)]
+ data (codec/base64-decode data)]
(if (= mac (hmac key data))
(read-string (decrypt key data)))))
View
19 ring-core/src/ring/util/codec.clj
@@ -0,0 +1,19 @@
+(ns ring.util.codec
+ (:import java.io.File java.net.URLDecoder
+ org.apache.commons.codec.binary.Base64))
+
+(defn url-decode
+ "Returns the form-url-decoded version of the given string, using either a
+ specified encoding or UTF-8 by default."
+ [encoded & [encoding]]
+ (URLDecoder/decode encoded (or encoding "UTF-8")))
+
+(defn base64-encode
+ "Encode an array of bytes into a base64 encoded string."
+ [unencoded]
+ (String. (Base64/encodeBase64 unencoded)))
+
+(defn base64-decode
+ "Decode a base64 encoded string into an array of bytes."
+ [encoded]
+ (Base64/decodeBase64 (.getBytes encoded)))

0 comments on commit 15fc2b6

Please sign in to comment.