X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FInteraction.hs;h=0f3e7bfeae5492a73edddd387528d9368dca57e8;hb=57b93dc50655857941cc176aed2fc085e6555ff7;hp=3be8928bf83a7edf473458c6760b06275b3cf886;hpb=a362be1c8664306b970c32e1df9b62081498feb1;p=Lucu.git diff --git a/Network/HTTP/Lucu/Interaction.hs b/Network/HTTP/Lucu/Interaction.hs index 3be8928..0f3e7bf 100644 --- a/Network/HTTP/Lucu/Interaction.hs +++ b/Network/HTTP/Lucu/Interaction.hs @@ -1,5 +1,6 @@ {-# LANGUAGE - DeriveDataTypeable + CPP + , DeriveDataTypeable , ExistentialQuantification , OverloadedStrings , RecordWildCards @@ -8,6 +9,7 @@ module Network.HTTP.Lucu.Interaction ( Interaction(..) , SomeInteraction(..) + , EndOfInteraction(..) , SyntacticallyInvalidInteraction(..) , mkSyntacticallyInvalidInteraction @@ -23,7 +25,6 @@ module Network.HTTP.Lucu.Interaction , InteractionQueue , mkInteractionQueue - , setResponseStatus , getCurrentDate ) where @@ -31,7 +32,7 @@ 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.Monoid.Unicode import Data.Sequence (Seq) import Data.Time @@ -44,7 +45,10 @@ 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 class Typeable i ⇒ Interaction i where toInteraction ∷ i → SomeInteraction @@ -61,6 +65,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'. @@ -109,7 +120,6 @@ mkSemanticallyInvalidInteraction ∷ Config → IO SemanticallyInvalidInteraction mkSemanticallyInvalidInteraction config@(Config {..}) (AugmentedRequest {..}) = do date ← getCurrentDate - -- FIXME: DRY let res = setHeader "Server" cnfServerSoftware $ setHeader "Date" date $ setHeader "Content-Type" defaultPageContentType $ @@ -133,14 +143,16 @@ data NormalInteraction = NI { niConfig ∷ !Config , niRemoteAddr ∷ !SockAddr +#if defined(HAVE_SSL) , 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) @@ -148,6 +160,7 @@ data NormalInteraction , niWillDiscardBody ∷ !(TVar Bool) , niWillClose ∷ !(TVar Bool) , niResponseHasCType ∷ !(TVar Bool) + -- FIXME: use TBChan Builder (in stm-chans package) , niBodyToSend ∷ !(TMVar Builder) , niState ∷ !(TVar InteractionState) @@ -172,11 +185,17 @@ data InteractionState mkNormalInteraction ∷ Config → SockAddr +#if defined(HAVE_SSL) → Maybe X509 +#endif → AugmentedRequest - → [Strict.ByteString] + → Path → IO NormalInteraction +#if defined(HAVE_SSL) mkNormalInteraction config remoteAddr remoteCert (AugmentedRequest {..}) rsrcPath +#else +mkNormalInteraction config remoteAddr (AugmentedRequest {..}) rsrcPath +#endif = do receiveBodyReq ← newEmptyTMVarIO receivedBody ← newEmptyTMVarIO @@ -192,7 +211,9 @@ mkNormalInteraction config remoteAddr remoteCert (AugmentedRequest {..}) rsrcPat return NI { niConfig = config , niRemoteAddr = remoteAddr +#if defined(HAVE_SSL) , niRemoteCert = remoteCert +#endif , niRequest = arRequest , niResourcePath = rsrcPath , niExpectedContinue = arExpectedContinue @@ -217,13 +238,5 @@ type InteractionQueue = TVar (Seq SomeInteraction) mkInteractionQueue ∷ IO InteractionQueue mkInteractionQueue = newTVarIO (∅) -setResponseStatus ∷ NormalInteraction → StatusCode → STM () -setResponseStatus (NI {..}) sc - = do res ← readTVar niResponse - let res' = res { - resStatus = sc - } - writeTVar niResponse res' - getCurrentDate ∷ IO Ascii getCurrentDate = HTTP.toAscii <$> getCurrentTime