FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log inLog in 


VPcURL revised

Post new topic   Reply to topic    discuss.visual-prolog.com Forum Index -> Visual Prolog Tips & Samples
View previous topic :: View next topic  
Author Message
Jan de Lint



The Netherlands
Joined: 06 Mar 2000
Posts: 388

PostPosted: 6 Oct 2010 19:28    Post subject: VPcURL revised Reply with quote

Change 27-4-2011 : the VP monitor class (nice stuff!) replaces Windows postaction. Courtesy Gildas Ménier. That way VPcURL can also run as a console application. A change in the callback is needed in order to write callback messages to windows UI. That is all there is to it, see below.
------
This new VPcURL package makes the old one obsolete: it not only faciltates more Curl functions, it also takes care of all required Curl housekeeping. Stream management, unchecked converts etc. are also dealt with. It compiles with the student edition of VP.
It exploits and combines the connectivety of Curl and the OO structure and multithreading of VP. Imbedding in your program has become a snap: a curl_init statement early in your program should be all that is needed.
A minimal complete example is

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:

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!



CurlExample.zip
 Description:
examples package v2

Download
 Filename:  CurlExample.zip
 Filesize:  6.08 KB
 Downloaded:  814 Time(s)


VPcURL.zip
 Description:
VPcURL package v2

Download
 Filename:  VPcURL.zip
 Filesize:  21.67 KB
 Downloaded:  697 Time(s)


zlib.zip
 Description:
zip package

Download
 Filename:  zlib.zip
 Filesize:  3.03 KB
 Downloaded:  783 Time(s)



Last edited by Jan de Lint on 27 Apr 2011 20:44; edited 4 times in total
Back to top
View user's profile Send private message
Jan de Lint



The Netherlands
Joined: 06 Mar 2000
Posts: 388

PostPosted: 7 Oct 2010 19:28    Post subject: Constructors and URL's Reply with quote

There are four types of constructors:

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:

- 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:

- Conn:setHeadersOn(),

and in the callback:

ReceivedHeaders = Conn:getHeadersOutput(),

Back to top
View user's profile Send private message
Jan de Lint



The Netherlands
Joined: 06 Mar 2000
Posts: 388

PostPosted: 10 Oct 2010 10:38    Post subject: callback Reply with quote

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

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:

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:

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

Back to top
View user's profile Send private message
Jan de Lint



The Netherlands
Joined: 06 Mar 2000
Posts: 388

PostPosted: 13 Oct 2010 11:25    Post subject: Housekeeping Reply with quote

The following Curl commands are now given automatically:

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

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.
Back to top
View user's profile Send private message
Jan de Lint



The Netherlands
Joined: 06 Mar 2000
Posts: 388

PostPosted: 27 Apr 2011 20:25    Post subject: Reply with quote

Change 27-4-2011 : the VP monitor class (nice stuff!) replaces Windows postAction(). Courtesy Gildas Ménier. That way VPcURL can also be used in a console application and raw performance has improved. A change in the callback is needed in order to write callback messages to windows UI.
In a GUI application, the lines like

 stdIO::write("\nHttp responsecode: ", ResultCode),

should be replaced by something like:

 write_to_ui("\nHttp responsecode: ", ResultCode),

Where write_to_ui is as follows:

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!
Back to top
View user's profile Send private message
Aliraza03




Joined: 29 Sep 2014
Posts: 1

PostPosted: 29 Sep 2014 12:40    Post subject: Reply with quote

with this implementation, can I also do a https request ? ( https instead of http )
_________________
http://cert-killer.com/
Back to top
View user's profile Send private message
Jan de Lint



The Netherlands
Joined: 06 Mar 2000
Posts: 388

PostPosted: 30 Sep 2014 7:02    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    discuss.visual-prolog.com Forum Index -> Visual Prolog Tips & Samples All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum