X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FRequest.hs;h=58286dbe6130733fcdc4b19057a061410df43a29;hb=a362be1c8664306b970c32e1df9b62081498feb1;hp=d23dc6331790455b347f26f03371126ed069ee30;hpb=ca338174155913a969808d7b20193973394e474e;p=Lucu.git diff --git a/Network/HTTP/Lucu/Request.hs b/Network/HTTP/Lucu/Request.hs index d23dc63..58286db 100644 --- a/Network/HTTP/Lucu/Request.hs +++ b/Network/HTTP/Lucu/Request.hs @@ -9,8 +9,8 @@ module Network.HTTP.Lucu.Request ( Method(..) , Request(..) - , reqHasBody - , requestP + , reqMustHaveBody + , request ) where import Control.Applicative @@ -25,7 +25,7 @@ import Network.URI import Prelude.Unicode -- |This is the definition of HTTP request methods, which shouldn't --- require any description. +-- require any descriptions. data Method = OPTIONS | GET | HEAD @@ -37,7 +37,7 @@ data Method = OPTIONS | ExtensionMethod !Ascii deriving (Eq, Show) --- |This is the definition of HTTP reqest. +-- |This is the definition of an HTTP reqest. data Request = Request { reqMethod ∷ !Method @@ -48,50 +48,56 @@ data Request deriving (Eq, Show) instance HasHeaders Request where + {-# INLINE getHeaders #-} getHeaders = reqHeaders + {-# INLINE setHeaders #-} setHeaders req hdr = req { reqHeaders = hdr } -- |Returns 'True' iff the 'Request' must have an entity body. -reqHasBody ∷ Request → Bool -reqHasBody (reqMethod → m) - = m ≡ POST ∨ m ≡ PUT +reqMustHaveBody ∷ Request → Bool +{-# INLINEABLE reqMustHaveBody #-} +reqMustHaveBody (reqMethod → m) + | m ≡ POST = True + | m ≡ PUT = True + | otherwise = False -requestP ∷ Parser Request -requestP = do skipMany crlf - (method, uri, version) ← requestLineP - headers ← headersP - return Request { - reqMethod = method - , reqURI = uri - , reqVersion = version - , reqHeaders = headers - } +-- |'Parser' for a 'Request'. +request ∷ Parser Request +request = do skipMany crlf + (meth, u, ver) ← requestLine + hdrs ← headers + return Request { + reqMethod = meth + , reqURI = u + , reqVersion = ver + , reqHeaders = hdrs + } -requestLineP ∷ Parser (Method, URI, HttpVersion) -requestLineP = do method ← methodP - sp - uri ← uriP - sp - ver ← httpVersionP - crlf - return (method, uri, ver) +requestLine ∷ Parser (Method, URI, HttpVersion) +requestLine = do meth ← method + sp + u ← uri + sp + ver ← httpVersion + crlf + return (meth, u, ver) -methodP ∷ Parser Method -methodP = choice - [ string "OPTIONS" ≫ return OPTIONS - , string "GET" ≫ return GET - , string "HEAD" ≫ return HEAD - , string "POST" ≫ return POST - , string "PUT" ≫ return PUT - , string "DELETE" ≫ return DELETE - , string "TRACE" ≫ return TRACE - , string "CONNECT" ≫ return CONNECT - , ExtensionMethod <$> token - ] +method ∷ Parser Method +method = choice + [ string "OPTIONS" ≫ return OPTIONS + , string "GET" ≫ return GET + , string "HEAD" ≫ return HEAD + , string "POST" ≫ return POST + , string "PUT" ≫ return PUT + , string "DELETE" ≫ return DELETE + , string "TRACE" ≫ return TRACE + , string "CONNECT" ≫ return CONNECT + , ExtensionMethod <$> token + ] -uriP ∷ Parser URI -uriP = do bs ← takeWhile1 (\c → (¬) (isCtl c ∨ c ≡ '\x20')) - let str = C8.unpack bs - case parseURIReference str of - Nothing -> fail ("Unparsable URI: " ⧺ str) - Just uri -> return uri +uri ∷ Parser URI +uri = do bs ← takeWhile1 (\c → (¬) (isCtl c ∨ c ≡ '\x20')) + let str = C8.unpack bs + case parseURIReference str of + Nothing → fail ("Unparsable URI: " ⧺ str) + Just u → return u