forked from kennytilton/qooxlisp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hunch.lisp
75 lines (59 loc) · 2.93 KB
/
hunch.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(in-package :qxl)
(defclass hunchentoot-backend (qooxlisp-server-backend)
((acceptor :accessor acceptor :initform nil)))
(defvar *default-hunchentoot-dispatch-table* hunchentoot:*dispatch-table*)
(defmethod shutdown-backend ((backend hunchentoot-backend))
(let ((ac (acceptor backend)))
(when ac
(hunchentoot:stop ac)
(setf hunchentoot:*dispatch-table* *default-hunchentoot-dispatch-table*))))
(defun add-dispatcher (dispatcher)
(push dispatcher hunchentoot:*dispatch-table*))
(defmethod backend-debug-off ((backend hunchentoot-backend)))
(defmethod start-backend ((backend hunchentoot-backend) &key port)
(let ((ac (make-instance 'hunchentoot:acceptor :port port)))
(hunchentoot:start ac)
(setf (acceptor backend) ac)))
(defmethod backend-publish-file ((backend hunchentoot-backend)
&key port path file)
(declare (ignore port)) ;; We expect that we only use one port
(assert (probe-file file))
(add-dispatcher
(hunchentoot:create-static-file-dispatcher-and-handler path file)))
(defmethod backend-publish-directory ((backend hunchentoot-backend)
&key port prefix destination)
(declare (ignore port)) ;; We expect that we only use one port
(add-dispatcher
(hunchentoot:create-folder-dispatcher-and-handler prefix destination)))
;;; Allegroserve code often returns nil when hunchentoot excepts string data,
;;; for safety we save strings during processing of callback functions.
(defvar *response-strings*)
(defmethod backend-publish-function ((backend hunchentoot-backend)
&key path function)
(add-dispatcher
(hunchentoot:create-prefix-dispatcher
path
(lambda ()
(let ((*response-strings* nil))
(funcall function hunchentoot:*request* 'ent-unused)
(if (= 1 (length *response-strings*))
(first *response-strings*) ; this is probably almost always the case
(apply #'concatenate 'string (nreverse *response-strings*))))))))
(defmethod backend-get-raw-request ((backend hunchentoot-backend) request)
(hunchentoot:raw-post-data :request request :force-text t))
(defmethod render-unescaped-response ((backend hunchentoot-backend) string)
(push string *response-strings*)
string)
(defmethod backend-js-response ((backend hunchentoot-backend) req ent function)
(declare (ignore req ent))
(setf (hunchentoot:content-type*) "text/javascript")
(render-unescaped-response backend (funcall function)))
(defmethod backend-json-response ((backend hunchentoot-backend) req ent function)
(declare (ignore req ent))
(setf (hunchentoot:content-type*) "application/json")
(render-unescaped-response backend (funcall function)))
(defmethod backend-request-value ((backend hunchentoot-backend) request tag)
(hunchentoot:get-parameter tag request))
(unless *default-backend-classname*
(setf *default-backend-classname* 'hunchentoot-backend)
(initialize-backend))