-
Notifications
You must be signed in to change notification settings - Fork 2
/
easy.clj
177 lines (155 loc) · 5.87 KB
/
easy.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
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
(ns clj-curl.easy
(:refer-clojure :exclude [send])
(:import [com.sun.jna NativeLibrary Pointer Memory NativeLong]
[com.sun.jna.ptr PointerByReference DoubleByReference LongByReference]
[clj_curl.Exceptions CurlEasyError])
(:require [clj-curl.opts :as opts]))
(def ^com.sun.jna.NativeLibrary libcurl (com.sun.jna.NativeLibrary/getInstance "curl"))
(defn init
"https://curl.haxx.se/libcurl/c/curl_easy_init.html"
^Pointer
[]
(.invoke (.getFunction libcurl "curl_easy_init") Pointer (to-array [])))
(defn perform
"https://curl.haxx.se/libcurl/c/curl_easy_perform.html"
^Long
[^Pointer curl]
(let [return (.invoke (.getFunction libcurl "curl_easy_perform") Long (to-array [curl]))]
(if (> return opts/e-ok)
(throw (CurlEasyError. return))
return)))
(defn cleanup
"https://curl.haxx.se/libcurl/c/curl_easy_cleanup.html"
[^Pointer curl]
(.invoke (.getFunction libcurl "curl_easy_cleanup") Void (to-array [curl])))
(defn getinfo
"https://curl.haxx.se/libcurl/c/curl_easy_getinfo.html"
^Long
[^Pointer curl ^Long opt param]
(let [return (.invoke (.getFunction libcurl "curl_easy_getinfo") Long (to-array [curl opt param]))]
(if (> return opts/e-ok)
(throw (CurlEasyError. return))
return)))
(defn getinfo-double
^Double
[^Pointer curl ^Long opt]
(let [p-double (DoubleByReference.)
return (.invoke (.getFunction libcurl "curl_easy_getinfo") Long (to-array [curl opt p-double]))]
(if (> return opts/e-ok)
(throw (CurlEasyError. return))
(.getValue p-double))))
(defn getinfo-long
^Long
[^Pointer curl ^Long opt]
(let [p-long (LongByReference.)
return (.invoke (.getFunction libcurl "curl_easy_getinfo") Long (to-array [curl opt p-long]))]
(if (> return (CurlEasyError. return))
(throw (CurlEasyError. return))
(.getValue p-long))))
(defn getinfo-string
^String
[^Pointer curl ^Long opt]
(let [p-str (PointerByReference.)
return (.invoke (.getFunction libcurl "curl_easy_getinfo") Long (to-array [curl opt p-str]))]
(if (> return opts/e-ok)
(throw (CurlEasyError. return))
(let [value (.getValue p-str)]
(if (= value Pointer/NULL) "" value)))))
(defn duphandle
"https://curl.haxx.se/libcurl/c/curl_easy_duphandle.html"
^Pointer
[^Pointer curl]
(.invoke (.getFunction libcurl "curl_easy_duphandle") Pointer (to-array [curl])))
(defn reset
"https://curl.haxx.se/libcurl/c/curl_easy_reset.html"
[^Pointer curl]
(.invoke (.getFunction libcurl "curl_easy_reset") Void (to-array [curl])))
(defn slist-append
"https://curl.haxx.se/libcurl/c/curl_slist_append.html"
^Pointer
[^Pointer slist ^String s]
(.invoke (.getFunction libcurl "curl_slist_append") Pointer (to-array [slist s])))
(defn slist-free-all
"https://curl.haxx.se/libcurl/c/curl_slist_free_all.html"
^Pointer
[^Pointer slist]
(.invoke (.getFunction libcurl "curl_slist_free_all") Void (to-array [slist])))
(defn setopt
"https://curl.haxx.se/libcurl/c/curl_easy_setopt.html"
^Long
[^Pointer curl ^Long opt param]
(if (= (type param) clojure.lang.PersistentVector)
(let [slist (Memory. NativeLong/SIZE)]
(do
(doseq [s param]
(slist-append slist s))
(let [return (.invoke (.getFunction libcurl "curl_easy_setopt") Long (to-array [curl opt slist]))]
(slist-free-all slist)
(if (> return opts/e-ok)
(throw (CurlEasyError. return))
return))))
(let [return (.invoke (.getFunction libcurl "curl_easy_setopt") Long (to-array [curl opt param]))]
(if (> return opts/e-ok)
(throw (CurlEasyError. return))
return))))
(defn escape
"https://curl.haxx.se/libcurl/c/curl_easy_escape.html"
^String
([^Pointer curl ^String s]
(escape curl s (count s)))
([^Pointer curl ^String s ^Long length]
(.invoke (.getFunction libcurl "curl_easy_escape") String (to-array [curl s length]))))
(defn unescape
"https://curl.haxx.se/libcurl/c/curl_easy_unescape.html"
^String
[^Pointer curl ^String s]
(.invoke (.getFunction libcurl "curl_easy_unescape") String (to-array [curl s 0 Pointer/NULL])))
(defn getdate
"https://curl.haxx.se/libcurl/c/curl_getdate.html"
^Long
[^String date-str]
(.invoke (.getFunction libcurl "curl_getdate") String (to-array [date-str Pointer/NULL])))
(defn version
"https://curl.haxx.se/libcurl/c/curl_version.html"
^String
[]
(.invoke (.getFunction libcurl "curl_version") String (to-array [])))
(defn pause
"https://curl.haxx.se/libcurl/c/curl_easy_pause.html"
^Long
[^Pointer curl ^Long bitmask]
(let [return (.invoke (.getFunction libcurl "curl_easy_pause") Long (to-array [curl bitmask]))]
(if (> return opts/e-ok)
(throw (CurlEasyError. return))
return)))
(defn send
"https://curl.haxx.se/libcurl/c/curl_easy_send.html"
^Long
([^Pointer curl buffer ^LongByReference n]
(send curl buffer (count buffer) n))
([^Pointer curl buffer ^Long buflen ^LongByReference n]
(let [return (.invoke (.getFunction libcurl "curl_easy_send") Long (to-array [curl buffer buflen n]))]
(if (> return opts/e-ok)
(throw (CurlEasyError. return))
return))))
(defn recv
"https://curl.haxx.se/libcurl/c/curl_easy_recv.html"
^Long
[^Pointer curl buffer ^Long buflen ^LongByReference n]
(let [return (.invoke (.getFunction libcurl "curl_easy_recv") Long (to-array [curl buffer buflen n]))]
(if (> return opts/e-ok)
(throw (CurlEasyError. return))
return)))
(defn strerror
"https://curl.haxx.se/libcurl/c/curl_easy_strerror.html"
^String
[^Long errornum]
(.invoke (.getFunction libcurl "curl_easy_strerror") String (to-array [errornum])))
(defn upkeep
"https://curl.haxx.se/libcurl/c/curl_easy_upkeep.html"
^Long
[^Pointer curl]
(let [return (.invoke (.getFunction libcurl "curl_easy_upkeep") Long (to-array [curl]))]
(if (> return opts/e-ok)
(throw (CurlEasyError. return))
return)))