1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: wol -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: project.lisp
6 ;;;; Purpose: Project handler for wol library
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: July 2003
10 ;;;; $Id: project.lisp,v 1.10 2003/08/09 22:18:32 kevin Exp $
12 ;;;; This file and Wol are Copyright (c) 2001-2003 by Kevin M. Rosenberg
13 ;;;; *************************************************************************
17 (defun wol-project (name &key (project-prefix "/") map index
18 (sessions t) (session-lifetime 18000)
19 (reap-interval 300) server
24 (:modlisp ml:*ml-server*)
25 (:aserve net.aserve:*wserver*))))
28 (warn "Can't start project without server")
29 (return-from wol-project nil))
31 (multiple-value-bind (project found) (gethash name *active-projects*)
33 (setq project (make-instance 'wol-project))
34 (setf (gethash name *active-projects*) project))
36 (setf (project-name project) name)
37 (setf (project-prefix project) project-prefix)
38 (setf (project-map project) map)
39 (setf (project-index project) index)
40 (setf (project-server project) server)
41 (setf (project-connector project) connector)
42 (setf (lifetime (session-master project)) session-lifetime)
43 (setf (cookie-name (session-master project)) name)
45 (let ((hash (make-hash-table :size (length map) :test 'equal)))
46 (dolist (map-item map)
47 (setf (gethash (first map-item) hash) (second map-item)))
48 (setf (project-hash-map project) hash))
52 (setf (ml::processor server) 'wol-ml-processor))
54 (net.aserve:publish-prefix :prefix project-prefix
56 :function 'wol-aserve-processor)))
59 (when (null (sessions (session-master project)))
60 (setf (sessions (session-master project))
61 (make-hash-table :test 'eq)))
62 (when (sessions (session-master project))
63 (setf (sessions (session-master project)) nil)))
65 (setq *reap-interval* reap-interval)
66 (when (and sessions (null *reaper-process*))
67 (setq *reaper-process* (start-reaper)))))
69 (defun wol-ml-processor (command)
70 "Processes an incoming modlisp command"
71 (let* ((req (command->request command
72 :ml-server *ml-server*))
73 (ent (make-entity-for-request req)))
75 (dispatch-request req ent)
76 (no-url-handler req ent))))
79 (defun wol-aserve-processor (as-req as-ent)
80 "Processes an incoming modlisp command"
81 (multiple-value-bind (req ent) (make-request/ent-from-aserve as-req as-ent)
82 (dispatch-request req ent)))
86 (defun make-request/ent-from-aserve (as-req as-ent)
87 (let* ((req (make-instance
89 :method (net.aserve:request-method as-req)
90 ;;:host (net.aserve:request-host as-req)
91 :raw-request (net.aserve::request-raw-request as-req)
92 :raw-uri (puri:intern-uri
94 (net.aserve:request-raw-uri as-req) nil))
96 (net.aserve::request-decoded-uri-path as-req)
99 (net.aserve:request-uri as-req) nil))
100 :protocol (net.aserve:request-protocol as-req)
102 (net.aserve:request-protocol-string as-req)
103 :posted-content (net.aserve::request-request-body as-req)
104 :socket (net.aserve:request-socket as-req)
105 :headers (net.aserve::request-headers as-req)
106 :aserve-server net.aserve:*wserver*
107 :aserve-request as-req))
108 (ent (make-instance 'entity
109 :project (find-project-for-request req)
110 :aserve-entity as-ent)))
114 (defun command->request (command &key ml-server)
115 "Convert a cl-modlisp command into a wol request"
117 (make-instance 'http-request
118 :host (header-value command :host)
119 :raw-request (header-value command :url)
120 :raw-uri (puri:intern-uri (header-value command :url))
121 :uri (puri:intern-uri (command->uri command))
122 :protocol (ensure-keyword
123 (header-value command :server-protocol))
124 :protocol-string (header-value command :server-protocol)
125 :method (ensure-keyword (header-value command :method))
126 :posted-content (header-value command :posted-content)
128 :socket *modlisp-socket*
129 :ml-server ml-server)))
130 (awhen (request-raw-uri req)
131 (setf (request-decoded-uri-path req) (puri:uri-path it)))
134 (defun header-slot-value (req slot)
135 (header-value (request-headers req) slot))
137 (defun command->uri (command)
138 (format nil "http://~A:~D~A"
139 (header-value command :host)
140 (header-value command :server-ip-port)
141 (header-value command :url)))
143 (defun is-index-request (req ent)
144 (string= (request-decoded-uri-path req)
145 (project-prefix (entity-project ent))))
147 (defun set-cookie (req ent)
148 (let ((session (websession-from-req req)))
149 (when (and session (websession-key session)
150 (not (eq :url (websession-method session))))
151 (let ((proj (entity-project ent)))
152 (ecase (project-connector proj)
154 (net.aserve:set-cookie-header (aserve-request req)
156 (entity-project ent))
160 :value (websession-key
161 (websession-from-req req))
168 (defun redirect-entity (page req ent &optional plist)
169 (let ((proj (entity-project ent))
171 (copy-uri (request-uri req)
172 :path (make-wol-url page req ent plist))
174 (ecase (project-connector proj)
176 (net.aserve:with-http-response
177 ((aserve-request req)
178 (entity-aserve-entity ent)
179 :response net.aserve:*response-temporary-redirect*)
181 (net.aserve:with-http-body
182 ((aserve-request req)
183 (entity-aserve-entity ent)
184 :headers `((:location . ,url))))))
186 (redirect-to-location url)))))
188 (defun dispatch-request (req ent)
189 (let ((proj (entity-project ent)))
190 (if (is-index-request req ent)
191 (redirect-entity (project-index proj) req ent)
193 (compute-uris req ent)
194 (dispatch-to-handler req ent)))))
196 (defun make-entity (&key project)
197 (make-instance 'entity :project project))
199 (defun make-entity-for-request (req)
200 (awhen (find-project-for-request req)
201 (make-entity :project it)))
203 (defun find-project-for-request (req)
204 (maphash (lambda (name project)
205 (declare (ignore name))
206 (when (request-matches-prefix req (project-prefix project))
207 (return-from find-project-for-request project)))
210 (defun request-matches-prefix (req prefix)
211 "Returns project if request matches project"
212 (string-starts-with prefix (request-decoded-uri-path req)))
215 (defun dispatch-to-handler (req ent)
216 (let ((handler (request-find-handler req ent))
217 (*wol-stream* (request-socket req)))
219 (handle-request handler req ent)
220 (no-url-handler req ent))))
222 (defun request-find-handler (req ent)
223 (nth-value 0 (gethash (request-page req)
224 (project-hash-map (entity-project ent)))))
226 (defun handle-request (handler req ent)
230 ((or symbol function)
231 (when (and (symbolp handler)
232 (not (fboundp handler)))
233 (cmsg "handler given a symbol without a function ~S" handler)
234 (return-from handle-request nil))
235 (let ((next-page (funcall handler req ent)))
238 (redirect-entity next-page req ent))
240 (redirect-entity (car next-page) req ent (cadr next-page)))
244 (cmsg "handler should return nil or a string, not ~S" next-page))))
247 (cmsg "string handler not supported: ~A" handler)
250 (cmsg "unknown handler type: ~S" handler)
255 (defun wol-version-string ()
256 (format nil "~{~D~^.~}" *wol-version*))
258 (defun alist-key->keyword (alist)
260 collect (cons (kmrcl:ensure-keyword (car a)) (cdr a))))
262 (defun request-query (req &key (uri t) (post t))
263 (aif (aserve-request req)
265 (net.aserve:request-query it :uri uri :post post))
266 (let ((desired (cons uri post)))
267 (if (equal desired (request-desired-query req))
268 ;; Same desired as cached
269 (request-query-alist req)
271 (setf (request-desired-query req) desired)
272 (setf (request-query-alist req)
274 (when (and uri (request-uri-query req))
275 (query-to-alist (request-uri-query req)))
276 (when (and post (request-posted-content req))
277 (query-to-alist (request-posted-content req))))))))))
279 (defun request-query-value (key req &key (uri t) (post t))
280 (aif (aserve-request req)
281 (net.aserve:request-query-value (string key) it :uri uri :post post)
282 (cdr (assoc key (request-query req :uri uri :post post)
285 (defun websession-variable (ws name)
287 (gethash name (websession-variables ws))))
289 (defun (setf websession-variable) (value ws name)
291 (setf (gethash name (websession-variables ws)) value)))
294 (defmacro with-wol-page ((req ent
295 &key (format :html) (precompute t) headers)
297 `(ecase (project-connector (entity-project ,ent))
299 (net.aserve:with-http-response
300 ((aserve-request ,req)
301 (entity-aserve-entity ,ent)
302 :content-type (ml::format-string ,format))
303 (set-cookie ,req ,ent)
304 (net.aserve:with-http-body
305 ((aserve-request ,req)
306 (entity-aserve-entity ,ent)
308 (let ((*html-stream* net.html.generator:*html-stream*))
311 (%with-wol-page (,req ,ent :format ,format :precompute ,precompute
316 (defmacro %with-wol-page ((req ent
317 &key (format :html) (precompute t) headers)
319 (declare (ignore req ent))
320 (let ((fmt (gensym "FMT-"))
321 (precomp (gensym "PRE-"))
322 (result (gensym "RES-"))
323 (outstr (gensym "STR-"))
324 (stream (gensym "STRM-"))
325 (hdr (gensym "HDR-")))
326 `(let ((,fmt ,format)
327 (,precomp ,precompute)
328 ,result ,outstr ,stream)
329 (declare (ignorable ,stream))
330 (write-header-line "Status" "200 OK")
331 (write-header-line "Content-Type" (ml::format-string ,fmt))
332 (dolist (,hdr ,headers)
333 (write-header-line (car ,hdr) (cdr ,hdr)))
335 (write-string "end" *wol-stream*)
336 (write-char #\NewLine *wol-stream*))
338 (with-output-to-string (,stream)
339 (let ((*html-stream* (if ,precomp
342 (*wol-stream* (if ,precomp
345 (setq ,result (progn ,@body)))))
348 (write-header-line "Content-Length"
349 (write-to-string (length ,outstr)))
350 (write-header-line "Keep-Socket" "1")
351 (write-header-line "Connection" "Keep-Alive")
352 (write-string "end" *wol-stream*)
353 (write-char #\NewLine *wol-stream*)
354 (write-string ,outstr *wol-stream*)
355 (finish-output *wol-stream*)
356 (setq *close-modlisp-socket* nil))
358 (finish-output *wol-stream*)
359 (setq *close-modlisp-socket* t)))
363 (defun no-url-handler (req ent)
364 (with-wol-page (req ent)
368 (:title "404 - NotFound"))
371 (:p "The request for "
372 (:b (:write-string (render-uri (request-uri req) nil)))
373 " was not found on this server.")
376 (:write-string (wol-version-string)))))))))