Skip to content

Instantly share code, notes, and snippets.

@MarcelineVQ
Last active November 7, 2020 01:25
Show Gist options
  • Save MarcelineVQ/7fccdb60330a907fc3d80a87b79cd7b8 to your computer and use it in GitHub Desktop.
Save MarcelineVQ/7fccdb60330a907fc3d80a87b79cd7b8 to your computer and use it in GitHub Desktop.
module Main
import Data.IORef
import Data.Buffer
import Network.Curl.Easy
partial
expect : String -> (1 _ : Maybe t) -> t
expect msg Nothing = idris_crash msg
expect msg (Just val) = val
partial
writeFunction : Buffer -> (itemsize : Int) -> (len : Int) -> IORef Buffer -> PrimIO Int
writeFunction buf itemsize len ref = toPrim $ do
Just newbuf <- concatBuffers [!(readIORef ref),buf]
| Nothing => idris_crash "concatBuffers returned Nothing"
writeIORef ref newbuf
rawSize buf -- for reporting to curl that we used all of buf
partial
main : IO ()
main = do
CURLE_OK <- curl_global_init | c => do
putStrLn $ "error in curl_global_init: " ++ show c
Just cli <- curlEasyInit | Nothing => putStrLn "error in curlEasyInit"
CURLE_OK <- curlEasySetopt cli CURLOPT_URL "https://www.google.com/" | c => do
putStrLn $ "error in curlEasySetopt: " ++ show c
bufferOpt <- newBuffer 0
let buffer = expect "newBuffer returned Nothing" bufferOpt
ref <- newIORef buffer
CURLE_OK <- curlEasySetopt cli CURLOPT_WRITEFUNCTION
(\b,s,l,_ => writeFunction b s l ref) | c => do -- note how we're ignoring the AnyPtr entirely
putStrLn $ "error in curlEasySetopt: " ++ show c
CURLE_OK <- curlEasyPerform cli | c => do
putStrLn $ "error in curlEasyPerform: " ++ show c
buffer <- readIORef ref
bufferSize <- rawSize buffer
body <- getString buffer 0 bufferSize
putStrLn body
freeBuffer buffer
curlEasyCleanup cli
curl_global_cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment