/
dump.clj
58 lines (52 loc) · 1.78 KB
/
dump.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
(ns ring.handler.dump
"A handler that displays the received request map.
This is useful for debugging new adapters."
(:require [clojure.set :as set]
[clojure.pprint :as pprint]
[clojure.java.io :as io]
[hiccup.core :refer [html h]]
[hiccup.page :refer [doctype]]
[hiccup.def :refer [defhtml]]
[ring.util.response :refer [content-type response]]))
(def ^:no-doc ring-keys
'(:server-port :server-name :remote-addr :uri :query-string :scheme
:request-method :content-type :content-length :character-encoding
:ssl-client-cert :headers :body))
(defn- style-resource [path]
(html [:style {:type "text/css"} (slurp (io/resource path))]))
(defn- req-pair [key req]
(html
[:tr
[:td.key (h (str key))]
[:td.val (h (pr-str (key req)))]]))
(defhtml ^:no-doc template
[req]
(doctype :xhtml-transitional)
[:html {:xmlns "http://www.w3.org/1999/xhtml"}
[:head
[:title "Ring: Request Dump"]
(style-resource "ring/css/dump.css")]
[:body
[:div#content
[:h3.info "Ring Request Values"]
[:table.request
[:tbody
(for [key ring-keys]
(req-pair key req))]]
(if-let [user-keys (set/difference (set (keys req)) (set ring-keys))]
(html
[:br]
[:table.request.user
[:tbody [:tr
(for [key (sort user-keys)]
(req-pair key req))]]]))]]])
(defn handle-dump
"Returns a HTML response that shows the information in the request map.
Also prints the request map to STDOUT."
([request]
(pprint/pprint request)
(println)
(-> (response (template request))
(content-type "text/html")))
([request respond raise]
(respond (handle-dump request))))