2014-09-26 33 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)))))