X-Git-Url: http://git.cielonegro.org/gitweb.cgi?p=Lucu.git;a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FInteraction.hs;h=7c43f96c5e7a12bcf3145771a7ea6b4976fed17e;hp=018ee00d0eaa65e197c508a8d8d7c968d2fb3fe2;hb=246d66d6d3130e03834a6c3badc38711a1879aae;hpb=874e6a4cc1229d29f1d902f36482cf0f78e30c9f diff --git a/Network/HTTP/Lucu/Interaction.hs b/Network/HTTP/Lucu/Interaction.hs index 018ee00..7c43f96 100644 --- a/Network/HTTP/Lucu/Interaction.hs +++ b/Network/HTTP/Lucu/Interaction.hs @@ -9,6 +9,7 @@ module Network.HTTP.Lucu.Interaction ( Interaction(..) , SomeInteraction(..) + , EndOfInteraction(..) , SyntacticallyInvalidInteraction(..) , mkSyntacticallyInvalidInteraction @@ -24,7 +25,6 @@ module Network.HTTP.Lucu.Interaction , InteractionQueue , mkInteractionQueue - , setResponseStatus , getCurrentDate ) where @@ -32,11 +32,13 @@ import Blaze.ByteString.Builder (Builder) import Control.Applicative import Control.Concurrent.STM import Data.Ascii (Ascii) -import qualified Data.ByteString as Strict +import Data.ByteString (ByteString) +import Data.Convertible.Base import Data.Monoid.Unicode +import Data.Proxy import Data.Sequence (Seq) import Data.Time -import qualified Data.Time.HTTP as HTTP +import Data.Time.Format.HTTP import Data.Typeable import Network.Socket import Network.HTTP.Lucu.Config @@ -45,9 +47,11 @@ import Network.HTTP.Lucu.Headers import Network.HTTP.Lucu.Preprocess import Network.HTTP.Lucu.Request import Network.HTTP.Lucu.Response +import Network.HTTP.Lucu.Utils #if defined(HAVE_SSL) import OpenSSL.X509 #endif +import Prelude.Unicode class Typeable i ⇒ Interaction i where toInteraction ∷ i → SomeInteraction @@ -64,6 +68,13 @@ instance Interaction SomeInteraction where toInteraction = id fromInteraction = Just +-- |'EndOfInteraction' is an 'Interaction' indicating the end of +-- (possibly pipelined) requests. The connection has already been +-- closed so no need to reply anything. +data EndOfInteraction = EndOfInteraction + deriving Typeable +instance Interaction EndOfInteraction + -- |'SyntacticallyInvalidInteraction' is an 'Interaction' without even -- a syntactically valid 'Request'. The response code will always be -- 'BadRequest'. @@ -77,13 +88,13 @@ instance Interaction SyntacticallyInvalidInteraction mkSyntacticallyInvalidInteraction ∷ Config → IO SyntacticallyInvalidInteraction -mkSyntacticallyInvalidInteraction config@(Config {..}) +mkSyntacticallyInvalidInteraction conf@(Config {..}) = do date ← getCurrentDate let res = setHeader "Server" cnfServerSoftware $ setHeader "Date" date $ setHeader "Content-Type" defaultPageContentType $ emptyResponse BadRequest - body = getDefaultPage config Nothing res + body = defaultPageForResponse conf Nothing res return SYI { syiResponse = res , syiBodyToSend = body @@ -100,7 +111,6 @@ data SemanticallyInvalidInteraction , seiResponse ∷ !Response , seiWillChunkBody ∷ !Bool - , seiWillDiscardBody ∷ !Bool , seiWillClose ∷ !Bool , seiBodyToSend ∷ !Builder } @@ -115,8 +125,16 @@ mkSemanticallyInvalidInteraction config@(Config {..}) (AugmentedRequest {..}) let res = setHeader "Server" cnfServerSoftware $ setHeader "Date" date $ setHeader "Content-Type" defaultPageContentType $ + ( if arWillChunkBody + then setHeader "Transfer-Encoding" "chunked" + else id + ) $ + ( if arWillClose + then setHeader "Connection" "close" + else id + ) $ emptyResponse arInitialStatus - body = getDefaultPage config (Just arRequest) res + body = defaultPageForResponse config (Just arRequest) res return SEI { seiRequest = arRequest , seiExpectedContinue = arExpectedContinue @@ -124,7 +142,6 @@ mkSemanticallyInvalidInteraction config@(Config {..}) (AugmentedRequest {..}) , seiResponse = res , seiWillChunkBody = arWillChunkBody - , seiWillDiscardBody = arWillDiscardBody , seiWillClose = arWillClose , seiBodyToSend = body } @@ -139,17 +156,16 @@ data NormalInteraction , niRemoteCert ∷ !(Maybe X509) #endif , niRequest ∷ !Request - , niResourcePath ∷ ![Strict.ByteString] + , niResourcePath ∷ !Path , niExpectedContinue ∷ !Bool , niReqBodyLength ∷ !(Maybe RequestBodyLength) , niReceiveBodyReq ∷ !(TMVar ReceiveBodyRequest) - , niReceivedBody ∷ !(TMVar Strict.ByteString) + , niReceivedBody ∷ !(TMVar ByteString) , niResponse ∷ !(TVar Response) , niSendContinue ∷ !(TMVar Bool) , niWillChunkBody ∷ !Bool - , niWillDiscardBody ∷ !(TVar Bool) , niWillClose ∷ !(TVar Bool) , niResponseHasCType ∷ !(TVar Bool) -- FIXME: use TBChan Builder (in stm-chans package) @@ -181,7 +197,7 @@ mkNormalInteraction ∷ Config → Maybe X509 #endif → AugmentedRequest - → [Strict.ByteString] + → Path → IO NormalInteraction #if defined(HAVE_SSL) mkNormalInteraction config remoteAddr remoteCert (AugmentedRequest {..}) rsrcPath @@ -193,7 +209,6 @@ mkNormalInteraction config remoteAddr (AugmentedRequest {..}) rsrcPath response ← newTVarIO $ emptyResponse arInitialStatus sendContinue ← newEmptyTMVarIO - willDiscardBody ← newTVarIO arWillDiscardBody willClose ← newTVarIO arWillClose responseHasCType ← newTVarIO False bodyToSend ← newEmptyTMVarIO @@ -217,7 +232,6 @@ mkNormalInteraction config remoteAddr (AugmentedRequest {..}) rsrcPath , niResponse = response , niSendContinue = sendContinue , niWillChunkBody = arWillChunkBody - , niWillDiscardBody = willDiscardBody , niWillClose = willClose , niResponseHasCType = responseHasCType , niBodyToSend = bodyToSend @@ -228,16 +242,9 @@ mkNormalInteraction config remoteAddr (AugmentedRequest {..}) rsrcPath type InteractionQueue = TVar (Seq SomeInteraction) mkInteractionQueue ∷ IO InteractionQueue +{-# INLINE mkInteractionQueue #-} mkInteractionQueue = newTVarIO (∅) --- FIXME: Response.hs should provide setStatus ∷ sc → Response → Response -setResponseStatus ∷ StatusCode sc ⇒ NormalInteraction → sc → STM () -setResponseStatus (NI {..}) sc - = do res ← readTVar niResponse - let res' = res { - resStatus = fromStatusCode sc - } - writeTVar niResponse res' - getCurrentDate ∷ IO Ascii -getCurrentDate = HTTP.toAscii <$> getCurrentTime +{-# INLINE getCurrentDate #-} +getCurrentDate = flip proxy http ∘ cs <$> getCurrentTime