X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FPostprocess.hs;h=1a00b00b0eab578bca9db5d52e3e6bf4003abf46;hb=cc55fb9a095c9c583ed6fe2ded3eaf6401fb760f;hp=989ad164707ca9afb99f94f55dcc69fe2840e658;hpb=8510a3765130fb171c06b448c50a74e65ac8ae11;p=Lucu.git diff --git a/Network/HTTP/Lucu/Postprocess.hs b/Network/HTTP/Lucu/Postprocess.hs index 989ad16..1a00b00 100644 --- a/Network/HTTP/Lucu/Postprocess.hs +++ b/Network/HTTP/Lucu/Postprocess.hs @@ -1,5 +1,5 @@ {-# LANGUAGE - BangPatterns + DoAndIfThenElse , OverloadedStrings , UnicodeSyntax #-} @@ -9,27 +9,24 @@ module Network.HTTP.Lucu.Postprocess ) where import Control.Applicative -import Control.Concurrent.STM -import Control.Monad +import Control.Concurrent.STM +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.Time +import Data.Maybe +import Data.Monoid.Unicode +import Data.Time import qualified Data.Time.HTTP as HTTP -import GHC.Conc (unsafeIOToSTM) -import Network.HTTP.Lucu.Abortion -import Network.HTTP.Lucu.Config -import Network.HTTP.Lucu.Headers -import Network.HTTP.Lucu.HttpVersion -import Network.HTTP.Lucu.Interaction -import Network.HTTP.Lucu.Request -import Network.HTTP.Lucu.Response +import GHC.Conc (unsafeIOToSTM) +import Network.HTTP.Lucu.Abortion +import Network.HTTP.Lucu.Config +import Network.HTTP.Lucu.Headers +import Network.HTTP.Lucu.HttpVersion +import Network.HTTP.Lucu.Interaction +import Network.HTTP.Lucu.Request +import Network.HTTP.Lucu.Response import Prelude.Unicode -import System.IO.Unsafe {- @@ -65,37 +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 ("The status code is not good for a final status: " - ++ show sc) - - when (sc ≡ MethodNotAllowed ∧ getHeader (C8.pack "Allow") res ≡ Nothing) - $ abortSTM InternalServerError [] - $ Just ("The status was " ++ show sc ++ " but no Allow header.") - - when (sc /= NotModified ∧ isRedirection sc ∧ getHeader (C8.pack "Location") res ≡ Nothing) - $ abortSTM InternalServerError [] - $ Just ("The status code was " ++ show sc ++ " but no Location header.") - - when (reqM /= Nothing) relyOnRequest + $ abortSTM InternalServerError [] + $ Just + $ 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 + $ A.fromAsciiBuilder + $ A.toAsciiBuilder "The status was " + ⊕ printStatusCode sc + ⊕ A.toAsciiBuilder " but no Allow header." + + when (sc ≢ NotModified ∧ isRedirection sc ∧ getHeader "Location" res ≡ Nothing) + $ abortSTM InternalServerError [] + $ Just + $ A.toText + $ A.fromAsciiBuilder + $ A.toAsciiBuilder "The status code was " + ⊕ printStatusCode sc + ⊕ A.toAsciiBuilder " but no Location header." + + 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 @@ -115,33 +125,35 @@ postprocess !itr if canHaveBody then when (reqVer ≡ HttpVersion 1 1) - $ do updateRes $ setHeader "Transfer-Encoding" "chunked" - writeItr itr itrWillChunkBody True - else + $ do updateRes $ setHeader "Transfer-Encoding" "chunked" + writeItr itrWillChunkBody True itr + else -- body 関連のヘッダを削除。但し HEAD なら Content-* は殘す - when (reqMethod req /= HEAD) - $ do updateRes $ deleteHeader "Content-Type" - updateRes $ deleteHeader "Etag" - updateRes $ deleteHeader "Last-Modified" + when (reqMethod req ≢ HEAD) + $ do updateRes $ deleteHeader "Content-Type" + updateRes $ deleteHeader "Etag" + updateRes $ deleteHeader "Last-Modified" conn ← readHeader "Connection" 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" + $ updateRes $ setHeader "Connection" "close" when (reqMethod req ≡ HEAD ∨ not canHaveBody) - $ writeTVar (itrWillDiscardBody itr) True + $ writeTVar (itrWillDiscardBody itr) True readHeader ∷ CIAscii → STM (Maybe Ascii) - readHeader = readItr itr itrResponse ∘ getHeader + {-# INLINE readHeader #-} + readHeader k = getHeader k <$> readItr itrResponse itr updateRes ∷ (Response → Response) → STM () - updateRes = updateItr itr itrResponse + {-# INLINE updateRes #-} + updateRes f = updateItr itrResponse f itr completeUnconditionalHeaders ∷ Config → Response → IO Response completeUnconditionalHeaders conf = (compDate =≪) ∘ compServer @@ -158,4 +170,4 @@ completeUnconditionalHeaders conf = (compDate =≪) ∘ compServer Just _ → return res' getCurrentDate ∷ IO Ascii -getCurrentDate = HTTP.format <$> getCurrentTime +getCurrentDate = HTTP.toAscii <$> getCurrentTime