-
-
Notifications
You must be signed in to change notification settings - Fork 149
/
http.clj
37 lines (33 loc) · 1 KB
/
http.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
(ns clojure-lsp.http
(:import
(java.io IOException)
(java.net URL)
(javax.net.ssl HttpsURLConnection)))
(set! *warn-on-reflection* true)
(def ^:private connect-timeout-ms 1000)
(def ^:private read-timeout-ms 30000)
(defn test-remote-url! [^String url]
(if-not (.startsWith url "http")
[true]
(let [url (URL. url)
conn ^HttpsURLConnection (.openConnection url)]
(.setConnectTimeout conn connect-timeout-ms)
(.setReadTimeout conn read-timeout-ms)
(try
(.connect conn)
[true]
(catch IOException ex
[false ex])
(finally
(.disconnect conn))))))
(defn request! [^String url]
(try
(let [url (URL. url)
conn ^HttpsURLConnection (.openConnection url)]
(.setConnectTimeout conn connect-timeout-ms)
(.setReadTimeout conn read-timeout-ms)
{:status (.getResponseCode conn)
:body (.getInputStream conn)
:content-type (.getContentType conn)})
(catch Exception ex
{:error ex})))