urllib.anubis 3.71 KB
/*
 * Created by PyramIDE.
 * User: ricard
 * Date: 21/11/2009
 * Time: 01:27
 * 
 */

read tools/basis.anubis
read system/lists.anubis
read system/string.anubis

public define String
  _url_encode_char
  (
    Word8 c
  )=
  "%" +to_upper(to_hexa(c)).


public define List(Word8)
  _url_quote
  (
    List(Word8) string,
    List(Word8) safe,
    Bool        plus,
    List(Word8) so_far
  ) =
  if string is
  {
    [] then reverse(so_far),
    [h . t] then
      if (h = '-' | h = '_' | h = '.' | contains(safe, h)
          | (h >=+ '0' & h +=< '9')
          | (h >=+ 'a' & h +=< 'z') 
          | (h >=+ 'A' & h +=< 'Z') ) then 
          _url_quote(t, safe, plus, [h . so_far])
      else if (h = ' ' & plus) then
        _url_quote(t, safe, plus, ['+' . so_far])
      else
        _url_quote(t, safe, plus, reverse(explode(_url_encode_char(h))) + so_far)
  }.
  

/**
 * Replace special characters in string using the "%xx" escape. Letters, digits, and the characters "_.-" are never quoted.
 * The optional safe parameter specifies additional characters that should not be quoted -- its default value is '/'. 
 */
public define String
  url_quote
  (
    String string,
    String safe
  ) =
  implode(_url_quote(explode(string), explode(safe), false, [])).

/**
 * Replace special characters in string using the "%xx" escape. Letters, digits, and the characters "_.-" are never quoted.
 * The optional safe parameter specifies additional characters that should not be quoted -- its default value is '/'. 
 */
public define String
  url_quote
  (
    String string,
  ) =
  url_quote(string, "/"). 

/**
 * Like quote(), but also replaces spaces by plus signs, as required for quoting HTML form values. 
 * Plus signs in the original string are escaped unless they are included in safe. It also does not have safe default to '/'.
 */
public define String
  url_quote_plus
  (
    String string,
    String safe
  ) =
  implode(_url_quote(explode(string), explode(safe), true, [])).

/**
 * Like quote(), but also replaces spaces by plus signs, as required for quoting HTML form values. 
 * Plus signs in the original string are escaped unless they are included in safe. It also does not have safe default to '/'.
 */
public define String
  url_quote_plus
  (
    String string,
  ) =
  url_quote_plus(string, ""). 


//-----------

define Word8
   _url_decode_char
     (
       Word8 x1,
       Word8 x2
     ) =
   with n1 = if x1 +=< '9' then (x1 - '0') else if x1 +=< 'F' then (x1 - 'A' + 10) else (x1 - 'a' + 10),
        n2 = if x2 +=< '9' then (x2 - '0') else if x1 +=< 'F' then (x1 - 'A' + 10) else (x2 - 'a' + 10),
   (n1 << 4) + n2. 
   

public define List(Word8)
  _url_unquote
  (
    List(Word8) string,
    Bool        plus,
    List(Word8) so_far
  ) =
  if string is
  {
    [] then reverse(so_far),
    [h . t] then
      if h = '%' then
        if t is
        {
          [] then reverse([h . so_far]), 
          [h1 . t1] then
            if t1 is
            {
              [] then reverse([h1, h . so_far]), 
              [h2 . t2] then
                _url_unquote(t2, plus, [_url_decode_char(h1, h2) . so_far])
            }
        }
      else if (h = '+' & plus) then
        _url_unquote(t, plus, [' ' . so_far])
      else
        _url_unquote(t, plus, [h . so_far])
  }.

/**
 * Replace "%xx" escapes by their single-character equivalent. 
 * Example: unquote("/%7Econnolly/") yields "/~connolly/". 
 */
public define String
  url_unquote
  (
    String string,
  ) =
  implode(_url_unquote(explode(string), false, [])).
  
/**
 * Like unquote(), but also replaces plus signs by spaces, as required for unquoting HTML form values. 
 */
public define String
  url_unquote_plus
  (
    String string,
  ) =
  implode(_url_unquote(explode(string), true, [])).