, OverloadedStrings
, RecordWildCards
, UnicodeSyntax
+ , ViewPatterns
#-}
module Network.HTTP.Lucu.Preprocess
( AugmentedRequest(..)
)
where
import Control.Applicative
+import Control.Applicative.Unicode
import Control.Monad
import Control.Monad.State.Strict
import Data.Ascii (Ascii)
| Chunked
deriving (Eq, Show)
-preprocess ∷ CI Text → PortNumber → Request → AugmentedRequest
-preprocess localHost localPort req@(Request {..})
+preprocess ∷ CI Text → PortNumber → Bool → Request → AugmentedRequest
+preprocess localHost localPort isSSL req@(Request {..})
= execState go initialAR
where
initialAR ∷ AugmentedRequest
go ∷ State AugmentedRequest ()
go = do examineHttpVersion
examineMethod
+ examineScheme isSSL
examineAuthority localHost localPort
examineHeaders
examineBodyLength
DELETE → return ()
_ → setStatus NotImplemented
+examineScheme ∷ Bool → State AugmentedRequest ()
+examineScheme isSSL
+ = do req ← gets arRequest
+ when (null ∘ uriScheme $ reqURI req) $
+ let uri' = (reqURI req) {
+ uriScheme = if isSSL then
+ "https:"
+ else
+ "http:"
+ }
+ req' = req { reqURI = uri' }
+ in
+ setRequest req'
+
examineAuthority ∷ CI Text → PortNumber → State AugmentedRequest ()
examineAuthority localHost localPort
= do req ← gets arRequest
HttpVersion 1 0
→ let host = localHost
port = case localPort of
- 80 → ""
- n → A.unsafeFromString $ ':':show n
+ n | Just n ≡ defaultPort (reqURI req)
+ → ""
+ n → A.unsafeFromString $ ':':show n
req' = updateAuthority host port req
in
setRequest req'
-- Should never reach here...
ver → fail ("internal error: unknown version: " ⧺ show ver)
+defaultPort ∷ Alternative f ⇒ URI → f PortNumber
+{-# INLINEABLE defaultPort #-}
+defaultPort (uriScheme → s)
+ | s ≡ "http:" = pure 80
+ | s ≡ "https:" = pure 443
+ | otherwise = (∅)
+
parseHost ∷ Ascii → (CI Text, Ascii)
parseHost hp
= let (h, p) = C8.break (≡ ':') $ cs hp