summaryrefslogtreecommitdiff
path: root/curl.ha
diff options
context:
space:
mode:
authorJulian Hurst <ark@mansus.space>2024-02-27 01:51:12 +0100
committerJulian Hurst <ark@mansus.space>2024-02-27 01:51:12 +0100
commite6eca1bca427678aefa2cc172444d0be484b2e46 (patch)
tree9566c2dcc43d225275dd8044786f04ad11ee3803 /curl.ha
downloadhacurl-e6eca1bca427678aefa2cc172444d0be484b2e46.tar.gz
Initial commit
Diffstat (limited to 'curl.ha')
-rw-r--r--curl.ha55
1 files changed, 55 insertions, 0 deletions
diff --git a/curl.ha b/curl.ha
new file mode 100644
index 0000000..1f85959
--- /dev/null
+++ b/curl.ha
@@ -0,0 +1,55 @@
+use types::c;
+use fmt;
+
+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 = {
+ match (get("https://harelang.org")) {
+ case void =>
+ yield;
+ case let err: curlerror =>
+ fmt::fatalf("{} returned {} instead of 0", err.0, err.1);
+ };
+};