X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FPreprocess.hs;h=26fbd53546a2412a90d40f5f30c234620ab6890d;hb=48bc90d;hp=739dec89f6d6058486a00cf404e7d6a12b7280c9;hpb=72a3e24a952616e32845eeb4fc05048e841c91a2;p=Lucu.git diff --git a/Network/HTTP/Lucu/Preprocess.hs b/Network/HTTP/Lucu/Preprocess.hs index 739dec8..26fbd53 100644 --- a/Network/HTTP/Lucu/Preprocess.hs +++ b/Network/HTTP/Lucu/Preprocess.hs @@ -12,7 +12,7 @@ module Network.HTTP.Lucu.Preprocess where import Control.Applicative import Control.Monad -import Control.Monad.State +import Control.Monad.State.Strict import Data.Ascii (Ascii) import qualified Data.Ascii as A import qualified Data.ByteString.Char8 as C8 @@ -30,11 +30,12 @@ import Prelude.Unicode data AugmentedRequest = AugmentedRequest { - arRequest ∷ !(Maybe Request) + arRequest ∷ !Request , arInitialStatus ∷ !StatusCode - , arWillClose ∷ !Bool + , arWillChunkBody ∷ !Bool , arWillDiscardBody ∷ !Bool - , arExpectedContinue ∷ !(Maybe Bool) + , arWillClose ∷ !Bool + , arExpectedContinue ∷ !Bool , arReqBodyLength ∷ !(Maybe RequestBodyLength) } @@ -43,42 +44,20 @@ data RequestBodyLength | Chunked deriving (Eq, Show) -preprocess ∷ Text - → PortNumber - → Either StatusCode Request - → AugmentedRequest -preprocess localHost localPort request - = case request of - Right req - → preprocess' localHost localPort req - Left sc - → unparsable sc - -unparsable ∷ StatusCode → AugmentedRequest -unparsable sc - = AugmentedRequest { - arRequest = Nothing - , arInitialStatus = sc - , arWillClose = True - , arWillDiscardBody = False - , arExpectedContinue = Nothing - , arReqBodyLength = Nothing - } - -preprocess' ∷ Text → PortNumber → Request → AugmentedRequest -preprocess' localHost localPort req@(Request {..}) +preprocess ∷ Text → PortNumber → Request → AugmentedRequest +preprocess localHost localPort req@(Request {..}) = execState go initialAR where initialAR ∷ AugmentedRequest initialAR = AugmentedRequest { - arRequest = Just req + arRequest = req , arInitialStatus = Ok - , arWillClose = False + , arWillChunkBody = False , arWillDiscardBody = False - , arExpectedContinue = Just False + , arWillClose = False + , arExpectedContinue = False , arReqBodyLength = Nothing } - go ∷ State AugmentedRequest () go = do examineHttpVersion examineMethod @@ -88,7 +67,7 @@ preprocess' localHost localPort req@(Request {..}) setRequest ∷ Request → State AugmentedRequest () setRequest req - = modify $ \ar → ar { arRequest = Just req } + = modify $ \ar → ar { arRequest = req } setStatus ∷ StatusCode → State AugmentedRequest () setStatus sc @@ -104,19 +83,19 @@ setBodyLength len examineHttpVersion ∷ State AugmentedRequest () examineHttpVersion - = do req ← gets (fromJust ∘ arRequest) + = do req ← gets arRequest case reqVersion req of -- HTTP/1.0 requests can't Keep-Alive. HttpVersion 1 0 → setWillClose True HttpVersion 1 1 - → return () + → modify $ \ar → ar { arWillChunkBody = True } _ → do setStatus HttpVersionNotSupported setWillClose True examineMethod ∷ State AugmentedRequest () examineMethod - = do req ← gets (fromJust ∘ arRequest) + = do req ← gets arRequest case reqMethod req of GET → return () HEAD → modify $ \ar → ar { arWillDiscardBody = True } @@ -127,7 +106,7 @@ examineMethod examineAuthority ∷ Text → PortNumber → State AugmentedRequest () examineAuthority localHost localPort - = do req ← gets (fromJust ∘ arRequest) + = do req ← gets arRequest when (isNothing $ uriAuthority $ reqURI req) $ case reqVersion req of -- HTTP/1.0 requests have no Host header so complete it @@ -178,13 +157,13 @@ updateAuthority host port req examineHeaders ∷ State AugmentedRequest () examineHeaders - = do req ← gets (fromJust ∘ arRequest) + = do req ← gets arRequest case getCIHeader "Expect" req of Nothing → return () Just v | v ≡ "100-continue" - → modify $ \ar → ar { arExpectedContinue = Just True } + → modify $ \ar → ar { arExpectedContinue = True } | otherwise → setStatus ExpectationFailed @@ -214,7 +193,7 @@ examineHeaders examineBodyLength ∷ State AugmentedRequest () examineBodyLength - = do req ← gets (fromJust ∘ arRequest) + = do req ← gets arRequest len ← gets arReqBodyLength if reqMustHaveBody req then -- POST and PUT requests must have an entity body.