/
uri.cljc
312 lines (272 loc) · 9.95 KB
/
uri.cljc
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
(ns lambdaisland.uri
(:refer-clojure :exclude [uri?])
(:require [clojure.string :as str]
[lambdaisland.uri.normalize :as normalize])
#?(:clj (:import clojure.lang.IFn)))
(def uri-regex #?(:clj #"\A(([^:/?#]+):)?(//([^/?#\\]*))?([^?#]*)?(\?([^#]*))?(#(.*))?\z"
:cljs #"^(([^:/?#]+):)?(//([^/?#\\]*))?([^?#]*)?(\?([^#]*))?(#(.*))?$"))
(def authority-regex #?(:clj #"\A(([^:]*)(:(.*))?@)?(\[[0-9a-fA-F:]+\]|[^:]*)(:(\d*))?\z"
:cljs #"^(([^:]*)(:(.*))?@)?(\[[0-9a-fA-F:]+\]|[^:]*)(:(\d*))?$"))
(defn- authority-string [user password host port]
(when host
(cond-> user
(and user password) (str ":" password)
user (str "@")
true (str host)
port (str ":" port))))
(defn uri-str
"Convert the URI instance back to a string"
[{:keys [scheme user password host port path query fragment]}]
(let [authority (authority-string user password host port)]
(cond-> ""
scheme (str scheme ":")
authority (str "//" authority)
true (str path)
query (str "?" query)
fragment (str "#" fragment))))
(defrecord URI [scheme user password host port path query fragment]
#?@(:bb []
:default
[IFn
(#?(:clj invoke :cljs -invoke) [this kw]
(get this kw))])
Object
(toString [this]
(uri-str this)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; parse
(defn- match-uri [uri]
(let [matches (re-matches uri-regex uri)
[_ _ scheme _ authority path _ query _ fragment] matches]
[scheme authority (when (seq path) path) query fragment]))
(defn- match-authority [authority]
(let [matches (re-matches authority-regex authority)
[_ _ user _ password host _ port] matches]
[user password host port]))
(defn parse
"Parse a URI string into a lambadisland.uri.URI record."
[uri]
(let [[scheme authority path query fragment] (match-uri uri)]
(if authority
(let [[user password host port] (match-authority authority)]
(URI. scheme user password host port path query fragment))
(URI. scheme nil nil nil nil path query fragment))))
(defn uri
"Turn the given value into a lambdaisland.uri.URI record, if it isn't one
already. Supports String, java.net.URI, and other URI-like objects that return
a valid URI string with `str`."
[uri-like]
(if (instance? URI uri-like)
uri-like
(parse (str uri-like))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; join / resolve
;; This section is based on RFC 3986
(defn- absolute-path? [path]
(= (first path) \/))
(defn- remove-dot-segments
"As per RFC 3986 section 5.2.4"
[path]
(when path
(loop [in (str/split path #"(?=/)")
out []]
(case (first in)
"/." (if (next in)
(recur (next in) out)
(recur nil (conj out "/")))
"/.." (if (next in)
(recur (next in) (vec (butlast out)))
(recur nil (conj (vec (butlast out)) "/")))
nil (str/join out)
(recur (next in) (conj out (first in)))))))
(defn- merge-paths [a b]
(if (some #{\/} a)
(str (re-find #?(:clj #"\A.*/"
:cljs #"^.*/") a) b)
(if (absolute-path? b)
b
(str "/" b))))
(defn join*
"Join two URI records as per RFC 3986. Handles relative URIs."
[base ref]
(if (:scheme ref)
(update ref :path remove-dot-segments)
(-> (if (:host ref)
(assoc ref
:scheme (:scheme base)
:query (:query ref))
(if (nil? (:path ref))
(assoc base :query (some :query [ref base]))
(assoc base :path
(remove-dot-segments
(if (absolute-path? (:path ref))
(:path ref)
(merge-paths (:path base) (:path ref))))
:query (:query ref))))
(assoc :fragment (:fragment ref)))))
(defn join
"Joins any number of URIs as per RFC3986. Arguments can be strings, they will
be coerced to URI records."
[& uris]
(reduce join* (map uri uris)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Query strings
(defn- decode-param-pair [param]
(let [[k v] (str/split param #"=" 2)]
[(if k (normalize/percent-decode k) "")
(if v (normalize/percent-decode (str/replace v #"\+" " ")) "")]))
(defn query-string->map
"Parse a query string, consisting of key=value pairs, separated by \"&\". Takes
the following options:
- `:keywordize?` whether to turn return keys as keywords. Defaults to `true`.
- `:multikeys` how to handle the same key occuring multiple times, defaults to
`:duplicates`
- `:into` provide `clojure.lang.IPersistentMap` to define target data structure,
defaults to `clojure.lang.PersistentHashMap`
The possible values for `:multikeys` are
- `:never` always return a single value for a key. The rightmost value
\"wins\"
- `:always` return a map with vectors as values, with successive
values of the same key in order
- `:duplicates` return a vector for keys that occur multiple times, or a
string otherwise"
([q]
(query-string->map q nil))
([q {:keys [multikeys keywordize? into]
:or {multikeys :duplicates
keywordize? true}}]
(if (str/blank? q)
into
(->> (str/split q #"&")
(map decode-param-pair)
(reduce
(fn [m [k v]]
(let [k (if keywordize? (keyword k) k)]
(case multikeys
:never
(assoc m k v)
:always
(if (contains? m k)
(update m k conj v)
(assoc m k [v]))
:duplicates
(if (contains? m k)
(if (vector? (m k))
(update m k conj v)
(assoc m k [(m k) v]))
(assoc m k v)))))
(or into {}))))))
(defn query-string->seq
"Parse a query string, consisting of key=value pairs, separated by `\"&\"`.
Return a seq of `[key value]` pairs, in the exact order they occur in the
query string. Keys and values are strings. Returns `nil` if `q` is blank or
`nil`."
[q]
(when-not (str/blank? q)
(map decode-param-pair (str/split q #"&"))))
(defn query-map
"Return the query section of a URI as a map. Will coerce its argument
with [[uri]]. Takes an options map, see [[query-string->map]] for options."
([uri]
(query-map uri nil))
([u opts]
(query-string->map (:query (uri u)) opts)))
(defn query-encode
"Percent encoding for query strings. Will percent-encode values that are
reserved in query strings only. Encodes spaces as +."
[s]
(let [encode-char #(cond
(= " " %)
"+"
(re-find #"[^a-zA-Z0-9\-\._~@\/]" %)
(normalize/percent-encode %)
:else
%)]
(->> (normalize/char-seq s)
(map encode-char)
(apply str))))
(defn- encode-param-pair [k v]
(str (query-encode
(cond
(simple-ident? k)
(name k)
(qualified-ident? k)
(str (namespace k) "/" (name k))
:else (str k)))
"="
(query-encode (str v))))
(defn map->query-string
"Convert a map into a query string, consisting of key=value pairs separated by
`&`. The result is percent-encoded so it is always safe to use. Keys can be
strings or keywords. If values are collections then this results in multiple
entries for the same key. `nil` values are ignored. Values are stringified."
[m]
(when (seq m)
(->> m
(mapcat (fn [[k v]]
(cond
(nil? v)
[]
(coll? v)
(map (partial encode-param-pair k) v)
:else
[(encode-param-pair k v)])))
(interpose "&")
(apply str))))
(defn seq->query-string
"Convert a positional sequence of `[key value]` tuples into a query string,
consisting of key=value pairs separated by `&`. The result is percent-encoded,
so it is always safe to use. Keys can be strings or keywords. Tuples with
`nil` values adds entries like `key=` to query string. Values are
stringified."
[pairs]
(->> pairs
(map (fn [[k v]] (encode-param-pair k v)))
(interpose "&")
(apply str)))
(defn assoc-query*
"Add additional query parameters to a URI. Takes a URI (or coercible to URI) and
a map of query params."
[u m]
(let [u (uri u)]
(assoc u :query (map->query-string (merge (query-map u) m)))))
(defn assoc-query
"Add additional query parameters to a URI. Takes a URI (or coercible to URI)
followed key value pairs.
(assoc-query \"http://example.com?id=1&name=John\" :name \"Jack\" :style \"goth\")
;;=> #lambdaisland/uri \"http://example.com?id=1&name=Jack&style=goth\" "
[u & {:as kvs}]
(assoc-query* u kvs))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Predicates
(defn relative?
"Is the URI relative? Returns true if the URI does not have a scheme (protocol)."
[uri]
(nil? (:scheme uri)))
(def
^{:doc
"Is the URI absolute? Returns true if the URI has a scheme (protocol), and hence also an origin."}
absolute? (complement relative?))
(defn uri?
"Check if `o` is URI instance."
[o]
(instance? URI o))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; EDN
(def edn-tag 'lambdaisland/uri)
#?(:clj
(defmethod print-method URI [^URI this ^java.io.Writer writer]
(.write writer "#")
(.write writer (str edn-tag))
(.write writer " ")
(.write writer (prn-str (.toString this))))
:cljs
(extend-type URI
IPrintWithWriter
(-pr-writer [this writer _opts]
(write-all writer "#" (str edn-tag) " " (prn-str (.toString this))))))
(def
^{:doc
"A map that can be passed to clojure.edn/read, so tagged URI literals are
read back correctly."}
edn-readers {edn-tag parse})