我正在使用Snap信息,我想知道是否有某种类型的Request函数(例如:: Request - > IO Snap()或:: Request - > Handler App App())返回访问网页的用户的操作系统或浏览器信息。
我想获取正在访问该网页的人的操作系统和浏览器信息。
答案 0 :(得分:3)
您可以通过getHeader
获取User-Agent
HTTP标头,因为Request
有HasHeaders
个实例。
示例摘录:
import qualified Data.ByteString.Char8 as CS
import qualified Data.CaseInsensitive as CI
import Data.Maybe (listToMaybe)
uaName :: ByteString
uaName = CS.pack "User-Agent"
-- You can avoid CS.pack with OverloadedStrings extension.
uahName :: CI ByteString
uahName = CI.mk uaName
-- OverloadedStrings also gets rid of the CI.mk call.
getUserAgent :: Request -> Snap (Maybe ByteString)
getUserAgent rq = return . coerce $ getHeader uahName rq
where
coerce :: Maybe [ByteString] -> Maybe ByteString
coerce = (>>= listToMaybe)
-- Some HTTP headers can appear multiple times, hence the list.
-- `coerce` ignores all but the first occurrence.
对于更详细/更少自愿的信息,您可以将JS注入初始请求并设置可以在过滤请求中使用rqCookies
提取的Cookie。