renamed Request to GemURL

- A request should contain a URL and an optional client certigicate.
- renamed accompanying functions accordingly
This commit is contained in:
Jonathan Lamothe 2021-10-24 15:43:29 -04:00
parent db718eedcd
commit a2d8f1a5ea
3 changed files with 39 additions and 39 deletions

View File

@ -27,7 +27,7 @@ License along with this program. If not, see
{-# LANGUAGE LambdaCase #-}
module Network.GemServ (
encodeRequest,
encodeGemURL,
escapeString,
unescapeString
) where
@ -43,16 +43,16 @@ import Data.Text.Encoding (decodeUtf8')
import Network.GemServ.Types
-- | Encodes a 'Request' into a 'String'
encodeRequest :: Request -> String
encodeRequest req =
-- | Encodes a 'GemURL' into a 'String'
encodeGemURL :: GemURL -> String
encodeGemURL url =
"gemini://" ++ authority ++ "/" ++ path ++ query
where
authority = reqHost req ++ case reqPort req of
authority = gemHost url ++ case gemPort url of
Just port -> ':' : show port
Nothing -> ""
path = intercalate "/" $ map escapeString $ reqPath req
query = case reqQuery req of
path = intercalate "/" $ map escapeString $ gemPath url
query = case gemQuery url of
"" -> ""
q -> '?' : escapeString q

View File

@ -25,34 +25,34 @@ License along with this program. If not, see
-}
module Network.GemServ.Types (
Request (..),
newRequest
GemURL (..),
newGemURL
) where
import Data.Word (Word32)
-- | Gemini request
data Request = Request
{ reqHost :: String
-- | Gemini URL
data GemURL = GemURL
{ gemHost :: String
-- ^ The host part of the authority section, e.g.: "example.com"
, reqPort :: Maybe Word32
, gemPort :: Maybe Word32
-- ^ The port number (if supplied)
, reqPath :: [String]
, gemPath :: [String]
-- ^ The decoded path segments
, reqQuery :: String
, gemQuery :: String
-- ^ The decoded request query
} deriving (Eq, Show)
-- | Builds a new request
newRequest
-- | Builds a new 'GemURL'
newGemURL
:: String
-- ^ The hostname
-> Request
newRequest host = Request
{ reqHost = host
, reqPort = Nothing
, reqPath = []
, reqQuery = ""
-> GemURL
newGemURL host = GemURL
{ gemHost = host
, gemPort = Nothing
, gemPath = []
, gemQuery = ""
}
--jl

View File

@ -29,38 +29,38 @@ import Network.GemServ.Types
spec :: Spec
spec = describe "Network.GemServ" $ do
encodeRequestSpec
encodeGemURLSpec
escapeStringSpec
unescapeStringSpec
encodeRequestSpec :: Spec
encodeRequestSpec = describe "encodeRequest" $ mapM_
encodeGemURLSpec :: Spec
encodeGemURLSpec = describe "encodeGemURL" $ mapM_
( \(desc, req, expected) -> context desc $
it ("should be " ++ show expected) $
encodeRequest req `shouldBe` expected
encodeGemURL req `shouldBe` expected
)
-- description, request, expected
[ ( "simple", simpleReq, simpleExp )
, ( "with port", withPortReq, withPortExp )
, ( "with path", withPathReq, withPathExp )
, ( "with query", withQueryReq, withQueryExp )
, ( "with escape", withEscapeReq, withEscapeExp )
[ ( "simple", simpleURL, simpleExp )
, ( "with port", withPortURL, withPortExp )
, ( "with path", withPathURL, withPathExp )
, ( "with query", withQueryURL, withQueryExp )
, ( "with escape", withEscapeURL, withEscapeExp )
]
where
simpleReq = newRequest "example.com"
simpleURL = newGemURL "example.com"
simpleExp = "gemini://example.com/"
withPortReq = simpleReq { reqPort = Just 1965 }
withPortURL = simpleURL { gemPort = Just 1965 }
withPortExp = "gemini://example.com:1965/"
withPathReq = simpleReq { reqPath = ["foo", "bar"] }
withPathURL = simpleURL { gemPath = ["foo", "bar"] }
withPathExp = "gemini://example.com/foo/bar"
withQueryReq = simpleReq { reqQuery = "foo" }
withQueryURL = simpleURL { gemQuery = "foo" }
withQueryExp = "gemini://example.com/?foo"
withEscapeReq = simpleReq
{ reqPath = ["foo bar"]
, reqQuery = "baz quux"
withEscapeURL = simpleURL
{ gemPath = ["foo bar"]
, gemQuery = "baz quux"
}
withEscapeExp = "gemini://example.com/foo%20bar?baz%20quux"