From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/100822 Path: news.gmane.org!.POSTED!not-for-mail From: Henri Menke Newsgroups: gmane.comp.tex.context Subject: libcurl via FFI Date: Thu, 17 May 2018 22:11:12 +1200 Message-ID: Reply-To: mailing list for ConTeXt users NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------50A27A26384F5942788BCDAD" X-Trace: blaine.gmane.org 1526551793 31225 195.159.176.226 (17 May 2018 10:09:53 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 17 May 2018 10:09:53 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 To: "ntg-context@ntg.nl >> mailing list for ConTeXt users" Original-X-From: ntg-context-bounces@ntg.nl Thu May 17 12:09:49 2018 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane.org Original-Received: from zapf.boekplan.nl ([5.39.185.232] helo=zapf.ntg.nl) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fJFr2-00080W-75 for gctc-ntg-context-518@m.gmane.org; Thu, 17 May 2018 12:09:48 +0200 Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id BB611292E5; Thu, 17 May 2018 12:11:36 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id x86lv7YloOy4; Thu, 17 May 2018 12:11:36 +0200 (CEST) Original-Received: from zapf.ntg.nl (localhost [IPv6:::1]) by zapf.ntg.nl (Postfix) with ESMTP id 5AC872927D; Thu, 17 May 2018 12:11:35 +0200 (CEST) Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 8E9F52927D for ; Thu, 17 May 2018 12:11:34 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Nu3DDb3r5xlN for ; Thu, 17 May 2018 12:11:29 +0200 (CEST) Original-Received: from mail-pg0-f43.google.com (mail-pg0-f43.google.com [74.125.83.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by zapf.ntg.nl (Postfix) with ESMTPS id 9C44429265 for ; Thu, 17 May 2018 12:11:19 +0200 (CEST) Original-Received: by mail-pg0-f43.google.com with SMTP id v7-v6so1641564pgs.0 for ; Thu, 17 May 2018 03:11:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-language; bh=StFUz0WEKzeCsjgxi0OkN5N/4NDMCqA4ux53t1mJVIA=; b=XXXaqH1oezDd6GYIgjEcAsj5XPlRg8BGYhrC0rYdSNAW5sAtemvzshQeMbNP0v3yBM BPEdgSUjHNLVAs6dLAccS3DaAATpiiZLwjJCpw08xq0c/YgyUjKgvYBhawvsPR6OJk6N jPzba1fsGPEciGqDniiVLhcU/5TMQNI6kwFpLqQFhBplAIUrGNU1ICguqdm8aDHwCetT 7PN9UlhGggo3GElnhuB6Tr3nWmc11jXTCCSNwjmS8wINKDitIwD2WkW2YswPZ5tReQW1 ZjrtSdmBz/APAmNqQXb7A2veW61zZEfCp2tmXuPN3p6T7Q/XsO/1cYDr2jkklDE1BAPE Q5Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=StFUz0WEKzeCsjgxi0OkN5N/4NDMCqA4ux53t1mJVIA=; b=KfvB1J1P3/WovOXNitmaZ3hI9pk1ZNQ3NHIyGFFUNle9bAY/iZEq+m87sI20cKG0vD kQDPpiQIWyT/k49tQ5luNQv2IzvktlHZAZJMCn1mj+KIt/7EkdMvj/bAcGiQPleaq/9b ruKGtEzJPUC8S9iUC9CxP+4YcIhVj4e06t5ZUUlyG1IJpaYMxWFJynONvyb7ChbHkc23 8e4Md113mN8gU6AVIIMKpLOWzZzCCkuz3C21vFt6xLtqXTfVNEuD5HcjO/P4mDri7S8a VDCMPg51OeLf7kE5Owf/oPAABUzHWCRVD7wMqFdetz18qOWO/eT9eVofzhlVz0YUoplg huGQ== X-Gm-Message-State: ALKqPweI/a8xStzA8d6JfKsdCwmPmEasfIR5VZMF54Ku6dZtdKrIrY0m tMlG5s3ZFES1feR2JyC8E9ODB7T+ X-Google-Smtp-Source: AB8JxZpY7hwpxUV+vaZGTjPU6iY7CQAq4FnQYTZ8RZTdpHFM1SY7gDfkKm1qow2AkARRIAyqmABX8Q== X-Received: by 2002:a63:81c7:: with SMTP id t190-v6mr3596090pgd.293.1526551877417; Thu, 17 May 2018 03:11:17 -0700 (PDT) Original-Received: from ?IPv6:2406:e007:a910:1:ed19:7a64:4c5b:fb5e? ([2406:e007:a910:1:ed19:7a64:4c5b:fb5e]) by smtp.gmail.com with ESMTPSA id y83-v6sm11384298pfd.21.2018.05.17.03.11.15 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 17 May 2018 03:11:16 -0700 (PDT) Content-Language: de-DE X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.16 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ntg-context-bounces@ntg.nl Original-Sender: "ntg-context" Xref: news.gmane.org gmane.comp.tex.context:100822 Archived-At: This is a multi-part message in MIME format. --------------50A27A26384F5942788BCDAD Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit 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 --------------50A27A26384F5942788BCDAD Content-Type: text/x-tex; name="curl.tex" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="curl.tex" \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 --------------50A27A26384F5942788BCDAD Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX18KSWYgeW91ciBxdWVzdGlvbiBpcyBvZiBpbnRlcmVz dCB0byBvdGhlcnMgYXMgd2VsbCwgcGxlYXNlIGFkZCBhbiBlbnRyeSB0byB0aGUgV2lraSEKCm1h aWxsaXN0IDogbnRnLWNvbnRleHRAbnRnLm5sIC8gaHR0cDovL3d3dy5udGcubmwvbWFpbG1hbi9s aXN0aW5mby9udGctY29udGV4dAp3ZWJwYWdlICA6IGh0dHA6Ly93d3cucHJhZ21hLWFkZS5ubCAv IGh0dHA6Ly9jb250ZXh0LmFhbmhldC5uZXQKYXJjaGl2ZSAgOiBodHRwczovL2JpdGJ1Y2tldC5v cmcvcGhnL2NvbnRleHQtbWlycm9yL2NvbW1pdHMvCndpa2kgICAgIDogaHR0cDovL2NvbnRleHRn YXJkZW4ubmV0Cl9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f --------------50A27A26384F5942788BCDAD--