Skip to content

Instantly share code, notes, and snippets.

@thetristan
Created June 23, 2017 13:57
Show Gist options
  • Save thetristan/61bdce030624af77550fe6357a3a44b4 to your computer and use it in GitHub Desktop.
Save thetristan/61bdce030624af77550fe6357a3a44b4 to your computer and use it in GitHub Desktop.
libcurl w/ scala-native
import scala.scalanative.native
import native._
@native.link("curl")
@native.extern object curl {
import CurlTypes._
@native.name("curl_global_init")
def globalInit(flags: native.CLong): native.Ptr[Byte] = native.extern
@native.name("curl_easy_init")
def easyInit(): native.Ptr[Byte] = native.extern
@native.name("curl_easy_setopt")
def easySetOpt(handle: native.Ptr[Byte], option: native.UInt, args: native.CVararg*): native.UInt = native.extern
@native.name("curl_easy_perform")
def easyPerform(handle: native.Ptr[Byte]): native.UInt = native.extern
@native.name("curl_slist_append")
def slistAppend(slist: CurlSlist, data: native.CString): CurlSlist = native.extern
@native.name("curl_slist_free_all")
def slistFreeAll(slist: CurlSlist): Unit = native.extern
}
object CurlTypes {
type CurlSlist = native.Ptr[native.CStruct2[CString, native.Ptr[Byte]]]
}
object CurlOpts {
private def longOffset(offset: Int) = offset.toUInt
private def stringOffset(offset: Int) = (offset + 10000).toUInt
private def objectOffset(offset: Int) = (offset + 10000).toUInt
val Url: native.UInt = stringOffset(2)
val Headers: native.UInt = objectOffset(23)
val Verbose: native.UInt = longOffset(41)
}
object Hello extends App {
native.Zone { implicit z =>
val headers = curl.slistAppend(null, c"User-Agent: scala-native-with-libcurl/0.0.0")
curl.slistAppend(headers, c"Test-Header: foobarbaz")
val curlHandle = curl.easyInit()
curl.easySetOpt(curlHandle, CurlOpts.Verbose, 1)
curl.easySetOpt(curlHandle, CurlOpts.Headers, headers)
curl.easySetOpt(curlHandle, CurlOpts.Url, c"http://www.google.com")
curl.easyPerform(curlHandle)
curl.slistFreeAll(headers)
}
}
@ahjohannessen
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment