-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathriku.elv
218 lines (186 loc) · 7.76 KB
/
riku.elv
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
use str
use file
var version = '0.2.0'
fn -parse-bool {|val|
if (eq (str:to-lower $val) "true") {
put $true
} elif (eq (str:to-lower $val) "true") {
put $false
} else {
put $val
}
}
# See https://curl.haxx.se/docs/http-cookies.html for more detail
fn -parse-cookies {|lines|
for l $lines {
if (and (not (str:has-prefix $l '#')) (not-eq (count $l) (num 0))) {
var domain subdomains path secure expires name value = (str:split "\t" $l)
put [
&domain=$domain
&subdomains=(-parse-bool $subdomains)
&path=$path
&secure=(-parse-bool $secure)
&expires=$expires
&name=$name
&value=$value
]
}
}
}
fn -parse-headers {|lines|
var headers = [&]
for l $lines {
var l = (str:replace "\r" '' $l)
if (and (not (str:has-prefix $l "HTTP")) (not-eq (count $l) 0)) {
var key @value = (str:split ':' $l)
var value = (str:trim-space (str:join ':' $value))
set headers[$key] = $value
}
}
put $headers
}
fn request {|method url
¶ms=$nil
&data=$nil
&headers=$nil
&cookies=$nil
&files=$nil
&auth=$nil
&timeout=$nil
&allow_redirects=$true
&proxies=$nil
&verify=$true
&cert=$nil|
var session-path = (mktemp -d riku-XXXXXXXXXXXXXXXXXXXX --tmpdir)
var output-file = $session-path"/"output
var headers-file = $session-path"/"headers
var cookies-file = $session-path"/"cookies
# --disable Disables loading of a .curlrc
var curl_args = ['--silent' '--show-error' '--disable'
'--user-agent' 'riku/'$version
'--output' $output-file
'--dump-header' $headers-file
'--cookie-jar' $cookies-file
'-w' '%{http_code}\n%{time_total}\n%{url_effective}\n'
]
if (eq $method "GET") {
set curl_args = [$@curl_args "-G"]
} elif (eq $method "HEAD") {
set curl_args = [$@curl_args "-I"]
} else {
set curl_args = [$@curl_args "-X" $method]
}
if (not-eq $params $nil) {
keys $params | each {|k|
set curl_args = [$@curl_args '-d' $k'='$params[$k]]
}
}
if (not-eq $data $nil) {
if (eq (kind-of $data) "string") {
set curl_args = [$@curl_args '-d' $data]
} elif (eq (kind-of $data) "list") {
set curl_args = [$@curl_args '-d' $data]
} elif (eq (kind-of $data) "map") {
set curl_args = [$@curl_args '-H' 'Content-Type: application/json'
'--data-raw' (put $data | to-json)]
}
}
if (not-eq $headers $nil) {
keys $headers | each {|k|
set curl_args = [$@curl_args '-H' $k': '$headers[$k]]
}
}
if (not-eq $cookies $nil) {
var cookie_string = ""
keys $cookies | each {|k|
set cookie_string = $cookie_string$k'='$cookies[$k]";"
}
set curl_args = [$@curl_args '--cookie' $cookie_string]
}
if (not-eq $files $nil) {
fail "riku: &files Not implemented"
}
if (not-eq $auth $nil) {
fail "riku: &auth Not implemented"
}
if (not-eq $timeout $nil) {
if (eq (kind-of $timeout) "string") {
set curl_args = [$@curl_args "--max-time" $timeout]
} elif (eq (kind-of $timeout) "list") {
var connect_timeout read_timeout = $@timeout
set curl_args = [$@curl_args "--connect-timeout" $connect_timeout]
# add connect and read timeout together since --max-time is
# the maximum time allowed for the entire command
set curl_args = [$@curl_args "--max-time" (+ $connect_timeout $read_timeout)]
}
}
if (eq $allow_redirects $true) {
set curl_args = [$@curl_args "--location"]
}
if (not-eq $proxies $nil) {
fail "riku: &proxies Not implemented"
}
if (eq $verify $false) {
set curl_args = [$@curl_args "--insecure"]
} elif (eq (kind-of $verify "string")) {
if ?(test -d $verify) {
set curl_args = [$@curl_args "--capath" $verify]
} else {
set curl_args = [$@curl_args "--cacert" $verify]
}
}
if (not-eq $cert $nil) {
var cert_arg = ""
if (eq (kind-of $cert) "string") {
set cert_arg = $cert
} elif (eq (kind-of $cert) "list") {
var certificate password = $@cert
set cert_arg = $certificate":"$password
}
set curl_args = [$@curl_args "--cert" $cert_arg]
}
var status_code total_time effective_url = (e:curl $url $@curl_args)
var status_ok = $false
if (< $status_code 400) {
set status_ok = $true
}
var f = (file:open $output-file)
var content = (str:join "\n" [(cat < $f)])
file:close $f
var f = (file:open $headers-file)
var headers = (-parse-headers [(cat < $f)])
file:close $f
var f = (file:open $cookies-file)
var cookies = [(-parse-cookies [(cat < $f)])]
file:close $f
var response = [
&content=$content
&cookies=$cookies
&elapsed=$total_time
&headers=$headers
&ok=$status_ok
&status_code=$status_code
&url=$effective_url
]
put $response
e:rm -r $session-path
}
# All convenience functions have the full siganture until https://b.elv.sh/584 is implemented
fn head {|url ¶ms=$nil &data=$nil &headers=$nil &cookies=$nil &files=$nil &auth=$nil &timeout=$nil &allow_redirects=$true &proxies=$nil &verify=$true &cert=$nil|
request "HEAD" $url ¶ms=$params &data=$data &headers=$headers &cookies=$cookies &files=$files &auth=$auth &timeout=$timeout &allow_redirects=$allow_redirects &proxies=$proxies &verify=$verify &cert=$cert
}
fn get {|url ¶ms=$nil &data=$nil &headers=$nil &cookies=$nil &files=$nil &auth=$nil &timeout=$nil &allow_redirects=$true &proxies=$nil &verify=$true &cert=$nil|
request "GET" $url ¶ms=$params &data=$data &headers=$headers &cookies=$cookies &files=$files &auth=$auth &timeout=$timeout &allow_redirects=$allow_redirects &proxies=$proxies &verify=$verify &cert=$cert
}
fn post {|url ¶ms=$nil &data=$nil &headers=$nil &cookies=$nil &files=$nil &auth=$nil &timeout=$nil &allow_redirects=$true &proxies=$nil &verify=$true &cert=$nil|
request "POST" $url ¶ms=$params &data=$data &headers=$headers &cookies=$cookies &files=$files &auth=$auth &timeout=$timeout &allow_redirects=$allow_redirects &proxies=$proxies &verify=$verify &cert=$cert
}
fn put {|url ¶ms=$nil &data=$nil &headers=$nil &cookies=$nil &files=$nil &auth=$nil &timeout=$nil &allow_redirects=$true &proxies=$nil &verify=$true &cert=$nil|
request "PUT" $url ¶ms=$params &data=$data &headers=$headers &cookies=$cookies &files=$files &auth=$auth &timeout=$timeout &allow_redirects=$allow_redirects &proxies=$proxies &verify=$verify &cert=$cert
}
fn patch {|url ¶ms=$nil &data=$nil &headers=$nil &cookies=$nil &files=$nil &auth=$nil &timeout=$nil &allow_redirects=$true &proxies=$nil &verify=$true &cert=$nil|
request "PATCH" $url ¶ms=$params &data=$data &headers=$headers &cookies=$cookies &files=$files &auth=$auth &timeout=$timeout &allow_redirects=$allow_redirects &proxies=$proxies &verify=$verify &cert=$cert
}
fn delete {|url ¶ms=$nil &data=$nil &headers=$nil &cookies=$nil &files=$nil &auth=$nil &timeout=$nil &allow_redirects=$true &proxies=$nil &verify=$true &cert=$nil|
request "DELETE" $url ¶ms=$params &data=$data &headers=$headers &cookies=$cookies &files=$files &auth=$auth &timeout=$timeout &allow_redirects=$allow_redirects &proxies=$proxies &verify=$verify &cert=$cert
}