URLEncode: Difference between revisions

From Pickwiki
Jump to navigationJump to search
Stuboy (talk | contribs)
mNo edit summary
Stuboy (talk | contribs)
Simplified it
 
Line 3: Line 3:
This is a function to correctly URLEncode a string.<br>
This is a function to correctly URLEncode a string.<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>
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>
Some of the encoding decisions can get tricky in the 32-47 range and you will have to decide how you might encode sections of strings to override encrypting some potentially valid unencoded URI delimiters.


References:<br>
References:<br>
Line 11: Line 10:


<PRE>
<PRE>
function URLEncode(inString)
function urlEncode(param)
 
   equ eSafeChars to 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~'
   *// perform standard URL encoding for UV ascii. ISO-8859-1 (ISO-Latin) character set.
   string = param<1>
  *// stuart boydell 17-Dec-2004
   flag  = param<2> + 0
 
   nstring = ''
  *// basic example:
   max = len(string)
  *// deffun URLEncode(string) ;* calling *URLEncode ;*[use 'calling globID' if you globally catalogue the program]
   for i = 1 to max
  *// encodedString = URLEncode(unencodedString)
       if flag then
 
      *// decode
  *// Only alphanumerics [0-9a-zA-Z], the special characters $-_.+!*'(),
        if string[i,3] match '"%"2X' then
   *// and reserved characters used for their reserved purposes may be
            nstring := oconv(string[i+1,2],'my')
   *// used unencoded within a URL.
            i += 2
 
        end else
  *// This process encodes unsafe characters to their URL safe
            nstring := string[i,1]
  *// equivalent. eg. char(2) -> "%02"
        end
 
      end else
  *// First encodes char(37) "%" to %25 then loops through other ASCII.CODES ranges.
       *// encode
 
         if index(eSafeChars,string[i,1],1) then
  *// ranges of ascii codes of all characters to be encoded
             nstring := string[i,1]
  *// 0-31 ASCII Control Characters Unsafe
        end else
  *// 32-47 Reserved Characters ' '!?#$%&'()*+,-./ Unsafe
            nstring := '%':iconv(string[i,1],'my')
  *// 48-57 ASCII Characters and Numbers 0-9 Safe
   *// 58-64 Reserved Characters :;<=>?@ Unsafe
  *// 65-90 ASCII Characters A-Z Safe
  *// 91-96 Reserved Characters [\]^_` Unsafe
  *// 97-122 ASCII Characters a-z Safe
  *// 123-126 Reserved Characters {|}~ Unsafe
  *// 127 Control Characters ' ' Unsafe
   *// 128-255 Non-ASCII Characters ' ' Unsafe
    
  *// 'Special' encoding characters
  equ E.PERCENT    to '%',
      E.SPACE       to ' ',
      E.PLUS        to '+'
 
  *// 'Normal' encoding start/end characters (decimal ascii)
  equ E.ASCII.START to (00:@am:38:@am:47:@am:58:@am:91:@am:96:@am:123),
      E.ASCII.END  to (36:@am:44:@am:47:@am:64:@am:94:@am:96:@am:255),
      E.MAXRANGE    to 7
 
  *// convert 'special' encoding characters "%+[sp]"
  outString = change(inString,E.PERCENT,E.PERCENT:'25')
 
  *// space may be converted to '+' under some schemes...
  *//outString = change(outString,E.PLUS,E.PERCENT:'2B')
  *//outString = change(outString,E.SPACE,E.PLUS)
 
  *// now convert 'normal' character ranges
  for i = 1 to E.MAXRANGE
       for j = E.ASCII.START<i> to E.ASCII.END<i>
        jchar = char(j)  ;*// get char from decimal val
        *// index() character before change() - faster
         if index(outString,jchar,1) then
             outString = change(outString,jchar,E.PERCENT:dtx(j,2))
         end
         end
       next j
       end
   next i
   next i
 
 
return(outString)
return(nstring)
the:end
the:end





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