r5339: *** empty log message ***
[wol.git] / project.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: wol -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          project.lisp
6 ;;;; Purpose:       Project handler for wol library
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  July 2003
9 ;;;;
10 ;;;; $Id: project.lisp,v 1.4 2003/07/19 20:32:48 kevin Exp $
11 ;;;;
12 ;;;; This file and Wol are Copyright (c) 2001-2003 by Kevin M. Rosenberg
13 ;;;; *************************************************************************
14
15 (in-package #:wol)
16
17 (defun wol-project (name &key (project-prefix "/") map index
18                     (sessions t) (session-lifetime 18000)
19                     (reap-interval 300) server
20                     (connector :modlisp))
21
22   (unless server
23     (setq server 
24           (ecase connector
25             (:modlisp ml:*ml-server*)
26             (:aserve net.aserve:*wserver*))))
27   
28   (unless server
29     (warn "Can't start project without server")
30     (return-from wol-project nil))
31   
32   (multiple-value-bind (project found) (gethash name *active-projects*)
33     (unless found
34       (setq project (make-instance 'wol-project))
35       (setf (gethash name *active-projects*) project))
36     
37     (setf (project-name project) name)
38     (setf (project-prefix project) project-prefix)
39     (setf (project-map project) map)
40     (setf (project-index project) index) 
41     (setf (project-server project) server)
42     (setf (project-connector project) connector)
43     (setf (lifetime (session-master project)) session-lifetime)
44     (setf (cookie-name (session-master project)) name)
45
46     (let ((hash (make-hash-table :size (length map) :test 'equal)))
47       (dolist (map-item map)
48         (setf (gethash (first map-item) hash) (second map-item)))
49       (setf (project-hash-map project) hash))
50
51     (ecase connector
52       (:modlisp
53        (setf (ml::processor server) 'wol-ml-processor))
54       (:aserve
55        (net.aserve:publish-prefix :prefix project-prefix
56                                   :server server
57                                   :function 'wol-aserve-processor)))
58   
59   (if sessions
60         (when (null (sessions (session-master project)))
61           (setf (sessions (session-master project))
62             (make-hash-table :test 'eq)))
63       (when (sessions (session-master project))
64         (setf (sessions (session-master project)) nil)))
65
66     (setq *reap-interval* reap-interval)
67     (when (and sessions (null *reaper-process*))
68       (setq *reaper-process* (start-reaper)))))
69     
70 (defun wol-ml-processor (command)
71   "Processes an incoming modlisp command"
72   (let* ((req (command->request command
73                                 :ml-server *ml-server*))
74          (ent (make-entity-for-request req)))
75     (if ent
76         (dispatch-request req ent)
77         (no-url-handler req ent))))
78
79
80 (defun wol-aserve-processor (as-req as-ent)
81   "Processes an incoming modlisp command"
82   (let* ((req (make-request-from-aserve as-req))
83          (ent (make-entity-from-aserve req as-ent)))
84     (dispatch-request req ent)))
85
86 (defun make-request-from-aserve (as-req)
87   (make-instance 'http-request
88                  :method (net.aserve:request-method as-req)
89                  ;;:host (net.aserve:request-host as-req)
90                  :raw-uri (puri:intern-uri
91                            (net.uri:render-uri
92                             (net.aserve:request-raw-uri as-req) nil))
93                  :uri (puri:intern-uri
94                        (net.uri:render-uri
95                         (net.aserve:request-uri as-req) nil))
96                  :protocol (net.aserve:request-protocol as-req)
97                  :protocol-string
98                  (net.aserve:request-protocol-string as-req)
99                  :posted-content (net.aserve::request-request-body as-req)
100                  :socket (net.aserve:request-socket as-req)
101                  :aserve-server net.aserve:*wserver*
102                  :aserve-request as-req))
103
104 (defun make-entity-from-aserve (req as-ent)
105   (make-instance 'entity
106                  :project (find-project-for-request req)
107                  :aserve-entity as-ent))
108
109
110 (defun command->request (command &key ml-server)
111   "Convert a cl-modlisp command into a wol request"
112   (let ((req
113          (make-instance 'http-request
114            :host (header-value command :host)
115            :raw-uri (puri:intern-uri (header-value command :url))
116            :uri (puri:intern-uri (command->uri command))
117            :protocol (ensure-keyword
118                       (header-value command :server-protocol))
119            :protocol-string (header-value command :server-protocol)
120            :method (ensure-keyword (header-value command :method))
121            :posted-content (header-value command :posted-content)
122            :headers command
123            :socket *modlisp-socket*
124            :ml-server ml-server)))
125     req))
126
127 (defun header-slot-value (req slot)
128   (header-value (request-headers req) slot))
129
130 (defun command->uri (command)
131   (format nil "http://~A:~D~A"
132           (header-value command :host)
133           (awhen (header-value
134                   command :server-ip-port)
135                  (parse-integer it))
136           (header-value command :url)))
137
138 (defun is-index-request (req ent)
139   (string= (puri:uri-path (request-raw-uri req)) 
140            (project-prefix (entity-project ent))))
141
142 (defun redirect-entity (page ent)
143   (redirect-to-location 
144    (format nil "~A~A" (project-prefix (entity-project ent)) page)))
145
146 (defun dispatch-request (req ent)
147   (let ((proj (entity-project ent)))
148     (if (is-index-request req ent)
149         (redirect-entity (project-index proj) ent)
150         (progn
151           (request-decompile-uri req ent)
152           (compute-session req ent)
153           (dispatch-to-handler req ent)))))
154
155 (defun make-entity (&key project)
156   (make-instance 'entity :project project))
157
158 (defun make-entity-for-request (req)
159   (awhen (find-project-for-request req)
160          (make-entity :project it)))
161
162 (defun find-project-for-request (req)
163   (maphash (lambda (name project)
164              (declare (ignore name))
165              (when (request-matches-prefix req (project-prefix project))
166                (return-from find-project-for-request project)))
167            *active-projects*))
168
169 (defun request-matches-prefix (req prefix)
170   "Returns project if request matches project"
171   (string-starts-with prefix (puri:uri-path (request-raw-uri req))))
172
173
174 (defun dispatch-to-handler (req ent)
175   (let ((handler (request-find-handler req ent))
176         (*wol-stream* (request-socket req)))
177     (if handler
178         (handle-request handler req ent)
179       (no-url-handler req ent))))
180
181 (defun request-find-handler (req ent)
182   (nth-value 0 (gethash (request-page req) 
183                         (project-hash-map (entity-project ent)))))
184
185 (defun handle-request (handler req ent)
186   (typecase handler
187     (null
188      nil)
189     ((or symbol function)
190      (when (and (symbolp handler)
191                 (not (fboundp handler)))
192        (cmsg "handler given a symbol without a function ~S" handler)
193        (return-from handle-request nil))
194      (let ((next-page (funcall handler req ent)))
195        (typecase next-page
196          (string
197           (redirect-entity next-page ent))
198          (null
199           t)
200          (t
201           (cmsg "handler should return nil or a string, not ~S" next-page))))
202      t)
203     (string
204      (cmsg "string handler not supported: ~A" handler)
205      nil)
206     (t
207      (cmsg "unknown handler type: ~S" handler)
208      nil)))
209
210
211
212 (defun wol-version-string ()
213   (format nil "~{~D~^.~}" *wol-version*))
214   
215 (defun request-query (req &key (uri t) (post t))
216   (append
217     (when (and uri (request-uri-query req))
218       (aif (request-query-alist req)
219          it
220          (setf (request-query-alist req)
221            (query-to-alist (request-uri-query req)))))
222     (when (and post (request-posted-content req))
223       (query-to-alist (request-posted-content req)))))
224
225 (defun request-query-value (key req &key (uri t) (post t))
226   (cdr (assoc key (request-query req :uri uri :post post)
227               :test 'equal)))
228     
229 (defun websession-variable (ws name)
230   (when ws
231     (gethash name (websession-variables ws))))
232
233 (defun (setf websession-variable) (value ws name)
234   (when ws
235     (setf (gethash name (websession-variables ws)) value)))
236
237
238 (defmacro with-wol-page ((req ent
239                               &key (format :html) (precompute t) headers)
240                          &body body)
241   (let ((fmt (gensym "FMT-"))
242         (precomp (gensym "PRE-"))
243         (result (gensym "RES-"))
244         (outstr (gensym "STR-"))
245         (stream (gensym "STRM-"))
246         (hdr (gensym "HDR-")))
247     `(let ((,fmt ,format)
248            (,precomp ,precompute)
249            ,result ,outstr ,stream)
250        (declare (ignorable ,stream))
251        (write-header-line "Status" "200 OK")
252        (write-header-line "Content-Type" (ml::format-string ,fmt))
253        (dolist (,hdr ,headers)
254          (write-header-line (car ,hdr) (cdr ,hdr)))
255        (unless ,precomp
256          (write-string "end" *wol-stream*)
257          (write-char #\NewLine *wol-stream*))
258        (setq ,outstr
259          (with-output-to-string (,stream)
260            (let ((*html-stream* (if ,precomp
261                                    ,stream
262                                    *wol-stream*))
263                  (*wol-stream* (if ,precomp
264                                    ,stream
265                                    *wol-stream*)))
266              (setq ,result (progn ,@body)))))
267        (cond
268         (,precomp
269          (write-header-line "Content-Length" 
270                             (write-to-string (length ,outstr)))
271          (write-header-line "Keep-Socket" "1")
272          (write-header-line "Connection" "Keep-Alive")
273          (write-string "end" *wol-stream*)
274          (write-char #\NewLine *wol-stream*)
275          (write-string ,outstr *wol-stream*)
276          (finish-output *wol-stream*)
277          (setq *close-modlisp-socket* nil))
278         (t
279          (finish-output *wol-stream*)
280          (setq *close-modlisp-socket* t)))
281        ,result)))
282
283
284 (defun no-url-handler (req ent)
285   (with-wol-page (req ent)
286     (html
287      (:html
288       (:head
289        (:title "404 - NotFound"))
290       (:body
291        (:h1 "Not Found")
292        (:p "The request for "
293            (:b (:write-string (request-uri req)))
294            " was not found on this server.")
295        (:hr)
296        (:div (:i "WOL "
297                  (:write-string (wol-version-string)))))))))