ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Henri Menke <henrimenke@gmail.com>
To: "ntg-context@ntg.nl >> mailing list for ConTeXt users"
	<ntg-context@ntg.nl>
Subject: libcurl via FFI
Date: Thu, 17 May 2018 22:11:12 +1200	[thread overview]
Message-ID: <eb3ad313-236f-40cb-4e83-7ffcc4135c3f@gmail.com> (raw)

[-- 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
___________________________________________________________________________________

                 reply	other threads:[~2018-05-17 10:11 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=eb3ad313-236f-40cb-4e83-7ffcc4135c3f@gmail.com \
    --to=henrimenke@gmail.com \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).