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

[REBOL] RWT: Cookies

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

REBOL [ Title: "Logic to set and get HTTP cookies" ] cookies: context [ 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 ] to-GMT-idate: func [ "Returns a standard GMT-based Internet date string." date [date!] /local str GMT-date ] [ str: copy {GMT} GMT-date: date - date/zone head insert str reform [ pick ["Mon," "Tue," "Wed," "Thu," "Fri," "Sat," "Sun,"] GMT-date/weekday GMT-date/day pick [ "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" ] GMT-date/month GMT-date/year GMT-date/time "" ] ] init: func [ {Reads cookies from the HTTP header.} /local cookies rule name val ] [ data: copy [] if cookies: select system/options/cgi/other-headers "HTTP_COOKIE" [ rule: [ copy name to "=" "=" [ copy val to ";" "; " | copy val to end ] (append data reduce [ name url-decode val ])] parse cookies [ any rule ] ] ;write/append %/tmp/log rejoin [ cookies newline ] ] set-cookie: func [ {By default, cookie expires at end of session, path is /, domain is server, and not secure.} name [string!] "The cookie's name" value [string!] "The cookie's value" expires [date! none!] "Date, should also include time" path [string! none!] "Path in which cookie should be returned" domain [string! none!] "Your server or domain" secure [logic! none!] "Return only over secure channels" ] [ prin rejoin [{Set-Cookie:} name {=} url-encode value {; }] all [expires prin rejoin [{expires=} to-GMT-idate expires {; }]] all [path prin rejoin [{path=} path {; }]] all [domain prin rejoin [{domain=} domain {; }]] all [secure prin {secure}] prin {^/} ] get-cookie: func [ {Returns the value of a cookie. Return none if the cookie is not set.} name [string!] {Name of the cookie.} ] [ select data name ] ]