VPcURL revised

Share Tips, Code Samples, etc. with the Visual Prolog community.
Post Reply
User avatar
Jan de Lint
VIP Member
Posts: 235
Joined: 6 Mar 2000 0:01

VPcURL revised

Post by Jan de Lint » 6 Oct 2010 19:28

Code: Select all

class predicates     http_simple_next : vPcURL::connectionReturnHandlerP. clauses     http_simple() :-   % http simple download         C1 = vPcURL::new(http_simple_next),         C1:curl_easy_setopt(cURLOPT_URL, s("http://192.168.178.24/tkal2009.css")),         C1:curl_easy_perform().       http_simple_next(_Returncode, Conn) :-         Content = Conn:getOutput(),         stdIO::write("\n",Content).
Need foreground processing (do nothing until curl is finished), use vPcURLforeground:

Code: Select all

class predicates     http_simple_next : vPcURL::connectionReturnHandlerP. clauses     http_simple() :-   % http simple download         C1 = vPcURLforeground::new(http_simple_next),         C1:curl_easy_setopt(cURLOPT_URL, s("http://192.168.178.24/tkal2009.css")),         C1:curl_easy_perform().       http_simple_next(_Returncode, Conn) :-         Content = Conn:getOutput(),         stdIO::write("\n",Content).
As you can see, the structure is the same in both situations.
Curl debug output can be caught by setting C1:setDebugOn() and DebugInfo = Conn:getDebugOutput(). Debug output also contains protocol header info.
This header info can also be retrieved separately by C1:setHeadersOn() and HeaderInfo = Conn:getHeadersOutput().
More sophisticated (binary upload/download, cascaded requests, http posting, etc.) examples are included.
What can Curl do?
Curl is an Internet protocol handler and is also able to do authentication. Secure connections e.g. https are also supported.
For the moment I would like to refer to the examples package and to the Curl website: http://curl.haxx.se/.
VPcURL supports all curl features, a number of them is in my opinion deprecated by the added VIP features, especially multithreading (use of curl-multi is disencouraged). The required Windows binaries (dll's) can be downloaded from the download page: http://curl.haxx.se/download.html.
Have fun!
Attachments
VPcURL.zip
VPcURL package v2
(21.67 KiB) Downloaded 739 times
zlib.zip
zip package
(3.03 KiB) Downloaded 828 times
CurlExample.zip
examples package v2
(6.08 KiB) Downloaded 859 times
Last edited by Jan de Lint on 27 Apr 2011 20:44, edited 4 times in total.

User avatar
Jan de Lint
VIP Member
Posts: 235
Joined: 6 Mar 2000 0:01

Constructors and URL's

Post by Jan de Lint » 7 Oct 2010 19:28

There are four types of constructors:

Code: Select all

Conn = vPcURL::new(http_download_next) % get result in memory Conn = vPcURL::new("tkal2009.css", http_download_next) % get result in a file Conn = vPcURLforeground::new(http_download_next) % get result in memory Conn = vPcURLforeground::new("tkal2009.css", http_download_next) % get result in a file
The callback (e.g. http_download_next) to process the result after the transfer has taken place is always required. Note that the callback can be an empty function.
The next thing to do is to set the protocol and the url - examples:

Code: Select all

- Conn:curl_easy_setopt(cURLOPT_URL, s("http://192.168.178.24/tkal2009.css")) - Conn:curl_easy_setopt(cURLOPT_URL, s("https://192.168.178.24/tkal2009.css")) - Conn:curl_easy_setopt(cURLOPT_URL, s("ftp://192.168.178.24/tkal2009.css"))
Curl supports many protocols, even SMTP and POP, and is able to do authentication. For a complete list, see the Curl website. Protocols generally come with specific "header" information (up and downstream). Curl recognizes what is data and what is header info. Normally it generates headers, it transfers the data and keeps the recieved header info for itself. But it allows you to put a tap on the recieved header info:

Code: Select all

- Conn:setHeadersOn(),
and in the callback:

Code: Select all

ReceivedHeaders = Conn:getHeadersOutput(),

User avatar
Jan de Lint
VIP Member
Posts: 235
Joined: 6 Mar 2000 0:01

callback

Post by Jan de Lint » 10 Oct 2010 10:38

Callback.
All constructors need a callback. The callback is performed after Curl has made a transfer to/from the URL. The function domain of a callback is

Code: Select all

connectionReturnHandlerP = (curlcode Returncode, vPcURL Connection) procedure.
The returncode lets you decide whether you want to trust the result or not. If the returncode equals cURLE_OK, the output can be trusted otherwise a network error may have occurred. The connection gives access to the object. Taken from the Curl examples:

Code: Select all

example_callback(cURLE_OK, Conn) :-     [Received|_] = Conn:getOutput(codepage(codePageId::codePage_iso88591)),  % download codepage is ISO-8859-1     stdio::write("\n",Received,"\n-----------"),     X = Conn:curl_easy_getinfoS(cURLINFO_EFFECTIVE_URL, ResultUrl),     stdIO::write("\nUrl in effect: ", ResultUrl,"  curl-returncode= ",  X ), % gives the resulting new URL if location was changed/followed     _ = Conn:curl_easy_getinfoI(cURLINFO_RESPONSE_CODE, ResultCode),     stdIO::write("\nHttp responsecode: ", ResultCode ). example_callback(_CurlCode, _Conn) :-     stdio::write("Network error while performing example.").
Download in memory (constructor/1) can be retrieved by either:

Code: Select all

Conn:getOutput() -> string % the budget version Conn:getOutput(codepage) -> stringlist % deluxe Conn:getOutputBin() -> binary % most versatile

User avatar
Jan de Lint
VIP Member
Posts: 235
Joined: 6 Mar 2000 0:01

Housekeeping

Post by Jan de Lint » 13 Oct 2010 11:25

The following Curl commands are now given automatically:

Code: Select all

curl_easy_init() curl_easy_cleanup() curl_formfree()          % free a previously built form POST curl_slist_free_all()     % frees a whole curl_slist as made with curl_slist_append()
They have been removed from the VPcURL interface.
Also, do NOT use any of the curl commands to establish inputs or outputs. VPcURL does the opening and closing of streams for you.
The download-stream is most of the time handled by the constructor, and if applicable the upload-stream is set by

Code: Select all

setInput : (string) determ. setInputBin : (binary). % in case of upload from memory.
The file-streams are closed just before returning to the callback and the memory-streams are closed after performing the callback. That is why the callback has to be a procedure.

User avatar
Jan de Lint
VIP Member
Posts: 235
Joined: 6 Mar 2000 0:01

Post by Jan de Lint » 27 Apr 2011 20:25

Code: Select all

 stdIO::write("\nHttp responsecode: ", ResultCode),
should be replaced by something like:

Code: Select all

 write_to_ui("\nHttp responsecode: ", ResultCode),
Where write_to_ui is as follows:

Code: Select all

class predicates     write_to_ui : (...).   % the ellipsis are real! clauses     write_to_ui(...) :-         applicationwindow::get():postaction({         :-             stdio::write(...)         }).
The examples have been updated.
Note Please be aware that in a few places in the VPcURL package, output is also still directly written to the windows UI. This is true for error messages and the like. I don't think this does much harm, but time for another (the last?) update to do proper exception processing!

Aliraza03
Posts: 1
Joined: 29 Sep 2014 12:33

Post by Aliraza03 » 29 Sep 2014 12:40

with this implementation, can I also do a https request ? ( https instead of http )

User avatar
Jan de Lint
VIP Member
Posts: 235
Joined: 6 Mar 2000 0:01

Post by Jan de Lint » 30 Sep 2014 7:02

That is supposed to work yes. I never tried it and I don't know how the required certificate is provided. Perhaps you should have Firefox installed first for that.
Also, try the curl command line utility first.
Succes,
]an

Post Reply