Skip to content

Commit

Permalink
Merge branch '0.3-beta'
Browse files Browse the repository at this point in the history
Conflicts:
	project.clj
	ring-core/project.clj
	ring-devel/project.clj
	ring-httpcore-adapter/project.clj
	ring-jetty-adapter/project.clj
	ring-servlet/project.clj
  • Loading branch information
weavejester committed Sep 9, 2010
2 parents 4cc7eda + 720c677 commit b84b44a
Show file tree
Hide file tree
Showing 27 changed files with 216 additions and 188 deletions.
12 changes: 6 additions & 6 deletions project.clj
@@ -1,12 +1,12 @@
(defproject ring "0.2.6"
(defproject ring "0.3.0-beta1"
:description "A Clojure web applications library."
:url "http://github.com/mmcgrana/ring"
:dependencies
[[ring/ring-core "0.2.6"]
[ring/ring-devel "0.2.6"]
[ring/ring-httpcore-adapter "0.2.6"]
[ring/ring-jetty-adapter "0.2.6"]
[ring/ring-servlet "0.2.6"]]
[[ring/ring-core "0.3.0-beta1"]
[ring/ring-devel "0.3.0-beta1"]
[ring/ring-httpcore-adapter "0.3.0-beta1"]
[ring/ring-jetty-adapter "0.3.0-beta1"]
[ring/ring-servlet "0.3.0-beta1"]]
:dev-dependencies
[[autodoc "0.7.1"]
[lein-clojars "0.6.0"]]
Expand Down
9 changes: 5 additions & 4 deletions ring-core/project.clj
@@ -1,10 +1,11 @@
(defproject ring/ring-core "0.2.6"
(defproject ring/ring-core "0.3.0-beta1"
:description "Ring core libraries."
:url "http://github.com/mmcgrana/ring"
:dependencies [[org.clojure/clojure "1.1.0"]
[org.clojure/clojure-contrib "1.1.0"]
:dependencies [[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]
[commons-codec "1.4"]
[commons-io "1.4"]
[commons-fileupload "1.2.1"]
[javax.servlet/servlet-api "2.5"]]
:dev-dependencies [[lein-clojars "0.6.0"]])
:dev-dependencies [[lein-clojars "0.6.0"]
[swank-clojure "1.2.1"]])
19 changes: 9 additions & 10 deletions ring-core/src/ring/middleware/cookies.clj
@@ -1,7 +1,6 @@
(ns ring.middleware.cookies
"Cookie manipulation."
(:use [clojure.contrib.def :only (defvar-)]
[clojure.contrib.java-utils :only (as-str)]
ring.util.codec))

(defvar- re-token #"[!#$%&'*\-+.0-9A-Z\^_`a-z\|~]+"
Expand Down Expand Up @@ -40,7 +39,7 @@
[cookies]
(for [[name value] cookies]
(let [value (url-decode value)]
(if (.startsWith #^String value "\"")
(if (.startsWith ^String value "\"")
[name (read-string value)]
[name value]))))

Expand Down Expand Up @@ -76,8 +75,8 @@

(defn- write-value
"Write the main cookie value."
[name value]
(str (as-str name) "=" (url-encode value)))
[key value]
(str (name key) "=" (url-encode value)))

(defn- valid-attr?
"Is the attribute valid?"
Expand All @@ -89,20 +88,20 @@
[attrs]
{:pre [(every? valid-attr? attrs)]}
(for [[key value] attrs]
(let [name (set-cookie-attrs key)]
(let [attr-name (name (set-cookie-attrs key))]
(cond
(true? value) (str ";" (as-str name))
(true? value) (str ";" attr-name)
(false? value) ""
:else (str ";" (as-str name) "=" value)))))
:else (str ";" attr-name "=" value)))))

(defn- write-cookies
"Turn a map of cookies into a seq of strings for a Set-Cookie header."
[cookies]
(for [[name value] cookies]
(for [[key value] cookies]
(if (map? value)
(apply str (write-value name (:value value))
(apply str (write-value key (:value value))
(write-attr-map (dissoc value :value)))
(write-value name value))))
(write-value key value))))

(defn- set-cookies
"Add a Set-Cookie header to a response if there is a :cookies key."
Expand Down
8 changes: 4 additions & 4 deletions ring-core/src/ring/middleware/file.clj
Expand Up @@ -2,21 +2,21 @@
"Static file serving."
(:use (clojure.contrib [def :only (defvar-)]
[except :only (throw-if-not)]))
(:require (ring.util [codec :as codec] [response :as response])
[clojure.contrib.java-utils :as ju])
(:require (ring.util [codec :as codec]
[response :as response]))
(:import java.io.File))

