Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] RWT: CGI logic

From: maarten:koopmans:surfnet:nl at: 3-Mar-2003 19:43

REBOL [ Title: {Logic for getting (Fast)CGI parameters.} Author: {Ernie van der Meer} Copyright: "2002, ING The Netherlands" ] cgi: make object! [ data: copy [] url-encode: func [ {Replaces characters that are not allowed in URLs by their url-encoded values.} val [string!] /local illegal-chars lval ] [ lval: copy val illegal-chars: {+%;/?:@= "<>#{}|\^~[]`} foreach char illegal-chars [ replace/all lval char join "%" enbase/base to-string char 16 ] return lval ] url-decode: func [ val [string!] /local illegal-chars lval ] [ lval: copy val illegal-chars: {+%;/?:@= "<>#{}|\^~[]`} foreach char illegal-chars [ replace/all lval join "%" enbase/base to-string char 16 char ] return lval ] two-binary: func [ {Converts a string representation of a binary (eg. EFAB2E) back to its binary representation (#{EFAB2E}).} val [string!] /local bval ] [ either error? try [ val: do rejoin [ "#{" val "}" ] ] [ return none ] [ return val ] ] two-string: func [ {Converts a binary value (eg #{EFAB2E}) to its string representation (EFAB2E).} val [binary!] /local bval ] [ either parse (mold val) [ thru "{" copy bval to "}" to end ] [ return trim/all bval ] [ return none ] ] init: func [ {Initializes the CGI object.} /local x buf parameters ] [ parameters: either system/options/cgi/request-method = "POST" [ x: make string! 1000 buf: make string! 1000 while [0 < read-io system/ports/input clear x 1000] [append buf x] buf ] [ system/options/cgi/query-string ] if parameters [ data: decode-cgi parameters ] ] get-value: func [ {Reads the value of a named CGI parameter by literal name.} name [string!] /loose {Returns an empty string instead of an error message if the CGI parameter cannot be found.} /local result ] [ result: select data to-set-word name either result [ return trim/lines url-decode result ] [ either loose [ return copy {} ] [ make error! rejoin [ {Unknown CGI parameter: } name ] ] ] ] ]