Ring和Compojure中的绝对路由URL

3

我需要在oAuth身份验证后将用户重定向到一个绝对URL。

如何为Compojure路由构建绝对URL?非AJAX HTTP请求似乎省略了Origin标头。是否有Ring或Compojure辅助函数来构建绝对URL,或者应该手动使用方案和Host标头进行操作?

最后,可能值得单独提出一个问题,在Compojure中是否有帮助程序函数来根据处理程序生成路由URL,类似于MVC地带中的Html.ActionLink(...)

1个回答

4

ring-headers项目有一个中间件,可以将相对URL转换为绝对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)))))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接