X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FResource.hs;h=ec3447e7f488585570b4e8891b4da5abdbdad0f0;hb=874e6a4;hp=e6a03aca748a539c048c4b720be3de2a162db9eb;hpb=5e561403ba8ad9c440cc2bf2bacb61ebc3c7a111;p=Lucu.git diff --git a/Network/HTTP/Lucu/Resource.hs b/Network/HTTP/Lucu/Resource.hs index e6a03ac..ec3447e 100644 --- a/Network/HTTP/Lucu/Resource.hs +++ b/Network/HTTP/Lucu/Resource.hs @@ -1,8 +1,10 @@ {-# LANGUAGE - BangPatterns + CPP + , BangPatterns , GeneralizedNewtypeDeriving , DoAndIfThenElse , OverloadedStrings + , QuasiQuotes , RecordWildCards , UnicodeSyntax #-} @@ -81,7 +83,9 @@ module Network.HTTP.Lucu.Resource , getRemoteAddr , getRemoteAddr' , getRemoteHost +#if defined(HAVE_SSL) , getRemoteCertificate +#endif , getRequest , getMethod , getRequestURI @@ -174,7 +178,9 @@ import Network.HTTP.Lucu.Request import Network.HTTP.Lucu.Resource.Internal import Network.HTTP.Lucu.Response import Network.HTTP.Lucu.MIMEParams -import Network.HTTP.Lucu.MIMEType +import Network.HTTP.Lucu.MIMEType (MIMEType(..)) +import qualified Network.HTTP.Lucu.MIMEType as MT +import Network.HTTP.Lucu.MIMEType.TH import Network.HTTP.Lucu.Utils import Network.Socket hiding (accept) import Network.URI hiding (path) @@ -236,7 +242,7 @@ toPairWithFormData ∷ (ByteString, ByteString) → (Strict.ByteString, FormData toPairWithFormData (name, value) = let fd = FormData { fdFileName = Nothing - , fdMIMEType = parseMIMEType "text/plain" + , fdMIMEType = [mimeType| text/plain |] , fdContent = Lazy.fromChunks [value] } in (name, fd) @@ -258,7 +264,7 @@ getAccept Nothing → return [] Just accept - → case P.parseOnly (finishOff mimeTypeList) (A.toByteString accept) of + → case P.parseOnly (finishOff MT.mimeTypeList) (A.toByteString accept) of Right xs → return xs Left _ → abort $ mkAbortion' BadRequest $ "Unparsable Accept: " ⊕ A.toText accept @@ -310,7 +316,7 @@ getContentType Nothing → return Nothing Just cType - → case P.parseOnly (finishOff mimeType) (A.toByteString cType) of + → case P.parseOnly (finishOff MT.mimeType) (A.toByteString cType) of Right t → return $ Just t Left _ → abort $ mkAbortion' BadRequest $ "Unparsable Content-Type: " ⊕ A.toText cType @@ -401,9 +407,9 @@ foundETag tag let statusForNoneMatch = if method ≡ GET ∨ method ≡ HEAD then - NotModified + fromStatusCode NotModified else - PreconditionFailed + fromStatusCode PreconditionFailed -- If-None-Match があればそれを見る。 ifNoneMatch ← getHeader "If-None-Match" @@ -447,15 +453,14 @@ foundTimeStamp timeStamp when (method ≡ POST) $ abort $ mkAbortion' InternalServerError - "Illegal computation of foundTimeStamp for POST request." + "Illegal call of foundTimeStamp for POST request." let statusForIfModSince = if method ≡ GET ∨ method ≡ HEAD then - NotModified + fromStatusCode NotModified else - PreconditionFailed + fromStatusCode PreconditionFailed - -- If-Modified-Since があればそれを見る。 ifModSince ← getHeader "If-Modified-Since" case ifModSince of Just str → case HTTP.fromAscii str of @@ -464,11 +469,11 @@ foundTimeStamp timeStamp $ abort $ mkAbortion' statusForIfModSince $ "The entity has not been modified since " ⊕ A.toText str - Left _ - → return () -- 不正な時刻は無視 + Left e + → abort $ mkAbortion' BadRequest + $ "Malformed If-Modified-Since: " ⊕ T.pack e Nothing → return () - -- If-Unmodified-Since があればそれを見る。 ifUnmodSince ← getHeader "If-Unmodified-Since" case ifUnmodSince of Just str → case HTTP.fromAscii str of @@ -477,8 +482,9 @@ foundTimeStamp timeStamp $ abort $ mkAbortion' PreconditionFailed $ "The entity has not been modified since " ⊕ A.toText str - Left _ - → return () -- 不正な時刻は無視 + Left e + → abort $ mkAbortion' BadRequest + $ "Malformed If-Unmodified-Since: " ⊕ T.pack e Nothing → return () driftTo ReceivingBody @@ -580,7 +586,7 @@ getForm limit $ A.toText $ A.fromAsciiBuilder $ A.toAsciiBuilder "Unsupported media type: " - ⊕ printMIMEType cType + ⊕ MT.printMIMEType cType where readWWWFormURLEncoded = (map toPairWithFormData ∘ parseWWWFormURLEncoded) @@ -609,16 +615,16 @@ getForm limit -- |@'redirect' code uri@ declares the response status as @code@ and -- \"Location\" header field as @uri@. The @code@ must satisfy -- 'isRedirection' or it raises an error. -redirect ∷ StatusCode → URI → Resource () -redirect code uri - = do when (code ≡ NotModified ∨ not (isRedirection code)) +redirect ∷ StatusCode sc ⇒ sc → URI → Resource () +redirect sc uri + = do when (sc ≈ NotModified ∨ (¬) (isRedirection sc)) $ abort $ mkAbortion' InternalServerError $ A.toText $ A.fromAsciiBuilder $ A.toAsciiBuilder "Attempted to redirect with status " - ⊕ printStatusCode code - setStatus code + ⊕ printStatusCode sc + setStatus sc setLocation uri -- |@'setContentType' mType@ declares the response header @@ -626,7 +632,7 @@ redirect code uri -- mandatory for sending a response body. setContentType ∷ MIMEType → Resource () setContentType - = setHeader "Content-Type" ∘ A.fromAsciiBuilder ∘ printMIMEType + = setHeader "Content-Type" ∘ A.fromAsciiBuilder ∘ MT.printMIMEType -- |@'setLocation' uri@ declares the response header \"Location\" as -- @uri@. You usually don't need to call this function directly.