]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - Network/HTTP/Lucu/Request.hs
Code reorganisation
[Lucu.git] / Network / HTTP / Lucu / Request.hs
index 58286dbe6130733fcdc4b19057a061410df43a29..ea855ba30cffc108ac7764766196b63a03ff8ce0 100644 (file)
@@ -1,16 +1,16 @@
 {-# LANGUAGE
-    OverloadedStrings
+    FlexibleInstances
+  , MultiParamTypeClasses
+  , OverloadedStrings
+  , TypeSynonymInstances
   , UnicodeSyntax
   , ViewPatterns
   #-}
--- |Definition of things related on HTTP request.
---
--- In general you don't have to use this module directly.
+-- |Definition of HTTP requests.
 module Network.HTTP.Lucu.Request
     ( Method(..)
     , Request(..)
-    , reqMustHaveBody
-    , request
+    , reqHasBody
     )
     where
 import Control.Applicative
@@ -18,14 +18,14 @@ import Control.Monad.Unicode
 import Data.Ascii (Ascii)
 import Data.Attoparsec.Char8
 import qualified Data.ByteString.Char8 as C8
+import Data.Default
 import Network.HTTP.Lucu.Headers
 import Network.HTTP.Lucu.HttpVersion
 import Network.HTTP.Lucu.Parser.Http
 import Network.URI
 import Prelude.Unicode
 
--- |This is the definition of HTTP request methods, which shouldn't
--- require any descriptions.
+-- |Definition of HTTP request methods.
 data Method = OPTIONS
             | GET
             | HEAD
@@ -37,7 +37,7 @@ data Method = OPTIONS
             | ExtensionMethod !Ascii
               deriving (Eq, Show)
 
--- |This is the definition of an HTTP reqest.
+-- |Definition of HTTP requests.
 data Request
     = Request {
         reqMethod  ∷ !Method
@@ -53,19 +53,33 @@ instance HasHeaders Request where
     {-# INLINE setHeaders #-}
     setHeaders req hdr = req { reqHeaders = hdr }
 
--- |Returns 'True' iff the 'Request' must have an entity body.
-reqMustHaveBody ∷ Request → Bool
-{-# INLINEABLE reqMustHaveBody #-}
-reqMustHaveBody (reqMethod → m)
+-- |Returns 'True' iff the 'Request' would have an entity body.
+reqHasBody ∷ Request → Bool
+{-# INLINEABLE reqHasBody #-}
+reqHasBody (reqMethod → m)
     | m ≡ POST  = True
     | m ≡ PUT   = True
     | otherwise = False
 
--- |'Parser' for a 'Request'.
-request ∷ Parser Request
-request = do skipMany crlf
+instance Default (Parser Method) where
+    {-# INLINEABLE def #-}
+    def = 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
+          ]
+
+instance Default (Parser Request) where
+    {-# INLINEABLE def #-}
+    def = do skipMany crlf
              (meth, u, ver) ← requestLine
-             hdrs           ← headers
+             hdrs           ← def
              return Request {
                           reqMethod  = meth
                         , reqURI     = u
@@ -74,28 +88,17 @@ request = do skipMany crlf
                         }
 
 requestLine ∷ Parser (Method, URI, HttpVersion)
-requestLine = do meth ← method
+{-# INLINEABLE requestLine #-}
+requestLine = do meth ← def
                  sp
                  u ← uri
                  sp
-                 ver ← httpVersion
+                 ver ← def
                  crlf
                  return (meth, u, ver)
 
-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
-         ]
-
 uri ∷ Parser URI
+{-# INLINEABLE uri #-}
 uri = do bs ← takeWhile1 (\c → (¬) (isCtl c ∨ c ≡ '\x20'))
          let str = C8.unpack bs
          case parseURIReference str of