-
Notifications
You must be signed in to change notification settings - Fork 517
/
request.clj
85 lines (71 loc) · 2.42 KB
/
request.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
(ns ring.util.request
"Functions for augmenting and pulling information from request maps."
(:require [ring.util.parsing :as parsing]))
(defn request-url
"Return the full URL of the request."
{:added "1.2"}
[request]
(str (-> request :scheme name)
"://"
(get-in request [:headers "host"])
(:uri request)
(if-let [query (:query-string request)]
(str "?" query))))
(defn content-type
"Return the content-type of the request, or nil if no content-type is set."
{:added "1.3"}
[request]
(if-let [type ^String (get (:headers request) "content-type")]
(let [i (.indexOf type ";")]
(if (neg? i) type (subs type 0 i)))))
(defn content-length
"Return the content-length of the request, or nil no content-length is set."
{:added "1.3"}
[request]
(if-let [^String length (get-in request [:headers "content-length"])]
(Long/valueOf length)))
(defn character-encoding
"Return the character encoding for the request, or nil if it is not set."
{:added "1.3"}
[request]
(some-> (get-in request [:headers "content-type"])
parsing/find-content-type-charset))
(defn urlencoded-form?
"True if a request contains a urlencoded form in the body."
{:added "1.3"}
[request]
(if-let [^String type (content-type request)]
(.startsWith type "application/x-www-form-urlencoded")))
(defmulti ^String body-string
"Return the request body as a string."
{:arglists '([request]), :added "1.2"}
(comp class :body))
(defmethod body-string nil [_] nil)
(defmethod body-string String [request]
(:body request))
(defmethod body-string clojure.lang.ISeq [request]
(apply str (:body request)))
(defmethod body-string java.io.File [request]
(slurp (:body request)))
(defmethod body-string java.io.InputStream [request]
(slurp (:body request)))
(defn path-info
"Returns the relative path of the request."
{:added "1.2"}
[request]
(or (:path-info request)
(:uri request)))
(defn in-context?
"Returns true if the URI of the request is a subpath of the supplied context."
{:added "1.2"}
[request context]
(.startsWith ^String (:uri request) context))
(defn set-context
"Associate a context and path-info with the request. The request URI must be
a subpath of the supplied context."
{:added "1.2"}
[request ^String context]
{:pre [(in-context? request context)]}
(assoc request
:context context
:path-info (subs (:uri request) (.length context))))