[tbnl-devel] create-static-file-dispatcher-and-handler

Stefan Scholl stesch at no-spoon.de
Tue Jul 20 20:56:21 UTC 2004


Hi!


A simple convenience function for serving static files with TBNL.

Example:

    (create-static-file-dispatcher-and-handler "/css/main.css" 
     #P"css/main.css" 
     "text/css")

Maybe someone finds a better name and documentation. :-)



(defun create-static-file-dispatcher-and-handler (uri path content-type)
  "Creates a dispatch function which will dispatch to a handler
function which emits the file PATH with CONTENT-TYPE if the URI
matches the SCRIPT-NAME of the request."
  (let ((buf (make-array 8192 :element-type #-:tbnl-bivalent-streams 'character
                         #+:tbnl-bivalent-streams '(unsigned-byte 8))))
    (let ((handler #'(lambda ()
                       (setf (content-type)
                             content-type)
                       (with-output-to-string (str)
                         (with-open-file (file path :direction :input
                                               #+:tbnl-bivalent-streams :element-type
                                               #+:tbnl-bivalent-streams '(unsigned-byte 8))
                           (do ((pos (read-sequence buf file) (read-sequence buf file)))
                               ((zerop pos))
                             (write-sequence buf str :end pos)))))))
      #'(lambda (request)
          (when (equal (script-name request) uri)
            handler)))))



Regards,
Stefan






More information about the Tbnl-devel mailing list