Permalink
Browse files

Use ring.util.response.

  • Loading branch information...
1 parent 107924d commit 1bb3893656ec45933c3d1391e00d7f282d1e85fa @mmcgrana committed Feb 28, 2010
View
27 ring-core/src/ring/middleware/file.clj
@@ -1,11 +1,13 @@
(ns ring.middleware.file
- (:use clojure.contrib.except)
- (:import (java.io File)))
+ (: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]
- (java.net.URLDecoder/decode encoded "UTF-8"))
+ (URLDecoder/decode encoded "UTF-8"))
(defn- str-includes?
"Returns logical truth iff the given target appears in the given string"
@@ -18,19 +20,14 @@
returning."
[dir]
(let [#^File fdir (if (string? dir) (File. #^String dir) dir)]
- (throw-if-not (.exists fdir)
+ (ex/throw-if-not (.exists fdir)
"Directory does not exist: %s" fdir)
fdir))
-(defn- forbidden
- []
- {:status 403
- :headers {"Content-Type" "text/html"}
- :body "<html><body><h1>403 Forbidden</h1></body></html>"})
-
-(defn- success
- [file]
- {:status 200 :headers {} :body file})
+(defvar- forbidden
+ (-> (response "<html><body><h1>403 Forbidden</h1></body></html>")
+ (status 403)
+ (content-type "text/html")))
(defn- maybe-file
"Returns the File corresponding to the given relative path within the given
@@ -49,12 +46,12 @@
(if (#{:get :head} (:request-method req))
(let [uri (url-decode (:uri req))]
(if (str-includes? ".." uri)
- (forbidden)
+ forbidden
(let [path (cond
(.endsWith "/" uri) (str uri "index.html")
(re-find #"\.[a-z]+$" uri) uri
:else (str uri ".html"))]
(if-let [file (maybe-file fdir path)]
- (success file)
+ (respond file)
(app req)))))
(app req)))))
View
15 ring-devel/src/ring/handler/dump.clj
@@ -1,7 +1,8 @@
(ns ring.handler.dump
(:use (clj-html core helpers)
- (clojure.contrib def)
- (clojure set)))
+ (clojure.contrib [def :only (defvar-)])
+ ring.util.response)
+ (:require (clojure [set :as set])))
(declare css)
@@ -30,7 +31,7 @@
[:tbody
(for [key ring-keys]
(req-pair key req))]]
- (if-let [user-keys (difference (set (keys req)) (set ring-keys))]
+ (if-let [user-keys (set/difference (set (keys req)) (set ring-keys))]
(html
[:br]
[:table.request.user
@@ -42,11 +43,11 @@
"Returns a response tuple corresponding to an HTML dump of the request
req as it was recieved by this app."
[req]
- {:status 200
- :headers {"Content-Type" "text/html"}
- :body (template req)})
+ (-> (response (template req))
+ (status 200)
+ (content-type "text/html")))
-(def #^{:private true} css "
+(defvar- css "
/*
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
View
58 ring-devel/src/ring/middleware/stacktrace.clj
@@ -1,14 +1,15 @@
(ns ring.middleware.stacktrace
(:use (clj-html core helpers)
- (clojure.contrib str-utils)
- (clj-stacktrace core repl)))
+ (clj-stacktrace core repl)
+ (clojure.contrib [def :only (defvar-)])
+ ring.util.response))
(declare css)
-(defn- js-response [req e]
- {:status 500
- :headers {"Content-Type" "text/javascript"}
- :body (pst-str e)})
+(defn- js-ex-response [e]
+ (-> (response (pst-str e))
+ (status 500)
+ (content-type "text/javascript")))
(defn- elem-partial [elem]
(if (:clojure elem)
@@ -19,32 +20,33 @@
[:td.source (h (source-str elem))]
[:td.method (h (java-method-str elem))]]))
-(defn- html-reponse [req e]
- (let [excp (parse-exception e)]
- {:status 500
- :headers {"Content-Type" "text/html"}
- :body
- (html
- (doctype :xhtml-transitional)
- [:html {:xmlns "http://www.w3.org/1999/xhtml"}
- [:head
- [:meta {:http-equiv "Content-Type" :content "text/html;charset=utf-8"}]
- [:title "Ring: Stacktrace"]
- [:style {:type "text/css"} css]
- [:body
- [:div#content
- [:h3.info (h (str e))]
- [:table.trace [:tbody
- (map elem-partial (:trace-elems excp))]]]]]])}))
+(defn- html-ex-view [e]
+ (html
+ (doctype :xhtml-transitional)
+ [:html {:xmlns "http://www.w3.org/1999/xhtml"}
+ [:head
+ [:meta {:http-equiv "Content-Type" :content "text/html;charset=utf-8"}]
+ [:title "Ring: Stacktrace"]
+ [:style {:type "text/css"} css]
+ [:body
+ [:div#content
+ [:h3.info (h (str e))]
+ [:table.trace [:tbody
+ (map elem-partial (:trace-elems excp))]]]]]]))
-(defn- response
+(defn- html-ex-response [e]
+ (-> (response (html-ex-view e))
+ (status 500)
+ (content-type "text/html")))
+
+(defn- ex-response
"Returns a response showing debugging information about the exception.
Currently supports delegation to either js or html exception views."
[req e]
(let [accept (get-in req [:headers "accept"])]
(if (and accept (re-find #"^text/javascript" accept))
- (js-response req e)
- (html-reponse req e))))
+ (js-ex-response e)
+ (html-ex-reponse e))))
(defn wrap-stacktrace
"Wrap an app such that exceptions thrown within the wrapped app are caught
@@ -54,9 +56,9 @@
(try
(app req)
(catch Exception e
- (response req e)))))
+ (ex-response req e)))))
-(def #^{:private true} css "
+(defvar- css "
/*
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:

0 comments on commit 1bb3893

Please sign in to comment.