public
Created

Dealing with redirects across schemes in Ring

  • Download Gist
redirect.clj
Clojure
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
[ring.util.response :as res]))
 
; This is the code I've currently got doing redirects between http and https
 
(def port-override {})
 
(def standard-ports {:http 80 :https 443})
 
(defn set-override [scheme port]
(if-not (= port (standard-ports scheme))
(alter-var-root #'port-override assoc scheme port)))
 
(defn url [{:keys [scheme server-name] :as request} url requested-scheme]
(if (= scheme requested-scheme)
url
(let [port (port-override requested-scheme)]
(str (name requestedScheme)
"://" server-name (if port (str ":" port)) url))))
 
(defn redirect [request destination scheme]
(res/redirect (url request destination :https)))
 
(defn report-ports [http https]
(println (str "Ports reported " http "," https))
(set-override :http http)
(set-override :https https))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.