如果我在settings.yml中将基本路径追加到approot属性,如下所示
approot="http://localhost:3000/base“
在浏览器中调用此URL将“未找到”显示为消息,而不是正确地提供主页。
我找不到解决这个问题的办法。
..。
我明白了,在提交给yesod任务之前,我必须重写不带基前缀的URL,并且只能在所有的页面链接前面加上审批人。
是否有正确的方法来处理基于URL的非空路径?
发布于 2012-10-02 18:11:06
对此有两种相对简单的方法:
一般来说,如果有一些前端服务器正在调用您的Yesod应用程序(无论是通过反向代理还是通过FastCGI),具有领先路径部分的许可程序是最有用的。
发布于 2012-10-03 14:01:20
下面是M.Snoyman提示,下面是解决这个问题的cleanPath版本,仅用于yesod init生成的ApprootMaster构造函数的接近情况,作为生成模块Foundation.hs的一个补充:
{-# LANGUAGE PackageImports #-}
import qualified Data.Text as Txt
import qualified "url" Network.URL as Url
import qualified Network.HTTP.Types as H
import qualified Data.Text.Encoding as TE
import qualified Data.List as L
import qualified "utf8-string" Data.ByteString.UTF8 as UTF8BS
-- instance Yesod App where
-- cleanPath - A function used to clean up path segments.
-- It returns Right with a clean path or Left with a new set of pieces the user should be redirected to
-- cleanPath :: Yesod a => a -> [Text] -> Either [Text] [Text]
cleanPath master s =
if corrected == s'
then Right $ cutoffBasePrefix s'
else Left $ cutoffBasePrefix corrected -- to be redirected
where
-- avoid redirection on slash ended urls by junking the last piece if it's null
s' = if not (L.null s) && Txt.null (last s) then init s else s
corrected = filter (not . Txt.null) s'
-- strToUtf8BS = TE.encodeUtf8 . Txt.pack -- packs to UTF16 then encodes to UTF8
strToUtf8BS = UTF8BS.fromString
-- cut off "base prefix" or leave as it is
cutoffBasePrefix segmts =
case approot of
ApprootMaster f ->
case Url.importURL $ Txt.unpack $ f master of
Nothing -> segmts -- not expected, incorrect url in settings.yml approot
Just url -> let basePrefixSegmts = H.decodePathSegments $ strToUtf8BS $ Url.url_path url in
case basePrefixSegmts of
[] -> segmts
_ -> if basePrefixSegmts `L.isPrefixOf` segmts
then drop (length basePrefixSegmts) segmts
else segmts
_ -> segmts对于附加的包依赖项:
, url >= 2.1.2
, network
, http-types
, utf8-stringhttps://stackoverflow.com/questions/12694140
复制相似问题