URLEncode: Difference between revisions

From Pickwiki
Jump to navigationJump to search
Stuboy (talk | contribs)
mNo edit summary
 
Stuboy (talk | contribs)
Simplified it
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
HomePage>>SourceCode>>BasicSource>>URLEncode
HomePage>>SourceCode>>BasicSource>><nowiki>URLEncode</nowiki>


This is a function to correctly URLEncode a string.<br>
This is a function to correctly URLEncode a string.<br>
I wrote this when I was posting data into a SQL database using XML datagrams (very useful). The posted data had to be URL encoded and this was what I came up with.<br>
URL encoding of a character consists of a "%" symbol, followed by the two-digit hexadecimal representation (case-insensitive) of the ISO-Latin code point for the character.<br>
Because the data I was dealing with was only printable ascii < char(128) I have encoded only this range + uv delimiters for speed.
 
References:<br>
See http://www.gbiv.com/protocols/uri/rfc/rfc3986.html#characters<br>
See http://www.permadi.com/tutorial/urlEncoding




<PRE>
<PRE>
function URLEncode(string)
function urlEncode(param)
 
   equ eSafeChars to 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~'
   *// perform standard URL encoding for UV ascii.
   string = param<1>
   *// sboydell 17-Dec-2004
   flag  = param<2> + 0
 
   nstring = ''
   *// deffun URLEncode(string) [calling *URLEncode]
   max = len(string)
   *// encodedString = URLEncode(unencodedString)
   for i = 1 to max
 
      if flag then
   *// Only alphanumerics [0-9a-zA-Z], the special characters $-_.+!*'(),
      *// decode
   *// and reserved characters used for their reserved purposes may be
        if string[i,3] match '"%"2X' then
  *// used unencoded within a URL.
            nstring := oconv(string[i+1,2],'my')
 
            i += 2
  *// ranges of ascii codes of all characters to be encoded
        end else
  *// 0-31 ASCII Control Characters These characters are not printable Unsafe
            nstring := string[i,1]
  *// 32-47 Reserved Characters ' '!?#$%&'()*+,-./ Unsafe
        end
  *// 48-57 ASCII Characters and Numbers 0-9 Safe
      end else
  *// 58-64 Reserved Characters :;<=>?@ Unsafe
      *// encode
  *// 65-90 ASCII Characters A-Z Safe
        if index(eSafeChars,string[i,1],1) then
  *// 91-96 Reserved Characters [\]^_` Unsafe
            nstring := string[i,1]
  *// 97-122 ASCII Characters a-z Safe
        end else
  *// 123-126 Reserved Characters {|}~ Unsafe
            nstring := '%':iconv(string[i,1],'my')
  *// 127 Control Characters ' ' Unsafe
        end
   *// 128-255 Non-ASCII Characters ' ' Unsafe
      end
   next i
    
    
  equ E.COMMA      to ',',
return(nstring)
      E.PERCENT    to '%',
the:end
      E.MAXRANGE    to 9,
      E.ASCII.CODES to '37,37,32,36,38,44,47,47,58,64,91,94,96,96,123,128,250,255'


  *// define pairs of ranges to encode
  dim ENCODE.RANGE(E.MAXRANGE,2)
  matparse ENCODE.RANGE from E.ASCII.CODES, E.COMMA


  for i = 1 to E.MAXRANGE
      for j = ENCODE.RANGE(i,1) to ENCODE.RANGE(i,2)
        string = change(string,char(j),E.PERCENT:dtx(j,2))
      next j
  next i


return(string)
</PRE>
 
the:end


</PRE>
HomePage>>SourceCode>>BasicSource>><nowiki>URLEncode</nowiki>

Latest revision as of 06:44, 6 July 2018

HomePage>>SourceCode>>BasicSource>>URLEncode

This is a function to correctly URLEncode a string.
URL encoding of a character consists of a "%" symbol, followed by the two-digit hexadecimal representation (case-insensitive) of the ISO-Latin code point for the character.

References:
See http://www.gbiv.com/protocols/uri/rfc/rfc3986.html#characters
See http://www.permadi.com/tutorial/urlEncoding


function urlEncode(param)
   equ eSafeChars to 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~'
   string = param<1>
   flag   = param<2> + 0
   nstring = ''
   max = len(string)
   for i = 1 to max
      if flag then
      *// decode
         if string[i,3] match '"%"2X' then
            nstring := oconv(string[i+1,2],'my')
            i += 2
         end else
            nstring := string[i,1]
         end
      end else
      *// encode
         if index(eSafeChars,string[i,1],1) then
            nstring := string[i,1]
         end else
            nstring := '%':iconv(string[i,1],'my')
         end
      end
   next i
   
return(nstring)
the:end



HomePage>>SourceCode>>BasicSource>>URLEncode