ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* libcurl via FFI
@ 2018-05-17 10:11 Henri Menke
  0 siblings, 0 replies; only message in thread
From: Henri Menke @ 2018-05-17 10:11 UTC (permalink / raw)
  To: ntg-context@ntg.nl >> mailing list for ConTeXt users

[-- Attachment #1: Type: text/plain, Size: 259 bytes --]

Dear list,

As announced in another thread, here a better annotated example of using
libcurl via FFI in ConTeXt, see attached.  On my old laptop:

context:          3.482 seconds
context --jit:    2.742 seconds
context --jiton:  2.693 seconds

Cheers, Henri


[-- Attachment #2: curl.tex --]
[-- Type: text/x-tex, Size: 2667 bytes --]

\startluacode
local ffi = assert(require("ffi"))

-- Definition copied straight from curl.h
ffi.cdef[[
typedef struct Curl_easy CURL;
typedef int CURLcode;
typedef int CURLoption;
typedef int CURLINFO;
typedef size_t(*callback)(void *buffer, size_t size, size_t nmemb, void *userp);

CURLcode curl_global_init(long flags);
CURL *curl_easy_init(void);
CURLcode curl_easy_setopt(CURL *curl, CURLoption option, ...);
CURLcode curl_easy_perform(CURL *curl);
CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ...);
void curl_easy_cleanup(CURL *curl);
void curl_global_cleanup(void);
]]

-- Magic numbers
-- for more complex applications one should copy the full enums
-- CURLcode, CURLoption, and CURLINFO into ffi.cdef
local CURLE_OK = 0
local CURL_GLOBAL_DEFAULT = 3
local CURLOPT_URL = 10002
local CURLOPT_NOBODY = 44
local CURLOPT_HEADER = 42
local CURLOPT_WRITEFUNCTION = 20011
local CURLINFO_RESPONSE_CODE = 0x200000 + 2

local lcurl = assert(ffi.load("curl"))

-- Callback function for data received by the request
local function discard(buffer, size, nmemb, userp)
    return size * nmemb
end

-- Initialize the curl session
lcurl.curl_global_init(CURL_GLOBAL_DEFAULT)
-- Make the cleanup function available globally so we can call it when
-- ConTeXt is done
curl_global_cleanup = lcurl.curl_global_cleanup

function curl_check_status(url)
    -- Start a libcurl easy session
    local curl = assert(lcurl.curl_easy_init())

    -- URL to work on
    assert(CURLE_OK == lcurl.curl_easy_setopt(curl, CURLOPT_URL, url))
    -- Do not get the body contents
    assert(CURLE_OK == lcurl.curl_easy_setopt(curl, CURLOPT_NOBODY, ffi.cast("long",1)))
    -- Include the header in the body output
    assert(CURLE_OK == lcurl.curl_easy_setopt(curl, CURLOPT_HEADER, ffi.cast("long",1)))
    -- Callback for writing data
    assert(CURLE_OK == lcurl.curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ffi.cast("callback",discard)))

    -- perform a blocking file transfer
    assert(CURLE_OK == lcurl.curl_easy_perform(curl))

    -- extract last received response code from a curl handle
    local c_http_code = ffi.new("long[1]")
    assert(CURLE_OK == lcurl.curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, c_http_code))

    -- End a libcurl easy handle
    lcurl.curl_easy_cleanup(curl)

    -- Convert C long* to Lua number
    return tonumber(c_http_code[0])
end
\stopluacode

\appendtoks
    \ctxlua{curl_global_cleanup()}
\to \everystoptext

\starttext

HTTP: \ctxlua{context(curl_check_status("http://example.com/"))}

HTTPS: \ctxlua{context(curl_check_status("https://example.com/"))}

404: \ctxlua{context(curl_check_status("https://example.com/xyz"))}

\stoptext

[-- Attachment #3: Type: text/plain, Size: 492 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2018-05-17 10:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-17 10:11 libcurl via FFI Henri Menke

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).