X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FHttpVersion.hs;h=4466f1ecda8959aa102e76e6137cd57c8b53e420;hb=cc074d0ce3f7df2544bc2baddca4e7730ecdf0a0;hp=e0694f151d97db6a1ff3ddd44fa4c4a0293faf1d;hpb=3d017dd65ddede9a11c5b7a34a91e04340e67bc4;p=Lucu.git diff --git a/Network/HTTP/Lucu/HttpVersion.hs b/Network/HTTP/Lucu/HttpVersion.hs index e0694f1..4466f1e 100644 --- a/Network/HTTP/Lucu/HttpVersion.hs +++ b/Network/HTTP/Lucu/HttpVersion.hs @@ -1,24 +1,26 @@ --- #prune - --- |Manipulation of HTTP version string. +{-# LANGUAGE + OverloadedStrings + , UnicodeSyntax + #-} +-- |An internal module for HTTP version numbers. module Network.HTTP.Lucu.HttpVersion ( HttpVersion(..) - , httpVersionP - , hPutHttpVersion + , printHttpVersion + , httpVersion ) where - -import qualified Data.ByteString.Char8 as C8 -import Network.HTTP.Lucu.Parser -import Prelude hiding (min) -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) @@ -28,23 +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) - where - read' "1" = 1 -- この二つが - read' "0" = 0 -- 壓倒的に頻出する - read' s = read s - - -hPutHttpVersion :: Handle -> HttpVersion -> IO () -hPutHttpVersion h (HttpVersion maj min) - = h `seq` - do C8.hPut h (C8.pack "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))