UrlEncoder with &

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

UrlEncoder with &

Post by DoubleDutch »

The urlencoder doesn't encode & !

See:

Code: Select all

URLEncoder("test&blah")
It should encode it just in case you encode a string with '&' within it and use it as a parameter among other parameters...

EG:

Code: Select all

a$="blah"
b$="spaces here plus this&that"
c$="bling"

Debug("http:\\mydomain.com?param1="+URLEncoder(a$)+"&param2="+URLEncoder(b$)+"&param2="+URLEncoder(c$))
The parameters should be:
param1=blah
param2=spaces here plus this&that
param3=bling
but what we get is:
param1=blah
param2=spaces here plus this
that
param3=bling
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
TomS
Enthusiast
Enthusiast
Posts: 342
Joined: Sun Mar 18, 2007 2:26 pm
Location: Munich, Germany

Re: UrlEncoder with &

Post by TomS »

I don't think this is a bug, since you should pass an URL to that function and not a single parameter.
Anyways:

Code: Select all

Procedure.s URLParamPathEncoder(param.s)
    ProcedureReturn ReplaceString(URLEncoder(param), "&", "%26")
EndProcedure
Last edited by TomS on Wed Feb 16, 2011 9:38 am, edited 2 times in total.
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: UrlEncoder with &

Post by DoubleDutch »

If that's so then the help should be updated with a note about it, possibly the command optionallt extended for parameter encoding instead of full url lines?
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Lush
User
User
Posts: 20
Joined: Sat Feb 12, 2011 5:58 pm
Location: France

Re: UrlEncoder with &

Post by Lush »

Hi,

TomS, I think you got your parentheses wrong in your ProcedureReturn. The inner parentheses should only be around "param":

Code: Select all

ProcedureReturn ReplaceString(URLEncoder(param), "&", "%26")
But then there's another problem. a ReplaceString on the whole query string will also replace the ampersands that separate key/value pairs instead of only replacing the ampersands inside a value.

I think the solution is to use your FullURLEncoder procedure on each separate value, then build the query string just like DoubleDutch did in his example.
BTW, DoubleDutch, your "spaces here plus this&that" should have "%20" instead of spaces.

This is my first post here, I hope it'll be a useful one ;)
User avatar
TomS
Enthusiast
Enthusiast
Posts: 342
Joined: Sun Mar 18, 2007 2:26 pm
Location: Munich, Germany

Re: UrlEncoder with &

Post by TomS »

Cr@p, you're right. Wrote that on the fly :oops:

But you got my intentions wrong.
DoubleDutch used the function only for a single parameter and my code works for that.
There's no way anyone could tell whether an ampersand is a parameter separator or has to be encoded.
This has to be done by the programmer.

Code: Select all

url$ = URLEncoder("path.php?param1="+FullURLEncoder("bla&blub")+"&param2="+FullURLEncoder("ab&c"))
url$ = URLEncoder("path.php?param1=bla%26blub&param2=ab%26c")
URLParamPathEncoder() would have been a better name, eh?
Lush wrote:BTW, DoubleDutch, your "spaces here plus this&that" should have "%20" instead of spaces.
That's what URLEncoder() is for :lol:
Lush
User
User
Posts: 20
Joined: Sat Feb 12, 2011 5:58 pm
Location: France

Re: UrlEncoder with &

Post by Lush »

TomS wrote:
Lush wrote:BTW, DoubleDutch, your "spaces here plus this&that" should have "%20" instead of spaces.
That's what URLEncoder() is for :lol:
Oops, I selected the wrong one. I meant to quote the "spaces here plus this" in the "but what we get is:" quote. Those spaces should have been "%20".

Code: Select all

url$ = URLEncoder("path.php?param1="+FullURLEncoder("bla&blub")+"&param2="+FullURLEncoder("ab&c"))
This won't work either :D
All the "%26" returned by your procedure will be encoded as "%2526" by URLEncoder() ;)
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: UrlEncoder with &

Post by DoubleDutch »

The problem comes if someone who uses a program enters data (with a & in it) that gets put into a string that then gets sent by the program to a website. The result would be wrong. The command should be re-thought or the documentation should be updated with a warning.

For me, as it stands, the urlencoder is a bit useless - I know how to properly write the http:// bit... ;) The pain is the parameters being encoded properly.

So I just use a custom routine to encode parameters and convert anything that isn't a letter into a %xx thingy!

btw: Anyone tried the url encode with a full unicode string - there is a standard to convert unicode domain into asciii domain - would be cool to use this to convert to and from unicode domain names.

I have "http://ɯoɔ.com/" which should convert to/from "http://xn--o-10a3f.com/" ;)
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: UrlEncoder with &

Post by jesperbrannmark »

Since everyone needs this function:
replacestring will not do them all, so i did my own as well.

Code: Select all

Procedure.s replacestrin(st.s,vad.s,till.s)
  If FindString(st.s,vad.s,1)
    For a=1 To Len(st.s)
      If Mid(st.s,a,1)=vad.s 
        final.s+till.s
      Else
        final.s+Mid(st.s,a,1)
      EndIf
    Next
    ProcedureReturn final.s
  Else
    ProcedureReturn st.s
  EndIf
EndProcedure
Procedure.s URLEncode(st.s)
  st.s=URLEncoder(st.s)
  st.s=replacestrin(st.s,"&","%26")
  st.s=replacestrin(st.s,"!","%21")
  st.s=replacestrin(st.s,"*","%2A")
  st.s=replacestrin(st.s,"'","%27")
  st.s=replacestrin(st.s,"(","%28")
  st.s=replacestrin(st.s,")","%29")
  st.s=replacestrin(st.s,";","%3B")
  st.s=replacestrin(st.s,":","%3A")
  st.s=replacestrin(st.s,"@","%40")
  st.s=replacestrin(st.s,"&","%26")
  st.s=replacestrin(st.s,"=","%3D")
  st.s=replacestrin(st.s,"+","%2B")
  st.s=replacestrin(st.s,"$","%24")
  st.s=replacestrin(st.s,",","%2C")
  st.s=replacestrin(st.s,"/","%2F")
  st.s=replacestrin(st.s,"?","%3F")
  st.s=replacestrin(st.s,"#","%23")
  st.s=replacestrin(st.s,"[","%5B")
  st.s=replacestrin(st.s,"]","%5D")
  ProcedureReturn st.s
EndProcedure		
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: UrlEncoder with &

Post by Fred »

This is a good idea, we will add a #PB_URL_Parameter flag to this command.
Post Reply