]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - Network/HTTP/Lucu/Postprocess.hs
ResponseWriter now compiles.
[Lucu.git] / Network / HTTP / Lucu / Postprocess.hs
index 49c95e809be046489bed306c83db6f77eab12baf..1a00b00b0eab578bca9db5d52e3e6bf4003abf46 100644 (file)
@@ -1,6 +1,5 @@
 {-# LANGUAGE
-    BangPatterns
-  , DoAndIfThenElse
+    DoAndIfThenElse
   , OverloadedStrings
   , UnicodeSyntax
   #-}
@@ -15,9 +14,6 @@ import Control.Monad
 import Control.Monad.Unicode
 import Data.Ascii (Ascii, CIAscii)
 import qualified Data.Ascii as A
-import qualified Data.ByteString as Strict (ByteString)
-import qualified Data.ByteString.Char8 as C8 hiding (ByteString)
-import Data.IORef
 import Data.Maybe
 import Data.Monoid.Unicode
 import Data.Time
@@ -31,7 +27,6 @@ import Network.HTTP.Lucu.Interaction
 import Network.HTTP.Lucu.Request
 import Network.HTTP.Lucu.Response
 import Prelude.Unicode
-import System.IO.Unsafe
 
 {-
   
@@ -67,44 +62,50 @@ import System.IO.Unsafe
 -}
 
 postprocess ∷ Interaction → STM ()
-postprocess !itr
-    = do reqM ← readItr itr itrRequest id
-         res  ← readItr itr itrResponse id
+postprocess itr
+    = do reqM ← readItr itrRequest  itr
+         res  ← readItr itrResponse itr
          let sc = resStatus res
 
          unless (any (\ p → p sc) [isSuccessful, isRedirection, isError])
              $ abortSTM InternalServerError []
              $ Just
-             $ A.toText ( "The status code is not good for a final status of a response: "
-                          ⊕ printStatusCode sc )
+             $ A.toText
+             $ A.fromAsciiBuilder
+             $ A.toAsciiBuilder "The status code is not good for a final status of a response: "
+             ⊕ printStatusCode sc
 
          when (sc ≡ MethodNotAllowed ∧ getHeader "Allow" res ≡ Nothing)
              $ abortSTM InternalServerError []
              $ Just
-             $ A.toText ( "The status was "
-                          ⊕ printStatusCode sc
-                          ⊕ " but no Allow header." )
+             $ A.toText
+             $ A.fromAsciiBuilder
+             $ A.toAsciiBuilder "The status was "
+             ⊕ printStatusCode sc
+             ⊕ A.toAsciiBuilder " but no Allow header."
 
-         when (sc /= NotModified ∧ isRedirection sc ∧ getHeader "Location" res ≡ Nothing)
+         when (sc  NotModified ∧ isRedirection sc ∧ getHeader "Location" res ≡ Nothing)
              $ abortSTM InternalServerError []
              $ Just
-             $ A.toText ( "The status code was "
-                          ⊕ printStatusCode sc
-                          ⊕ " but no Location header." )
+             $ A.toText
+             $ A.fromAsciiBuilder
+             $ A.toAsciiBuilder "The status code was "
+             ⊕ printStatusCode sc
+             ⊕ A.toAsciiBuilder " but no Location header."
 
-         when (reqM /= Nothing) relyOnRequest
+         when (reqM  Nothing) relyOnRequest
 
          -- itrResponse の内容は relyOnRequest によって變へられてゐる可
          -- 能性が高い。
-         do oldRes ← readItr itr itrResponse id
+         do oldRes ← readItr itrResponse itr
             newRes ← unsafeIOToSTM
                      $ completeUnconditionalHeaders (itrConfig itr) oldRes
-            writeItr itr itrResponse newRes
+            writeItr itrResponse newRes itr
     where
       relyOnRequest ∷ STM ()
       relyOnRequest
-          = do status ← readItr itr itrResponse resStatus
-               req    ← readItr itr itrRequest fromJust
+          = do status ← resStatus <$> readItr itrResponse itr
+               req    ← fromJust  <$> readItr itrRequest  itr
 
                let reqVer      = reqVersion req
                    canHaveBody = if reqMethod req ≡ HEAD then
@@ -125,10 +126,10 @@ postprocess !itr
                if canHaveBody then
                    when (reqVer ≡ HttpVersion 1 1)
                        $ do updateRes $ setHeader "Transfer-Encoding" "chunked"
-                            writeItr itr itrWillChunkBody True
+                            writeItr itrWillChunkBody True itr
                else
                    -- body 関連のヘッダを削除。但し HEAD なら Content-* は殘す
-                   when (reqMethod req /= HEAD)
+                   when (reqMethod req  HEAD)
                        $ do updateRes $ deleteHeader "Content-Type"
                             updateRes $ deleteHeader "Etag"
                             updateRes $ deleteHeader "Last-Modified"
@@ -137,9 +138,9 @@ postprocess !itr
                case conn of
                  Nothing    → return ()
                  Just value → when (A.toCIAscii value ≡ "close")
-                                  $ writeItr itr itrWillClose True
+                                  $ writeItr itrWillClose True itr
 
-               willClose ← readItr itr itrWillClose id
+               willClose ← readItr itrWillClose itr
                when willClose
                    $ updateRes $ setHeader "Connection" "close"
 
@@ -148,11 +149,11 @@ postprocess !itr
 
       readHeader ∷ CIAscii → STM (Maybe Ascii)
       {-# INLINE readHeader #-}
-      readHeader = readItr itr itrResponse ∘ getHeader
+      readHeader k = getHeader k <$> readItr itrResponse itr
 
       updateRes ∷ (Response → Response) → STM ()
       {-# INLINE updateRes #-}
-      updateRes = updateItr itr itrResponse
+      updateRes f = updateItr itrResponse f itr
 
 completeUnconditionalHeaders ∷ Config → Response → IO Response
 completeUnconditionalHeaders conf = (compDate =≪) ∘ compServer
@@ -169,4 +170,4 @@ completeUnconditionalHeaders conf = (compDate =≪) ∘ compServer
                 Just _  → return res'
 
 getCurrentDate ∷ IO Ascii
-getCurrentDate = HTTP.format <$> getCurrentTime
+getCurrentDate = HTTP.toAscii <$> getCurrentTime