X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=utils.lisp;h=ec42df6332429479e2994b18f666c3960903e149;hb=49f84ccd7347606524722e1c064027bb7c96c3a6;hp=04376fdd43dcb018ebb66e3c30d1078e2c259414;hpb=118ee93d69e2b09d12eb317f6db3fbda113be82f;p=cl-modlisp.git diff --git a/utils.lisp b/utils.lisp index 04376fd..ec42df6 100644 --- a/utils.lisp +++ b/utils.lisp @@ -7,7 +7,7 @@ ;;;; Programmer: Kevin M. Rosenberg ;;;; Date Started: Dec 2002 ;;;; -;;;; $Id: utils.lisp,v 1.1 2003/07/04 19:52:32 kevin Exp $ +;;;; $Id: utils.lisp,v 1.10 2003/08/18 05:48:55 kevin Exp $ ;;;; ************************************************************************* (in-package #:modlisp) @@ -19,54 +19,68 @@ (:text "text/plain") (otherwise fmt))) -(defmacro with-ml-page ((&key (format :html) (precompute t)) &body body) - (let ((fmt (gensym)) - (precomp (gensym)) - (result (gensym)) - (outstr (gensym)) - (stream (gensym))) +(defmacro with-ml-page ((&key (format :html) (precompute t) headers) + &body body) + (let ((fmt (gensym "FMT-")) + (precomp (gensym "PRE-")) + (result (gensym "RES-")) + (outstr (gensym "STR-")) + (stream (gensym "STRM-")) + (hdr (gensym "HDR-"))) `(let ((,fmt ,format) (,precomp ,precompute) - ,result ,outstr) + ,result ,outstr ,stream) + (declare (ignorable ,stream)) (write-header-line "Status" "200 OK") (write-header-line "Content-Type" (format-string ,fmt)) + (dolist (,hdr ,headers) + (write-header-line (car ,hdr) (cdr ,hdr))) (unless ,precomp - (write-string "end" *apache-socket*) - (write-char #\NewLine *apache-socket*)) + (write-string "end" *modlisp-socket*) + (write-char #\NewLine *modlisp-socket*)) (setq ,outstr (with-output-to-string (,stream) - (let ((*apache-socket* (if ,precomp + (let ((*modlisp-socket* (if ,precomp ,stream - *apache-socket*))) + *modlisp-socket*))) (setq ,result (progn ,@body))))) (cond (,precomp - (write-header-line "Content-Length" (write-to-string (length ,outstr))) + (write-header-line "Content-Length" + (write-to-string (length ,outstr))) (write-header-line "Keep-Socket" "1") - (write-string "end" *apache-socket*) - (write-char #\NewLine *apache-socket*) - (write-string ,outstr *apache-socket*) - (setq *close-apache-socket* nil)) + (write-header-line "Connection" "Keep-Alive") + (write-string "end" *modlisp-socket*) + (write-char #\NewLine *modlisp-socket*) + (write-string ,outstr *modlisp-socket*) + (finish-output *modlisp-socket*) + (setq *close-modlisp-socket* nil)) (t - (finish-output *apache-socket*) - (setq *close-apache-socket* t))) + (finish-output *modlisp-socket*) + (setq *close-modlisp-socket* t))) ,result))) (defun redirect-to-location (url) - (write-header-line "Status" "302 Redirect") + (write-header-line "Status" "307 Temporary Redirect") (write-header-line "Location" url) - (write-char #\NewLine *apache-socket*) - (setq *close-apache-socket* t)) + ;;(write-header-line "Keep-Socket" "1") + ;;(write-header-line "Connection" "Keep-Alive") + (write-string "end" *modlisp-socket*) + (write-char #\NewLine *modlisp-socket*) + (force-output *modlisp-socket*) + (setq *close-modlisp-socket* t)) (defun output-ml-page (format html) (write-header-line "Status" "200 OK") (write-header-line "Content-Type" (format-string format)) (write-header-line "Content-Length" (format nil "~d" (length html))) (write-header-line "Keep-Socket" "1") - (write-string "end" *apache-socket*) - (write-char #\NewLine *apache-socket*) - (write-string html *apache-socket*) - (setq *close-apache-socket* nil)) + (write-header-line "Connection" "Keep-Alive") + (write-string "end" *modlisp-socket*) + (write-char #\NewLine *modlisp-socket*) + (write-string html *modlisp-socket*) + (force-output *modlisp-socket*) + (setq *close-modlisp-socket* nil)) (defun output-html-page (str) (output-ml-page :html str)) @@ -74,16 +88,23 @@ (defun output-xml-page (str) (output-ml-page :xml str)) -(defun posted-to-alist (posted-string) - "Converts a posted string to an assoc list of variable names and values" +;; Utility functions for library users + +(defun query-to-alist (posted-string &key (keyword t)) + "Converts a posted string to an assoc list of keyword names and values, +\"a=1&bc=demo\" => ((:a . \"1\") (:bc . \"demo\"))" (when posted-string (let ((alist '())) (dolist (name=val (kmrcl:delimited-string-to-list posted-string #\&) (nreverse alist)) (let ((name-val-list (kmrcl:delimited-string-to-list name=val #\=))) - (when (= 2 (length name-val-list)) + (if (= 2 (length name-val-list)) (destructuring-bind (name val) name-val-list - (push (cons (kmrcl:ensure-keyword name) - (decode-uri-query-string val)) - alist)))))))) + (push (cons (if keyword + (kmrcl:ensure-keyword name) + name) + (kmrcl:decode-uri-query-string val)) + alist)) + (cmsg-c :debug "Invalid number of #\= in ~S" name-val-list))))))) +