Discussions related to Visual Prolog
User avatar
Tonton Luc
VIP Member
Posts: 204
Joined: 16 Oct 2001 23:01

vPcURL encoded an URL

Unread post by Tonton Luc »

Hi,

Is there any vPcURL curl_easy_setopt
User avatar
Jan de Lint
VIP Member
Posts: 83
Joined: 6 Mar 2000 0:01

Re: vPcURL encoded an URL

Unread post by Jan de Lint »

Tonton Luc wrote:Hi,

Is there any vPcURL curl_easy_setopt
Yes, there is curl_easy_escape (and curl_easy_unescape) in VPcURL.i
]an
User avatar
Tonton Luc
VIP Member
Posts: 204
Joined: 16 Oct 2001 23:01

Unread post by Tonton Luc »

Hi Jan,

curl_easy_escape convert ALL special caracters like "-","_","/",".",etc... too, but it's not my goal.
Before to call :

Code: Select all

C:curl_easy_setopt(vPcURLconst::cURLOPT_URL, vPcURL::s(URL)),
I need to convert only some special caracters like in this URL :
https://www.bmw.fr/dam/brandBM/marketFR ... fs_BMW_S[b][/b]rie_4_Coupe_Cabriolet_Juillet_2015.pdf
Harrison Pratt
VIP Member
Posts: 439
Joined: 5 Nov 2000 0:01

Unread post by Harrison Pratt »

I have acquired an irrational (or not) abhorrence of string generation, thanks to VIP 5.2! For simple tasks like converting a URL there's not much risk of stack overflow, but I still like to use routines like this:

Code: Select all

class predicates     url_encoded : ( string ) -> string EncodedURL. % Move to .CL file     encode_urlChars : ( char_list, char_list ) -> string. clauses     url_encoded( URL ) = EncodedURL :-         URL_Chars = string::toCharList(URL),         EncodedURL = encode_urlChars( URL_Chars, [] ).               encode_urlChars( [], TempCC ) = string::createFromCharList(TempCC).             encode_urlChars( [H|T], TempCC ) = U :-                 char_urlCode(H,CC),                 !,                 NewTemp = list::append(TempCC,CC),                 U = encode_urlChars(T,NewTemp).             encode_urlChars( [H|T], TempCC ) = U :-                 NewTemp = list::append(TempCC,[H]),                 U = encode_urlChars( T, NewTemp  ).               class facts                 char_urlCode : ( char, char_list ).             clauses                 char_urlCode(' ',  ['%','2','0']).                 char_urlCode('!',  ['%','2','1']).                 char_urlCode('"',  ['%','2','2']).                 char_urlCode('#',  ['%','2','3']).                 char_urlCode('$',  ['%','2','4']).                 char_urlCode('%',  ['%','2','5']).                 char_urlCode('&',  ['%','2','6']).                 char_urlCode('\\', ['%','2','7']).                 char_urlCode('(',  ['%','2','8']).                 char_urlCode(')',  ['%','2','9']).                 char_urlCode('*',  ['%','2','A']).                 char_urlCode('+',  ['%','2','B']).                 char_urlCode(',',  ['%','2','C']).                 char_urlCode('-',  ['%','2','D']).                 char_urlCode('.',  ['%','2','E']).                 char_urlCode('/',  ['%','2','F']).    
User avatar
Tonton Luc
VIP Member
Posts: 204
Joined: 16 Oct 2001 23:01

Unread post by Tonton Luc »

Hi,

I use this one :
Paul Cerkez
VIP Member
Posts: 106
Joined: 6 Mar 2000 0:01

Unread post by Paul Cerkez »

I use something similar to Tonton's. (learned back in the VIP 4.x/5.x days and carried out to today)
AI Rules!
P.
Harrison Pratt
VIP Member
Posts: 439
Joined: 5 Nov 2000 0:01

Re:

Unread post by Harrison Pratt »

Tonton Luc wrote:Hi,

I use this one :
I've done it that way too, but I worry about an unnoticed typing entry in entering the lists. Fact structures are easier for me to visually check in code, but I do know that database operations can be slower than list operations.
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

Given this description https://en.wikipedia.org/wiki/Percent-encoding, I would do something like this:

Code: Select all

class predicates     urlEncode : (string String) -> string Encoded. clauses     urlEncode(String) = outputStream_string::getString({ (S) :- urlEncode2(S, string8::toUtf8(String)) }).   class predicates     urlEncode2 : (outputStream S, string8 Utf8). clauses     urlEncode2(S, Utf8_1) :-         Char8 = memory::getChar8(Utf8_1, Utf8_2),         if 0 <> Char8 then             urlEncodeChar8(S, Char8),             urlEncode2(S, Utf8_2)         end if.   class predicates     urlEncodeChar8 : (outputStream S, char8 Char8). clauses     urlEncodeChar8(S, Char8) :-         Char = string8::mapToChar(Char8, utf8),         if 'A' <= Char and Char <= 'Z'             or 'a' <= Char and Char <= 'z'             or '0' <= Char and Char <= '9'             or '-' = Char             or '_' = Char             or '.' = Char             or '~' = Char         then             S:write(Char)         else             S:writef("%%%02x", Char8)         end if.
This code also encodes the Reserved Characters, so to create a URL you will have to encode the parts and then construct the URL from them. E.g.:

Code: Select all

URL = string::format("http://%/subdir/query?name=%&value=%", Server, urlEncode(Name), urlEncode(Value))
I assume the Server does not need to be urlEncoded.
Regards Thomas Linder Puls
PDC
User avatar
Jan de Lint
VIP Member
Posts: 83
Joined: 6 Mar 2000 0:01

Unread post by Jan de Lint »

As long as you are not in a position to predict the exact url that the server expects this is thin ice. You have indeed to take the practical approach and revert to custom character translation and elaborate testing. This is especially true for (very) old servers which you do not own yourself.
Post Reply