code critique



For educational purposes (resp. fun) I wrote some code for the cgi
GET method. It works like this:

(get-parameter 'username) => "John"

Since I'm learning Lisp on my own, I would highly appreciate any tips
concerning my code. What could be done better, what is already ok?

Regards,

dhl


(defun convert-hex-encoded-chars (string)
"Converts %HEX encoded chars found in string."
(if (cl-ppcre:scan "%.." string)
(cl-ppcre:register-groups-bind
(before match after) ("([^%]*)(%..)(.*)" string)
(concatenate 'string before
(string (code-char
(parse-integer (subseq match 1) :radix 16)))
(convert-hex-encoded-chars after)))
string))

(defun split-query-string (query-string)
"Splits a query-string and returns a list of variable=value assignments."
(cl-ppcre:split "&" (substitute #\Space #\+
(convert-hex-encoded-chars query-string))))

(defun get-parameters (&optional
(query-string
(osicat:environment-variable "QUERY_STRING")))
"Returns an alist of (variable . value) pairs for a cgi query-string, which
may be given as an optional parameter. Otherwise, the environment-variable
QUERY_STRING is used."
(loop for variable in (split-query-string query-string)
collect (cl-ppcre:register-groups-bind
(key value) ("(.*)=(.*)" variable)
(cons (intern (string-upcase key))
value))))

(defparameter *parameters* (get-parameters)
"Alist of QUERY_STRING variables.")

(defun get-parameter (key)
"Returns the value of a QUERY_STRING variable named key."
(cdr (assoc key *parameters*)))

.



Relevant Pages

  • receiving an XML POST via CGI
    ... having the occasional problem getting my head around the "Ruby way". ... class that gives me most headaches is CGI.. ... Obviously at that stage REXML is packing in as the XML string is ... I'm wondering whether there is any "ruby magic" to the cgi.params call. ...
    (comp.lang.ruby)
  • pattern matching problems
    ... i have some problems processing the query-string exchanged between client and server. ... i would like to parse the query-string the following way: the query-string contains a sentence, for example: the cat lies on the mat. ... this solution succeeds in finding and returning the last element of the string printing it as often as required. ... or does anybody know of a program which is able to restore deleted files. ...
    (perl.beginners)
  • Problem with CGI and .NET
    ... string QueryString = ... CGI Error ... HTTP headers. ... Unhandled Exception: System.ArgumentException: The parameter is incorrect. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: CGI help
    ... integrity modifier,integm,System/Application,30,Customer ... after figuring out how CGI works in ruby ... ... The above will generate a body tag with a h1 tag and then close the body. ... The block passed to the form method must evaluate to a string which will ...
    (comp.lang.ruby)
  • Re: Cant use variable from cgi with hpricot
    ... A cursory examination of those strings reveals that they are not ... That's the string you are getting from cgi. ... special characters have to be 'untransformed' on the other side. ...
    (comp.lang.ruby)