-
Notifications
You must be signed in to change notification settings - Fork 11
/
uri.clj
72 lines (63 loc) · 1.7 KB
/
uri.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
(ns net.http.uri
(:require [clojure.string :as str])
(:import java.net.URI
io.netty.handler.codec.http.QueryStringEncoder))
(defn ^URI encode
"Produce a valid URI from arguments found in a request map"
[^URI uri query]
(if (seq query)
(let [qse (QueryStringEncoder. (str uri))]
(doseq [[k v] query]
(if (sequential? v)
(doseq [subv v]
(.addParam qse (name k) (str subv)))
(.addParam qse (name k) (str v))))
(.toUri qse))
uri))
(defn raw-path
[^URI uri]
(.getRawPath uri))
(defn raw-query
[^URI uri]
(.getRawQuery uri))
(defn host
[^URI uri]
(.getHost uri))
(defn ^String encoded-path
[^URI uri query]
(let [encoded (encode uri query)
raw-q (raw-query uri)]
(cond-> (raw-path encoded)
(seq raw-q) (str "?" raw-q))))
(defn ^String scheme
([^URI uri]
(when-let [s (.getScheme uri)]
(str/lower-case s)))
([^URI uri ^String default]
(or (scheme uri) default)))
(defn port
[^URI uri]
(let [p (.getPort uri)]
(when-not (= p -1)
p)))
(defn ^URI ->uri
[input]
(cond
(string? input) (URI. input)
(instance? URI input) input
:else (throw (IllegalArgumentException.
"Invalid URI provided"))))
(defn parse
([input params]
(let [uri (encode (->uri input) params)
uri-port (port uri)
uri-scheme (scheme uri)]
{:uri uri
:host (host uri)
:scheme uri-scheme
:port (cond (some? uri-port) uri-port
(= "http" uri-scheme) 80
(= "https" uri-scheme) 443)
:ssl? (= "https" uri-scheme)}))
([input]
(parse input {})))