Page 1 of 1

vPcURL encoded an URL

Posted: 28 Sep 2015 13:23
by Tonton Luc
Hi,

Is there any vPcURL curl_easy_setopt

Re: vPcURL encoded an URL

Posted: 3 Oct 2015 10:59
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

Posted: 14 Oct 2015 13:32
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/countryFR/fr_FR/Brochures/New-Vehicles/BMW-Serie-4/Tarifs_BMW_Srie_4_Coupe_Cabriolet_Juillet_2015.pdf

Posted: 14 Oct 2015 22:05
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']).    

Posted: 15 Oct 2015 15:01
by Tonton Luc
Hi,

I use this one :

Code: Select all

 

Posted: 15 Oct 2015 15:53
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)

Re:

Posted: 15 Oct 2015 20:27
by Harrison Pratt
Tonton Luc wrote:Hi,

I use this one :

Code: Select all

 
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.

Posted: 15 Oct 2015 22:51
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.

Posted: 17 Oct 2015 9:17
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.