【问题标题】:Absolute Route URLs with Ring and Compojure使用 Ring 和 Compojure 的绝对路由 URL
【发布时间】:2014-11-21 05:16:59
【问题描述】:
我需要在 oAuth 身份验证后将用户重定向到绝对 URL。
如何为 Compojure 路由构造绝对 URL?非 AJAX HTTP 请求似乎省略了 Origin 标头。是否有 Ring 或 Compojure 辅助函数来构建绝对 URL,或者我应该使用方案和 Host 标头手动执行此操作?
最后,也许值得提出一个单独的问题,Compojure 中是否有帮助函数来基于处理程序生成路由 URL,比如 MVC 领域中的 Html.ActionLink(...)?
【问题讨论】:
标签:
clojure
absolute-path
compojure
ring
【解决方案1】:
ring-headers 项目有一个middleware 相对于绝对网址进行转换:
(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)))))