Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to https with hunchentoot

I've set up a hunchentoot server with ssl. I'd like regular http requests to be redirected to https.

It seems like some combination of hunchentoot:define-easy-handler and hunchentoot:redirect is the way to go, but I can't figure it out.

Here's what I have so far:

(defvar *https-handler*
  (make-instance 'hunchentoot:easy-ssl-acceptor
                 :name 'ssl
                 :ssl-privatekey-file #P"/path/to/privkey.pem"
                 :ssl-certificate-file #P"/path/to/cert.pem"
                 :port 443))

(hunchentoot:start *https-handler*)
like image 450
ark Avatar asked May 02 '19 20:05

ark


2 Answers

Yes, you can add simple http handler with redirect to ssl version:

(defvar *http-handler*
  (make-instance 'hunchentoot:easy-acceptor
                 :name 'http
                 :port 80))

(hunchentoot:define-easy-handler (redir-to-ssl :uri (lambda (uri) t) :acceptor-names '(http)) ()
  (hunchentoot:redirect "/" :protocol :https)) ; where magic happens

...and then start it too:

(hunchentoot:start *http-handler*)

This version redirects simply to index /.

like image 101
rsm Avatar answered Oct 29 '22 19:10

rsm


Well, I am using the hunchentoot:*dispatch-table* directly. The way to redirect it independent of the path I discovered was to hunchentoot:redirect unless (hunchentoot:ssl-p) inside the handler. Most of my defuned handlers are wrapped inside a macro for authenthentication. So, I merely had to modify that macro, and then M-x slime-who-macroexpands -> C-c C-k.

(unless (hunchentoot:ssl-p)
  (hunchentoot:redirect (hunchentoot:request-uri*)
                       :protocol :https))
like image 32
digikar Avatar answered Oct 29 '22 19:10

digikar