/
acceptor.lisp
59 lines (47 loc) · 2.31 KB
/
acceptor.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
(in-package :weblocks)
(export '(weblocks-acceptor
weblocks-ssl-acceptor
ssl-redirect-acceptor))
(defclass weblocks-acceptor (#.(if (find-class 'easy-acceptor nil)
'easy-acceptor
'acceptor))
((session-cookie-name :type string :accessor session-cookie-name
:initarg :session-cookie-name
:initform (format nil "weblocks-~(~A~)" (gensym)))))
(defmethod initialize-instance :after ((inst weblocks-acceptor) &rest initargs)
"Set the session secret to prevent a Hunchentoot warning emitted upon
starting the acceptor."
(unless (boundp 'hunchentoot:*session-secret*)
(hunchentoot:reset-session-secret)))
(defmethod process-connection ((acceptor weblocks-acceptor) socket)
;; CCL uses predictable random states for new threads
#+ccl(setf *random-state* (make-random-state t))
(let ((*print-readably* nil))
(call-next-method)))
(defmethod acceptor-status-message :around ((acceptor weblocks-acceptor) (http-status-code (eql hunchentoot:+http-internal-server-error+)) &key &allow-other-keys)
nil)
(defmethod acceptor-status-message :around ((acceptor weblocks-acceptor) (http-status-code (eql hunchentoot:+http-not-found+)) &key &allow-other-keys)
nil)
;;; To support both http: and https:, call START-WEBLOCKS twice, once with
;;; :ACCEPTOR-CLASS 'WEBLOCKS-SSL-ACCEPTOR, once using the default acceptor.
;;; To force https:, call START-WEBLOCKS with :ACCEPTOR-CLASS 'WEBLOCKS-SSL-ACCEPTOR,
;;; and also do
;;;
;;; (hunchentoot:start (make-instance 'ssl-redirect-acceptor))
;;;
(defclass weblocks-ssl-acceptor (weblocks-acceptor ssl-acceptor)
())
(defclass ssl-redirect-acceptor (acceptor)
((ssl-port :reader ssl-redirect-acceptor-ssl-port
:initarg :ssl-port
:initform 443
:documentation
"The port used by the SSL acceptor."))
(:documentation
"A very simple acceptor for handling non-SSL requests and redirecting them
to the SSL port."))
(defmethod acceptor-dispatch-request ((acceptor ssl-redirect-acceptor) request)
(hunchentoot:redirect (request-uri* request)
:protocol ':https
:port (ssl-redirect-acceptor-ssl-port acceptor)
:add-session-id nil))