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
|
use types::c;
use fmt;
use os;
export type CURL = opaque;
export type CURLcode = int;
export type CURLINFO = int;
def CURLOPT_URL = 10002;
def CURLINFO_LONG = 0x200000;
def CURLINFO_RESPONSE_CODE = CURLINFO_LONG + 2;
export @symbol("curl_easy_init") fn curl_easy_init() nullable *CURL;
export @symbol("curl_easy_cleanup") fn curl_easy_cleanup(handle: nullable *CURL) void;
export @symbol("curl_easy_setopt") fn curl_easy_setopt(
handle: nullable *CURL,
option: int,
parameter: const *c::char
) CURLcode;
export @symbol("curl_easy_perform") fn curl_easy_perform(easy_handle: nullable *CURL) CURLcode;
export @symbol("curl_easy_getinfo") fn curl_easy_getinfo(
easy_handle: nullable *CURL,
info: CURLINFO,
code: nullable *c::long
) CURLcode;
type curlerror = !(str, int);
fn get(url: str) (void | curlerror) = {
let c = curl_easy_init();
defer curl_easy_cleanup(c);
let c_url = c::fromstr(url);
defer free(c_url);
let res = curl_easy_setopt(c, CURLOPT_URL, c_url);
if (res != 0) {
return ("setopt", res): curlerror;
};
res = curl_easy_perform(c);
if (res != 0) {
return ("perform", res): curlerror;
};
let rc: c::long = 0;
curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &rc);
fmt::println(rc)!;
};
export fn main() void = {
if (len(os::args) != 2) {
fmt::fatalf("USAGE: {} <url>", os::args[0]);
};
match (get(os::args[1])) {
case void =>
yield;
case let err: curlerror =>
fmt::fatalf("{} returned {} instead of 0", err.0, err.1);
};
};
|