Skip to content

Commit

Permalink
Canonicalize destination URLs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Trinkle committed Nov 15, 2013
1 parent 4692ced commit 9b4ec9c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
3 changes: 2 additions & 1 deletion memoise.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Name: memoise
Version: 0.9
Version: 0.10
License: BSD3
Author: Ryan Trinkle
Maintainer: [email protected]
Expand All @@ -21,6 +21,7 @@ Executable memoise
, heist >= 0.12 && < 0.13
, lens >= 3.9 && < 3.10
, text >= 0.11 && < 0.12
, network >= 2.4 && < 2.5
, snaplet-postgresql-simple >= 0.4 && < 0.5
GHC-options: -threaded -O2
Extensions: OverloadedStrings
Expand Down
21 changes: 17 additions & 4 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Data.Text.Encoding
import Data.Monoid
import Heist
import Heist.Interpreted
import Network.URI hiding (query)

data Memoise
= Memoise { _heist :: Snaplet (Heist Memoise)
Expand All @@ -24,8 +25,17 @@ instance HasHeist Memoise where
instance HasPostgres (Handler Memoise Memoise) where
getPostgresState = with db get

getUrlId :: Text -> Handler Memoise Memoise Integer
getUrlId url = do
canonicalizeUrl :: Text -> Maybe URI
canonicalizeUrl rawUrl =
let url = unpack rawUrl
url' = if isAbsoluteURI url
then url
else "http://" <> url
in parseURI $ normalizeCase $ normalizePathSegments $ normalizeEscape url'

getUrlId :: URI -> Handler Memoise Memoise Integer
getUrlId uri = do
let url = showT uri
existingResults <- query "SELECT id FROM urls WHERE url = ?" (Only url)
case existingResults of
(Only urlId) : _ -> return urlId
Expand All @@ -38,8 +48,11 @@ indexHandler = do
mUrl <- getParam "url"
case mUrl of
Just url -> do
urlId <- getUrlId $ decodeUtf8 url
mainTextboxContents .= Just ("http://memoi.se/" <> showT urlId)
case canonicalizeUrl (decodeUtf8 url) of
Just uri -> do
urlId <- getUrlId uri
mainTextboxContents .= Just ("http://memoi.se/" <> showT urlId)
Nothing -> mainTextboxContents .= Just "Invalid URL"
render "index"
Nothing -> render "index"

Expand Down

0 comments on commit 9b4ec9c

Please sign in to comment.