2014-09-26 29 views
3

我需要将用户重定向到oAuth身份验证后的绝对URL。使用Ring和Compojure的绝对路由URL

如何构建Compojure路线的绝对URL?非AJAX HTTP请求似乎省略了Origin标头。是否有Ring或Compojure帮助函数来构建绝对URL?或者我应该使用方案和Host标题手动执行此操作吗?

最后,也许值得一个单独的问题,Compojure中有帮助函数来生成基于处理程序的路径URL,在MVC土地上的ala Html.ActionLink(...)

回答

4

ring-headers项目有一个middleware,要绝对URL转换相对:

(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)))))