(defn- ensure-dir
"Ensures that a directory exists at the given path, throwing if one does not."
[#^String dir-path]
[^String dir-path]
(let [dir (File. dir-path)]
(throw-if-not (.exists dir) "Directory does not exist: %s" dir-path)))

(defn wrap-file
"Wrap an app such that the directory at the given root-path is checked for a
static file with which to respond to the request, proxying the request to the
wrapped app if such a file does not exist."
[app #^String root-path]
[app ^String root-path]
(ensure-dir root-path)
(fn [req]
(if-not (= :get (:request-method req))
Expand Down
8 changes: 4 additions & 4 deletions ring-core/src/ring/middleware/file_info.clj
Expand Up @@ -64,13 +64,13 @@

(defn- get-extension
"Returns the file extension of a file."
[#^File file]
[^File file]
(second (re-find #"\.([^./\\]+)$" (.getPath file))))

(defn- guess-mime-type
"Returns a String corresponding to the guessed mime type for the given file,
or application/octet-stream if a type cannot be guessed."
[#^File file mime-types]
[^File file mime-types]
(get mime-types (get-extension file) "application/octet-stream"))

(defn make-http-format
Expand Down Expand Up @@ -100,8 +100,8 @@
(let [{:keys [headers body] :as response} (app req)]
(if (instance? File body)
(let [file-type (guess-mime-type body mime-types)
file-length (.length #^File body)
lmodified (Date. (.lastModified #^File body))
file-length (.length ^File body)
lmodified (Date. (.lastModified ^File body))
response (-> response
(content-type file-type)
(header "Last-Modified"
Expand Down
18 changes: 9 additions & 9 deletions ring-core/src/ring/middleware/multipart_params.clj
Expand Up @@ -11,10 +11,10 @@
(defn- multipart-form?
"Does a request have a multipart form?"
[request]
(if-let [#^String content-type (:content-type request)]
(if-let [^String content-type (:content-type request)]
(.startsWith content-type "multipart/form-data")))

(defvar- #^FileUpload file-upload
(defvar- ^FileUpload file-upload
(FileUpload.
(doto (DiskFileItemFactory.)
(.setSizeThreshold -1)
Expand All @@ -25,15 +25,15 @@
"Create a RequestContext object from a request map."
{:tag RequestContext}
[request encoding]
(proxy [RequestContext] []
(getContentType [] (:content-type request))
(getContentLength [] (:content-length request))
(getCharacterEncoding [] encoding)
(getInputStream [] (:body request))))
(reify RequestContext
(getContentType [this] (:content-type request))
(getContentLength [this] (:content-length request))
(getCharacterEncoding [this] encoding)
(getInputStream [this] (:body request))))

(defn- file-map
"Create a file map from a DiskFileItem."
[#^DiskFileItem item]
[^DiskFileItem item]
(with-meta
{:filename (.getName item)
:size (.getSize item)
Expand All @@ -45,7 +45,7 @@
"Parse a map of multipart parameters from the request."
[request encoding]
(reduce
(fn [param-map, #^DiskFileItem item]
(fn [param-map, ^DiskFileItem item]
(assoc-param param-map
(.getFieldName item)
(if (.isFormField item)
Expand Down
13 changes: 7 additions & 6 deletions ring-core/src/ring/middleware/params.clj
@@ -1,7 +1,8 @@
(ns ring.middleware.params
"Parse form and query params."
(:require (clojure.contrib [str-utils :as str] [duck-streams :as duck])
(ring.util [codec :as codec])))
(:require [ring.util.codec :as codec]
[clojure.string :as string]
[clojure.contrib.io :as io]))

(defn assoc-param
"Associate a key with a value. If the key already exists in the map,
Expand All @@ -16,7 +17,7 @@

(defn- parse-params
"Parse parameters from a string into a map."
[#^String param-string encoding]
[^String param-string encoding]
(reduce
(fn [param-map encoded-param]
(if-let [[_ key val] (re-matches #"([^=]+)=(.*)" encoded-param)]
Expand All @@ -25,7 +26,7 @@
(codec/url-decode (or val "") encoding))
param-map))
{}
(str/re-split #"&" param-string)))
(string/split param-string #"&")))

(defn- assoc-query-params
"Parse and assoc parameters from the query string with the request."
Expand All @@ -39,15 +40,15 @@
(defn- urlencoded-form?
"Does a request have a urlencoded form?"
[request]
(if-let [#^String type (:content-type request)]
(if-let [^String type (:content-type request)]
(.startsWith type "application/x-www-form-urlencoded")))

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

Expand Down
8 changes: 4 additions & 4 deletions ring-core/src/ring/middleware/session.clj
@@ -1,7 +1,7 @@
(ns ring.middleware.session
"Session manipulation."
(:use ring.middleware.cookies
ring.middleware.session.memory))
[ring.middleware.session store memory]))

(defn wrap-session
"Reads in the current HTTP session map, and adds it to the :session key on
Expand Down Expand Up @@ -34,14 +34,14 @@
(wrap-cookies
(fn [request]
(let [sess-key (get-in request [:cookies cookie-name :value])
session ((store :read) sess-key)
session (read-session store sess-key)
request (assoc request :session session)
response (handler request)
sess-key* (if (contains? response :session)
(if (response :session)
((store :write) sess-key (response :session))
(write-session store sess-key (response :session))
(if sess-key
((store :delete) sess-key))))
(delete-session store sess-key))))
response (dissoc response :session)
cookie {cookie-name (merge cookie-attrs
(response :session-cookie-attrs)
Expand Down
29 changes: 15 additions & 14 deletions ring-core/src/ring/middleware/session/cookie.clj
@@ -1,6 +1,7 @@
(ns ring.middleware.session.cookie
"Encrypted cookie session storage."
(:use [clojure.contrib.def :only (defvar-)])
(:use [clojure.contrib.def :only (defvar-)]
ring.middleware.session.store)
(:require (ring.util [codec :as codec]))
(:import java.security.SecureRandom
(javax.crypto Cipher Mac)
Expand Down Expand Up @@ -59,7 +60,7 @@
[options]
(if-let [secret-key (:key options)]
(if (string? secret-key)
(.getBytes #^String secret-key)
(.getBytes ^String secret-key)
secret-key)
(secure-random-bytes 16)))

Expand All @@ -71,23 +72,23 @@

(defn- unseal
"Retrieve a sealed Clojure data structure from a string"
[key #^String string]
[key ^String string]
(let [[data mac] (.split string "--")
data (codec/base64-decode data)]
(if (= mac (hmac key data))
(read-string (decrypt key data)))))

(deftype CookieStore [secret-key]
SessionStore
(read-session [_ data]
(or (if data (unseal secret-key data)) {}))
(write-session [_ _ data]
(seal secret-key data))
(delete-session [_ _]
(seal secret-key {})))

(defn cookie-store
"Creates an encrypted cookie storage engine."
([]
(cookie-store {}))
([] (cookie-store {}))
([options]
(let [secret-key (get-secret-key options)]
{:read (fn [session-data]
(if session-data
(or (unseal secret-key session-data) {})
{}))
:write (fn [_ session]
(seal secret-key session))
:delete (fn [_]
(seal secret-key {}))})))
(CookieStore. (get-secret-key options))))
24 changes: 14 additions & 10 deletions ring-core/src/ring/middleware/session/memory.clj
@@ -1,17 +1,21 @@
(ns ring.middleware.session.memory
"In-memory session storage."
(:use ring.middleware.session.store)
(:import java.util.UUID))

(deftype MemoryStore [session-map]
SessionStore
(read-session [_ key]
(@session-map key {}))
(write-session [_ key data]
(let [key (or key (str (UUID/randomUUID)))]
(swap! session-map assoc key data)
key))
(delete-session [_ key]
(swap! session-map dissoc key)
nil))

(defn memory-store
"Creates an in-memory session storage engine."
[]
(let [session-map (atom {})]
{:read (fn [session-key]
(@session-map session-key {}))
:write (fn [session-key session]
(let [session-key (or session-key (str (UUID/randomUUID)))]
(swap! session-map assoc session-key session)
session-key))
:delete (fn [session-key]
(swap! session-map dissoc session-key)
nil)}))
(MemoryStore. (atom {})))
7 changes: 7 additions & 0 deletions ring-core/src/ring/middleware/session/store.clj
@@ -0,0 +1,7 @@
(ns ring.middleware.session.store
"Common session store objects and functions.")

(defprotocol SessionStore
(read-session [store key] "Read a session map from the store.")
(write-session [store key data] "Write a session map to the store.")
(delete-session [store key] "Delete a session map from the store."))
2 changes: 1 addition & 1 deletion ring-core/src/ring/middleware/static.clj
Expand Up @@ -12,7 +12,7 @@
[app public-dir statics]
(let [app-with-file (wrap-file app public-dir)]
(fn [req]
(let [#^String uri (:uri req)]
(let [^String uri (:uri req)]
(if (some #(.startsWith uri %) statics)
(app-with-file req)
(app req))))))
2 changes: 1 addition & 1 deletion ring-core/src/ring/util/codec.clj
Expand Up @@ -23,5 +23,5 @@

(defn base64-decode
"Decode a base64 encoded string into an array of bytes."
[#^String encoded]
[^String encoded]
(Base64/decodeBase64 (.getBytes encoded)))
10 changes: 5 additions & 5 deletions ring-core/src/ring/util/response.clj
Expand Up @@ -19,23 +19,23 @@

(defn- safe-path?
"Is a filepath safe for a particular root?"
[#^String root #^String path]
[^String root ^String path]
(.startsWith (.getCanonicalPath (File. root path))
(.getCanonicalPath (File. root))))

(defn- find-index-file
"Search the directory for an index file."
[#^File dir]
[^File dir]
(first
(filter
#(.startsWith (.toLowerCase (.getName #^File %)) "index.")
#(.startsWith (.toLowerCase (.getName ^File %)) "index.")
(.listFiles dir))))

(defn- get-file
"Safely retrieve the correct file. See file-response for an
explanation of options."
[#^String path opts]
(if-let [file (if-let [#^String root (:root opts)]
[^String path opts]
(if-let [file (if-let [^String root (:root opts)]
(and (safe-path? root path) (File. root path))
(File. path))]
(cond
Expand Down

0 comments on commit b84b44a

Please sign in to comment.