Using web-routes again (version 0.23)

This commit is contained in:
Michael Snoyman 2010-09-24 05:10:57 +02:00
parent 7547aaa8fd
commit 60fab19ef6
2 changed files with 5 additions and 84 deletions

View File

@ -4,85 +4,5 @@ module Yesod.WebRoutes
, Site (..)
) where
import Codec.Binary.UTF8.String (encodeString)
import Data.List (intercalate)
import Network.URI
encodePathInfo :: [String] -> [(String, String)] -> String
encodePathInfo pieces qs =
let x = map encodeString `o` -- utf-8 encode the data characters in path components (we have not added any delimiters yet)
map (escapeURIString (\c -> isUnreserved c || c `elem` ":@&=+$,")) `o` -- percent encode the characters
map (\str -> case str of "." -> "%2E" ; ".." -> "%2E%2E" ; _ -> str) `o` -- encode . and ..
intercalate "/" -- add in the delimiters
y = showParams qs
in x pieces ++ y
where
-- reverse composition
o :: (a -> b) -> (b -> c) -> a -> c
o = flip (.)
{-|
A site groups together the three functions necesary to make an application:
* A function to convert from the URL type to path segments.
* A function to convert from path segments to the URL, if possible.
* A function to return the application for a given URL.
There are two type parameters for Site: the first is the URL datatype, the
second is the application datatype. The application datatype will depend upon
your server backend.
-}
data Site url a
= Site {
{-|
Return the appropriate application for a given URL.
The first argument is a function which will give an appropriate
URL (as a String) for a URL datatype. This is usually
constructed by a combination of 'formatPathSegments' and the
prepending of an absolute application root.
Well behaving applications should use this function to
generating all internal URLs.
-}
handleSite :: (url -> [(String, String)] -> String) -> url -> a
-- | This function must be the inverse of 'parsePathSegments'.
, formatPathSegments :: url -> ([String], [(String, String)])
-- | This function must be the inverse of 'formatPathSegments'.
, parsePathSegments :: [String] -> Either String url
}
showParams :: [(String, String)] -> String
showParams [] = ""
showParams z =
'?' : intercalate "&" (map go z)
where
go (x, "") = go' x
go (x, y) = go' x ++ '=' : go' y
go' = concatMap encodeUrlChar
-- | Taken straight from web-encodings; reimplemented here to avoid extra
-- dependencies.
encodeUrlChar :: Char -> String
encodeUrlChar c
-- List of unreserved characters per RFC 3986
-- Gleaned from http://en.wikipedia.org/wiki/Percent-encoding
| 'A' <= c && c <= 'Z' = [c]
| 'a' <= c && c <= 'z' = [c]
| '0' <= c && c <= '9' = [c]
encodeUrlChar c@'-' = [c]
encodeUrlChar c@'_' = [c]
encodeUrlChar c@'.' = [c]
encodeUrlChar c@'~' = [c]
encodeUrlChar ' ' = "+"
encodeUrlChar y =
let (a, c) = fromEnum y `divMod` 16
b = a `mod` 16
showHex' x
| x < 10 = toEnum $ x + (fromEnum '0')
| x < 16 = toEnum $ x - 10 + (fromEnum 'A')
| otherwise = error $ "Invalid argument to showHex: " ++ show x
in ['%', showHex' b, showHex' c]
import Web.Routes.Base (encodePathInfo)
import Web.Routes.Site (Site (..))

View File

@ -13,7 +13,7 @@ category: Web
stability: Stable
cabal-version: >= 1.6
build-type: Simple
homepage: http://docs.yesodweb.com/yesod/
homepage: http://docs.yesodweb.com/
extra-source-files: scaffold/*.cg
flag buildtests
@ -48,7 +48,8 @@ library
data-object >= 0.3.1 && < 0.4,
network >= 2.2.1.5 && < 2.3,
email-validate >= 0.2.5 && < 0.3,
process >= 1.0.1 && < 1.1
process >= 1.0.1 && < 1.1,
web-routes >= 0.23 && < 0.24
exposed-modules: Yesod
Yesod.Content
Yesod.Dispatch