Query Parameter Types

Since you’ll be using define-url-function, you need to define a few methods on the string->type generic function from Chapter 28 that define-url-function uses to convert string query parameters into Lisp objects. In this application, you’ll need methods to convert strings to integers, keyword symbols, and a list of values.

The first two are quite simple.

  1. (defmethod string->type ((type (eql 'integer)) value)
  2. (parse-integer (or value "") :junk-allowed t))
  3. (defmethod string->type ((type (eql 'keyword)) value)
  4. (and (plusp (length value)) (intern (string-upcase value) :keyword)))

The last string->type method is slightly more complex. For reasons I’ll get to in a moment, you’ll need to generate pages that display a form that contains a hidden field whose value is a list of strings. Since you’re responsible for generating the value in the hidden field and for parsing it when it comes back, you can use whatever encoding is convenient. You could use the functions **WRITE-TO-STRING** and **READ-FROM-STRING**, which use the Lisp printer and reader to write and read data to and from strings, except the printed representation of strings can contain quotation marks and other characters that may cause problems when embedded in the value attribute of an INPUT element. So, you’ll need to escape those characters somehow. Rather than trying to come up with your own escaping scheme, you can just use base 64, an encoding commonly used to protect binary data sent through e-mail. AllegroServe comes with two functions, base64-encode and base64-decode, that do the encoding and decoding for you, so all you have to do is write a pair of functions: one that encodes a Lisp object by converting it to a readable string with **WRITE-TO-STRING** and then base 64 encoding it and, conversely, another to decode such a string by base 64 decoding it and passing the result to **READ-FROM-STRING**. You’ll want to wrap the calls to **WRITE-TO-STRING** and **READ-FROM-STRING** in **WITH-STANDARD-IO-SYNTAX** to make sure all the variables that affect the printer and reader are set to their standard values. However, because you’re going to be reading data that’s coming in from the network, you’ll definitely want to turn off one feature of the reader—the ability to evaluate arbitrary Lisp code while reading!4 You can define your own macro with-safe-io-syntax, which wraps its body forms in **WITH-STANDARD-IO-SYNTAX** wrapped around a **LET** that binds ***READ-EVAL*** to **NIL**.

  1. (defmacro with-safe-io-syntax (&body body)
  2. `(with-standard-io-syntax
  3. (let ((*read-eval* nil))
  4. ,@body)))

Then the encoding and decoding functions are trivial.

  1. (defun obj->base64 (obj)
  2. (base64-encode (with-safe-io-syntax (write-to-string obj))))
  3. (defun base64->obj (string)
  4. (ignore-errors
  5. (with-safe-io-syntax (read-from-string (base64-decode string)))))

Finally, you can use these functions to define a method on string->type that defines the conversion for the query parameter type base64-list.

  1. (defmethod string->type ((type (eql 'base-64-list)) value)
  2. (let ((obj (base64->obj value)))
  3. (if (listp obj) obj nil)))