r5250: *** empty log message ***
[cl-modlisp.git] / utils.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: modlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          utils.lisp
6 ;;;; Purpose:       Utility functions for modlisp package
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Dec 2002
9 ;;;;
10 ;;;; $Id: utils.lisp,v 1.3 2003/07/08 06:40:00 kevin Exp $
11 ;;;; *************************************************************************
12
13 (in-package #:modlisp)
14
15 (defun format-string (fmt)
16   (case fmt
17     (:html "text/html")
18     (:xml "text/xml")
19     (:text "text/plain")
20     (otherwise fmt)))
21                            
22 (defmacro with-ml-page ((&key (format :html) (precompute t)) &body body)
23   (let ((fmt (gensym))
24         (precomp (gensym))
25         (result (gensym))
26         (outstr (gensym))
27         (stream (gensym)))
28     `(let ((,fmt ,format)
29            (,precomp ,precompute)
30            ,result ,outstr)
31        (write-header-line "Status" "200 OK")
32        (write-header-line "Content-Type" (format-string ,fmt))
33        (unless ,precomp
34          (write-string "end" *apache-socket*)
35          (write-char #\NewLine *apache-socket*))
36        (setq ,outstr
37          (with-output-to-string (,stream)
38            (let ((*apache-socket* (if ,precomp
39                                       ,stream
40                                     *apache-socket*)))
41              (setq ,result (progn ,@body)))))
42        (cond
43         (,precomp
44          (write-header-line "Content-Length" 
45                             (write-to-string (length ,outstr)))
46          (write-header-line "Keep-Socket" "1")
47          (write-string "end" *apache-socket*)
48          (write-char #\NewLine *apache-socket*)
49          (write-string ,outstr *apache-socket*)
50          (force-output *apache-socket*)
51          (set-close-apache-socket nil))
52         (t
53          (set-close-apache-socket t)
54          (finish-output *apache-socket*)))
55        ,result)))
56
57 (defun redirect-to-location (url)
58   (write-header-line "Status" "302 Redirect")
59   (write-header-line "Location" url)
60   (write-char #\NewLine *apache-socket*)
61   (set-close-apache-socket t))
62
63 (defun output-ml-page (format html)
64   (write-header-line "Status" "200 OK")
65   (write-header-line "Content-Type" (format-string format))
66   (write-header-line "Content-Length" (format nil "~d" (length html)))
67   (write-header-line "Keep-Socket" "1")
68   (write-string "end" *apache-socket*)
69   (write-char #\NewLine *apache-socket*)
70   (write-string html *apache-socket*)
71   (set-close-apache-socket nil))
72
73 (defun output-html-page (str)
74   (output-ml-page :html str))
75
76 (defun output-xml-page (str)
77   (output-ml-page :xml str))
78
79 (defun posted-to-alist (posted-string)
80   "Converts a posted string to an assoc list of variable names and values"
81   (when posted-string
82     (let ((alist '()))
83       (dolist (name=val (kmrcl:delimited-string-to-list posted-string #\&)
84                (nreverse alist))
85         (let ((name-val-list (kmrcl:delimited-string-to-list name=val #\=)))
86           (when (= 2 (length name-val-list))
87             (destructuring-bind (name val) name-val-list
88               (push (cons (kmrcl:ensure-keyword name)
89                           (kmrcl:decode-uri-query-string val))
90                     alist))))))))
91
92
93
94
95