I need to redirect users to an absolute URL following oAuth authentication.
How do I construct an absolute URL for a Compojure route? Non-AJAX HTTP requests seem to omit the Origin
header. Is there a Ring or Compojure helper function to build absolute URLs, or should I do this manually with the scheme and Host
headers?
Lastly, and probably deserving of a separate question, are there helper functions in Compojure to generate route URLs based on the handler, ala Html.ActionLink(...)
in MVC land?
The ring-headers project has a middleware that transforms relative to absolute urls:
(ns ring.middleware.absolute-redirects
"Middleware for correcting relative redirects so they adhere to the HTTP RFC."
(:require [ring.util.request :as req])
(:import [java.net URL MalformedURLException]))
(defn- url? [^String s]
(try (URL. s) true
(catch MalformedURLException _ false)))
(defn absolute-url [location request]
(if (url? location)
location
(let [url (URL. (req/request-url request))]
(str (URL. url location)))))