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
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
25 (:modlisp ml:*ml-server*)
26 (:aserve net.aserve:*wserver*))))
29 (warn "Can't start project without server")
30 (return-from wol-project nil))
32 (multiple-value-bind (project found) (gethash name *active-projects*)
34 (setq project (make-instance 'wol-project))
35 (setf (gethash name *active-projects*) project))
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)
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))
53 (setf (ml::processor server) 'wol-ml-processor))
55 (net.aserve:publish-prefix :prefix project-prefix
57 :function 'wol-aserve-processor
61 (when (null (sessions (session-master project)))
62 (setf (sessions (session-master project))
63 (make-hash-table :test 'eq)))
64 (when (sessions (session-master project))
65 (setf (sessions (session-master project)) nil)))
67 (setq *reap-interval* reap-interval)
68 (when (and sessions (null *reaper-process*))
69 (setq *reaper-process* (start-reaper)))))
71 (defun wol-ml-processor (command)
72 "Processes an incoming modlisp command"
73 (let* ((req (command->request command
74 :ml-server *ml-server*))
75 (ent (make-entity-for-request req)))
77 (dispatch-request req ent)
78 (no-url-handler req ent))))
81 (defun wol-aserve-processor (as-req as-ent)
82 "Processes an incoming modlisp command"
83 (multiple-value-bind (req ent) (make-request/ent-from-aserve as-req as-ent)
84 (if (entity-project ent)
85 (dispatch-request req ent)
86 (no-url-handler req ent))))
89 (defun make-request/ent-from-aserve (as-req as-ent)
90 (let* ((req (make-instance
92 :method (net.aserve:request-method as-req)
93 ;;:host (net.aserve:request-host as-req)
94 :raw-request (net.aserve::request-raw-request as-req)
95 :raw-uri (puri:intern-uri
97 (net.aserve:request-raw-uri as-req) nil))
99 (net.aserve::request-decoded-uri-path as-req)
100 :uri (puri:intern-uri
102 (net.aserve:request-uri as-req) nil))
103 :protocol (net.aserve:request-protocol as-req)
105 (net.aserve:request-protocol-string as-req)
106 :posted-content (net.aserve::request-request-body as-req)
107 :socket (net.aserve:request-socket as-req)
108 :headers (net.aserve::request-headers as-req)
109 :aserve-server net.aserve:*wserver*
110 :aserve-request as-req))
111 (project (find-project-for-request req))
112 (ent (make-instance 'entity :project project
113 :aserve-entity as-ent)))
117 (defun command->request (command &key ml-server)
118 "Convert a cl-modlisp command into a wol request"
120 (make-instance 'http-request
121 :host (header-value command :host)
122 :raw-request (header-value command :url)
123 :raw-uri (puri:intern-uri (header-value command :url))
124 :uri (puri:intern-uri (command->uri command))
125 :protocol (ensure-keyword
126 (header-value command :server-protocol))
127 :protocol-string (header-value command :server-protocol)
128 :method (ensure-keyword (header-value command :method))
129 :posted-content (header-value command :posted-content)
131 :socket *modlisp-socket*
132 :ml-server ml-server)))
133 (awhen (request-raw-uri req)
134 (setf (request-decoded-uri-path req) (puri:uri-path it)))
137 (defun header-slot-value (req slot)
138 (header-value (request-headers req) slot))
140 (defun command->uri (command)
141 (format nil "http://~A:~D~A"
142 (header-value command :host)
143 (header-value command :server-ip-port)
144 (header-value command :url)))
146 (defun is-index-request (req ent)
147 (string= (request-decoded-uri-path req)
148 (project-prefix (entity-project ent))))
150 (defun set-cookie (req ent)
151 (let ((session (websession-from-req req)))
152 (when (and session (websession-key session)
153 (not (eq :url (websession-method session))))
154 (let ((proj (entity-project ent)))
155 (ecase (project-connector proj)
157 (net.aserve:set-cookie-header (aserve-request req)
159 (entity-project ent))
163 :value (websession-key
164 (websession-from-req req))
171 (defun redirect-entity (page req ent &optional plist)
172 (let ((proj (entity-project ent))
174 (copy-uri (request-uri req)
175 :path (make-wol-url page req ent plist))
177 (ecase (project-connector proj)
179 (net.aserve:with-http-response
180 ((aserve-request req)
181 (entity-aserve-entity ent)
182 :response net.aserve:*response-temporary-redirect*)
184 (net.aserve:with-http-body
185 ((aserve-request req)
186 (entity-aserve-entity ent)
187 :headers `((:location . ,url))))))
189 (redirect-to-location url)))))
191 (defun dispatch-request (req ent)
194 (let ((proj (entity-project ent)))
195 (if (is-index-request req ent)
196 (redirect-entity (project-index proj) req ent)
198 (compute-uris req ent)
199 (dispatch-to-handler req ent)))))
201 (defun make-entity (&key project)
202 (make-instance 'entity :project project))
204 (defun make-entity-for-request (req)
205 (awhen (find-project-for-request req)
206 (make-entity :project it)))
208 (defun find-project-for-request (req)
209 (maphash (lambda (name project)
210 (declare (ignore name))
211 (when (and (eq (project-server project)
212 (or (request-aserve-server req)
213 (request-ml-server req)))
214 (request-matches-prefix
215 req (project-prefix project)))
216 (return-from find-project-for-request project)))
219 (defun request-matches-prefix (req prefix)
220 "Returns project if request matches project"
221 (string-starts-with prefix (request-decoded-uri-path req)))
224 (defun dispatch-to-handler (req ent)
225 (let ((handler (request-find-handler req ent))
226 (*wol-stream* (request-socket req)))
228 (handle-request handler req ent)
229 (no-url-handler req ent))))
231 (defun request-find-handler (req ent)
232 (nth-value 0 (gethash (request-page req)
233 (project-hash-map (entity-project ent)))))
235 (defun handle-request (handler req ent)
239 ((or symbol function)
240 (when (and (symbolp handler)
241 (not (fboundp handler)))
242 (cmsg "handler given a symbol without a function ~S" handler)
243 (return-from handle-request nil))
244 (let ((next-page (funcall handler req ent)))
247 (redirect-entity next-page req ent))
249 (redirect-entity (car next-page) req ent (cadr next-page)))
253 (cmsg "handler should return nil or a string, not ~S" next-page))))
256 (cmsg "string handler not supported: ~A" handler)
259 (cmsg "unknown handler type: ~S" handler)
264 (defun wol-version-string ()
265 (format nil "~{~D~^.~}" *wol-version*))
267 (defun alist-key->keyword (alist)
269 collect (cons (kmrcl:ensure-keyword (car a)) (cdr a))))
271 (defun request-query (req &key (uri t) (post t))
272 (aif (aserve-request req)
274 (net.aserve:request-query it :uri uri :post post))
275 (let ((desired (cons uri post)))
276 (if (equal desired (request-desired-query req))
277 ;; Same desired as cached
278 (request-query-alist req)
280 (setf (request-desired-query req) desired)
281 (setf (request-query-alist req)
283 (when (and uri (request-uri-query req))
284 (query-to-alist (request-uri-query req)))
285 (when (and post (request-posted-content req))
286 (query-to-alist (request-posted-content req))))))))))
288 (defun request-query-value (key req &key (uri t) (post t))
289 (aif (aserve-request req)
290 (net.aserve:request-query-value (string key) it :uri uri :post post)
291 (cdr (assoc key (request-query req :uri uri :post post)
294 (defun websession-variable (ws name)
296 (gethash name (websession-variables ws))))
298 (defun (setf websession-variable) (value ws name)
300 (setf (gethash name (websession-variables ws)) value)))
303 (defmacro with-wol-page ((req ent
304 &key (format :html) (precompute t) headers
308 `(if (request-aserve-server ,req)
309 (net.aserve:with-http-response
310 ((aserve-request ,req)
311 (entity-aserve-entity ,ent)
312 :content-type (ml::format->string ,format)
316 (302 net.aserve::*response-moved-permanently*)
317 (307 net.aserve::*response-temporary-redirect*)
318 (404 net.aserve::*response-not-found*)
319 (otherwise net.aserve::*response-ok*)))
320 (set-cookie ,req ,ent)
321 (net.aserve:with-http-body
322 ((aserve-request ,req)
323 (entity-aserve-entity ,ent)
325 (let ((*html-stream* net.html.generator:*html-stream*))
327 (%with-wol-page (,req ,ent :format ,format :precompute ,precompute
331 (302 "302 Moved Permanently")
332 (307 "307 Temporary Redirect")
333 (404 "404 Not Found")
334 (otherwise "200 OK")))
338 (defmacro %with-wol-page ((req ent
339 &key (format :html) (precompute t) headers
340 (response-string "200 OK"))
342 (declare (ignore req ent))
343 (let ((fmt (gensym "FMT-"))
344 (precomp (gensym "PRE-"))
345 (result (gensym "RES-"))
346 (outstr (gensym "STR-"))
347 (stream (gensym "STRM-"))
348 (hdr (gensym "HDR-")))
349 `(let ((,fmt ,format)
350 (,precomp ,precompute)
351 ,result ,outstr ,stream)
352 (declare (ignorable ,stream))
353 (write-header-line "Status" ,response-string)
354 (write-header-line "Content-Type" (ml::format->string ,fmt))
355 (dolist (,hdr ,headers)
356 (write-header-line (car ,hdr) (cdr ,hdr)))
358 (write-string "end" *wol-stream*)
359 (write-char #\NewLine *wol-stream*))
361 (with-output-to-string (,stream)
362 (let ((*html-stream* (if ,precomp
365 (*wol-stream* (if ,precomp
368 (setq ,result (progn ,@body)))))
371 (write-header-line "Content-Length"
372 (write-to-string (length ,outstr)))
373 (write-header-line "Keep-Socket" "1")
374 (write-header-line "Connection" "Keep-Alive")
375 (write-string "end" *wol-stream*)
376 (write-char #\NewLine *wol-stream*)
377 (write-string ,outstr *wol-stream*)
378 (finish-output *wol-stream*)
379 (setq *close-modlisp-socket* nil))
381 (finish-output *wol-stream*)
382 (setq *close-modlisp-socket* t)))
386 (defun no-url-handler (req ent)
387 (with-wol-page (req ent :response-code 404)
391 (:title "404 - NotFound"))
394 (:p "The request for "
395 (:b (:write-string (render-uri (request-uri req) nil)))
396 " was not found on this server.")
399 (:write-string (wol-version-string)))))))))