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: Re: Files on a server
Date: Wed, 16 May 2018 23:45:08 +1200	[thread overview]
Message-ID: <406b185d-8476-d488-5391-c34482066a33@gmail.com> (raw)
In-Reply-To: <D3B195ED-6921-4EAD-8E70-1554A8060EB1@ziggo.nl>

On 05/16/2018 11:23 PM, Hans van der Meer wrote:
> I would be satisfied when a returned 404 error code will be handled
> within a reasonable (configurable) time delay. As for redirection, there
> I would not mind if it is not included.
> Restrictions like that are not a problem for me, because this is for
> building a pdf that accesses many internet located files. The check on
> existence is therefore especially meant for spotting missing or misnamed
> files.

You can use libcurl via ffi: https://curl.haxx.se/libcurl/

\startluacode
local ffi = require("ffi")

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
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 = ffi.load("curl")

local function discard(buffer, size, nmemb, userp)
    return size * nmemb
end

function check_status(url)
    lcurl.curl_global_init(CURL_GLOBAL_DEFAULT)

    local curl = lcurl.curl_easy_init()

    lcurl.curl_easy_setopt(curl, CURLOPT_URL, url)
    lcurl.curl_easy_setopt(curl, CURLOPT_NOBODY, ffi.cast("long",1))
    lcurl.curl_easy_setopt(curl, CURLOPT_HEADER, ffi.cast("long",1))
    lcurl.curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
ffi.cast("callback",discard));

    local res = lcurl.curl_easy_perform(curl)
    local c_http_code = ffi.new("long[1]")
    lcurl.curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, c_http_code);

    lcurl.curl_easy_cleanup(curl)
    lcurl.curl_global_cleanup()

    return tonumber(c_http_code[0])
end
\stopluacode

\starttext

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

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

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

\stoptext

> 
> Hans van der Meer
> 
>> On 16 May 2018, at 12:41, Henri Menke <henrimenke@gmail.com
>> <mailto:henrimenke@gmail.com>> wrote:
>>
>> On 05/16/2018 09:31 PM, Hans van der Meer wrote:
>>> I tried to determine the existence of a file on the internet. See the
>>> following macro call:
>>>
>>>         % Test if file exists.
>>>         \edef\theurl{\linkprotocol://\urlbase\xmlatt{#1}{link}\thesuffix}
>>>         \doiffileelse
>>>         {\theurl}
>>>         {\verbose{HVDM-PEV-TEST}{file exists: \theurl}}
>>>         {\verbose{HVDM-PEV-TEST}{file does not exist: \theurl}}
>>>
>>> It seems that \doiffileelse always results in true except for local
>>> files where nonexistence is correctly handled.
>>>
>>> For example the following url definitely does not exist but nevertheless
>>> \doiffileelse reports its existence:
>>> HVDM-PEV-TEST   > file exists:
>>> http://hansvandermeer.myqnapcloud.com/archive/denhaag/hga-dtb-1869-6040.pdf
>>>
>>> Is it possible that the \doiffile macros are extended for files on the
>>> internet or a correponding set \doifurl is developed?
>>
>> I'm not speaking for the devs here, but I don't think that this is going
>> to be included.  It would require that all webpages send proper 404
>> codes and timeout after reasonable delay.  Also how would you handle
>> redirects?  Should the ConTeXt run be stalled because checking for a
>> files existence on the web is caught in a redirect loop?
>>
>>>
>>> Hans van der Meer
>>>
>>>
>>>
>>> ___________________________________________________________________________________
>>> If your question is of interest to others as well, please add an
>>> entry to the Wiki!
>>>
>>> maillist : ntg-context@ntg.nl <mailto: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
>>> ___________________________________________________________________________________
>>>
>>
>> ___________________________________________________________________________________
>> If your question is of interest to others as well, please add an entry
>> to the Wiki!
>>
>> maillist : ntg-context@ntg.nl
>> <mailto:ntg-context@ntg.nl> / http://www.ntg.nl/mailman/listinfo/ntg-context
>> webpage  : http://www.pragma-ade.nl
>> <http://www.pragma-ade.nl/> / http://context.aanhet.net
>> <http://context.aanhet.net/>
>> archive  : https://bitbucket.org/phg/context-mirror/commits/
>> wiki     : http://contextgarden.net <http://contextgarden.net/>
>> ___________________________________________________________________________________
> 
> 
> 
> ___________________________________________________________________________________
> 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
> ___________________________________________________________________________________
> 

___________________________________________________________________________________
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-16 11:45 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-16  9:31 Hans van der Meer
2018-05-16 10:41 ` Henri Menke
2018-05-16 11:23   ` Hans van der Meer
2018-05-16 11:45     ` Henri Menke [this message]
2018-05-16 12:23       ` Taco Hoekwater
2018-05-16 12:58         ` Hans van der Meer
2018-05-16 13:41           ` Hans Hagen
2018-05-16 14:05             ` Taco Hoekwater
2018-05-17  7:32         ` Ulrike Fischer
2018-05-17  7:39           ` Taco Hoekwater
2018-05-17  7:52             ` Hans van der Meer
2018-05-17  7:54             ` Taco Hoekwater
2018-05-17  8:30               ` Ulrike Fischer
2018-05-17  8:42               ` Hans Hagen
2018-05-17  8:46               ` luigi scarso
2018-05-17  8:20             ` Ulrike Fischer
2018-05-17  8:31               ` Taco Hoekwater
2018-05-17  9:03                 ` Taco Hoekwater
2018-05-17  9:49                   ` Henri Menke

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=406b185d-8476-d488-5391-c34482066a33@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).