Created
June 23, 2017 13:57
-
-
Save thetristan/61bdce030624af77550fe6357a3a44b4 to your computer and use it in GitHub Desktop.
libcurl w/ scala-native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