]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - Network/HTTP/Lucu/Preprocess.hs
StatusCode is now a type class, not an algebraic data type.
[Lucu.git] / Network / HTTP / Lucu / Preprocess.hs
index f2212ab104b3052a47fd91de90050f0ccf31a6cd..3a02ad8f194c4a0b6e41cd850e59725a0030752f 100644 (file)
@@ -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,54 +30,34 @@ import Prelude.Unicode
 
 data AugmentedRequest
     = AugmentedRequest {
-        arRequest          ∷ !(Maybe Request)
-      , arInitialStatus    ∷ !StatusCode
-      , arWillClose        ∷ !Bool
+        arRequest          ∷ !Request
+      , arInitialStatus    ∷ !SomeStatusCode
+      , arWillChunkBody    ∷ !Bool
       , arWillDiscardBody  ∷ !Bool
-      , arExpectedContinue ∷ !(Maybe Bool)
+      , arWillClose        ∷ !Bool
+      , arExpectedContinue ∷ !Bool
       , arReqBodyLength    ∷ !(Maybe RequestBodyLength)
       }
 
 data RequestBodyLength
     = Fixed !Int
     | 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
-                  , arInitialStatus    = Ok
-                  , arWillClose        = False
+                    arRequest          = req
+                  , arInitialStatus    = fromStatusCode OK
+                  , arWillChunkBody    = False
                   , arWillDiscardBody  = False
-                  , arExpectedContinue = Just False
+                  , arWillClose        = False
+                  , arExpectedContinue = False
                   , arReqBodyLength    = Nothing
                   }
-
       go ∷ State AugmentedRequest ()
       go = do examineHttpVersion
               examineMethod
@@ -87,11 +67,11 @@ 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 ∷ StatusCode sc ⇒ sc → State AugmentedRequest ()
 setStatus sc
-    = modify $ \ar → ar { arInitialStatus = sc }
+    = modify $ \ar → ar { arInitialStatus = fromStatusCode sc }
 
 setWillClose ∷ Bool → State AugmentedRequest ()
 setWillClose b
@@ -103,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 ()
-           _   → do setStatus    HttpVersionNotSupported
+               → 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 }
@@ -126,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
@@ -177,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
 
@@ -213,9 +193,9 @@ examineHeaders
 
 examineBodyLength ∷ State AugmentedRequest ()
 examineBodyLength
-    = do req ← gets (fromJust ∘ arRequest)
+    = do req ← gets arRequest
          len ← gets arReqBodyLength
-         if reqHasBody req then
+         if reqMustHaveBody req then
              -- POST and PUT requests must have an entity body.
              when (isNothing len)
                  $ setStatus LengthRequired