From 3484fda01eca89fea1a40a14281e925781e54a93 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Fri, 22 Oct 2021 16:51:04 -0400 Subject: [PATCH] escape characters in URLs --- src/Network/GemServ.hs | 4 ++-- test/Network/GemServSpec.hs | 34 +++++++++++++++++++++------------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/Network/GemServ.hs b/src/Network/GemServ.hs index 9ab6455..2f25344 100644 --- a/src/Network/GemServ.hs +++ b/src/Network/GemServ.hs @@ -42,10 +42,10 @@ encodeRequest req = authority = reqHost req ++ case reqPort req of Just port -> ':' : show port Nothing -> "" - path = intercalate "/" $ reqPath req + path = intercalate "/" $ map escapeString $ reqPath req query = case reqQuery req of "" -> "" - q -> '?' : q + q -> '?' : escapeString q -- | add required escape sequences to a string escapeString :: String -> String diff --git a/test/Network/GemServSpec.hs b/test/Network/GemServSpec.hs index 7657075..ebdc3e4 100644 --- a/test/Network/GemServSpec.hs +++ b/test/Network/GemServSpec.hs @@ -39,22 +39,30 @@ encodeRequestSpec = describe "encodeRequest" $ mapM_ encodeRequest req `shouldBe` expected ) - -- description, request, expected - [ ( "simple", simpleReq, simpleExp ) - , ( "with port", withPortReq, withPortExp ) - , ( "with path", withPathReq, withPathExp ) - , ( "with query", withQueryReq, withQueryExp ) + -- description, request, expected + [ ( "simple", simpleReq, simpleExp ) + , ( "with port", withPortReq, withPortExp ) + , ( "with path", withPathReq, withPathExp ) + , ( "with query", withQueryReq, withQueryExp ) + , ( "with escape", withEscapeReq, withEscapeExp ) ] where - simpleReq = newRequest "example.com" - simpleExp = "gemini://example.com/" - withPortReq = simpleReq { reqPort = Just 1965 } - withPortExp = "gemini://example.com:1965/" - withPathReq = simpleReq { reqPath = ["foo", "bar"] } - withPathExp = "gemini://example.com/foo/bar" - withQueryReq = simpleReq { reqQuery = "foo" } - withQueryExp = "gemini://example.com/?foo" + simpleReq = newRequest "example.com" + simpleExp = "gemini://example.com/" + withPortReq = simpleReq { reqPort = Just 1965 } + withPortExp = "gemini://example.com:1965/" + withPathReq = simpleReq { reqPath = ["foo", "bar"] } + withPathExp = "gemini://example.com/foo/bar" + withQueryReq = simpleReq { reqQuery = "foo" } + withQueryExp = "gemini://example.com/?foo" + + withEscapeReq = simpleReq + { reqPath = ["foo bar"] + , reqQuery = "baz quux" + } + + withEscapeExp = "gemini://example.com/foo%20bar?baz%20quux" escapeStringSpec :: Spec escapeStringSpec = describe "escapeString" $ mapM_