X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FHttpVersion.hs;h=36b6c499b1428e48f9ea4c0c1a383fbeab8f8026;hb=a362be1c8664306b970c32e1df9b62081498feb1;hp=e1ed0f38024fd9836629c84eee5d9bef20fd46cc;hpb=8e78bc83bfe67a376293c346ae0b30f1a684c787;p=Lucu.git diff --git a/Network/HTTP/Lucu/HttpVersion.hs b/Network/HTTP/Lucu/HttpVersion.hs index e1ed0f3..36b6c49 100644 --- a/Network/HTTP/Lucu/HttpVersion.hs +++ b/Network/HTTP/Lucu/HttpVersion.hs @@ -1,22 +1,26 @@ --- #prune - --- |Manipulation of HTTP version string. +{-# LANGUAGE + OverloadedStrings + , UnicodeSyntax + #-} +-- |HTTP version number module Network.HTTP.Lucu.HttpVersion ( HttpVersion(..) - , httpVersionP - , hPutHttpVersion + , printHttpVersion + , httpVersion ) where - -import Network.HTTP.Lucu.Parser -import System.IO - --- |@'HttpVersion' major minor@ represents \"HTTP\/major.minor\". -data HttpVersion = HttpVersion !Int !Int - deriving (Eq) - -instance Show HttpVersion where - show (HttpVersion maj min) = "HTTP/" ++ show maj ++ "." ++ show min +import Control.Applicative +import Control.Applicative.Unicode +import Data.Ascii (AsciiBuilder) +import qualified Data.Ascii as A +import Data.Attoparsec.Char8 +import Data.Monoid.Unicode +import Prelude hiding (min) + +-- |An HTTP version consists of major and minor versions. +data HttpVersion + = HttpVersion !Int !Int + deriving (Eq, Show) instance Ord HttpVersion where (HttpVersion majA minA) `compare` (HttpVersion majB minB) @@ -26,19 +30,22 @@ instance Ord HttpVersion where | minA < minB = LT | otherwise = EQ - -httpVersionP :: Parser HttpVersion -httpVersionP = do string "HTTP/" - major <- many1 digit - char '.' - minor <- many1 digit - return $ HttpVersion (read major) (read minor) - - -hPutHttpVersion :: Handle -> HttpVersion -> IO () -hPutHttpVersion h (HttpVersion maj min) - = h `seq` - do hPutStr h "HTTP/" - hPutStr h (show maj) - hPutChar h '.' - hPutStr h (show min) \ No newline at end of file +-- |Convert an 'HttpVersion' to 'AsciiBuilder'. +printHttpVersion ∷ HttpVersion → AsciiBuilder +printHttpVersion v + = case v of + -- Optimisation for special cases. + HttpVersion 1 0 → A.toAsciiBuilder "HTTP/1.0" + HttpVersion 1 1 → A.toAsciiBuilder "HTTP/1.1" + -- General (but almost never stumbling) cases. + HttpVersion maj min + → A.toAsciiBuilder "HTTP/" ⊕ + A.toAsciiBuilder (A.unsafeFromString $ show maj) ⊕ + A.toAsciiBuilder "." ⊕ + A.toAsciiBuilder (A.unsafeFromString $ show min) + +-- |'Parser' for an 'HttpVersion'. +httpVersion ∷ Parser HttpVersion +httpVersion = string "HTTP/" + *> + (HttpVersion <$> decimal ⊛ (char '.' *> decimal))