hunchentoot define-easy-handler with ssl?

不羁岁月 提交于 2019-12-01 08:41:52

You can keep your easy-handlers and change the type of acceptor you need.

(defpackage :web (:use :cl :hunchentoot))
(in-package :web)

;; This url can be accessed by all acceptors
(define-easy-handler (no-ssl :uri "/normal") ()
  (setf (content-type*) "text/plain")
  "NORMAL PAGE")

;; This url can be accessed only by an acceptor named SSL
(define-easy-handler (ssl :uri "/secure" :acceptor-names '(ssl)) ()
  (setf (content-type*) "text/plain")
  "SECURED PAGE")

If you don't have a self-signed certificate for tests, you can do:

$ cd /tmp
$ openssl req -new -x509 -nodes -out server.crt -keyout server.key

Then, we define two kinds of acceptors:

(defvar *no-ssl-acceptor*
  (make-instance 'easy-acceptor :port 8080))

(defvar *ssl-acceptor*
  (make-instance 'easy-ssl-acceptor
                 :name 'ssl
                 :port 7777
                 :ssl-privatekey-file  #P"/tmp/server.key"
                 :ssl-certificate-file #P"/tmp/server.crt"))

Start them:

(start *ssl-acceptor*)
(start *no-ssl-acceptor*)

Your browser should complain the first time you try to access HTTPS pages (ignore the security exception).

This is not a function of the handlers but of the acceptor. All you need to do is use an easy-ssl-acceptor instead of an easy-acceptor for starting your server:

(hunchentoot:start (make-instance 'hunchentoot:easy-ssl-acceptor :port 4242))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!